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