1fe8567ecSFrançois Revol #include "settings.h"
2*2708c3e1SJonas Sundström #include <iostream>
3fe8567ecSFrançois Revol
4fe8567ecSFrançois Revol #define CONF_ADDPROP(_type, _name) \
5fe8567ecSFrançois Revol void AutoRaiseSettings::Set##_name(_type value)\
6fe8567ecSFrançois Revol {\
7fe8567ecSFrançois Revol _conf##_name = value;\
8fe8567ecSFrançois Revol }\
9fe8567ecSFrançois Revol \
10*2708c3e1SJonas Sundström _type AutoRaiseSettings::_name()\
11fe8567ecSFrançois Revol {\
12fe8567ecSFrançois Revol return _conf##_name;\
13fe8567ecSFrançois Revol }\
14fe8567ecSFrançois Revol
15fe8567ecSFrançois Revol
CONF_ADDPROP(bool,Active)16fe8567ecSFrançois Revol CONF_ADDPROP(bool, Active)
17fe8567ecSFrançois Revol CONF_ADDPROP(bigtime_t, Delay)
18fe8567ecSFrançois Revol CONF_ADDPROP(int32, Mode)
19fe8567ecSFrançois Revol #undef CONF_ADDPROP
20fe8567ecSFrançois Revol
21fe8567ecSFrançois Revol AutoRaiseSettings::AutoRaiseSettings()
22fe8567ecSFrançois Revol {
23fe8567ecSFrançois Revol _confActive = false;
24fe8567ecSFrançois Revol _confDelay = DEFAULT_DELAY;
25fe8567ecSFrançois Revol _confMode = Mode_All;
26fe8567ecSFrançois Revol BPath prefPath;
27fe8567ecSFrançois Revol
28fe8567ecSFrançois Revol find_directory(B_USER_SETTINGS_DIRECTORY, &prefPath);
29fe8567ecSFrançois Revol prefPath.Append(SETTINGS_FILE);
30fe8567ecSFrançois Revol
31fe8567ecSFrançois Revol _settingsFile.SetTo(prefPath.Path(), B_READ_WRITE | B_CREATE_FILE);
32fe8567ecSFrançois Revol
33fe8567ecSFrançois Revol if (_settingsMessage.Unflatten(&_settingsFile) == B_OK){
34fe8567ecSFrançois Revol
35fe8567ecSFrançois Revol if (_settingsMessage.FindBool(AR_ACTIVE, &_confActive) != B_OK)
36fe8567ecSFrançois Revol printf("AutoRaiseSettings::AutoRaiseSettings();\tFailed to load active boolean from settings file. Using default\n");
37fe8567ecSFrançois Revol
38fe8567ecSFrançois Revol if (_settingsMessage.FindInt64(AR_DELAY, &_confDelay) != B_OK)
39fe8567ecSFrançois Revol printf("AutoRaiseSettings::AutoRaiseSettings();\tFailed to load delay from settings file. Using default\n");
40fe8567ecSFrançois Revol
41fe8567ecSFrançois Revol if (_settingsMessage.FindInt32(AR_MODE, &_confMode) != B_OK)
42fe8567ecSFrançois Revol printf("AutoRaiseSettings::AutoRaiseSettings();\tFailed to load mode from settings file. Using default\n");
43fe8567ecSFrançois Revol }
44fe8567ecSFrançois Revol else
45fe8567ecSFrançois Revol {
46fe8567ecSFrançois Revol printf("AutoRaiseSettings::AutoRaiseSettings()\nUnable to open settings file (either corrupted or doesn't exist), using defaults.\n");
47fe8567ecSFrançois Revol }
48fe8567ecSFrançois Revol
49fe8567ecSFrançois Revol _settingsFile.Unset();
50fe8567ecSFrançois Revol }
51fe8567ecSFrançois Revol
~AutoRaiseSettings()52fe8567ecSFrançois Revol AutoRaiseSettings::~AutoRaiseSettings()
53fe8567ecSFrançois Revol {
54fe8567ecSFrançois Revol BPath prefPath;
55fe8567ecSFrançois Revol find_directory(B_USER_SETTINGS_DIRECTORY, &prefPath);
56fe8567ecSFrançois Revol prefPath.Append(SETTINGS_FILE);
57fe8567ecSFrançois Revol
58fe8567ecSFrançois Revol //clobber existing settings and write in new ones
59fe8567ecSFrançois Revol _settingsFile.SetTo(prefPath.Path(), B_READ_WRITE | B_ERASE_FILE);
60fe8567ecSFrançois Revol
61fe8567ecSFrançois Revol
62fe8567ecSFrançois Revol //empty message and refill it with whatever has been set
63fe8567ecSFrançois Revol _settingsMessage.MakeEmpty();
64fe8567ecSFrançois Revol
65fe8567ecSFrançois Revol _settingsMessage.AddBool(AR_ACTIVE, _confActive);
66fe8567ecSFrançois Revol _settingsMessage.AddInt64(AR_DELAY, _confDelay);
67fe8567ecSFrançois Revol _settingsMessage.AddInt32(AR_MODE, _confMode);
68fe8567ecSFrançois Revol
69fe8567ecSFrançois Revol //write message to settings file
70fe8567ecSFrançois Revol if (_settingsMessage.Flatten(&_settingsFile) != B_OK)
71fe8567ecSFrançois Revol printf("Error occurred writing settings\n");
72fe8567ecSFrançois Revol
73fe8567ecSFrançois Revol _settingsFile.Unset();
74fe8567ecSFrançois Revol }
75fe8567ecSFrançois Revol
76