1 // 2 // TPreferences 3 // 4 // Class for saving and loading preference information 5 // via BMessages. 6 // 7 // Eric Shepherd 8 // 9 10 /* 11 Copyright 1999, Be Incorporated. All Rights Reserved. 12 This file may be used under the terms of the Be Sample Code License. 13 14 Modified by H. Reh Dec. 2001 15 */ 16 17 #include <Message.h> 18 #include <Messenger.h> 19 #include <File.h> 20 #include <FindDirectory.h> 21 #include <Beep.h> // wieder entfernen 22 23 #include "TPreferences.h" 24 25 26 // 27 // TPreferences::TPreferences 28 // 29 // Open the settings file and read the data in. 30 // 31 TPreferences::TPreferences(char *filename) : BMessage('pref') 32 // TPreferences von BMessage ableiten, 'pref' = Message-Konstante 33 { 34 BFile file; 35 36 status = find_directory(B_COMMON_SETTINGS_DIRECTORY, &path); 37 // B_COMMON_SETTINGS_DIRECTORY = Ordner in dem Settings stehen 38 if (status != B_OK) 39 { 40 return; 41 } 42 43 path.Append(filename); // Filenamen anhängen 44 45 path.GetParent(&parent); // übergeordnetes Verzeichnis suchen 46 create_directory(parent.Path(), 0777); // Verzeichnis anlegen 47 parent.Unset(); // File schließen 48 49 status = file.SetTo(path.Path(), B_READ_ONLY); 50 if (status == B_OK) 51 { 52 status = Unflatten(&file); 53 // da file eine Datei ist, ruft Unflatten sofort Read() auf 54 } 55 } 56 57 58 // 59 // TPreferences::~TPreferences 60 // 61 // Write the preferences to disk. 62 // 63 TPreferences::~TPreferences() 64 { 65 BFile file; 66 67 if (file.SetTo(path.Path(), B_WRITE_ONLY | B_CREATE_FILE) == B_OK) 68 { 69 Flatten(&file); 70 // da file eine Datei ist, ruft Flatten sofort Write() auf 71 } 72 } 73 74 75 status_t TPreferences::SetBool(const char *name, bool b) 76 { 77 if (HasBool(name)) // Testen auf Typ bool 78 { 79 return ReplaceBool(name, 0, b); // Wenn Variable schon existiert -> überschreiben -> Funktion beenden 80 } 81 return AddBool(name, b); // Wenn Variable nicht existiert -> neu anlegen -> Funktion beenden 82 } 83 84 status_t TPreferences::SetInt8(const char *name, int8 i) 85 { 86 if (HasInt8(name)) 87 { 88 return ReplaceInt8(name, 0, i); 89 } 90 return AddInt8(name, i); 91 } 92 93 status_t TPreferences::SetInt16(const char *name, int16 i) 94 { 95 if (HasInt16(name)) 96 { 97 return ReplaceInt16(name, 0, i); 98 } 99 return AddInt16(name, i); 100 } 101 102 status_t TPreferences::SetInt32(const char *name, int32 i) 103 { 104 if (HasInt32(name)) 105 { 106 return ReplaceInt32(name, 0, i); 107 } 108 return AddInt32(name, i); 109 } 110 111 status_t TPreferences::SetInt64(const char *name, int64 i) 112 { 113 if (HasInt64(name)) 114 { 115 return ReplaceInt64(name, 0, i); 116 } 117 return AddInt64(name, i); 118 } 119 120 status_t TPreferences::SetFloat(const char *name, float f) 121 { 122 if (HasFloat(name)) 123 { 124 return ReplaceFloat(name, 0, f); 125 } 126 return AddFloat(name, f); 127 } 128 129 status_t TPreferences::SetDouble(const char *name, double f) 130 { 131 if (HasDouble(name)) 132 { 133 return ReplaceDouble(name, 0, f); 134 } 135 return AddDouble(name, f); 136 } 137 138 status_t TPreferences::SetString(const char *name, const char *s) 139 { 140 if (HasString(name)) 141 { 142 return ReplaceString(name, 0, s); 143 } 144 return AddString(name, s); 145 } 146 147 status_t TPreferences::SetPoint(const char *name, BPoint p) 148 { 149 if (HasPoint(name)) 150 { 151 return ReplacePoint(name, 0, p); 152 } 153 return AddPoint(name, p); 154 } 155 156 status_t TPreferences::SetRect(const char *name, BRect r) 157 { 158 if (HasRect(name)) 159 { 160 return ReplaceRect(name, 0, r); 161 } 162 return AddRect(name, r); 163 } 164 165 status_t TPreferences::SetMessage(const char *name, const BMessage *message) 166 { 167 if (HasMessage(name)) 168 { 169 return ReplaceMessage(name, 0, message); 170 } 171 return AddMessage(name, message); 172 } 173 174 status_t TPreferences::SetFlat(const char *name, const BFlattenable *obj) 175 { 176 if (HasFlat(name, obj)) 177 { 178 return ReplaceFlat(name, 0, (BFlattenable *) obj); 179 } 180 return AddFlat(name, (BFlattenable *) obj); 181 } 182 183 status_t TPreferences::SetData(const char *name, type_code type,const void *data, ssize_t numBytes) 184 { 185 if (HasData(name, type)) 186 { 187 return ReplaceData(name, type, 0, data, numBytes); 188 } 189 return AddData(name, type, data, numBytes); 190 } 191 192 status_t TPreferences::SetRef(const char *name, entry_ref *ref) 193 { 194 if (HasRef(name)) 195 { 196 return ReplaceRef(name, 0, ref); 197 } 198 return AddRef(name, ref); 199 } 200 201