From bede6967d5f0eaaeb927f43ac1168912e5dc1c98 Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Wed, 3 Jun 2015 17:27:57 +0200 Subject: [PATCH] Installation folder check both at startup and when user attempts to change sketchbook location. Fixes #2719 --- .../arduino/view/preferences/Preferences.java | 15 +++++++++++ app/src/processing/app/Base.java | 2 ++ .../app/helpers/GUIUserNotifier.java | 2 +- .../src/processing/app/BaseNoGui.java | 25 ++++++++++++++++++- 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/app/src/cc/arduino/view/preferences/Preferences.java b/app/src/cc/arduino/view/preferences/Preferences.java index 0bc832d95a..3bfc2c41be 100644 --- a/app/src/cc/arduino/view/preferences/Preferences.java +++ b/app/src/cc/arduino/view/preferences/Preferences.java @@ -39,6 +39,7 @@ import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.io.File; +import java.util.*; import static processing.app.I18n._; @@ -588,6 +589,12 @@ private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN- }//GEN-LAST:event_cancelButtonActionPerformed private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed + java.util.List errors = validateData(); + if (!errors.isEmpty()) { + Base.showWarning(_("Error"), errors.get(0), null); + return; + } + savePreferencesData(); for (Editor editor : base.getEditors()) { editor.applyPreferences(); @@ -619,6 +626,14 @@ private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS private javax.swing.JCheckBox verifyUploadBox; // End of variables declaration//GEN-END:variables + private java.util.List validateData() { + java.util.List errors = new LinkedList(); + if (FileUtils.isSubDirectory(new File(sketchbookLocationField.getText()), new File(PreferencesData.get("runtime.ide.path")))) { + errors.add(_("The specified sketchbook folder contains your copy of the IDE.\nPlease choose a different folder for your sketchbook.")); + } + return errors; + } + private void savePreferencesData() { String oldPath = PreferencesData.get("sketchbook.path"); String newPath = sketchbookLocationField.getText(); diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 70906bf6c0..0771b37689 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -274,6 +274,8 @@ public Base(String[] args) throws Exception { BaseNoGui.notifier = new GUIUserNotifier(this); this.recentSketchesMenuItems = new LinkedList(); + BaseNoGui.checkInstallationFolder(); + String sketchbookPath = BaseNoGui.getSketchbookPath(); // If no path is set, get the default sketchbook folder for this platform diff --git a/app/src/processing/app/helpers/GUIUserNotifier.java b/app/src/processing/app/helpers/GUIUserNotifier.java index ec41c59999..0e82cc32a7 100644 --- a/app/src/processing/app/helpers/GUIUserNotifier.java +++ b/app/src/processing/app/helpers/GUIUserNotifier.java @@ -48,7 +48,7 @@ public void showMessage(String title, String message) { public void showWarning(String title, String message, Exception e) { if (title == null) title = _("Warning"); - JOptionPane.showMessageDialog(new Frame(), message, title, + JOptionPane.showMessageDialog(base.getActiveEditor(), message, title, JOptionPane.WARNING_MESSAGE); if (e != null) e.printStackTrace(); diff --git a/arduino-core/src/processing/app/BaseNoGui.java b/arduino-core/src/processing/app/BaseNoGui.java index 9fa933e2ca..fa4a272151 100644 --- a/arduino-core/src/processing/app/BaseNoGui.java +++ b/arduino-core/src/processing/app/BaseNoGui.java @@ -754,10 +754,33 @@ static public void main(String args[]) throws Exception { initPortableFolder(); initParameters(args); - + + checkInstallationFolder(); + init(args); } + public static void checkInstallationFolder() { + if (isIDEInstalledIntoSettingsFolder()) { + showError(_("Incorrect IDE installation folder"), _("Your copy of the IDE is installed in a subfolder of your settings folder.\nPlease move the IDE to another folder."), 10); + } + if (isIDEInstalledIntoSketchbookFolder()) { + showError(_("Incorrect IDE installation folder"), _("Your copy of the IDE is installed in a subfolder of your sketchbook.\nPlease move the IDE to another folder."), 10); + } + } + + public static boolean isIDEInstalledIntoSketchbookFolder() { + return PreferencesData.has("sketchbook.path") && FileUtils.isSubDirectory(new File(PreferencesData.get("sketchbook.path")), new File(PreferencesData.get("runtime.ide.path"))); + } + + public static boolean isIDEInstalledIntoSettingsFolder() { + try { + return FileUtils.isSubDirectory(BaseNoGui.getPlatform().getSettingsFolder(), new File(PreferencesData.get("runtime.ide.path"))); + } catch (Exception e) { + return false; + } + } + static public void onBoardOrPortChange() { examplesFolder = getContentFile("examples"); toolsFolder = getContentFile("tools");