1 /* 2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Copyright 2013-2015, Rene Gollent, rene@gollent.com. 4 * Distributed under the terms of the MIT License. 5 */ 6 7 8 #include "TeamSettings.h" 9 10 #include <new> 11 12 #include <Message.h> 13 14 #include <AutoLocker.h> 15 16 #include "ArchivingUtils.h" 17 #include "BreakpointSetting.h" 18 #include "Team.h" 19 #include "TeamFileManagerSettings.h" 20 #include "TeamSignalSettings.h" 21 #include "TeamUiSettings.h" 22 #include "TeamUiSettingsFactory.h" 23 #include "UserBreakpoint.h" 24 25 26 TeamSettings::TeamSettings() 27 { 28 fFileManagerSettings = new TeamFileManagerSettings(); 29 fSignalSettings = new TeamSignalSettings(); 30 } 31 32 33 TeamSettings::TeamSettings(const TeamSettings& other) 34 { 35 try { 36 *this = other; 37 } catch (...) { 38 _Unset(); 39 throw; 40 } 41 } 42 43 44 TeamSettings::~TeamSettings() 45 { 46 _Unset(); 47 } 48 49 50 status_t 51 TeamSettings::SetTo(Team* team) 52 { 53 _Unset(); 54 55 AutoLocker<Team> locker(team); 56 57 fTeamName = team->Name(); 58 59 // add breakpoints 60 for (UserBreakpointList::ConstIterator it 61 = team->UserBreakpoints().GetIterator(); 62 UserBreakpoint* breakpoint = it.Next();) { 63 BreakpointSetting* breakpointSetting 64 = new(std::nothrow) BreakpointSetting; 65 if (breakpointSetting == NULL) 66 return B_NO_MEMORY; 67 68 status_t error = breakpointSetting->SetTo(breakpoint->Location(), 69 breakpoint->IsEnabled(), breakpoint->IsHidden(), 70 breakpoint->Condition()); 71 if (error == B_OK && !fBreakpoints.AddItem(breakpointSetting)) 72 error = B_NO_MEMORY; 73 if (error != B_OK) { 74 delete breakpointSetting; 75 return error; 76 } 77 } 78 79 // add signal configuration 80 81 fSignalSettings->SetDefaultSignalDisposition( 82 team->DefaultSignalDisposition()); 83 84 const SignalDispositionMappings& mappings 85 = team->GetSignalDispositionMappings(); 86 87 for (SignalDispositionMappings::const_iterator it = mappings.begin(); 88 it != mappings.end(); ++it) { 89 status_t error = fSignalSettings->AddCustomSignalDisposition( 90 it->first, it->second); 91 if (error != B_OK) 92 return error; 93 } 94 95 return B_OK; 96 } 97 98 99 status_t 100 TeamSettings::SetTo(const BMessage& archive, 101 const TeamUiSettingsFactory& factory) 102 { 103 _Unset(); 104 105 status_t error = archive.FindString("teamName", &fTeamName); 106 if (error != B_OK) 107 return error; 108 109 // add breakpoints 110 BMessage childArchive; 111 for (int32 i = 0; archive.FindMessage("breakpoints", i, &childArchive) 112 == B_OK; i++) { 113 BreakpointSetting* breakpointSetting 114 = new(std::nothrow) BreakpointSetting; 115 if (breakpointSetting == NULL) 116 return B_NO_MEMORY; 117 118 error = breakpointSetting->SetTo(childArchive); 119 if (error == B_OK && !fBreakpoints.AddItem(breakpointSetting)) 120 error = B_NO_MEMORY; 121 if (error != B_OK) { 122 delete breakpointSetting; 123 return error; 124 } 125 } 126 127 // add UI settings 128 for (int32 i = 0; archive.FindMessage("uisettings", i, &childArchive) 129 == B_OK; i++) { 130 TeamUiSettings* setting = NULL; 131 error = factory.Create(childArchive, setting); 132 if (error == B_OK && !fUiSettings.AddItem(setting)) 133 error = B_NO_MEMORY; 134 if (error != B_OK) { 135 delete setting; 136 return error; 137 } 138 } 139 140 if (archive.FindMessage("filemanagersettings", &childArchive) == B_OK) { 141 error = fFileManagerSettings->SetTo(childArchive); 142 if (error != B_OK) 143 return error; 144 } 145 146 if (archive.FindMessage("signalsettings", &childArchive) == B_OK) { 147 error = fSignalSettings->SetTo(childArchive); 148 if (error != B_OK) 149 return error; 150 } 151 152 return B_OK; 153 } 154 155 156 status_t 157 TeamSettings::WriteTo(BMessage& archive) const 158 { 159 status_t error = archive.AddString("teamName", fTeamName); 160 if (error != B_OK) 161 return error; 162 163 BMessage childArchive; 164 for (int32 i = 0; BreakpointSetting* breakpoint = fBreakpoints.ItemAt(i); 165 i++) { 166 error = breakpoint->WriteTo(childArchive); 167 if (error != B_OK) 168 return error; 169 170 error = archive.AddMessage("breakpoints", &childArchive); 171 if (error != B_OK) 172 return error; 173 } 174 175 for (int32 i = 0; TeamUiSettings* uiSetting = fUiSettings.ItemAt(i); 176 i++) { 177 error = uiSetting->WriteTo(childArchive); 178 if (error != B_OK) 179 return error; 180 181 error = archive.AddMessage("uisettings", &childArchive); 182 if (error != B_OK) 183 return error; 184 } 185 186 error = fFileManagerSettings->WriteTo(childArchive); 187 if (error != B_OK) 188 return error; 189 190 error = archive.AddMessage("filemanagersettings", &childArchive); 191 if (error != B_OK) 192 return error; 193 194 error = fSignalSettings->WriteTo(childArchive); 195 if (error != B_OK) 196 return error; 197 198 error = archive.AddMessage("signalsettings", &childArchive); 199 if (error != B_OK) 200 return error; 201 202 return B_OK; 203 } 204 205 206 int32 207 TeamSettings::CountBreakpoints() const 208 { 209 return fBreakpoints.CountItems(); 210 } 211 212 213 const BreakpointSetting* 214 TeamSettings::BreakpointAt(int32 index) const 215 { 216 return fBreakpoints.ItemAt(index); 217 } 218 219 220 int32 221 TeamSettings::CountUiSettings() const 222 { 223 return fUiSettings.CountItems(); 224 } 225 226 227 const TeamUiSettings* 228 TeamSettings::UiSettingAt(int32 index) const 229 { 230 return fUiSettings.ItemAt(index); 231 } 232 233 234 const TeamUiSettings* 235 TeamSettings::UiSettingFor(const char* id) const 236 { 237 for (int32 i = 0; i < fUiSettings.CountItems(); i++) { 238 TeamUiSettings* settings = fUiSettings.ItemAt(i); 239 if (strcmp(settings->ID(), id) == 0) 240 return settings; 241 } 242 243 return NULL; 244 } 245 246 247 status_t 248 TeamSettings::AddUiSettings(TeamUiSettings* settings) 249 { 250 if (!fUiSettings.AddItem(settings)) 251 return B_NO_MEMORY; 252 253 return B_OK; 254 } 255 256 257 TeamSettings& 258 TeamSettings::operator=(const TeamSettings& other) 259 { 260 if (this == &other) 261 return *this; 262 263 _Unset(); 264 265 fTeamName = other.fTeamName; 266 267 for (int32 i = 0; BreakpointSetting* breakpoint 268 = other.fBreakpoints.ItemAt(i); i++) { 269 BreakpointSetting* clonedBreakpoint 270 = new BreakpointSetting(*breakpoint); 271 if (!fBreakpoints.AddItem(clonedBreakpoint)) { 272 delete clonedBreakpoint; 273 throw std::bad_alloc(); 274 } 275 } 276 277 for (int32 i = 0; TeamUiSettings* uiSetting 278 = other.fUiSettings.ItemAt(i); i++) { 279 TeamUiSettings* clonedSetting 280 = uiSetting->Clone(); 281 if (!fUiSettings.AddItem(clonedSetting)) { 282 delete clonedSetting; 283 throw std::bad_alloc(); 284 } 285 } 286 287 *fFileManagerSettings = *other.fFileManagerSettings; 288 289 *fSignalSettings = *other.fSignalSettings; 290 291 return *this; 292 } 293 294 295 TeamFileManagerSettings* 296 TeamSettings::FileManagerSettings() const 297 { 298 return fFileManagerSettings; 299 } 300 301 302 status_t 303 TeamSettings::SetFileManagerSettings(TeamFileManagerSettings* settings) 304 { 305 try { 306 *fFileManagerSettings = *settings; 307 } catch (...) { 308 return B_NO_MEMORY; 309 } 310 311 return B_OK; 312 } 313 314 315 TeamSignalSettings* 316 TeamSettings::SignalSettings() const 317 { 318 return fSignalSettings; 319 } 320 321 322 status_t 323 TeamSettings::SetSignalSettings(TeamSignalSettings* settings) 324 { 325 try { 326 *fSignalSettings = *settings; 327 } catch (...) { 328 return B_NO_MEMORY; 329 } 330 331 return B_OK; 332 } 333 334 335 void 336 TeamSettings::_Unset() 337 { 338 for (int32 i = 0; BreakpointSetting* breakpoint = fBreakpoints.ItemAt(i); 339 i++) { 340 delete breakpoint; 341 } 342 343 for (int32 i = 0; TeamUiSettings* uiSetting = fUiSettings.ItemAt(i); i++) 344 delete uiSetting; 345 346 fBreakpoints.MakeEmpty(); 347 fUiSettings.MakeEmpty(); 348 fSignalSettings->Unset(); 349 350 fTeamName.Truncate(0); 351 } 352