2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
7 #include "SettingsManager.h"
11 #include <Directory.h>
13 #include <FindDirectory.h>
15 #include <AutoDeleter.h>
16 #include <AutoLocker.h>
18 #include "TeamSettings.h"
21 static const char* const kSettingsDirPath
= "Debugger";
22 static const char* const kGlobalSettingsName
= "Global";
23 static const int32 kMaxRecentTeamSettings
= 10;
26 SettingsManager::SettingsManager()
28 fLock("settings manager"),
29 fRecentTeamSettings(kMaxRecentTeamSettings
, true),
30 fUiSettingsFactory(NULL
)
35 SettingsManager::~SettingsManager()
42 SettingsManager::Init(TeamUiSettingsFactory
* factory
)
45 status_t error
= fLock
.InitCheck();
49 fUiSettingsFactory
= factory
;
51 // get and create our settings directory
52 if (find_directory(B_USER_SETTINGS_DIRECTORY
, &fSettingsPath
, true) == B_OK
53 && fSettingsPath
.Append(kSettingsDirPath
) == B_OK
54 && create_directory(fSettingsPath
.Path(), 0700) == B_OK
55 && fSettingsPath
.Append(kGlobalSettingsName
) == B_OK
) {
59 // something went wrong -- clear the path
60 fSettingsPath
.Unset();
68 SettingsManager::LoadTeamSettings(const char* teamName
, TeamSettings
& settings
)
70 AutoLocker
<BLocker
> locker(fLock
);
72 int32 index
= _TeamSettingsIndex(teamName
);
74 return B_ENTRY_NOT_FOUND
;
77 settings
= *fRecentTeamSettings
.ItemAt(index
);
79 } catch (std::bad_alloc
) {
86 SettingsManager::SaveTeamSettings(const TeamSettings
& _settings
)
88 AutoLocker
<BLocker
> locker(fLock
);
90 TeamSettings
* settings
;
91 int32 index
= _TeamSettingsIndex(_settings
.TeamName());
93 settings
= fRecentTeamSettings
.RemoveItemAt(index
);
95 settings
= new(std::nothrow
) TeamSettings
;
99 // enforce recent limit
100 while (fRecentTeamSettings
.CountItems() >= kMaxRecentTeamSettings
)
101 delete fRecentTeamSettings
.RemoveItemAt(0);
104 ObjectDeleter
<TeamSettings
> settingsDeleter(settings
);
107 *settings
= _settings
;
108 if (!fRecentTeamSettings
.AddItem(settings
))
110 settingsDeleter
.Detach();
112 return _SaveSettings();
113 } catch (std::bad_alloc
) {
120 SettingsManager::_Unset()
122 fRecentTeamSettings
.MakeEmpty();
127 SettingsManager::_LoadSettings()
131 if (fSettingsPath
.Path() == NULL
)
132 return B_ENTRY_NOT_FOUND
;
134 // read the settings file
136 status_t error
= file
.SetTo(fSettingsPath
.Path(), B_READ_ONLY
);
141 error
= archive
.Unflatten(&file
);
145 // unarchive the recent team settings
146 BMessage childArchive
;
147 for (int32 i
= 0; archive
.FindMessage("teamSettings", i
, &childArchive
)
149 TeamSettings
* settings
= new(std::nothrow
) TeamSettings
;
150 if (settings
== NULL
)
153 error
= settings
->SetTo(childArchive
, *fUiSettingsFactory
);
159 if (!fRecentTeamSettings
.AddItem(settings
)) {
170 SettingsManager::_SaveSettings()
172 if (fSettingsPath
.Path() == NULL
)
173 return B_ENTRY_NOT_FOUND
;
175 // archive the recent team settings
177 for (int32 i
= 0; TeamSettings
* settings
= fRecentTeamSettings
.ItemAt(i
);
179 BMessage childArchive
;
180 status_t error
= settings
->WriteTo(childArchive
);
184 error
= archive
.AddMessage("teamSettings", &childArchive
);
189 // open the settings file
191 status_t error
= file
.SetTo(fSettingsPath
.Path(),
192 B_WRITE_ONLY
| B_CREATE_FILE
| B_ERASE_FILE
);
196 return archive
.Flatten(&file
);
201 SettingsManager::_TeamSettingsIndex(const char* teamName
) const
203 for (int32 i
= 0; TeamSettings
* settings
= fRecentTeamSettings
.ItemAt(i
);
205 if (settings
->TeamName() == teamName
)