2005-07-29 11:41:58 +04:00
|
|
|
/*
|
2006-06-06 23:47:23 +04:00
|
|
|
* Copyright 2003-2006, Haiku.
|
2005-07-29 11:41:58 +04:00
|
|
|
* Distributed under the terms of the MIT License.
|
2005-09-05 16:54:15 +04:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Michael Phipps
|
2006-06-06 23:47:23 +04:00
|
|
|
* Jérôme Duval, jerome.duval@free.fr
|
2005-07-29 11:41:58 +04:00
|
|
|
*/
|
2006-06-06 23:47:23 +04:00
|
|
|
|
|
|
|
|
2006-06-07 16:43:31 +04:00
|
|
|
#include "ScreenSaverSettings.h"
|
2006-06-06 23:47:23 +04:00
|
|
|
|
2005-08-18 19:30:25 +04:00
|
|
|
#include <Debug.h>
|
2005-07-26 20:12:05 +04:00
|
|
|
#include <File.h>
|
2005-07-29 16:05:22 +04:00
|
|
|
#include <FindDirectory.h>
|
2005-07-26 20:12:05 +04:00
|
|
|
#include <Path.h>
|
2005-07-29 16:05:22 +04:00
|
|
|
#include <StorageDefs.h>
|
|
|
|
#include <String.h>
|
2006-06-06 23:47:23 +04:00
|
|
|
|
2005-07-29 16:05:22 +04:00
|
|
|
#include <string.h>
|
2004-03-20 21:53:33 +03:00
|
|
|
|
2005-07-26 20:12:05 +04:00
|
|
|
|
2006-06-07 16:43:31 +04:00
|
|
|
ScreenSaverSettings::ScreenSaverSettings()
|
2004-05-04 02:41:30 +04:00
|
|
|
{
|
2006-06-06 23:47:23 +04:00
|
|
|
BPath path;
|
|
|
|
find_directory(B_USER_SETTINGS_DIRECTORY, &path);
|
|
|
|
|
|
|
|
fSettingsPath = path;
|
|
|
|
fSettingsPath.Append("ScreenSaver_settings", true);
|
|
|
|
fNetworkPath = path;
|
|
|
|
fNetworkPath.Append("network", true);
|
2005-07-29 11:41:58 +04:00
|
|
|
|
|
|
|
Defaults();
|
2004-03-20 21:53:33 +03:00
|
|
|
}
|
|
|
|
|
2005-07-26 20:12:05 +04:00
|
|
|
|
2006-06-06 23:47:23 +04:00
|
|
|
//! Load the flattened settings BMessage from disk and parse it.
|
|
|
|
bool
|
2006-06-09 20:03:16 +04:00
|
|
|
ScreenSaverSettings::Load()
|
2004-05-04 02:41:30 +04:00
|
|
|
{
|
2006-06-06 23:47:23 +04:00
|
|
|
BFile file(fSettingsPath.Path(), B_READ_ONLY);
|
|
|
|
if (file.InitCheck() != B_OK)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// File exists. Unflatten the message and call the settings parser.
|
|
|
|
if (fSettings.Unflatten(&file) != B_OK)
|
2005-08-02 13:57:45 +04:00
|
|
|
return false;
|
2006-06-06 23:47:23 +04:00
|
|
|
|
|
|
|
PRINT_OBJECT(fSettings);
|
|
|
|
|
2006-06-09 20:03:16 +04:00
|
|
|
BRect rect;
|
|
|
|
if (fSettings.FindRect("windowframe", &rect) == B_OK)
|
|
|
|
fWindowFrame = rect;
|
|
|
|
int32 value;
|
|
|
|
if (fSettings.FindInt32("windowtab", &value) == B_OK)
|
|
|
|
fWindowTab = value;
|
|
|
|
if (fSettings.FindInt32("timeflags", &value) == B_OK)
|
|
|
|
fTimeFlags = value;
|
|
|
|
|
|
|
|
if (fSettings.FindInt32("timefade", &value) == B_OK)
|
|
|
|
fBlankTime = value * 1000000LL;
|
|
|
|
if (fSettings.FindInt32("timestandby", &value) == B_OK)
|
|
|
|
fStandByTime = value * 1000000LL;
|
|
|
|
if (fSettings.FindInt32("timesuspend", &value) == B_OK)
|
|
|
|
fSuspendTime = value * 1000000LL;
|
|
|
|
if (fSettings.FindInt32("timeoff", &value) == B_OK)
|
|
|
|
fOffTime = value * 1000000LL;
|
|
|
|
|
|
|
|
if (fSettings.FindInt32("cornernow", &value) == B_OK)
|
|
|
|
fBlankCorner = (screen_corner)value;
|
|
|
|
if (fSettings.FindInt32("cornernever", &value) == B_OK)
|
|
|
|
fNeverBlankCorner = (screen_corner)value;
|
|
|
|
|
|
|
|
bool lockEnabled;
|
|
|
|
if (fSettings.FindBool("lockenable", &lockEnabled) == B_OK)
|
|
|
|
fLockEnabled = lockEnabled;
|
|
|
|
if (fSettings.FindInt32("lockdelay", &value) == B_OK)
|
|
|
|
fPasswordTime = value * 1000000LL;
|
|
|
|
const char* string;
|
|
|
|
if (fSettings.FindString("lockpassword", &string) == B_OK)
|
|
|
|
fPassword = string;
|
|
|
|
if (fSettings.FindString("lockmethod", &string) == B_OK)
|
|
|
|
fLockMethod = string;
|
|
|
|
|
|
|
|
if (fSettings.FindString("modulename", &string) == B_OK)
|
|
|
|
fModuleName = string;
|
2006-06-06 23:47:23 +04:00
|
|
|
|
|
|
|
if (IsNetworkPassword()) {
|
|
|
|
FILE *networkFile = NULL;
|
|
|
|
char buffer[512], *start;
|
2006-06-09 20:03:16 +04:00
|
|
|
// TODO: this only works under R5 net_server!
|
2006-06-06 23:47:23 +04:00
|
|
|
// This ugly piece opens the networking file and reads the password, if it exists.
|
|
|
|
if ((networkFile = fopen(fNetworkPath.Path(),"r")))
|
|
|
|
while (fgets(buffer, 512, networkFile) != NULL) {
|
|
|
|
if ((start = strstr(buffer,"PASSWORD = ")) != NULL) {
|
|
|
|
char password[14];
|
|
|
|
strncpy(password, start+11,strlen(start+11)-1);
|
|
|
|
password[strlen(start+11)-1] = 0;
|
|
|
|
fPassword = password;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2005-07-29 11:41:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2006-06-07 16:43:31 +04:00
|
|
|
ScreenSaverSettings::Defaults()
|
2005-07-29 11:41:58 +04:00
|
|
|
{
|
|
|
|
fWindowFrame = BRect(96.5, 77.0, 542.5, 402);
|
|
|
|
fWindowTab = 0;
|
2006-06-06 23:47:23 +04:00
|
|
|
fTimeFlags = SAVER_DISABLED;
|
2005-08-18 19:30:25 +04:00
|
|
|
fBlankTime = 120*1000000LL;
|
|
|
|
fStandByTime = 120*1000000LL;
|
|
|
|
fSuspendTime = 120*1000000LL;
|
|
|
|
fOffTime = 120*1000000LL;
|
2006-06-09 20:03:16 +04:00
|
|
|
fBlankCorner = NO_CORNER;
|
|
|
|
fNeverBlankCorner = NO_CORNER;
|
2005-07-29 11:41:58 +04:00
|
|
|
fLockEnabled = false;
|
2005-08-18 19:30:25 +04:00
|
|
|
fPasswordTime = 120*1000000LL;
|
2005-07-29 11:41:58 +04:00
|
|
|
fPassword = "";
|
|
|
|
fLockMethod = "custom";
|
|
|
|
fModuleName = "";
|
2004-03-20 21:53:33 +03:00
|
|
|
}
|
|
|
|
|
2005-07-29 11:41:58 +04:00
|
|
|
|
2005-07-29 16:05:22 +04:00
|
|
|
BMessage &
|
2006-06-09 20:03:16 +04:00
|
|
|
ScreenSaverSettings::Message()
|
2005-07-29 11:41:58 +04:00
|
|
|
{
|
2006-06-06 23:47:23 +04:00
|
|
|
// We can't just empty the message, because the module states are stored
|
|
|
|
// in this message as well.
|
|
|
|
|
|
|
|
if (fSettings.ReplaceRect("windowframe", fWindowFrame) != B_OK)
|
|
|
|
fSettings.AddRect("windowframe", fWindowFrame);
|
|
|
|
if (fSettings.ReplaceInt32("windowtab", fWindowTab) != B_OK)
|
|
|
|
fSettings.AddInt32("windowtab", fWindowTab);
|
|
|
|
if (fSettings.ReplaceInt32("timeflags", fTimeFlags) != B_OK)
|
|
|
|
fSettings.AddInt32("timeflags", fTimeFlags);
|
|
|
|
if (fSettings.ReplaceInt32("timefade", fBlankTime / 1000000LL) != B_OK)
|
|
|
|
fSettings.AddInt32("timefade", fBlankTime / 1000000LL);
|
|
|
|
if (fSettings.ReplaceInt32("timestandby", fStandByTime / 1000000LL) != B_OK)
|
|
|
|
fSettings.AddInt32("timestandby", fStandByTime / 1000000LL);
|
|
|
|
if (fSettings.ReplaceInt32("timesuspend", fSuspendTime / 1000000LL) != B_OK)
|
|
|
|
fSettings.AddInt32("timesuspend", fSuspendTime / 1000000LL);
|
|
|
|
if (fSettings.ReplaceInt32("timeoff", fOffTime / 1000000LL) != B_OK)
|
|
|
|
fSettings.AddInt32("timeoff", fOffTime / 1000000LL);
|
|
|
|
if (fSettings.ReplaceInt32("cornernow", fBlankCorner) != B_OK)
|
|
|
|
fSettings.AddInt32("cornernow", fBlankCorner);
|
|
|
|
if (fSettings.ReplaceInt32("cornernever", fNeverBlankCorner) != B_OK)
|
|
|
|
fSettings.AddInt32("cornernever", fNeverBlankCorner);
|
|
|
|
if (fSettings.ReplaceBool("lockenable", fLockEnabled) != B_OK)
|
|
|
|
fSettings.AddBool("lockenable", fLockEnabled);
|
|
|
|
if (fSettings.ReplaceInt32("lockdelay", fPasswordTime / 1000000LL) != B_OK)
|
|
|
|
fSettings.AddInt32("lockdelay", fPasswordTime / 1000000LL);
|
|
|
|
if (fSettings.ReplaceString("lockmethod", fLockMethod) != B_OK)
|
|
|
|
fSettings.AddString("lockmethod", fLockMethod);
|
|
|
|
|
|
|
|
const char* password = IsNetworkPassword() ? "" : fPassword.String();
|
|
|
|
if (fSettings.ReplaceString("lockpassword", password) != B_OK)
|
|
|
|
fSettings.AddString("lockpassword", password);
|
|
|
|
|
|
|
|
if (fSettings.ReplaceString("modulename", fModuleName) != B_OK)
|
|
|
|
fSettings.AddString("modulename", fModuleName);
|
|
|
|
|
2005-07-29 16:05:22 +04:00
|
|
|
return fSettings;
|
2005-07-29 11:41:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-29 16:05:22 +04:00
|
|
|
status_t
|
2006-06-09 20:03:16 +04:00
|
|
|
ScreenSaverSettings::GetModuleState(const char *name, BMessage *stateMsg)
|
2005-07-29 11:41:58 +04:00
|
|
|
{
|
2005-07-29 16:05:22 +04:00
|
|
|
BString stateName("modulesettings_");
|
|
|
|
stateName += name;
|
|
|
|
return fSettings.FindMessage(stateName.String(), stateMsg);
|
2004-03-20 21:53:33 +03:00
|
|
|
}
|
|
|
|
|
2005-07-29 11:41:58 +04:00
|
|
|
|
2004-10-10 03:17:47 +04:00
|
|
|
void
|
2006-06-09 20:03:16 +04:00
|
|
|
ScreenSaverSettings::SetModuleState(const char *name, BMessage *stateMsg)
|
2005-07-29 11:41:58 +04:00
|
|
|
{
|
2005-07-29 16:05:22 +04:00
|
|
|
BString stateName("modulesettings_");
|
|
|
|
stateName += name;
|
|
|
|
fSettings.RemoveName(stateName.String());
|
|
|
|
fSettings.AddMessage(stateName.String(), stateMsg);
|
2004-04-20 06:06:39 +04:00
|
|
|
}
|
|
|
|
|
2005-07-29 11:41:58 +04:00
|
|
|
|
2004-05-04 02:41:30 +04:00
|
|
|
void
|
2006-06-09 20:03:16 +04:00
|
|
|
ScreenSaverSettings::Save()
|
2005-07-29 11:41:58 +04:00
|
|
|
{
|
2006-06-09 20:03:16 +04:00
|
|
|
BMessage &settings = Message();
|
2005-08-19 23:33:49 +04:00
|
|
|
PRINT_OBJECT(settings);
|
2006-06-06 23:47:23 +04:00
|
|
|
BFile file(fSettingsPath.Path(), B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE);
|
|
|
|
if (file.InitCheck() != B_OK || settings.Flatten(&file) != B_OK)
|
2005-07-29 11:41:58 +04:00
|
|
|
fprintf(stderr, "Problem while saving screensaver preferences file!\n");
|
2004-03-20 21:53:33 +03:00
|
|
|
}
|
2005-07-29 16:05:22 +04:00
|
|
|
|