1 /* 2 * Copyright 2005-2006, Haiku. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Stephan Aßmus <superstippi@gmx.de> 7 * Axel Dörfler, axeld@pinc-software.de 8 */ 9 10 11 #include "DesktopSettings.h" 12 #include "DesktopSettingsPrivate.h" 13 #include "Desktop.h" 14 #include "FontManager.h" 15 #include "ServerConfig.h" 16 17 #include <Directory.h> 18 #include <File.h> 19 #include <FindDirectory.h> 20 //#include <DataIO./h> 21 #include <Path.h> 22 23 24 DesktopSettings::Private::Private() 25 : BLocker("DesktopSettings_Private") 26 { 27 // if the on-disk settings are not complete, the defaults will be kept 28 _SetDefaults(); 29 _Load(); 30 } 31 32 33 DesktopSettings::Private::~Private() 34 { 35 } 36 37 38 void 39 DesktopSettings::Private::_SetDefaults() 40 { 41 fPlainFont = *gFontManager->DefaultPlainFont(); 42 fBoldFont = *gFontManager->DefaultBoldFont(); 43 fFixedFont = *gFontManager->DefaultFixedFont(); 44 45 fMouseMode = B_NORMAL_MOUSE; 46 47 // init scrollbar info 48 fScrollBarInfo.proportional = true; 49 fScrollBarInfo.double_arrows = false; 50 // look of the knob (R5: (0, 1, 2), 1 = default) 51 fScrollBarInfo.knob = 1; 52 fScrollBarInfo.min_knob_size = 15; 53 54 // init menu info 55 fMenuInfo.font_size = 12.0; 56 // TODO: ... 57 // fMenuInfo.f_family; 58 // fMenuInfo.f_style; 59 // fMenuInfo.background_color = gColorSet->menu_background; 60 // look of the separator (R5: (0, 1, 2), default ?) 61 fMenuInfo.separator = 0; 62 fMenuInfo.click_to_open = true; 63 fMenuInfo.triggers_always_shown = false; 64 65 fWorkspacesCount = 4; 66 } 67 68 69 status_t 70 DesktopSettings::Private::_GetPath(BPath& path) 71 { 72 status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path); 73 if (status < B_OK) 74 return status; 75 76 status = path.Append("system/app_server"); 77 if (status < B_OK) 78 return status; 79 80 return create_directory(path.Path(), 0755); 81 } 82 83 84 status_t 85 DesktopSettings::Private::_Load() 86 { 87 // TODO: add support for old app_server_settings file as well 88 89 BPath basePath; 90 status_t status = _GetPath(basePath); 91 if (status < B_OK) 92 return status; 93 94 // read workspaces settings 95 96 BPath path(basePath); 97 path.Append("workspaces"); 98 99 BFile file; 100 status = file.SetTo(path.Path(), B_READ_ONLY); 101 if (status == B_OK) { 102 BMessage settings; 103 status = settings.Unflatten(&file); 104 if (status == B_OK) { 105 int32 count; 106 if (settings.FindInt32("count", &count) == B_OK) { 107 fWorkspacesCount = count; 108 if (fWorkspacesCount < 1 || fWorkspacesCount > 32) 109 fWorkspacesCount = 4; 110 } 111 112 int32 i = 0; 113 while (i < 32 114 && settings.FindMessage("workspace", i, &fWorkspaceMessages[i]) == B_OK) { 115 i++; 116 } 117 } 118 } 119 120 return B_ERROR; 121 } 122 123 124 status_t 125 DesktopSettings::Private::Save(uint32 mask) 126 { 127 BPath basePath; 128 status_t status = _GetPath(basePath); 129 if (status < B_OK) 130 return status; 131 132 if (mask & kWorkspacesSettings) { 133 BPath path(basePath); 134 if (path.Append("workspaces") == B_OK) { 135 BMessage settings('asws'); 136 settings.AddInt32("count", fWorkspacesCount); 137 138 for (int32 i = 0; i < kMaxWorkspaces; i++) { 139 settings.AddMessage("workspace", &fWorkspaceMessages[i]); 140 } 141 142 BFile file; 143 status = file.SetTo(path.Path(), B_CREATE_FILE | B_ERASE_FILE | B_READ_WRITE); 144 if (status == B_OK) { 145 status = settings.Flatten(&file, NULL); 146 } 147 } 148 } 149 150 return status; 151 } 152 153 154 void 155 DesktopSettings::Private::SetDefaultPlainFont(const ServerFont &font) 156 { 157 fPlainFont = font; 158 } 159 160 161 const ServerFont & 162 DesktopSettings::Private::DefaultPlainFont() const 163 { 164 return fPlainFont; 165 } 166 167 168 void 169 DesktopSettings::Private::SetDefaultBoldFont(const ServerFont &font) 170 { 171 fBoldFont = font; 172 } 173 174 175 const ServerFont & 176 DesktopSettings::Private::DefaultBoldFont() const 177 { 178 return fBoldFont; 179 } 180 181 182 void 183 DesktopSettings::Private::SetDefaultFixedFont(const ServerFont &font) 184 { 185 fFixedFont = font; 186 } 187 188 189 const ServerFont & 190 DesktopSettings::Private::DefaultFixedFont() const 191 { 192 return fFixedFont; 193 } 194 195 196 void 197 DesktopSettings::Private::SetScrollBarInfo(const scroll_bar_info& info) 198 { 199 fScrollBarInfo = info; 200 } 201 202 203 const scroll_bar_info& 204 DesktopSettings::Private::ScrollBarInfo() const 205 { 206 return fScrollBarInfo; 207 } 208 209 210 void 211 DesktopSettings::Private::SetMenuInfo(const menu_info& info) 212 { 213 fMenuInfo = info; 214 } 215 216 217 const menu_info& 218 DesktopSettings::Private::MenuInfo() const 219 { 220 return fMenuInfo; 221 } 222 223 224 void 225 DesktopSettings::Private::SetMouseMode(const mode_mouse mode) 226 { 227 fMouseMode = mode; 228 } 229 230 231 mode_mouse 232 DesktopSettings::Private::MouseMode() const 233 { 234 return fMouseMode; 235 } 236 237 238 bool 239 DesktopSettings::Private::FocusFollowsMouse() const 240 { 241 return MouseMode() != B_NORMAL_MOUSE; 242 } 243 244 245 void 246 DesktopSettings::Private::SetWorkspacesCount(int32 number) 247 { 248 if (number < 1) 249 number = 1; 250 else if (number > kMaxWorkspaces) 251 number = kMaxWorkspaces; 252 253 fWorkspacesCount = number; 254 } 255 256 257 int32 258 DesktopSettings::Private::WorkspacesCount() const 259 { 260 return fWorkspacesCount; 261 } 262 263 264 void 265 DesktopSettings::Private::SetWorkspacesMessage(int32 index, BMessage& message) 266 { 267 if (index < 0 || index > kMaxWorkspaces) 268 return; 269 270 fWorkspaceMessages[index] = message; 271 } 272 273 274 const BMessage* 275 DesktopSettings::Private::WorkspacesMessage(int32 index) const 276 { 277 if (index < 0 || index > kMaxWorkspaces) 278 return NULL; 279 280 return &fWorkspaceMessages[index]; 281 } 282 283 284 285 // #pragma mark - 286 287 288 DesktopSettings::DesktopSettings(Desktop* desktop) 289 { 290 fSettings = desktop->fSettings; 291 fSettings->Lock(); 292 } 293 294 295 DesktopSettings::~DesktopSettings() 296 { 297 fSettings->Unlock(); 298 } 299 300 301 void 302 DesktopSettings::SetDefaultPlainFont(const ServerFont &font) 303 { 304 fSettings->SetDefaultPlainFont(font); 305 } 306 307 308 void 309 DesktopSettings::GetDefaultPlainFont(ServerFont &font) const 310 { 311 font = fSettings->DefaultPlainFont(); 312 } 313 314 315 void 316 DesktopSettings::SetDefaultBoldFont(const ServerFont &font) 317 { 318 fSettings->SetDefaultBoldFont(font); 319 } 320 321 322 void 323 DesktopSettings::GetDefaultBoldFont(ServerFont &font) const 324 { 325 font = fSettings->DefaultBoldFont(); 326 } 327 328 329 void 330 DesktopSettings::SetDefaultFixedFont(const ServerFont &font) 331 { 332 fSettings->SetDefaultFixedFont(font); 333 } 334 335 336 void 337 DesktopSettings::GetDefaultFixedFont(ServerFont &font) const 338 { 339 font = fSettings->DefaultFixedFont(); 340 } 341 342 343 void 344 DesktopSettings::SetScrollBarInfo(const scroll_bar_info& info) 345 { 346 fSettings->SetScrollBarInfo(info); 347 } 348 349 350 void 351 DesktopSettings::GetScrollBarInfo(scroll_bar_info& info) const 352 { 353 info = fSettings->ScrollBarInfo(); 354 } 355 356 357 void 358 DesktopSettings::SetMenuInfo(const menu_info& info) 359 { 360 fSettings->SetMenuInfo(info); 361 } 362 363 364 void 365 DesktopSettings::GetMenuInfo(menu_info& info) const 366 { 367 info = fSettings->MenuInfo(); 368 } 369 370 371 void 372 DesktopSettings::SetMouseMode(const mode_mouse mode) 373 { 374 fSettings->SetMouseMode(mode); 375 } 376 377 378 mode_mouse 379 DesktopSettings::MouseMode() const 380 { 381 return fSettings->MouseMode(); 382 } 383 384 385 bool 386 DesktopSettings::FocusFollowsMouse() const 387 { 388 return fSettings->FocusFollowsMouse(); 389 } 390 391 392 void 393 DesktopSettings::SetWorkspacesCount(int32 number) 394 { 395 fSettings->SetWorkspacesCount(number); 396 } 397 398 399 int32 400 DesktopSettings::WorkspacesCount() const 401 { 402 return fSettings->WorkspacesCount(); 403 } 404 405 406 void 407 DesktopSettings::SetWorkspacesMessage(int32 index, BMessage& message) 408 { 409 fSettings->SetWorkspacesMessage(index, message); 410 } 411 412 413 const BMessage* 414 DesktopSettings::WorkspacesMessage(int32 index) const 415 { 416 return fSettings->WorkspacesMessage(index); 417 } 418 419