1 /* 2 * Copyright (C) 2021-2022 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 #define MLOG_TAG "Album" 16 17 #include "native_album_asset.h" 18 19 #include "media_file_utils.h" 20 #include "media_log.h" 21 #include "medialibrary_type_const.h" 22 23 using namespace std; 24 25 namespace OHOS { 26 namespace Media { NativeAlbumAsset()27NativeAlbumAsset::NativeAlbumAsset() 28 { 29 albumId_ = DEFAULT_ALBUM_ID; 30 albumName_ = DEFAULT_ALBUM_NAME; 31 albumUri_ = DEFAULT_ALBUM_URI; 32 albumDateModified_ = DEFAULT_ALBUM_DATE_MODIFIED; 33 count_ = DEFAULT_COUNT; 34 albumRelativePath_ = DEFAULT_ALBUM_RELATIVE_PATH; 35 coverUri_ = DEFAULT_COVERURI; 36 albumPath_ = DEFAULT_ALBUM_PATH; 37 albumVirtual_ = DEFAULT_ALBUM_VIRTUAL; 38 }; 39 NativeAlbumAsset::~NativeAlbumAsset() = default; 40 SetAlbumId(const int32_t albumId)41void NativeAlbumAsset::SetAlbumId(const int32_t albumId) 42 { 43 albumId_ = albumId; 44 } 45 SetAlbumName(const string albumName)46void NativeAlbumAsset::SetAlbumName(const string albumName) 47 { 48 albumName_ = albumName; 49 } 50 SetAlbumUri(const string albumUri)51void NativeAlbumAsset::SetAlbumUri(const string albumUri) 52 { 53 albumUri_ = albumUri; 54 } 55 SetAlbumDateModified(const int64_t albumDateModified)56void NativeAlbumAsset::SetAlbumDateModified(const int64_t albumDateModified) 57 { 58 albumDateModified_ = albumDateModified; 59 } 60 SetCount(const int32_t count)61void NativeAlbumAsset::SetCount(const int32_t count) 62 { 63 count_ = count; 64 } 65 SetAlbumRelativePath(const string albumRelativePath)66void NativeAlbumAsset::SetAlbumRelativePath(const string albumRelativePath) 67 { 68 albumRelativePath_ = albumRelativePath; 69 } 70 SetCoverUri(const string coverUri)71void NativeAlbumAsset::SetCoverUri(const string coverUri) 72 { 73 coverUri_ = coverUri; 74 } 75 SetAlbumPath(const string albumPath)76void NativeAlbumAsset::SetAlbumPath(const string albumPath) 77 { 78 albumPath_ = albumPath; 79 } 80 SetAlbumVirtual(const bool albumVirtual)81void NativeAlbumAsset::SetAlbumVirtual(const bool albumVirtual) 82 { 83 albumVirtual_ = albumVirtual; 84 } 85 86 GetAlbumId() const87int32_t NativeAlbumAsset::GetAlbumId() const 88 { 89 return albumId_; 90 } 91 GetAlbumName() const92string NativeAlbumAsset::GetAlbumName() const 93 { 94 return albumName_; 95 } 96 GetAlbumUri() const97string NativeAlbumAsset::GetAlbumUri() const 98 { 99 return albumUri_; 100 } 101 GetAlbumDateModified() const102int64_t NativeAlbumAsset::GetAlbumDateModified() const 103 { 104 return albumDateModified_; 105 } 106 GetCount() const107int32_t NativeAlbumAsset::GetCount() const 108 { 109 return count_; 110 } 111 GetAlbumRelativePath() const112string NativeAlbumAsset::GetAlbumRelativePath() const 113 { 114 return albumRelativePath_; 115 } 116 GetCoverUri() const117string NativeAlbumAsset::GetCoverUri() const 118 { 119 return coverUri_; 120 } 121 GetAlbumPath() const122string NativeAlbumAsset::GetAlbumPath() const 123 { 124 return albumPath_; 125 } 126 GetAlbumVirtual() const127bool NativeAlbumAsset::GetAlbumVirtual() const 128 { 129 return albumVirtual_; 130 } 131 CreateAlbumAsset()132bool NativeAlbumAsset::CreateAlbumAsset() 133 { 134 if (!(MediaFileUtils::IsDirectory(albumPath_))) { 135 return MediaFileUtils::CreateDirectory(albumPath_); 136 } else { 137 MEDIA_ERR_LOG("Cannot create album that already exists"); 138 return false; 139 } 140 } 141 DeleteAlbumAsset(const string & albumUri)142bool NativeAlbumAsset::DeleteAlbumAsset(const string &albumUri) 143 { 144 return MediaFileUtils::DeleteDir(albumUri); 145 } 146 ModifyAlbumAsset(const string & albumUri)147bool NativeAlbumAsset::ModifyAlbumAsset(const string &albumUri) 148 { 149 string newAlbumUri; 150 string oldAlbumUri; 151 size_t slashIndex; 152 bool errCode = false; 153 154 oldAlbumUri = albumUri; 155 slashIndex = albumUri.rfind("/"); 156 if (slashIndex != string::npos) { 157 newAlbumUri = albumUri.substr(0, slashIndex) + SLASH_CHAR + albumName_; 158 errCode = MediaFileUtils::RenameDir(oldAlbumUri, newAlbumUri); 159 } 160 161 return errCode; 162 } 163 } // namespace Media 164 } // namespace OHOS 165