2 * Copyright 2008-2010, Stephan Aßmus <superstippi@gmx.de>.
3 * Copyright 1998, Eric Shepherd.
4 * All rights reserved. Distributed under the terms of the Be Sample Code
8 //! Be Newsletter Volume II, Issue 35; September 2, 1998 (Eric Shepherd)
10 #include "SettingsMessage.h"
17 #include <Messenger.h>
21 SettingsMessage::SettingsMessage(directory_which directory
,
27 fStatus
= find_directory(directory
, &fPath
);
30 fStatus
= fPath
.Append(filename
);
37 SettingsMessage::~SettingsMessage()
41 for (int32 i
= fListeners
.CountItems() - 1; i
>= 0; i
--)
42 delete reinterpret_cast<BMessenger
*>(fListeners
.ItemAtFast(i
));
47 SettingsMessage::InitCheck() const
54 SettingsMessage::Load()
58 BFile
file(fPath
.Path(), B_READ_ONLY
);
59 status_t status
= file
.InitCheck();
62 status
= Unflatten(&file
);
69 SettingsMessage::Save() const
71 BAutolock
_(const_cast<SettingsMessage
*>(this));
73 BFile
file(fPath
.Path(), B_WRITE_ONLY
| B_CREATE_FILE
| B_ERASE_FILE
);
74 status_t status
= file
.InitCheck();
77 status
= Flatten(&file
);
84 SettingsMessage::AddListener(const BMessenger
& listener
)
88 BMessenger
* listenerCopy
= new(std::nothrow
) BMessenger(listener
);
89 if (listenerCopy
&& fListeners
.AddItem(listenerCopy
))
97 SettingsMessage::RemoveListener(const BMessenger
& listener
)
101 for (int32 i
= fListeners
.CountItems() - 1; i
>= 0; i
--) {
102 BMessenger
* listenerItem
= reinterpret_cast<BMessenger
*>(
103 fListeners
.ItemAtFast(i
));
104 if (*listenerItem
== listener
) {
105 fListeners
.RemoveItem(i
);
117 SettingsMessage::SetValue(const char* name
, bool value
)
119 status_t ret
= ReplaceBool(name
, value
);
121 ret
= AddBool(name
, value
);
123 _NotifyValueChanged(name
);
129 SettingsMessage::SetValue(const char* name
, int8 value
)
131 status_t ret
= ReplaceInt8(name
, value
);
133 ret
= AddInt8(name
, value
);
135 _NotifyValueChanged(name
);
141 SettingsMessage::SetValue(const char* name
, int16 value
)
143 status_t ret
= ReplaceInt16(name
, value
);
145 ret
= AddInt16(name
, value
);
147 _NotifyValueChanged(name
);
153 SettingsMessage::SetValue(const char* name
, int32 value
)
155 status_t ret
= ReplaceInt32(name
, value
);
157 ret
= AddInt32(name
, value
);
159 _NotifyValueChanged(name
);
165 SettingsMessage::SetValue(const char* name
, uint32 value
)
167 status_t ret
= ReplaceUInt32(name
, value
);
169 ret
= AddUInt32(name
, value
);
171 _NotifyValueChanged(name
);
177 SettingsMessage::SetValue(const char* name
, int64 value
)
179 status_t ret
= ReplaceInt64(name
, value
);
181 ret
= AddInt64(name
, value
);
183 _NotifyValueChanged(name
);
189 SettingsMessage::SetValue(const char* name
, float value
)
191 status_t ret
= ReplaceFloat(name
, value
);
193 ret
= AddFloat(name
, value
);
195 _NotifyValueChanged(name
);
201 SettingsMessage::SetValue(const char* name
, double value
)
203 status_t ret
= ReplaceDouble(name
, value
);
205 ret
= AddDouble(name
, value
);
207 _NotifyValueChanged(name
);
213 SettingsMessage::SetValue(const char* name
, const char* value
)
215 status_t ret
= ReplaceString(name
, value
);
217 ret
= AddString(name
, value
);
219 _NotifyValueChanged(name
);
225 SettingsMessage::SetValue(const char* name
, const BString
& value
)
227 status_t ret
= ReplaceString(name
, value
);
229 ret
= AddString(name
, value
);
231 _NotifyValueChanged(name
);
237 SettingsMessage::SetValue(const char* name
, const BPoint
& value
)
239 status_t ret
= ReplacePoint(name
, value
);
241 ret
= AddPoint(name
, value
);
243 _NotifyValueChanged(name
);
249 SettingsMessage::SetValue(const char* name
, const BRect
& value
)
251 status_t ret
= ReplaceRect(name
, value
);
253 ret
= AddRect(name
, value
);
255 _NotifyValueChanged(name
);
261 SettingsMessage::SetValue(const char* name
, const entry_ref
& value
)
263 status_t ret
= ReplaceRef(name
, &value
);
265 ret
= AddRef(name
, &value
);
267 _NotifyValueChanged(name
);
273 SettingsMessage::SetValue(const char* name
, const BMessage
& value
)
275 status_t ret
= ReplaceMessage(name
, &value
);
277 ret
= AddMessage(name
, &value
);
279 _NotifyValueChanged(name
);
285 SettingsMessage::SetValue(const char* name
, const BFlattenable
* value
)
287 status_t ret
= ReplaceFlat(name
, const_cast<BFlattenable
*>(value
));
289 ret
= AddFlat(name
, const_cast<BFlattenable
*>(value
));
291 _NotifyValueChanged(name
);
297 SettingsMessage::SetValue(const char* name
, const BFont
& value
)
301 value
.GetFamilyAndStyle(&family
, &style
);
303 BMessage fontMessage
;
304 status_t ret
= fontMessage
.AddString("family", family
);
306 ret
= fontMessage
.AddString("style", style
);
308 ret
= fontMessage
.AddFloat("size", value
.Size());
311 if (ReplaceMessage(name
, &fontMessage
) != B_OK
)
312 ret
= AddMessage(name
, &fontMessage
);
315 _NotifyValueChanged(name
);
324 SettingsMessage::GetValue(const char* name
, bool defaultValue
) const
327 if (FindBool(name
, &value
) != B_OK
)
334 SettingsMessage::GetValue(const char* name
, int8 defaultValue
) const
337 if (FindInt8(name
, &value
) != B_OK
)
344 SettingsMessage::GetValue(const char* name
, int16 defaultValue
) const
347 if (FindInt16(name
, &value
) != B_OK
)
354 SettingsMessage::GetValue(const char* name
, int32 defaultValue
) const
357 if (FindInt32(name
, &value
) != B_OK
)
364 SettingsMessage::GetValue(const char* name
, uint32 defaultValue
) const
367 if (FindUInt32(name
, &value
) != B_OK
)
374 SettingsMessage::GetValue(const char* name
, int64 defaultValue
) const
377 if (FindInt64(name
, &value
) != B_OK
)
384 SettingsMessage::GetValue(const char* name
, float defaultValue
) const
387 if (FindFloat(name
, &value
) != B_OK
)
394 SettingsMessage::GetValue(const char* name
, double defaultValue
) const
397 if (FindDouble(name
, &value
) != B_OK
)
404 SettingsMessage::GetValue(const char* name
, const BString
& defaultValue
) const
407 if (FindString(name
, &value
) != B_OK
)
414 SettingsMessage::GetValue(const char* name
, const char* defaultValue
) const
417 if (FindString(name
, &value
) != B_OK
)
424 SettingsMessage::GetValue(const char *name
, BPoint defaultValue
) const
427 if (FindPoint(name
, &value
) != B_OK
)
434 SettingsMessage::GetValue(const char* name
, BRect defaultValue
) const
437 if (FindRect(name
, &value
) != B_OK
)
444 SettingsMessage::GetValue(const char* name
, const entry_ref
& defaultValue
) const
447 if (FindRef(name
, &value
) != B_OK
)
454 SettingsMessage::GetValue(const char* name
, const BMessage
& defaultValue
) const
457 if (FindMessage(name
, &value
) != B_OK
)
464 SettingsMessage::GetValue(const char* name
, const BFont
& defaultValue
) const
466 BMessage fontMessage
;
467 if (FindMessage(name
, &fontMessage
) != B_OK
)
473 if (fontMessage
.FindString("family", &family
) != B_OK
474 || fontMessage
.FindString("style", &style
) != B_OK
475 || fontMessage
.FindFloat("size", &size
) != B_OK
) {
480 if (value
.SetFamilyAndStyle(family
, style
) != B_OK
)
489 // #pragma mark - private
493 SettingsMessage::_NotifyValueChanged(const char* name
) const
495 BMessage
message(SETTINGS_VALUE_CHANGED
);
496 message
.AddString("name", name
);
498 // Add the value of that name to the notification.
500 if (GetInfo(name
, &type
) == B_OK
) {
503 if (FindData(name
, type
, &data
, &numBytes
) == B_OK
)
504 message
.AddData("value", type
, data
, numBytes
);
507 int32 count
= fListeners
.CountItems();
508 for (int32 i
= 0; i
< count
; i
++) {
509 BMessenger
* listener
= reinterpret_cast<BMessenger
*>(
510 fListeners
.ItemAtFast(i
));
511 listener
->SendMessage(&message
);