Skip to content

Commit 46d1c89

Browse files
author
Federico Fissore
committed
Windows: even old settings folder may be missing from the registry. Fixes #4124
1 parent 49a0f76 commit 46d1c89

File tree

4 files changed

+16
-25
lines changed

4 files changed

+16
-25
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ static public void populateImportToLibraryTable() {
973973
}
974974
}
975975

976-
static public void initParameters(String args[]) throws IOException {
976+
static public void initParameters(String args[]) throws Exception {
977977
String preferencesFile = null;
978978

979979
// Do a first pass over the commandline arguments, the rest of them

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public void chmod(File file, int mode) throws IOException, InterruptedException
252252
process.waitFor();
253253
}
254254

255-
public void fixSettingsLocation() throws IOException {
255+
public void fixSettingsLocation() throws Exception {
256256
//noop
257257
}
258258
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class PreferencesData {
3131
static boolean doSave = true;
3232

3333

34-
static public void init(File file) throws IOException {
34+
static public void init(File file) throws Exception {
3535
if (file == null) {
3636
BaseNoGui.getPlatform().fixSettingsLocation();
3737
}

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

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424

2525
import cc.arduino.os.windows.FolderFinderInWindowsEnvVar;
2626
import cc.arduino.os.windows.FolderFinderInWindowsRegistry;
27-
import com.sun.jna.platform.win32.Advapi32Util;
28-
import com.sun.jna.platform.win32.WinReg;
2927
import org.apache.commons.exec.CommandLine;
3028
import org.apache.commons.exec.DefaultExecutor;
3129
import org.apache.commons.exec.Executor;
@@ -44,7 +42,6 @@
4442
import java.util.LinkedList;
4543
import java.util.List;
4644
import java.util.Map;
47-
import java.util.regex.Matcher;
4845

4946

5047
public class Platform extends processing.app.Platform {
@@ -68,6 +65,14 @@ private void recoverSettingsFolderPath() throws Exception {
6865
this.settingsFolder = path.resolve("Arduino15").toFile();
6966
}
7067

68+
private Path recoverOldSettingsFolderPath() throws Exception {
69+
FolderFinderInWindowsRegistry findInUserShellFolders = new FolderFinderInWindowsRegistry(null, "Documents", "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders", "AppData");
70+
FolderFinderInWindowsRegistry findInShellFolders = new FolderFinderInWindowsRegistry(findInUserShellFolders, "Documents", "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "AppData");
71+
72+
Path path = findInShellFolders.find();
73+
return path.resolve("Arduino15");
74+
}
75+
7176
private void recoverDefaultSketchbookFolder() throws Exception {
7277
FolderFinderInWindowsEnvVar findInUserProfile = new FolderFinderInWindowsEnvVar(null, "Documents", "USERPROFILE");
7378
FolderFinderInWindowsRegistry findInUserShellFolders = new FolderFinderInWindowsRegistry(findInUserProfile, "Documents", "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders", "Personal");
@@ -77,19 +82,6 @@ private void recoverDefaultSketchbookFolder() throws Exception {
7782
this.defaultSketchbookFolder = path.resolve("Arduino").toFile();
7883
}
7984

80-
private String readRegistryEntry(String[] lastPathElements, String key) {
81-
for (String lastPathElement : lastPathElements) {
82-
try {
83-
String value = Advapi32Util.registryGetStringValue(WinReg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\" + lastPathElement, key);
84-
value = value.replaceAll("%[uU][sS][eE][rR][pP][rR][oO][fF][iI][lL][eE]%", Matcher.quoteReplacement(System.getenv("USERPROFILE")));
85-
return value;
86-
} catch (Exception e) {
87-
//ignore
88-
}
89-
}
90-
throw new IllegalStateException("Unable to find " + key + " key in Windows registry");
91-
}
92-
9385
/**
9486
* Remove extra quotes, slashes, and garbage from the Windows PATH.
9587
*/
@@ -255,21 +247,20 @@ public void chmod(File file, int mode) throws IOException, InterruptedException
255247
}
256248

257249
@Override
258-
public void fixSettingsLocation() throws IOException {
259-
String path = Advapi32Util.registryGetStringValue(WinReg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "AppData");
260-
Path previousSettingsFolder = Paths.get(path, "Arduino15");
261-
if (!Files.exists(previousSettingsFolder)) {
250+
public void fixSettingsLocation() throws Exception {
251+
Path oldSettingsFolder = recoverOldSettingsFolderPath();
252+
if (!Files.exists(oldSettingsFolder)) {
262253
return;
263254
}
264255

265-
if (!Files.exists(previousSettingsFolder.resolve(Paths.get("preferences.txt")))) {
256+
if (!Files.exists(oldSettingsFolder.resolve(Paths.get("preferences.txt")))) {
266257
return;
267258
}
268259

269260
if (settingsFolder.exists()) {
270261
return;
271262
}
272263

273-
Files.move(previousSettingsFolder, settingsFolder.toPath());
264+
Files.move(oldSettingsFolder, settingsFolder.toPath());
274265
}
275266
}

0 commit comments

Comments
 (0)