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  */
15  #ifndef FRAMEWORKS_SERVICES_MEDIA_MTP_INCLUDE_MTP_DATA_UTILS_H_
16  #define FRAMEWORKS_SERVICES_MEDIA_MTP_INCLUDE_MTP_DATA_UTILS_H_
17  #include <memory>
18  #include <vector>
19  #include <string>
20  #include <variant>
21  #include <sys/time.h>
22  #include <stdio.h>
23  #include <unordered_map>
24  
25  #include "datashare_result_set.h"
26  #include "mtp_operation_context.h"
27  #include "property.h"
28  #include "userfile_manager_types.h"
29  #include "result_set_utils.h"
30  
31  namespace OHOS {
32  namespace Media {
33  struct PropertyValue {
34      uint64_t outIntVal = 0;
35      uint128_t outLongVal = {0};
36      std::string outStrVal;
37  };
38  struct MovingType {
39      uint64_t parent;
40      std::string displayName;
41  };
42  const std::string MTP_FORMAT_ALL = ".all"; // Undefined
43  const std::string MTP_FORMAT_UNDEFINED = ".undefined"; // Undefined
44  const std::string MTP_FORMAT_ASSOCIATION = ".floader"; // associations (folders and directories)
45  const std::string MTP_FORMAT_SCRIPT = ".js"; // script files
46  const std::string MTP_FORMAT_EXECUTABLE = ".exe"; // executable files
47  const std::string MTP_FORMAT_TEXT = ".txt"; // text files
48  const std::string MTP_FORMAT_HTML = ".html"; // HTML files
49  const std::string MTP_FORMAT_DPOF = ".dp"; // DPOF files
50  const std::string MTP_FORMAT_AIFF = ".aiff"; // AIFF audio files
51  const std::string MTP_FORMAT_WAV = ".wav"; // WAV audio files
52  const std::string MTP_FORMAT_MP3 = ".mp3"; // MP3 audio files
53  const std::string MTP_FORMAT_AVI = ".avi"; // AVI video files
54  const std::string MTP_FORMAT_MPEG = ".mpeg"; // MPEG video files
55  const std::string MTP_FORMAT_ASF = ".asf"; // ASF files
56  // Unknown image files which are not specified in PTP specification
57  const std::string MTP_FORMAT_DEFINED = ".image"; // Unknown image files
58  const std::string MTP_FORMAT_HEIC = ".heic"; // HEIC image files
59  const std::string MTP_FORMAT_HEICS = ".heics"; // HEICS image files
60  const std::string MTP_FORMAT_HEIFS = ".heifs"; // HEIFS image files
61  const std::string MTP_FORMAT_BM = ".bm"; // BM image files
62  const std::string MTP_FORMAT_HEIF = ".heif"; // HEIF image files
63  const std::string MTP_FORMAT_HIF = ".hif"; // HIF image files
64  const std::string MTP_FORMAT_AVIF = ".avif"; // AVIF image files
65  const std::string MTP_FORMAT_CUR = ".cur"; // CUR image files
66  const std::string MTP_FORMAT_WEBP = ".webp"; // WEBP image files
67  const std::string MTP_FORMAT_DNG = ".dng"; // DNG image files
68  const std::string MTP_FORMAT_RAF = ".raf"; // RAF image files
69  const std::string MTP_FORMAT_ICO = ".ico"; // ICO image files
70  const std::string MTP_FORMAT_NRW = ".nrw"; // NRW image files
71  const std::string MTP_FORMAT_RW2 = ".rw2"; // RW2 image files
72  const std::string MTP_FORMAT_PEF = ".pef"; // PEF image files
73  const std::string MTP_FORMAT_SRW = ".srw"; // SRW image files
74  const std::string MTP_FORMAT_ARW = ".arw"; // ARW image files
75  const std::string MTP_FORMAT_SVG = ".svg"; // SVG image files
76  const std::string MTP_FORMAT_RAW = ".raw"; // RAW image files
77  const std::string MTP_FORMAT_EXIF_JPEG = ".jpeg?.jpg"; // JPEG image files
78  const std::string MTP_FORMAT_JPEG = ".jpeg";
79  const std::string MTP_FORMAT_JPG = ".jpg";
80  const std::string MTP_FORMAT_TIFF_EP = ".tiff"; // TIFF EP image files
81  const std::string MTP_FORMAT_FLASHPIX = ".swf";
82  const std::string MTP_FORMAT_BMP = ".bmp"; // BMP image files
83  const std::string MTP_FORMAT_CIFF = ".ciff";
84  const std::string MTP_FORMAT_GIF = ".giff"; // GIF image files
85  const std::string MTP_FORMAT_JFIF = ".jsif"; // JFIF image files
86  const std::string MTP_FORMAT_CD = ".cd";
87  const std::string MTP_FORMAT_PICT = ".pict"; // PICT image files
88  const std::string MTP_FORMAT_PNG = ".png"; // PNG image files
89  const std::string MTP_FORMAT_TIFF = ".tif"; // TIFF image files
90  const std::string MTP_FORMAT_TIFF_IT = ".tiff";
91  const std::string MTP_FORMAT_JP2 = ".jp2"; // JP2 files
92  const std::string MTP_FORMAT_JPX = ".jpx"; // JPX files
93  const std::string MTP_FORMAT_UNDEFINED_FIRMWARE = ".undefinedfirmware"; // firmware files
94  const std::string MTP_FORMAT_WINDOWS_IMAGE_FORMAT = ".img"; // Windows image files
95  const std::string MTP_FORMAT_UNDEFINED_AUDIO = ".undefinedaudio"; // undefined audio files files
96  const std::string MTP_FORMAT_WMA = ".wma"; // WMA audio files
97  const std::string MTP_FORMAT_OGG = ".ogg"; // OGG audio files
98  const std::string MTP_FORMAT_AAC = ".aac"; // AAC audio files
99  const std::string MTP_FORMAT_AUDIBLE = ".aa"; // Audible audio files
100  const std::string MTP_FORMAT_FLAC = ".flac"; // FLAC audio files
101  const std::string MTP_FORMAT_UNDEFINED_VIDEO = ".undefinedvideo"; // undefined video files
102  const std::string MTP_FORMAT_WMV = ".wmv"; // WMV video files
103  const std::string MTP_FORMAT_MP4_CONTAINER = ".mp4"; // MP4 files
104  const std::string MTP_FORMAT_MP2 = ".mp2"; // MP2 files
105  const std::string MTP_FORMAT_3GP_CONTAINER = ".3gp"; // 3GP files
106  const std::string MTP_FORMAT_3GPP2 = ".3gpp2"; // 3GPP2 files
107  const std::string MTP_FORMAT_3GP2 = ".3gp2"; // 3GP2 files
108  const std::string MTP_FORMAT_3G2 = ".3g2"; // 3G2 files
109  const std::string MTP_FORMAT_3GPP = ".3gpp"; // 3GPP files
110  const std::string MTP_FORMAT_M4V = ".m4v"; // M4V files
111  const std::string MTP_FORMAT_F4V = ".f4v"; // F4V files
112  const std::string MTP_FORMAT_MP4V = ".mp4v"; // MP4V files
113  const std::string MTP_FORMAT_MPEG4 = ".mpeg4"; // MPEG4 files
114  const std::string MTP_FORMAT_M2TS = ".m2ts"; // M2TS files
115  const std::string MTP_FORMAT_MTS = ".mts"; // MTS files
116  const std::string MTP_FORMAT_TS = ".ts"; // TS files
117  const std::string MTP_FORMAT_YT = ".yt"; // YT files
118  const std::string MTP_FORMAT_WRF = ".wrf"; // WRF files
119  const std::string MTP_FORMAT_MPEG2 = ".mpeg2"; // MPEG2 files
120  const std::string MTP_FORMAT_MPV2 = ".mpv2"; // MPV2 files
121  const std::string MTP_FORMAT_MP2V = ".mp2v"; // MP2V files
122  const std::string MTP_FORMAT_M2V = ".m2v"; // M2V files
123  const std::string MTP_FORMAT_M2T = ".m2t"; // M2T files
124  const std::string MTP_FORMAT_MPEG1 = ".mpeg1"; // MPEG1 files
125  const std::string MTP_FORMAT_MPV1 = ".mpv1"; // MPV1 files
126  const std::string MTP_FORMAT_MP1V = ".mp1v"; // MP1V files
127  const std::string MTP_FORMAT_M1V = ".m1v"; // M1V files
128  const std::string MTP_FORMAT_MPG = ".mpg"; // MPG files
129  const std::string MTP_FORMAT_MOV = ".mov"; // MOV files
130  const std::string MTP_FORMAT_MKV = ".mkv"; // MKV files
131  const std::string MTP_FORMAT_WEBM = ".webm"; // WEBM files
132  const std::string MTP_FORMAT_H264 = ".h264"; // H264 files
133  const std::string MTP_FORMAT_UNDEFINED_COLLECTION = ".undefinedcollections"; // undefined collections
134  const std::string MTP_FORMAT_ABSTRACT_MULTIMEDIA_ALBUM = ".album"; // multimedia albums
135  const std::string MTP_FORMAT_ABSTRACT_IMAGE_ALBUM = ".albumimage"; // image albums
136  const std::string MTP_FORMAT_ABSTRACT_AUDIO_ALBUM = ".albumaudio"; // audio albums
137  const std::string MTP_FORMAT_ABSTRACT_VIDEO_ALBUM = ".albumvideo"; // video albums
138  const std::string MTP_FORMAT_ABSTRACT_AUDIO_VIDEO_PLAYLIST = ""; // abstract AV playlists
139  const std::string MTP_FORMAT_ABSTRACT_CONTACT_GROUP = "";
140  const std::string MTP_FORMAT_ABSTRACT_MESSAGE_FOLDER = ".mesfloader";
141  const std::string MTP_FORMAT_ABSTRACT_CHAPTERED_PRODUCTION = "";
142  const std::string MTP_FORMAT_ABSTRACT_AUDIO_PLAYLIST = ".allaudio"; // abstract audio playlists
143  const std::string MTP_FORMAT_ABSTRACT_VIDEO_PLAYLIST = ".allvideo"; // abstract video playlists
144  const std::string MTP_FORMAT_ABSTRACT_MEDIACAST = ""; // abstract mediacasts
145  const std::string MTP_FORMAT_WPL_PLAYLIST = ".wpl"; // WPL playlist files
146  const std::string MTP_FORMAT_M3U_PLAYLIST = ".m3u"; // M3u playlist files
147  const std::string MTP_FORMAT_MPL_PLAYLIST = ".mpl"; // MPL playlist files
148  const std::string MTP_FORMAT_ASX_PLAYLIST = ".asx"; // ASX playlist files
149  const std::string MTP_FORMAT_PLS_PLAYLIST = ".pls"; // PLS playlist files
150  const std::string MTP_FORMAT_UNDEFINED_DOCUMENT = ".undefineddocument"; // undefined document files
151  const std::string MTP_FORMAT_ABSTRACT_DOCUMENT = ".alldocuments"; // abstract documents
152  const std::string MTP_FORMAT_XML_DOCUMENT = ".xml"; // XML documents
153  const std::string MTP_FORMAT_MICROSOFT_WORD_DOCUMENT = ".doc"; // MS Word documents
154  const std::string MTP_FORMAT_MHT_COMPILED_HTML_DOCUMENT = ".html";
155  const std::string MTP_FORMAT_MICROSOFT_EXCEL_SPREADSHEET = ""; // MS Excel spreadsheets
156  const std::string MTP_FORMAT_MICROSOFT_POWERPOINT_PRESENTATION = ""; // MS PowerPoint presentatiosn
157  const std::string MTP_FORMAT_UNDEFINED_MESSAGE = "";
158  const std::string MTP_FORMAT_ABSTRACT_MESSAGE = "";
159  const std::string MTP_FORMAT_UNDEFINED_CONTACT = "";
160  const std::string MTP_FORMAT_ABSTRACT_CONTACT = "";
161  const std::string MTP_FORMAT_VCARD_2 = "";
162  
163  class MtpDataUtils {
164  public:
165      static int32_t SolveHandlesFormatData(const uint16_t format, std::string &outExtension, MediaType &outMediaType);
166      static int32_t SolveSendObjectFormatData(const uint16_t format, MediaType &outMediaType);
167      static int32_t SolveSetObjectPropValueData(const std::shared_ptr<MtpOperationContext> &context,
168          std::string &outColName, std::variant<int64_t, std::string> &outColVal);
169      static void GetMediaTypeByformat(const uint16_t format, MediaType &outMediaType);
170      static void GetPropGropByMediaType(const MediaType &mediaType, std::vector<std::string> &outPropGrop);
171      static int32_t GetPropListBySet(const std::shared_ptr<MtpOperationContext> &context,
172          const std::shared_ptr<DataShare::DataShareResultSet> &resultSet,
173          std::shared_ptr<std::vector<Property>> &outProps);
174      static int32_t GetPropValueBySet(const uint32_t property,
175          const std::shared_ptr<DataShare::DataShareResultSet> &resultSet,
176          PropertyValue &outPropValue);
177      static int32_t GetMediaTypeByName(std::string &displayName, MediaType &outMediaType);
178      // MTP
179      static int32_t GetMtpPropList(const std::shared_ptr<std::unordered_map<uint32_t, std::string>> &handles,
180          const std::unordered_map<std::string, uint32_t> &pathHandles,
181          const std::shared_ptr<MtpOperationContext> &context, shared_ptr<vector<Property>> &outPropValue);
182      static int32_t GetMtpPropValue(const std::string &path,
183          const uint32_t property, const uint16_t format, PropertyValue &outPropValue);
184      static uint32_t GetMtpFormatByPath(const std::string &path, uint16_t &outFormat);
185      static std::string GetMovingOrEnditSourcePath(const std::string &path, const int32_t &subtype,
186          const std::shared_ptr<MtpOperationContext> &context);
187  private:
188      static int32_t GetPropList(const std::shared_ptr<MtpOperationContext> &context,
189          const std::shared_ptr<DataShare::DataShareResultSet> &resultSet,
190          const std::shared_ptr<UInt16List> &properties, std::shared_ptr<std::vector<Property>> &outProps);
191      static void GetFormatByPath(const std::string &path, uint16_t &outFormat);
192      static std::variant<int32_t, int64_t, std::string> ReturnError(const std::string &errMsg,
193          const ResultSetDataType &type);
194      static int32_t GetFormat(const std::shared_ptr<DataShare::DataShareResultSet> &resultSet, uint16_t &outFormat);
195      static void GetOneRowPropList(uint32_t handle, const std::shared_ptr<DataShare::DataShareResultSet> &resultSet,
196          const std::shared_ptr<UInt16List> &properties, std::shared_ptr<std::vector<Property>> &outProps);
197      static void GetOneRowPropVal(const std::shared_ptr<DataShare::DataShareResultSet> &resultSet,
198          const uint32_t property, PropertyValue &outPropValue);
199      static void SetOneDefaultlPropList(uint32_t handle,
200          uint16_t property, std::shared_ptr<std::vector<Property>> &outProps);
201      static void SetProperty(const std::string &column,
202          const std::shared_ptr<DataShare::DataShareResultSet> &resultSet, ResultSetDataType &type, Property &prop);
203      // MTP
204      static void GetMtpOneRowProp(const std::shared_ptr<UInt16List> &properties, const uint32_t parentId,
205          std::unordered_map<uint32_t, std::string>::iterator it, shared_ptr<vector<Property>> &outProps,
206          int32_t storageId);
207      static void SetMtpProperty(const std::string &column, const std::string &path,
208          ResultSetDataType &type, Property &prop);
209      static void SetMtpOneDefaultlPropList(uint32_t handle,
210          uint16_t property, std::shared_ptr<std::vector<Property>> &outProps, int32_t storageId);
211      static void SetPtpProperty(const std::string &column, const std::string &path, const MovingType &movingType,
212          Property &prop);
213      static void GetMovingOrEnditOneRowPropList(const shared_ptr<UInt16List> &properties, const std::string &path,
214          const std::shared_ptr<MtpOperationContext> &context, shared_ptr<vector<Property>> &outProps,
215          const MovingType &movingType);
216  };
217  } // namespace Media
218  } // namespace OHOS
219  #endif  // FRAMEWORKS_SERVICES_MEDIA_MTP_INCLUDE_MTP_DATA_UTILS_H_
220