1 /*
2  * Copyright (c) 2023 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_UTILS_H
17 #define PREFERENCES_UTILS_H
18 
19 #include <string>
20 #include <vector>
21 
22 #ifdef NATIVE_PREFERENCES_ENABLE
23 #include "preferences.h"
24 #include "preferences_errno.h"
25 #endif
26 
27 namespace OHOS {
28 namespace UpdateEngine {
29 class PreferencesUtil {
30 public:
31     PreferencesUtil() = default;
32     virtual ~PreferencesUtil() = default;
33 
34     bool SaveString(const std::string &key, const std::string &value);
35     bool SaveInt(const std::string &key, int value);
36     bool SaveBool(const std::string &key, bool value);
37     bool SaveLong(const std::string &key, int64_t value);
38     bool SaveFloat(const std::string &key, float value);
39 
40     std::string ObtainString(const std::string &key, const std::string &defValue);
41     int ObtainInt(const std::string &key, int defValue);
42     bool ObtainBool(const std::string &key, bool defValue);
43     int64_t ObtainLong(const std::string &key, int64_t defValue);
44     float ObtainFloat(const std::string &key, float defValue);
45 
46     bool IsExist(const std::string &key);
47     bool Remove(const std::string &key);
48     bool RemoveAll();
49 
50     bool DeletePreference();
51 
52 protected:
53     virtual std::string GetPath() = 0;
54 
55 private:
56 #ifdef NATIVE_PREFERENCES_ENABLE
57     std::shared_ptr<NativePreferences::Preferences> GetPreference();
58 #endif
59 
60     bool RefreshSync();
61 
62     template <typename T>
63     bool Save(const std::string &key, const T &value);
64 
65 #ifdef NATIVE_PREFERENCES_ENABLE
66     bool SaveInner(
67         std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key, const std::string &value);
68     bool SaveInner(std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key, const int &value);
69     bool SaveInner(std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key, const bool &value);
70     bool SaveInner(std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key, const int64_t &value);
71     bool SaveInner(std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key, const float &value);
72 #endif
73     template <typename T>
74     T Obtain(const std::string &key, const T &defValue);
75 
76 #ifdef NATIVE_PREFERENCES_ENABLE
77     std::string ObtainInner(
78         std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key, const std::string &defValue);
79     int ObtainInner(std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key, const int &defValue);
80     bool ObtainInner(std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key, const bool &defValue);
81     int64_t ObtainInner(
82         std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key, const int64_t &defValue);
83     float ObtainInner(
84         std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key, const float &defValue);
85 #endif
86 };
87 } // namespace UpdateEngine
88 } // namespace OHOS
89 #endif // PREFERENCES_UTILS_H