xref: /haiku/src/kits/screensaver/ScreenSaverSettings.cpp (revision 1d9d47fc72028bb71b5f232a877231e59cfe2438)
1 /*
2  * Copyright 2003-2006, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Michael Phipps
7  *		Jérôme Duval, jerome.duval@free.fr
8  */
9 
10 
11 #include "ScreenSaverSettings.h"
12 
13 #include <Debug.h>
14 #include <File.h>
15 #include <FindDirectory.h>
16 #include <Path.h>
17 #include <StorageDefs.h>
18 #include <String.h>
19 
20 #include <string.h>
21 
22 
23 ScreenSaverSettings::ScreenSaverSettings()
24 {
25 	BPath path;
26   	find_directory(B_USER_SETTINGS_DIRECTORY, &path);
27 
28 	fSettingsPath = path;
29 	fSettingsPath.Append("ScreenSaver_settings", true);
30 	fNetworkPath = path;
31   	fNetworkPath.Append("network", true);
32 
33 	Defaults();
34 }
35 
36 
37 //! Load the flattened settings BMessage from disk and parse it.
38 bool
39 ScreenSaverSettings::Load()
40 {
41 	BFile file(fSettingsPath.Path(), B_READ_ONLY);
42 	if (file.InitCheck() != B_OK)
43 		return false;
44 
45 	// File exists. Unflatten the message and call the settings parser.
46 	if (fSettings.Unflatten(&file) != B_OK)
47 		return false;
48 
49 	PRINT_OBJECT(fSettings);
50 
51 	BRect rect;
52 	if (fSettings.FindRect("windowframe", &rect) == B_OK)
53 		fWindowFrame = rect;
54 	int32 value;
55 	if (fSettings.FindInt32("windowtab", &value) == B_OK)
56 		fWindowTab = value;
57 	if (fSettings.FindInt32("timeflags", &value) == B_OK)
58 		fTimeFlags = value;
59 
60 	if (fSettings.FindInt32("timefade", &value) == B_OK)
61 		fBlankTime = value * 1000000LL;
62 	if (fSettings.FindInt32("timestandby", &value) == B_OK)
63 		fStandByTime = value * 1000000LL;
64 	if (fSettings.FindInt32("timesuspend", &value) == B_OK)
65 		fSuspendTime = value * 1000000LL;
66 	if (fSettings.FindInt32("timeoff", &value) == B_OK)
67 		fOffTime = value * 1000000LL;
68 
69 	if (fSettings.FindInt32("cornernow", &value) == B_OK)
70 		fBlankCorner = (screen_corner)value;
71 	if (fSettings.FindInt32("cornernever", &value) == B_OK)
72 		fNeverBlankCorner = (screen_corner)value;
73 
74 	bool lockEnabled;
75 	if (fSettings.FindBool("lockenable", &lockEnabled) == B_OK)
76 		fLockEnabled = lockEnabled;
77 	if (fSettings.FindInt32("lockdelay", &value) == B_OK)
78 		fPasswordTime = value * 1000000LL;
79 	const char* string;
80 	if (fSettings.FindString("lockpassword", &string) == B_OK)
81 		fPassword = string;
82 	if (fSettings.FindString("lockmethod", &string) == B_OK)
83 		fLockMethod = string;
84 
85 	if (fSettings.FindString("modulename", &string) == B_OK)
86 		fModuleName = string;
87 
88 	if (IsNetworkPassword()) {
89 		FILE *networkFile = NULL;
90 		char buffer[512], *start;
91 		// TODO: this only works under R5 net_server!
92 		// This ugly piece opens the networking file and reads the password, if it exists.
93 		if ((networkFile = fopen(fNetworkPath.Path(),"r")))
94 		while (fgets(buffer, 512, networkFile) != NULL) {
95 			if ((start = strstr(buffer,"PASSWORD = ")) != NULL) {
96 				char password[14];
97 				strncpy(password, start+11,strlen(start+11)-1);
98 				password[strlen(start+11)-1] = 0;
99 				fPassword = password;
100 				break;
101 			}
102 		}
103 	}
104 
105 	return true;
106 }
107 
108 
109 void
110 ScreenSaverSettings::Defaults()
111 {
112 	fWindowFrame = BRect(96.5, 77.0, 542.5, 402);
113 	fWindowTab = 0;
114 	fTimeFlags = SAVER_DISABLED;
115 	fBlankTime = 120*1000000LL;
116 	fStandByTime = 120*1000000LL;
117 	fSuspendTime = 120*1000000LL;
118 	fOffTime = 120*1000000LL;
119 	fBlankCorner = NO_CORNER;
120 	fNeverBlankCorner = NO_CORNER;
121 	fLockEnabled = false;
122 	fPasswordTime = 120*1000000LL;
123 	fPassword = "";
124 	fLockMethod = "custom";
125 	fModuleName = "";
126 }
127 
128 
129 BMessage &
130 ScreenSaverSettings::Message()
131 {
132 	// We can't just empty the message, because the module states are stored
133 	// in this message as well.
134 
135 	if (fSettings.ReplaceRect("windowframe", fWindowFrame) != B_OK)
136 		fSettings.AddRect("windowframe", fWindowFrame);
137 	if (fSettings.ReplaceInt32("windowtab", fWindowTab) != B_OK)
138 		fSettings.AddInt32("windowtab", fWindowTab);
139 	if (fSettings.ReplaceInt32("timeflags", fTimeFlags) != B_OK)
140 		fSettings.AddInt32("timeflags", fTimeFlags);
141 	if (fSettings.ReplaceInt32("timefade", fBlankTime / 1000000LL) != B_OK)
142 		fSettings.AddInt32("timefade", fBlankTime / 1000000LL);
143 	if (fSettings.ReplaceInt32("timestandby", fStandByTime / 1000000LL) != B_OK)
144 		fSettings.AddInt32("timestandby", fStandByTime / 1000000LL);
145 	if (fSettings.ReplaceInt32("timesuspend", fSuspendTime / 1000000LL) != B_OK)
146 		fSettings.AddInt32("timesuspend", fSuspendTime / 1000000LL);
147 	if (fSettings.ReplaceInt32("timeoff", fOffTime / 1000000LL) != B_OK)
148 		fSettings.AddInt32("timeoff", fOffTime / 1000000LL);
149 	if (fSettings.ReplaceInt32("cornernow", fBlankCorner) != B_OK)
150 		fSettings.AddInt32("cornernow", fBlankCorner);
151 	if (fSettings.ReplaceInt32("cornernever", fNeverBlankCorner) != B_OK)
152 		fSettings.AddInt32("cornernever", fNeverBlankCorner);
153 	if (fSettings.ReplaceBool("lockenable", fLockEnabled) != B_OK)
154 		fSettings.AddBool("lockenable", fLockEnabled);
155 	if (fSettings.ReplaceInt32("lockdelay", fPasswordTime / 1000000LL) != B_OK)
156 		fSettings.AddInt32("lockdelay", fPasswordTime / 1000000LL);
157 	if (fSettings.ReplaceString("lockmethod", fLockMethod) != B_OK)
158 		fSettings.AddString("lockmethod", fLockMethod);
159 
160 	const char* password = IsNetworkPassword() ? "" : fPassword.String();
161 	if (fSettings.ReplaceString("lockpassword", password) != B_OK)
162 		fSettings.AddString("lockpassword", password);
163 
164 	if (fSettings.ReplaceString("modulename", fModuleName) != B_OK)
165 		fSettings.AddString("modulename", fModuleName);
166 
167 	return fSettings;
168 }
169 
170 
171 status_t
172 ScreenSaverSettings::GetModuleState(const char *name, BMessage *stateMsg)
173 {
174 	BString stateName("modulesettings_");
175 	stateName += name;
176 	return fSettings.FindMessage(stateName.String(), stateMsg);
177 }
178 
179 
180 void
181 ScreenSaverSettings::SetModuleState(const char *name, BMessage *stateMsg)
182 {
183 	BString stateName("modulesettings_");
184 	stateName += name;
185 	fSettings.RemoveName(stateName.String());
186 	fSettings.AddMessage(stateName.String(), stateMsg);
187 }
188 
189 
190 void
191 ScreenSaverSettings::Save()
192 {
193   	BMessage &settings = Message();
194 	PRINT_OBJECT(settings);
195 	BFile file(fSettingsPath.Path(), B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE);
196 	if (file.InitCheck() != B_OK || settings.Flatten(&file) != B_OK)
197 		fprintf(stderr, "Problem while saving screensaver preferences file!\n");
198 }
199 
200