176fd607bSopenharmony_ci/*
276fd607bSopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
376fd607bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
476fd607bSopenharmony_ci * you may not use this file except in compliance with the License.
576fd607bSopenharmony_ci * You may obtain a copy of the License at
676fd607bSopenharmony_ci *
776fd607bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
876fd607bSopenharmony_ci *
976fd607bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1076fd607bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1176fd607bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1276fd607bSopenharmony_ci * See the License for the specific language governing permissions and
1376fd607bSopenharmony_ci * limitations under the License.
1476fd607bSopenharmony_ci */
1576fd607bSopenharmony_ci#include "delete_callback.h"
1676fd607bSopenharmony_ci
1776fd607bSopenharmony_ci#include "media_library_napi.h"
1876fd607bSopenharmony_ci#include "medialibrary_napi_utils.h"
1976fd607bSopenharmony_ci#include "medialibrary_napi_log.h"
2076fd607bSopenharmony_ci#include "media_file_utils.h"
2176fd607bSopenharmony_ci#include "userfile_client.h"
2276fd607bSopenharmony_ci
2376fd607bSopenharmony_ciusing namespace std;
2476fd607bSopenharmony_ciusing namespace OHOS::DataShare;
2576fd607bSopenharmony_ci
2676fd607bSopenharmony_cinamespace OHOS {
2776fd607bSopenharmony_cinamespace Media {
2876fd607bSopenharmony_ci#ifdef HAS_ACE_ENGINE_PART
2976fd607bSopenharmony_ciDeleteCallback::DeleteCallback(napi_env env, Ace::UIContent *uiContent)
3076fd607bSopenharmony_ci{
3176fd607bSopenharmony_ci    this->env_ = env;
3276fd607bSopenharmony_ci    this->uiContent = uiContent;
3376fd607bSopenharmony_ci}
3476fd607bSopenharmony_ci#else
3576fd607bSopenharmony_ciDeleteCallback::DeleteCallback(napi_env env)
3676fd607bSopenharmony_ci{
3776fd607bSopenharmony_ci    this->env_ = env;
3876fd607bSopenharmony_ci}
3976fd607bSopenharmony_ci#endif
4076fd607bSopenharmony_ci
4176fd607bSopenharmony_civoid DeleteCallback::OnRelease(int32_t releaseCode)
4276fd607bSopenharmony_ci{
4376fd607bSopenharmony_ci    CloseModalUIExtension();
4476fd607bSopenharmony_ci}
4576fd607bSopenharmony_ci
4676fd607bSopenharmony_civoid DeleteCallback::OnResult(int32_t resultCode, const OHOS::AAFwk::Want &result)
4776fd607bSopenharmony_ci{
4876fd607bSopenharmony_ci    if (resultCode == DELETE_CODE_SUCCESS) {
4976fd607bSopenharmony_ci        this->resultCode_ = resultCode;
5076fd607bSopenharmony_ci        string trashUri = PAH_TRASH_PHOTO;
5176fd607bSopenharmony_ci        MediaLibraryNapiUtils::UriAppendKeyValue(trashUri, API_VERSION, to_string(MEDIA_API_VERSION_V10));
5276fd607bSopenharmony_ci        Uri updateAssetUri(trashUri);
5376fd607bSopenharmony_ci        DataSharePredicates predicates;
5476fd607bSopenharmony_ci        predicates.In(MediaColumn::MEDIA_ID, this->uris_);
5576fd607bSopenharmony_ci        DataShareValuesBucket valuesBucket;
5676fd607bSopenharmony_ci        valuesBucket.Put(MediaColumn::MEDIA_DATE_TRASHED, MediaFileUtils::UTCTimeSeconds());
5776fd607bSopenharmony_ci        int32_t changedRows = UserFileClient::Update(updateAssetUri, predicates, valuesBucket);
5876fd607bSopenharmony_ci        if (changedRows < 0) {
5976fd607bSopenharmony_ci            this->resultCode_ = JS_INNER_FAIL;
6076fd607bSopenharmony_ci        }
6176fd607bSopenharmony_ci    } else {
6276fd607bSopenharmony_ci        this->resultCode_ = JS_ERR_PERMISSION_DENIED;
6376fd607bSopenharmony_ci    }
6476fd607bSopenharmony_ci    SendMessageBack();
6576fd607bSopenharmony_ci}
6676fd607bSopenharmony_ci
6776fd607bSopenharmony_civoid DeleteCallback::OnError(int32_t code, const string &name, const string &message)
6876fd607bSopenharmony_ci{
6976fd607bSopenharmony_ci    this->resultCode_ = JS_INNER_FAIL;
7076fd607bSopenharmony_ci    SendMessageBack();
7176fd607bSopenharmony_ci}
7276fd607bSopenharmony_ci
7376fd607bSopenharmony_civoid DeleteCallback::OnReceive(const OHOS::AAFwk::WantParams &request)
7476fd607bSopenharmony_ci{
7576fd607bSopenharmony_ci    NAPI_INFO_LOG("OnReceive enter.");
7676fd607bSopenharmony_ci}
7776fd607bSopenharmony_ci
7876fd607bSopenharmony_civoid DeleteCallback::SetSessionId(int32_t sessionId)
7976fd607bSopenharmony_ci{
8076fd607bSopenharmony_ci    this->sessionId_ = sessionId;
8176fd607bSopenharmony_ci}
8276fd607bSopenharmony_ci
8376fd607bSopenharmony_civoid DeleteCallback::SetUris(vector<string> uris)
8476fd607bSopenharmony_ci{
8576fd607bSopenharmony_ci    this->uris_.assign(uris.begin(), uris.end());
8676fd607bSopenharmony_ci}
8776fd607bSopenharmony_ci
8876fd607bSopenharmony_civoid DeleteCallback::SetFunc(napi_value func)
8976fd607bSopenharmony_ci{
9076fd607bSopenharmony_ci    napi_valuetype valueType = napi_undefined;
9176fd607bSopenharmony_ci    napi_typeof(this->env_, func, &valueType);
9276fd607bSopenharmony_ci    if (valueType == napi_function) {
9376fd607bSopenharmony_ci        napi_create_reference(this->env_, func, ARGS_ONE, &this->callbackRef);
9476fd607bSopenharmony_ci    }
9576fd607bSopenharmony_ci}
9676fd607bSopenharmony_ci
9776fd607bSopenharmony_civoid DeleteCallback::SendMessageBack()
9876fd607bSopenharmony_ci{
9976fd607bSopenharmony_ci    CloseModalUIExtension();
10076fd607bSopenharmony_ci    napi_value undefined;
10176fd607bSopenharmony_ci    CHECK_ARGS_RET_VOID(this->env_, napi_get_undefined(this->env_, &undefined), JS_ERR_PARAMETER_INVALID);
10276fd607bSopenharmony_ci
10376fd607bSopenharmony_ci    napi_value results[ARGS_ONE] = {nullptr};
10476fd607bSopenharmony_ci    CHECK_ARGS_RET_VOID(this->env_, napi_create_object(this->env_, &results[PARAM0]), JS_ERR_PARAMETER_INVALID);
10576fd607bSopenharmony_ci
10676fd607bSopenharmony_ci    napi_value result = 0;
10776fd607bSopenharmony_ci    CHECK_ARGS_RET_VOID(this->env_, napi_create_int32(this->env_, this->resultCode_, &result),
10876fd607bSopenharmony_ci                        JS_ERR_PARAMETER_INVALID);
10976fd607bSopenharmony_ci    CHECK_ARGS_RET_VOID(this->env_, napi_set_named_property(this->env_, results[PARAM0], RESULT.c_str(), result),
11076fd607bSopenharmony_ci                        JS_ERR_PARAMETER_INVALID);
11176fd607bSopenharmony_ci
11276fd607bSopenharmony_ci    napi_value callback = nullptr;
11376fd607bSopenharmony_ci    CHECK_ARGS_RET_VOID(this->env_, napi_get_reference_value(this->env_, this->callbackRef, &callback),
11476fd607bSopenharmony_ci                        JS_ERR_PARAMETER_INVALID);
11576fd607bSopenharmony_ci    napi_value returnVal;
11676fd607bSopenharmony_ci    CHECK_ARGS_RET_VOID(this->env_, napi_call_function(this->env_, undefined, callback, ARGS_ONE, results, &returnVal),
11776fd607bSopenharmony_ci                        JS_ERR_PARAMETER_INVALID);
11876fd607bSopenharmony_ci}
11976fd607bSopenharmony_ci
12076fd607bSopenharmony_civoid DeleteCallback::CloseModalUIExtension()
12176fd607bSopenharmony_ci{
12276fd607bSopenharmony_ci#ifdef HAS_ACE_ENGINE_PART
12376fd607bSopenharmony_ci    if (this->uiContent != nullptr) {
12476fd607bSopenharmony_ci        uiContent->CloseModalUIExtension(this->sessionId_);
12576fd607bSopenharmony_ci    }
12676fd607bSopenharmony_ci#endif
12776fd607bSopenharmony_ci}
12876fd607bSopenharmony_ci}
12976fd607bSopenharmony_ci}