Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Portable Arduino #1211

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 deletions app/src/processing/app/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,18 @@ public class Base {
List<Editor> editors = Collections.synchronizedList(new ArrayList<Editor>());
Editor activeEditor;

static File portableFolder = null;
static final String portableSketchbookFolder = "sketchbook";


static public void main(String args[]) throws Exception {
initPlatform();

// Portable folder
portableFolder = getContentFile("portable");
if (!portableFolder.exists())
portableFolder = null;

// run static initialization that grabs all the prefs
Preferences.init(null);

Expand Down Expand Up @@ -249,8 +257,12 @@ public Base(String[] args) throws Exception {
// If a value is at least set, first check to see if the folder exists.
// If it doesn't, warn the user that the sketchbook folder is being reset.
if (sketchbookPath != null) {
File skechbookFolder = new File(sketchbookPath);
if (!skechbookFolder.exists()) {
File sketchbookFolder;
if (portableFolder != null)
sketchbookFolder = new File(portableFolder, sketchbookPath);
else
sketchbookFolder = new File(sketchbookPath);
if (!sketchbookFolder.exists()) {
Base.showWarning(_("Sketchbook folder disappeared"),
_("The sketchbook folder no longer exists.\n" +
"Arduino will switch to the default sketchbook\n" +
Expand All @@ -264,7 +276,10 @@ public Base(String[] args) throws Exception {
// If no path is set, get the default sketchbook folder for this platform
if (sketchbookPath == null) {
File defaultFolder = getDefaultSketchbookFolder();
Preferences.set("sketchbook.path", defaultFolder.getAbsolutePath());
if (portableFolder != null)
Preferences.set("sketchbook.path", portableSketchbookFolder);
else
Preferences.set("sketchbook.path", defaultFolder.getAbsolutePath());
if (!defaultFolder.exists()) {
defaultFolder.mkdirs();
}
Expand Down Expand Up @@ -422,6 +437,15 @@ protected boolean restoreSketches() {
int opened = 0;
for (int i = 0; i < count; i++) {
String path = Preferences.get("last.sketch" + i + ".path");
if (portableFolder != null) {
File absolute = new File(portableFolder, path);
try {
path = absolute.getCanonicalPath();
}
catch (IOException e) {
// path unchanged.
}
}
int[] location;
if (windowPositionValid) {
String locationStr = Preferences.get("last.sketch" + i + ".location");
Expand Down Expand Up @@ -460,6 +484,11 @@ protected void storeSketches() {
!editor.getSketch().isModified()) {
continue;
}
if (portableFolder != null) {
path = RelativePath.relativePath(portableFolder.toString(), path);
if (path == null)
continue;
}
Preferences.set("last.sketch" + index + ".path", path);

int[] location = editor.getPlacement();
Expand All @@ -478,6 +507,11 @@ protected void storeSketchPath(Editor editor, int index) {
String untitledPath = untitledFolder.getAbsolutePath();
if (path.startsWith(untitledPath)) {
path = "";
} else
if (portableFolder != null) {
path = RelativePath.relativePath(portableFolder.toString(), path);
if (path == null)
path = "";
}
Preferences.set("last.sketch" + index + ".path", path);
}
Expand Down Expand Up @@ -1759,6 +1793,9 @@ static public boolean isLinux() {


static public File getSettingsFolder() {
if (portableFolder != null)
return portableFolder;

File settingsFolder = null;

String preferencesPath = Preferences.get("settings.path");
Expand Down Expand Up @@ -1938,7 +1975,19 @@ static public Map<String, String> getBoardPreferences() {
return boardPreferences;
}

static public File getPortableFolder() {
return portableFolder;
}


static public String getPortableSketchbookFolder() {
return portableSketchbookFolder;
}


static public File getSketchbookFolder() {
if (portableFolder != null)
return new File(portableFolder, Preferences.get("sketchbook.path"));
return new File(Preferences.get("sketchbook.path"));
}

Expand Down Expand Up @@ -1971,6 +2020,9 @@ static public File getSketchbookHardwareFolder() {


protected File getDefaultSketchbookFolder() {
if (portableFolder != null)
return new File(portableFolder, portableSketchbookFolder);

File sketchbookFolder = null;
try {
sketchbookFolder = platform.getDefaultSketchbookFolder();
Expand Down
19 changes: 18 additions & 1 deletion app/src/processing/app/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,14 @@ public void actionPerformed(ActionEvent e) {
File file =
Base.selectFolder(_("Select new sketchbook location"), dflt, dialog);
if (file != null) {
sketchbookLocationField.setText(file.getAbsolutePath());
String path = file.getAbsolutePath();
if (Base.getPortableFolder() != null) {
path = RelativePath.relativePath(Base.getPortableFolder().toString(), path);
if (path == null) {
path = Base.getPortableSketchbookFolder();
}
}
sketchbookLocationField.setText(path);
}
}
});
Expand Down Expand Up @@ -439,6 +446,10 @@ public void actionPerformed(ActionEvent e) {
autoAssociateBox.setBounds(left, top, d.width + 10, d.height);
right = Math.max(right, left + d.width);
top += d.height + GUI_BETWEEN;

// If using portable mode, it's bad manner to change PC setting.
if (Base.getPortableFolder() != null)
autoAssociateBox.setEnabled(false);
}

// More preferences are in the ...
Expand Down Expand Up @@ -591,6 +602,12 @@ protected void applyFrame() {
// if the sketchbook path has changed, rebuild the menus
String oldPath = get("sketchbook.path");
String newPath = sketchbookLocationField.getText();
if (newPath.isEmpty()) {
if (Base.getPortableFolder() == null)
newPath = editor.base.getDefaultSketchbookFolder().toString();
else
newPath = Base.getPortableSketchbookFolder();
}
if (!newPath.equals(oldPath)) {
editor.base.rebuildSketchbookMenus();
set("sketchbook.path", newPath);
Expand Down
74 changes: 74 additions & 0 deletions app/src/processing/app/RelativePath.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* by Shigeru KANEMOTO at SWITCHSCIENCE.
*/

package processing.app;

import java.io.File;
import java.io.IOException;

class RelativePath {
//
// Compute relative path to "target" from a directory "origin".
//
// If "origin" is not absolute, it is relative from the current directory.
// If "target" is not absolute, it is relative from "origin".
//
public static String relativePath(String origin, String target) {
try {
origin = (new File(origin)).getCanonicalPath();
File targetFile = new File(target);
if (targetFile.isAbsolute())
target = targetFile.getCanonicalPath();
else
target = (new File(origin, target)).getCanonicalPath();
}
catch (IOException e) {
return null;
}

if (origin.equals(target)) {
// origin and target is identical.
return ".";
}

if (origin.equals(File.separator)) {
// origin is root.
return "." + target;
}

String prefix = "";
String root = File.separator;

if (System.getProperty("os.name").indexOf("Windows") != -1) {
if (origin.startsWith("\\\\") || target.startsWith("\\\\")) {
// Windows UNC path not supported.
return null;
}

char originLetter = origin.charAt(0);
char targetLetter = target.charAt(0);
if (Character.isLetter(originLetter) && Character.isLetter(targetLetter)) {
// Windows only
if (originLetter != targetLetter) {
// Drive letters differ
return null;
}
}

prefix = "" + originLetter + ':';
root = prefix + File.separator;
}

String relative = "";
while (!target.startsWith(origin + File.separator)) {
origin = (new File(origin)).getParent();
if (origin.equals(root))
origin = prefix;
relative += "..";
relative += File.separator;
}

return relative + target.substring(origin.length() + 1);
}
}
2 changes: 1 addition & 1 deletion app/src/processing/app/Sketch.java
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ protected boolean saveAs() throws IOException {
FileDialog.SAVE);
if (isReadOnly() || isUntitled()) {
// default to the sketchbook folder
fd.setDirectory(Preferences.get("sketchbook.path"));
fd.setDirectory(Base.getSketchbookFolder().getAbsolutePath());
} else {
// default to the parent folder of where this was
fd.setDirectory(folder.getParent());
Expand Down