OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Setting the src of an img to an empty string can crash the browser, so we | 5 // Setting the src of an img to an empty string can crash the browser, so we |
6 // use an empty 1x1 gif instead. | 6 // use an empty 1x1 gif instead. |
7 | 7 |
8 /** | 8 /** |
9 * FileManager constructor. | 9 * FileManager constructor. |
10 * | 10 * |
(...skipping 3967 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3978 } | 3978 } |
3979 } | 3979 } |
3980 callback(fileUrls); | 3980 callback(fileUrls); |
3981 }); | 3981 }); |
3982 } else { | 3982 } else { |
3983 callback(fileUrls); | 3983 callback(fileUrls); |
3984 } | 3984 } |
3985 }, | 3985 }, |
3986 | 3986 |
3987 /** | 3987 /** |
3988 * Selects a file. | 3988 * Closes this modal dialog with some files selected. |
3989 * | 3989 * TODO(jamescook): Make unload handler work automatically, crbug.com/104811 |
3990 * @param {string} fileUrl The filename as a URL. | 3990 * @param {Object} selection Contains urls, filterIndex and multiple fields. |
3991 * @param {number} filterIndex The integer file filter index. | |
3992 */ | 3991 */ |
3993 FileManager.prototype.selectFile_ = function(fileUrl, filterIndex) { | 3992 FileManager.prototype.callSelectFilesApiAndClose_ = function(selection) { |
3994 this.resolveSelectResults_( | 3993 if (selection.multiple) { |
3995 [fileUrl], | 3994 chrome.fileBrowserPrivate.selectFiles(selection.urls); |
3996 function(resolvedUrls) { | 3995 } else { |
3997 // Call doSelectFiles_ on a timeout, as it's unsafe to | 3996 chrome.fileBrowserPrivate.selectFile( |
3998 // close a window from a callback. | 3997 selection.urls[0], selection.filterIndex); |
3999 setTimeout( | 3998 } |
4000 this.doSelectFile_.bind(this, resolvedUrls[0], filterIndex), 0); | |
4001 }.bind(this)); | |
4002 }; | |
4003 | |
4004 /** | |
4005 * Selects a file. Closes the window. | |
4006 * TODO(jamescook): Make unload handler work automatically, crbug.com/104811 | |
4007 * | |
4008 * @param {string} fileUrl The filename as a URL. | |
4009 * @param {number} filterIndex The integer file filter index. | |
4010 */ | |
4011 FileManager.prototype.doSelectFile_ = function(fileUrl, filterIndex) { | |
4012 chrome.fileBrowserPrivate.selectFile(fileUrl, filterIndex); | |
4013 this.onUnload_(); | |
4014 window.close(); | |
4015 }; | |
4016 | |
4017 | |
4018 /** | |
4019 * Selects a file. Starts getting gdata files if needed. | |
4020 * | |
4021 * @param {Array.<string>} fileUrls Array of filename URLs. | |
4022 */ | |
4023 FileManager.prototype.selectFiles_ = function(fileUrls) { | |
4024 this.resolveSelectResults_( | |
4025 fileUrls, | |
4026 function(resolvedUrls) { | |
4027 // Call doSelectFiles_ on a timeout, as it's unsafe to | |
4028 // close a window from a callback. | |
4029 setTimeout(this.doSelectFiles_.bind(this, resolvedUrls), 0); | |
4030 }.bind(this)); | |
4031 }; | |
4032 | |
4033 /** | |
4034 * Selects multiple files. Closes the window. | |
4035 * TODO(jamescook): Make unload handler work automatically, crbug.com/104811 | |
4036 * | |
4037 * @param {Array.<string>} fileUrls Array of filename URLs. | |
4038 */ | |
4039 FileManager.prototype.doSelectFiles_ = function(fileUrls) { | |
4040 chrome.fileBrowserPrivate.selectFiles(fileUrls); | |
4041 this.onUnload_(); | 3999 this.onUnload_(); |
4042 window.close(); | 4000 window.close(); |
4043 }; | 4001 }; |
4044 | 4002 |
4045 /** | 4003 /** |
4004 * Tries to close this modal dialog with some files selected. | |
4005 * Performs preprocessing if needed (e.g. for GData). | |
4006 * @param {Object} selection Contains urls, filterIndex and multiple fields. | |
4007 */ | |
4008 FileManager.prototype.selectFilesAndClose_ = function(selection) { | |
4009 if (!this.isOnGData()) { | |
4010 this.callSelectFilesApiAndClose_(selection); | |
Vladislav Kaznacheev
2012/03/23 07:20:52
We used to make this call from setTimeout. I trust
dgozman
2012/03/23 12:44:05
Good catch. Missed this here.
| |
4011 return; | |
4012 } | |
4013 | |
4014 var shade = this.document_.createElement('div'); | |
4015 shade.className = 'shade'; | |
4016 var footer = this.document_.querySelector('.dialog-footer'); | |
4017 var progress = footer.querySelector('.progress-track'); | |
4018 progress.style.width = '0%'; | |
4019 var cancelled = false; | |
4020 | |
4021 var onFileTransfersUpdated = function(statusList) { | |
4022 var total = 0; | |
4023 var done = 0; | |
4024 var known = 0; // The number of files we have information about. | |
4025 for (var index = 0; index < statusList.length; index++) { | |
4026 var status = statusList[index]; | |
4027 var escaped = encodeURI(status.fileUrl); | |
4028 if (selection.urls.indexOf(escaped) == -1) continue; | |
4029 if (status.total == -1) continue; | |
4030 known++; | |
4031 total += status.total; | |
4032 done += status.processed; | |
4033 } | |
4034 | |
4035 var percent = total == 0 ? 0 : done / total; | |
4036 // For files we don't have information about, assume the progress is zero. | |
4037 percent = percent * known / selection.urls.length * 100; | |
4038 progress.style.width = percent + '%'; | |
4039 }.bind(this); | |
4040 | |
4041 var setup = function() { | |
4042 this.document_.querySelector('.dialog-container').appendChild(shade); | |
4043 setTimeout(function() { shade.setAttribute('fadein', 'fadein') }, 100); | |
4044 footer.setAttribute('progress', 'progress'); | |
4045 this.cancelButton_.removeEventListener('click', this.onCancelBound_); | |
4046 this.cancelButton_.addEventListener('click', onCancel); | |
4047 chrome.fileBrowserPrivate.onFileTransfersUpdated.addListener( | |
4048 onFileTransfersUpdated); | |
4049 }.bind(this); | |
4050 | |
4051 var cleanup = function() { | |
4052 shade.parentNode.removeChild(shade); | |
4053 footer.removeAttribute('progress'); | |
4054 this.cancelButton_.removeEventListener('click', onCancel); | |
4055 this.cancelButton_.addEventListener('click', this.onCancelBound_); | |
4056 chrome.fileBrowserPrivate.onFileTransfersUpdated.removeListener( | |
Vladislav Kaznacheev
2012/03/23 07:20:52
I am paranoid today:) Are you absolutely sure that
dgozman
2012/03/23 12:44:05
Hmm.. But we do not remove listeners for other eve
| |
4057 onFileTransfersUpdated); | |
4058 }.bind(this); | |
4059 | |
4060 var onCancel = function() { | |
4061 cancelled = true; | |
4062 chrome.fileBrowserPrivate.cancelFileTransfers( | |
4063 selection.urls, function(responses) {}); | |
4064 cleanup(); | |
4065 }.bind(this); | |
4066 | |
4067 var onResolved = function(resolvedUrls) { | |
4068 if (cancelled) return; | |
4069 cleanup(); | |
4070 selection.urls = resolvedUrls; | |
4071 // Call next method on a timeout, as it's unsafe to | |
4072 // close a window from a callback. | |
4073 setTimeout(this.callSelectFilesApiAndClose_.bind(this, selection), 0); | |
4074 }.bind(this); | |
4075 | |
4076 setup(); | |
4077 this.resolveSelectResults_(selection.urls, onResolved); | |
4078 }; | |
4079 | |
4080 /** | |
4046 * Handle a click of the ok button. | 4081 * Handle a click of the ok button. |
4047 * | 4082 * |
4048 * The ok button has different UI labels depending on the type of dialog, but | 4083 * The ok button has different UI labels depending on the type of dialog, but |
4049 * in code it's always referred to as 'ok'. | 4084 * in code it's always referred to as 'ok'. |
4050 * | 4085 * |
4051 * @param {Event} event The click event. | 4086 * @param {Event} event The click event. |
4052 */ | 4087 */ |
4053 FileManager.prototype.onOk_ = function(event) { | 4088 FileManager.prototype.onOk_ = function(event) { |
4054 var currentDirUrl = this.getCurrentDirectoryURL(); | 4089 var currentDirUrl = this.getCurrentDirectoryURL(); |
4055 | 4090 |
4056 if (currentDirUrl.charAt(currentDirUrl.length - 1) != '/') | 4091 if (currentDirUrl.charAt(currentDirUrl.length - 1) != '/') |
4057 currentDirUrl += '/'; | 4092 currentDirUrl += '/'; |
4058 | 4093 |
4059 var self = this; | 4094 var self = this; |
4060 if (this.dialogType_ == FileManager.DialogType.SELECT_SAVEAS_FILE) { | 4095 if (this.dialogType_ == FileManager.DialogType.SELECT_SAVEAS_FILE) { |
4061 // Save-as doesn't require a valid selection from the list, since | 4096 // Save-as doesn't require a valid selection from the list, since |
4062 // we're going to take the filename from the text input. | 4097 // we're going to take the filename from the text input. |
4063 var filename = this.filenameInput_.value; | 4098 var filename = this.filenameInput_.value; |
4064 if (!filename) | 4099 if (!filename) |
4065 throw new Error('Missing filename!'); | 4100 throw new Error('Missing filename!'); |
4066 if (!this.validateFileName_(filename)) | 4101 if (!this.validateFileName_(filename)) |
4067 return; | 4102 return; |
4068 | 4103 |
4104 var singleSelection = { | |
4105 urls: [currentDirUrl + encodeURIComponent(filename)], | |
4106 multiple: false, | |
4107 filterIndex: self.getSelectedFilterIndex_(filename) | |
4108 }; | |
4109 | |
4069 function resolveCallback(victim) { | 4110 function resolveCallback(victim) { |
4070 if (victim instanceof FileError) { | 4111 if (victim instanceof FileError) { |
4071 // File does not exist. Closes the window and does not return. | 4112 // File does not exist. |
4072 self.selectFile_( | 4113 self.selectFilesAndClose_(singleSelection); |
4073 currentDirUrl + encodeURIComponent(filename), | 4114 return; |
4074 self.getSelectedFilterIndex_(filename)); | |
4075 } | 4115 } |
4076 | 4116 |
4077 if (victim.isDirectory) { | 4117 if (victim.isDirectory) { |
4078 // Do not allow to overwrite directory. | 4118 // Do not allow to overwrite directory. |
4079 self.alert.show(strf('DIRECTORY_ALREADY_EXISTS', filename)); | 4119 self.alert.show(strf('DIRECTORY_ALREADY_EXISTS', filename)); |
4080 } else { | 4120 } else { |
4081 self.confirm.show(strf('CONFIRM_OVERWRITE_FILE', filename), | 4121 self.confirm.show(strf('CONFIRM_OVERWRITE_FILE', filename), |
4082 function() { | 4122 function() { |
4083 // User selected Ok from the confirm dialog. | 4123 // User selected Ok from the confirm dialog. |
4084 self.selectFile_( | 4124 self.selectFilesAndClose_(singleSelection); |
4085 currentDirUrl + encodeURIComponent(filename), | |
4086 self.getSelectedFilterIndex_(filename)); | |
4087 }); | 4125 }); |
4088 } | 4126 } |
4089 return; | |
4090 } | 4127 } |
4091 | 4128 |
4092 this.resolvePath(this.getCurrentDirectory() + '/' + filename, | 4129 this.resolvePath(this.getCurrentDirectory() + '/' + filename, |
4093 resolveCallback, resolveCallback); | 4130 resolveCallback, resolveCallback); |
4094 return; | 4131 return; |
4095 } | 4132 } |
4096 | 4133 |
4097 var files = []; | 4134 var files = []; |
4098 var selectedIndexes = this.currentList_.selectionModel.selectedIndexes; | 4135 var selectedIndexes = this.currentList_.selectionModel.selectedIndexes; |
4099 | 4136 |
4100 // All other dialog types require at least one selected list item. | 4137 // All other dialog types require at least one selected list item. |
4101 // The logic to control whether or not the ok button is enabled should | 4138 // The logic to control whether or not the ok button is enabled should |
4102 // prevent us from ever getting here, but we sanity check to be sure. | 4139 // prevent us from ever getting here, but we sanity check to be sure. |
4103 if (!selectedIndexes.length) | 4140 if (!selectedIndexes.length) |
4104 throw new Error('Nothing selected!'); | 4141 throw new Error('Nothing selected!'); |
4105 | 4142 |
4106 var dm = this.directoryModel_.fileList; | 4143 var dm = this.directoryModel_.fileList; |
4107 for (var i = 0; i < selectedIndexes.length; i++) { | 4144 for (var i = 0; i < selectedIndexes.length; i++) { |
4108 var entry = dm.item(selectedIndexes[i]); | 4145 var entry = dm.item(selectedIndexes[i]); |
4109 if (!entry) { | 4146 if (!entry) { |
4110 console.log('Error locating selected file at index: ' + i); | 4147 console.log('Error locating selected file at index: ' + i); |
4111 continue; | 4148 continue; |
4112 } | 4149 } |
4113 | 4150 |
4114 files.push(currentDirUrl + encodeURIComponent(entry.name)); | 4151 files.push(currentDirUrl + encodeURIComponent(entry.name)); |
4115 } | 4152 } |
4116 | 4153 |
4117 // Multi-file selection has no other restrictions. | 4154 // Multi-file selection has no other restrictions. |
4118 if (this.dialogType_ == FileManager.DialogType.SELECT_OPEN_MULTI_FILE) { | 4155 if (this.dialogType_ == FileManager.DialogType.SELECT_OPEN_MULTI_FILE) { |
4119 // Closes the window and does not return. | 4156 var multipleSelection = { |
4120 this.selectFiles_(files); | 4157 urls: files, |
4158 multiple: true | |
4159 }; | |
4160 this.selectFilesAndClose_(multipleSelection); | |
4121 return; | 4161 return; |
4122 } | 4162 } |
4123 | 4163 |
4124 // Everything else must have exactly one. | 4164 // Everything else must have exactly one. |
4125 if (files.length > 1) | 4165 if (files.length > 1) |
4126 throw new Error('Too many files selected!'); | 4166 throw new Error('Too many files selected!'); |
4127 | 4167 |
4128 var selectedEntry = dm.item(selectedIndexes[0]); | 4168 var selectedEntry = dm.item(selectedIndexes[0]); |
4129 | 4169 |
4130 if (this.dialogType_ == FileManager.DialogType.SELECT_FOLDER) { | 4170 if (this.dialogType_ == FileManager.DialogType.SELECT_FOLDER) { |
4131 if (!selectedEntry.isDirectory) | 4171 if (!selectedEntry.isDirectory) |
4132 throw new Error('Selected entry is not a folder!'); | 4172 throw new Error('Selected entry is not a folder!'); |
4133 } else if (this.dialogType_ == FileManager.DialogType.SELECT_OPEN_FILE) { | 4173 } else if (this.dialogType_ == FileManager.DialogType.SELECT_OPEN_FILE) { |
4134 if (!selectedEntry.isFile) | 4174 if (!selectedEntry.isFile) |
4135 throw new Error('Selected entry is not a file!'); | 4175 throw new Error('Selected entry is not a file!'); |
4136 } | 4176 } |
4137 | 4177 |
4138 // Closes the window and does not return. | 4178 var singleSelection = { |
4139 this.selectFile_(files[0], this.getSelectedFilterIndex_(files[0])); | 4179 urls: [files[0]], |
4180 multiple: false, | |
4181 filterIndex: this.getSelectedFilterIndex_(files[0]) | |
4182 }; | |
4183 this.selectFilesAndClose_(singleSelection); | |
4140 }; | 4184 }; |
4141 | 4185 |
4142 /** | 4186 /** |
4143 * Verifies the user entered name for file or folder to be created or | 4187 * Verifies the user entered name for file or folder to be created or |
4144 * renamed to. Name restrictions must correspond to File API restrictions | 4188 * renamed to. Name restrictions must correspond to File API restrictions |
4145 * (see DOMFilePath::isValidPath). Curernt WebKit implementation is | 4189 * (see DOMFilePath::isValidPath). Curernt WebKit implementation is |
4146 * out of date (spec is | 4190 * out of date (spec is |
4147 * http://dev.w3.org/2009/dap/file-system/file-dir-sys.html, 8.3) and going to | 4191 * http://dev.w3.org/2009/dap/file-system/file-dir-sys.html, 8.3) and going to |
4148 * be fixed. Shows message box if the name is invalid. | 4192 * be fixed. Shows message box if the name is invalid. |
4149 * | 4193 * |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4222 }); | 4266 }); |
4223 }, onError); | 4267 }, onError); |
4224 | 4268 |
4225 function onError(err) { | 4269 function onError(err) { |
4226 console.log('Error while checking free space: ' + err); | 4270 console.log('Error while checking free space: ' + err); |
4227 setTimeout(doCheck, 1000 * 60); | 4271 setTimeout(doCheck, 1000 * 60); |
4228 } | 4272 } |
4229 } | 4273 } |
4230 } | 4274 } |
4231 })(); | 4275 })(); |
OLD | NEW |