1bea4f105Sopenharmony_ci/*
2bea4f105Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3bea4f105Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4bea4f105Sopenharmony_ci * you may not use this file except in compliance with the License.
5bea4f105Sopenharmony_ci * You may obtain a copy of the License at
6bea4f105Sopenharmony_ci *
7bea4f105Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8bea4f105Sopenharmony_ci *
9bea4f105Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10bea4f105Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11bea4f105Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bea4f105Sopenharmony_ci * See the License for the specific language governing permissions and
13bea4f105Sopenharmony_ci * limitations under the License.
14bea4f105Sopenharmony_ci */
15bea4f105Sopenharmony_ci
16bea4f105Sopenharmony_ciimport { getFileIcon, randomId, formatSuffix } from '../../base/utils/Tools';
17bea4f105Sopenharmony_ciimport { MILLISECOND } from '../../base/constants/Constant';
18bea4f105Sopenharmony_ciimport fileAccess from '@ohos.file.fileAccess';
19bea4f105Sopenharmony_ciimport { THUMBNAIL_SIZE } from '../../base/constants/UiConstant';
20bea4f105Sopenharmony_ciimport { MimeType } from './MimeType';
21bea4f105Sopenharmony_ciimport { BasicDataSource } from './BasicDataSource';
22bea4f105Sopenharmony_ciimport AbilityCommonUtil, { ResultCodePicker } from '../../base/utils/AbilityCommonUtil';
23bea4f105Sopenharmony_ciimport { FileUtil } from '../../base/utils/FileUtil';
24bea4f105Sopenharmony_ciimport { ArrayUtil } from '../../base/utils/ArrayUtil';
25bea4f105Sopenharmony_ciimport { StartModeOptions } from '../../base/model/StartModeOptions';
26bea4f105Sopenharmony_ci
27bea4f105Sopenharmony_ciexport class BreadData {
28bea4f105Sopenharmony_ci  public title: string;
29bea4f105Sopenharmony_ci  public url: string;
30bea4f105Sopenharmony_ci  public fileIterator?: fileAccess.FileInfo;
31bea4f105Sopenharmony_ci
32bea4f105Sopenharmony_ci  constructor(obj) {
33bea4f105Sopenharmony_ci    this.title = obj.title;
34bea4f105Sopenharmony_ci    this.url = obj.url;
35bea4f105Sopenharmony_ci    this.fileIterator = obj.fileIterator;
36bea4f105Sopenharmony_ci  }
37bea4f105Sopenharmony_ci}
38bea4f105Sopenharmony_ci
39bea4f105Sopenharmony_ciexport class FileDataSource extends BasicDataSource {
40bea4f105Sopenharmony_ci  private dataArray: FilesData[] = [];
41bea4f105Sopenharmony_ci  public dataCount: number = 0;
42bea4f105Sopenharmony_ci
43bea4f105Sopenharmony_ci  public totalCount(): number {
44bea4f105Sopenharmony_ci    return this.dataArray.length;
45bea4f105Sopenharmony_ci  }
46bea4f105Sopenharmony_ci
47bea4f105Sopenharmony_ci  public getDataArray(): FilesData[] {
48bea4f105Sopenharmony_ci    return this.dataArray;
49bea4f105Sopenharmony_ci  }
50bea4f105Sopenharmony_ci
51bea4f105Sopenharmony_ci  public getFileList(): FilesData[] {
52bea4f105Sopenharmony_ci    return this.dataArray.filter(item =>!item.isFolder);
53bea4f105Sopenharmony_ci  }
54bea4f105Sopenharmony_ci
55bea4f105Sopenharmony_ci  public setData(data: FilesData[]): void {
56bea4f105Sopenharmony_ci    this.dataArray = [...data];
57bea4f105Sopenharmony_ci    this.dataCount = this.dataArray.length;
58bea4f105Sopenharmony_ci    this.notifyDataReload();
59bea4f105Sopenharmony_ci  }
60bea4f105Sopenharmony_ci
61bea4f105Sopenharmony_ci  public getData(index: number): FilesData {
62bea4f105Sopenharmony_ci    return this.dataArray[index];
63bea4f105Sopenharmony_ci  }
64bea4f105Sopenharmony_ci
65bea4f105Sopenharmony_ci  public selectAll(isSelected): void {
66bea4f105Sopenharmony_ci    this.dataArray.forEach(item => {
67bea4f105Sopenharmony_ci      item.isChecked = isSelected;
68bea4f105Sopenharmony_ci    });
69bea4f105Sopenharmony_ci  }
70bea4f105Sopenharmony_ci
71bea4f105Sopenharmony_ci  public getIndex(uri): number {
72bea4f105Sopenharmony_ci    return this.dataArray.findIndex(item => item.uri === uri);
73bea4f105Sopenharmony_ci  }
74bea4f105Sopenharmony_ci
75bea4f105Sopenharmony_ci  public getSelectedFileList(): FilesData[] {
76bea4f105Sopenharmony_ci    return this.dataArray.filter(item => item.isChecked);
77bea4f105Sopenharmony_ci  }
78bea4f105Sopenharmony_ci
79bea4f105Sopenharmony_ci  public replaceData(index, data: FilesData): void {
80bea4f105Sopenharmony_ci    this.dataArray.splice(index, 1, data);
81bea4f105Sopenharmony_ci    this.notifyDataChange(index);
82bea4f105Sopenharmony_ci  }
83bea4f105Sopenharmony_ci
84bea4f105Sopenharmony_ci  public addData(index: number, data: FilesData): void {
85bea4f105Sopenharmony_ci    this.dataArray.splice(index, 0, data);
86bea4f105Sopenharmony_ci    this.dataCount = this.dataArray.length;
87bea4f105Sopenharmony_ci    this.notifyDataAdd(index);
88bea4f105Sopenharmony_ci  }
89bea4f105Sopenharmony_ci
90bea4f105Sopenharmony_ci  public pushData(data: FilesData): void {
91bea4f105Sopenharmony_ci    this.dataArray.push(data);
92bea4f105Sopenharmony_ci    this.dataCount = this.dataArray.length;
93bea4f105Sopenharmony_ci    this.notifyDataAdd(this.dataArray.length - 1);
94bea4f105Sopenharmony_ci  }
95bea4f105Sopenharmony_ci
96bea4f105Sopenharmony_ci  public deleteData(index: number): void {
97bea4f105Sopenharmony_ci    this.dataArray.splice(index, 1);
98bea4f105Sopenharmony_ci    this.dataCount = this.dataArray.length;
99bea4f105Sopenharmony_ci    this.notifyDataDelete(index);
100bea4f105Sopenharmony_ci  }
101bea4f105Sopenharmony_ci}
102bea4f105Sopenharmony_ci
103bea4f105Sopenharmony_ciexport class FilesData {
104bea4f105Sopenharmony_ci  public id: string;
105bea4f105Sopenharmony_ci  public uri: string;
106bea4f105Sopenharmony_ci  public thumbUri: string;
107bea4f105Sopenharmony_ci  public displayName: string;
108bea4f105Sopenharmony_ci  public deviceId: string;
109bea4f105Sopenharmony_ci  public flags: number;
110bea4f105Sopenharmony_ci  public name: string;
111bea4f105Sopenharmony_ci  public fileName: string;
112bea4f105Sopenharmony_ci  public mode: string;
113bea4f105Sopenharmony_ci  public size: number;
114bea4f105Sopenharmony_ci  public mtime: number;
115bea4f105Sopenharmony_ci  public suffix: string;
116bea4f105Sopenharmony_ci  public mimeType: string;
117bea4f105Sopenharmony_ci  public icon: any;
118bea4f105Sopenharmony_ci  public gridIcon: any;
119bea4f105Sopenharmony_ci  public localGridIcon: any;
120bea4f105Sopenharmony_ci  public isChecked: boolean;
121bea4f105Sopenharmony_ci  public path?: string;
122bea4f105Sopenharmony_ci  public sub?: number;
123bea4f105Sopenharmony_ci  public scale?: number;
124bea4f105Sopenharmony_ci  public angle?: number;
125bea4f105Sopenharmony_ci  public offsetX?: number | string;
126bea4f105Sopenharmony_ci  public offsetY?: number | string;
127bea4f105Sopenharmony_ci  public picWidth?: number | string;
128bea4f105Sopenharmony_ci  public picHeight?: number | string;
129bea4f105Sopenharmony_ci  public fileIterator?: fileAccess.FileInfo;
130bea4f105Sopenharmony_ci  public isMedia: boolean = false;
131bea4f105Sopenharmony_ci  public isImage: boolean = false;
132bea4f105Sopenharmony_ci  public isVideo: boolean = false;
133bea4f105Sopenharmony_ci  public isAudio: boolean = false;
134bea4f105Sopenharmony_ci  public isFolder: boolean = false;
135bea4f105Sopenharmony_ci  public duration: number;
136bea4f105Sopenharmony_ci  public mimeTypeObj: MimeType;
137bea4f105Sopenharmony_ci  public subFolderList: FilesData[];
138bea4f105Sopenharmony_ci  public subFileList: FilesData[];
139bea4f105Sopenharmony_ci  public layer: number;
140bea4f105Sopenharmony_ci  public autoShow: boolean = false;
141bea4f105Sopenharmony_ci  public currentDir?: string;
142bea4f105Sopenharmony_ci
143bea4f105Sopenharmony_ci  constructor(obj) {
144bea4f105Sopenharmony_ci    this.isFolder = obj.isFolder || FileUtil.isFolder(obj.mode);
145bea4f105Sopenharmony_ci    this.id = obj.id || randomId();
146bea4f105Sopenharmony_ci    this.uri = obj.uri || '';
147bea4f105Sopenharmony_ci    this.deviceId = obj.deviceId || '';
148bea4f105Sopenharmony_ci    this.flags = obj.flags || 0;
149bea4f105Sopenharmony_ci    this.name = obj.name || '';
150bea4f105Sopenharmony_ci    this.fileName = obj.fileName || obj.displayName || '';
151bea4f105Sopenharmony_ci    this.suffix = formatSuffix(this.fileName);
152bea4f105Sopenharmony_ci    this.mode = obj.mode || '';
153bea4f105Sopenharmony_ci    this.size = obj.size || 0;
154bea4f105Sopenharmony_ci    this.mtime = obj.mtime * MILLISECOND.ONE_SECOND || 0;
155bea4f105Sopenharmony_ci    this.mimeType = obj.mimeType || '';
156bea4f105Sopenharmony_ci    this.isChecked = false;
157bea4f105Sopenharmony_ci    this.path = obj.path || obj.relativePath || '';
158bea4f105Sopenharmony_ci    this.sub = obj.sub || 0;
159bea4f105Sopenharmony_ci    this.scale = obj.scale || 1;
160bea4f105Sopenharmony_ci    this.angle = obj.angle || 0;
161bea4f105Sopenharmony_ci    this.offsetX = obj.offsetX || 0;
162bea4f105Sopenharmony_ci    this.offsetY = obj.offsetY || 0;
163bea4f105Sopenharmony_ci    this.picWidth = obj.picWidth || '100%';
164bea4f105Sopenharmony_ci    this.picHeight = obj.picHeight || '100%';
165bea4f105Sopenharmony_ci    this.fileIterator = obj.fileIterator;
166bea4f105Sopenharmony_ci    this.duration = obj.duration || 0;
167bea4f105Sopenharmony_ci    this.mimeTypeObj = getFileIcon(this.fileName, this.isFolder);
168bea4f105Sopenharmony_ci    this.icon = this.mimeTypeObj.getResID();
169bea4f105Sopenharmony_ci    this.gridIcon = this.mimeTypeObj.getGridResID();
170bea4f105Sopenharmony_ci    this.localGridIcon = this.mimeTypeObj.getLocalGridResID();
171bea4f105Sopenharmony_ci    if (this.mimeTypeObj.isMedia()) {
172bea4f105Sopenharmony_ci      this.thumbUri = this.uri;
173bea4f105Sopenharmony_ci    }
174bea4f105Sopenharmony_ci    if (this.isFolder) {
175bea4f105Sopenharmony_ci      this.sub = getSubFileNum(this.fileIterator);
176bea4f105Sopenharmony_ci    }
177bea4f105Sopenharmony_ci    this.currentDir = FileUtil.getCurrentDir(this.path, this.isFolder);
178bea4f105Sopenharmony_ci  }
179bea4f105Sopenharmony_ci
180bea4f105Sopenharmony_ci  setFileName(fileName: string): void {
181bea4f105Sopenharmony_ci    this.fileName = fileName;
182bea4f105Sopenharmony_ci    this.mimeTypeObj = getFileIcon(this.fileName);
183bea4f105Sopenharmony_ci    this.icon = this.mimeTypeObj.getResID();
184bea4f105Sopenharmony_ci    this.gridIcon = this.mimeTypeObj.getGridResID();
185bea4f105Sopenharmony_ci    this.localGridIcon = this.mimeTypeObj.getLocalGridResID();
186bea4f105Sopenharmony_ci    if (this.mimeTypeObj.isMedia()) {
187bea4f105Sopenharmony_ci      this.thumbUri = `${this.uri}/thumbnail/${THUMBNAIL_SIZE.WIDTH}/${THUMBNAIL_SIZE.HEIGHT}`;
188bea4f105Sopenharmony_ci    }
189bea4f105Sopenharmony_ci  }
190bea4f105Sopenharmony_ci
191bea4f105Sopenharmony_ci  pickFile(startModeOptions: StartModeOptions): void {
192bea4f105Sopenharmony_ci    AbilityCommonUtil.terminateFilePicker([this.uri], ResultCodePicker.SUCCESS, startModeOptions);
193bea4f105Sopenharmony_ci  }
194bea4f105Sopenharmony_ci
195bea4f105Sopenharmony_ci  setSubFolderList(subFolderList: FilesData[]) {
196bea4f105Sopenharmony_ci    this.subFolderList = subFolderList;
197bea4f105Sopenharmony_ci  }
198bea4f105Sopenharmony_ci
199bea4f105Sopenharmony_ci  setSubList(subList: FilesData[]) {
200bea4f105Sopenharmony_ci    if (!ArrayUtil.isEmpty(subList)) {
201bea4f105Sopenharmony_ci      let folderList: FilesData[] =[];
202bea4f105Sopenharmony_ci      let fileList: FilesData[] = [];
203bea4f105Sopenharmony_ci      for (let i = 0; i < subList.length; i++) {
204bea4f105Sopenharmony_ci        let fileData: FilesData = subList[i];
205bea4f105Sopenharmony_ci        if (fileData.isFolder) {
206bea4f105Sopenharmony_ci          folderList.push(fileData);
207bea4f105Sopenharmony_ci        } else {
208bea4f105Sopenharmony_ci          fileList.push(fileData);
209bea4f105Sopenharmony_ci        }
210bea4f105Sopenharmony_ci      }
211bea4f105Sopenharmony_ci      this.subFolderList = folderList;
212bea4f105Sopenharmony_ci      this.subFileList = fileList;
213bea4f105Sopenharmony_ci    }
214bea4f105Sopenharmony_ci  }
215bea4f105Sopenharmony_ci
216bea4f105Sopenharmony_ci  hasSubFolderList(): boolean {
217bea4f105Sopenharmony_ci    if (ArrayUtil.isEmpty(this.subFolderList)) {
218bea4f105Sopenharmony_ci      return false;
219bea4f105Sopenharmony_ci    }
220bea4f105Sopenharmony_ci    return this.subFolderList.length > 0;
221bea4f105Sopenharmony_ci  }
222bea4f105Sopenharmony_ci
223bea4f105Sopenharmony_ci  getSubFolderList(): FilesData[] {
224bea4f105Sopenharmony_ci    return this.subFolderList;
225bea4f105Sopenharmony_ci  }
226bea4f105Sopenharmony_ci
227bea4f105Sopenharmony_ci  setLayer(layer: number) {
228bea4f105Sopenharmony_ci    this.layer = layer;
229bea4f105Sopenharmony_ci  }
230bea4f105Sopenharmony_ci
231bea4f105Sopenharmony_ci  getLayer(): number {
232bea4f105Sopenharmony_ci    if (this.layer) {
233bea4f105Sopenharmony_ci      return this.layer;
234bea4f105Sopenharmony_ci    }
235bea4f105Sopenharmony_ci    return 1;
236bea4f105Sopenharmony_ci  }
237bea4f105Sopenharmony_ci}
238bea4f105Sopenharmony_ci
239bea4f105Sopenharmony_ciexport function getSubFileNum(fileInfo: fileAccess.FileInfo): number {
240bea4f105Sopenharmony_ci  let subFileNum = 0;
241bea4f105Sopenharmony_ci  if (!fileInfo) {
242bea4f105Sopenharmony_ci    return subFileNum;
243bea4f105Sopenharmony_ci  }
244bea4f105Sopenharmony_ci  let fileIterator = fileInfo.listFile();
245bea4f105Sopenharmony_ci  if (!fileIterator) {
246bea4f105Sopenharmony_ci    return subFileNum;
247bea4f105Sopenharmony_ci  }
248bea4f105Sopenharmony_ci  let result = fileIterator.next();
249bea4f105Sopenharmony_ci  let isDone = result.done;
250bea4f105Sopenharmony_ci  while (!isDone) {
251bea4f105Sopenharmony_ci    subFileNum += 1;
252bea4f105Sopenharmony_ci    result = fileIterator.next();
253bea4f105Sopenharmony_ci    isDone = result.done;
254bea4f105Sopenharmony_ci  }
255bea4f105Sopenharmony_ci  return subFileNum;
256bea4f105Sopenharmony_ci}
257bea4f105Sopenharmony_ci
258bea4f105Sopenharmony_ciexport class SelectedFileData {
259bea4f105Sopenharmony_ci  public id?: string;
260bea4f105Sopenharmony_ci  public uri: string;
261bea4f105Sopenharmony_ci  public mimeType: string;
262bea4f105Sopenharmony_ci  public thumbUri: string;
263bea4f105Sopenharmony_ci  public title: string;
264bea4f105Sopenharmony_ci  public fileName: string;
265bea4f105Sopenharmony_ci  public mode: string;
266bea4f105Sopenharmony_ci  public relativePath: string;
267bea4f105Sopenharmony_ci  public size: number;
268bea4f105Sopenharmony_ci  public albumUri: string;
269bea4f105Sopenharmony_ci  public albumName: string;
270bea4f105Sopenharmony_ci  public isFolder: boolean;
271bea4f105Sopenharmony_ci  public displayName?: string;
272bea4f105Sopenharmony_ci
273bea4f105Sopenharmony_ci  constructor(obj) {
274bea4f105Sopenharmony_ci    this.id = obj.id || -1;
275bea4f105Sopenharmony_ci    this.uri = obj.uri || '';
276bea4f105Sopenharmony_ci    this.mimeType = obj.mimetype || '';
277bea4f105Sopenharmony_ci    this.thumbUri = obj.thumbUri || '';
278bea4f105Sopenharmony_ci    this.title = obj.title || '';
279bea4f105Sopenharmony_ci    this.fileName = obj.fileName || obj.displayName || '';
280bea4f105Sopenharmony_ci    this.mode = obj.mode || '';
281bea4f105Sopenharmony_ci    this.isFolder = obj.isFolder;
282bea4f105Sopenharmony_ci    this.relativePath = obj.relativePath || '';
283bea4f105Sopenharmony_ci    this.size = obj.size || 0;
284bea4f105Sopenharmony_ci    this.albumUri = obj.albumUri || '';
285bea4f105Sopenharmony_ci    this.albumName = obj.albumName || '';
286bea4f105Sopenharmony_ci  }
287bea4f105Sopenharmony_ci}
288bea4f105Sopenharmony_ci
289bea4f105Sopenharmony_ciexport class FileData {
290bea4f105Sopenharmony_ci  public id: string;
291bea4f105Sopenharmony_ci  public fileName: string;
292bea4f105Sopenharmony_ci  public thumbnail: string;
293bea4f105Sopenharmony_ci  public editedTime: string;
294bea4f105Sopenharmony_ci  public subItemSize: number;
295bea4f105Sopenharmony_ci  public parentFolder: string;
296bea4f105Sopenharmony_ci  public fileType: string;
297bea4f105Sopenharmony_ci  public fileSize: number;
298bea4f105Sopenharmony_ci  public isChecked: boolean;
299bea4f105Sopenharmony_ci  public children: [];
300bea4f105Sopenharmony_ci
301bea4f105Sopenharmony_ci  constructor(id: string, fileName: string, thumbnail: string, editedTime: string, subItemSize: number,
302bea4f105Sopenharmony_ci              parentFolder: string, fileType: string, fileSize: number, isChecked: boolean, children: []) {
303bea4f105Sopenharmony_ci    this.id = id;
304bea4f105Sopenharmony_ci    this.fileName = fileName;
305bea4f105Sopenharmony_ci    this.thumbnail = thumbnail;
306bea4f105Sopenharmony_ci    this.editedTime = editedTime;
307bea4f105Sopenharmony_ci    this.subItemSize = subItemSize;
308bea4f105Sopenharmony_ci    this.parentFolder = parentFolder;
309bea4f105Sopenharmony_ci    this.fileType = fileType;
310bea4f105Sopenharmony_ci    this.fileSize = fileSize;
311bea4f105Sopenharmony_ci    this.isChecked = isChecked;
312bea4f105Sopenharmony_ci    this.children = children;
313bea4f105Sopenharmony_ci  }
314bea4f105Sopenharmony_ci}
315bea4f105Sopenharmony_ci
316bea4f105Sopenharmony_ciexport class FileDetail {
317bea4f105Sopenharmony_ci  public key: string | Resource;
318bea4f105Sopenharmony_ci  public value: string | Resource;
319bea4f105Sopenharmony_ci  public path?: string = '';
320bea4f105Sopenharmony_ci  public params?: any;
321bea4f105Sopenharmony_ci
322bea4f105Sopenharmony_ci  constructor(key: string, value: string, path: string, params: any) {
323bea4f105Sopenharmony_ci    this.key = key;
324bea4f105Sopenharmony_ci    this.value = value;
325bea4f105Sopenharmony_ci    this.path = path;
326bea4f105Sopenharmony_ci    this.params = params;
327bea4f105Sopenharmony_ci  }
328bea4f105Sopenharmony_ci}
329bea4f105Sopenharmony_ci
330bea4f105Sopenharmony_ciexport class BottomOptions {
331bea4f105Sopenharmony_ci  public optionName: string;
332bea4f105Sopenharmony_ci  public name: string;
333bea4f105Sopenharmony_ci  public icon: Resource;
334bea4f105Sopenharmony_ci  public disabled: boolean = false;
335bea4f105Sopenharmony_ci  public display: boolean = true;
336bea4f105Sopenharmony_ci
337bea4f105Sopenharmony_ci  constructor(optionName: string, name: string, icon: Resource, disabled: boolean, display: boolean) {
338bea4f105Sopenharmony_ci    this.optionName = optionName;
339bea4f105Sopenharmony_ci    this.name = name;
340bea4f105Sopenharmony_ci    this.icon = icon;
341bea4f105Sopenharmony_ci    this.disabled = disabled;
342bea4f105Sopenharmony_ci    this.display = display;
343bea4f105Sopenharmony_ci  }
344bea4f105Sopenharmony_ci}