xref: /haiku/src/servers/app/DesktopSettings.cpp (revision 23ae77aa566566465e1fca7aedbf0f1139acca1e)
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 <DefaultColors.h>
18 #include <ServerReadOnlyMemory.h>
19 
20 #include <Directory.h>
21 #include <File.h>
22 #include <FindDirectory.h>
23 #include <Path.h>
24 
25 
26 DesktopSettings::Private::Private(server_read_only_memory* shared)
27 	: BLocker("DesktopSettings_Private"),
28 	fShared(*shared)
29 {
30 	// if the on-disk settings are not complete, the defaults will be kept
31 	_SetDefaults();
32 	_Load();
33 }
34 
35 
36 DesktopSettings::Private::~Private()
37 {
38 }
39 
40 
41 void
42 DesktopSettings::Private::_SetDefaults()
43 {
44 	fPlainFont = *gFontManager->DefaultPlainFont();
45 	fBoldFont = *gFontManager->DefaultBoldFont();
46 	fFixedFont = *gFontManager->DefaultFixedFont();
47 
48 	fMouseMode = B_NORMAL_MOUSE;
49 
50 	// init scrollbar info
51 	fScrollBarInfo.proportional = true;
52 	fScrollBarInfo.double_arrows = false;
53 	// look of the knob (R5: (0, 1, 2), 1 = default)
54 	fScrollBarInfo.knob = 1;
55 	fScrollBarInfo.min_knob_size = 15;
56 
57 	// init menu info
58 	strlcpy(fMenuInfo.f_family, fPlainFont.Family(), B_FONT_FAMILY_LENGTH);
59 	strlcpy(fMenuInfo.f_style, fPlainFont.Style(), B_FONT_STYLE_LENGTH);
60 	fMenuInfo.font_size = fPlainFont.Size();
61 	// TODO:
62 	fMenuInfo.background_color.set_to(216, 216, 216);
63 
64 	// look of the separator (R5: (0, 1, 2), default 0)
65 	// TODO: we could just choose a nice one and remove the others
66 	fMenuInfo.separator = 0;
67 	fMenuInfo.click_to_open = true; // always true
68 	fMenuInfo.triggers_always_shown = false;
69 
70 	fWorkspacesCount = 4;
71 
72 	memcpy(fShared.colors, BPrivate::kDefaultColors, sizeof(rgb_color) * kNumColors);
73 }
74 
75 
76 status_t
77 DesktopSettings::Private::_GetPath(BPath& path)
78 {
79 	status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
80 	if (status < B_OK)
81 		return status;
82 
83 	status = path.Append("system/app_server");
84 	if (status < B_OK)
85 		return status;
86 
87 	return create_directory(path.Path(), 0755);
88 }
89 
90 
91 status_t
92 DesktopSettings::Private::_Load()
93 {
94 	// TODO: add support for old app_server_settings file as well
95 
96 	BPath basePath;
97 	status_t status = _GetPath(basePath);
98 	if (status < B_OK)
99 		return status;
100 
101 	// read workspaces settings
102 
103 	BPath path(basePath);
104 	path.Append("workspaces");
105 
106 	BFile file;
107 	status = file.SetTo(path.Path(), B_READ_ONLY);
108 	if (status == B_OK) {
109 		BMessage settings;
110 		status = settings.Unflatten(&file);
111 		if (status == B_OK) {
112 			int32 count;
113 			if (settings.FindInt32("count", &count) == B_OK) {
114 				fWorkspacesCount = count;
115 				if (fWorkspacesCount < 1 || fWorkspacesCount > 32)
116 					fWorkspacesCount = 4;
117 			}
118 
119 			int32 i = 0;
120 			while (i < kMaxWorkspaces
121 				&& settings.FindMessage("workspace", i, &fWorkspaceMessages[i]) == B_OK) {
122 				i++;
123 			}
124 		}
125 	}
126 
127 	// read font settings
128 
129 	path = basePath;
130 	path.Append("fonts");
131 
132 	status = file.SetTo(path.Path(), B_READ_ONLY);
133 	if (status == B_OK) {
134 		BMessage settings;
135 		status = settings.Unflatten(&file);
136 		if (status == B_OK && gFontManager->Lock()) {
137 			const char* family;
138 			const char* style;
139 			float size;
140 			if (settings.FindString("plain family", &family) == B_OK
141 				&& settings.FindString("plain style", &style) == B_OK
142 				&& settings.FindFloat("plain size", &size) == B_OK) {
143 				FontStyle* fontStyle = gFontManager->GetStyle(family, style);
144 				fPlainFont.SetStyle(fontStyle);
145 				fPlainFont.SetSize(size);
146 			}
147 			if (settings.FindString("bold family", &family) == B_OK
148 				&& settings.FindString("bold style", &style) == B_OK
149 				&& settings.FindFloat("bold size", &size) == B_OK) {
150 				FontStyle* fontStyle = gFontManager->GetStyle(family, style);
151 				fBoldFont.SetStyle(fontStyle);
152 				fBoldFont.SetSize(size);
153 			}
154 			if (settings.FindString("fixed family", &family) == B_OK
155 				&& settings.FindString("fixed style", &style) == B_OK
156 				&& settings.FindFloat("fixed size", &size) == B_OK) {
157 				FontStyle* fontStyle = gFontManager->GetStyle(family, style);
158 				if (fontStyle->IsFixedWidth())
159 					fFixedFont.SetStyle(fontStyle);
160 				fFixedFont.SetSize(size);
161 			}
162 			gFontManager->Unlock();
163 		}
164 	}
165 
166 	return B_OK;
167 }
168 
169 
170 status_t
171 DesktopSettings::Private::Save(uint32 mask)
172 {
173 	BPath basePath;
174 	status_t status = _GetPath(basePath);
175 	if (status < B_OK)
176 		return status;
177 
178 	if (mask & kWorkspacesSettings) {
179 		BPath path(basePath);
180 		if (path.Append("workspaces") == B_OK) {
181 			BMessage settings('asws');
182 			settings.AddInt32("count", fWorkspacesCount);
183 
184 			for (int32 i = 0; i < kMaxWorkspaces; i++) {
185 				settings.AddMessage("workspace", &fWorkspaceMessages[i]);
186 			}
187 
188 			BFile file;
189 			status = file.SetTo(path.Path(), B_CREATE_FILE | B_ERASE_FILE | B_READ_WRITE);
190 			if (status == B_OK) {
191 				status = settings.Flatten(&file, NULL);
192 			}
193 		}
194 	}
195 
196 	if (mask & kFontSettings) {
197 		BPath path(basePath);
198 		if (path.Append("fonts") == B_OK) {
199 			BMessage settings('asfn');
200 
201 			settings.AddString("plain family", fPlainFont.Family());
202 			settings.AddString("plain style", fPlainFont.Style());
203 			settings.AddFloat("plain size", fPlainFont.Size());
204 
205 			settings.AddString("bold family", fBoldFont.Family());
206 			settings.AddString("bold style", fBoldFont.Style());
207 			settings.AddFloat("bold size", fBoldFont.Size());
208 
209 			settings.AddString("fixed family", fFixedFont.Family());
210 			settings.AddString("fixed style", fFixedFont.Style());
211 			settings.AddFloat("fixed size", fFixedFont.Size());
212 
213 			BFile file;
214 			status = file.SetTo(path.Path(), B_CREATE_FILE | B_ERASE_FILE | B_READ_WRITE);
215 			if (status == B_OK) {
216 				status = settings.Flatten(&file, NULL);
217 			}
218 		}
219 	}
220 
221 	return status;
222 }
223 
224 
225 void
226 DesktopSettings::Private::SetDefaultPlainFont(const ServerFont &font)
227 {
228 	fPlainFont = font;
229 	Save(kFontSettings);
230 }
231 
232 
233 const ServerFont &
234 DesktopSettings::Private::DefaultPlainFont() const
235 {
236 	return fPlainFont;
237 }
238 
239 
240 void
241 DesktopSettings::Private::SetDefaultBoldFont(const ServerFont &font)
242 {
243 	fBoldFont = font;
244 	Save(kFontSettings);
245 }
246 
247 
248 const ServerFont &
249 DesktopSettings::Private::DefaultBoldFont() const
250 {
251 	return fBoldFont;
252 }
253 
254 
255 void
256 DesktopSettings::Private::SetDefaultFixedFont(const ServerFont &font)
257 {
258 	fFixedFont = font;
259 	Save(kFontSettings);
260 }
261 
262 
263 const ServerFont &
264 DesktopSettings::Private::DefaultFixedFont() const
265 {
266 	return fFixedFont;
267 }
268 
269 
270 void
271 DesktopSettings::Private::SetScrollBarInfo(const scroll_bar_info& info)
272 {
273 	fScrollBarInfo = info;
274 	Save(kAppearanceSettings);
275 }
276 
277 
278 const scroll_bar_info&
279 DesktopSettings::Private::ScrollBarInfo() const
280 {
281 	return fScrollBarInfo;
282 }
283 
284 
285 void
286 DesktopSettings::Private::SetMenuInfo(const menu_info& info)
287 {
288 	fMenuInfo = info;
289 	Save(kAppearanceSettings);
290 }
291 
292 
293 const menu_info&
294 DesktopSettings::Private::MenuInfo() const
295 {
296 	return fMenuInfo;
297 }
298 
299 
300 void
301 DesktopSettings::Private::SetMouseMode(const mode_mouse mode)
302 {
303 	fMouseMode = mode;
304 }
305 
306 
307 mode_mouse
308 DesktopSettings::Private::MouseMode() const
309 {
310 	return fMouseMode;
311 }
312 
313 
314 bool
315 DesktopSettings::Private::FocusFollowsMouse() const
316 {
317 	return MouseMode() != B_NORMAL_MOUSE;
318 }
319 
320 
321 void
322 DesktopSettings::Private::SetWorkspacesCount(int32 number)
323 {
324 	if (number < 1)
325 		number = 1;
326 	else if (number > kMaxWorkspaces)
327 		number = kMaxWorkspaces;
328 
329 	fWorkspacesCount = number;
330 }
331 
332 
333 int32
334 DesktopSettings::Private::WorkspacesCount() const
335 {
336 	return fWorkspacesCount;
337 }
338 
339 
340 void
341 DesktopSettings::Private::SetWorkspacesMessage(int32 index, BMessage& message)
342 {
343 	if (index < 0 || index > kMaxWorkspaces)
344 		return;
345 
346 	fWorkspaceMessages[index] = message;
347 }
348 
349 
350 const BMessage*
351 DesktopSettings::Private::WorkspacesMessage(int32 index) const
352 {
353 	if (index < 0 || index > kMaxWorkspaces)
354 		return NULL;
355 
356 	return &fWorkspaceMessages[index];
357 }
358 
359 
360 
361 //	#pragma mark -
362 
363 
364 DesktopSettings::DesktopSettings(Desktop* desktop)
365 {
366 	fSettings = desktop->fSettings;
367 	fSettings->Lock();
368 }
369 
370 
371 DesktopSettings::~DesktopSettings()
372 {
373 	fSettings->Unlock();
374 }
375 
376 
377 void
378 DesktopSettings::SetDefaultPlainFont(const ServerFont &font)
379 {
380 	fSettings->SetDefaultPlainFont(font);
381 }
382 
383 
384 void
385 DesktopSettings::GetDefaultPlainFont(ServerFont &font) const
386 {
387 	font = fSettings->DefaultPlainFont();
388 }
389 
390 
391 void
392 DesktopSettings::SetDefaultBoldFont(const ServerFont &font)
393 {
394 	fSettings->SetDefaultBoldFont(font);
395 }
396 
397 
398 void
399 DesktopSettings::GetDefaultBoldFont(ServerFont &font) const
400 {
401 	font = fSettings->DefaultBoldFont();
402 }
403 
404 
405 void
406 DesktopSettings::SetDefaultFixedFont(const ServerFont &font)
407 {
408 	fSettings->SetDefaultFixedFont(font);
409 }
410 
411 
412 void
413 DesktopSettings::GetDefaultFixedFont(ServerFont &font) const
414 {
415 	font = fSettings->DefaultFixedFont();
416 }
417 
418 
419 void
420 DesktopSettings::SetScrollBarInfo(const scroll_bar_info& info)
421 {
422 	fSettings->SetScrollBarInfo(info);
423 }
424 
425 
426 void
427 DesktopSettings::GetScrollBarInfo(scroll_bar_info& info) const
428 {
429 	info = fSettings->ScrollBarInfo();
430 }
431 
432 
433 void
434 DesktopSettings::SetMenuInfo(const menu_info& info)
435 {
436 	fSettings->SetMenuInfo(info);
437 }
438 
439 
440 void
441 DesktopSettings::GetMenuInfo(menu_info& info) const
442 {
443 	info = fSettings->MenuInfo();
444 }
445 
446 
447 void
448 DesktopSettings::SetMouseMode(const mode_mouse mode)
449 {
450 	fSettings->SetMouseMode(mode);
451 }
452 
453 
454 mode_mouse
455 DesktopSettings::MouseMode() const
456 {
457 	return fSettings->MouseMode();
458 }
459 
460 
461 bool
462 DesktopSettings::FocusFollowsMouse() const
463 {
464 	return fSettings->FocusFollowsMouse();
465 }
466 
467 
468 void
469 DesktopSettings::SetWorkspacesCount(int32 number)
470 {
471 	fSettings->SetWorkspacesCount(number);
472 }
473 
474 
475 int32
476 DesktopSettings::WorkspacesCount() const
477 {
478 	return fSettings->WorkspacesCount();
479 }
480 
481 
482 void
483 DesktopSettings::SetWorkspacesMessage(int32 index, BMessage& message)
484 {
485 	fSettings->SetWorkspacesMessage(index, message);
486 }
487 
488 
489 const BMessage*
490 DesktopSettings::WorkspacesMessage(int32 index) const
491 {
492 	return fSettings->WorkspacesMessage(index);
493 }
494 
495