12bf55b39SIngo Weinhold/*
22bf55b39SIngo Weinhold * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
386a5bc31SRene Gollent * Copyright 2013-2015, Rene Gollent, rene@gollent.com.
42bf55b39SIngo Weinhold * Distributed under the terms of the MIT License.
52bf55b39SIngo Weinhold */
62bf55b39SIngo Weinhold
72bf55b39SIngo Weinhold
82bf55b39SIngo Weinhold#include "TeamSettings.h"
92bf55b39SIngo Weinhold
102bf55b39SIngo Weinhold#include <new>
112bf55b39SIngo Weinhold
122bf55b39SIngo Weinhold#include <Message.h>
132bf55b39SIngo Weinhold
142bf55b39SIngo Weinhold#include <AutoLocker.h>
152bf55b39SIngo Weinhold
162bf55b39SIngo Weinhold#include "ArchivingUtils.h"
172bf55b39SIngo Weinhold#include "BreakpointSetting.h"
182bf55b39SIngo Weinhold#include "Team.h"
19b9b12613SRene Gollent#include "TeamFileManagerSettings.h"
2086a5bc31SRene Gollent#include "TeamSignalSettings.h"
21f4b1ddb5SIngo Weinhold#include "TeamUiSettings.h"
22f4b1ddb5SIngo Weinhold#include "TeamUiSettingsFactory.h"
232bf55b39SIngo Weinhold#include "UserBreakpoint.h"
242bf55b39SIngo Weinhold
252bf55b39SIngo Weinhold
262bf55b39SIngo WeinholdTeamSettings::TeamSettings()
272bf55b39SIngo Weinhold{
28b9b12613SRene Gollent	fFileManagerSettings = new TeamFileManagerSettings();
2986a5bc31SRene Gollent	fSignalSettings = new TeamSignalSettings();
302bf55b39SIngo Weinhold}
312bf55b39SIngo Weinhold
322bf55b39SIngo Weinhold
332bf55b39SIngo WeinholdTeamSettings::TeamSettings(const TeamSettings& other)
342bf55b39SIngo Weinhold{
352bf55b39SIngo Weinhold	try {
362bf55b39SIngo Weinhold		*this = other;
372bf55b39SIngo Weinhold	} catch (...) {
382bf55b39SIngo Weinhold		_Unset();
392bf55b39SIngo Weinhold		throw;
402bf55b39SIngo Weinhold	}
412bf55b39SIngo Weinhold}
422bf55b39SIngo Weinhold
432bf55b39SIngo Weinhold
442bf55b39SIngo WeinholdTeamSettings::~TeamSettings()
452bf55b39SIngo Weinhold{
462bf55b39SIngo Weinhold	_Unset();
473995592cSRene Gollent	delete fFileManagerSettings;
483995592cSRene Gollent	delete fSignalSettings;
492bf55b39SIngo Weinhold}
502bf55b39SIngo Weinhold
512bf55b39SIngo Weinhold
522bf55b39SIngo Weinholdstatus_t
532bf55b39SIngo WeinholdTeamSettings::SetTo(Team* team)
542bf55b39SIngo Weinhold{
552bf55b39SIngo Weinhold	_Unset();
562bf55b39SIngo Weinhold
572bf55b39SIngo Weinhold	AutoLocker<Team> locker(team);
582bf55b39SIngo Weinhold
592bf55b39SIngo Weinhold	fTeamName = team->Name();
602bf55b39SIngo Weinhold
612bf55b39SIngo Weinhold	// add breakpoints
622bf55b39SIngo Weinhold	for (UserBreakpointList::ConstIterator it
632bf55b39SIngo Weinhold			= team->UserBreakpoints().GetIterator();
642bf55b39SIngo Weinhold		UserBreakpoint* breakpoint = it.Next();) {
652bf55b39SIngo Weinhold		BreakpointSetting* breakpointSetting
662bf55b39SIngo Weinhold			= new(std::nothrow) BreakpointSetting;
672bf55b39SIngo Weinhold		if (breakpointSetting == NULL)
682bf55b39SIngo Weinhold			return B_NO_MEMORY;
692bf55b39SIngo Weinhold
702bf55b39SIngo Weinhold		status_t error = breakpointSetting->SetTo(breakpoint->Location(),
7165a10b50SRene Gollent			breakpoint->IsEnabled(), breakpoint->IsHidden(),
7265a10b50SRene Gollent			breakpoint->Condition());
732bf55b39SIngo Weinhold		if (error == B_OK && !fBreakpoints.AddItem(breakpointSetting))
742bf55b39SIngo Weinhold			error = B_NO_MEMORY;
752bf55b39SIngo Weinhold		if (error != B_OK) {
762bf55b39SIngo Weinhold			delete breakpointSetting;
772bf55b39SIngo Weinhold			return error;
782bf55b39SIngo Weinhold		}
792bf55b39SIngo Weinhold	}
802bf55b39SIngo Weinhold
8186a5bc31SRene Gollent	// add signal configuration
8286a5bc31SRene Gollent
8386a5bc31SRene Gollent	fSignalSettings->SetDefaultSignalDisposition(
8486a5bc31SRene Gollent		team->DefaultSignalDisposition());
8586a5bc31SRene Gollent
8686a5bc31SRene Gollent	const SignalDispositionMappings& mappings
8786a5bc31SRene Gollent		= team->GetSignalDispositionMappings();
8886a5bc31SRene Gollent
8986a5bc31SRene Gollent	for (SignalDispositionMappings::const_iterator it = mappings.begin();
9086a5bc31SRene Gollent		it != mappings.end(); ++it) {
9186a5bc31SRene Gollent		status_t error = fSignalSettings->AddCustomSignalDisposition(
9286a5bc31SRene Gollent			it->first, it->second);
9386a5bc31SRene Gollent		if (error != B_OK)
9486a5bc31SRene Gollent			return error;
9586a5bc31SRene Gollent	}
9686a5bc31SRene Gollent
972bf55b39SIngo Weinhold	return B_OK;
982bf55b39SIngo Weinhold}
992bf55b39SIngo Weinhold
1002bf55b39SIngo Weinhold
1012bf55b39SIngo Weinholdstatus_t
102ec60909aSRene GollentTeamSettings::SetTo(const BMessage& archive,
103ec60909aSRene Gollent	const TeamUiSettingsFactory& factory)
1042bf55b39SIngo Weinhold{
1052bf55b39SIngo Weinhold	_Unset();
1062bf55b39SIngo Weinhold
1072bf55b39SIngo Weinhold	status_t error = archive.FindString("teamName", &fTeamName);
1082bf55b39SIngo Weinhold	if (error != B_OK)
1092bf55b39SIngo Weinhold		return error;
1102bf55b39SIngo Weinhold
1112bf55b39SIngo Weinhold	// add breakpoints
1122bf55b39SIngo Weinhold	BMessage childArchive;
1132bf55b39SIngo Weinhold	for (int32 i = 0; archive.FindMessage("breakpoints", i, &childArchive)
1142bf55b39SIngo Weinhold			== B_OK; i++) {
1152bf55b39SIngo Weinhold		BreakpointSetting* breakpointSetting
1162bf55b39SIngo Weinhold			= new(std::nothrow) BreakpointSetting;
1172bf55b39SIngo Weinhold		if (breakpointSetting == NULL)
1182bf55b39SIngo Weinhold			return B_NO_MEMORY;
1192bf55b39SIngo Weinhold
1202bf55b39SIngo Weinhold		error = breakpointSetting->SetTo(childArchive);
1212bf55b39SIngo Weinhold		if (error == B_OK && !fBreakpoints.AddItem(breakpointSetting))
1222bf55b39SIngo Weinhold			error = B_NO_MEMORY;
1232bf55b39SIngo Weinhold		if (error != B_OK) {
1242bf55b39SIngo Weinhold			delete breakpointSetting;
1252bf55b39SIngo Weinhold			return error;
1262bf55b39SIngo Weinhold		}
1272bf55b39SIngo Weinhold	}
128bbc85109SRene Gollent
1290d3e3475SRene Gollent	// add UI settings
1300d3e3475SRene Gollent	for (int32 i = 0; archive.FindMessage("uisettings", i, &childArchive)
1310d3e3475SRene Gollent		== B_OK; i++) {
132f4b1ddb5SIngo Weinhold		TeamUiSettings* setting = NULL;
133ec60909aSRene Gollent		error = factory.Create(childArchive, setting);
134f4b1ddb5SIngo Weinhold		if (error == B_OK && !fUiSettings.AddItem(setting))
13576ce2c7eSRene Gollent			error = B_NO_MEMORY;
13676ce2c7eSRene Gollent		if (error != B_OK) {
13776ce2c7eSRene Gollent			delete setting;
13876ce2c7eSRene Gollent			return error;
139bbc85109SRene Gollent		}
1400d3e3475SRene Gollent	}
1412bf55b39SIngo Weinhold
142b9b12613SRene Gollent	if (archive.FindMessage("filemanagersettings", &childArchive) == B_OK) {
143b9b12613SRene Gollent		error = fFileManagerSettings->SetTo(childArchive);
144b9b12613SRene Gollent		if (error != B_OK)
145b9b12613SRene Gollent			return error;
146b9b12613SRene Gollent	}
147b9b12613SRene Gollent
14886a5bc31SRene Gollent	if (archive.FindMessage("signalsettings", &childArchive) == B_OK) {
14986a5bc31SRene Gollent		error = fSignalSettings->SetTo(childArchive);
15086a5bc31SRene Gollent		if (error != B_OK)
15186a5bc31SRene Gollent			return error;
15286a5bc31SRene Gollent	}
15386a5bc31SRene Gollent
1542bf55b39SIngo Weinhold	return B_OK;
1552bf55b39SIngo Weinhold}
1562bf55b39SIngo Weinhold
1572bf55b39SIngo Weinhold
1582bf55b39SIngo Weinholdstatus_t
1592bf55b39SIngo WeinholdTeamSettings::WriteTo(BMessage& archive) const
1602bf55b39SIngo Weinhold{
1612bf55b39SIngo Weinhold	status_t error = archive.AddString("teamName", fTeamName);
1622bf55b39SIngo Weinhold	if (error != B_OK)
1632bf55b39SIngo Weinhold		return error;
1642bf55b39SIngo Weinhold
16576ce2c7eSRene Gollent	BMessage childArchive;
1662bf55b39SIngo Weinhold	for (int32 i = 0; BreakpointSetting* breakpoint = fBreakpoints.ItemAt(i);
1672bf55b39SIngo Weinhold			i++) {
1682bf55b39SIngo Weinhold		error = breakpoint->WriteTo(childArchive);
1692bf55b39SIngo Weinhold		if (error != B_OK)
1702bf55b39SIngo Weinhold			return error;
1712bf55b39SIngo Weinhold
1722bf55b39SIngo Weinhold		error = archive.AddMessage("breakpoints", &childArchive);
1732bf55b39SIngo Weinhold		if (error != B_OK)
1742bf55b39SIngo Weinhold			return error;
1752bf55b39SIngo Weinhold	}
176bbc85109SRene Gollent
177f4b1ddb5SIngo Weinhold	for (int32 i = 0; TeamUiSettings* uiSetting = fUiSettings.ItemAt(i);
17876ce2c7eSRene Gollent			i++) {
17976ce2c7eSRene Gollent		error = uiSetting->WriteTo(childArchive);
18076ce2c7eSRene Gollent		if (error != B_OK)
18176ce2c7eSRene Gollent			return error;
182bbc85109SRene Gollent
18376ce2c7eSRene Gollent		error = archive.AddMessage("uisettings", &childArchive);
18476ce2c7eSRene Gollent		if (error != B_OK)
18576ce2c7eSRene Gollent			return error;
18676ce2c7eSRene Gollent	}
1872bf55b39SIngo Weinhold
188b9b12613SRene Gollent	error = fFileManagerSettings->WriteTo(childArchive);
189b9b12613SRene Gollent	if (error != B_OK)
190b9b12613SRene Gollent		return error;
191b9b12613SRene Gollent
192b9b12613SRene Gollent	error = archive.AddMessage("filemanagersettings", &childArchive);
193b9b12613SRene Gollent	if (error != B_OK)
194b9b12613SRene Gollent		return error;
195b9b12613SRene Gollent
19686a5bc31SRene Gollent	error = fSignalSettings->WriteTo(childArchive);
19786a5bc31SRene Gollent	if (error != B_OK)
19886a5bc31SRene Gollent		return error;
19986a5bc31SRene Gollent
20086a5bc31SRene Gollent	error = archive.AddMessage("signalsettings", &childArchive);
20186a5bc31SRene Gollent	if (error != B_OK)
20286a5bc31SRene Gollent		return error;
20386a5bc31SRene Gollent
2042bf55b39SIngo Weinhold	return B_OK;
2052bf55b39SIngo Weinhold}
2062bf55b39SIngo Weinhold
2072bf55b39SIngo Weinhold
2082bf55b39SIngo Weinholdint32
2092bf55b39SIngo WeinholdTeamSettings::CountBreakpoints() const
2102bf55b39SIngo Weinhold{
2112bf55b39SIngo Weinhold	return fBreakpoints.CountItems();
2122bf55b39SIngo Weinhold}
2132bf55b39SIngo Weinhold
2142bf55b39SIngo Weinhold
2152bf55b39SIngo Weinholdconst BreakpointSetting*
2162bf55b39SIngo WeinholdTeamSettings::BreakpointAt(int32 index) const
2172bf55b39SIngo Weinhold{
2182bf55b39SIngo Weinhold	return fBreakpoints.ItemAt(index);
2192bf55b39SIngo Weinhold}
2202bf55b39SIngo Weinhold
2212bf55b39SIngo Weinhold
2220d3e3475SRene Gollentint32
223f4b1ddb5SIngo WeinholdTeamSettings::CountUiSettings() const
2240d3e3475SRene Gollent{
225f4b1ddb5SIngo Weinhold	return fUiSettings.CountItems();
2260d3e3475SRene Gollent}
2270d3e3475SRene Gollent
2280d3e3475SRene Gollent
229f4b1ddb5SIngo Weinholdconst TeamUiSettings*
230f4b1ddb5SIngo WeinholdTeamSettings::UiSettingAt(int32 index) const
2310d3e3475SRene Gollent{
232f4b1ddb5SIngo Weinhold	return fUiSettings.ItemAt(index);
2330d3e3475SRene Gollent}
2340d3e3475SRene Gollent
2350d3e3475SRene Gollent
236f4b1ddb5SIngo Weinholdconst TeamUiSettings*
237f4b1ddb5SIngo WeinholdTeamSettings::UiSettingFor(const char* id) const
238bbc85109SRene Gollent{
239f4b1ddb5SIngo Weinhold	for (int32 i = 0; i < fUiSettings.CountItems(); i++) {
240f4b1ddb5SIngo Weinhold		TeamUiSettings* settings = fUiSettings.ItemAt(i);
241bbc85109SRene Gollent		if (strcmp(settings->ID(), id) == 0)
242bbc85109SRene Gollent			return settings;
243bbc85109SRene Gollent	}
244bbc85109SRene Gollent
245bbc85109SRene Gollent	return NULL;
246bbc85109SRene Gollent}
247bbc85109SRene Gollent
248bbc85109SRene Gollent
249bbc85109SRene Gollentstatus_t
250f4b1ddb5SIngo WeinholdTeamSettings::AddUiSettings(TeamUiSettings* settings)
251bbc85109SRene Gollent{
252f4b1ddb5SIngo Weinhold	if (!fUiSettings.AddItem(settings))
253bbc85109SRene Gollent		return B_NO_MEMORY;
254bbc85109SRene Gollent
255bbc85109SRene Gollent	return B_OK;
256bbc85109SRene Gollent}
257bbc85109SRene Gollent
258bbc85109SRene Gollent
2592bf55b39SIngo WeinholdTeamSettings&
2602bf55b39SIngo WeinholdTeamSettings::operator=(const TeamSettings& other)
2612bf55b39SIngo Weinhold{
2622bf55b39SIngo Weinhold	if (this == &other)
2632bf55b39SIngo Weinhold		return *this;
2642bf55b39SIngo Weinhold
2652bf55b39SIngo Weinhold	_Unset();
2662bf55b39SIngo Weinhold
2672bf55b39SIngo Weinhold	fTeamName = other.fTeamName;
2682bf55b39SIngo Weinhold
2692bf55b39SIngo Weinhold	for (int32 i = 0; BreakpointSetting* breakpoint
2702bf55b39SIngo Weinhold			= other.fBreakpoints.ItemAt(i); i++) {
2712bf55b39SIngo Weinhold		BreakpointSetting* clonedBreakpoint
2722bf55b39SIngo Weinhold			= new BreakpointSetting(*breakpoint);
2732bf55b39SIngo Weinhold		if (!fBreakpoints.AddItem(clonedBreakpoint)) {
2742bf55b39SIngo Weinhold			delete clonedBreakpoint;
2752bf55b39SIngo Weinhold			throw std::bad_alloc();
2762bf55b39SIngo Weinhold		}
2772bf55b39SIngo Weinhold	}
2782bf55b39SIngo Weinhold
279f4b1ddb5SIngo Weinhold	for (int32 i = 0; TeamUiSettings* uiSetting
280f4b1ddb5SIngo Weinhold			= other.fUiSettings.ItemAt(i); i++) {
281f4b1ddb5SIngo Weinhold		TeamUiSettings* clonedSetting
2820d3e3475SRene Gollent			= uiSetting->Clone();
283f4b1ddb5SIngo Weinhold		if (!fUiSettings.AddItem(clonedSetting)) {
2840d3e3475SRene Gollent			delete clonedSetting;
2850d3e3475SRene Gollent			throw std::bad_alloc();
2860d3e3475SRene Gollent		}
2870d3e3475SRene Gollent	}
2880d3e3475SRene Gollent
289b9b12613SRene Gollent	*fFileManagerSettings = *other.fFileManagerSettings;
290b9b12613SRene Gollent
29186a5bc31SRene Gollent	*fSignalSettings = *other.fSignalSettings;
29286a5bc31SRene Gollent
2932bf55b39SIngo Weinhold	return *this;
2942bf55b39SIngo Weinhold}
2952bf55b39SIngo Weinhold
2962bf55b39SIngo Weinhold
297b9b12613SRene GollentTeamFileManagerSettings*
298b9b12613SRene GollentTeamSettings::FileManagerSettings() const
299b9b12613SRene Gollent{
300b9b12613SRene Gollent	return fFileManagerSettings;
301b9b12613SRene Gollent}
302b9b12613SRene Gollent
303b9b12613SRene Gollent
304b9b12613SRene Gollentstatus_t
305b9b12613SRene GollentTeamSettings::SetFileManagerSettings(TeamFileManagerSettings* settings)
306b9b12613SRene Gollent{
307b9b12613SRene Gollent	try {
308b9b12613SRene Gollent		*fFileManagerSettings = *settings;
309b9b12613SRene Gollent	} catch (...) {
310b9b12613SRene Gollent		return B_NO_MEMORY;
311b9b12613SRene Gollent	}
312b9b12613SRene Gollent
313b9b12613SRene Gollent	return B_OK;
314b9b12613SRene Gollent}
315b9b12613SRene Gollent
316b9b12613SRene Gollent
31786a5bc31SRene GollentTeamSignalSettings*
31886a5bc31SRene GollentTeamSettings::SignalSettings() const
31986a5bc31SRene Gollent{
32086a5bc31SRene Gollent	return fSignalSettings;
32186a5bc31SRene Gollent}
32286a5bc31SRene Gollent
32386a5bc31SRene Gollent
32486a5bc31SRene Gollentstatus_t
32586a5bc31SRene GollentTeamSettings::SetSignalSettings(TeamSignalSettings* settings)
32686a5bc31SRene Gollent{
32786a5bc31SRene Gollent	try {
32886a5bc31SRene Gollent		*fSignalSettings = *settings;
32986a5bc31SRene Gollent	} catch (...) {
33086a5bc31SRene Gollent		return B_NO_MEMORY;
33186a5bc31SRene Gollent	}
33286a5bc31SRene Gollent
33386a5bc31SRene Gollent	return B_OK;
33486a5bc31SRene Gollent}
33586a5bc31SRene Gollent
33686a5bc31SRene Gollent
3372bf55b39SIngo Weinholdvoid
3382bf55b39SIngo WeinholdTeamSettings::_Unset()
3392bf55b39SIngo Weinhold{
3402bf55b39SIngo Weinhold	for (int32 i = 0; BreakpointSetting* breakpoint = fBreakpoints.ItemAt(i);
3412bf55b39SIngo Weinhold			i++) {
3422bf55b39SIngo Weinhold		delete breakpoint;
3432bf55b39SIngo Weinhold	}
344bbc85109SRene Gollent
345f4b1ddb5SIngo Weinhold	for (int32 i = 0; TeamUiSettings* uiSetting = fUiSettings.ItemAt(i); i++)
3460d3e3475SRene Gollent		delete uiSetting;
3470d3e3475SRene Gollent
3482bf55b39SIngo Weinhold	fBreakpoints.MakeEmpty();
349f4b1ddb5SIngo Weinhold	fUiSettings.MakeEmpty();
35086a5bc31SRene Gollent	fSignalSettings->Unset();
3512bf55b39SIngo Weinhold
3522bf55b39SIngo Weinhold	fTeamName.Truncate(0);
3532bf55b39SIngo Weinhold}
354