1 /* 2 ProcessController © 2000, Georges-Edouard Berenger, All Rights Reserved. 3 Copyright (C) 2004 beunited.org 4 5 This library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 10 This library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with this library; if not, write to the Free Software 17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 */ 19 20 21 #include "Preferences.h" 22 #include "Utilities.h" 23 24 #include <stdio.h> 25 #include <stdlib.h> 26 #include <string.h> 27 28 #include <Alert.h> 29 #include <Catalog.h> 30 #include <Directory.h> 31 #include <File.h> 32 #include <FindDirectory.h> 33 #include <Locker.h> 34 #include <Mime.h> 35 #include <Path.h> 36 37 #undef B_TRANSLATE_CONTEXT 38 #define B_TRANSLATE_CONTEXT "ProcessController" 39 40 Preferences::Preferences(const char* name, const char* signature, bool doSave) 41 : BMessage('Pref'), BLocker("Preferences", true), 42 fSavePreferences(doSave) 43 { 44 fNewPreferences = false; 45 fSettingsFile = 0; 46 BPath prefpath; 47 fName = strdup(name); 48 if (signature) 49 fSignature = strdup(signature); 50 else 51 fSignature = NULL; 52 53 if (find_directory(B_USER_SETTINGS_DIRECTORY, &prefpath) == B_OK) { 54 BDirectory prefdir(prefpath.Path()); 55 BEntry entry; 56 prefdir.FindEntry(fName, &entry); 57 58 BFile file(&entry, B_READ_ONLY); 59 if (file.InitCheck() == B_OK) 60 Unflatten(&file); 61 else 62 fNewPreferences = true; 63 } 64 } 65 66 67 Preferences::Preferences(const entry_ref &ref, const char* signature, bool doSave) 68 : BMessage('Pref'), BLocker("Preferences", true), 69 fSavePreferences(doSave) 70 { 71 fSettingsFile = new entry_ref(ref); 72 fNewPreferences = false; 73 BPath prefpath; 74 fName = NULL; 75 if (signature) 76 fSignature = strdup(signature); 77 else 78 fSignature = NULL; 79 80 BFile file(fSettingsFile, B_READ_ONLY); 81 if (file.InitCheck() == B_OK) 82 Unflatten(&file); 83 else 84 fNewPreferences = true; 85 } 86 87 88 Preferences::~Preferences() 89 { 90 if (fSavePreferences) { 91 BFile file; 92 if (fSettingsFile) 93 file.SetTo(fSettingsFile, B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE); 94 else { 95 BPath prefpath; 96 if (find_directory(B_USER_SETTINGS_DIRECTORY, &prefpath, true) == B_OK) { 97 BDirectory prefdir(prefpath.Path()); 98 prefdir.CreateFile(fName, &file, false); 99 } 100 } 101 102 if (file.InitCheck () == B_OK) { 103 Flatten(&file); 104 if (fSignature) { 105 file.WriteAttr("BEOS:TYPE", B_MIME_STRING_TYPE, 0, fSignature, 106 strlen(fSignature) + 1); 107 } 108 } else { 109 // implement saving somewhere else! 110 BString error; 111 snprintf(error.LockBuffer(256), 256, 112 B_TRANSLATE("Your setting file could not be saved!\n(%s)"), 113 strerror(file.InitCheck())); 114 error.UnlockBuffer(); 115 BAlert *alert = new BAlert(B_TRANSLATE("Error saving file"), 116 error.String(), B_TRANSLATE("Damned!"), NULL, NULL, 117 B_WIDTH_AS_USUAL, B_STOP_ALERT); 118 alert->SetShortcut(0, B_ESCAPE); 119 120 alert->Go(); 121 } 122 } 123 delete fSettingsFile; 124 free(fName); 125 free(fSignature); 126 } 127 128 129 status_t 130 Preferences::MakeEmpty() 131 { 132 Lock(); 133 status_t status = BMessage::MakeEmpty(); 134 Unlock(); 135 return status; 136 } 137 138 139 void 140 Preferences::SaveWindowPosition(BWindow* window, const char* name) 141 { 142 Lock(); 143 144 BRect rect = window->Frame(); 145 if (HasPoint(name)) 146 ReplacePoint(name, rect.LeftTop()); 147 else 148 AddPoint(name, rect.LeftTop()); 149 150 Unlock(); 151 } 152 153 154 void 155 Preferences::LoadWindowPosition(BWindow* window, const char* name) 156 { 157 Lock(); 158 159 BPoint p; 160 if (FindPoint(name, &p) == B_OK) { 161 window->MoveTo(p); 162 make_window_visible(window); 163 } 164 165 Unlock(); 166 } 167 168 169 void 170 Preferences::SaveWindowFrame(BWindow* window, const char* name) 171 { 172 Lock(); 173 174 BRect rect = window->Frame(); 175 if (HasRect(name)) 176 ReplaceRect(name, rect); 177 else 178 AddRect(name, rect); 179 180 Unlock(); 181 } 182 183 184 void 185 Preferences::LoadWindowFrame(BWindow* window, const char* name) 186 { 187 Lock(); 188 189 BRect frame; 190 if (FindRect(name, &frame) == B_OK) { 191 window->MoveTo(frame.LeftTop()); 192 window->ResizeTo(frame.Width(), frame.Height()); 193 make_window_visible(window); 194 } 195 196 Unlock(); 197 } 198 199 200 void 201 Preferences::SaveInt32(int32 value, const char* name) 202 { 203 Lock(); 204 205 if (HasInt32(name)) 206 ReplaceInt32(name, value); 207 else 208 AddInt32(name, value); 209 210 Unlock(); 211 } 212 213 214 bool 215 Preferences::ReadInt32(int32 &val, const char* name) 216 { 217 Lock(); 218 int32 readVal; 219 bool found = FindInt32(name, &readVal) == B_OK; 220 if (found) 221 val = readVal; 222 Unlock(); 223 return found; 224 } 225 226 227 void 228 Preferences::SaveFloat(float val, const char* name) 229 { 230 Lock(); 231 if (HasFloat(name)) 232 ReplaceFloat(name, val); 233 else 234 AddFloat(name, val); 235 Unlock(); 236 } 237 238 239 bool 240 Preferences::ReadFloat(float &val, const char* name) 241 { 242 Lock(); 243 float readVal; 244 bool found = FindFloat(name, &readVal) == B_OK; 245 if (found) 246 val = readVal; 247 Unlock(); 248 return found; 249 } 250 251 252 void 253 Preferences::SaveRect(BRect& rect, const char* name) 254 { 255 Lock(); 256 if (HasRect(name)) 257 ReplaceRect(name, rect); 258 else 259 AddRect(name, rect); 260 Unlock(); 261 } 262 263 264 BRect & 265 Preferences::ReadRect(BRect& rect, const char* name) 266 { 267 Lock(); 268 BRect loaded; 269 if (FindRect(name, &loaded) == B_OK) 270 rect = loaded; 271 Unlock(); 272 return rect; 273 } 274 275 276 void 277 Preferences::SaveString(BString &string, const char* name) 278 { 279 Lock(); 280 if (HasString(name)) 281 ReplaceString(name, string); 282 else 283 AddString(name, string); 284 Unlock(); 285 } 286 287 288 void 289 Preferences::SaveString(const char* string, const char* name) 290 { 291 Lock(); 292 if (HasString(name)) 293 ReplaceString(name, string); 294 else 295 AddString(name, string); 296 Unlock(); 297 } 298 299 300 bool 301 Preferences::ReadString(BString &string, const char* name) 302 { 303 Lock(); 304 bool loaded = FindString(name, &string) == B_OK; 305 Unlock(); 306 return loaded; 307 } 308