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 status_t set = B_ERROR; 93 if (fSettingsFile) 94 file.SetTo(fSettingsFile, B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE); 95 else { 96 BPath prefpath; 97 if (find_directory(B_USER_SETTINGS_DIRECTORY, &prefpath, true) == B_OK) { 98 BDirectory prefdir(prefpath.Path()); 99 set = prefdir.CreateFile(fName, &file, false); 100 } 101 } 102 103 if (file.InitCheck () == B_OK) { 104 Flatten(&file); 105 if (fSignature) { 106 file.WriteAttr("BEOS:TYPE", B_MIME_STRING_TYPE, 0, fSignature, 107 strlen(fSignature) + 1); 108 } 109 } else { 110 // implement saving somewhere else! 111 BString error; 112 snprintf(error.LockBuffer(256), 256, 113 B_TRANSLATE("Your setting file could not be saved!\n(%s)"), 114 strerror(file.InitCheck())); 115 error.UnlockBuffer(); 116 BAlert *alert = new BAlert(B_TRANSLATE("Error saving file"), 117 error.String(), B_TRANSLATE("Damned!"), NULL, NULL, 118 B_WIDTH_AS_USUAL, B_STOP_ALERT); 119 alert->SetShortcut(0, B_ESCAPE); 120 121 alert->Go(); 122 } 123 } 124 delete fSettingsFile; 125 free(fName); 126 free(fSignature); 127 } 128 129 130 status_t 131 Preferences::MakeEmpty() 132 { 133 Lock(); 134 status_t status = BMessage::MakeEmpty(); 135 Unlock(); 136 return status; 137 } 138 139 140 void 141 Preferences::SaveWindowPosition(BWindow* window, const char* name) 142 { 143 Lock(); 144 145 BRect rect = window->Frame(); 146 if (HasPoint(name)) 147 ReplacePoint(name, rect.LeftTop()); 148 else 149 AddPoint(name, rect.LeftTop()); 150 151 Unlock(); 152 } 153 154 155 void 156 Preferences::LoadWindowPosition(BWindow* window, const char* name) 157 { 158 Lock(); 159 160 BPoint p; 161 if (FindPoint(name, &p) == B_OK) { 162 window->MoveTo(p); 163 make_window_visible(window); 164 } 165 166 Unlock(); 167 } 168 169 170 void 171 Preferences::SaveWindowFrame(BWindow* window, const char* name) 172 { 173 Lock(); 174 175 BRect rect = window->Frame(); 176 if (HasRect(name)) 177 ReplaceRect(name, rect); 178 else 179 AddRect(name, rect); 180 181 Unlock(); 182 } 183 184 185 void 186 Preferences::LoadWindowFrame(BWindow* window, const char* name) 187 { 188 Lock(); 189 190 BRect frame; 191 if (FindRect(name, &frame) == B_OK) { 192 window->MoveTo(frame.LeftTop()); 193 window->ResizeTo(frame.Width(), frame.Height()); 194 make_window_visible(window); 195 } 196 197 Unlock(); 198 } 199 200 201 void 202 Preferences::SaveInt32(int32 value, const char* name) 203 { 204 Lock(); 205 206 if (HasInt32(name)) 207 ReplaceInt32(name, value); 208 else 209 AddInt32(name, value); 210 211 Unlock(); 212 } 213 214 215 bool 216 Preferences::ReadInt32(int32 &val, const char* name) 217 { 218 Lock(); 219 int32 readVal; 220 bool found = FindInt32(name, &readVal) == B_OK; 221 if (found) 222 val = readVal; 223 Unlock(); 224 return found; 225 } 226 227 228 void 229 Preferences::SaveFloat(float val, const char* name) 230 { 231 Lock(); 232 if (HasFloat(name)) 233 ReplaceFloat(name, val); 234 else 235 AddFloat(name, val); 236 Unlock(); 237 } 238 239 240 bool 241 Preferences::ReadFloat(float &val, const char* name) 242 { 243 Lock(); 244 float readVal; 245 bool found = FindFloat(name, &readVal) == B_OK; 246 if (found) 247 val = readVal; 248 Unlock(); 249 return found; 250 } 251 252 253 void 254 Preferences::SaveRect(BRect& rect, const char* name) 255 { 256 Lock(); 257 if (HasRect(name)) 258 ReplaceRect(name, rect); 259 else 260 AddRect(name, rect); 261 Unlock(); 262 } 263 264 265 BRect & 266 Preferences::ReadRect(BRect& rect, const char* name) 267 { 268 Lock(); 269 BRect loaded; 270 if (FindRect(name, &loaded) == B_OK) 271 rect = loaded; 272 Unlock(); 273 return rect; 274 } 275 276 277 void 278 Preferences::SaveString(BString &string, const char* name) 279 { 280 Lock(); 281 if (HasString(name)) 282 ReplaceString(name, string); 283 else 284 AddString(name, string); 285 Unlock(); 286 } 287 288 289 void 290 Preferences::SaveString(const char* string, const char* name) 291 { 292 Lock(); 293 if (HasString(name)) 294 ReplaceString(name, string); 295 else 296 AddString(name, string); 297 Unlock(); 298 } 299 300 301 bool 302 Preferences::ReadString(BString &string, const char* name) 303 { 304 Lock(); 305 bool loaded = FindString(name, &string) == B_OK; 306 Unlock(); 307 return loaded; 308 } 309