mirror of
https://github.com/arduino/Arduino.git
synced 2025-02-20 14:54:31 +01:00
Windows: when finding default Documents folder, if registry keys are missing,
fallback to environment variable. See #4124
This commit is contained in:
parent
b5565c5356
commit
2747fddd10
65
arduino-core/src/cc/arduino/os/windows/FolderFinder.java
Normal file
65
arduino-core/src/cc/arduino/os/windows/FolderFinder.java
Normal file
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
|
||||
}
|
@ -74,7 +74,7 @@ public class Platform {
|
||||
}
|
||||
|
||||
|
||||
public void init() throws IOException {
|
||||
public void init() throws Exception {
|
||||
}
|
||||
|
||||
|
||||
|
@ -60,7 +60,7 @@ public class Platform extends processing.app.Platform {
|
||||
Toolkit.getDefaultToolkit();
|
||||
}
|
||||
|
||||
public void init() throws IOException {
|
||||
public void init() throws Exception {
|
||||
super.init();
|
||||
|
||||
System.setProperty("apple.laf.useScreenMenuBar", "true");
|
||||
|
@ -22,6 +22,8 @@
|
||||
|
||||
package processing.app.windows;
|
||||
|
||||
import cc.arduino.os.windows.FolderFinderInWindowsEnvVar;
|
||||
import cc.arduino.os.windows.FolderFinderInWindowsRegistry;
|
||||
import com.sun.jna.platform.win32.Advapi32Util;
|
||||
import com.sun.jna.platform.win32.WinReg;
|
||||
import org.apache.commons.exec.CommandLine;
|
||||
@ -50,7 +52,7 @@ public class Platform extends processing.app.Platform {
|
||||
private File settingsFolder;
|
||||
private File defaultSketchbookFolder;
|
||||
|
||||
public void init() throws IOException {
|
||||
public void init() throws Exception {
|
||||
super.init();
|
||||
|
||||
checkPath();
|
||||
@ -58,14 +60,21 @@ public class Platform extends processing.app.Platform {
|
||||
recoverDefaultSketchbookFolder();
|
||||
}
|
||||
|
||||
private void recoverSettingsFolderPath() {
|
||||
String path = readRegistryEntry(new String[]{"Shell Folders", "User Shell Folders"}, "Local AppData");
|
||||
this.settingsFolder = new File(path, "Arduino15");
|
||||
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();
|
||||
}
|
||||
|
||||
private void recoverDefaultSketchbookFolder() {
|
||||
String path = readRegistryEntry(new String[]{"Shell Folders", "User Shell Folders"}, "Personal");
|
||||
this.defaultSketchbookFolder = new File(path, "Arduino");
|
||||
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();
|
||||
}
|
||||
|
||||
private String readRegistryEntry(String[] lastPathElements, String key) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user