xref: /haiku/src/apps/processcontroller/Preferences.cpp (revision 2600324b57fa31cdea1627d584d314f2a579c4a8)
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 <Directory.h>
30 #include <File.h>
31 #include <FindDirectory.h>
32 #include <Locker.h>
33 #include <Mime.h>
34 #include <Path.h>
35 
36 
37 Preferences::Preferences(const char* name, const char* signature, bool doSave)
38 	: BMessage('Pref'), BLocker("Preferences", true),
39 	fSavePreferences(doSave)
40 {
41 	fNewPreferences = false;
42 	fSettingsFile = 0;
43 	BPath prefpath;
44 	fName = strdup(name);
45 	if (signature)
46 		fSignature = strdup(signature);
47 	else
48 		fSignature = NULL;
49 
50 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &prefpath) == B_OK) {
51 		BDirectory prefdir(prefpath.Path());
52 		BEntry entry;
53 		prefdir.FindEntry(fName, &entry);
54 
55 		BFile file(&entry, B_READ_ONLY);
56 		if (file.InitCheck() == B_OK)
57 			Unflatten(&file);
58 		else
59 			fNewPreferences = true;
60 	}
61 }
62 
63 
64 Preferences::Preferences(const entry_ref &ref, const char* signature, bool doSave)
65 	: BMessage('Pref'), BLocker("Preferences", true),
66 	fSavePreferences(doSave)
67 {
68 	fSettingsFile = new entry_ref(ref);
69 	fNewPreferences = false;
70 	BPath prefpath;
71 	fName = NULL;
72 	if (signature)
73 		fSignature = strdup(signature);
74 	else
75 		fSignature = NULL;
76 
77 	BFile file(fSettingsFile, B_READ_ONLY);
78 	if (file.InitCheck() == B_OK)
79 		Unflatten(&file);
80 	else
81 		fNewPreferences = true;
82 }
83 
84 
85 Preferences::~Preferences()
86 {
87 	if (fSavePreferences) {
88 		BFile file;
89 		status_t set = B_ERROR;
90 		if (fSettingsFile)
91 			file.SetTo (fSettingsFile, B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE);
92 		else {
93 			BPath prefpath;
94 			if (find_directory(B_USER_SETTINGS_DIRECTORY, &prefpath, true) == B_OK) {
95 				BDirectory prefdir(prefpath.Path());
96 				set = prefdir.CreateFile(fName, &file, false);
97 			}
98 		}
99 
100 		if (file.InitCheck () == B_OK) {
101 			Flatten(&file);
102 			if (fSignature) {
103 				file.WriteAttr("BEOS:TYPE", B_MIME_STRING_TYPE, 0, fSignature,
104 					strlen(fSignature) + 1);
105 			}
106 		} else {
107 			// implement saving somewhere else!
108 			char error[1024];
109 			sprintf(error, "Your setting file could not be saved!\n(%s)",
110 				strerror(file.InitCheck()));
111 			BAlert *alert = new BAlert("Error saving file", error,
112 				"Damned!", NULL, NULL, B_WIDTH_AS_USUAL, B_STOP_ALERT);
113 			alert->Go();
114 		}
115 	}
116 	free(fName);
117 	free(fSignature);
118 }
119 
120 
121 status_t
122 Preferences::MakeEmpty()
123 {
124 	Lock();
125 	status_t status = BMessage::MakeEmpty();
126 	Unlock();
127 	return status;
128 }
129 
130 
131 void
132 Preferences::SaveWindowPosition(BWindow* window, const char* name)
133 {
134 	Lock();
135 
136 	BRect rect = window->Frame();
137 	if (HasPoint(name))
138 		ReplacePoint(name, rect.LeftTop());
139 	else
140 		AddPoint(name, rect.LeftTop());
141 
142 	Unlock();
143 }
144 
145 
146 void
147 Preferences::LoadWindowPosition(BWindow* window, const char* name)
148 {
149 	Lock();
150 
151 	BPoint p;
152 	if (FindPoint(name, &p) == B_OK) {
153 		window->MoveTo(p);
154 		make_window_visible(window);
155 	}
156 
157 	Unlock();
158 }
159 
160 
161 void
162 Preferences::SaveWindowFrame(BWindow* window, const char* name)
163 {
164 	Lock();
165 
166 	BRect rect = window->Frame();
167 	if (HasRect(name))
168 		ReplaceRect(name, rect);
169 	else
170 		AddRect(name, rect);
171 
172 	Unlock();
173 }
174 
175 
176 void
177 Preferences::LoadWindowFrame(BWindow* window, const char* name)
178 {
179 	Lock();
180 
181 	BRect frame;
182 	if (FindRect(name, &frame) == B_OK) {
183 		window->MoveTo(frame.LeftTop());
184 		window->ResizeTo(frame.Width(), frame.Height());
185 		make_window_visible(window);
186 	}
187 
188 	Unlock();
189 }
190 
191 
192 void
193 Preferences::SaveInt32(int32 value, const char* name)
194 {
195 	Lock();
196 
197 	if (HasInt32(name))
198 		ReplaceInt32(name, value);
199 	else
200 		AddInt32(name, value);
201 
202 	Unlock();
203 }
204 
205 
206 bool
207 Preferences::ReadInt32(int32 &val, const char* name)
208 {
209 	Lock();
210 	int32 readVal;
211 	bool found = FindInt32(name, &readVal) == B_OK;
212 	if (found)
213 		val = readVal;
214 	Unlock();
215 	return found;
216 }
217 
218 
219 void
220 Preferences::SaveFloat(float val, const char* name)
221 {
222 	Lock();
223 	if (HasFloat(name))
224 		ReplaceFloat(name, val);
225 	else
226 		AddFloat(name, val);
227 	Unlock();
228 }
229 
230 
231 bool
232 Preferences::ReadFloat(float &val, const char* name)
233 {
234 	Lock();
235 	float readVal;
236 	bool found = FindFloat(name, &readVal) == B_OK;
237 	if (found)
238 		val = readVal;
239 	Unlock();
240 	return found;
241 }
242 
243 
244 void
245 Preferences::SaveRect(BRect& rect, const char* name)
246 {
247 	Lock();
248 	if (HasRect(name))
249 		ReplaceRect(name, rect);
250 	else
251 		AddRect(name, rect);
252 	Unlock();
253 }
254 
255 
256 BRect &
257 Preferences::ReadRect(BRect& rect, const char* name)
258 {
259 	Lock();
260 	BRect loaded;
261 	if (FindRect(name, &loaded) == B_OK)
262 		rect = loaded;
263 	Unlock();
264 	return rect;
265 }
266 
267 
268 void
269 Preferences::SaveString(BString &string, const char* name)
270 {
271 	Lock();
272 	if (HasString(name))
273 		ReplaceString(name, string);
274 	else
275 		AddString(name, string);
276 	Unlock();
277 }
278 
279 
280 void
281 Preferences::SaveString(const char* string, const char* name)
282 {
283 	Lock();
284 	if (HasString(name))
285 		ReplaceString(name, string);
286 	else
287 		AddString(name, string);
288 	Unlock();
289 }
290 
291 
292 bool
293 Preferences::ReadString(BString &string, const char* name)
294 {
295 	Lock();
296 	bool loaded = FindString(name, &string) == B_OK;
297 	Unlock();
298 	return loaded;
299 }
300