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