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