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 22 #include "TPreferences.h" 23 24 25 // 26 // TPreferences::TPreferences 27 // 28 // Open the settings file and read the data in. 29 // 30 TPreferences::TPreferences(char *filename) : BMessage('pref') 31 // TPreferences inherited from BMessage , 'pref' = Message-constant 32 { 33 BFile file; 34 35 status = find_directory(B_COMMON_SETTINGS_DIRECTORY, &path); 36 if (status != B_OK) 37 { 38 return; 39 } 40 41 path.Append(filename); 42 43 path.GetParent(&parent); 44 create_directory(parent.Path(), 0777); 45 parent.Unset(); 46 47 status = file.SetTo(path.Path(), B_READ_ONLY); 48 if (status == B_OK) 49 { 50 status = Unflatten(&file); 51 // Unflatten is calling Read() 52 } 53 } 54 55 56 // 57 // TPreferences::~TPreferences 58 // 59 // Write the preferences to disk. 60 // 61 TPreferences::~TPreferences() 62 { 63 BFile file; 64 65 if (file.SetTo(path.Path(), B_WRITE_ONLY | B_CREATE_FILE) == B_OK) 66 { 67 Flatten(&file); 68 // Flatten is calling Write() 69 } 70 } 71 72 73 status_t TPreferences::SetBool(const char *name, bool b) 74 { 75 if (HasBool(name)) 76 { 77 return ReplaceBool(name, 0, b); 78 } 79 return AddBool(name, b); 80 } 81 82 status_t TPreferences::SetInt8(const char *name, int8 i) 83 { 84 if (HasInt8(name)) 85 { 86 return ReplaceInt8(name, 0, i); 87 } 88 return AddInt8(name, i); 89 } 90 91 status_t TPreferences::SetInt16(const char *name, int16 i) 92 { 93 if (HasInt16(name)) 94 { 95 return ReplaceInt16(name, 0, i); 96 } 97 return AddInt16(name, i); 98 } 99 100 status_t TPreferences::SetInt32(const char *name, int32 i) 101 { 102 if (HasInt32(name)) 103 { 104 return ReplaceInt32(name, 0, i); 105 } 106 return AddInt32(name, i); 107 } 108 109 status_t TPreferences::SetInt64(const char *name, int64 i) 110 { 111 if (HasInt64(name)) 112 { 113 return ReplaceInt64(name, 0, i); 114 } 115 return AddInt64(name, i); 116 } 117 118 status_t TPreferences::SetFloat(const char *name, float f) 119 { 120 if (HasFloat(name)) 121 { 122 return ReplaceFloat(name, 0, f); 123 } 124 return AddFloat(name, f); 125 } 126 127 status_t TPreferences::SetDouble(const char *name, double f) 128 { 129 if (HasDouble(name)) 130 { 131 return ReplaceDouble(name, 0, f); 132 } 133 return AddDouble(name, f); 134 } 135 136 status_t TPreferences::SetString(const char *name, const char *s) 137 { 138 if (HasString(name)) 139 { 140 return ReplaceString(name, 0, s); 141 } 142 return AddString(name, s); 143 } 144 145 status_t TPreferences::SetPoint(const char *name, BPoint p) 146 { 147 if (HasPoint(name)) 148 { 149 return ReplacePoint(name, 0, p); 150 } 151 return AddPoint(name, p); 152 } 153 154 status_t TPreferences::SetRect(const char *name, BRect r) 155 { 156 if (HasRect(name)) 157 { 158 return ReplaceRect(name, 0, r); 159 } 160 return AddRect(name, r); 161 } 162 163 status_t TPreferences::SetMessage(const char *name, const BMessage *message) 164 { 165 if (HasMessage(name)) 166 { 167 return ReplaceMessage(name, 0, message); 168 } 169 return AddMessage(name, message); 170 } 171 172 status_t TPreferences::SetFlat(const char *name, const BFlattenable *obj) 173 { 174 if (HasFlat(name, obj)) 175 { 176 return ReplaceFlat(name, 0, (BFlattenable *) obj); 177 } 178 return AddFlat(name, (BFlattenable *) obj); 179 } 180 181 status_t TPreferences::SetData(const char *name, type_code type,const void *data, ssize_t numBytes) 182 { 183 if (HasData(name, type)) 184 { 185 return ReplaceData(name, type, 0, data, numBytes); 186 } 187 return AddData(name, type, data, numBytes); 188 } 189 190 status_t TPreferences::SetRef(const char *name, entry_ref *ref) 191 { 192 if (HasRef(name)) 193 { 194 return ReplaceRef(name, 0, ref); 195 } 196 return AddRef(name, ref); 197 } 198 199