Skip to content

Commit 9729b1b

Browse files
author
Federico Fissore
committed
Windows: in case Shell Folders entry is missing, attempts to discover Documents folder using User Shell Folders. See #4124
1 parent 8b35216 commit 9729b1b

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* This file is part of Arduino.
3+
*
4+
* Arduino is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
* As a special exception, you may use this file as part of a free software
19+
* library without restriction. Specifically, if other files instantiate
20+
* templates or use macros or inline functions from this file, or you compile
21+
* this file and link it with other files to produce an executable, this
22+
* file does not by itself cause the resulting executable to be covered by
23+
* the GNU General Public License. This exception does not however
24+
* invalidate any other reasons why the executable file might be covered by
25+
* the GNU General Public License.
26+
*
27+
* Copyright 2015 Arduino LLC (http://www.arduino.cc/)
28+
*/
29+
30+
package processing.app.windows;
31+
32+
import org.junit.Test;
33+
34+
import java.util.regex.Matcher;
35+
36+
import static org.junit.Assert.assertEquals;
37+
38+
public class RegexpTest {
39+
40+
@Test
41+
public void testReplaceAll() throws Exception {
42+
assertEquals("c:\\\\hello", Matcher.quoteReplacement("c:\\hello"));
43+
String result = "%UsErPROFile%\\world".replaceAll("%[uU][sS][eE][rR][pP][rR][oO][fF][iI][lL][eE]%", Matcher.quoteReplacement("c:\\hello"));
44+
assertEquals("c:\\hello\\world", result);
45+
46+
result = "%USERPROFILE%\\world".replaceAll("%[uU][sS][eE][rR][pP][rR][oO][fF][iI][lL][eE]%", Matcher.quoteReplacement("c:\\hello"));
47+
assertEquals("c:\\hello\\world", result);
48+
}
49+
50+
}

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.util.LinkedList;
4343
import java.util.List;
4444
import java.util.Map;
45+
import java.util.regex.Matcher;
4546

4647

4748
public class Platform extends processing.app.Platform {
@@ -57,16 +58,29 @@ public void init() throws IOException {
5758
recoverDefaultSketchbookFolder();
5859
}
5960

60-
private void recoverSettingsFolderPath() throws IOException {
61-
String path = Advapi32Util.registryGetStringValue(WinReg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Local AppData");
61+
private void recoverSettingsFolderPath() {
62+
String path = readRegistryEntry(new String[]{"User Shell Folders", "Shell Folders"}, "Local AppData");
6263
this.settingsFolder = new File(path, "Arduino15");
6364
}
6465

65-
private void recoverDefaultSketchbookFolder() throws IOException {
66-
String path = Advapi32Util.registryGetStringValue(WinReg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Personal");
66+
private void recoverDefaultSketchbookFolder() {
67+
String path = readRegistryEntry(new String[]{"User Shell Folders", "Shell Folders"}, "Personal");
6768
this.defaultSketchbookFolder = new File(path, "Arduino");
6869
}
6970

71+
private String readRegistryEntry(String[] lastPathElements, String key) {
72+
for (String lastPathElement : lastPathElements) {
73+
try {
74+
String value = Advapi32Util.registryGetStringValue(WinReg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\" + lastPathElement, key);
75+
value = value.replaceAll("%[uU][sS][eE][rR][pP][rR][oO][fF][iI][lL][eE]%", Matcher.quoteReplacement(System.getenv("USERPROFILE")));
76+
return value;
77+
} catch (Exception e) {
78+
//ignore
79+
}
80+
}
81+
throw new IllegalStateException("Unable to find " + key + " key in Windows registry");
82+
}
83+
7084
/**
7185
* Remove extra quotes, slashes, and garbage from the Windows PATH.
7286
*/

0 commit comments

Comments
 (0)