diff --git a/src/filesystem/impls/filer/ArchiveUtils.js b/src/filesystem/impls/filer/ArchiveUtils.js index eddc72c2ed5..d0ac49f6c08 100644 --- a/src/filesystem/impls/filer/ArchiveUtils.js +++ b/src/filesystem/impls/filer/ArchiveUtils.js @@ -55,7 +55,7 @@ define(function (require, exports, module) { return false; } - function _refreshFilesystem(callback) { + function _refreshFileTree(callback) { // Update the file tree to show the new files CommandManager.execute(Commands.FILE_REFRESH).always(callback); } @@ -108,6 +108,10 @@ define(function (require, exports, module) { function decompress(path, callback) { var basedir = Path.dirname(path.absPath); + function writeFile() { + FilerUtils.writeFileAsBinary(path.absPath, path.data, callback); + } + if(path.isDirectory) { fs.mkdirp(path.absPath, callback); } else { @@ -124,10 +128,10 @@ define(function (require, exports, module) { return callback(err); } - FilerUtils.writeFileAsBinary(path.absPath, path.data, callback); + writeFile(); }); } else { - FilerUtils.writeFileAsBinary(path.absPath, path.data, callback); + writeFile(); } }); } @@ -138,7 +142,7 @@ define(function (require, exports, module) { return callback(err); } - _refreshFilesystem(function(err) { + _refreshFileTree(function(err) { if(err) { return callback(err); } @@ -147,7 +151,8 @@ define(function (require, exports, module) { DefaultDialogs.DIALOG_ID_INFO, Strings.DND_SUCCESS_UNZIP_TITLE ).getPromise().then(function() { - callback(null); + // We don't bother sending a list of files to open for archives. + callback(null, []); }, callback); }); }); @@ -272,7 +277,7 @@ define(function (require, exports, module) { untarWorker.terminate(); untarWorker = null; - _refreshFilesystem(function(err) { + _refreshFileTree(function(err) { if(err) { return callback(err); } @@ -281,7 +286,8 @@ define(function (require, exports, module) { DefaultDialogs.DIALOG_ID_INFO, Strings.DND_SUCCESS_UNTAR_TITLE ).getPromise().then(function() { - callback(null); + // We don't bother sending a list of files to open for archives. + callback(null, []); }, callback); }); } diff --git a/src/filesystem/impls/filer/lib/LegacyFileImport.js b/src/filesystem/impls/filer/lib/LegacyFileImport.js index 6edc679ac43..595a6ba9ba3 100644 --- a/src/filesystem/impls/filer/lib/LegacyFileImport.js +++ b/src/filesystem/impls/filer/lib/LegacyFileImport.js @@ -33,8 +33,9 @@ define(function (require, exports, module) { FileSystem = require("filesystem/FileSystem"), FileUtils = require("file/FileUtils"), Strings = require("strings"), - Filer = require("filesystem/impls/filer/BracketsFiler"), - Path = Filer.Path, + FilerUtils = require("filesystem/impls/filer/FilerUtils"), + Buffer = FilerUtils.Buffer, + Path = FilerUtils.Path, Content = require("filesystem/impls/filer/lib/content"), ArchiveUtils = require("filesystem/impls/filer/ArchiveUtils"); @@ -43,50 +44,43 @@ define(function (require, exports, module) { // We want event.dataTransfer.files for legacy browsers. LegacyFileImport.prototype.import = function(source, parentPath, callback) { var files = source instanceof DataTransfer ? source.files : source; - var pathList = []; + var pathsToOpen = []; var errorList = []; if (!(files && files.length)) { - return callback(); + return callback(null, []); } - function shouldOpenFile(filename, encoding) { - return Content.isImage(Path.extname(filename)) || encoding === "utf8"; - } - - function handleRegularFile(deferred, file, filename, buffer, encoding) { + function handleRegularFile(deferred, filename, buffer) { // Don't write thing like .DS_Store, thumbs.db, etc. if(ArchiveUtils.skipFile(filename)) { deferred.resolve(); return; } - file.write(buffer, {encoding: encoding}, function(err) { - if (err) { - errorList.push({path: filename, error: "unable to write file: " + err.message || ""}); + FilerUtils + .writeFileAsBinary(filename, buffer) + .done(function() { + pathsToOpen.push(filename); + deferred.resolve(); + }) + .fail(function(err) { + errorList.push({path: filename, error: err.message || "unable to write file" }); deferred.reject(err); - return; - } - - // See if this file is worth trying to open in the editor or not - if(shouldOpenFile(filename, encoding)) { - pathList.push(filename); - } - - deferred.resolve(); - }); + }); } function handleZipFile(deferred, file, filename, buffer, encoding) { var basename = Path.basename(filename); - ArchiveUtils.unzip(buffer, { root: parentPath }, function(err) { + ArchiveUtils.unzip(buffer, { root: parentPath }, function(err, unzippedPaths) { if (err) { errorList.push({path: filename, error: Strings.DND_ERROR_UNZIP}); deferred.reject(err); return; } + pathsToOpen = pathsToOpen.concat(unzippedPaths); deferred.resolve(); }); } @@ -94,13 +88,14 @@ define(function (require, exports, module) { function handleTarFile(deferred, file, filename, buffer, encoding) { var basename = Path.basename(filename); - ArchiveUtils.untar(buffer, { root: parentPath }, function(err) { + ArchiveUtils.untar(buffer, { root: parentPath }, function(err, untarredPaths) { if (err) { errorList.push({path: filename, error: Strings.DND_ERROR_UNTAR}); deferred.reject(err); return; } + pathsToOpen = pathsToOpen.concat(untarredPaths); deferred.resolve(); }); } @@ -156,26 +151,16 @@ define(function (require, exports, module) { delete reader.onload; var filename = Path.join(parentPath, item.name); - var file = FileSystem.getFileForPath(filename); - var ext = Path.extname(filename).toLowerCase(); - - // Create a Filer Buffer, and determine the proper encoding. We - // use the extension, and also the OS provided mime type for clues. - var buffer = new Filer.Buffer(e.target.result); - var utf8FromExt = Content.isUTF8Encoded(ext); - var utf8FromOS = Content.isTextType(item.type); - var encoding = utf8FromExt || utf8FromOS ? 'utf8' : null; - if(encoding === 'utf8') { - buffer = buffer.toString(); - } + var ext = FilerUtils.normalizeExtension(Path.extname(filename)); + var buffer = new Buffer(e.target.result); // Special-case .zip files, so we can offer to extract the contents if(ext === ".zip") { - handleZipFile(deferred, file, filename, buffer, encoding); + handleZipFile(deferred, filename, buffer); } else if(ext === ".tar") { - handleTarFile(deferred, file, filename, buffer, encoding); + handleTarFile(deferred, filename, buffer); } else { - handleRegularFile(deferred, file, filename, buffer, encoding); + handleRegularFile(deferred, filename, buffer); } }; @@ -193,7 +178,7 @@ define(function (require, exports, module) { Async.doSequentially(prepareDropPaths(files), maybeImportFile, false) .done(function() { - callback(null, pathList); + callback(null, pathsToOpen); }) .fail(function() { callback(errorList); diff --git a/src/filesystem/impls/filer/lib/WebKitFileImport.js b/src/filesystem/impls/filer/lib/WebKitFileImport.js index 3b0e25af7c0..b7d2fb64fa6 100644 --- a/src/filesystem/impls/filer/lib/WebKitFileImport.js +++ b/src/filesystem/impls/filer/lib/WebKitFileImport.js @@ -32,9 +32,10 @@ define(function (require, exports, module) { Async = require("utils/Async"), FileSystem = require("filesystem/FileSystem"), FileUtils = require("file/FileUtils"), + FilerUtils = require("filesystem/impls/filer/FilerUtils"), + Path = FilerUtils.Path, + Buffer = FilerUtils.Buffer, Strings = require("strings"), - Filer = require("filesystem/impls/filer/BracketsFiler"), - Path = Filer.Path, Content = require("filesystem/impls/filer/lib/content"), ArchiveUtils = require("filesystem/impls/filer/ArchiveUtils"); @@ -43,17 +44,13 @@ define(function (require, exports, module) { // We want event.dataTransfer.items for WebKit style browsers WebKitFileImport.prototype.import = function(source, parentPath, callback) { var items = source instanceof DataTransfer ? source.items : source; - var pathList = []; + var pathsToOpen = []; var errorList = []; var started = 0; var completed = 0; if (!(items && items.length)) { - return callback(); - } - - function shouldOpenFile(filename, encoding) { - return Content.isImage(Path.extname(filename)) || encoding === "utf8"; + return callback(null, []); } function checkDone() { @@ -61,7 +58,7 @@ define(function (require, exports, module) { if(errorList.length) { callback(errorList); } else { - callback(null, pathList); + callback(null, pathsToOpen); } } } @@ -123,44 +120,42 @@ define(function (require, exports, module) { }); } - function handleRegularFile(deferred, file, filename, buffer, encoding) { - file.write(buffer, {encoding: encoding}, function(err) { - if (err) { + function handleRegularFile(deferred, filename, buffer) { + FilerUtils + .writeFileAsBinary(filename, buffer) + .done(function() { + pathsToOpen.push(filename); + onSuccess(deferred); + }) + .fail(function(err) { onError(deferred, filename, err); - return; - } - - // See if this file is worth trying to open in the editor or not - if(shouldOpenFile(filename, encoding)) { - pathList.push(filename); - } - - onSuccess(deferred); - }); + }); } - function handleZipFile(deferred, file, filename, buffer, encoding) { + function handleZipFile(deferred, filename, buffer) { var basename = Path.basename(filename); - ArchiveUtils.unzip(buffer, { root: parentPath }, function(err) { + ArchiveUtils.unzip(buffer, { root: parentPath }, function(err, unzippedPaths) { if (err) { onError(deferred, filename, new Error(Strings.DND_ERROR_UNZIP)); return; } + pathsToOpen = pathsToOpen.concat(unzippedPaths); onSuccess(deferred); }); } - function handleTarFile(deferred, file, filename, buffer, encoding) { + function handleTarFile(deferred, filename, buffer) { var basename = Path.basename(filename); - ArchiveUtils.untar(buffer, { root: parentPath }, function(err) { + ArchiveUtils.untar(buffer, { root: parentPath }, function(err, untarredPaths) { if (err) { onError(deferred, filename, new Error(Strings.DND_ERROR_UNTAR)); return; } + pathsToOpen = pathsToOpen.concat(untarredPaths); onSuccess(deferred); }); } @@ -184,16 +179,8 @@ define(function (require, exports, module) { delete reader.onload; var filename = Path.join(parentPath, entry.name); - var file = FileSystem.getFileForPath(filename); - var ext = Path.extname(filename).toLowerCase(); - - // Create a Filer Buffer, and determine the proper encoding. - var buffer = new Filer.Buffer(e.target.result); - var utf8FromExt = Content.isUTF8Encoded(ext); - var encoding = utf8FromExt ? "utf8" : null; - if(utf8FromExt) { - buffer = buffer.toString(); - } + var ext = FilerUtils.normalizeExtension(Path.extname(filename)); + var buffer = new Buffer(e.target.result); // Don't bother writing things like .DS_Store, thumbs.db, etc. if(ArchiveUtils.skipFile(filename)) { @@ -210,11 +197,11 @@ define(function (require, exports, module) { // Special-case .zip files, so we can offer to extract the contents if(ext === ".zip") { - handleZipFile(deferred, file, filename, buffer, encoding); + handleZipFile(deferred, filename, buffer); } else if(ext === ".tar") { - handleTarFile(deferred, file, filename, buffer, encoding); + handleTarFile(deferred, filename, buffer); } else { - handleRegularFile(deferred, file, filename, buffer, encoding); + handleRegularFile(deferred, filename, buffer); } }; diff --git a/src/filesystem/impls/filer/lib/content.js b/src/filesystem/impls/filer/lib/content.js index 6eb86c168d0..06aa0f13892 100644 --- a/src/filesystem/impls/filer/lib/content.js +++ b/src/filesystem/impls/filer/lib/content.js @@ -123,6 +123,20 @@ define(function (require, exports, module) { } module.exports = { + mimeFromExt: function(ext) { + return _lookupMimeType(ext); + }, + + // Whether or not we can provide a useful editor for this, and should try to open. + isEditable: function(ext) { + return this.isUTF8Encoded(ext) || + this.isImage(ext) || + this.isAudio(ext) || + this.isVideo(ext) || + this.isFont(ext) || + this.isPDF(ext); + }, + isImage: function(ext) { var info = new FileInfo(ext); return info.type === "image"; @@ -193,9 +207,6 @@ define(function (require, exports, module) { } }, - mimeFromExt: function(ext) { - return _lookupMimeType(ext); - }, // Whether or not this is a text/* mime type isTextType: function(mime) { diff --git a/src/project/FileViewController.js b/src/project/FileViewController.js index 1cc852a7781..ece4f353b6d 100644 --- a/src/project/FileViewController.js +++ b/src/project/FileViewController.js @@ -76,7 +76,7 @@ define(function (require, exports, module) { */ EventDispatcher.on_duringInit(MainViewManager, "workingSetAdd", function (event, addedFile) { // XXXBramble: always keep focus on the Project Manager vs. Working Set View - _fileSelectionFocus = WORKING_SET_VIEW; + _fileSelectionFocus = PROJECT_MANAGER; exports.trigger("documentSelectionFocusChange"); }); /** @@ -87,11 +87,11 @@ define(function (require, exports, module) { if (!_curDocChangedDueToMe) { // The the cause of the doc change was not openAndSelectDocument, so pick the best fileSelectionFocus perfTimerName = PerfUtils.markStart("FileViewController._oncurrentFileChange():\t" + (file ? (file.fullPath) : "(no open file)")); - - // XXXBramble: always keep focus on the Project Manager vs. Working Set View - _fileSelectionFocus = PROJECT_MANAGER; } + // XXXBramble: always keep focus on the Project Manager vs. Working Set View + _fileSelectionFocus = PROJECT_MANAGER; + exports.trigger("documentSelectionFocusChange"); if (!_curDocChangedDueToMe) { @@ -125,13 +125,9 @@ define(function (require, exports, module) { console.error("Bad parameter passed to FileViewController.setFileViewFocus"); return; } - // XXXBramble: always keep focus on the Project Manager vs. Working Set View - fileSelectionFocus = PROJECT_MANAGER; - - if (_fileSelectionFocus !== fileSelectionFocus) { - _fileSelectionFocus = fileSelectionFocus; - exports.trigger("fileViewFocusChange"); - } + // XXXBramble: always keep focus on the Project Manager vs. Working Set View + _fileSelectionFocus = PROJECT_MANAGER; + exports.trigger("fileViewFocusChange"); } /** @@ -169,8 +165,8 @@ define(function (require, exports, module) { // and checked in the currentFileChange handler _curDocChangedDueToMe = true; - _fileSelectionFocus = fileSelectionFocus; - + // XXXBramble: always keep focus on the Project Manager vs. Working Set View + _fileSelectionFocus = PROJECT_MANAGER; paneId = (paneId || _getDerivedPaneContext() || MainViewManager.ACTIVE_PANE); diff --git a/src/utils/DragAndDrop.js b/src/utils/DragAndDrop.js index 317f1afab22..832ecb1fb6b 100644 --- a/src/utils/DragAndDrop.js +++ b/src/utils/DragAndDrop.js @@ -332,7 +332,7 @@ define(function (require, exports, module) { }); } - FileImport.import(source, _dropPathHint, function(err, paths) { + FileImport.import(source, _dropPathHint, function(err, pathsToOpen) { // Reset drop path, until we get an explicit one set in future. _dropPathHint = null; @@ -342,9 +342,8 @@ define(function (require, exports, module) { return; } - // Don't crash in legacy browsers if we rejected all paths (e.g., folder(s)). - paths = paths || []; - openDroppedFiles(paths); + pathsToOpen = pathsToOpen || []; + openDroppedFiles(pathsToOpen); callback(); });