xref: /haiku/src/apps/processcontroller/Preferences.cpp (revision e688bf23d48bfd1216a0cacbdbda5e35a1bcd779)
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_TRANSLATION_CONTEXT
38 #define B_TRANSLATION_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->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
119 			alert->Go();
120 		}
121 	}
122 	delete fSettingsFile;
123 	free(fName);
124 	free(fSignature);
125 }
126 
127 
128 status_t
129 Preferences::MakeEmpty()
130 {
131 	Lock();
132 	status_t status = BMessage::MakeEmpty();
133 	Unlock();
134 	return status;
135 }
136 
137 
138 void
139 Preferences::SaveWindowPosition(BWindow* window, const char* name)
140 {
141 	Lock();
142 
143 	BRect rect = window->Frame();
144 	if (HasPoint(name))
145 		ReplacePoint(name, rect.LeftTop());
146 	else
147 		AddPoint(name, rect.LeftTop());
148 
149 	Unlock();
150 }
151 
152 
153 void
154 Preferences::LoadWindowPosition(BWindow* window, const char* name)
155 {
156 	Lock();
157 
158 	BPoint p;
159 	if (FindPoint(name, &p) == B_OK) {
160 		window->MoveTo(p);
161 		make_window_visible(window);
162 	}
163 
164 	Unlock();
165 }
166 
167 
168 void
169 Preferences::SaveWindowFrame(BWindow* window, const char* name)
170 {
171 	Lock();
172 
173 	BRect rect = window->Frame();
174 	if (HasRect(name))
175 		ReplaceRect(name, rect);
176 	else
177 		AddRect(name, rect);
178 
179 	Unlock();
180 }
181 
182 
183 void
184 Preferences::LoadWindowFrame(BWindow* window, const char* name)
185 {
186 	Lock();
187 
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);
193 	}
194 
195 	Unlock();
196 }
197 
198 
199 void
200 Preferences::SaveInt32(int32 value, const char* name)
201 {
202 	Lock();
203 
204 	if (HasInt32(name))
205 		ReplaceInt32(name, value);
206 	else
207 		AddInt32(name, value);
208 
209 	Unlock();
210 }
211 
212 
213 bool
214 Preferences::ReadInt32(int32 &val, const char* name)
215 {
216 	Lock();
217 	int32 readVal;
218 	bool found = FindInt32(name, &readVal) == B_OK;
219 	if (found)
220 		val = readVal;
221 	Unlock();
222 	return found;
223 }
224 
225 
226 void
227 Preferences::SaveFloat(float val, const char* name)
228 {
229 	Lock();
230 	if (HasFloat(name))
231 		ReplaceFloat(name, val);
232 	else
233 		AddFloat(name, val);
234 	Unlock();
235 }
236 
237 
238 bool
239 Preferences::ReadFloat(float &val, const char* name)
240 {
241 	Lock();
242 	float readVal;
243 	bool found = FindFloat(name, &readVal) == B_OK;
244 	if (found)
245 		val = readVal;
246 	Unlock();
247 	return found;
248 }
249 
250 
251 void
252 Preferences::SaveRect(BRect& rect, const char* name)
253 {
254 	Lock();
255 	if (HasRect(name))
256 		ReplaceRect(name, rect);
257 	else
258 		AddRect(name, rect);
259 	Unlock();
260 }
261 
262 
263 BRect &
264 Preferences::ReadRect(BRect& rect, const char* name)
265 {
266 	Lock();
267 	BRect loaded;
268 	if (FindRect(name, &loaded) == B_OK)
269 		rect = loaded;
270 	Unlock();
271 	return rect;
272 }
273 
274 
275 void
276 Preferences::SaveString(BString &string, const char* name)
277 {
278 	Lock();
279 	if (HasString(name))
280 		ReplaceString(name, string);
281 	else
282 		AddString(name, string);
283 	Unlock();
284 }
285 
286 
287 void
288 Preferences::SaveString(const char* string, const char* name)
289 {
290 	Lock();
291 	if (HasString(name))
292 		ReplaceString(name, string);
293 	else
294 		AddString(name, string);
295 	Unlock();
296 }
297 
298 
299 bool
300 Preferences::ReadString(BString &string, const char* name)
301 {
302 	Lock();
303 	bool loaded = FindString(name, &string) == B_OK;
304 	Unlock();
305 	return loaded;
306 }
307