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"
30 #include <Directory.h>
32 #include <FindDirectory.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;
49 fSignature
= strdup(signature
);
53 if (find_directory(B_USER_SETTINGS_DIRECTORY
, &prefpath
) == B_OK
) {
54 BDirectory
prefdir(prefpath
.Path());
56 prefdir
.FindEntry(fName
, &entry
);
58 BFile
file(&entry
, B_READ_ONLY
);
59 if (file
.InitCheck() == B_OK
)
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;
76 fSignature
= strdup(signature
);
80 BFile
file(fSettingsFile
, B_READ_ONLY
);
81 if (file
.InitCheck() == B_OK
)
84 fNewPreferences
= true;
88 Preferences::~Preferences()
90 if (fSavePreferences
) {
93 file
.SetTo(fSettingsFile
, B_READ_WRITE
| B_CREATE_FILE
| B_ERASE_FILE
);
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
) {
105 file
.WriteAttr("BEOS:TYPE", B_MIME_STRING_TYPE
, 0, fSignature
,
106 strlen(fSignature
) + 1);
109 // implement saving somewhere else!
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
);
122 delete fSettingsFile
;
129 Preferences::MakeEmpty()
132 status_t status
= BMessage::MakeEmpty();
139 Preferences::SaveWindowPosition(BWindow
* window
, const char* name
)
143 BRect rect
= window
->Frame();
145 ReplacePoint(name
, rect
.LeftTop());
147 AddPoint(name
, rect
.LeftTop());
154 Preferences::LoadWindowPosition(BWindow
* window
, const char* name
)
159 if (FindPoint(name
, &p
) == B_OK
) {
161 make_window_visible(window
);
169 Preferences::SaveWindowFrame(BWindow
* window
, const char* name
)
173 BRect rect
= window
->Frame();
175 ReplaceRect(name
, rect
);
184 Preferences::LoadWindowFrame(BWindow
* window
, const char* name
)
189 if (FindRect(name
, &frame
) == B_OK
) {
190 window
->MoveTo(frame
.LeftTop());
191 window
->ResizeTo(frame
.Width(), frame
.Height());
192 make_window_visible(window
);
200 Preferences::SaveInt32(int32 value
, const char* name
)
205 ReplaceInt32(name
, value
);
207 AddInt32(name
, value
);
214 Preferences::ReadInt32(int32
&val
, const char* name
)
218 bool found
= FindInt32(name
, &readVal
) == B_OK
;
227 Preferences::SaveFloat(float val
, const char* name
)
231 ReplaceFloat(name
, val
);
239 Preferences::ReadFloat(float &val
, const char* name
)
243 bool found
= FindFloat(name
, &readVal
) == B_OK
;
252 Preferences::SaveRect(BRect
& rect
, const char* name
)
256 ReplaceRect(name
, rect
);
264 Preferences::ReadRect(BRect
& rect
, const char* name
)
268 if (FindRect(name
, &loaded
) == B_OK
)
276 Preferences::SaveString(BString
&string
, const char* name
)
280 ReplaceString(name
, string
);
282 AddString(name
, string
);
288 Preferences::SaveString(const char* string
, const char* name
)
292 ReplaceString(name
, string
);
294 AddString(name
, string
);
300 Preferences::ReadString(BString
&string
, const char* name
)
303 bool loaded
= FindString(name
, &string
) == B_OK
;