1 /* 2 * Copyright 2003-2009, 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 /*! This is the class that wraps the screensaver settings, as well as the 11 settings of the screensaver preference application. 12 */ 13 14 15 #include "ScreenSaverSettings.h" 16 17 #include <string.h> 18 19 #include <Debug.h> 20 #include <File.h> 21 #include <FindDirectory.h> 22 #include <Path.h> 23 #include <StorageDefs.h> 24 #include <String.h> 25 26 27 ScreenSaverSettings::ScreenSaverSettings() 28 { 29 BPath path; 30 if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK) { 31 fSettingsPath = path; 32 fSettingsPath.Append("ScreenSaver_settings", true); 33 } 34 35 Defaults(); 36 } 37 38 39 //! Load the flattened settings BMessage from disk and parse it. 40 bool 41 ScreenSaverSettings::Load() 42 { 43 BFile file(fSettingsPath.Path(), B_READ_ONLY); 44 if (file.InitCheck() != B_OK) 45 return false; 46 47 // File exists. Unflatten the message and call the settings parser. 48 if (fSettings.Unflatten(&file) != B_OK) 49 return false; 50 51 PRINT_OBJECT(fSettings); 52 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; 61 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; 70 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; 75 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; 86 87 if (fSettings.FindString("modulename", &string) == B_OK) 88 fModuleName = string; 89 90 if (IsNetworkPassword()) { 91 // TODO: this does not work yet 92 } 93 94 return true; 95 } 96 97 98 void 99 ScreenSaverSettings::Defaults() 100 { 101 fWindowFrame = BRect(96.5, 77.0, 542.5, 402); 102 fWindowTab = 0; 103 104 // Enable blanker + turning off the screen 105 fTimeFlags = ENABLE_SAVER | ENABLE_DPMS_STAND_BY | ENABLE_DPMS_SUSPEND 106 | ENABLE_DPMS_OFF; 107 108 // Times are in microseconds 109 fBlankTime = 900 * 1000000LL; // 15 minutes 110 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; 116 117 fBlankCorner = NO_CORNER; 118 fNeverBlankCorner = NO_CORNER; 119 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"; 127 128 fModuleName = ""; 129 } 130 131 132 BMessage& 133 ScreenSaverSettings::Message() 134 { 135 // We can't just empty the message, because the module states are stored 136 // in this message as well. 137 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); 162 163 const char* password = IsNetworkPassword() ? "" : fPassword.String(); 164 if (fSettings.ReplaceString("lockpassword", password) != B_OK) 165 fSettings.AddString("lockpassword", password); 166 167 if (fSettings.ReplaceString("modulename", fModuleName) != B_OK) 168 fSettings.AddString("modulename", fModuleName); 169 170 return fSettings; 171 } 172 173 174 status_t 175 ScreenSaverSettings::GetModuleState(const char* name, BMessage* stateMessage) 176 { 177 if (name == NULL || *name == '\0') 178 return B_BAD_VALUE; 179 180 BString stateName("modulesettings_"); 181 stateName << name; 182 return fSettings.FindMessage(stateName, stateMessage); 183 } 184 185 186 void 187 ScreenSaverSettings::SetModuleState(const char* name, BMessage* stateMessage) 188 { 189 if (name == NULL || *name == '\0') 190 return; 191 192 BString stateName("modulesettings_"); 193 stateName << name; 194 fSettings.RemoveName(stateName.String()); 195 fSettings.AddMessage(stateName.String(), stateMessage); 196 } 197 198 199 void 200 ScreenSaverSettings::Save() 201 { 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"); 208 } 209 210