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//CONF_ADDPROP(BPath, AppPath)
20CONF_ADDPROP(entry_ref, AppPath)
21#undef CONF_ADDPROP
22
23AutoRaiseSettings::AutoRaiseSettings()
24{
25	_confActive = false;
26	_confDelay = DEFAULT_DELAY;
27	_confMode = Mode_All;
28	BPath prefPath;
29
30	find_directory(B_USER_SETTINGS_DIRECTORY, &prefPath);
31	prefPath.Append(SETTINGS_FILE);
32
33	_settingsFile.SetTo(prefPath.Path(), B_READ_WRITE | B_CREATE_FILE);
34
35	if (_settingsMessage.Unflatten(&_settingsFile) == B_OK){
36
37		if (_settingsMessage.FindBool(AR_ACTIVE, &_confActive) != B_OK)
38			printf("AutoRaiseSettings::AutoRaiseSettings();\tFailed to load active boolean from settings file. Using default\n");
39
40		if (_settingsMessage.FindInt64(AR_DELAY, &_confDelay) != B_OK)
41			printf("AutoRaiseSettings::AutoRaiseSettings();\tFailed to load delay from settings file. Using default\n");
42
43		if (_settingsMessage.FindInt32(AR_MODE, &_confMode) != B_OK)
44			printf("AutoRaiseSettings::AutoRaiseSettings();\tFailed to load mode from settings file. Using default\n");
45
46//		if (_settingsMessage.FindFlat(AR_APP_PATH, (BFlattenable *) &_appPath) != B_OK)
47//			printf("AutoRaiseSettings::AutoRaiseSettings();\tFailed to load application path.\n");
48
49		if (_settingsMessage.FindRef(AR_APP_PATH, &_confAppPath) != B_OK)
50			printf("AutoRaiseSettings::AutoRaiseSettings();\tFailed to load application path.\n");
51	}
52	else
53	{
54		printf("AutoRaiseSettings::AutoRaiseSettings()\nUnable to open settings file (either corrupted or doesn't exist), using defaults.\n");
55	}
56
57	_settingsFile.Unset();
58}
59
60AutoRaiseSettings::~AutoRaiseSettings()
61{
62	BPath prefPath;
63	find_directory(B_USER_SETTINGS_DIRECTORY, &prefPath);
64	prefPath.Append(SETTINGS_FILE);
65
66	//clobber existing settings and write in new ones
67	_settingsFile.SetTo(prefPath.Path(), B_READ_WRITE | B_ERASE_FILE);
68
69
70	//empty message and refill it with whatever has been set
71	_settingsMessage.MakeEmpty();
72
73	_settingsMessage.AddBool(AR_ACTIVE, _confActive);
74	_settingsMessage.AddInt64(AR_DELAY, _confDelay);
75	_settingsMessage.AddInt32(AR_MODE, _confMode);
76//	_settingsMessage.AddFlat(AR_APP_PATH, &_appPath);
77	_settingsMessage.AddRef(AR_APP_PATH, &_confAppPath);
78
79	//write message to settings file
80	if (_settingsMessage.Flatten(&_settingsFile) != B_OK)
81		printf("Error occurred writing settings\n");
82
83	_settingsFile.Unset();
84}
85
86