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_RESTORE_BASE_H
17 #define RINGTONE_RESTORE_BASE_H
18 
19 #include "restore_interface.h"
20 #include "ringtone_restore_type.h"
21 #include "ringtone_rdb_transaction.h"
22 #include "ringtone_setting_manager.h"
23 
24 namespace OHOS {
25 namespace Media {
26 class RingtoneRestoreBase : public RestoreInterface {
27 public:
28     RingtoneRestoreBase() = default;
29     virtual ~RingtoneRestoreBase() = default;
30     int32_t Init(const std::string &backupPath) override;
31     int32_t StartRestore() override;
GetBaseDb()32     virtual std::shared_ptr<NativeRdb::RdbStore> GetBaseDb() override
33     {
34         return localRdb_;
35     }
36 protected:
37     virtual bool OnPrepare(FileInfo &info, const std::string &destPath) = 0;
38     virtual void OnFinished(std::vector<FileInfo> &fileInfos) = 0;
39     virtual std::vector<NativeRdb::ValuesBucket> MakeInsertValues(std::vector<FileInfo> &infos);
40     virtual int32_t InsertTones(std::vector<FileInfo> &infos);
LoadDualFwkConf(const std::string & backupPath)41     virtual int32_t LoadDualFwkConf(const std::string &backupPath) { return -1; }
42     virtual void CheckSetting(FileInfo &info);
43     static bool MoveFile(const std::string &src, const std::string &dst);
44     static int32_t MoveDirectory(const std::string &srcDir, const std::string &dstDir);
45     void ExtractMetaFromColumn(const std::shared_ptr<NativeRdb::ResultSet> &resultSet,
46         std::unique_ptr<RingtoneMetadata> &metadata, const std::string &col);
47     int32_t PopulateMetadata(const std::shared_ptr<NativeRdb::ResultSet> &resultSet,
48         std::unique_ptr<RingtoneMetadata> &metaData);
49     virtual void FlushSettings();
50 private:
51     static std::string GetRestoreDir(const int32_t toneType);
52     static NativeRdb::ValuesBucket SetInsertValue(const FileInfo &fileInfo);
53     int32_t BatchInsert(const std::string &tableName, std::vector<NativeRdb::ValuesBucket> &values, int64_t &rowNum);
54     bool NeedCommitSetting(const std::string &typeColumn, const std::string &sourceColumn,
55         int type, int allSetType);
56 
57     std::shared_ptr<NativeRdb::RdbStore> localRdb_ = nullptr;
58     std::unique_ptr<RingtoneSettingManager> settingMgr_ = nullptr;
59 };
60 } // namespace Media
61 } // namespace OHOS
62 
63 #endif  // RINGTONE_RESTORE_BASE_H
64