BTRFS: Implement BTree::Path and change _Find.
[haiku.git] / src / apps / pulse / Prefs.cpp
blobbc5ad53152be0d7a9ab258ad8d7de8cfc817b70a
1 //****************************************************************************************
2 //
3 // File: Prefs.cpp
4 //
5 // Written by: Daniel Switkin
6 //
7 // Copyright 1999, Be Incorporated
8 //
9 //****************************************************************************************
11 #include "Prefs.h"
13 #include <stdio.h>
14 #include <string.h>
16 #include <FindDirectory.h>
17 #include <OS.h>
18 #include <Path.h>
19 #include <Screen.h>
21 #include "Common.h"
22 #include "PulseApp.h"
25 Prefs::Prefs()
27 fFatalError(false)
29 BPath path;
30 find_directory(B_USER_SETTINGS_DIRECTORY, &path);
31 path.Append("Pulse_settings");
32 fFile = new BFile(path.Path(), B_READ_WRITE | B_CREATE_FILE);
33 if (fFile->InitCheck() != B_OK) {
34 // try to open read-only
35 if (fFile->SetTo(path.Path(), B_READ_ONLY) != B_OK) {
36 fFatalError = true;
37 return;
41 int i = NORMAL_WINDOW_MODE;
42 if (!GetInt("window_mode", &window_mode, &i)) {
43 fFatalError = true;
44 return;
47 // These three prefs require a connection to the app_server
48 BRect r = GetNormalWindowRect();
49 if (!GetRect("normal_window_rect", &normal_window_rect, &r)) {
50 fFatalError = true;
51 return;
53 // While normal window position is under user control, size is not.
54 // Width is fixed and height must be dynamically computed each time,
55 // as number of CPUs could change since boot.
56 ComputeNormalWindowSize();
58 r = GetMiniWindowRect();
59 if (!GetRect("mini_window_rect", &mini_window_rect, &r)) {
60 fFatalError = true;
61 return;
64 r.Set(100, 100, 415, 329);
65 if (!GetRect("prefs_window_rect", &prefs_window_rect, &r)) {
66 fFatalError = true;
67 return;
70 i = DEFAULT_NORMAL_BAR_COLOR;
71 if (!GetInt("normal_bar_color", &normal_bar_color, &i)) {
72 fFatalError = true;
73 return;
76 i = DEFAULT_MINI_ACTIVE_COLOR;
77 if (!GetInt("mini_active_color", &mini_active_color, &i)) {
78 fFatalError = true;
79 return;
82 i = DEFAULT_MINI_IDLE_COLOR;
83 if (!GetInt("mini_idle_color", &mini_idle_color, &i)) {
84 fFatalError = true;
85 return;
88 i = DEFAULT_MINI_FRAME_COLOR;
89 if (!GetInt("mini_frame_color", &mini_frame_color, &i)) {
90 fFatalError = true;
91 return;
94 i = DEFAULT_DESKBAR_ACTIVE_COLOR;
95 if (!GetInt("deskbar_active_color", &deskbar_active_color, &i)) {
96 fFatalError = true;
97 return;
100 i = DEFAULT_DESKBAR_IDLE_COLOR;
101 if (!GetInt("deskbar_idle_color", &deskbar_idle_color, &i)) {
102 fFatalError = true;
103 return;
106 i = DEFAULT_DESKBAR_FRAME_COLOR;
107 if (!GetInt("deskbar_frame_color", &deskbar_frame_color, &i)) {
108 fFatalError = true;
109 return;
112 bool b = DEFAULT_NORMAL_FADE_COLORS;
113 if (!GetBool("normal_fade_colors", &normal_fade_colors, &b)) {
114 fFatalError = true;
115 return;
118 // Use the default size unless it would prevent having at least
119 // a one pixel wide display per CPU... this will only happen with > quad
120 i = DEFAULT_DESKBAR_ICON_WIDTH;
121 if (i < GetMinimumViewWidth())
122 i = GetMinimumViewWidth();
123 if (!GetInt("deskbar_icon_width", &deskbar_icon_width, &i)) {
124 fFatalError = true;
125 return;
130 Prefs::~Prefs()
132 delete fFile;
136 float
137 Prefs::GetNormalWindowHeight()
139 system_info sys_info;
140 get_system_info(&sys_info);
142 float height = PROGRESS_MTOP + PROGRESS_MBOTTOM
143 + sys_info.cpu_count * ITEM_OFFSET;
144 if (PULSEVIEW_MIN_HEIGHT > height)
145 height = PULSEVIEW_MIN_HEIGHT;
147 return height;
151 void
152 Prefs::ComputeNormalWindowSize()
154 normal_window_rect.right = normal_window_rect.left + PULSEVIEW_WIDTH;
155 normal_window_rect.bottom = normal_window_rect.top + GetNormalWindowHeight();
159 BRect
160 Prefs::GetNormalWindowRect()
162 // Dock the window in the lower right hand corner just like the original
163 BRect r(0, 0, PULSEVIEW_WIDTH, GetNormalWindowHeight());
164 BRect screen_rect = BScreen(B_MAIN_SCREEN_ID).Frame();
165 r.OffsetTo(screen_rect.right - r.Width() - 5, screen_rect.bottom - r.Height() - 5);
166 return r;
170 BRect
171 Prefs::GetMiniWindowRect()
173 // Lower right hand corner by default
174 BRect screen_rect = BScreen(B_MAIN_SCREEN_ID).Frame();
175 screen_rect.left = screen_rect.right - 30;
176 screen_rect.top = screen_rect.bottom - 150;
177 screen_rect.OffsetBy(-5, -5);
178 return screen_rect;
182 bool
183 Prefs::GetInt(const char *name, int *value, int *defaultvalue)
185 status_t err = fFile->ReadAttr(name, B_INT32_TYPE, 0, value, 4);
186 if (err == B_ENTRY_NOT_FOUND) {
187 *value = *defaultvalue;
188 if (fFile->WriteAttr(name, B_INT32_TYPE, 0, defaultvalue, 4) < 0) {
189 printf("WriteAttr on %s died\n", name);
190 fFatalError = true;
192 } else if (err < 0) {
193 printf("Unknown error reading %s:\n%s\n", name, strerror(err));
194 fFatalError = true;
195 return false;
197 return true;
201 bool
202 Prefs::GetBool(const char *name, bool *value, bool *defaultvalue)
204 status_t err = fFile->ReadAttr(name, B_BOOL_TYPE, 0, value, 1);
205 if (err == B_ENTRY_NOT_FOUND) {
206 *value = *defaultvalue;
207 if (fFile->WriteAttr(name, B_BOOL_TYPE, 0, defaultvalue, 1) < 0) {
208 printf("WriteAttr on %s died\n", name);
209 fFatalError = true;
211 } else if (err < 0) {
212 printf("Unknown error reading %s:\n%s\n", name, strerror(err));
213 fFatalError = true;
214 return false;
216 return true;
220 bool
221 Prefs::GetRect(const char *name, BRect *value, BRect *defaultvalue)
223 status_t err = fFile->ReadAttr(name, B_RECT_TYPE, 0, value, sizeof(BRect));
224 if (err == B_ENTRY_NOT_FOUND) {
225 *value = *defaultvalue;
226 if (fFile->WriteAttr(name, B_RECT_TYPE, 0, defaultvalue, sizeof(BRect)) < 0) {
227 printf("WriteAttr on %s died\n", name);
228 fFatalError = true;
230 } else if (err < 0) {
231 printf("Unknown error reading %s:\n%s\n", name, strerror(err));
232 fFatalError = true;
233 return false;
235 return true;
239 bool
240 Prefs::PutInt(const char *name, int *value)
242 status_t err = fFile->WriteAttr(name, B_INT32_TYPE, 0, value, 4);
243 if (err < 0) {
244 printf("Unknown error writing %s:\n%s\n", name, strerror(err));
245 fFatalError = true;
246 return false;
248 return true;
252 bool
253 Prefs::PutBool(const char *name, bool *value)
255 status_t err = fFile->WriteAttr(name, B_BOOL_TYPE, 0, value, 1);
256 if (err < 0) {
257 printf("Unknown error writing %s:\n%s\n", name, strerror(err));
258 fFatalError = true;
259 return false;
261 return true;
265 bool
266 Prefs::PutRect(const char *name, BRect *value)
268 status_t err = fFile->WriteAttr(name, B_RECT_TYPE, 0, value, sizeof(BRect));
269 if (err < 0) {
270 printf("Unknown error writing %s:\n%s\n", name, strerror(err));
271 fFatalError = true;
272 return false;
274 return true;
278 bool
279 Prefs::Save()
281 if (fFatalError)
282 return false;
284 if (!PutInt("window_mode", &window_mode)
285 || !PutRect("normal_window_rect", &normal_window_rect)
286 || !PutRect("mini_window_rect", &mini_window_rect)
287 || !PutRect("prefs_window_rect", &prefs_window_rect)
288 || !PutInt("normal_bar_color", &normal_bar_color)
289 || !PutInt("mini_active_color", &mini_active_color)
290 || !PutInt("mini_idle_color", &mini_idle_color)
291 || !PutInt("mini_frame_color", &mini_frame_color)
292 || !PutInt("deskbar_active_color", &deskbar_active_color)
293 || !PutInt("deskbar_idle_color", &deskbar_idle_color)
294 || !PutInt("deskbar_frame_color", &deskbar_frame_color)
295 || !PutBool("normal_fade_colors", &normal_fade_colors)
296 || !PutInt("deskbar_icon_width", &deskbar_icon_width))
297 return false;
299 return true;