BTRFS: Implement BTree::Path and change _Find.
[haiku.git] / src / apps / showimage / ShowImageSettings.cpp
blobcc55565dff36899db17dc2aa64c5c7bfd59743a6
1 /*
2 * Copyright 2003-2011 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Michael Pfeiffer, laplace@haiku-os.org
7 */
10 #include "ShowImageSettings.h"
12 #include <File.h>
13 #include <FindDirectory.h>
14 #include <Path.h>
17 ShowImageSettings::ShowImageSettings()
19 fLock("settings lock"),
20 fUpdated(false)
22 _Load();
26 ShowImageSettings::~ShowImageSettings()
28 if (Lock()) {
29 if (fUpdated)
30 _Save();
32 Unlock();
37 bool
38 ShowImageSettings::Lock()
40 return fLock.Lock();
44 void
45 ShowImageSettings::Unlock()
47 fLock.Unlock();
51 bool
52 ShowImageSettings::GetBool(const char* name, bool defaultValue)
54 bool value;
55 if (fSettings.FindBool(name, &value) == B_OK)
56 return value;
58 return defaultValue;
62 int32
63 ShowImageSettings::GetInt32(const char* name, int32 defaultValue)
65 int32 value;
66 if (fSettings.FindInt32(name, &value) == B_OK)
67 return value;
69 return defaultValue;
73 float
74 ShowImageSettings::GetFloat(const char* name, float defaultValue)
76 float value;
77 if (fSettings.FindFloat(name, &value) == B_OK)
78 return value;
80 return defaultValue;
84 BRect
85 ShowImageSettings::GetRect(const char* name, BRect defaultValue)
87 BRect value;
88 if (fSettings.FindRect(name, &value) == B_OK)
89 return value;
91 return defaultValue;
95 bigtime_t
96 ShowImageSettings::GetTime(const char* name, bigtime_t defaultValue)
98 int64 value;
99 if (fSettings.FindInt64(name, &value) == B_OK)
100 return value;
102 return defaultValue;
106 const char*
107 ShowImageSettings::GetString(const char* name, const char* defaultValue)
109 const char* value;
110 if (fSettings.FindString(name, &value) == B_OK)
111 return value;
113 return defaultValue;
117 void
118 ShowImageSettings::SetBool(const char* name, bool value)
120 if (fSettings.HasBool(name))
121 fSettings.ReplaceBool(name, value);
122 else
123 fSettings.AddBool(name, value);
125 fUpdated = true;
129 void
130 ShowImageSettings::SetInt32(const char* name, int32 value)
132 if (fSettings.HasInt32(name))
133 fSettings.ReplaceInt32(name, value);
134 else
135 fSettings.AddInt32(name, value);
137 fUpdated = true;
141 void
142 ShowImageSettings::SetFloat(const char* name, float value)
144 if (fSettings.HasFloat(name))
145 fSettings.ReplaceFloat(name, value);
146 else
147 fSettings.AddFloat(name, value);
149 fUpdated = true;
153 void
154 ShowImageSettings::SetRect(const char* name, BRect value)
156 if (fSettings.HasRect(name))
157 fSettings.ReplaceRect(name, value);
158 else
159 fSettings.AddRect(name, value);
161 fUpdated = true;
165 void
166 ShowImageSettings::SetTime(const char* name, bigtime_t value)
168 if (fSettings.ReplaceInt64(name, value) != B_OK)
169 fSettings.AddInt64(name, value);
171 fUpdated = true;
175 void
176 ShowImageSettings::SetString(const char* name, const char* value)
178 if (fSettings.HasString(name))
179 fSettings.ReplaceString(name, value);
180 else
181 fSettings.AddString(name, value);
183 fUpdated = true;
187 bool
188 ShowImageSettings::_OpenSettingsFile(BFile* file, bool forReading)
190 BPath path;
191 status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
192 if (status != B_OK)
193 return false;
195 path.Append("ShowImage_settings");
197 return file->SetTo(path.Path(), forReading
198 ? B_READ_ONLY : B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE) == B_OK;
202 void
203 ShowImageSettings::_Load()
205 BFile file;
206 if (_OpenSettingsFile(&file, true))
207 fSettings.Unflatten(&file);
211 void
212 ShowImageSettings::_Save()
214 BFile file;
215 if (_OpenSettingsFile(&file, false))
216 fSettings.Flatten(&file);