Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Detect Graphics file in project and associate it with OS default application #15092

Merged
merged 12 commits into from
Mar 18, 2020
209 changes: 209 additions & 0 deletions src/extensions/default/OpenWithExternalApplication/GraphicsFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
/*
* Copyright (c) 2013 - present Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/

define(function (require, exports, module) {
"use strict";


var PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
Strings = brackets.getModule("strings"),
StringsUtils = brackets.getModule("utils/StringUtils"),
ProjectManager = brackets.getModule("project/ProjectManager"),
Dialogs = brackets.getModule("widgets/Dialogs"),
DefaultDialogs = brackets.getModule("widgets/DefaultDialogs"),
HealthLogger = brackets.getModule("utils/HealthLogger");


var _requestID = 0,
_initialized = false;

var _graphicsFileTypes = ["jpg", "jpeg", "png", "svg", "xd", "psd", "ai"];

var _nodeDomain;

function init(nodeDomain) {

if (_initialized) {
return;
}
_initialized = true;

_nodeDomain = nodeDomain;

_nodeDomain.on('checkFileTypesInFolderResponse', function (event, response) {
if (response.id !== _requestID) {
return;
}
_graphicsFilePresentInProject(response.present);
});

ProjectManager.on("projectOpen", function () {
_checkForGraphicsFileInPrjct();
});

_checkForGraphicsFileInPrjct();

}


function _checkForGraphicsFileInPrjct() {

if (PreferencesManager.getViewState("AssociateGraphicsFileDialogShown")) {
return;
}

_nodeDomain.exec("checkFileTypesInFolder", {
extensions: _graphicsFileTypes.join(),
folder: ProjectManager.getProjectRoot().fullPath,
reqId: ++_requestID
});

}

function _graphicsFilePresentInProject(isPresent) {

if (!isPresent) {
return;
}

Dialogs.showModalDialog(
DefaultDialogs.DIALOG_ID_INFO,
Strings.ASSOCIATE_GRAPHICS_FILE_TO_DEFAULT_APP_TITLE,
Strings.ASSOCIATE_GRAPHICS_FILE_TO_DEFAULT_APP_MSG,
[
{ className: Dialogs.DIALOG_BTN_CLASS_NORMAL, id: Dialogs.DIALOG_BTN_CANCEL,
text: Strings.CANCEL
},
{ className: Dialogs.DIALOG_BTN_CLASS_PRIMARY, id: Dialogs.DIALOG_BTN_OK,
text: Strings.OK
}
]
).done(function (id) {

if (id !== Dialogs.DIALOG_BTN_OK) {
HealthLogger.sendAnalyticsData(
"externalEditorsCancelled",
"usage",
"externalEditors",
"Cancelled",
""
);
return;
}
HealthLogger.sendAnalyticsData(
"LinkExternalEditors",
"usage",
"externalEditors",
"LinkExternalEditors",
""
);

brackets.app.getSystemDefaultApp(_graphicsFileTypes.join(), function (err, out) {

if (err) {
return;
}
var associateApp = out.split(','),
fileTypeToAppMap = {},
AppToFileTypeMap = {};

associateApp.forEach(function (item) {

var filetype = item.split('##')[0],
app = item.split('##')[1];

if (!filetype) {
return;
}

if (filetype === "xd") {
if (app.toLowerCase() !== "adobe xd" && app.toLowerCase() !== "adobe.cc.xd") {
return;
}

app = "Adobe XD";
}
fileTypeToAppMap[filetype] = app;

if (brackets.platform === "win" && app.toLowerCase().endsWith('.exe')) {
app = app.substring(app.lastIndexOf('\\') + 1, app.length - 4);
}
if (AppToFileTypeMap[app]) {
AppToFileTypeMap[app].push(filetype);
} else {
AppToFileTypeMap[app] = [filetype];
}
});

var prefs = PreferencesManager.get('externalApplications');

for (var key in fileTypeToAppMap) {
if (fileTypeToAppMap.hasOwnProperty(key)) {
if(key && !prefs[key]) {
prefs[key] = fileTypeToAppMap[key];
if(brackets.platform === "win" && !fileTypeToAppMap[key].toLowerCase().endsWith('.exe')) {
prefs[key] = "default";
}
HealthLogger.sendAnalyticsData(
"AddExternalEditorForFileType_" + key.toUpperCase(),
"usage",
"externalEditors",
"AddExternalEditorForFileType_" + key.toUpperCase(),
""
);
}
}
}

PreferencesManager.set('externalApplications', prefs);

var str = "";
for(var app in AppToFileTypeMap) {
str += AppToFileTypeMap[app].join() + "->" + app + "<br/>";
}

if(!str) {
return;
}

str+="<br/>";

Dialogs.showModalDialog(
DefaultDialogs.DIALOG_ID_INFO,
Strings.ASSOCIATE_GRAPHICS_FILE_TO_DEFAULT_APP_TITLE,
StringsUtils.format(Strings.ASSOCIATE_GRAPHICS_FILE_TO_DEFAULT_APP_CNF_MSG, str),
[
{ className: Dialogs.DIALOG_BTN_CLASS_PRIMARY, id: Dialogs.DIALOG_BTN_OK,
text: Strings.OK
}
]
);
});
});
PreferencesManager.setViewState("AssociateGraphicsFileDialogShown", true);

}

exports.init = init;

});
5 changes: 4 additions & 1 deletion src/extensions/default/OpenWithExternalApplication/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ define(function (require, exports, module) {
FileViewController = brackets.getModule("project/FileViewController"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
NodeDomain = brackets.getModule("utils/NodeDomain"),
FileUtils = brackets.getModule("file/FileUtils");
FileUtils = brackets.getModule("file/FileUtils"),
GraphicsFile = require("GraphicsFile");

/**
* @private
Expand Down Expand Up @@ -66,6 +67,8 @@ define(function (require, exports, module) {
FileViewController.on("openWithExternalApplication", _openWithExternalApplication);

AppInit.appReady(function () {

GraphicsFile.init(_nodeDomain);
extensionToExtApplicationMap = PreferencesManager.get("externalApplications");
FileUtils.addExtensionToExternalAppList(Object.keys(extensionToExtApplicationMap));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
/*jslint node: true */
"use strict";

