Windows: Use Shell32 utility to determine known paths

This commit is contained in:
Cristian Maglie 2016-08-20 09:09:06 +03:00
parent cbf3cdcfbb
commit 2856600463
6 changed files with 6 additions and 274 deletions

View File

@ -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);
}
}

View File

@ -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;
}
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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();
}
/**

View File

@ -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<String> 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;
}
}