Skip to content

Commit 38151d6

Browse files
committed
Only apply folder rename if the original colder contains proper file name
1 parent f33d47c commit 38151d6

File tree

1 file changed

+34
-50
lines changed

1 file changed

+34
-50
lines changed

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

+34-50
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,7 @@ protected boolean handleOpenInternal(File sketchFile) {
17531753

17541754
} else {
17551755
String properParent = fileName.substring(0, fileName.length() - 4);
1756+
File properFolder;
17561757

17571758
Object[] options = {tr("OK"), tr("Cancel")};
17581759
String prompt = I18n.format(tr("The file \"{0}\" needs to be inside\n" +
@@ -1761,40 +1762,51 @@ protected boolean handleOpenInternal(File sketchFile) {
17611762
fileName,
17621763
properParent);
17631764

1764-
int result = JOptionPane.showOptionDialog(this, prompt, tr("Moving"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
1765+
properFolder = new File(sketchFile.getParent(), properParent);
17651766

1766-
if (result != JOptionPane.YES_OPTION) {
1767-
return false;
1768-
}
1767+
int result;
1768+
if (parentFolderContainsSketchName(sketchFile, properParent)) {
17691769

1770-
// create properly named folder
1771-
File properFolder;
1772-
if (onlyContainsSketchFiles(sketchFile.getParent())) {
1773-
// Move the whole folder, so properFolder needs to be created one level above
1770+
// properFolder needs to be created one level above
17741771
properFolder = new File(new File(sketchFile.getParent()).getParent(), properParent);
1772+
1773+
// ask for different confirmation
1774+
prompt = I18n.format(tr("The file \"{0}\" needs to be inside\n" +
1775+
"a sketch folder named \"{1}\".\n" +
1776+
"Renaming folder \"{2}\" into \"{3}\"\n" +
1777+
"Continue?"),
1778+
fileName,
1779+
properParent,
1780+
sketchFile.getParent(),
1781+
properFolder);
1782+
1783+
result = JOptionPane.showOptionDialog(this, prompt, tr("Moving"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
17751784
} else {
1776-
properFolder = new File(sketchFile.getParent(), properParent);
1785+
result = JOptionPane.showOptionDialog(this, prompt, tr("Moving"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
1786+
}
1787+
1788+
if (result != JOptionPane.YES_OPTION) {
1789+
return false;
17771790
}
17781791

17791792
if (properFolder.exists()) {
17801793
Base.showWarning(tr("Error"), I18n.format(tr("A folder named \"{0}\" already exists. " +
17811794
"Can't open sketch."), properParent), null);
17821795
return false;
17831796
}
1784-
if (!properFolder.mkdirs()) {
1785-
//throw new IOException("Couldn't create sketch folder");
1786-
Base.showWarning(tr("Error"), tr("Could not create the sketch folder."), null);
1787-
return false;
1788-
}
1797+
17891798
// copy the sketch inside
17901799
File properPdeFile = new File(properFolder, sketchFile.getName());
17911800
try {
1792-
if (onlyContainsSketchFiles(sketchFile.getParent())) {
1801+
if (parentFolderContainsSketchName(sketchFile, properParent)) {
17931802
File dir = new File(sketchFile.getParent());
1794-
FileUtils.copy(dir, properFolder);
1795-
// remove the original folder, so user doesn't get confused
1796-
FileUtils.recursiveDelete(dir);
1803+
dir.renameTo(properFolder);
17971804
} else {
1805+
if (!properFolder.mkdirs()) {
1806+
//throw new IOException("Couldn't create sketch folder");
1807+
Base.showWarning(tr("Error"), tr("Could not create the sketch folder."), null);
1808+
return false;
1809+
}
17981810
Base.copyFile(sketchFile, properPdeFile);
17991811
// remove the original file, so user doesn't get confused
18001812
sketchFile.delete();
@@ -1826,38 +1838,10 @@ protected boolean handleOpenInternal(File sketchFile) {
18261838
return true;
18271839
}
18281840

1829-
private boolean allowedExtension(String fileName, String[] validExtensions) {
1830-
int i = fileName.lastIndexOf('.');
1831-
if (i > 0) {
1832-
String ext = fileName.substring(i+1).toLowerCase();
1833-
if (!Arrays.asList(validExtensions).contains(ext)) {
1834-
return false;
1835-
}
1836-
}
1837-
// no extension or valid extension
1838-
return true;
1839-
}
1840-
1841-
private boolean onlyContainsSketchFiles(String path) {
1842-
File folder = new File(path);
1843-
File[] listOfFiles = folder.listFiles();
1844-
1845-
for (int i = 0; i < listOfFiles.length; i++) {
1846-
String name = listOfFiles[i].getName();
1847-
if (listOfFiles[i].isFile()) {
1848-
// allowed files extensions are only .ino, .h, .hpp, .c, .cpp (all cases)
1849-
String[] valid = {"ino", "h", "hpp" , "c" , "cpp", "pde"};
1850-
if (!allowedExtension(name, valid)) {
1851-
return false;
1852-
}
1853-
} else if (listOfFiles[i].isDirectory()) {
1854-
// allowed dir names are only src and extras , plus source control folders
1855-
if (name != "src" && name != "extras" && !FileUtils.SOURCE_CONTROL_FOLDERS.contains(name)) {
1856-
return false;
1857-
}
1858-
}
1859-
}
1860-
return true;
1841+
private boolean parentFolderContainsSketchName(File sketchFile, String sketchName) {
1842+
String dir = sketchFile.getParent().toLowerCase();
1843+
System.out.println(dir + " " + sketchName);
1844+
return dir.contains(sketchName.toLowerCase());
18611845
}
18621846

18631847
public void updateTitle() {

0 commit comments

Comments
 (0)