var open = require("open");
var open = require("open"),
Glob = require("glob").Glob,
path = require('path');

var _domainManager;

Expand All @@ -39,6 +41,38 @@ function _openWithExternalApplication(params) {
open(params.path, application);
}

/**
* @private
*
* @param {Object} params Object to use
*/
function _checkFileTypesInFolder(params) {

var extList = params.extensions,
dirPath = path.normalize(params.folder),

pattern = dirPath + "/**/*.+(" + extList.replace(/,/g, "|") + ")";

var globMgr = new Glob(pattern, function (err, matches) {

var respObj = {
id: params.reqId,
present: matches.length > 0 ? true : false
};
_domainManager.emitEvent('OpenWithExternalApplication', 'checkFileTypesInFolderResponse', [respObj]);
});

globMgr.on("match", function() {
globMgr.abort();
var respObj = {
id: params.reqId,
present: true
};
_domainManager.emitEvent('OpenWithExternalApplication', 'checkFileTypesInFolderResponse', [respObj]);
});

}


/**
* Initializes the OpenWithExternalEditor domain with its commands.
Expand All @@ -50,6 +84,7 @@ function init(domainManager) {
if (!domainManager.hasDomain("OpenWithExternalApplication")) {
domainManager.registerDomain("OpenWithExternalApplication", {major: 0, minor: 1});
}

_domainManager.registerCommand(
"OpenWithExternalApplication",
"open",
Expand All @@ -63,6 +98,32 @@ function init(domainManager) {
}],
[]
);

_domainManager.registerCommand(
"OpenWithExternalApplication",
"checkFileTypesInFolder",
_checkFileTypesInFolder,
true,
"looks for File Types in a folder.",
[{
name: "params",
type: "object",
description: "Params Object having File Extensions and Folder Path."
}],
[]
);

_domainManager.registerEvent(
"OpenWithExternalApplication",
"checkFileTypesInFolderResponse",
[
{
name: "msgObj",
type: "object",
description: "json object containing message info to pass to brackets"
}
]
);
}

exports.init = init;
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "brackets-open-external_application",
"dependencies": {
"open": "0.0.5"
"open": "0.0.5",
"glob": "7.1.1"
}
}
8 changes: 7 additions & 1 deletion src/nls/root/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -906,5 +906,11 @@ define({
"REMOTE_DEBUGGING_PORT_INVALID" : "Cannot enable remote debugging on port {0}. Port numbers should be between {1} and {2}.",

//Associate File Type to External App
"DESCRIPTION_EXTERNAL_APPLICATION_ASSOCIATE" : "Add File type association to external App here"
"DESCRIPTION_EXTERNAL_APPLICATION_ASSOCIATE" : "Associate File type to external App settings. e.g { \"<file_type>\": \"<app_name>\" } app_name is OS dependant, for example \"google chrome\" on macOS and \"chrome\" on Windows. app_name can also be given as \"default\" for OS default application.",

"ASSOCIATE_GRAPHICS_FILE_TO_DEFAULT_APP_TITLE" : "Open Graphic Files in External Editors.",
"ASSOCIATE_GRAPHICS_FILE_TO_DEFAULT_APP_MSG" : "Your current folder has graphic file types which are not supported by Brackets.<br/>You can now associate specific file types with external editors. Once associated, you can open graphic files like .xd, .psd, .jpg, .png, .ai, .svg in their default applications by double clicking on the files in File Tree.<br/><br/>Please click on ‘Ok’ button to associate the graphic file types with their respective default applications.",
"ASSOCIATE_GRAPHICS_FILE_TO_DEFAULT_APP_CNF_MSG" : "Following file types have been successfully associated with default applications.<br/>{0} You can further add new file type associations or customize in brackets.json by going to “Debug->Open Preferences File” menu."


});