1/* 2 * Copyright (c) 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 */ 15import { Log } from '@ohos/base/src/main/ets/utils/Log'; 16import { BroadcastConstants } from '@ohos/base/src/main/ets/constants/BroadcastConstants'; 17import type { MenuOperationCallback } from '@ohos/base/src/main/ets/operation/MenuOperationCallback'; 18import type { MenuOperation } from '@ohos/base/src/main/ets/operation/MenuOperation'; 19import { MenuContext } from '@ohos/base/src/main/ets/operation/MenuContext'; 20import mediaModel from '@ohos/base/src/main/ets/model/MediaModel'; 21import { getFetchOptionsByItem } from '@ohos/base/src/main/ets/helper/MediaDataHelper'; 22import { SimpleAlbumDataItem } from '@ohos/base/src/main/ets/data/SimpleAlbumDataItem'; 23import { Constants } from '../constants/Constants'; 24import { getResourceString } from '@ohos/base/src/main/ets/utils/ResourceUtils'; 25import { showToast } from '@ohos/base/src/main/ets/utils/UiUtil'; 26import { MediaDataItem } from '@ohos/base/src/main/ets/data/MediaDataItem'; 27 28const TAG = "RenameMenuOperation" 29 30export class RenameMenuOperation implements MenuOperation, MenuOperationCallback { 31 private menuContext: MenuContext; 32 33 constructor(menuContext: MenuContext) { 34 this.menuContext = menuContext; 35 } 36 37 doAction(): void { 38 if (this.menuContext == null) { 39 Log.warn(TAG, 'menuContext is null, return'); 40 return; 41 } 42 let mediaItem = this.menuContext.items[0]; 43 if (mediaItem == null) { 44 Log.warn(TAG, 'mediaItem is null, return'); 45 return; 46 } 47 48 this.confirmCallback = this.confirmCallback.bind(this); 49 this.cancelCallback = this.cancelCallback.bind(this); 50 let fileName = ''; 51 if (mediaItem.title) { 52 fileName = mediaItem.title; 53 } else { 54 let index = mediaItem.displayName.lastIndexOf('.'); 55 fileName = mediaItem.displayName.substr(0, index); 56 } 57 58 this.menuContext.broadCast.emit(BroadcastConstants.SHOW_RENAME_PHOTO_DIALOG, 59 [fileName, this.confirmCallback, this.cancelCallback]); 60 } 61 62 onCompleted(): void { 63 Log.info(TAG, 'Rename data succeed!'); 64 } 65 66 onError(): void { 67 Log.error(TAG, 'Rename data failed!'); 68 } 69 70 private async confirmCallback(title: string) { 71 Log.info(TAG, `Rename confirm new name: ${title}`); 72 let mediaItem = (this.menuContext.items[0] as MediaDataItem); 73 if (mediaItem == null) { 74 Log.warn(TAG, 'mediaItem is null, return'); 75 return; 76 } 77 78 let hasSameName = await this.hasSameNameAsset(mediaItem, title); 79 if (hasSameName) { 80 Log.info(TAG, 'show find same file dialog'); 81 getResourceString($r('app.string.name_already_use')).then((message: string) => { 82 showToast(message); 83 }) 84 return; 85 } 86 try { 87 let result = await this.rename(mediaItem, title); 88 Log.info(TAG, `Rename confirm result: ${result}`); 89 this.menuContext.broadCast.emit(Constants.RENAME, [result]); 90 } catch (err) { 91 Log.error(TAG, `Rename error: ${err}`); 92 getResourceString($r('app.string.rename_failed')).then((message: string) => { 93 showToast(message); 94 }) 95 } 96 97 } 98 99 private async rename(item: MediaDataItem, name: string) { 100 Log.info(TAG, 'renameSinglePhoto start'); 101 item.setName(name); 102 return [item.title, item.displayName]; 103 } 104 105 private async hasSameNameAsset(item: MediaDataItem, name: string) { 106 Log.debug(TAG, 'hasSameNameAsset start'); 107 let fileAsset = await item.loadFileAsset(); 108 let displayName = fileAsset.displayName; 109 let index = displayName.lastIndexOf('.'); 110 displayName = name + displayName.slice(index); 111 112 let simpleAlbumDataItem = new SimpleAlbumDataItem("", displayName, fileAsset.relativePath, "", ""); 113 let fetchOption = await getFetchOptionsByItem(simpleAlbumDataItem); 114 let counts = (await mediaModel.getAllCommonMediaItem(fetchOption, true)).counts; 115 116 if (counts == 0) { 117 Log.warn(TAG, 'hasSameNameAsset is false'); 118 return false; 119 } 120 121 Log.debug(TAG, 'hasSameNameAsset true'); 122 return true; 123 } 124 125 private cancelCallback(): void { 126 Log.info(TAG, 'Rename cancel'); 127 } 128}