Skip to content

Commit

Permalink
add support for portable settings (resolves #360 and https://github.c…
Browse files Browse the repository at this point in the history
  • Loading branch information
benfry committed Jan 22, 2022
1 parent f2e584b commit 1a49263
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 6 deletions.
44 changes: 43 additions & 1 deletion app/src/processing/app/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ public class Base {

static private boolean commandLine;

/**
* If settings.txt is present inside lib, it will be used to override
* the location of the settings folder so that "portable" versions
* of the software are possible.
*/
static private File settingsOverride;

// A single instance of the preferences window
PreferencesFrame preferencesFrame;

Expand Down Expand Up @@ -158,6 +165,35 @@ static private void createAndShowGUI(String[] args) {
}
}

// Detect settings.txt in the lib folder for portable versions
File settingsFile = Platform.getContentFile("lib/settings.txt");
if (settingsFile != null && settingsFile.exists()) {
try {
Settings portable = new Settings(settingsFile);
String path = portable.get("settings.path");
File folder = new File(path);
boolean success = true;
if (!folder.exists()) {
success = folder.mkdirs();
if (!success) {
Messages.err("Could not create " + folder + " to store settings.");
}
}
if (success) {
if (!folder.canRead()) {
Messages.err("Cannot read from " + folder);
} else if (!folder.canWrite()) {
Messages.err("Cannot write to " + folder);
} else {
settingsOverride = folder.getAbsoluteFile();
}
}
} catch (IOException e) {
Messages.err("Error while reading the settings.txt file", e);
}
}


Platform.init();
// call after Platform.init() because we need the settings folder
Console.startup();
Expand Down Expand Up @@ -1946,7 +1982,8 @@ static public InputStream getLibStream(String filename) throws IOException {
/**
* Get the directory that can store settings. (Library on OS X, App Data or
* something similar on Windows, a dot folder on Linux.) Removed this as a
* preference for 3.0a3 because we need this to be stable.
* preference for 3.0a3 because we need this to be stable, but adding back
* for 4.0 beta 4 so that folks can do 'portable' versions again.
*/
static public File getSettingsFolder() {
File settingsFolder = null;
Expand All @@ -1973,6 +2010,11 @@ static public File getSettingsFolder() {
}


static public File getSettingsOverride() {
return settingsOverride;
}


/**
* Convenience method to get a File object for the specified filename inside
* the settings folder. Used to get preferences and recent sketch files.
Expand Down
8 changes: 7 additions & 1 deletion app/src/processing/app/platform/DefaultPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,13 @@ public void saveLanguage(String languageCode) { }
* Do not return null.
*/
public File getSettingsFolder() throws Exception {
// otherwise make a .processing directory int the user's home dir
File override = Base.getSettingsOverride();
if (override != null) {
return override;
}

// If no subclass has a behavior, default to making a
// ".processing" directory in the user's home directory.
File home = new File(System.getProperty("user.home"));
return new File(home, ".processing");
}
Expand Down
5 changes: 5 additions & 0 deletions app/src/processing/app/platform/LinuxPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ static public String getHomeDir(String user) throws Exception {

@Override
public File getSettingsFolder() throws Exception {
File override = Base.getSettingsOverride();
if (override != null) {
return override;
}

// https://github.com/processing/processing4/issues/203
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html

Expand Down
4 changes: 4 additions & 0 deletions app/src/processing/app/platform/MacPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ public void initBase(Base base) {


public File getSettingsFolder() throws Exception {
File override = Base.getSettingsOverride();
if (override != null) {
return override;
}
return new File(getLibraryFolder(), "Processing");
}

Expand Down
5 changes: 5 additions & 0 deletions app/src/processing/app/platform/WindowsPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ protected void checkPath() {

// looking for Documents and Settings/blah/Application Data/Processing
public File getSettingsFolder() throws Exception {
File override = Base.getSettingsOverride();
if (override != null) {
return override;
}

try {
String appDataRoaming = getAppDataPath();
if (appDataRoaming != null) {
Expand Down
4 changes: 2 additions & 2 deletions build/shared/lib/defaults.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ recent.count = 10
chooser.files.native = true
# We were shutting this off on macOS because it broke Copy/Paste:
# https://github.com/processing/processing/issues/1035
# But removing again for 4.0 alpha 5, because the JFileChooser is awful,
# and worse on Big Sur, so a bigger problem than the Copy/Paste issue.
# https://github.com/processing/processing4/issues/77
# But changing this for 4.0 alpha 5, because the JFileChooser is awful,
# and even worse on Big Sur, and worse than the Copy/Paste issue.
#chooser.files.native.macosx = false


Expand Down
5 changes: 3 additions & 2 deletions todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ X write release notes about not moving to RSyntaxArea
X https://github.com/processing/processing4/issues/355
X https://github.com/processing/processing/issues/3199
X https://github.com/processing/processing4/blob/master/app/src/processing/app/syntax/README.md
X did we lose settings.path because it was too buggy?
_ https://github.com/processing/processing/issues/3948
_ https://github.com/processing/processing4/pull/360

windows scaling
X Fix "Could not delete disable_hidpi" message on macOS/Linux after closing prefs
Expand Down Expand Up @@ -355,8 +358,6 @@ _ already reported?
teaching
_ teacher wants user input on the console
_ https://github.com/processing/processing/issues/5779
_ did we lose settings.path because it was too buggy?
_ https://github.com/processing/processing/issues/3948
_ proxy trouble with p5? since adding the system proxy?
_ https://github.com/processing/processing/pull/3251/files
_ larger problem thread https://github.com/processing/processing/issues/3891
Expand Down

0 comments on commit 1a49263

Please sign in to comment.