1 /*
2 * Copyright 2003-2011 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 * Michael Pfeiffer, laplace@haiku-os.org
7 */
8
9
10 #include "ShowImageSettings.h"
11
12 #include <File.h>
13 #include <FindDirectory.h>
14 #include <Path.h>
15
16
ShowImageSettings()17 ShowImageSettings::ShowImageSettings()
18 :
19 fLock("settings lock"),
20 fUpdated(false)
21 {
22 _Load();
23 }
24
25
~ShowImageSettings()26 ShowImageSettings::~ShowImageSettings()
27 {
28 if (Lock()) {
29 if (fUpdated)
30 _Save();
31
32 Unlock();
33 }
34 }
35
36
37 bool
Lock()38 ShowImageSettings::Lock()
39 {
40 return fLock.Lock();
41 }
42
43
44 void
Unlock()45 ShowImageSettings::Unlock()
46 {
47 fLock.Unlock();
48 }
49
50
51 bool
GetBool(const char * name,bool defaultValue)52 ShowImageSettings::GetBool(const char* name, bool defaultValue)
53 {
54 bool value;
55 if (fSettings.FindBool(name, &value) == B_OK)
56 return value;
57
58 return defaultValue;
59 }
60
61
62 int32
GetInt32(const char * name,int32 defaultValue)63 ShowImageSettings::GetInt32(const char* name, int32 defaultValue)
64 {
65 int32 value;
66 if (fSettings.FindInt32(name, &value) == B_OK)
67 return value;
68
69 return defaultValue;
70 }
71
72
73 float
GetFloat(const char * name,float defaultValue)74 ShowImageSettings::GetFloat(const char* name, float defaultValue)
75 {
76 float value;
77 if (fSettings.FindFloat(name, &value) == B_OK)
78 return value;
79
80 return defaultValue;
81 }
82
83
84 BRect
GetRect(const char * name,BRect defaultValue)85 ShowImageSettings::GetRect(const char* name, BRect defaultValue)
86 {
87 BRect value;
88 if (fSettings.FindRect(name, &value) == B_OK)
89 return value;
90
91 return defaultValue;
92 }
93
94
95 bigtime_t
GetTime(const char * name,bigtime_t defaultValue)96 ShowImageSettings::GetTime(const char* name, bigtime_t defaultValue)
97 {
98 int64 value;
99 if (fSettings.FindInt64(name, &value) == B_OK)
100 return value;
101
102 return defaultValue;
103 }
104
105
106 const char*
GetString(const char * name,const char * defaultValue)107 ShowImageSettings::GetString(const char* name, const char* defaultValue)
108 {
109 const char* value;
110 if (fSettings.FindString(name, &value) == B_OK)
111 return value;
112
113 return defaultValue;
114 }
115
116
117 void
SetBool(const char * name,bool value)118 ShowImageSettings::SetBool(const char* name, bool value)
119 {
120 if (fSettings.HasBool(name))
121 fSettings.ReplaceBool(name, value);
122 else
123 fSettings.AddBool(name, value);
124
125 fUpdated = true;
126 }
127
128
129 void
SetInt32(const char * name,int32 value)130 ShowImageSettings::SetInt32(const char* name, int32 value)
131 {
132 if (fSettings.HasInt32(name))
133 fSettings.ReplaceInt32(name, value);
134 else
135 fSettings.AddInt32(name, value);
136
137 fUpdated = true;
138 }
139
140
141 void
SetFloat(const char * name,float value)142 ShowImageSettings::SetFloat(const char* name, float value)
143 {
144 if (fSettings.HasFloat(name))
145 fSettings.ReplaceFloat(name, value);
146 else
147 fSettings.AddFloat(name, value);
148
149 fUpdated = true;
150 }
151
152
153 void
SetRect(const char * name,BRect value)154 ShowImageSettings::SetRect(const char* name, BRect value)
155 {
156 if (fSettings.HasRect(name))
157 fSettings.ReplaceRect(name, value);
158 else
159 fSettings.AddRect(name, value);
160
161 fUpdated = true;
162 }
163
164
165 void
SetTime(const char * name,bigtime_t value)166 ShowImageSettings::SetTime(const char* name, bigtime_t value)
167 {
168 if (fSettings.ReplaceInt64(name, value) != B_OK)
169 fSettings.AddInt64(name, value);
170
171 fUpdated = true;
172 }
173
174
175 void
SetString(const char * name,const char * value)176 ShowImageSettings::SetString(const char* name, const char* value)
177 {
178 if (fSettings.HasString(name))
179 fSettings.ReplaceString(name, value);
180 else
181 fSettings.AddString(name, value);
182
183 fUpdated = true;
184 }
185
186
187 bool
_OpenSettingsFile(BFile * file,bool forReading)188 ShowImageSettings::_OpenSettingsFile(BFile* file, bool forReading)
189 {
190 BPath path;
191 status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
192 if (status != B_OK)
193 return false;
194
195 path.Append("ShowImage_settings");
196
197 return file->SetTo(path.Path(), forReading
198 ? B_READ_ONLY : B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE) == B_OK;
199 }
200
201
202 void
_Load()203 ShowImageSettings::_Load()
204 {
205 BFile file;
206 if (_OpenSettingsFile(&file, true))
207 fSettings.Unflatten(&file);
208 }
209
210
211 void
_Save()212 ShowImageSettings::_Save()
213 {
214 BFile file;
215 if (_OpenSettingsFile(&file, false))
216 fSettings.Flatten(&file);
217 }
218