|
| 1 | +/* |
| 2 | + * Copyright (c) 2013 - present Adobe Systems Incorporated. All rights reserved. |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | + * copy of this software and associated documentation files (the "Software"), |
| 6 | + * to deal in the Software without restriction, including without limitation |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | + * Software is furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | + * DEALINGS IN THE SOFTWARE. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +define(function (require, exports, module) { |
| 25 | + "use strict"; |
| 26 | + |
| 27 | + |
| 28 | + var PreferencesManager = brackets.getModule("preferences/PreferencesManager"), |
| 29 | + Strings = brackets.getModule("strings"), |
| 30 | + StringsUtils = brackets.getModule("utils/StringUtils"), |
| 31 | + ProjectManager = brackets.getModule("project/ProjectManager"), |
| 32 | + Dialogs = brackets.getModule("widgets/Dialogs"), |
| 33 | + DefaultDialogs = brackets.getModule("widgets/DefaultDialogs"), |
| 34 | + HealthLogger = brackets.getModule("utils/HealthLogger"); |
| 35 | + |
| 36 | + |
| 37 | + var _requestID = 0, |
| 38 | + _initialized = false; |
| 39 | + |
| 40 | + var _graphicsFileTypes = ["jpg", "jpeg", "png", "svg", "xd", "psd", "ai"]; |
| 41 | + |
| 42 | + var _nodeDomain; |
| 43 | + |
| 44 | + function init(nodeDomain) { |
| 45 | + |
| 46 | + if (_initialized) { |
| 47 | + return; |
| 48 | + } |
| 49 | + _initialized = true; |
| 50 | + |
| 51 | + _nodeDomain = nodeDomain; |
| 52 | + |
| 53 | + _nodeDomain.on('checkFileTypesInFolderResponse', function (event, response) { |
| 54 | + if (response.id !== _requestID) { |
| 55 | + return; |
| 56 | + } |
| 57 | + _graphicsFilePresentInProject(response.present); |
| 58 | + }); |
| 59 | + |
| 60 | + ProjectManager.on("projectOpen", function () { |
| 61 | + _checkForGraphicsFileInPrjct(); |
| 62 | + }); |
| 63 | + |
| 64 | + _checkForGraphicsFileInPrjct(); |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + function _checkForGraphicsFileInPrjct() { |
| 70 | + |
| 71 | + if (PreferencesManager.getViewState("AssociateGraphicsFileDialogShown")) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + _nodeDomain.exec("checkFileTypesInFolder", { |
| 76 | + extensions: _graphicsFileTypes.join(), |
| 77 | + folder: ProjectManager.getProjectRoot().fullPath, |
| 78 | + reqId: ++_requestID |
| 79 | + }); |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + function _graphicsFilePresentInProject(isPresent) { |
| 84 | + |
| 85 | + if (!isPresent) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + Dialogs.showModalDialog( |
| 90 | + DefaultDialogs.DIALOG_ID_INFO, |
| 91 | + Strings.ASSOCIATE_GRAPHICS_FILE_TO_DEFAULT_APP_TITLE, |
| 92 | + Strings.ASSOCIATE_GRAPHICS_FILE_TO_DEFAULT_APP_MSG, |
| 93 | + [ |
| 94 | + { className: Dialogs.DIALOG_BTN_CLASS_NORMAL, id: Dialogs.DIALOG_BTN_CANCEL, |
| 95 | + text: Strings.CANCEL |
| 96 | + }, |
| 97 | + { className: Dialogs.DIALOG_BTN_CLASS_PRIMARY, id: Dialogs.DIALOG_BTN_OK, |
| 98 | + text: Strings.OK |
| 99 | + } |
| 100 | + ] |
| 101 | + ).done(function (id) { |
| 102 | + |
| 103 | + if (id !== Dialogs.DIALOG_BTN_OK) { |
| 104 | + HealthLogger.sendAnalyticsData( |
| 105 | + "externalEditorsCancelled", |
| 106 | + "usage", |
| 107 | + "externalEditors", |
| 108 | + "Cancelled", |
| 109 | + "" |
| 110 | + ); |
| 111 | + return; |
| 112 | + } |
| 113 | + HealthLogger.sendAnalyticsData( |
| 114 | + "LinkExternalEditors", |
| 115 | + "usage", |
| 116 | + "externalEditors", |
| 117 | + "LinkExternalEditors", |
| 118 | + "" |
| 119 | + ); |
| 120 | + |
| 121 | + brackets.app.getSystemDefaultApp(_graphicsFileTypes.join(), function (err, out) { |
| 122 | + |
| 123 | + if (err) { |
| 124 | + return; |
| 125 | + } |
| 126 | + var associateApp = out.split(','), |
| 127 | + fileTypeToAppMap = {}, |
| 128 | + AppToFileTypeMap = {}; |
| 129 | + |
| 130 | + associateApp.forEach(function (item) { |
| 131 | + |
| 132 | + var filetype = item.split('##')[0], |
| 133 | + app = item.split('##')[1]; |
| 134 | + |
| 135 | + if (!filetype) { |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + if (filetype === "xd") { |
| 140 | + if (app.toLowerCase() !== "adobe xd" && app.toLowerCase() !== "adobe.cc.xd") { |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + app = "Adobe XD"; |
| 145 | + } |
| 146 | + fileTypeToAppMap[filetype] = app; |
| 147 | + |
| 148 | + if (brackets.platform === "win" && app.toLowerCase().endsWith('.exe')) { |
| 149 | + app = app.substring(app.lastIndexOf('\\') + 1, app.length - 4); |
| 150 | + } |
| 151 | + if (AppToFileTypeMap[app]) { |
| 152 | + AppToFileTypeMap[app].push(filetype); |
| 153 | + } else { |
| 154 | + AppToFileTypeMap[app] = [filetype]; |
| 155 | + } |
| 156 | + }); |
| 157 | + |
| 158 | + var prefs = PreferencesManager.get('externalApplications'); |
| 159 | + |
| 160 | + for (var key in fileTypeToAppMap) { |
| 161 | + if (fileTypeToAppMap.hasOwnProperty(key)) { |
| 162 | + if(key && !prefs[key]) { |
| 163 | + prefs[key] = fileTypeToAppMap[key]; |
| 164 | + if(brackets.platform === "win" && !fileTypeToAppMap[key].toLowerCase().endsWith('.exe')) { |
| 165 | + prefs[key] = "default"; |
| 166 | + } |
| 167 | + HealthLogger.sendAnalyticsData( |
| 168 | + "AddExternalEditorForFileType_" + key.toUpperCase(), |
| 169 | + "usage", |
| 170 | + "externalEditors", |
| 171 | + "AddExternalEditorForFileType_" + key.toUpperCase(), |
| 172 | + "" |
| 173 | + ); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + PreferencesManager.set('externalApplications', prefs); |
| 179 | + |
| 180 | + var str = ""; |
| 181 | + for(var app in AppToFileTypeMap) { |
| 182 | + str += AppToFileTypeMap[app].join() + "->" + app + "<br/>"; |
| 183 | + } |
| 184 | + |
| 185 | + if(!str) { |
| 186 | + return; |
| 187 | + } |
| 188 | + |
| 189 | + str+="<br/>"; |
| 190 | + |
| 191 | + Dialogs.showModalDialog( |
| 192 | + DefaultDialogs.DIALOG_ID_INFO, |
| 193 | + Strings.ASSOCIATE_GRAPHICS_FILE_TO_DEFAULT_APP_TITLE, |
| 194 | + StringsUtils.format(Strings.ASSOCIATE_GRAPHICS_FILE_TO_DEFAULT_APP_CNF_MSG, str), |
| 195 | + [ |
| 196 | + { className: Dialogs.DIALOG_BTN_CLASS_PRIMARY, id: Dialogs.DIALOG_BTN_OK, |
| 197 | + text: Strings.OK |
| 198 | + } |
| 199 | + ] |
| 200 | + ); |
| 201 | + }); |
| 202 | + }); |
| 203 | + PreferencesManager.setViewState("AssociateGraphicsFileDialogShown", true); |
| 204 | + |
| 205 | + } |
| 206 | + |
| 207 | + exports.init = init; |
| 208 | + |
| 209 | +}); |
0 commit comments