headers/bsd: Add sys/queue.h.
[haiku.git] / src / kits / screensaver / ScreenSaverSettings.cpp
blob73175cd6e9d9b605d5f47996f87ec5d176d7b3aa
1 /*
2 * Copyright 2003-2009, Haiku.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Michael Phipps
7 * Jérôme Duval, jerome.duval@free.fr
8 */
10 /*! This is the class that wraps the screensaver settings, as well as the
11 settings of the screensaver preference application.
15 #include "ScreenSaverSettings.h"
17 #include <string.h>
19 #include <Debug.h>
20 #include <File.h>
21 #include <FindDirectory.h>
22 #include <Path.h>
23 #include <StorageDefs.h>
24 #include <String.h>
27 ScreenSaverSettings::ScreenSaverSettings()
29 BPath path;
30 if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK) {
31 fSettingsPath = path;
32 fSettingsPath.Append("ScreenSaver_settings", true);
35 Defaults();
39 //! Load the flattened settings BMessage from disk and parse it.
40 bool
41 ScreenSaverSettings::Load()
43 BFile file(fSettingsPath.Path(), B_READ_ONLY);
44 if (file.InitCheck() != B_OK)
45 return false;
47 // File exists. Unflatten the message and call the settings parser.
48 if (fSettings.Unflatten(&file) != B_OK)
49 return false;
51 PRINT_OBJECT(fSettings);
53 BRect rect;
54 if (fSettings.FindRect("windowframe", &rect) == B_OK)
55 fWindowFrame = rect;
56 int32 value;
57 if (fSettings.FindInt32("windowtab", &value) == B_OK)
58 fWindowTab = value;
59 if (fSettings.FindInt32("timeflags", &value) == B_OK)
60 fTimeFlags = value;
62 if (fSettings.FindInt32("timefade", &value) == B_OK)
63 fBlankTime = value * 1000000LL;
64 if (fSettings.FindInt32("timestandby", &value) == B_OK)
65 fStandByTime = value * 1000000LL;
66 if (fSettings.FindInt32("timesuspend", &value) == B_OK)
67 fSuspendTime = value * 1000000LL;
68 if (fSettings.FindInt32("timeoff", &value) == B_OK)
69 fOffTime = value * 1000000LL;
71 if (fSettings.FindInt32("cornernow", &value) == B_OK)
72 fBlankCorner = (screen_corner)value;
73 if (fSettings.FindInt32("cornernever", &value) == B_OK)
74 fNeverBlankCorner = (screen_corner)value;
76 bool lockEnabled;
77 if (fSettings.FindBool("lockenable", &lockEnabled) == B_OK)
78 fLockEnabled = lockEnabled;
79 if (fSettings.FindInt32("lockdelay", &value) == B_OK)
80 fPasswordTime = value * 1000000LL;
81 const char* string;
82 if (fSettings.FindString("lockpassword", &string) == B_OK)
83 fPassword = string;
84 if (fSettings.FindString("lockmethod", &string) == B_OK)
85 fLockMethod = string;
87 if (fSettings.FindString("modulename", &string) == B_OK)
88 fModuleName = string;
90 if (IsNetworkPassword()) {
91 // TODO: this does not work yet
94 return true;
98 void
99 ScreenSaverSettings::Defaults()
101 fWindowFrame = BRect(96.5, 77.0, 542.5, 402);
102 fWindowTab = 0;
104 // Enable blanker + turning off the screen
105 fTimeFlags = ENABLE_SAVER | ENABLE_DPMS_STAND_BY | ENABLE_DPMS_SUSPEND
106 | ENABLE_DPMS_OFF;
108 // Times are in microseconds
109 fBlankTime = 900 * 1000000LL; // 15 minutes
111 // All these times are relative to fBlankTime; standby will start 5 minutes
112 // after the screen saver.
113 fStandByTime = 300 * 1000000LL; // 5 minutes
114 fSuspendTime = 300 * 1000000LL;
115 fOffTime = 300 * 1000000LL;
117 fBlankCorner = NO_CORNER;
118 fNeverBlankCorner = NO_CORNER;
120 fLockEnabled = false;
121 // This time is NOT referenced to when the screen saver starts, but to when
122 // idle time starts, like fBlankTime. By default it is the same as
123 // fBlankTime.
124 fPasswordTime = 900 * 1000000LL;
125 fPassword = "";
126 fLockMethod = "custom";
128 fModuleName = "";
132 BMessage&
133 ScreenSaverSettings::Message()
135 // We can't just empty the message, because the module states are stored
136 // in this message as well.
138 if (fSettings.ReplaceRect("windowframe", fWindowFrame) != B_OK)
139 fSettings.AddRect("windowframe", fWindowFrame);
140 if (fSettings.ReplaceInt32("windowtab", fWindowTab) != B_OK)
141 fSettings.AddInt32("windowtab", fWindowTab);
142 if (fSettings.ReplaceInt32("timeflags", fTimeFlags) != B_OK)
143 fSettings.AddInt32("timeflags", fTimeFlags);
144 if (fSettings.ReplaceInt32("timefade", fBlankTime / 1000000LL) != B_OK)
145 fSettings.AddInt32("timefade", fBlankTime / 1000000LL);
146 if (fSettings.ReplaceInt32("timestandby", fStandByTime / 1000000LL) != B_OK)
147 fSettings.AddInt32("timestandby", fStandByTime / 1000000LL);
148 if (fSettings.ReplaceInt32("timesuspend", fSuspendTime / 1000000LL) != B_OK)
149 fSettings.AddInt32("timesuspend", fSuspendTime / 1000000LL);
150 if (fSettings.ReplaceInt32("timeoff", fOffTime / 1000000LL) != B_OK)
151 fSettings.AddInt32("timeoff", fOffTime / 1000000LL);
152 if (fSettings.ReplaceInt32("cornernow", fBlankCorner) != B_OK)
153 fSettings.AddInt32("cornernow", fBlankCorner);
154 if (fSettings.ReplaceInt32("cornernever", fNeverBlankCorner) != B_OK)
155 fSettings.AddInt32("cornernever", fNeverBlankCorner);
156 if (fSettings.ReplaceBool("lockenable", fLockEnabled) != B_OK)
157 fSettings.AddBool("lockenable", fLockEnabled);
158 if (fSettings.ReplaceInt32("lockdelay", fPasswordTime / 1000000LL) != B_OK)
159 fSettings.AddInt32("lockdelay", fPasswordTime / 1000000LL);
160 if (fSettings.ReplaceString("lockmethod", fLockMethod) != B_OK)
161 fSettings.AddString("lockmethod", fLockMethod);
163 const char* password = IsNetworkPassword() ? "" : fPassword.String();
164 if (fSettings.ReplaceString("lockpassword", password) != B_OK)
165 fSettings.AddString("lockpassword", password);
167 if (fSettings.ReplaceString("modulename", fModuleName) != B_OK)
168 fSettings.AddString("modulename", fModuleName);
170 return fSettings;
174 status_t
175 ScreenSaverSettings::GetModuleState(const char* name, BMessage* stateMessage)
177 if (name == NULL || *name == '\0')
178 return B_BAD_VALUE;
180 BString stateName("modulesettings_");
181 stateName << name;
182 return fSettings.FindMessage(stateName, stateMessage);
186 void
187 ScreenSaverSettings::SetModuleState(const char* name, BMessage* stateMessage)
189 if (name == NULL || *name == '\0')
190 return;
192 BString stateName("modulesettings_");
193 stateName << name;
194 fSettings.RemoveName(stateName.String());
195 fSettings.AddMessage(stateName.String(), stateMessage);
199 void
200 ScreenSaverSettings::Save()
202 BMessage &settings = Message();
203 PRINT_OBJECT(settings);
204 BFile file(fSettingsPath.Path(),
205 B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE);
206 if (file.InitCheck() != B_OK || settings.Flatten(&file) != B_OK)
207 fprintf(stderr, "Problem while saving screensaver preferences file!\n");