Skip to content

Commit 52fd384

Browse files
author
Federico Fissore
committed
Working on how sketches are marked readonly.
Initial refactorings: stopped using static members (static is evil)
1 parent cb292d6 commit 52fd384

File tree

4 files changed

+23
-32
lines changed

4 files changed

+23
-32
lines changed

Diff for: app/src/processing/app/Base.java

-4
Original file line numberDiff line numberDiff line change
@@ -1772,10 +1772,6 @@ public List<JMenu> getBoardsCustomMenus() {
17721772
return boardsCustomMenus;
17731773
}
17741774

1775-
static public String getSketchbookLibrariesPath() {
1776-
return BaseNoGui.getSketchbookLibrariesFolder().getAbsolutePath();
1777-
}
1778-
17791775
public File getDefaultSketchbookFolderOrPromptForIt() {
17801776

17811777
File sketchbookFolder = BaseNoGui.getDefaultSketchbookFolder();

Diff for: app/src/processing/app/Editor.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ private static class ShouldSaveIfModified implements Predicate<Sketch> {
9393

9494
@Override
9595
public boolean test(Sketch sketch) {
96-
return PreferencesData.getBoolean("editor.save_on_verify") && sketch.isModified() && !sketch.isReadOnly();
96+
return PreferencesData.getBoolean("editor.save_on_verify") && sketch.isModified() && !sketch.isReadOnly(BaseNoGui.getLibrariesPath(), BaseNoGui.getExamplesPath());
9797
}
9898
}
9999

100100
private static class ShouldSaveReadOnly implements Predicate<Sketch> {
101101

102102
@Override
103103
public boolean test(Sketch sketch) {
104-
return sketch.isReadOnly();
104+
return sketch.isReadOnly(BaseNoGui.getLibrariesPath(), BaseNoGui.getExamplesPath());
105105
}
106106
}
107107

@@ -2282,7 +2282,7 @@ private boolean handleSave2() {
22822282
statusNotice(tr("Saving..."));
22832283
boolean saved = false;
22842284
try {
2285-
boolean wasReadOnly = sketch.isReadOnly();
2285+
boolean wasReadOnly = sketch.isReadOnly(BaseNoGui.getLibrariesPath(), BaseNoGui.getExamplesPath());
22862286
String previousMainFilePath = sketch.getMainFilePath();
22872287
saved = sketch.save();
22882288
if (saved) {
@@ -2395,7 +2395,7 @@ private boolean serialPrompt() {
23952395
*/
23962396
synchronized public void handleExport(final boolean usingProgrammer) {
23972397
if (PreferencesData.getBoolean("editor.save_on_verify")) {
2398-
if (sketch.isModified() && !sketch.isReadOnly()) {
2398+
if (sketch.isModified() && !sketch.isReadOnly(BaseNoGui.getLibrariesPath(), BaseNoGui.getExamplesPath())) {
23992399
handleSave(true);
24002400
}
24012401
}

Diff for: app/src/processing/app/Sketch.java

+19-17
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void handleNewCode() {
133133
ensureExistence();
134134

135135
// if read-only, give an error
136-
if (isReadOnly()) {
136+
if (isReadOnly(BaseNoGui.getLibrariesPath(), BaseNoGui.getExamplesPath())) {
137137
// if the files are read-only, need to first do a "save as".
138138
Base.showMessage(tr("Sketch is Read-Only"),
139139
tr("Some files are marked \"read-only\", so you'll\n" +
@@ -162,7 +162,7 @@ public void handleRenameCode() {
162162
}
163163

164164
// if read-only, give an error
165-
if (isReadOnly()) {
165+
if (isReadOnly(BaseNoGui.getLibrariesPath(), BaseNoGui.getExamplesPath())) {
166166
// if the files are read-only, need to first do a "save as".
167167
Base.showMessage(tr("Sketch is Read-Only"),
168168
tr("Some files are marked \"read-only\", so you'll\n" +
@@ -432,7 +432,7 @@ public void handleDeleteCode() {
432432
ensureExistence();
433433

434434
// if read-only, give an error
435-
if (isReadOnly()) {
435+
if (isReadOnly(BaseNoGui.getLibrariesPath(), BaseNoGui.getExamplesPath())) {
436436
// if the files are read-only, need to first do a "save as".
437437
Base.showMessage(tr("Sketch is Read-Only"),
438438
tr("Some files are marked \"read-only\", so you'll\n" +
@@ -558,7 +558,7 @@ public boolean save() throws IOException {
558558
// don't do anything if not actually modified
559559
//if (!modified) return false;
560560

561-
if (isReadOnly()) {
561+
if (isReadOnly(BaseNoGui.getLibrariesPath(), BaseNoGui.getExamplesPath())) {
562562
// if the files are read-only, need to first do a "save as".
563563
Base.showMessage(tr("Sketch is read-only"),
564564
tr("Some files are marked \"read-only\", so you'll\n" +
@@ -637,7 +637,7 @@ private boolean renameCodeToInoExtension(File pdeFile) {
637637
protected boolean saveAs() throws IOException {
638638
// get new name for folder
639639
FileDialog fd = new FileDialog(editor, tr("Save sketch folder as..."), FileDialog.SAVE);
640-
if (isReadOnly() || isUntitled()) {
640+
if (isReadOnly(BaseNoGui.getLibrariesPath(), BaseNoGui.getExamplesPath()) || isUntitled()) {
641641
// default to the sketchbook folder
642642
fd.setDirectory(BaseNoGui.getSketchbookFolder().getAbsolutePath());
643643
} else {
@@ -772,7 +772,7 @@ public void handleAddFile() {
772772
ensureExistence();
773773

774774
// if read-only, give an error
775-
if (isReadOnly()) {
775+
if (isReadOnly(BaseNoGui.getLibrariesPath(), BaseNoGui.getExamplesPath())) {
776776
// if the files are read-only, need to first do a "save as".
777777
Base.showMessage(tr("Sketch is Read-Only"),
778778
tr("Some files are marked \"read-only\", so you'll\n" +
@@ -1223,25 +1223,27 @@ private void ensureExistence() {
12231223
* Returns true if this is a read-only sketch. Used for the
12241224
* examples directory, or when sketches are loaded from read-only
12251225
* volumes or folders without appropriate permissions.
1226+
* @param librariesPaths
1227+
* @param examplesPath
12261228
*/
1227-
public boolean isReadOnly() {
1229+
public boolean isReadOnly(List<File> librariesPaths, String examplesPath) {
12281230
String apath = data.getFolder().getAbsolutePath();
1229-
for (File folder : BaseNoGui.getLibrariesPath()) {
1230-
if (apath.startsWith(folder.getAbsolutePath()))
1231+
for (File folder : librariesPaths) {
1232+
if (apath.startsWith(folder.getAbsolutePath())) {
12311233
return true;
1232-
}
1233-
if (apath.startsWith(BaseNoGui.getExamplesPath()) ||
1234-
apath.startsWith(Base.getSketchbookLibrariesPath())) {
1235-
return true;
1234+
}
12361235
}
12371236

1238-
// canWrite() doesn't work on directories
1239-
// } else if (!folder.canWrite()) {
1237+
return sketchIsSystemExample(apath, examplesPath) || sketchFilesAreReadOnly();
1238+
}
1239+
1240+
private boolean sketchIsSystemExample(String apath, String examplesPath) {
1241+
return apath.startsWith(examplesPath);
1242+
}
12401243

1241-
// check to see if each modified code file can be written to
1244+
private boolean sketchFilesAreReadOnly() {
12421245
for (SketchCode code : data.getCodes()) {
12431246
if (code.isModified() && code.fileReadOnly() && code.fileExists()) {
1244-
// System.err.println("found a read-only file " + code[i].file);
12451247
return true;
12461248
}
12471249
}

Diff for: arduino-core/src/processing/app/BaseNoGui.java

-7
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ public class BaseNoGui {
6969
// maps #included files to their library folder
7070
public static Map<String, LibraryList> importToLibraryTable;
7171

72-
// maps library name to their library folder
73-
static private LibraryList libraries;
74-
7572
// XXX: Remove this field
7673
static private List<File> librariesFolders;
7774

@@ -237,10 +234,6 @@ static public String getHardwarePath() {
237234
return getHardwareFolder().getAbsolutePath();
238235
}
239236

240-
static public LibraryList getLibraries() {
241-
return libraries;
242-
}
243-
244237
static public List<File> getLibrariesPath() {
245238
return librariesFolders;
246239
}

0 commit comments

Comments
 (0)