1 /*
2 * Copyright (c) 2024 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 #include "oh_preferences_option.h"
17
18 #include "log_print.h"
19 #include "oh_preferences_impl.h"
20 #include "oh_preferences_err_code.h"
21
22 using namespace OHOS::PreferencesNdk;
23
SetFileName(const std::string & str)24 int OH_PreferencesOption::SetFileName(const std::string &str)
25 {
26 std::unique_lock<std::shared_mutex> writeLock(opMutex_);
27 if (str.empty()) {
28 LOG_ERROR("Set file path failed, str is empty");
29 return OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM;
30 }
31 fileName = str;
32 return OH_Preferences_ErrCode::PREFERENCES_OK;
33 }
34
SetBundleName(const std::string & str)35 void OH_PreferencesOption::SetBundleName(const std::string &str)
36 {
37 std::unique_lock<std::shared_mutex> writeLock(opMutex_);
38 bundleName = str;
39 }
40
SetDataGroupId(const std::string & str)41 void OH_PreferencesOption::SetDataGroupId(const std::string &str)
42 {
43 std::unique_lock<std::shared_mutex> writeLock(opMutex_);
44 dataGroupId = str;
45 }
46
GetFileName()47 std::string OH_PreferencesOption::GetFileName()
48 {
49 return fileName;
50 }
51
GetBundleName()52 std::string OH_PreferencesOption::GetBundleName()
53 {
54 return bundleName;
55 }
56
GetDataGroupId()57 std::string OH_PreferencesOption::GetDataGroupId()
58 {
59 return dataGroupId;
60 }
61
OH_PreferencesOption_Create(void)62 OH_PreferencesOption* OH_PreferencesOption_Create(void)
63 {
64 OH_PreferencesOption* option = new (std::nothrow) OH_PreferencesOption();
65 if (option == nullptr) {
66 LOG_ERROR("new option object failed");
67 return nullptr;
68 }
69 option->cid = PreferencesNdkStructId::PREFERENCES_OH_OPTION_CID;
70 return option;
71 }
72
OH_PreferencesOption_SetFileName(OH_PreferencesOption * option,const char * fileName)73 int OH_PreferencesOption_SetFileName(OH_PreferencesOption *option, const char *fileName)
74 {
75 if (option == nullptr || fileName == nullptr ||
76 !NDKPreferencesUtils::PreferencesStructValidCheck(
77 option->cid, PreferencesNdkStructId::PREFERENCES_OH_OPTION_CID)) {
78 LOG_ERROR("set option's file path failed, option is null: %{public}d, fileName is null: %{public}d, "
79 "err: %{public}d", (option == nullptr), (fileName == nullptr),
80 OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM);
81 return OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM;
82 }
83 return option->SetFileName(std::string(fileName));
84 }
85
OH_PreferencesOption_SetBundleName(OH_PreferencesOption * option,const char * bundleName)86 int OH_PreferencesOption_SetBundleName(OH_PreferencesOption *option, const char *bundleName)
87 {
88 if (option == nullptr || bundleName == nullptr ||
89 !NDKPreferencesUtils::PreferencesStructValidCheck(
90 option->cid, PreferencesNdkStructId::PREFERENCES_OH_OPTION_CID)) {
91 LOG_ERROR("set option's bundleName failed, option is null: %{public}d, "
92 "bundleName is null: %{public}d, errCode: %{public}d", (option == nullptr),
93 (bundleName == nullptr), OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM);
94 return OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM;
95 }
96 option->SetBundleName(std::string(bundleName));
97 return OH_Preferences_ErrCode::PREFERENCES_OK;
98 }
99
OH_PreferencesOption_SetDataGroupId(OH_PreferencesOption * option,const char * dataGroupId)100 int OH_PreferencesOption_SetDataGroupId(OH_PreferencesOption *option, const char *dataGroupId)
101 {
102 if (option == nullptr || dataGroupId == nullptr ||
103 !NDKPreferencesUtils::PreferencesStructValidCheck(
104 option->cid, PreferencesNdkStructId::PREFERENCES_OH_OPTION_CID)) {
105 LOG_ERROR("set option's dataGroupId failed, option is null: %{public}d, "
106 "dataGroupId is null: %{public}d, errCode: %{public}d", (option == nullptr),
107 (dataGroupId == nullptr), OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM);
108 return OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM;
109 }
110 option->SetDataGroupId(std::string(dataGroupId));
111 return OH_Preferences_ErrCode::PREFERENCES_OK;
112 }
113
OH_PreferencesOption_Destroy(OH_PreferencesOption * option)114 int OH_PreferencesOption_Destroy(OH_PreferencesOption* option)
115 {
116 if (option == nullptr ||
117 !NDKPreferencesUtils::PreferencesStructValidCheck(
118 option->cid, PreferencesNdkStructId::PREFERENCES_OH_OPTION_CID)) {
119 LOG_ERROR("destroy option failed, option is null: %{public}d, errCode: %{public}d",
120 (option == nullptr), OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM);
121 return OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM;
122 }
123 delete option;
124 return OH_Preferences_ErrCode::PREFERENCES_OK;
125 }
126