xref: /haiku/src/apps/pulse/Prefs.cpp (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
1 //****************************************************************************************
2 //
3 //	File:		Prefs.cpp
4 //
5 //	Written by:	Daniel Switkin
6 //
7 //	Copyright 1999, Be Incorporated
8 //
9 //****************************************************************************************
10 
11 #include "Prefs.h"
12 #include "Common.h"
13 #include "PulseApp.h"
14 #include <FindDirectory.h>
15 #include <Path.h>
16 #include <interface/Screen.h>
17 #include <kernel/OS.h>
18 #include <stdio.h>
19 #include <string.h>
20 
21 Prefs::Prefs()
22 {
23 	BPath path;
24 	find_directory(B_USER_SETTINGS_DIRECTORY, &path);
25 	path.Append("Pulse_settings");
26 	file = new BFile(path.Path(), B_READ_WRITE | B_CREATE_FILE);
27 
28 	int i = NORMAL_WINDOW_MODE;
29 	if (!GetInt("window_mode", &window_mode, &i)) {
30 		fatalerror = true;
31 		return;
32 	}
33 
34 	// These three prefs require a connection to the app_server
35 	BRect r = GetNormalWindowRect();
36 	if (!GetRect("normal_window_rect", &normal_window_rect, &r)) {
37 		fatalerror = true;
38 		return;
39 	}
40 
41 	r = GetMiniWindowRect();
42 	if (!GetRect("mini_window_rect", &mini_window_rect, &r)) {
43 		fatalerror = true;
44 		return;
45 	}
46 
47 	r.Set(100, 100, 415, 329);
48 	if (!GetRect("prefs_window_rect", &prefs_window_rect, &r)) {
49 		fatalerror = true;
50 		return;
51 	}
52 
53 	i = DEFAULT_NORMAL_BAR_COLOR;
54 	if (!GetInt("normal_bar_color", &normal_bar_color, &i)) {
55 		fatalerror = true;
56 		return;
57 	}
58 
59 	i = DEFAULT_MINI_ACTIVE_COLOR;
60 	if (!GetInt("mini_active_color", &mini_active_color, &i)) {
61 		fatalerror = true;
62 		return;
63 	}
64 
65 	i = DEFAULT_MINI_IDLE_COLOR;
66 	if (!GetInt("mini_idle_color", &mini_idle_color, &i)) {
67 		fatalerror = true;
68 		return;
69 	}
70 
71 	i = DEFAULT_MINI_FRAME_COLOR;
72 	if (!GetInt("mini_frame_color", &mini_frame_color, &i)) {
73 		fatalerror = true;
74 		return;
75 	}
76 
77 	i = DEFAULT_DESKBAR_ACTIVE_COLOR;
78 	if (!GetInt("deskbar_active_color", &deskbar_active_color, &i)) {
79 		fatalerror = true;
80 		return;
81 	}
82 
83 	i = DEFAULT_DESKBAR_IDLE_COLOR;
84 	if (!GetInt("deskbar_idle_color", &deskbar_idle_color, &i)) {
85 		fatalerror = true;
86 		return;
87 	}
88 
89 	i = DEFAULT_DESKBAR_FRAME_COLOR;
90 	if (!GetInt("deskbar_frame_color", &deskbar_frame_color, &i)) {
91 		fatalerror = true;
92 		return;
93 	}
94 
95 	bool b = DEFAULT_NORMAL_FADE_COLORS;
96 	if (!GetBool("normal_fade_colors", &normal_fade_colors, &b)) {
97 		fatalerror = true;
98 		return;
99 	}
100 
101 	// Use the default size unless it would prevent having at least
102 	// a one pixel wide display per CPU... this will only happen with > quad
103 	i = DEFAULT_DESKBAR_ICON_WIDTH;
104 	if (i < GetMinimumViewWidth())
105 		i = GetMinimumViewWidth();
106 	if (!GetInt("deskbar_icon_width", &deskbar_icon_width, &i)) {
107 		fatalerror = true;
108 		return;
109 	}
110 }
111 
112 
113 Prefs::~Prefs()
114 {
115 	delete file;
116 }
117 
118 
119 BRect
120 Prefs::GetNormalWindowRect()
121 {
122 	system_info sys_info;
123 	get_system_info(&sys_info);
124 
125 	float height = PROGRESS_MTOP + PROGRESS_MBOTTOM + sys_info.cpu_count * ITEM_OFFSET;
126 	if (PULSEVIEW_MIN_HEIGHT > height)
127 		height = PULSEVIEW_MIN_HEIGHT;
128 
129 	// Dock the window in the lower right hand corner just like the original
130 	BRect r(0, 0, PULSEVIEW_WIDTH, height);
131 	BRect screen_rect = BScreen(B_MAIN_SCREEN_ID).Frame();
132 	r.OffsetTo(screen_rect.right - r.Width() - 5, screen_rect.bottom - r.Height() - 5);
133 	return r;
134 }
135 
136 
137 BRect
138 Prefs::GetMiniWindowRect()
139 {
140 	// Lower right hand corner by default
141 	BRect screen_rect = BScreen(B_MAIN_SCREEN_ID).Frame();
142 	screen_rect.left = screen_rect.right - 30;
143 	screen_rect.top = screen_rect.bottom - 150;
144 	screen_rect.OffsetBy(-5, -5);
145 	return screen_rect;
146 }
147 
148 
149 bool
150 Prefs::GetInt(char *name, int *value, int *defaultvalue)
151 {
152 	status_t err = file->ReadAttr(name, B_INT32_TYPE, 0, value, 4);
153 	if (err == B_ENTRY_NOT_FOUND) {
154 		*value = *defaultvalue;
155 		if (file->WriteAttr(name, B_INT32_TYPE, 0, defaultvalue, 4) < 0) {
156 			printf("WriteAttr on %s died\n", name);
157 			fatalerror = true;
158 			return false;
159 		}
160 	} else if  (err < 0) {
161 		printf("Unknown error reading %s:\n%s\n", name, strerror(err));
162 		fatalerror = true;
163 		return false;
164 	}
165 	return true;
166 }
167 
168 
169 bool
170 Prefs::GetBool(char *name, bool *value, bool *defaultvalue)
171 {
172 	status_t err = file->ReadAttr(name, B_BOOL_TYPE, 0, value, 1);
173 	if (err == B_ENTRY_NOT_FOUND) {
174 		*value = *defaultvalue;
175 		if (file->WriteAttr(name, B_BOOL_TYPE, 0, defaultvalue, 1) < 0) {
176 			printf("WriteAttr on %s died\n", name);
177 			fatalerror = true;
178 			return false;
179 		}
180 	} else if (err < 0) {
181 		printf("Unknown error reading %s:\n%s\n", name, strerror(err));
182 		fatalerror = true;
183 		return false;
184 	}
185 	return true;
186 }
187 
188 
189 bool
190 Prefs::GetRect(char *name, BRect *value, BRect *defaultvalue)
191 {
192 	status_t err = file->ReadAttr(name, B_RECT_TYPE, 0, value, sizeof(BRect));
193 	if (err == B_ENTRY_NOT_FOUND) {
194 		*value = *defaultvalue;
195 		if (file->WriteAttr(name, B_RECT_TYPE, 0, defaultvalue, sizeof(BRect)) < 0) {
196 			printf("WriteAttr on %s died\n", name);
197 			fatalerror = true;
198 			return false;
199 		}
200 	} else if (err < 0) {
201 		printf("Unknown error reading %s:\n%s\n", name, strerror(err));
202 		fatalerror = true;
203 		return false;
204 	}
205 	return true;
206 }
207 
208 
209 bool
210 Prefs::PutInt(char *name, int *value)
211 {
212 	status_t err = file->WriteAttr(name, B_INT32_TYPE, 0, value, 4);
213 	if (err < 0) {
214 		printf("Unknown error writing %s:\n%s\n", name, strerror(err));
215 		fatalerror = true;
216 		return false;
217 	}
218 	return true;
219 }
220 
221 
222 bool
223 Prefs::PutBool(char *name, bool *value)
224 {
225 	status_t err = file->WriteAttr(name, B_BOOL_TYPE, 0, value, 1);
226 	if (err < 0) {
227 		printf("Unknown error writing %s:\n%s\n", name, strerror(err));
228 		fatalerror = true;
229 		return false;
230 	}
231 	return true;
232 }
233 
234 
235 bool
236 Prefs::PutRect(char *name, BRect *value)
237 {
238 	status_t err = file->WriteAttr(name, B_RECT_TYPE, 0, value, sizeof(BRect));
239 	if (err < 0) {
240 		printf("Unknown error writing %s:\n%s\n", name, strerror(err));
241 		fatalerror = true;
242 		return false;
243 	}
244 	return true;
245 }
246 
247 
248 bool
249 Prefs::Save()
250 {
251 	if (!PutInt("window_mode", &window_mode)
252 		|| !PutRect("normal_window_rect", &normal_window_rect)
253 		|| !PutRect("mini_window_rect", &mini_window_rect)
254 		|| !PutRect("prefs_window_rect", &prefs_window_rect)
255 		|| !PutInt("normal_bar_color", &normal_bar_color)
256 		|| !PutInt("mini_active_color", &mini_active_color)
257 		|| !PutInt("mini_idle_color", &mini_idle_color)
258 		|| !PutInt("mini_frame_color", &mini_frame_color)
259 		|| !PutInt("deskbar_active_color", &deskbar_active_color)
260 		|| !PutInt("deskbar_idle_color", &deskbar_idle_color)
261 		|| !PutInt("deskbar_frame_color", &deskbar_frame_color)
262 		|| !PutBool("normal_fade_colors", &normal_fade_colors)
263 		|| !PutInt("deskbar_icon_width", &deskbar_icon_width))
264 		return false;
265 
266 	return true;
267 }
268 
269