1 /*****************************************************************************/ 2 // ShowImageSettings 3 // Written by Michael Pfeiffer 4 // 5 // ShowImageSettings.cpp 6 // 7 // 8 // Copyright (c) 2003 OpenBeOS Project 9 // 10 // Permission is hereby granted, free of charge, to any person obtaining a 11 // copy of this software and associated documentation files (the "Software"), 12 // to deal in the Software without restriction, including without limitation 13 // the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 // and/or sell copies of the Software, and to permit persons to whom the 15 // Software is furnished to do so, subject to the following conditions: 16 // 17 // The above copyright notice and this permission notice shall be included 18 // in all copies or substantial portions of the Software. 19 // 20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 21 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 // DEALINGS IN THE SOFTWARE. 27 /*****************************************************************************/ 28 29 #include <File.h> 30 #include <FindDirectory.h> 31 #include <Path.h> 32 33 #include "ShowImageSettings.h" 34 35 ShowImageSettings::ShowImageSettings() 36 { 37 Load(); 38 } 39 40 ShowImageSettings::~ShowImageSettings() 41 { 42 if (Lock()) { 43 Save(); 44 Unlock(); 45 } 46 } 47 48 bool 49 ShowImageSettings::Lock() 50 { 51 return fLock.Lock(); 52 } 53 54 void 55 ShowImageSettings::Unlock() 56 { 57 fLock.Unlock(); 58 } 59 60 bool 61 ShowImageSettings::GetBool(const char* name, bool defaultValue) 62 { 63 bool value; 64 if (fSettings.FindBool(name, &value) == B_OK) { 65 return value; 66 } else { 67 return defaultValue; 68 } 69 } 70 71 int32 72 ShowImageSettings::GetInt32(const char* name, int32 defaultValue) 73 { 74 int32 value; 75 if (fSettings.FindInt32(name, &value) == B_OK) { 76 return value; 77 } else { 78 return defaultValue; 79 } 80 } 81 82 float 83 ShowImageSettings::GetFloat(const char* name, float defaultValue) 84 { 85 float value; 86 if (fSettings.FindFloat(name, &value) == B_OK) { 87 return value; 88 } else { 89 return defaultValue; 90 } 91 } 92 93 BRect 94 ShowImageSettings::GetRect(const char* name, BRect defaultValue) 95 { 96 BRect value; 97 if (fSettings.FindRect(name, &value) == B_OK) { 98 return value; 99 } else { 100 return defaultValue; 101 } 102 } 103 104 const char* 105 ShowImageSettings::GetString(const char* name, const char* defaultValue) 106 { 107 const char* value; 108 if (fSettings.FindString(name, &value) == B_OK) { 109 return value; 110 } else { 111 return defaultValue; 112 } 113 } 114 115 void 116 ShowImageSettings::SetBool(const char* name, bool value) 117 { 118 if (fSettings.HasBool(name)) { 119 fSettings.ReplaceBool(name, value); 120 } else { 121 fSettings.AddBool(name, value); 122 } 123 } 124 125 void 126 ShowImageSettings::SetInt32(const char* name, int32 value) 127 { 128 if (fSettings.HasInt32(name)) { 129 fSettings.ReplaceInt32(name, value); 130 } else { 131 fSettings.AddInt32(name, value); 132 } 133 } 134 135 void 136 ShowImageSettings::SetFloat(const char* name, float value) 137 { 138 if (fSettings.HasFloat(name)) { 139 fSettings.ReplaceFloat(name, value); 140 } else { 141 fSettings.AddFloat(name, value); 142 } 143 } 144 145 void 146 ShowImageSettings::SetRect(const char* name, BRect value) 147 { 148 if (fSettings.HasRect(name)) { 149 fSettings.ReplaceRect(name, value); 150 } else { 151 fSettings.AddRect(name, value); 152 } 153 } 154 155 void 156 ShowImageSettings::SetString(const char* name, const char* value) 157 { 158 if (fSettings.HasString(name)) { 159 fSettings.ReplaceString(name, value); 160 } else { 161 fSettings.AddString(name, value); 162 } 163 } 164 165 bool 166 ShowImageSettings::OpenSettingsFile(BFile* file, bool forReading) 167 { 168 status_t st; 169 BPath path; 170 uint32 openMode; 171 172 st = find_directory(B_USER_SETTINGS_DIRECTORY, &path); 173 if (st != B_OK) return false; 174 175 path.Append("ShowImage_settings"); 176 if (forReading) { 177 openMode = B_READ_ONLY; 178 } else { 179 openMode = B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE; 180 } 181 st = file->SetTo(path.Path(), openMode); 182 return st == B_OK; 183 } 184 185 void 186 ShowImageSettings::Load() 187 { 188 BFile file; 189 if (OpenSettingsFile(&file, true)) { 190 fSettings.Unflatten(&file); 191 } 192 } 193 194 void 195 ShowImageSettings::Save() 196 { 197 BFile file; 198 if (OpenSettingsFile(&file, false)) { 199 fSettings.Flatten(&file); 200 } 201 } 202