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