1 /*
2  * Copyright (C) 2023 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 INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_MEDIA_ALBUM_CHANGE_REQUEST_NAPI_H
17 #define INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_MEDIA_ALBUM_CHANGE_REQUEST_NAPI_H
18 
19 #include <map>
20 
21 #include "datashare_helper.h"
22 #include "datashare_predicates.h"
23 #include "media_change_request_napi.h"
24 #include "photo_album.h"
25 #include "values_bucket.h"
26 
27 namespace OHOS {
28 namespace Media {
29 #define EXPORT __attribute__ ((visibility ("default")))
30 enum class AlbumChangeOperation {
31     CREATE_ALBUM,
32     ADD_ASSETS,
33     REMOVE_ASSETS,
34     MOVE_ASSETS,
35     RECOVER_ASSETS,
36     DELETE_ASSETS,
37     SET_ALBUM_NAME,
38     SET_COVER_URI,
39     ORDER_ALBUM,
40     SET_DISPLAY_LEVEL,
41     MERGE_ALBUM,
42     DISMISS_ASSET,
43     SET_IS_ME,
44     DISMISS,
45 };
46 
47 struct PhotoAlbumPtrCompare {
operatorPhotoAlbumPtrCompare48     bool operator()(const std::shared_ptr<PhotoAlbum>& album, const std::shared_ptr<PhotoAlbum>& albumCmp) const
49     {
50         if (album == nullptr || albumCmp == nullptr) {
51             return album < albumCmp;
52         }
53         return album->GetAlbumId() < albumCmp->GetAlbumId();
54     }
55 };
56 
57 class MediaAlbumChangeRequestNapi : public MediaChangeRequestNapi {
58 public:
59     static constexpr int32_t PORTRAIT_REMOVED = -3;
60     static const inline std::string TAG_ID = "tag_id";
61     EXPORT MediaAlbumChangeRequestNapi() = default;
62     EXPORT ~MediaAlbumChangeRequestNapi() override = default;
63 
64     EXPORT static napi_value Init(napi_env env, napi_value exports);
65 
66     std::shared_ptr<PhotoAlbum> GetPhotoAlbumInstance() const;
67     std::shared_ptr<PhotoAlbum> GetReferencePhotoAlbumInstance() const;
68     std::shared_ptr<PhotoAlbum> GetTargetPhotoAlbumInstance() const;
69     std::vector<std::string> GetAddAssetArray() const;
70     std::vector<std::string> GetRemoveAssetArray() const;
71     std::vector<std::string> GetRecoverAssetArray() const;
72     std::vector<std::string> GetDeleteAssetArray() const;
73     std::vector<std::string> GetDismissAssetArray() const;
74     std::map<std::shared_ptr<PhotoAlbum>, std::vector<std::string>, PhotoAlbumPtrCompare> GetMoveMap() const;
75     void RecordMoveAssets(std::vector<std::string>& assetArray, std::shared_ptr<PhotoAlbum>& targetAlbum);
76     void ClearAddAssetArray();
77     void ClearRemoveAssetArray();
78     void ClearRecoverAssetArray();
79     void ClearDeleteAssetArray();
80     void ClearDismissAssetArray();
81     void ClearMoveMap();
82     napi_value ApplyChanges(napi_env env, napi_callback_info info) override;
83 
84 private:
85     EXPORT static napi_value Constructor(napi_env env, napi_callback_info info);
86     EXPORT static void Destructor(napi_env env, void* nativeObject, void* finalizeHint);
87 
88     EXPORT static napi_value JSGetAlbum(napi_env env, napi_callback_info info);
89     EXPORT static napi_value JSCreateAlbumRequest(napi_env env, napi_callback_info info);
90     EXPORT static napi_value JSDeleteAlbums(napi_env env, napi_callback_info info);
91     EXPORT static napi_value JSAddAssets(napi_env env, napi_callback_info info);
92     EXPORT static napi_value JSRemoveAssets(napi_env env, napi_callback_info info);
93     EXPORT static napi_value JSMoveAssets(napi_env env, napi_callback_info info);
94     EXPORT static napi_value JSRecoverAssets(napi_env env, napi_callback_info info);
95     EXPORT static napi_value JSDeleteAssets(napi_env env, napi_callback_info info);
96     EXPORT static napi_value JSSetAlbumName(napi_env env, napi_callback_info info);
97     EXPORT static napi_value JSSetCoverUri(napi_env env, napi_callback_info info);
98     EXPORT static napi_value JSPlaceBefore(napi_env env, napi_callback_info info);
99     EXPORT static napi_value JSSetDisplayLevel(napi_env env, napi_callback_info info);
100     EXPORT static napi_value JSMergeAlbum(napi_env env, napi_callback_info info);
101     EXPORT static napi_value JSDismissAssets(napi_env env, napi_callback_info info);
102     EXPORT static napi_value JSSetIsMe(napi_env env, napi_callback_info info);
103     EXPORT static napi_value JSDismiss(napi_env env, napi_callback_info info);
104     EXPORT static bool CheckDismissAssetVaild(std::vector<std::string> &dismissAssets,
105         std::vector<std::string> &newAssetArray);
106 
107     bool CheckPortraitMergeAlbum();
108     bool CheckChangeOperations(napi_env env);
109 
110     static thread_local napi_ref constructor_;
111     std::shared_ptr<PhotoAlbum> photoAlbum_ = nullptr;
112     std::shared_ptr<PhotoAlbum> referencePhotoAlbum_ = nullptr;
113     std::shared_ptr<PhotoAlbum> targetAlbum_ = nullptr;
114     std::vector<std::string> assetsToAdd_;
115     std::vector<std::string> assetsToRemove_;
116     std::vector<std::string> assetsToRecover_;
117     std::vector<std::string> assetsToDelete_;
118     std::vector<std::string> dismissAssets_;
119     std::map<std::shared_ptr<PhotoAlbum>, std::vector<std::string>, PhotoAlbumPtrCompare> moveMap_;
120     std::vector<AlbumChangeOperation> albumChangeOperations_;
121 };
122 
123 struct MediaAlbumChangeRequestAsyncContext : public NapiError {
124     size_t argc;
125     napi_value argv[NAPI_ARGC_MAX];
126     napi_async_work work;
127     napi_deferred deferred;
128     napi_ref callbackRef;
129 
130     MediaAlbumChangeRequestNapi* objectInfo;
131     std::vector<AlbumChangeOperation> albumChangeOperations;
132     DataShare::DataSharePredicates predicates;
133     DataShare::DataShareValuesBucket valuesBucket;
134 };
135 } // namespace Media
136 } // namespace OHOS
137 
138 #endif // INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_MEDIA_ALBUM_CHANGE_REQUEST_NAPI_H