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