1 /* 2 * Copyright 2003-2009 Haiku Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Michael Pfeiffer, laplace@haiku-os.org 7 */ 8 9 #include "ShowImageSettings.h" 10 11 #include <File.h> 12 #include <FindDirectory.h> 13 #include <Path.h> 14 15 16 ShowImageSettings::ShowImageSettings() 17 { 18 Load(); 19 } 20 21 22 ShowImageSettings::~ShowImageSettings() 23 { 24 if (Lock()) { 25 Save(); 26 Unlock(); 27 } 28 } 29 30 31 bool 32 ShowImageSettings::Lock() 33 { 34 return fLock.Lock(); 35 } 36 37 38 void 39 ShowImageSettings::Unlock() 40 { 41 fLock.Unlock(); 42 } 43 44 45 bool 46 ShowImageSettings::GetBool(const char* name, bool defaultValue) 47 { 48 bool value; 49 if (fSettings.FindBool(name, &value) == B_OK) { 50 return value; 51 } else { 52 return defaultValue; 53 } 54 } 55 56 57 int32 58 ShowImageSettings::GetInt32(const char* name, int32 defaultValue) 59 { 60 int32 value; 61 if (fSettings.FindInt32(name, &value) == B_OK) { 62 return value; 63 } else { 64 return defaultValue; 65 } 66 } 67 68 69 float 70 ShowImageSettings::GetFloat(const char* name, float defaultValue) 71 { 72 float value; 73 if (fSettings.FindFloat(name, &value) == B_OK) { 74 return value; 75 } else { 76 return defaultValue; 77 } 78 } 79 80 81 BRect 82 ShowImageSettings::GetRect(const char* name, BRect defaultValue) 83 { 84 BRect value; 85 if (fSettings.FindRect(name, &value) == B_OK) { 86 return value; 87 } else { 88 return defaultValue; 89 } 90 } 91 92 93 const char* 94 ShowImageSettings::GetString(const char* name, const char* defaultValue) 95 { 96 const char* value; 97 if (fSettings.FindString(name, &value) == B_OK) { 98 return value; 99 } else { 100 return defaultValue; 101 } 102 } 103 104 105 void 106 ShowImageSettings::SetBool(const char* name, bool value) 107 { 108 if (fSettings.HasBool(name)) { 109 fSettings.ReplaceBool(name, value); 110 } else { 111 fSettings.AddBool(name, value); 112 } 113 } 114 115 116 void 117 ShowImageSettings::SetInt32(const char* name, int32 value) 118 { 119 if (fSettings.HasInt32(name)) { 120 fSettings.ReplaceInt32(name, value); 121 } else { 122 fSettings.AddInt32(name, value); 123 } 124 } 125 126 127 void 128 ShowImageSettings::SetFloat(const char* name, float value) 129 { 130 if (fSettings.HasFloat(name)) { 131 fSettings.ReplaceFloat(name, value); 132 } else { 133 fSettings.AddFloat(name, value); 134 } 135 } 136 137 138 void 139 ShowImageSettings::SetRect(const char* name, BRect value) 140 { 141 if (fSettings.HasRect(name)) { 142 fSettings.ReplaceRect(name, value); 143 } else { 144 fSettings.AddRect(name, value); 145 } 146 } 147 148 149 void 150 ShowImageSettings::SetString(const char* name, const char* value) 151 { 152 if (fSettings.HasString(name)) { 153 fSettings.ReplaceString(name, value); 154 } else { 155 fSettings.AddString(name, value); 156 } 157 } 158 159 160 bool 161 ShowImageSettings::OpenSettingsFile(BFile* file, bool forReading) 162 { 163 status_t st; 164 BPath path; 165 uint32 openMode; 166 167 st = find_directory(B_USER_SETTINGS_DIRECTORY, &path); 168 if (st != B_OK) return false; 169 170 path.Append("ShowImage_settings"); 171 if (forReading) { 172 openMode = B_READ_ONLY; 173 } else { 174 openMode = B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE; 175 } 176 st = file->SetTo(path.Path(), openMode); 177 return st == B_OK; 178 } 179 180 181 void 182 ShowImageSettings::Load() 183 { 184 BFile file; 185 if (OpenSettingsFile(&file, true)) { 186 fSettings.Unflatten(&file); 187 } 188 } 189 190 191 void 192 ShowImageSettings::Save() 193 { 194 BFile file; 195 if (OpenSettingsFile(&file, false)) { 196 fSettings.Flatten(&file); 197 } 198 } 199 200