Parcourir la source

tools: warn if the file already exists or it is outside the source directory

Daniele Bartolini il y a 5 ans
Parent
commit
121484046d
2 fichiers modifiés avec 47 ajouts et 17 suppressions
  1. 1 0
      docs/changelog.rst
  2. 46 17
      tools/level_editor/level_editor.vala

+ 1 - 0
docs/changelog.rst

@@ -36,6 +36,7 @@ Changelog
 * Level Editor now saves aggregate logs to disk. User can browse logs folder from Help > Browse Logs...
 * Level Editor now saves aggregate logs to disk. User can browse logs folder from Help > Browse Logs...
 * New Project dialog no longer allows selecting non-empty folders for new projects
 * New Project dialog no longer allows selecting non-empty folders for new projects
 * Objects inside .level files are now ordered by their ID before serialization
 * Objects inside .level files are now ordered by their ID before serialization
+* Save Level dialog now warns before overwriting a file that already exists
 * Unified Engine and Run menubar items into a single Debug menubar item
 * Unified Engine and Run menubar items into a single Debug menubar item
 
 
 0.38.0
 0.38.0

+ 46 - 17
tools/level_editor/level_editor.vala

@@ -1243,13 +1243,9 @@ public class LevelEditorApplication : Gtk.Application
 
 
 	private bool save_as(string? filename)
 	private bool save_as(string? filename)
 	{
 	{
-		string path;
+		string path = filename;
 
 
-		if (filename != null)
-		{
-			path = filename;
-		}
-		else
+		if (path == null)
 		{
 		{
 			Gtk.FileChooserDialog fcd = new Gtk.FileChooserDialog("Save As..."
 			Gtk.FileChooserDialog fcd = new Gtk.FileChooserDialog("Save As..."
 				, this.active_window
 				, this.active_window
@@ -1261,24 +1257,57 @@ public class LevelEditorApplication : Gtk.Application
 				);
 				);
 			fcd.add_filter(_file_filter);
 			fcd.add_filter(_file_filter);
 			fcd.set_current_folder(_project.source_dir());
 			fcd.set_current_folder(_project.source_dir());
-			int rt = fcd.run();
 
 
-			if (rt != ResponseType.ACCEPT)
+			int rt = ResponseType.CANCEL;
+			do
 			{
 			{
-				fcd.destroy();
-				return false;
+				// Select the file
+				rt = fcd.run();
+				if (rt != ResponseType.ACCEPT)
+				{
+					fcd.destroy();
+					return false;
+				}
+				path = fcd.get_filename();
+
+				// Check if the file is within the source directory
+				if (!_project.path_is_within_dir(path, _project.source_dir()))
+				{
+					Gtk.MessageDialog md = new Gtk.MessageDialog(fcd
+						, DialogFlags.MODAL
+						, MessageType.WARNING
+						, Gtk.ButtonsType.OK
+						, "The file must be within the source directory."
+						);
+					md.set_default_response(ResponseType.OK);
+
+					md.run();
+					md.destroy();
+					fcd.set_current_folder(_project.source_dir());
+					continue;
+				}
+
+				// Check if the file already exists
+				rt = ResponseType.YES;
+				if (GLib.FileUtils.test(path, FileTest.EXISTS))
+				{
+					Gtk.MessageDialog md = new Gtk.MessageDialog(fcd
+						, DialogFlags.MODAL
+						, MessageType.QUESTION
+						, Gtk.ButtonsType.YES_NO
+						, "A file named `%s` already exists.\nOverwrite?".printf(_project.basename(path))
+						);
+					md.set_default_response(ResponseType.NO);
+
+					rt = md.run();
+					md.destroy();
+				}
 			}
 			}
+			while (rt != ResponseType.YES);
 
 
-			path = fcd.get_filename();
 			fcd.destroy();
 			fcd.destroy();
 		}
 		}
 
 
-		if (!_project.path_is_within_dir(path, _project.source_dir()))
-		{
-			loge("File must be within `%s`".printf(_project.source_dir()));
-			return false;
-		}
-
 		_level.save(path.has_suffix(".level") ? path : path + ".level");
 		_level.save(path.has_suffix(".level") ? path : path + ".level");
 		_statusbar.set_temporary_message("Saved %s".printf(_level._filename));
 		_statusbar.set_temporary_message("Saved %s".printf(_level._filename));
 		return true;
 		return true;