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