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 "ringtone_data_command.h"
17 
18 #include "ringtone_errno.h"
19 #include "ringtone_log.h"
20 
21 namespace OHOS {
22 namespace Media {
23 using namespace std;
24 using namespace OHOS::NativeRdb;
25 using namespace OHOS::DataShare;
26 
RingtoneDataCommand(const Uri & uri,const string & table,const RingtoneOperationType type)27 RingtoneDataCommand::RingtoneDataCommand(const Uri &uri, const string &table, const RingtoneOperationType type)
28     : uri_(uri), tableName_(table), oprnType_(type)
29 {
30     toneId_ = -1;
31 }
32 
~RingtoneDataCommand()33 RingtoneDataCommand::~RingtoneDataCommand() {}
34 
35 // set functions
SetDataSharePred(const DataShare::DataSharePredicates & pred)36 void RingtoneDataCommand::SetDataSharePred(const DataShare::DataSharePredicates &pred)
37 {
38     datasharePred_ = make_unique<const DataSharePredicates>(pred);
39 }
40 
SetValueBucket(const NativeRdb::ValuesBucket & value)41 void RingtoneDataCommand::SetValueBucket(const NativeRdb::ValuesBucket &value)
42 {
43     insertValue_ = value;
44 }
45 
SetBundleName(const string & bundleName)46 void RingtoneDataCommand::SetBundleName(const string &bundleName)
47 {
48     bundleName_ = bundleName;
49 }
50 
SetResult(const string & result)51 void RingtoneDataCommand::SetResult(const string &result)
52 {
53     result_ = result;
54 }
55 
56 // get functions
GetValueBucket()57 ValuesBucket &RingtoneDataCommand::GetValueBucket()
58 {
59     return insertValue_;
60 }
61 
GetAbsRdbPredicates()62 AbsRdbPredicates *RingtoneDataCommand::GetAbsRdbPredicates()
63 {
64     if (absRdbPredicates_ == nullptr) {
65         absRdbPredicates_ = make_unique<AbsRdbPredicates>(tableName_);
66     }
67     return absRdbPredicates_.get();
68 }
69 
GetTableName()70 const string &RingtoneDataCommand::GetTableName()
71 {
72     return tableName_;
73 }
74 
GetUri() const75 const Uri &RingtoneDataCommand::GetUri() const
76 {
77     return uri_;
78 }
79 
GetBundleName()80 const string &RingtoneDataCommand::GetBundleName()
81 {
82     return bundleName_;
83 }
84 
GetResult()85 const string &RingtoneDataCommand::GetResult()
86 {
87     return result_;
88 }
89 
IsNumber(const string & str)90 static bool IsNumber(const string &str)
91 {
92     if (str.empty()) {
93         RINGTONE_DEBUG_LOG("IsNumber input is empty ");
94         return false;
95     }
96 
97     for (char const &c : str) {
98         if (isdigit(c) == 0) {
99             return false;
100         }
101     }
102     return true;
103 }
104 
GetToneIdFromUri(Uri & uri)105 int32_t RingtoneDataCommand::GetToneIdFromUri(Uri &uri)
106 {
107     string uriStr = uri.ToString();
108     if (uriStr.empty()) {
109         return E_INVALID_URI;
110     }
111 
112     auto toneId = uriStr.substr(RINGTONE_PATH_URI.size(), uriStr.size());
113     if (IsNumber(toneId)) {
114         RINGTONE_INFO_LOG("Get toneId=%{public}s from Uri", toneId.c_str());
115         return stoi(toneId);
116     }
117 
118     return E_INVALID_URI;
119 }
120 
GetOprnType() const121 RingtoneOperationType RingtoneDataCommand::GetOprnType() const
122 {
123     return oprnType_;
124 }
125 } // namespace Media
126 } // namespace OHOS
127