• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development 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 #include "preferences_util.h"
17 #include "common/sharing_log.h"
18 
19 namespace OHOS {
20 namespace Sharing {
21 const std::string SPLIT = ",";
22 
PreferencesUtil(const std::string & path)23 PreferencesUtil::PreferencesUtil(const std::string &path)
24 {
25     path_ = path;
26 }
27 
GetProfiles(const std::string & path,int & errCode)28 std::shared_ptr<NativePreferences::Preferences> PreferencesUtil::GetProfiles(const std::string &path, int &errCode)
29 {
30     return NativePreferences::PreferencesHelper::GetPreferences(path_, errCode);
31 }
32 
PutString(const std::string & key,const std::string & value)33 int PreferencesUtil::PutString(const std::string &key, const std::string &value)
34 {
35     std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_);
36     if (ptr == nullptr) {
37         SHARING_LOGE("get profiles failed. errcode_ %{public}d.", errCode_);
38         return NativePreferences::E_ERROR;
39     }
40     preferencesMutex_.lock();
41     int result = ptr->PutString(key, value);
42     ptr->FlushSync();
43     preferencesMutex_.unlock();
44     return result;
45 }
46 
GetString(const std::string & key)47 std::string PreferencesUtil::GetString(const std::string &key)
48 {
49     std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_);
50     if (ptr == nullptr) {
51         SHARING_LOGE("get profiles failed. errcode_ %{public}d.", errCode_);
52         return "";
53     }
54     std::string defaultValue = "";
55     preferencesMutex_.lock();
56     std::string result = ptr->GetString(key, defaultValue);
57     preferencesMutex_.unlock();
58     return result;
59 }
60 
HasKey(const std::string & key)61 bool PreferencesUtil::HasKey(const std::string &key)
62 {
63     std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_);
64     if (ptr == nullptr) {
65         SHARING_LOGE("get profiles failed. errcode_ %{public}d.", errCode_);
66         return NativePreferences::E_ERROR;
67     }
68     preferencesMutex_.lock();
69     int result = ptr->HasKey(key);
70     preferencesMutex_.unlock();
71     return result;
72 }
73 
DeleteKey(const std::string & key)74 int PreferencesUtil::DeleteKey(const std::string &key)
75 {
76     std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_);
77     if (ptr == nullptr) {
78         SHARING_LOGE("get profiles failed. errcode_ %{public}d.", errCode_);
79         return NativePreferences::E_ERROR;
80     }
81     preferencesMutex_.lock();
82     int result = ptr->Delete(key);
83     ptr->FlushSync();
84     preferencesMutex_.unlock();
85     return result;
86 }
87 
Clear()88 int PreferencesUtil::Clear()
89 {
90     std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_);
91     if (ptr == nullptr) {
92         SHARING_LOGE("get profiles failed. errcode_ %{public}d.", errCode_);
93         return NativePreferences::E_ERROR;
94     }
95     preferencesMutex_.lock();
96     int result = ptr->Clear();
97     preferencesMutex_.unlock();
98     return result;
99 }
100 
Flush()101 void PreferencesUtil::Flush()
102 {
103     std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_);
104     if (ptr != nullptr) {
105         ptr->Flush();
106     }
107 }
108 
FlushSync()109 int PreferencesUtil::FlushSync()
110 {
111     std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_);
112     if (ptr == nullptr) {
113         SHARING_LOGE("get profiles failed. errcode_ %{public}d.", errCode_);
114         return NativePreferences::E_ERROR;
115     }
116     return ptr->FlushSync();
117 }
118 
119 } // namespace Sharing
120 } // namespace OHOS