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 #ifndef OHOS_MEDIA_PHOTO_ALBUM_RESTORE 16 #define OHOS_MEDIA_PHOTO_ALBUM_RESTORE 17 18 #include <string> 19 #include <vector> 20 21 #include "rdb_store.h" 22 #include "photo_album_dao.h" 23 #include "media_log.h" 24 25 namespace OHOS::Media { 26 class PhotoAlbumRestore { 27 public: 28 struct GalleryAlbumRowData { 29 std::string albumId; 30 std::string relativeBucketId; 31 std::string albumName; 32 std::string bundleName; 33 std::string lPath; 34 int32_t priority = 1; 35 }; 36 37 /** 38 * @brief Restore Start Event Handler. 39 */ OnStart(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb,std::shared_ptr<NativeRdb::RdbStore> galleryRdb)40 void OnStart(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb, std::shared_ptr<NativeRdb::RdbStore> galleryRdb) 41 { 42 this->mediaLibraryRdb_ = mediaLibraryRdb; 43 this->galleryRdb_ = galleryRdb; 44 this->photoAlbumDao_.SetMediaLibraryRdb(mediaLibraryRdb); 45 } 46 TRACE_LOG(std::vector<GalleryAlbumRowData> & galleryAlbumInfos)47 void TRACE_LOG(std::vector<GalleryAlbumRowData> &galleryAlbumInfos) 48 { 49 MEDIA_INFO_LOG( 50 "Media_Restore: galleryAlbumInfos size : %{public}d", static_cast<int32_t>(galleryAlbumInfos.size())); 51 for (auto &info : galleryAlbumInfos) { 52 MEDIA_INFO_LOG("Media_Restore: restore album info: albumId = %{public}s, \ 53 relativeBucketId = %{public}s, \ 54 albumName = %{public}s, \ 55 bundleName = %{public}s, \ 56 lPath = %{public}s, \ 57 priority = %{public}d", 58 info.albumId.c_str(), 59 info.relativeBucketId.c_str(), 60 info.albumName.c_str(), 61 info.bundleName.c_str(), 62 info.lPath.c_str(), 63 info.priority); 64 } 65 } 66 TRACE_LOG(std::vector<PhotoAlbumDao::PhotoAlbumRowData> & albumInfos)67 void TRACE_LOG(std::vector<PhotoAlbumDao::PhotoAlbumRowData> &albumInfos) 68 { 69 MEDIA_INFO_LOG("Media_Restore: albumInfos size : %{public}d", static_cast<int32_t>(albumInfos.size())); 70 for (auto &info : albumInfos) { 71 MEDIA_INFO_LOG("Media_Restore: restore album info: albumId = %{public}d, \ 72 albumName = %{public}s, \ 73 albumType = %{public}d, \ 74 albumSubType = %{public}d, \ 75 lPath = %{public}s, \ 76 bundleName = %{public}s, \ 77 priority = %{public}d", 78 info.albumId, 79 info.albumName.c_str(), 80 info.albumType, 81 info.albumSubType, 82 info.lPath.c_str(), 83 info.bundleName.c_str(), 84 info.priority); 85 } 86 } 87 Restore()88 int32_t Restore() 89 { 90 // fetch all albums from galleryRdb 91 std::vector<GalleryAlbumRowData> galleryAlbumInfos = this->GetGalleryAlbums(); 92 TRACE_LOG(galleryAlbumInfos); 93 // fetch all albums from mediaLibraryRdb 94 std::vector<PhotoAlbumDao::PhotoAlbumRowData> albumInfos = this->photoAlbumDao_.GetPhotoAlbums(); 95 TRACE_LOG(albumInfos); 96 // compare albums to find the album that need to be restored 97 std::vector<PhotoAlbumDao::PhotoAlbumRowData> albumInfosToRestore = 98 this->GetAlbumsToRestore(albumInfos, galleryAlbumInfos); 99 TRACE_LOG(albumInfosToRestore); 100 // restore albums 101 return this->photoAlbumDao_.RestoreAlbums(albumInfosToRestore); 102 } 103 104 std::vector<PhotoAlbumDao::PhotoAlbumRowData> GetAlbumsToRestore( 105 const std::vector<PhotoAlbumDao::PhotoAlbumRowData> &photoAlbums, 106 const std::vector<GalleryAlbumRowData> &galleryAlbums); 107 108 private: 109 std::vector<GalleryAlbumRowData> GetGalleryAlbums(); 110 111 private: 112 std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb_; 113 std::shared_ptr<NativeRdb::RdbStore> galleryRdb_; 114 PhotoAlbumDao photoAlbumDao_; 115 116 private: 117 const std::string GALLERY_ALBUM_ID = "albumId"; 118 const std::string GALLERY_ALBUM_NAME = "albumName"; 119 const std::string GALLERY_ALBUM_BUCKET_ID = "relativeBucketId"; 120 const std::string GALLERY_ALBUM_lPATH = "lPath"; 121 const std::string GALLERY_BUNDLE_NAME = "bundleName"; 122 const std::string GALLERY_PRIORITY = "priority"; 123 124 const std::string SQL_GALLERY_ALBUM_SELECT = "\ 125 SELECT \ 126 albumId, \ 127 CASE \ 128 WHEN album_plugin.album_name IS NOT NULL OR album_plugin.album_name <> '' \ 129 THEN album_plugin.album_name \ 130 WHEN album_plugin.album_name_en IS NOT NULL OR album_plugin.album_name_en <> '' \ 131 THEN album_plugin.album_name_en \ 132 WHEN garbage_album.nick_name IS NOT NULL OR garbage_album.nick_name <> '' \ 133 THEN garbage_album.nick_name \ 134 ELSE albumName \ 135 END AS albumName, \ 136 relativeBucketId, \ 137 gallery_album.lPath AS lPath, \ 138 COALESCE(album_plugin.bundle_name, '') AS bundleName, \ 139 COALESCE(priority, 1) AS priority \ 140 FROM gallery_album \ 141 LEFT JOIN garbage_album \ 142 ON gallery_album.lPath = garbage_album.nick_dir \ 143 LEFT JOIN album_plugin \ 144 ON gallery_album.lPath = album_plugin.lpath \ 145 WHERE gallery_album.lPath !='/Pictures/cloud/Imports' \ 146 AND gallery_album.lPath !='/Pictures/hiddenAlbum' \ 147 ORDER BY gallery_album.albumId \ 148 LIMIT ?, ? ;"; 149 }; 150 } // namespace OHOS::Media 151 152 #endif // OHOS_MEDIA_PHOTO_ALBUM_RESTORE