xref: /haiku/src/servers/app/DesktopSettings.cpp (revision b9e275a6a57f11b781269d0a391e75efd60cee67)
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 	// read font settings
121 
122 	path = basePath;
123 	path.Append("fonts");
124 
125 	status = file.SetTo(path.Path(), B_READ_ONLY);
126 	if (status == B_OK) {
127 		BMessage settings;
128 		status = settings.Unflatten(&file);
129 		if (status == B_OK && gFontManager->Lock()) {
130 			const char* family;
131 			const char* style;
132 			float size;
133 			if (settings.FindString("plain family", &family) == B_OK
134 				&& settings.FindString("plain style", &style) == B_OK
135 				&& settings.FindFloat("plain size", &size) == B_OK) {
136 				FontStyle* fontStyle = gFontManager->GetStyle(family, style);
137 				fPlainFont.SetStyle(fontStyle);
138 				fPlainFont.SetSize(size);
139 			}
140 			if (settings.FindString("bold family", &family) == B_OK
141 				&& settings.FindString("bold style", &style) == B_OK
142 				&& settings.FindFloat("bold size", &size) == B_OK) {
143 				FontStyle* fontStyle = gFontManager->GetStyle(family, style);
144 				fBoldFont.SetStyle(fontStyle);
145 				fBoldFont.SetSize(size);
146 			}
147 			if (settings.FindString("fixed family", &family) == B_OK
148 				&& settings.FindString("fixed style", &style) == B_OK
149 				&& settings.FindFloat("fixed size", &size) == B_OK) {
150 				FontStyle* fontStyle = gFontManager->GetStyle(family, style);
151 				if (fontStyle->IsFixedWidth())
152 					fFixedFont.SetStyle(fontStyle);
153 				fFixedFont.SetSize(size);
154 			}
155 			gFontManager->Unlock();
156 		}
157 	}
158 
159 	return B_OK;
160 }
161 
162 
163 status_t
164 DesktopSettings::Private::Save(uint32 mask)
165 {
166 	BPath basePath;
167 	status_t status = _GetPath(basePath);
168 	if (status < B_OK)
169 		return status;
170 
171 	if (mask & kWorkspacesSettings) {
172 		BPath path(basePath);
173 		if (path.Append("workspaces") == B_OK) {
174 			BMessage settings('asws');
175 			settings.AddInt32("count", fWorkspacesCount);
176 
177 			for (int32 i = 0; i < kMaxWorkspaces; i++) {
178 				settings.AddMessage("workspace", &fWorkspaceMessages[i]);
179 			}
180 
181 			BFile file;
182 			status = file.SetTo(path.Path(), B_CREATE_FILE | B_ERASE_FILE | B_READ_WRITE);
183 			if (status == B_OK) {
184 				status = settings.Flatten(&file, NULL);
185 			}
186 		}
187 	}
188 
189 	if (mask & kFontSettings) {
190 		BPath path(basePath);
191 		if (path.Append("fonts") == B_OK) {
192 			BMessage settings('asfn');
193 
194 			settings.AddString("plain family", fPlainFont.Family());
195 			settings.AddString("plain style", fPlainFont.Style());
196 			settings.AddFloat("plain size", fPlainFont.Size());
197 
198 			settings.AddString("bold family", fBoldFont.Family());
199 			settings.AddString("bold style", fBoldFont.Style());
200 			settings.AddFloat("bold size", fBoldFont.Size());
201 
202 			settings.AddString("fixed family", fFixedFont.Family());
203 			settings.AddString("fixed style", fFixedFont.Style());
204 			settings.AddFloat("fixed size", fFixedFont.Size());
205 
206 			BFile file;
207 			status = file.SetTo(path.Path(), B_CREATE_FILE | B_ERASE_FILE | B_READ_WRITE);
208 			if (status == B_OK) {
209 				status = settings.Flatten(&file, NULL);
210 			}
211 		}
212 	}
213 
214 	return status;
215 }
216 
217 
218 void
219 DesktopSettings::Private::SetDefaultPlainFont(const ServerFont &font)
220 {
221 	fPlainFont = font;
222 	Save(kFontSettings);
223 }
224 
225 
226 const ServerFont &
227 DesktopSettings::Private::DefaultPlainFont() const
228 {
229 	return fPlainFont;
230 }
231 
232 
233 void
234 DesktopSettings::Private::SetDefaultBoldFont(const ServerFont &font)
235 {
236 	fBoldFont = font;
237 	Save(kFontSettings);
238 }
239 
240 
241 const ServerFont &
242 DesktopSettings::Private::DefaultBoldFont() const
243 {
244 	return fBoldFont;
245 }
246 
247 
248 void
249 DesktopSettings::Private::SetDefaultFixedFont(const ServerFont &font)
250 {
251 	fFixedFont = font;
252 	Save(kFontSettings);
253 }
254 
255 
256 const ServerFont &
257 DesktopSettings::Private::DefaultFixedFont() const
258 {
259 	return fFixedFont;
260 }
261 
262 
263 void
264 DesktopSettings::Private::SetScrollBarInfo(const scroll_bar_info& info)
265 {
266 	fScrollBarInfo = info;
267 	Save(kAppearanceSettings);
268 }
269 
270 
271 const scroll_bar_info&
272 DesktopSettings::Private::ScrollBarInfo() const
273 {
274 	return fScrollBarInfo;
275 }
276 
277 
278 void
279 DesktopSettings::Private::SetMenuInfo(const menu_info& info)
280 {
281 	fMenuInfo = info;
282 	Save(kAppearanceSettings);
283 }
284 
285 
286 const menu_info&
287 DesktopSettings::Private::MenuInfo() const
288 {
289 	return fMenuInfo;
290 }
291 
292 
293 void
294 DesktopSettings::Private::SetMouseMode(const mode_mouse mode)
295 {
296 	fMouseMode = mode;
297 }
298 
299 
300 mode_mouse
301 DesktopSettings::Private::MouseMode() const
302 {
303 	return fMouseMode;
304 }
305 
306 
307 bool
308 DesktopSettings::Private::FocusFollowsMouse() const
309 {
310 	return MouseMode() != B_NORMAL_MOUSE;
311 }
312 
313 
314 void
315 DesktopSettings::Private::SetWorkspacesCount(int32 number)
316 {
317 	if (number < 1)
318 		number = 1;
319 	else if (number > kMaxWorkspaces)
320 		number = kMaxWorkspaces;
321 
322 	fWorkspacesCount = number;
323 }
324 
325 
326 int32
327 DesktopSettings::Private::WorkspacesCount() const
328 {
329 	return fWorkspacesCount;
330 }
331 
332 
333 void
334 DesktopSettings::Private::SetWorkspacesMessage(int32 index, BMessage& message)
335 {
336 	if (index < 0 || index > kMaxWorkspaces)
337 		return;
338 
339 	fWorkspaceMessages[index] = message;
340 }
341 
342 
343 const BMessage*
344 DesktopSettings::Private::WorkspacesMessage(int32 index) const
345 {
346 	if (index < 0 || index > kMaxWorkspaces)
347 		return NULL;
348 
349 	return &fWorkspaceMessages[index];
350 }
351 
352 
353 
354 //	#pragma mark -
355 
356 
357 DesktopSettings::DesktopSettings(Desktop* desktop)
358 {
359 	fSettings = desktop->fSettings;
360 	fSettings->Lock();
361 }
362 
363 
364 DesktopSettings::~DesktopSettings()
365 {
366 	fSettings->Unlock();
367 }
368 
369 
370 void
371 DesktopSettings::SetDefaultPlainFont(const ServerFont &font)
372 {
373 	fSettings->SetDefaultPlainFont(font);
374 }
375 
376 
377 void
378 DesktopSettings::GetDefaultPlainFont(ServerFont &font) const
379 {
380 	font = fSettings->DefaultPlainFont();
381 }
382 
383 
384 void
385 DesktopSettings::SetDefaultBoldFont(const ServerFont &font)
386 {
387 	fSettings->SetDefaultBoldFont(font);
388 }
389 
390 
391 void
392 DesktopSettings::GetDefaultBoldFont(ServerFont &font) const
393 {
394 	font = fSettings->DefaultBoldFont();
395 }
396 
397 
398 void
399 DesktopSettings::SetDefaultFixedFont(const ServerFont &font)
400 {
401 	fSettings->SetDefaultFixedFont(font);
402 }
403 
404 
405 void
406 DesktopSettings::GetDefaultFixedFont(ServerFont &font) const
407 {
408 	font = fSettings->DefaultFixedFont();
409 }
410 
411 
412 void
413 DesktopSettings::SetScrollBarInfo(const scroll_bar_info& info)
414 {
415 	fSettings->SetScrollBarInfo(info);
416 }
417 
418 
419 void
420 DesktopSettings::GetScrollBarInfo(scroll_bar_info& info) const
421 {
422 	info = fSettings->ScrollBarInfo();
423 }
424 
425 
426 void
427 DesktopSettings::SetMenuInfo(const menu_info& info)
428 {
429 	fSettings->SetMenuInfo(info);
430 }
431 
432 
433 void
434 DesktopSettings::GetMenuInfo(menu_info& info) const
435 {
436 	info = fSettings->MenuInfo();
437 }
438 
439 
440 void
441 DesktopSettings::SetMouseMode(const mode_mouse mode)
442 {
443 	fSettings->SetMouseMode(mode);
444 }
445 
446 
447 mode_mouse
448 DesktopSettings::MouseMode() const
449 {
450 	return fSettings->MouseMode();
451 }
452 
453 
454 bool
455 DesktopSettings::FocusFollowsMouse() const
456 {
457 	return fSettings->FocusFollowsMouse();
458 }
459 
460 
461 void
462 DesktopSettings::SetWorkspacesCount(int32 number)
463 {
464 	fSettings->SetWorkspacesCount(number);
465 }
466 
467 
468 int32
469 DesktopSettings::WorkspacesCount() const
470 {
471 	return fSettings->WorkspacesCount();
472 }
473 
474 
475 void
476 DesktopSettings::SetWorkspacesMessage(int32 index, BMessage& message)
477 {
478 	fSettings->SetWorkspacesMessage(index, message);
479 }
480 
481 
482 const BMessage*
483 DesktopSettings::WorkspacesMessage(int32 index) const
484 {
485 	return fSettings->WorkspacesMessage(index);
486 }
487 
488