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 #ifndef RINGTONE_SETTING_MANAGER_H
17 #define RINGTONE_SETTING_MANAGER_H
18 
19 #include <string>
20 #include <unordered_set>
21 
22 #include "result_set.h"
23 #include "rdb_store.h"
24 #include "ringtone_metadata.h"
25 #include "ringtone_type.h"
26 
27 namespace OHOS {
28 namespace Media {
29 #define EXPORT __attribute__ ((visibility ("default")))
30 class RingtoneSettingManager final {
31 public:
32     EXPORT RingtoneSettingManager(std::shared_ptr<NativeRdb::RdbStore> rdb);
33     EXPORT ~RingtoneSettingManager() = default;
34     EXPORT int32_t CommitSetting(int32_t toneId, std::string &tonePath, int32_t settingType,
35         int32_t toneType, int32_t sourceType);
36     EXPORT void FlushSettings();
37     EXPORT int32_t TravelQueryResultSet(std::string querySql,
38         std::function<bool (std::shared_ptr<RingtoneMetadata> &)> func);
39     EXPORT int32_t Update(int &changedRows, const NativeRdb::ValuesBucket &values,
40         const NativeRdb::AbsRdbPredicates &predicates);
41 private:
42     struct SettingItem {
43         int32_t toneId = TONE_ID_DEFAULT;
44         int32_t settingType = TONE_SETTING_TYPE_DEFAULT;
45         int32_t toneType = TONE_TYPE_DEFAULT;
46         int32_t sourceType = SOURCE_TYPE_DEFAULT;
47     };
48     EXPORT int32_t CommitSettingCompare(int32_t settingType, int32_t toneType, int32_t sourceType);
49     EXPORT void TravelSettings(std::function<int32_t (std::string &, SettingItem &)> func);
50     EXPORT int32_t PopulateMetadata(const std::shared_ptr<NativeRdb::ResultSet> &resultSet,
51         std::unique_ptr<RingtoneMetadata> &metaData);
52     EXPORT int32_t GetMetaDataFromResultSet(std::shared_ptr<NativeRdb::ResultSet> resultSet,
53         std::vector<std::shared_ptr<RingtoneMetadata>> &metaDatas);
54     EXPORT int32_t CleanupSettingFromRdb(int32_t settingType, int32_t toneType, int32_t sourceType);
55     EXPORT int32_t UpdateSettingsWithTonePath(std::string &tonePath, int32_t settingType, int32_t toneType);
56     EXPORT int32_t UpdateSettingsWithToneId(int32_t settingType, int32_t toneId, int32_t toneType);
57     EXPORT int32_t CleanupSetting(int32_t settingType, int32_t toneType, int32_t sourceType);
58     EXPORT void ExtractMetaFromColumn(const std::shared_ptr<NativeRdb::ResultSet> &resultSet,
59         std::unique_ptr<RingtoneMetadata> &metadata, const std::string &col);
60     EXPORT int32_t UpdateSettingsByPath(std::string &tonePath, int32_t settingType, int32_t toneType,
61         int32_t sourceType);
62     EXPORT int32_t UpdateShotSetting(std::shared_ptr<RingtoneMetadata> &meta, int32_t toneType, int32_t sourceType);
63     EXPORT int32_t UpdateRingtoneSetting(std::shared_ptr<RingtoneMetadata> &meta, int32_t toneType,
64         int32_t sourceType);
65     EXPORT int32_t UpdateNotificationSetting(std::shared_ptr<RingtoneMetadata> &meta, int32_t toneType,
66         int32_t sourceType);
67     EXPORT int32_t UpdateAlarmSetting(std::shared_ptr<RingtoneMetadata> &meta, int32_t toneType,
68         int32_t sourceType);
69 private:
70     std::shared_ptr<NativeRdb::RdbStore> ringtoneRdb_ = nullptr;
71     std::unordered_multimap<std::string, SettingItem> settings_ = {};
72 };
73 } // namespace Media
74 } // namespace OHOS
75 
76 #endif // RINGTONE_SETTING_MANAGER_H
77