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
17ShowImageSettings::ShowImageSettings()
18	:
19	fLock("settings lock"),
20	fUpdated(false)
21{
22	_Load();
23}
24
25
26ShowImageSettings::~ShowImageSettings()
27{
28	if (Lock()) {
29		if (fUpdated)
30			_Save();
31
32		Unlock();
33	}
34}
35
36
37bool
38ShowImageSettings::Lock()
39{
40	return fLock.Lock();
41}
42
43
44void
45ShowImageSettings::Unlock()
46{
47	fLock.Unlock();
48}
49
50
51bool
52ShowImageSettings::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
62int32
63ShowImageSettings::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
73float
74ShowImageSettings::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
84BRect
85ShowImageSettings::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
95bigtime_t
96ShowImageSettings::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
106const char*
107ShowImageSettings::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
117void
118ShowImageSettings::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
129void
130ShowImageSettings::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
141void
142ShowImageSettings::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
153void
154ShowImageSettings::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
165void
166ShowImageSettings::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
175void
176ShowImageSettings::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
187bool
188ShowImageSettings::_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
202void
203ShowImageSettings::_Load()
204{
205	BFile file;
206	if (_OpenSettingsFile(&file, true))
207		fSettings.Unflatten(&file);
208}
209
210
211void
212ShowImageSettings::_Save()
213{
214	BFile file;
215	if (_OpenSettingsFile(&file, false))
216		fSettings.Flatten(&file);
217}
218