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