repository_infos: Enable automatic updates on the main Haiku repostiory.
[haiku.git] / src / apps / processcontroller / Preferences.cpp
blob3eb8d53bb98dcd3353f88e3fc185998c77d628cb
1 /*
2 ProcessController © 2000, Georges-Edouard Berenger, All Rights Reserved.
3 Copyright (C) 2004 beunited.org
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.
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.
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
21 #include "Preferences.h"
22 #include "Utilities.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
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>
37 #undef B_TRANSLATION_CONTEXT
38 #define B_TRANSLATION_CONTEXT "ProcessController"
40 Preferences::Preferences(const char* name, const char* signature, bool doSave)
41 : BMessage('Pref'), BLocker("Preferences", true),
42 fSavePreferences(doSave)
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;
53 if (find_directory(B_USER_SETTINGS_DIRECTORY, &prefpath) == B_OK) {
54 BDirectory prefdir(prefpath.Path());
55 BEntry entry;
56 prefdir.FindEntry(fName, &entry);
58 BFile file(&entry, B_READ_ONLY);
59 if (file.InitCheck() == B_OK)
60 Unflatten(&file);
61 else
62 fNewPreferences = true;
67 Preferences::Preferences(const entry_ref &ref, const char* signature, bool doSave)
68 : BMessage('Pref'), BLocker("Preferences", true),
69 fSavePreferences(doSave)
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;
80 BFile file(fSettingsFile, B_READ_ONLY);
81 if (file.InitCheck() == B_OK)
82 Unflatten(&file);
83 else
84 fNewPreferences = true;
88 Preferences::~Preferences()
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);
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);
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->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
119 alert->Go();
122 delete fSettingsFile;
123 free(fName);
124 free(fSignature);
128 status_t
129 Preferences::MakeEmpty()
131 Lock();
132 status_t status = BMessage::MakeEmpty();
133 Unlock();
134 return status;
138 void
139 Preferences::SaveWindowPosition(BWindow* window, const char* name)
141 Lock();
143 BRect rect = window->Frame();
144 if (HasPoint(name))
145 ReplacePoint(name, rect.LeftTop());
146 else
147 AddPoint(name, rect.LeftTop());
149 Unlock();
153 void
154 Preferences::LoadWindowPosition(BWindow* window, const char* name)
156 Lock();
158 BPoint p;
159 if (FindPoint(name, &p) == B_OK) {
160 window->MoveTo(p);
161 make_window_visible(window);
164 Unlock();
168 void
169 Preferences::SaveWindowFrame(BWindow* window, const char* name)
171 Lock();
173 BRect rect = window->Frame();
174 if (HasRect(name))
175 ReplaceRect(name, rect);
176 else
177 AddRect(name, rect);
179 Unlock();
183 void
184 Preferences::LoadWindowFrame(BWindow* window, const char* name)
186 Lock();
188 BRect frame;
189 if (FindRect(name, &frame) == B_OK) {
190 window->MoveTo(frame.LeftTop());
191 window->ResizeTo(frame.Width(), frame.Height());
192 make_window_visible(window);
195 Unlock();
199 void
200 Preferences::SaveInt32(int32 value, const char* name)
202 Lock();
204 if (HasInt32(name))
205 ReplaceInt32(name, value);
206 else
207 AddInt32(name, value);
209 Unlock();
213 bool
214 Preferences::ReadInt32(int32 &val, const char* name)
216 Lock();
217 int32 readVal;
218 bool found = FindInt32(name, &readVal) == B_OK;
219 if (found)
220 val = readVal;
221 Unlock();
222 return found;
226 void
227 Preferences::SaveFloat(float val, const char* name)
229 Lock();
230 if (HasFloat(name))
231 ReplaceFloat(name, val);
232 else
233 AddFloat(name, val);
234 Unlock();
238 bool
239 Preferences::ReadFloat(float &val, const char* name)
241 Lock();
242 float readVal;
243 bool found = FindFloat(name, &readVal) == B_OK;
244 if (found)
245 val = readVal;
246 Unlock();
247 return found;
251 void
252 Preferences::SaveRect(BRect& rect, const char* name)
254 Lock();
255 if (HasRect(name))
256 ReplaceRect(name, rect);
257 else
258 AddRect(name, rect);
259 Unlock();
263 BRect &
264 Preferences::ReadRect(BRect& rect, const char* name)
266 Lock();
267 BRect loaded;
268 if (FindRect(name, &loaded) == B_OK)
269 rect = loaded;
270 Unlock();
271 return rect;
275 void
276 Preferences::SaveString(BString &string, const char* name)
278 Lock();
279 if (HasString(name))
280 ReplaceString(name, string);
281 else
282 AddString(name, string);
283 Unlock();
287 void
288 Preferences::SaveString(const char* string, const char* name)
290 Lock();
291 if (HasString(name))
292 ReplaceString(name, string);
293 else
294 AddString(name, string);
295 Unlock();
299 bool
300 Preferences::ReadString(BString &string, const char* name)
302 Lock();
303 bool loaded = FindString(name, &string) == B_OK;
304 Unlock();
305 return loaded;