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 13 #include <stdio.h> 14 #include <string.h> 15 16 #include <FindDirectory.h> 17 #include <OS.h> 18 #include <Path.h> 19 #include <Screen.h> 20 21 #include "Common.h" 22 #include "PulseApp.h" 23 24 25 Prefs::Prefs() 26 : 27 fFatalError(false) 28 { 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; 38 } 39 } 40 41 int i = NORMAL_WINDOW_MODE; 42 if (!GetInt("window_mode", &window_mode, &i)) { 43 fFatalError = true; 44 return; 45 } 46 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; 52 } 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(); 57 58 r = GetMiniWindowRect(); 59 if (!GetRect("mini_window_rect", &mini_window_rect, &r)) { 60 fFatalError = true; 61 return; 62 } 63 64 r.Set(100, 100, 415, 329); 65 if (!GetRect("prefs_window_rect", &prefs_window_rect, &r)) { 66 fFatalError = true; 67 return; 68 } 69 70 i = DEFAULT_NORMAL_BAR_COLOR; 71 if (!GetInt("normal_bar_color", &normal_bar_color, &i)) { 72 fFatalError = true; 73 return; 74 } 75 76 i = DEFAULT_MINI_ACTIVE_COLOR; 77 if (!GetInt("mini_active_color", &mini_active_color, &i)) { 78 fFatalError = true; 79 return; 80 } 81 82 i = DEFAULT_MINI_IDLE_COLOR; 83 if (!GetInt("mini_idle_color", &mini_idle_color, &i)) { 84 fFatalError = true; 85 return; 86 } 87 88 i = DEFAULT_MINI_FRAME_COLOR; 89 if (!GetInt("mini_frame_color", &mini_frame_color, &i)) { 90 fFatalError = true; 91 return; 92 } 93 94 i = DEFAULT_DESKBAR_ACTIVE_COLOR; 95 if (!GetInt("deskbar_active_color", &deskbar_active_color, &i)) { 96 fFatalError = true; 97 return; 98 } 99 100 i = DEFAULT_DESKBAR_IDLE_COLOR; 101 if (!GetInt("deskbar_idle_color", &deskbar_idle_color, &i)) { 102 fFatalError = true; 103 return; 104 } 105 106 i = DEFAULT_DESKBAR_FRAME_COLOR; 107 if (!GetInt("deskbar_frame_color", &deskbar_frame_color, &i)) { 108 fFatalError = true; 109 return; 110 } 111 112 bool b = DEFAULT_NORMAL_FADE_COLORS; 113 if (!GetBool("normal_fade_colors", &normal_fade_colors, &b)) { 114 fFatalError = true; 115 return; 116 } 117 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; 126 } 127 } 128 129 130 Prefs::~Prefs() 131 { 132 delete fFile; 133 } 134 135 136 float 137 Prefs::GetNormalWindowHeight() 138 { 139 system_info sys_info; 140 get_system_info(&sys_info); 141 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; 146 147 return height; 148 } 149 150 151 void 152 Prefs::ComputeNormalWindowSize() 153 { 154 normal_window_rect.right = normal_window_rect.left + PULSEVIEW_WIDTH; 155 normal_window_rect.bottom = normal_window_rect.top + GetNormalWindowHeight(); 156 } 157 158 159 BRect 160 Prefs::GetNormalWindowRect() 161 { 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; 167 } 168 169 170 BRect 171 Prefs::GetMiniWindowRect() 172 { 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; 179 } 180 181 182 bool 183 Prefs::GetInt(const char *name, int *value, int *defaultvalue) 184 { 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; 191 } 192 } else if (err < 0) { 193 printf("Unknown error reading %s:\n%s\n", name, strerror(err)); 194 fFatalError = true; 195 return false; 196 } 197 return true; 198 } 199 200 201 bool 202 Prefs::GetBool(const char *name, bool *value, bool *defaultvalue) 203 { 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; 210 } 211 } else if (err < 0) { 212 printf("Unknown error reading %s:\n%s\n", name, strerror(err)); 213 fFatalError = true; 214 return false; 215 } 216 return true; 217 } 218 219 220 bool 221 Prefs::GetRect(const char *name, BRect *value, BRect *defaultvalue) 222 { 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; 229 } 230 } else if (err < 0) { 231 printf("Unknown error reading %s:\n%s\n", name, strerror(err)); 232 fFatalError = true; 233 return false; 234 } 235 return true; 236 } 237 238 239 bool 240 Prefs::PutInt(const char *name, int *value) 241 { 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; 247 } 248 return true; 249 } 250 251 252 bool 253 Prefs::PutBool(const char *name, bool *value) 254 { 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; 260 } 261 return true; 262 } 263 264 265 bool 266 Prefs::PutRect(const char *name, BRect *value) 267 { 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; 273 } 274 return true; 275 } 276 277 278 bool 279 Prefs::Save() 280 { 281 if (fFatalError) 282 return false; 283 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; 298 299 return true; 300 } 301 302