Skip to content

Commit 2747fdd

Browse files
author
Federico Fissore
committed
Windows: when finding default Documents folder, if registry keys are missing,
fallback to environment variable. See #4124
1 parent b5565c5 commit 2747fdd

File tree

6 files changed

+195
-9
lines changed

6 files changed

+195
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* This file is part of Arduino.
3+
*
4+
* Copyright 2015 Arduino LLC (http://www.arduino.cc/)
5+
*
6+
* Arduino is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*
20+
* As a special exception, you may use this file as part of a free software
21+
* library without restriction. Specifically, if other files instantiate
22+
* templates or use macros or inline functions from this file, or you compile
23+
* this file and link it with other files to produce an executable, this
24+
* file does not by itself cause the resulting executable to be covered by
25+
* the GNU General Public License. This exception does not however
26+
* invalidate any other reasons why the executable file might be covered by
27+
* the GNU General Public License.
28+
*/
29+
30+
package cc.arduino.os.windows;
31+
32+
import java.nio.file.Path;
33+
34+
public abstract class FolderFinder {
35+
36+
private final FolderFinder next;
37+
protected final String folderName;
38+
39+
protected FolderFinder(FolderFinder next, String folderName) {
40+
this.next = next;
41+
this.folderName = folderName;
42+
}
43+
44+
protected abstract Path findInternal() throws Exception;
45+
46+
public Path find() throws Exception {
47+
try {
48+
Path value = findInternal();
49+
if (value != null) {
50+
return value;
51+
}
52+
if (next != null) {
53+
return next.find();
54+
}
55+
throw new IllegalStateException("Unable to find your " + folderName + " folder");
56+
} catch (Exception e) {
57+
if (next != null) {
58+
return next.find();
59+
}
60+
throw e;
61+
}
62+
63+
}
64+
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* This file is part of Arduino.
3+
*
4+
* Copyright 2015 Arduino LLC (http://www.arduino.cc/)
5+
*
6+
* Arduino is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*
20+
* As a special exception, you may use this file as part of a free software
21+
* library without restriction. Specifically, if other files instantiate
22+
* templates or use macros or inline functions from this file, or you compile
23+
* this file and link it with other files to produce an executable, this
24+
* file does not by itself cause the resulting executable to be covered by
25+
* the GNU General Public License. This exception does not however
26+
* invalidate any other reasons why the executable file might be covered by
27+
* the GNU General Public License.
28+
*/
29+
30+
package cc.arduino.os.windows;
31+
32+
import java.nio.file.Files;
33+
import java.nio.file.Path;
34+
import java.nio.file.Paths;
35+
36+
public class FolderFinderInWindowsEnvVar extends FolderFinder {
37+
38+
private final String envVar;
39+
40+
public FolderFinderInWindowsEnvVar(FolderFinder next, String folderName, String evnVar) {
41+
super(next, folderName);
42+
this.envVar = evnVar;
43+
}
44+
45+
@Override
46+
public Path findInternal() throws Exception {
47+
String userprofile = System.getenv(envVar);
48+
Path documents = Paths.get(userprofile, folderName);
49+
if (Files.exists(documents)) {
50+
return documents;
51+
}
52+
return null;
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* This file is part of Arduino.
3+
*
4+
* Copyright 2015 Arduino LLC (http://www.arduino.cc/)
5+
*
6+
* Arduino is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*
20+
* As a special exception, you may use this file as part of a free software
21+
* library without restriction. Specifically, if other files instantiate
22+
* templates or use macros or inline functions from this file, or you compile
23+
* this file and link it with other files to produce an executable, this
24+
* file does not by itself cause the resulting executable to be covered by
25+
* the GNU General Public License. This exception does not however
26+
* invalidate any other reasons why the executable file might be covered by
27+
* the GNU General Public License.
28+
*/
29+
30+
package cc.arduino.os.windows;
31+
32+
import com.sun.jna.platform.win32.Advapi32Util;
33+
import com.sun.jna.platform.win32.WinReg;
34+
35+
import java.nio.file.Path;
36+
import java.nio.file.Paths;
37+
import java.util.regex.Matcher;
38+
39+
public class FolderFinderInWindowsRegistry extends FolderFinder {
40+
41+
private final String registryPath;
42+
private final String registryKey;
43+
44+
public FolderFinderInWindowsRegistry(FolderFinder next, String folderName, String registryPath, String registryKey) {
45+
super(next, folderName);
46+
47+
this.registryPath = registryPath;
48+
this.registryKey = registryKey;
49+
}
50+
51+
@Override
52+
public Path findInternal() throws Exception {
53+
String value = Advapi32Util.registryGetStringValue(WinReg.HKEY_CURRENT_USER, registryPath, registryKey);
54+
value = value.replaceAll("%[uU][sS][eE][rR][pP][rR][oO][fF][iI][lL][eE]%", Matcher.quoteReplacement(System.getenv("USERPROFILE")));
55+
return Paths.get(value);
56+
}
57+
58+
}

arduino-core/src/processing/app/Platform.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void setLookAndFeel() throws Exception {
7474
}
7575

7676

77-
public void init() throws IOException {
77+
public void init() throws Exception {
7878
}
7979

8080

arduino-core/src/processing/app/macosx/Platform.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public Platform() {
6060
Toolkit.getDefaultToolkit();
6161
}
6262

63-
public void init() throws IOException {
63+
public void init() throws Exception {
6464
super.init();
6565

6666
System.setProperty("apple.laf.useScreenMenuBar", "true");

arduino-core/src/processing/app/windows/Platform.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
package processing.app.windows;
2424

25+
import cc.arduino.os.windows.FolderFinderInWindowsEnvVar;
26+
import cc.arduino.os.windows.FolderFinderInWindowsRegistry;
2527
import com.sun.jna.platform.win32.Advapi32Util;
2628
import com.sun.jna.platform.win32.WinReg;
2729
import org.apache.commons.exec.CommandLine;
@@ -50,22 +52,29 @@ public class Platform extends processing.app.Platform {
5052
private File settingsFolder;
5153
private File defaultSketchbookFolder;
5254

53-
public void init() throws IOException {
55+
public void init() throws Exception {
5456
super.init();
5557

5658
checkPath();
5759
recoverSettingsFolderPath();
5860
recoverDefaultSketchbookFolder();
5961
}
6062

61-
private void recoverSettingsFolderPath() {
62-
String path = readRegistryEntry(new String[]{"Shell Folders", "User Shell Folders"}, "Local AppData");
63-
this.settingsFolder = new File(path, "Arduino15");
63+
private void recoverSettingsFolderPath() throws Exception {
64+
FolderFinderInWindowsRegistry findInUserShellFolders = new FolderFinderInWindowsRegistry(null, "Documents", "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders", "Local AppData");
65+
FolderFinderInWindowsRegistry findInShellFolders = new FolderFinderInWindowsRegistry(findInUserShellFolders, "Documents", "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Local AppData");
66+
67+
Path path = findInShellFolders.find();
68+
this.settingsFolder = path.resolve("Arduino15").toFile();
6469
}
6570

66-
private void recoverDefaultSketchbookFolder() {
67-
String path = readRegistryEntry(new String[]{"Shell Folders", "User Shell Folders"}, "Personal");
68-
this.defaultSketchbookFolder = new File(path, "Arduino");
71+
private void recoverDefaultSketchbookFolder() throws Exception {
72+
FolderFinderInWindowsEnvVar findInUserProfile = new FolderFinderInWindowsEnvVar(null, "Documents", "USERPROFILE");
73+
FolderFinderInWindowsRegistry findInUserShellFolders = new FolderFinderInWindowsRegistry(findInUserProfile, "Documents", "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders", "Personal");
74+
FolderFinderInWindowsRegistry findInShellFolders = new FolderFinderInWindowsRegistry(findInUserShellFolders, "Documents", "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Personal");
75+
76+
Path path = findInShellFolders.find();
77+
this.defaultSketchbookFolder = path.resolve("Arduino").toFile();
6978
}
7079

7180
private String readRegistryEntry(String[] lastPathElements, String key) {

0 commit comments

Comments
 (0)