2 * Copyright 2003-2009, Haiku.
3 * Distributed under the terms of the MIT License.
7 * Jérôme Duval, jerome.duval@free.fr
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"
21 #include <FindDirectory.h>
23 #include <StorageDefs.h>
27 ScreenSaverSettings::ScreenSaverSettings()
30 if (find_directory(B_USER_SETTINGS_DIRECTORY
, &path
) == B_OK
) {
32 fSettingsPath
.Append("ScreenSaver_settings", true);
39 //! Load the flattened settings BMessage from disk and parse it.
41 ScreenSaverSettings::Load()
43 BFile
file(fSettingsPath
.Path(), B_READ_ONLY
);
44 if (file
.InitCheck() != B_OK
)
47 // File exists. Unflatten the message and call the settings parser.
48 if (fSettings
.Unflatten(&file
) != B_OK
)
51 PRINT_OBJECT(fSettings
);
54 if (fSettings
.FindRect("windowframe", &rect
) == B_OK
)
57 if (fSettings
.FindInt32("windowtab", &value
) == B_OK
)
59 if (fSettings
.FindInt32("timeflags", &value
) == B_OK
)
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
;
77 if (fSettings
.FindBool("lockenable", &lockEnabled
) == B_OK
)
78 fLockEnabled
= lockEnabled
;
79 if (fSettings
.FindInt32("lockdelay", &value
) == B_OK
)
80 fPasswordTime
= value
* 1000000LL;
82 if (fSettings
.FindString("lockpassword", &string
) == B_OK
)
84 if (fSettings
.FindString("lockmethod", &string
) == B_OK
)
87 if (fSettings
.FindString("modulename", &string
) == B_OK
)
90 if (IsNetworkPassword()) {
91 // TODO: this does not work yet
99 ScreenSaverSettings::Defaults()
101 fWindowFrame
= BRect(96.5, 77.0, 542.5, 402);
104 // Enable blanker + turning off the screen
105 fTimeFlags
= ENABLE_SAVER
| ENABLE_DPMS_STAND_BY
| ENABLE_DPMS_SUSPEND
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
124 fPasswordTime
= 900 * 1000000LL;
126 fLockMethod
= "custom";
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
);
175 ScreenSaverSettings::GetModuleState(const char* name
, BMessage
* stateMessage
)
177 if (name
== NULL
|| *name
== '\0')
180 BString
stateName("modulesettings_");
182 return fSettings
.FindMessage(stateName
, stateMessage
);
187 ScreenSaverSettings::SetModuleState(const char* name
, BMessage
* stateMessage
)
189 if (name
== NULL
|| *name
== '\0')
192 BString
stateName("modulesettings_");
194 fSettings
.RemoveName(stateName
.String());
195 fSettings
.AddMessage(stateName
.String(), stateMessage
);
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");