1fc223305Sopenharmony_ci/* 2fc223305Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3fc223305Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fc223305Sopenharmony_ci * you may not use this file except in compliance with the License. 5fc223305Sopenharmony_ci * You may obtain a copy of the License at 6fc223305Sopenharmony_ci * 7fc223305Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fc223305Sopenharmony_ci * 9fc223305Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fc223305Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fc223305Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fc223305Sopenharmony_ci * See the License for the specific language governing permissions and 13fc223305Sopenharmony_ci * limitations under the License. 14fc223305Sopenharmony_ci */ 15fc223305Sopenharmony_ci 16fc223305Sopenharmony_ci#include "oh_preferences_option.h" 17fc223305Sopenharmony_ci 18fc223305Sopenharmony_ci#include "log_print.h" 19fc223305Sopenharmony_ci#include "oh_preferences_impl.h" 20fc223305Sopenharmony_ci#include "oh_preferences_err_code.h" 21fc223305Sopenharmony_ci 22fc223305Sopenharmony_ciusing namespace OHOS::PreferencesNdk; 23fc223305Sopenharmony_ci 24fc223305Sopenharmony_ciint OH_PreferencesOption::SetFileName(const std::string &str) 25fc223305Sopenharmony_ci{ 26fc223305Sopenharmony_ci std::unique_lock<std::shared_mutex> writeLock(opMutex_); 27fc223305Sopenharmony_ci if (str.empty()) { 28fc223305Sopenharmony_ci LOG_ERROR("Set file path failed, str is empty"); 29fc223305Sopenharmony_ci return OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM; 30fc223305Sopenharmony_ci } 31fc223305Sopenharmony_ci fileName = str; 32fc223305Sopenharmony_ci return OH_Preferences_ErrCode::PREFERENCES_OK; 33fc223305Sopenharmony_ci} 34fc223305Sopenharmony_ci 35fc223305Sopenharmony_civoid OH_PreferencesOption::SetBundleName(const std::string &str) 36fc223305Sopenharmony_ci{ 37fc223305Sopenharmony_ci std::unique_lock<std::shared_mutex> writeLock(opMutex_); 38fc223305Sopenharmony_ci bundleName = str; 39fc223305Sopenharmony_ci} 40fc223305Sopenharmony_ci 41fc223305Sopenharmony_civoid OH_PreferencesOption::SetDataGroupId(const std::string &str) 42fc223305Sopenharmony_ci{ 43fc223305Sopenharmony_ci std::unique_lock<std::shared_mutex> writeLock(opMutex_); 44fc223305Sopenharmony_ci dataGroupId = str; 45fc223305Sopenharmony_ci} 46fc223305Sopenharmony_ci 47fc223305Sopenharmony_cistd::string OH_PreferencesOption::GetFileName() 48fc223305Sopenharmony_ci{ 49fc223305Sopenharmony_ci return fileName; 50fc223305Sopenharmony_ci} 51fc223305Sopenharmony_ci 52fc223305Sopenharmony_cistd::string OH_PreferencesOption::GetBundleName() 53fc223305Sopenharmony_ci{ 54fc223305Sopenharmony_ci return bundleName; 55fc223305Sopenharmony_ci} 56fc223305Sopenharmony_ci 57fc223305Sopenharmony_cistd::string OH_PreferencesOption::GetDataGroupId() 58fc223305Sopenharmony_ci{ 59fc223305Sopenharmony_ci return dataGroupId; 60fc223305Sopenharmony_ci} 61fc223305Sopenharmony_ci 62fc223305Sopenharmony_ciOH_PreferencesOption* OH_PreferencesOption_Create(void) 63fc223305Sopenharmony_ci{ 64fc223305Sopenharmony_ci OH_PreferencesOption* option = new (std::nothrow) OH_PreferencesOption(); 65fc223305Sopenharmony_ci if (option == nullptr) { 66fc223305Sopenharmony_ci LOG_ERROR("new option object failed"); 67fc223305Sopenharmony_ci return nullptr; 68fc223305Sopenharmony_ci } 69fc223305Sopenharmony_ci option->cid = PreferencesNdkStructId::PREFERENCES_OH_OPTION_CID; 70fc223305Sopenharmony_ci return option; 71fc223305Sopenharmony_ci} 72fc223305Sopenharmony_ci 73fc223305Sopenharmony_ciint OH_PreferencesOption_SetFileName(OH_PreferencesOption *option, const char *fileName) 74fc223305Sopenharmony_ci{ 75fc223305Sopenharmony_ci if (option == nullptr || fileName == nullptr || 76fc223305Sopenharmony_ci !NDKPreferencesUtils::PreferencesStructValidCheck( 77fc223305Sopenharmony_ci option->cid, PreferencesNdkStructId::PREFERENCES_OH_OPTION_CID)) { 78fc223305Sopenharmony_ci LOG_ERROR("set option's file path failed, option is null: %{public}d, fileName is null: %{public}d, " 79fc223305Sopenharmony_ci "err: %{public}d", (option == nullptr), (fileName == nullptr), 80fc223305Sopenharmony_ci OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM); 81fc223305Sopenharmony_ci return OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM; 82fc223305Sopenharmony_ci } 83fc223305Sopenharmony_ci return option->SetFileName(std::string(fileName)); 84fc223305Sopenharmony_ci} 85fc223305Sopenharmony_ci 86fc223305Sopenharmony_ciint OH_PreferencesOption_SetBundleName(OH_PreferencesOption *option, const char *bundleName) 87fc223305Sopenharmony_ci{ 88fc223305Sopenharmony_ci if (option == nullptr || bundleName == nullptr || 89fc223305Sopenharmony_ci !NDKPreferencesUtils::PreferencesStructValidCheck( 90fc223305Sopenharmony_ci option->cid, PreferencesNdkStructId::PREFERENCES_OH_OPTION_CID)) { 91fc223305Sopenharmony_ci LOG_ERROR("set option's bundleName failed, option is null: %{public}d, " 92fc223305Sopenharmony_ci "bundleName is null: %{public}d, errCode: %{public}d", (option == nullptr), 93fc223305Sopenharmony_ci (bundleName == nullptr), OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM); 94fc223305Sopenharmony_ci return OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM; 95fc223305Sopenharmony_ci } 96fc223305Sopenharmony_ci option->SetBundleName(std::string(bundleName)); 97fc223305Sopenharmony_ci return OH_Preferences_ErrCode::PREFERENCES_OK; 98fc223305Sopenharmony_ci} 99fc223305Sopenharmony_ci 100fc223305Sopenharmony_ciint OH_PreferencesOption_SetDataGroupId(OH_PreferencesOption *option, const char *dataGroupId) 101fc223305Sopenharmony_ci{ 102fc223305Sopenharmony_ci if (option == nullptr || dataGroupId == nullptr || 103fc223305Sopenharmony_ci !NDKPreferencesUtils::PreferencesStructValidCheck( 104fc223305Sopenharmony_ci option->cid, PreferencesNdkStructId::PREFERENCES_OH_OPTION_CID)) { 105fc223305Sopenharmony_ci LOG_ERROR("set option's dataGroupId failed, option is null: %{public}d, " 106fc223305Sopenharmony_ci "dataGroupId is null: %{public}d, errCode: %{public}d", (option == nullptr), 107fc223305Sopenharmony_ci (dataGroupId == nullptr), OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM); 108fc223305Sopenharmony_ci return OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM; 109fc223305Sopenharmony_ci } 110fc223305Sopenharmony_ci option->SetDataGroupId(std::string(dataGroupId)); 111fc223305Sopenharmony_ci return OH_Preferences_ErrCode::PREFERENCES_OK; 112fc223305Sopenharmony_ci} 113fc223305Sopenharmony_ci 114fc223305Sopenharmony_ciint OH_PreferencesOption_Destroy(OH_PreferencesOption* option) 115fc223305Sopenharmony_ci{ 116fc223305Sopenharmony_ci if (option == nullptr || 117fc223305Sopenharmony_ci !NDKPreferencesUtils::PreferencesStructValidCheck( 118fc223305Sopenharmony_ci option->cid, PreferencesNdkStructId::PREFERENCES_OH_OPTION_CID)) { 119fc223305Sopenharmony_ci LOG_ERROR("destroy option failed, option is null: %{public}d, errCode: %{public}d", 120fc223305Sopenharmony_ci (option == nullptr), OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM); 121fc223305Sopenharmony_ci return OH_Preferences_ErrCode::PREFERENCES_ERROR_INVALID_PARAM; 122fc223305Sopenharmony_ci } 123fc223305Sopenharmony_ci delete option; 124fc223305Sopenharmony_ci return OH_Preferences_ErrCode::PREFERENCES_OK; 125fc223305Sopenharmony_ci} 126