Windows: even old settings folder may be missing from the registry. Fixes #4124

This commit is contained in:
Federico Fissore 2015-12-01 11:36:45 +01:00
parent 49a0f768a1
commit 46d1c89073
4 changed files with 16 additions and 25 deletions

View File

@ -973,7 +973,7 @@ public class BaseNoGui {
}
}
static public void initParameters(String args[]) throws IOException {
static public void initParameters(String args[]) throws Exception {
String preferencesFile = null;
// Do a first pass over the commandline arguments, the rest of them

View File

@ -252,7 +252,7 @@ public class Platform {
process.waitFor();
}
public void fixSettingsLocation() throws IOException {
public void fixSettingsLocation() throws Exception {
//noop
}
}

View File

@ -31,7 +31,7 @@ public class PreferencesData {
static boolean doSave = true;
static public void init(File file) throws IOException {
static public void init(File file) throws Exception {
if (file == null) {
BaseNoGui.getPlatform().fixSettingsLocation();
}

View File

@ -24,8 +24,6 @@ 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;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.Executor;
@ -44,7 +42,6 @@ import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
public class Platform extends processing.app.Platform {
@ -68,6 +65,14 @@ public class Platform extends processing.app.Platform {
this.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();
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");
@ -77,19 +82,6 @@ public class Platform extends processing.app.Platform {
this.defaultSketchbookFolder = path.resolve("Arduino").toFile();
}
private String readRegistryEntry(String[] lastPathElements, String key) {
for (String lastPathElement : lastPathElements) {
try {
String value = Advapi32Util.registryGetStringValue(WinReg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\" + lastPathElement, key);
value = value.replaceAll("%[uU][sS][eE][rR][pP][rR][oO][fF][iI][lL][eE]%", Matcher.quoteReplacement(System.getenv("USERPROFILE")));
return value;
} catch (Exception e) {
//ignore
}
}
throw new IllegalStateException("Unable to find " + key + " key in Windows registry");
}
/**
* Remove extra quotes, slashes, and garbage from the Windows PATH.
*/
@ -255,14 +247,13 @@ public class Platform extends processing.app.Platform {
}
@Override
public void fixSettingsLocation() throws IOException {
String path = Advapi32Util.registryGetStringValue(WinReg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "AppData");
Path previousSettingsFolder = Paths.get(path, "Arduino15");
if (!Files.exists(previousSettingsFolder)) {
public void fixSettingsLocation() throws Exception {
Path oldSettingsFolder = recoverOldSettingsFolderPath();
if (!Files.exists(oldSettingsFolder)) {
return;
}
if (!Files.exists(previousSettingsFolder.resolve(Paths.get("preferences.txt")))) {
if (!Files.exists(oldSettingsFolder.resolve(Paths.get("preferences.txt")))) {
return;
}
@ -270,6 +261,6 @@ public class Platform extends processing.app.Platform {
return;
}
Files.move(previousSettingsFolder, settingsFolder.toPath());
Files.move(oldSettingsFolder, settingsFolder.toPath());
}
}