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