@@ -111,6 +111,9 @@ public class Base {
111
111
List <Editor > editors = Collections .synchronizedList (new ArrayList <Editor >());
112
112
Editor activeEditor ;
113
113
114
+ static File portableFolder = null ;
115
+ static final String portableSketchbookFolder = "sketchbook" ;
116
+
114
117
115
118
static public void main (String args []) throws Exception {
116
119
initPlatform ();
@@ -156,6 +159,13 @@ static public void main(String args[]) throws Exception {
156
159
}
157
160
*/
158
161
162
+ initPlatform ();
163
+
164
+ // Portable folder
165
+ portableFolder = getContentFile ("portable" );
166
+ if (!portableFolder .exists ())
167
+ portableFolder = null ;
168
+
159
169
// // Set the look and feel before opening the window
160
170
// try {
161
171
// platform.setLookAndFeel();
@@ -249,8 +259,12 @@ public Base(String[] args) throws Exception {
249
259
// If a value is at least set, first check to see if the folder exists.
250
260
// If it doesn't, warn the user that the sketchbook folder is being reset.
251
261
if (sketchbookPath != null ) {
252
- File skechbookFolder = new File (sketchbookPath );
253
- if (!skechbookFolder .exists ()) {
262
+ File sketchbookFolder ;
263
+ if (portableFolder != null )
264
+ sketchbookFolder = new File (portableFolder , sketchbookPath );
265
+ else
266
+ sketchbookFolder = new File (sketchbookPath );
267
+ if (!sketchbookFolder .exists ()) {
254
268
Base .showWarning (_ ("Sketchbook folder disappeared" ),
255
269
_ ("The sketchbook folder no longer exists.\n " +
256
270
"Arduino will switch to the default sketchbook\n " +
@@ -264,7 +278,10 @@ public Base(String[] args) throws Exception {
264
278
// If no path is set, get the default sketchbook folder for this platform
265
279
if (sketchbookPath == null ) {
266
280
File defaultFolder = getDefaultSketchbookFolder ();
267
- Preferences .set ("sketchbook.path" , defaultFolder .getAbsolutePath ());
281
+ if (portableFolder != null )
282
+ Preferences .set ("sketchbook.path" , portableSketchbookFolder );
283
+ else
284
+ Preferences .set ("sketchbook.path" , defaultFolder .getAbsolutePath ());
268
285
if (!defaultFolder .exists ()) {
269
286
defaultFolder .mkdirs ();
270
287
}
@@ -422,6 +439,15 @@ protected boolean restoreSketches() {
422
439
int opened = 0 ;
423
440
for (int i = 0 ; i < count ; i ++) {
424
441
String path = Preferences .get ("last.sketch" + i + ".path" );
442
+ if (portableFolder != null ) {
443
+ File absolute = new File (portableFolder , path );
444
+ try {
445
+ path = absolute .getCanonicalPath ();
446
+ }
447
+ catch (IOException e ) {
448
+ // path unchanged.
449
+ }
450
+ }
425
451
int [] location ;
426
452
if (windowPositionValid ) {
427
453
String locationStr = Preferences .get ("last.sketch" + i + ".location" );
@@ -460,6 +486,11 @@ protected void storeSketches() {
460
486
!editor .getSketch ().isModified ()) {
461
487
continue ;
462
488
}
489
+ if (portableFolder != null ) {
490
+ path = RelativePath .relativePath (portableFolder .toString (), path );
491
+ if (path == null )
492
+ continue ;
493
+ }
463
494
Preferences .set ("last.sketch" + index + ".path" , path );
464
495
465
496
int [] location = editor .getPlacement ();
@@ -478,6 +509,11 @@ protected void storeSketchPath(Editor editor, int index) {
478
509
String untitledPath = untitledFolder .getAbsolutePath ();
479
510
if (path .startsWith (untitledPath )) {
480
511
path = "" ;
512
+ } else
513
+ if (portableFolder != null ) {
514
+ path = RelativePath .relativePath (portableFolder .toString (), path );
515
+ if (path == null )
516
+ path = "" ;
481
517
}
482
518
Preferences .set ("last.sketch" + index + ".path" , path );
483
519
}
@@ -1759,6 +1795,9 @@ static public boolean isLinux() {
1759
1795
1760
1796
1761
1797
static public File getSettingsFolder () {
1798
+ if (portableFolder != null )
1799
+ return portableFolder ;
1800
+
1762
1801
File settingsFolder = null ;
1763
1802
1764
1803
String preferencesPath = Preferences .get ("settings.path" );
@@ -1938,7 +1977,19 @@ static public Map<String, String> getBoardPreferences() {
1938
1977
return boardPreferences ;
1939
1978
}
1940
1979
1980
+ static public File getPortableFolder () {
1981
+ return portableFolder ;
1982
+ }
1983
+
1984
+
1985
+ static public String getPortableSketchbookFolder () {
1986
+ return portableSketchbookFolder ;
1987
+ }
1988
+
1989
+
1941
1990
static public File getSketchbookFolder () {
1991
+ if (portableFolder != null )
1992
+ return new File (portableFolder , Preferences .get ("sketchbook.path" ));
1942
1993
return new File (Preferences .get ("sketchbook.path" ));
1943
1994
}
1944
1995
@@ -1971,6 +2022,9 @@ static public File getSketchbookHardwareFolder() {
1971
2022
1972
2023
1973
2024
protected File getDefaultSketchbookFolder () {
2025
+ if (portableFolder != null )
2026
+ return new File (portableFolder , portableSketchbookFolder );
2027
+
1974
2028
File sketchbookFolder = null ;
1975
2029
try {
1976
2030
sketchbookFolder = platform .getDefaultSketchbookFolder ();
0 commit comments