1 /*****************************************************************************/ 2 // LiveSetting 3 // Written by Michael Wilber 4 // 5 // LiveSetting.cpp 6 // 7 // This class stores the default value for an individual setting and provides 8 // functions which read and write the setting to BMessages. 9 // 10 // 11 // Copyright (C) Haiku 12 // 13 // Permission is hereby granted, free of charge, to any person obtaining a 14 // copy of this software and associated documentation files (the "Software"), 15 // to deal in the Software without restriction, including without limitation 16 // the rights to use, copy, modify, merge, publish, distribute, sublicense, 17 // and/or sell copies of the Software, and to permit persons to whom the 18 // Software is furnished to do so, subject to the following conditions: 19 // 20 // The above copyright notice and this permission notice shall be included 21 // in all copies or substantial portions of the Software. 22 // 23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 29 // DEALINGS IN THE SOFTWARE. 30 /*****************************************************************************/ 31 32 #include "LiveSetting.h" 33 34 void 35 LiveSetting::Init(uint32 id, const char *name, const LiveSettingType dataType) 36 { 37 fId = id; 38 fName = name; 39 fDataType = dataType; 40 } 41 42 LiveSetting::LiveSetting(uint32 id, const char *name, bool val) 43 { 44 Init(id, name, LIVE_SETTING_BOOL); 45 fBoolVal = val; 46 } 47 48 LiveSetting::LiveSetting(uint32 id, const char *name, int32 val) 49 { 50 Init(id, name, LIVE_SETTING_INT32); 51 fInt32Val = val; 52 } 53 54 LiveSetting::LiveSetting(uint32 id, const char *name, const char *val) 55 { 56 Init(id, name, LIVE_SETTING_STRING); 57 fCharpVal = val; 58 } 59 60 bool 61 LiveSetting::AddReplaceValue(BMessage *msgTo, BMessage *msgFrom /*= NULL*/) const 62 { 63 bool bResult = false; 64 65 switch (fDataType) { 66 case LIVE_SETTING_BOOL: 67 { 68 bool bVal; 69 if (GetValue(msgFrom, bVal) == true) 70 return AddReplaceValue(msgTo, bVal); 71 } 72 case LIVE_SETTING_INT32: 73 { 74 int32 iVal; 75 if (GetValue(msgFrom, iVal) == true) 76 return AddReplaceValue(msgTo, iVal); 77 } 78 case LIVE_SETTING_STRING: 79 { 80 BString str; 81 if (GetValue(msgFrom, str) == true) 82 return AddReplaceValue(msgTo, str); 83 } 84 default: 85 break; 86 } 87 88 return bResult; 89 } 90 91 bool 92 LiveSetting::AddReplaceValue(BMessage *msgTo, bool val) const 93 { 94 if (msgTo == NULL) 95 return false; 96 97 bool bResult = false; 98 bool dummy; 99 if (msgTo->FindBool(fName, &dummy) == B_OK) { 100 if (msgTo->ReplaceBool(fName, val) == B_OK) 101 bResult = true; 102 } else { 103 if (msgTo->AddBool(fName, val) == B_OK) 104 bResult = true; 105 } 106 107 return bResult; 108 } 109 110 bool 111 LiveSetting::AddReplaceValue(BMessage *msgTo, int32 val) const 112 { 113 if (msgTo == NULL) 114 return false; 115 116 bool bResult = false; 117 int32 dummy; 118 if (msgTo->FindInt32(fName, &dummy) == B_OK) { 119 if (msgTo->ReplaceInt32(fName, val) == B_OK) 120 bResult = true; 121 } else { 122 if (msgTo->AddInt32(fName, val) == B_OK) 123 bResult = true; 124 } 125 126 return bResult; 127 } 128 129 bool 130 LiveSetting::AddReplaceValue(BMessage *msgTo, const BString &val) const 131 { 132 if (msgTo == NULL) 133 return false; 134 135 bool bResult = false; 136 BString dummy; 137 if (msgTo->FindString(fName, &dummy) == B_OK) { 138 if (msgTo->ReplaceString(fName, val) == B_OK) 139 bResult = true; 140 } else { 141 if (msgTo->AddString(fName, val) == B_OK) 142 bResult = true; 143 } 144 145 return bResult; 146 } 147 148 bool 149 LiveSetting::GetValue(BMessage *msgFrom, bool &val) const 150 { 151 if (fDataType != LIVE_SETTING_BOOL) 152 return false; 153 154 bool bResult = false; 155 if (msgFrom == NULL) { 156 val = fBoolVal; 157 bResult = true; 158 159 } else if (msgFrom->FindBool(fName, &val) == B_OK) 160 bResult = true; 161 162 return bResult; 163 } 164 165 bool 166 LiveSetting::GetValue(BMessage *msgFrom, int32 &val) const 167 { 168 if (fDataType != LIVE_SETTING_INT32) 169 return false; 170 171 bool bResult = false; 172 if (msgFrom == NULL) { 173 val = fInt32Val; 174 bResult = true; 175 176 } else if (msgFrom->FindInt32(fName, &val) == B_OK) 177 bResult = true; 178 179 return bResult; 180 } 181 182 bool 183 LiveSetting::GetValue(BMessage *msgFrom, BString &val) const 184 { 185 if (fDataType != LIVE_SETTING_STRING) 186 return false; 187 188 bool bResult = false; 189 if (msgFrom == NULL) { 190 val = fCharpVal; 191 bResult = true; 192 193 } else if (msgFrom->FindString(fName, &val) == B_OK) 194 bResult = true; 195 196 return bResult; 197 } 198 199