- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / file_manager / foreground / js / file_type.js
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 'use strict';
6
7 /**
8  * Namespace object for file type utility functions.
9  */
10 var FileType = {};
11
12 /**
13  * Description of known file types.
14  * Pair type-subtype defines order when sorted by file type.
15  */
16 FileType.types = [
17   // Images
18   {type: 'image', name: 'IMAGE_FILE_TYPE', subtype: 'JPEG',
19    pattern: /\.jpe?g$/i},
20   {type: 'image', name: 'IMAGE_FILE_TYPE', subtype: 'BMP',
21    pattern: /\.bmp$/i},
22   {type: 'image', name: 'IMAGE_FILE_TYPE', subtype: 'GIF',
23    pattern: /\.gif$/i},
24   {type: 'image', name: 'IMAGE_FILE_TYPE', subtype: 'ICO',
25    pattern: /\.ico$/i},
26   {type: 'image', name: 'IMAGE_FILE_TYPE', subtype: 'PNG',
27    pattern: /\.png$/i},
28   {type: 'image', name: 'IMAGE_FILE_TYPE', subtype: 'WebP',
29    pattern: /\.webp$/i},
30   {type: 'image', name: 'IMAGE_FILE_TYPE', subtype: 'TIFF',
31    pattern: /\.tiff?$/i},
32
33   // Video
34   {type: 'video', name: 'VIDEO_FILE_TYPE', subtype: '3GP',
35    pattern: /\.3gp$/i},
36   {type: 'video', name: 'VIDEO_FILE_TYPE', subtype: 'AVI',
37    pattern: /\.avi$/i},
38   {type: 'video', name: 'VIDEO_FILE_TYPE', subtype: 'QuickTime',
39    pattern: /\.mov$/i},
40   {type: 'video', name: 'VIDEO_FILE_TYPE', subtype: 'MKV',
41    pattern: /\.mkv$/i},
42   {type: 'video', name: 'VIDEO_FILE_TYPE', subtype: 'MPEG',
43    pattern: /\.m(p4|4v|pg|peg|pg4|peg4)$/i},
44   {type: 'video', name: 'VIDEO_FILE_TYPE', subtype: 'OGG',
45    pattern: /\.og(m|v|x)$/i},
46   {type: 'video', name: 'VIDEO_FILE_TYPE', subtype: 'WebM',
47    pattern: /\.webm$/i},
48
49   // Audio
50   {type: 'audio', name: 'AUDIO_FILE_TYPE', subtype: 'AMR',
51    pattern: /\.amr$/i},
52   {type: 'audio', name: 'AUDIO_FILE_TYPE', subtype: 'FLAC',
53    pattern: /\.flac$/i},
54   {type: 'audio', name: 'AUDIO_FILE_TYPE', subtype: 'MP3',
55    pattern: /\.mp3$/i},
56   {type: 'audio', name: 'AUDIO_FILE_TYPE', subtype: 'MPEG',
57    pattern: /\.m4a$/i},
58   {type: 'audio', name: 'AUDIO_FILE_TYPE', subtype: 'OGG',
59    pattern: /\.og(a|g)$/i},
60   {type: 'audio', name: 'AUDIO_FILE_TYPE', subtype: 'WAV',
61    pattern: /\.wav$/i},
62
63   // Text
64   {type: 'text', name: 'PLAIN_TEXT_FILE_TYPE', subtype: 'TXT',
65    pattern: /\.txt$/i},
66
67   // Archive
68   {type: 'archive', name: 'ZIP_ARCHIVE_FILE_TYPE', subtype: 'ZIP',
69    pattern: /\.zip$/i},
70   {type: 'archive', name: 'RAR_ARCHIVE_FILE_TYPE', subtype: 'RAR',
71    pattern: /\.rar$/i},
72   {type: 'archive', name: 'TAR_ARCHIVE_FILE_TYPE', subtype: 'TAR',
73    pattern: /\.tar$/i},
74   {type: 'archive', name: 'TAR_BZIP2_ARCHIVE_FILE_TYPE', subtype: 'TBZ2',
75    pattern: /\.(tar\.bz2|tbz|tbz2)$/i},
76   {type: 'archive', name: 'TAR_GZIP_ARCHIVE_FILE_TYPE', subtype: 'TGZ',
77    pattern: /\.(tar\.|t)gz$/i},
78
79   // Hosted docs.
80   {type: 'hosted', icon: 'gdoc', name: 'GDOC_DOCUMENT_FILE_TYPE',
81    subtype: 'doc', pattern: /\.gdoc$/i},
82   {type: 'hosted', icon: 'gsheet', name: 'GSHEET_DOCUMENT_FILE_TYPE',
83    subtype: 'sheet', pattern: /\.gsheet$/i},
84   {type: 'hosted', icon: 'gslides', name: 'GSLIDES_DOCUMENT_FILE_TYPE',
85    subtype: 'slides', pattern: /\.gslides$/i},
86   {type: 'hosted', icon: 'gdraw', name: 'GDRAW_DOCUMENT_FILE_TYPE',
87    subtype: 'draw', pattern: /\.gdraw$/i},
88   {type: 'hosted', icon: 'gtable', name: 'GTABLE_DOCUMENT_FILE_TYPE',
89    subtype: 'table', pattern: /\.gtable$/i},
90   {type: 'hosted', icon: 'glink', name: 'GLINK_DOCUMENT_FILE_TYPE',
91    subtype: 'glink', pattern: /\.glink$/i},
92   {type: 'hosted', icon: 'gform', name: 'GFORM_DOCUMENT_FILE_TYPE',
93    subtype: 'form', pattern: /\.gform$/i},
94
95   // Others
96   {type: 'document', icon: 'pdf', name: 'PDF_DOCUMENT_FILE_TYPE',
97    subtype: 'PDF', pattern: /\.pdf$/i},
98   {type: 'document', name: 'HTML_DOCUMENT_FILE_TYPE',
99    subtype: 'HTML', pattern: /\.(html?|mht|mhtml)$/i},
100   {type: 'document', icon: 'word', name: 'WORD_DOCUMENT_FILE_TYPE',
101    subtype: 'Word', pattern: /\.(doc|docx)$/i},
102   {type: 'document', icon: 'ppt', name: 'POWERPOINT_PRESENTATION_FILE_TYPE',
103    subtype: 'PPT', pattern: /\.(ppt|pptx)$/i},
104   {type: 'document', icon: 'excel', name: 'EXCEL_FILE_TYPE',
105    subtype: 'Excel', pattern: /\.(xls|xlsx)$/i}
106 ];
107
108 /**
109  * A special type for directory.
110  */
111 FileType.DIRECTORY = {name: 'FOLDER', type: '.folder', icon: 'folder'};
112
113 /**
114  * Returns the file path extension for a given file.
115  *
116  * @param {string|Entry} file Reference to the file.
117  *     Can be a name, a path, a url or a File API Entry.
118  * @return {string} The extension including a leading '.', or empty string if
119  *     not found.
120  */
121 FileType.getExtension = function(file) {
122   var fileName;
123   if (typeof file == 'object') {
124     if (file.isDirectory) {
125       // No extension for a directory.
126       return '';
127     } else {
128       fileName = file.name;
129     }
130   } else {
131     fileName = file;
132   }
133
134   var extensionStartIndex = fileName.lastIndexOf('.');
135   if (extensionStartIndex == -1 || extensionStartIndex == fileName.length - 1) {
136     return '';
137   }
138   return fileName.substr(extensionStartIndex);
139 };
140
141 /**
142  * Get the file type object for a given file.
143  *
144  * @param {string|Entry} file Reference to the file.
145  *     Can be a name, a path, a url or a File API Entry.
146  * @return {Object} The matching file type object or an empty object.
147  */
148 FileType.getType = function(file) {
149   if (typeof file == 'object') {
150     if (file.isDirectory)
151       return FileType.DIRECTORY;
152     else
153       file = file.name;
154   }
155   var types = FileType.types;
156   for (var i = 0; i < types.length; i++) {
157     if (types[i].pattern.test(file)) {
158       return types[i];
159     }
160   }
161
162   // Unknown file type.
163   var extension = FileType.getExtension(file);
164   if (extension == '') {
165     return { name: 'NO_EXTENSION_FILE_TYPE', type: 'UNKNOWN', icon: '' };
166   }
167   // subtype is the extension excluding the first dot.
168   return { name: 'GENERIC_FILE_TYPE', type: 'UNKNOWN',
169            subtype: extension.substr(1).toUpperCase(), icon: '' };
170 };
171
172 /**
173  * @param {string|Entry} file Reference to the file.
174  *     Can be a name, a path, a url or a File API Entry.
175  * @return {string} Localized string representation of file type.
176  */
177 FileType.getTypeString = function(file) {
178   var fileType = FileType.getType(file);
179   if (fileType.subtype)
180     return strf(fileType.name, fileType.subtype);
181   else
182     return str(fileType.name);
183 };
184
185 /**
186  * Pattern for urls pointing to Google Drive files.
187  */
188 FileType.DRIVE_URL_PATTERN =
189     new RegExp('^filesystem:[^/]*://[^/]*/[^/]*/drive/(.*)');
190
191 /**
192  * Pattern for file paths pointing to Google Drive files.
193  */
194 FileType.DRIVE_PATH_PATTERN =
195     new RegExp('^/drive/');
196
197 /**
198  * @param {string|Entry} file The url string or entry.
199  * @return {boolean} Whether this provider supports the url.
200  */
201 FileType.isOnDrive = function(file) {
202   return typeof file == 'string' ?
203       FileType.DRIVE_URL_PATTERN.test(file) :
204       FileType.DRIVE_PATH_PATTERN.test(file.fullPath);
205 };
206
207
208 /**
209  * Get the media type for a given file.
210  *
211  * @param {string|Entry} file Reference to the file.
212  * @return {string} The value of 'type' property from one of the elements in
213  *   FileType.types or undefined.
214  */
215 FileType.getMediaType = function(file) {
216   return FileType.getType(file).type;
217 };
218
219 /**
220  * @param {string|Entry} file Reference to the file.
221  * @return {boolean} True if audio file.
222  */
223 FileType.isAudio = function(file) {
224   return FileType.getMediaType(file) == 'audio';
225 };
226
227 /**
228  * @param {string|Entry} file Reference to the file.
229  * @return {boolean} True if image file.
230  */
231 FileType.isImage = function(file) {
232   return FileType.getMediaType(file) == 'image';
233 };
234
235 /**
236  * @param {string|Entry} file Reference to the file.
237  * @return {boolean} True if video file.
238  */
239 FileType.isVideo = function(file) {
240   return FileType.getMediaType(file) == 'video';
241 };
242
243
244 /**
245  * Files with more pixels won't have preview.
246  * @param {string|Entry} file Reference to the file.
247  * @return {boolean} True if image or video.
248  */
249 FileType.isImageOrVideo = function(file) {
250   var type = FileType.getMediaType(file);
251   return type == 'image' || type == 'video';
252 };
253
254 /**
255  * @param {string|Entry} file Reference to the file.
256  * @return {boolean} Returns true if the file is hosted.
257  */
258 FileType.isHosted = function(file) {
259   return FileType.getType(file).type === 'hosted';
260 };
261
262 /**
263  * @param {string|Entry} file Reference to the file.
264  * @return {boolean} Returns true if the file is not hidden, and we should
265  *     display it.
266  */
267 FileType.isVisible = function(file) {
268   if (typeof file == 'object') {
269     file = file.name;
270   }
271
272   var path = util.extractFilePath(file);
273   if (path) file = path;
274
275   file = file.split('/').pop();
276   return file.indexOf('.') != 0 && !(file in FileType.HIDDEN_NAMES);
277 };
278
279 /**
280  * File/directory names that we know are usually hidden.
281  */
282 FileType.HIDDEN_NAMES = {
283   'RECYCLED': true
284 };
285
286 /**
287  * @param {string|Entry} file Reference to the file.
288  * @return {string} Returns string that represents the file icon.
289  *                  It refers to a file 'images/filetype_' + icon + '.png'.
290  */
291 FileType.getIcon = function(file) {
292   var fileType = FileType.getType(file);
293   return fileType.icon || fileType.type || 'unknown';
294 };