From 285660046348142eb944787b9b6f1cfb275bf08a Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Sat, 20 Aug 2016 09:09:06 +0300 Subject: [PATCH] Windows: Use Shell32 utility to determine known paths --- .../app/windows/RegQueryParserTest.java | 53 --------------- .../cc/arduino/os/windows/FolderFinder.java | 65 ------------------- .../windows/FolderFinderInWindowsEnvVar.java | 54 --------------- .../FolderFinderInWindowsRegistry.java | 58 ----------------- .../src/processing/app/windows/Platform.java | 23 ++----- .../app/windows/RegQueryParser.java | 27 -------- 6 files changed, 6 insertions(+), 274 deletions(-) delete mode 100644 app/test/processing/app/windows/RegQueryParserTest.java delete mode 100644 arduino-core/src/cc/arduino/os/windows/FolderFinder.java delete mode 100644 arduino-core/src/cc/arduino/os/windows/FolderFinderInWindowsEnvVar.java delete mode 100644 arduino-core/src/cc/arduino/os/windows/FolderFinderInWindowsRegistry.java delete mode 100644 arduino-core/src/processing/app/windows/RegQueryParser.java diff --git a/app/test/processing/app/windows/RegQueryParserTest.java b/app/test/processing/app/windows/RegQueryParserTest.java deleted file mode 100644 index de26ae369..000000000 --- a/app/test/processing/app/windows/RegQueryParserTest.java +++ /dev/null @@ -1,53 +0,0 @@ -package processing.app.windows; - -import org.junit.Test; -import processing.app.helpers.FileUtils; - -import java.io.File; - -import static org.junit.Assert.assertEquals; - -public class RegQueryParserTest { - - @Test - public void testRegQueryParser() throws Exception { - String output = "! REG.EXE VERSION 3.0\n" + - "\n" + - "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\n" + - "\n" + - " Local AppData REG_SZ C:\\Documents and Settings\\username\\My Documents"; - - String folderPath = new RegQueryParser(output).getValueOfKey(); - assertEquals("C:\\Documents and Settings\\username\\My Documents", folderPath); - } - - @Test - public void testRegQueryParser2() throws Exception { - String output = "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\n" + - " Local AppData REG_SZ C:\\Users\\username\\AppData\\Local"; - - String folderPath = new RegQueryParser(output).getValueOfKey(); - assertEquals("C:\\Users\\username\\AppData\\Local", folderPath); - } - - @Test - public void testRegQueryParserXP() throws Exception { - String output = "! REG.EXE VERSION 3.0\n" + - "\n" + - "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\n" + - "\n" + - "\tLocal AppData REG_SZ C:\\Documents and Settings\\username\\My Documents"; - - String folderPath = new RegQueryParser(output).getValueOfKey(); - assertEquals("C:\\Documents and Settings\\username\\My Documents", folderPath); - } - - @Test - public void testRegQueryParserGerman() throws Exception { - String output = FileUtils.readFileToString(new File(RegQueryParserTest.class.getResource("german.reg.query.output.txt").getFile()), "Cp852"); - - String folderPath = new RegQueryParser(output).getValueOfKey(); - assertEquals("C:\\Users\\René\\AppData\\Roaming", folderPath); - } - -} diff --git a/arduino-core/src/cc/arduino/os/windows/FolderFinder.java b/arduino-core/src/cc/arduino/os/windows/FolderFinder.java deleted file mode 100644 index 0bc121fe4..000000000 --- a/arduino-core/src/cc/arduino/os/windows/FolderFinder.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * This file is part of Arduino. - * - * Copyright 2015 Arduino LLC (http://www.arduino.cc/) - * - * Arduino is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - * As a special exception, you may use this file as part of a free software - * library without restriction. Specifically, if other files instantiate - * templates or use macros or inline functions from this file, or you compile - * this file and link it with other files to produce an executable, this - * file does not by itself cause the resulting executable to be covered by - * the GNU General Public License. This exception does not however - * invalidate any other reasons why the executable file might be covered by - * the GNU General Public License. - */ - -package cc.arduino.os.windows; - -import java.nio.file.Path; - -public abstract class FolderFinder { - - private final FolderFinder next; - protected final String folderName; - - protected FolderFinder(FolderFinder next, String folderName) { - this.next = next; - this.folderName = folderName; - } - - protected abstract Path findInternal() throws Exception; - - public Path find() throws Exception { - try { - Path value = findInternal(); - if (value != null) { - return value; - } - if (next != null) { - return next.find(); - } - throw new IllegalStateException("Unable to find your " + folderName + " folder"); - } catch (Exception e) { - if (next != null) { - return next.find(); - } - throw e; - } - - } - -} diff --git a/arduino-core/src/cc/arduino/os/windows/FolderFinderInWindowsEnvVar.java b/arduino-core/src/cc/arduino/os/windows/FolderFinderInWindowsEnvVar.java deleted file mode 100644 index 5c217ead3..000000000 --- a/arduino-core/src/cc/arduino/os/windows/FolderFinderInWindowsEnvVar.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * This file is part of Arduino. - * - * Copyright 2015 Arduino LLC (http://www.arduino.cc/) - * - * Arduino is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - * As a special exception, you may use this file as part of a free software - * library without restriction. Specifically, if other files instantiate - * templates or use macros or inline functions from this file, or you compile - * this file and link it with other files to produce an executable, this - * file does not by itself cause the resulting executable to be covered by - * the GNU General Public License. This exception does not however - * invalidate any other reasons why the executable file might be covered by - * the GNU General Public License. - */ - -package cc.arduino.os.windows; - -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; - -public class FolderFinderInWindowsEnvVar extends FolderFinder { - - private final String envVar; - - public FolderFinderInWindowsEnvVar(FolderFinder next, String folderName, String evnVar) { - super(next, folderName); - this.envVar = evnVar; - } - - @Override - public Path findInternal() throws Exception { - String userprofile = System.getenv(envVar); - Path documents = Paths.get(userprofile, folderName); - if (Files.exists(documents)) { - return documents; - } - return null; - } -} diff --git a/arduino-core/src/cc/arduino/os/windows/FolderFinderInWindowsRegistry.java b/arduino-core/src/cc/arduino/os/windows/FolderFinderInWindowsRegistry.java deleted file mode 100644 index cd7abde76..000000000 --- a/arduino-core/src/cc/arduino/os/windows/FolderFinderInWindowsRegistry.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * This file is part of Arduino. - * - * Copyright 2015 Arduino LLC (http://www.arduino.cc/) - * - * Arduino is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - * As a special exception, you may use this file as part of a free software - * library without restriction. Specifically, if other files instantiate - * templates or use macros or inline functions from this file, or you compile - * this file and link it with other files to produce an executable, this - * file does not by itself cause the resulting executable to be covered by - * the GNU General Public License. This exception does not however - * invalidate any other reasons why the executable file might be covered by - * the GNU General Public License. - */ - -package cc.arduino.os.windows; - -import com.sun.jna.platform.win32.Advapi32Util; -import com.sun.jna.platform.win32.WinReg; - -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.regex.Matcher; - -public class FolderFinderInWindowsRegistry extends FolderFinder { - - private final String registryPath; - private final String registryKey; - - public FolderFinderInWindowsRegistry(FolderFinder next, String folderName, String registryPath, String registryKey) { - super(next, folderName); - - this.registryPath = registryPath; - this.registryKey = registryKey; - } - - @Override - public Path findInternal() throws Exception { - String value = Advapi32Util.registryGetStringValue(WinReg.HKEY_CURRENT_USER, registryPath, registryKey); - value = value.replaceAll("%[uU][sS][eE][rR][pP][rR][oO][fF][iI][lL][eE]%", Matcher.quoteReplacement(System.getenv("USERPROFILE"))); - return Paths.get(value); - } - -} diff --git a/arduino-core/src/processing/app/windows/Platform.java b/arduino-core/src/processing/app/windows/Platform.java index 790be03ed..5796c20cf 100644 --- a/arduino-core/src/processing/app/windows/Platform.java +++ b/arduino-core/src/processing/app/windows/Platform.java @@ -22,8 +22,7 @@ package processing.app.windows; -import cc.arduino.os.windows.FolderFinderInWindowsEnvVar; -import cc.arduino.os.windows.FolderFinderInWindowsRegistry; +import cc.arduino.os.windows.Win32KnownFolders; import processing.app.legacy.PApplet; import processing.app.legacy.PConstants; @@ -50,28 +49,18 @@ public class Platform extends processing.app.Platform { } private void recoverSettingsFolderPath() throws Exception { - FolderFinderInWindowsRegistry findInUserShellFolders = new FolderFinderInWindowsRegistry(null, "Documents", "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders", "Local AppData"); - FolderFinderInWindowsRegistry findInShellFolders = new FolderFinderInWindowsRegistry(findInUserShellFolders, "Documents", "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Local AppData"); - - Path path = findInShellFolders.find(); - this.settingsFolder = path.resolve("Arduino15").toFile(); + Path path = Win32KnownFolders.getLocalAppDataFolder().toPath(); + settingsFolder = path.resolve("Arduino15").toFile(); } private Path recoverOldSettingsFolderPath() throws Exception { - FolderFinderInWindowsRegistry findInUserShellFolders = new FolderFinderInWindowsRegistry(null, "Documents", "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders", "AppData"); - FolderFinderInWindowsRegistry findInShellFolders = new FolderFinderInWindowsRegistry(findInUserShellFolders, "Documents", "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "AppData"); - - Path path = findInShellFolders.find(); + Path path = Win32KnownFolders.getRoamingAppDataFolder().toPath(); return path.resolve("Arduino15"); } private void recoverDefaultSketchbookFolder() throws Exception { - FolderFinderInWindowsEnvVar findInUserProfile = new FolderFinderInWindowsEnvVar(null, "Documents", "USERPROFILE"); - FolderFinderInWindowsRegistry findInUserShellFolders = new FolderFinderInWindowsRegistry(findInUserProfile, "Documents", "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders", "Personal"); - FolderFinderInWindowsRegistry findInShellFolders = new FolderFinderInWindowsRegistry(findInUserShellFolders, "Documents", "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Personal"); - - Path path = findInShellFolders.find(); - this.defaultSketchbookFolder = path.resolve("Arduino").toFile(); + Path path = Win32KnownFolders.getDocumentsFolder().toPath(); + defaultSketchbookFolder = path.resolve("Arduino").toFile(); } /** diff --git a/arduino-core/src/processing/app/windows/RegQueryParser.java b/arduino-core/src/processing/app/windows/RegQueryParser.java deleted file mode 100644 index e20a27faf..000000000 --- a/arduino-core/src/processing/app/windows/RegQueryParser.java +++ /dev/null @@ -1,27 +0,0 @@ -package processing.app.windows; - -import java.util.Arrays; -import java.util.List; - -public class RegQueryParser { - - private String valueOfKey; - - public RegQueryParser(String regQueryOutput) { - parse(regQueryOutput); - } - - private void parse(String regQueryOutput) { - List rows = Arrays.asList(regQueryOutput.replace(" ", "\t").replace("\r", "\n").replace("\n\n", "\n").split("\n")); - - String row = rows.stream().filter(input -> input.startsWith("\t")).findFirst().get(); - - String[] cols = row.split("\t"); - assert cols.length == 4; - this.valueOfKey = cols[3]; - } - - public String getValueOfKey() { - return valueOfKey; - } -}