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()27 NativeAlbumAsset::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 };
38 NativeAlbumAsset::~NativeAlbumAsset() = default;
39 
SetAlbumId(const int32_t albumId)40 void NativeAlbumAsset::SetAlbumId(const int32_t albumId)
41 {
42     albumId_ = albumId;
43 }
44 
SetAlbumName(const string albumName)45 void NativeAlbumAsset::SetAlbumName(const string albumName)
46 {
47     albumName_ = albumName;
48 }
49 
SetAlbumUri(const string albumUri)50 void NativeAlbumAsset::SetAlbumUri(const string albumUri)
51 {
52     albumUri_ = albumUri;
53 }
54 
SetAlbumDateModified(const int64_t albumDateModified)55 void NativeAlbumAsset::SetAlbumDateModified(const int64_t albumDateModified)
56 {
57     albumDateModified_ = albumDateModified;
58 }
59 
SetCount(const int32_t count)60 void NativeAlbumAsset::SetCount(const int32_t count)
61 {
62     count_ = count;
63 }
64 
SetAlbumRelativePath(const string albumRelativePath)65 void NativeAlbumAsset::SetAlbumRelativePath(const string albumRelativePath)
66 {
67     albumRelativePath_ = albumRelativePath;
68 }
69 
SetCoverUri(const string coverUri)70 void NativeAlbumAsset::SetCoverUri(const string coverUri)
71 {
72     coverUri_ = coverUri;
73 }
74 
SetAlbumPath(const string albumPath)75 void NativeAlbumAsset::SetAlbumPath(const string albumPath)
76 {
77     albumPath_ = albumPath;
78 }
79 
GetAlbumId() const80 int32_t NativeAlbumAsset::GetAlbumId() const
81 {
82     return albumId_;
83 }
84 
GetAlbumName() const85 string NativeAlbumAsset::GetAlbumName() const
86 {
87     return albumName_;
88 }
89 
GetAlbumUri() const90 string NativeAlbumAsset::GetAlbumUri() const
91 {
92     return albumUri_;
93 }
94 
GetAlbumDateModified() const95 int64_t NativeAlbumAsset::GetAlbumDateModified() const
96 {
97     return albumDateModified_;
98 }
99 
GetCount() const100 int32_t NativeAlbumAsset::GetCount() const
101 {
102     return count_;
103 }
104 
GetAlbumRelativePath() const105 string NativeAlbumAsset::GetAlbumRelativePath() const
106 {
107     return albumRelativePath_;
108 }
109 
GetCoverUri() const110 string NativeAlbumAsset::GetCoverUri() const
111 {
112     return coverUri_;
113 }
114 
GetAlbumPath() const115 string NativeAlbumAsset::GetAlbumPath() const
116 {
117     return albumPath_;
118 }
119 
CreateAlbumAsset()120 bool NativeAlbumAsset::CreateAlbumAsset()
121 {
122     if (!(MediaFileUtils::IsDirectory(albumPath_))) {
123         return MediaFileUtils::CreateDirectory(albumPath_);
124     } else {
125         MEDIA_ERR_LOG("Cannot create album that already exists");
126         return false;
127     }
128 }
129 
DeleteAlbumAsset(const string & albumUri)130 bool NativeAlbumAsset::DeleteAlbumAsset(const string &albumUri)
131 {
132     return MediaFileUtils::DeleteDir(albumUri);
133 }
134 
ModifyAlbumAsset(const string & albumUri)135 bool NativeAlbumAsset::ModifyAlbumAsset(const string &albumUri)
136 {
137     string newAlbumUri;
138     string oldAlbumUri;
139     size_t slashIndex;
140     bool errCode = false;
141 
142     oldAlbumUri = albumUri;
143     slashIndex = albumUri.rfind("/");
144     if (slashIndex != string::npos) {
145         newAlbumUri = albumUri.substr(0, slashIndex) + SLASH_CHAR + albumName_;
146         errCode =  MediaFileUtils::RenameDir(oldAlbumUri, newAlbumUri);
147     }
148 
149     return errCode;
150 }
151 }  // namespace Media
152 }  // namespace OHOS
153