1/* 2 * Copyright (c) 2022-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 16import type { MenuOperation, MenuOperationCallback, } from '@ohos/common'; 17import { 18 BroadCastConstants, 19 BrowserDataFactory, 20 BrowserOperationFactory, 21 BrowserConstants as Constants, 22 Log, 23 MenuContext, 24 UiUtil 25} from '@ohos/common'; 26import userFileManager from '@ohos.filemanagement.userFileManager'; 27 28const TAG: string = 'browser_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.error(TAG, 'menuContext is null, return'); 40 return; 41 } 42 let mediaItem = this.menuContext.mediaItem; 43 if (mediaItem == null) { 44 Log.error(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 let title: string = mediaItem.getTitle(); 52 if (title) { 53 fileName = title; 54 } else { 55 let index = mediaItem.displayName.lastIndexOf('.'); 56 fileName = mediaItem.displayName.substr(0, index) 57 } 58 59 this.menuContext.broadCast.emit(BroadCastConstants.SHOW_RENAME_PHOTO_DIALOG, 60 [fileName, this.confirmCallback, this.cancelCallback]); 61 } 62 63 onCompleted(result: {title: string, displayName: string}): void { 64 Log.info(TAG, 'Rename data succeed!'); 65 } 66 67 onError(): void { 68 Log.error(TAG, 'Rename data failed!'); 69 } 70 71 async rename(uri: string, name: string): Promise<{title: string, displayName: string}> { 72 Log.info(TAG, 'renameSinglePhoto start'); 73 let operationImpl = BrowserOperationFactory.getFeature(BrowserOperationFactory.TYPE_PHOTO); 74 let dataImpl = BrowserDataFactory.getFeature(BrowserDataFactory.TYPE_PHOTO); 75 let fileAsset = await dataImpl.getDataByUri(uri); 76 operationImpl.setName(fileAsset, name); 77 await operationImpl.change(fileAsset); 78 return { title: String(fileAsset.get(userFileManager.ImageVideoKey.TITLE.toString())), 79 displayName: fileAsset.displayName }; 80 } 81 82 async confirmCallback(title: string): Promise<void> { 83 Log.info(TAG, `Rename confirm new name: ${title}`); 84 let mediaItem = this.menuContext.mediaItem; 85 if (mediaItem == null) { 86 Log.error(TAG, 'mediaItem is null, return'); 87 return; 88 } 89 try { 90 let result = await this.rename(mediaItem.uri, title); 91 Log.info(TAG, `Rename confirm result: ${JSON.stringify(result)}`); 92 this.menuContext.broadCast.emit(Constants.RENAME, [result]); 93 this.onCompleted(result); 94 } catch (err) { 95 Log.error(TAG, `Rename error: ${err}`); 96 UiUtil.showToast($r('app.string.rename_failed')); 97 } 98 } 99 100 private cancelCallback(): void { 101 Log.info(TAG, 'Rename cancel'); 102 } 103}