1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PREFERENCES_H 17 #define PREFERENCES_H 18 19 #include <map> 20 #include <memory> 21 #include <string> 22 #include <vector> 23 24 #include "preferences_observer.h" 25 #include "preferences_value.h" 26 #include "preferences_visibility.h" 27 28 namespace OHOS { 29 namespace NativePreferences { 30 using RegisterMode = PreferencesObserver::RegisterMode; 31 struct Options { 32 public: OptionsOptions33 Options(const std::string inputFilePath) : filePath(inputFilePath) 34 { 35 } 36 OptionsOptions37 Options(const char *inputFilePath) : filePath(inputFilePath) 38 { 39 } 40 OptionsOptions41 Options(const std::string &inputFilePath, const std::string &inputbundleName, const std::string &inputdataGroupId) 42 : filePath(inputFilePath), bundleName(inputbundleName), dataGroupId(inputdataGroupId) 43 { 44 } 45 46 public: 47 std::string filePath{ "" }; 48 std::string bundleName{ "" }; 49 std::string dataGroupId{ "" }; 50 }; 51 /** 52 * The function class of the preference. Various operations on preferences instances are provided in this class. 53 */ 54 class PREF_API_EXPORT Preferences { 55 public: ~Preferences()56 PREF_API_EXPORT virtual ~Preferences() 57 { 58 } 59 60 /** 61 * @brief The constant Indicates the maximum length of the key in the preferences. 62 */ 63 PREF_API_EXPORT static constexpr uint32_t MAX_KEY_LENGTH = 80; 64 65 /** 66 * @brief The constant Indicates the maximum length of the value in the preferences. 67 */ 68 PREF_API_EXPORT static constexpr uint32_t MAX_VALUE_LENGTH = 8 * 1024; 69 70 /** 71 * @brief Obtains the value of a preferences. 72 * 73 * This function is used to get the value of the corresponding key in the preference. 74 * 75 * @param key Indicates the key of the preferences. It cannot be empty. 76 * @param defValue Indicates the default value of the preferences. 77 * 78 * @return Returns the value matching the specified key if it is found; returns the default value otherwise. 79 */ 80 virtual PreferencesValue Get(const std::string &key, const PreferencesValue &defValue) = 0; 81 82 /** 83 * @brief Sets a value for the key in the preferences. 84 * 85 * This function is used to set or update the value of the corresponding key in the preferences. 86 * 87 * @param key Indicates the key of the preferences. It cannot be empty. 88 * @param value Indicates the default value of the preferences. 89 * 90 * @return Returns 0 for success, others for failure. 91 */ 92 virtual int Put(const std::string &key, const PreferencesValue &value) = 0; 93 94 /** 95 * @brief Obtains the int value of a preferences. 96 * 97 * This function is used to get an int value of the corresponding key in the preference. 98 * 99 * @param key Indicates the key of the preferences. It cannot be empty. 100 * @param defValue Indicates the default value of the preferences. 101 * 102 * @return Returns a int value matching the specified key if it is found; returns the default value otherwise. 103 */ 104 virtual int GetInt(const std::string &key, const int &defValue = {}) = 0; 105 106 /** 107 * @brief Obtains the string value of a preferences. 108 * 109 * This function is used to get a string value of the corresponding key in the preference. 110 * 111 * @param key Indicates the key of the preferences. It cannot be empty. 112 * @param defValue Indicates the default value of the preferences. 113 * 114 * @return Returns a string value matching the specified key if it is found; returns the default value otherwise. 115 */ 116 virtual std::string GetString(const std::string &key, const std::string &defValue = {}) = 0; 117 118 /** 119 * @brief Obtains the bool value of a preferences. 120 * 121 * This function is used to get a bool value of the corresponding key in the preference. 122 * 123 * @param key Indicates the key of the preferences. It cannot be empty. 124 * @param defValue Indicates the default value of the preferences. 125 * 126 * @return Returns a bool value matching the specified key if it is found; returns the default value otherwise. 127 */ 128 virtual bool GetBool(const std::string &key, const bool &defValue = {}) = 0; 129 130 /** 131 * @brief Obtains the float value of a preferences. 132 * 133 * This function is used to get a float value of the corresponding key in the preference. 134 * 135 * @param key Indicates the key of the preferences. It cannot be empty. 136 * @param defValue Indicates the default value of the preferences. 137 * 138 * @return Returns a float value matching the specified key if it is found; returns the default value otherwise. 139 */ 140 virtual float GetFloat(const std::string &key, const float &defValue = {}) = 0; 141 142 /** 143 * @brief Obtains the double value of a preferences. 144 * 145 * This function is used to get a double value of the corresponding key in the preference. 146 * 147 * @param key Indicates the key of the preferences. It cannot be empty. 148 * @param defValue Indicates the default value of the preferences. 149 * 150 * @return Returns a double value matching the specified key if it is found; returns the default value otherwise. 151 */ 152 virtual double GetDouble(const std::string &key, const double &defValue = {}) = 0; 153 154 /** 155 * @brief Obtains the long value of a preferences. 156 * 157 * This function is used to get a long value of the corresponding key in the preference. 158 * 159 * @param key Indicates the key of the preferences. It cannot be empty. 160 * @param defValue Indicates the default value of the preferences. 161 * 162 * @return Returns a long value matching the specified key if it is found; returns the default value otherwise. 163 */ 164 virtual int64_t GetLong(const std::string &key, const int64_t &defValue = {}) = 0; 165 166 /** 167 * @brief Obtains all the keys and values of a preferences. 168 * 169 * This function is used to get all keys and values in an object. 170 * 171 * @return Returns a map, the key is string type and the value is PreferencesValue type. 172 */ 173 virtual std::map<std::string, PreferencesValue> GetAll() = 0; 174 175 /** 176 * @brief Checks whether contains a preferences matching a specified key. 177 * 178 * This function is used to Checks whether contains a preferences matching a specified key. 179 * 180 * @param key Indicates the key of the preferences. It cannot be empty. 181 * 182 * @return Returning true means it contains, false means it doesn't. 183 */ 184 virtual bool HasKey(const std::string &key) = 0; 185 186 /** 187 * @brief Put or update an int value of a preferences. 188 * 189 * This function is used to put or update an int value of the corresponding key in the preference. 190 * 191 * @param key Indicates the key of the preferences. It cannot be empty. 192 * @param value Indicates the value of preferences to put or update. 193 * 194 * @return Returns 0 for success, others for failure. 195 */ 196 virtual int PutInt(const std::string &key, int value) = 0; 197 198 /** 199 * @brief Put or update an string value for the key. 200 * 201 * This function is used to put or update a string value of the corresponding key in the preference. 202 * 203 * @param key Indicates the key of the preferences. It cannot be empty. 204 * @param value Indicates the value of preferences to put or update. 205 * 206 * @return Returns 0 for success, others for failure. 207 */ 208 virtual int PutString(const std::string &key, const std::string &value) = 0; 209 210 /** 211 * @brief Put or update bool string value for the key. 212 * 213 * This function is used to put or update a bool value of the corresponding key in the preference. 214 * 215 * @param key Indicates the key of the preferences. It cannot be empty. 216 * @param value Indicates the value of preferences to put or update. 217 * 218 * @return Returns 0 for success, others for failure. 219 */ 220 virtual int PutBool(const std::string &key, bool value) = 0; 221 222 /** 223 * @brief Put or update an long value for the key. 224 * 225 * This function is used to put or update a long value of the corresponding key in the preference. 226 * 227 * @param key Indicates the key of the preferences. It cannot be empty. 228 * @param value Indicates the value of preferences to put or update. 229 * 230 * @return Returns 0 for success, others for failure. 231 */ 232 virtual int PutLong(const std::string &key, int64_t value) = 0; 233 234 /** 235 * @brief Put or update an float value for the key. 236 * 237 * This function is used to put or update a float value of the corresponding key in the preference. 238 * 239 * @param key Indicates the key of the preferences. It cannot be empty. 240 * @param value Indicates the value of preferences to put or update. 241 * 242 * @return Returns 0 for success, others for failure. 243 */ 244 virtual int PutFloat(const std::string &key, float value) = 0; 245 246 /** 247 * @brief Put or update an double value for the key. 248 * 249 * This function is used to put or update a double value of the corresponding key in the preference. 250 * 251 * @param key Indicates the key of the preferences. It cannot be empty. 252 * @param value Indicates the value of preferences to put or update. 253 * 254 * @return Returns 0 for success, others for failure. 255 */ 256 virtual int PutDouble(const std::string &key, double value) = 0; 257 258 /** 259 * @brief Deletes the preferences with a specified key. 260 * 261 * This function is used to delete the preferences with a specified key in the preference. 262 * 263 * @param key Indicates the key of the preferences. It cannot be empty. 264 * 265 * @return Returns 0 for success, others for failure. 266 */ 267 virtual int Delete(const std::string &key) = 0; 268 269 /** 270 * @brief Clears all preferences. 271 * 272 * This function is used to clear all preferences in an object. 273 * 274 * @return Returns 0 for success, others for failure. 275 */ 276 virtual int Clear() = 0; 277 278 /** 279 * @brief Asynchronously saves the preferences to the file. 280 * 281 * This function is used to saves the preferences to the file. Files are written to disk only after 282 * this interface or {@link FlushSync}is called. 283 */ 284 virtual void Flush() = 0; 285 286 /** 287 * @brief Synchronously saves the preferences to the file. 288 * 289 * This function is used to saves the preferences to the file synchronously. Files are written to disk only after 290 * this interface or {@link Flush} is called. 291 * 292 * @return The result of write to disk. Returns 0 for success, others for failure. 293 */ 294 virtual int FlushSync() = 0; 295 296 /** 297 * @brief Registers an observer. 298 * 299 * This function is used to registers an observer to listen for the change of a preferences. 300 * 301 * @param preferencesObserver Indicates callback function for data changes. 302 */ 303 virtual int RegisterObserver( 304 std::shared_ptr<PreferencesObserver> preferencesObserver, RegisterMode mode = RegisterMode::LOCAL_CHANGE) = 0; 305 306 /** 307 * @brief Unregister an existing observer. 308 * 309 * This function is used to unregister an existing observer. 310 * 311 * @param preferencesObserver Indicates callback function for data changes. 312 */ 313 virtual int UnRegisterObserver( 314 std::shared_ptr<PreferencesObserver> preferencesObserver, RegisterMode mode = RegisterMode::LOCAL_CHANGE) = 0; 315 }; 316 } // End of namespace NativePreferences 317 } // End of namespace OHOS 318 #endif // End of #ifndef PREFERENCES_H 319