From 932fd461c8117fea76170ae59ee3ed0f6cea637d Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Tue, 24 Dec 2024 17:11:04 +0000 Subject: [PATCH 1/5] Check whether new project is parent or child of existing one --- src/FolderManager/FileView.vala | 51 ++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/src/FolderManager/FileView.vala b/src/FolderManager/FileView.vala index c9efd85bb..a594c0797 100644 --- a/src/FolderManager/FileView.vala +++ b/src/FolderManager/FileView.vala @@ -514,25 +514,44 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane return; } - var folder_root = new ProjectFolderItem (folder, this); // Constructor adds project to GitManager - this.root.add (folder_root); - rename_items_with_same_name (folder_root); - - folder_root.expanded = expand; - folder_root.closed.connect (() => { - toplevel_action_group.activate_action (MainWindow.ACTION_CLOSE_PROJECT_DOCS, new Variant.string (folder_root.path)); - root.remove (folder_root); - foreach (var child in root.children) { - var child_folder = (ProjectFolderItem) child; - if (child_folder.name != child_folder.file.name) { - rename_items_with_same_name (child_folder); - } + var add_file = folder.file; + // Need to deal with case where folder is parent or child of an existing project + foreach (var child in root.children) { + var item = (ProjectFolderItem) child; + if (add_file.get_relative_path (item.file.file) != null) { + critical ("Trying to add parent of existing project"); + item.closed (); + } else if (item.file.file.get_relative_path (add_file) != null) { + critical ("Trying to add child of existing project"); + return; } - Scratch.Services.GitManager.get_instance ().remove_project (folder_root); + } + + //TODO Throw warning dialog? + + // Process any closed signals emitted before proceeding + Idle.add (() => { + var folder_root = new ProjectFolderItem (folder, this); // Constructor adds project to GitManager + this.root.add (folder_root); + rename_items_with_same_name (folder_root); + + folder_root.expanded = expand; + folder_root.closed.connect (() => { + toplevel_action_group.activate_action (MainWindow.ACTION_CLOSE_PROJECT_DOCS, new Variant.string (folder_root.path)); + root.remove (folder_root); + foreach (var child in root.children) { + var child_folder = (ProjectFolderItem) child; + if (child_folder.name != child_folder.file.name) { + rename_items_with_same_name (child_folder); + } + } + Scratch.Services.GitManager.get_instance ().remove_project (folder_root); + write_settings (); + }); + write_settings (); + return Source.REMOVE; }); - - write_settings (); } private bool is_open (File folder) { From c421b022a683a9050cafaddf762ba29125674604 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Tue, 24 Dec 2024 17:52:24 +0000 Subject: [PATCH 2/5] Dialog before close parent/child projects --- .../CloseProjectsConfirmationDialog.vala | 39 +++++++++++++++++++ src/FolderManager/FileView.vala | 34 ++++++++++++++-- src/meson.build | 1 + 3 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 src/Dialogs/CloseProjectsConfirmationDialog.vala diff --git a/src/Dialogs/CloseProjectsConfirmationDialog.vala b/src/Dialogs/CloseProjectsConfirmationDialog.vala new file mode 100644 index 000000000..c0bc85ba8 --- /dev/null +++ b/src/Dialogs/CloseProjectsConfirmationDialog.vala @@ -0,0 +1,39 @@ +/* +* Copyright 2011-2019 elementary, Inc. (https://elementary.io) +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License version 3 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this program; if not, write to the +* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +* Boston, MA 02110-1301 USA +*/ + +public class Scratch.Dialogs.CloseProjectsConfirmationDialog : Granite.MessageDialog { + + public CloseProjectsConfirmationDialog (MainWindow parent) { + Object ( + buttons: Gtk.ButtonsType.NONE, + transient_for: parent + ); + } + + construct { + image_icon = new ThemedIcon ("dialog-warning"); + + primary_text = _("This folder is a parent or child of an existing open project"); + secondary_text = _("Opening this folder will close all parent or child projects"); + + add_button (_("Don't Open"), Gtk.ResponseType.REJECT); + + var ignore_button = (Gtk.Button) add_button (_("Close Other Projects"), Gtk.ResponseType.ACCEPT); + ignore_button.get_style_context ().add_class (Gtk.STYLE_CLASS_DESTRUCTIVE_ACTION); + } +} diff --git a/src/FolderManager/FileView.vala b/src/FolderManager/FileView.vala index a594c0797..8b34d1470 100644 --- a/src/FolderManager/FileView.vala +++ b/src/FolderManager/FileView.vala @@ -516,18 +516,46 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane var add_file = folder.file; // Need to deal with case where folder is parent or child of an existing project + var parents = new List (); + var children = new List (); + foreach (var child in root.children) { var item = (ProjectFolderItem) child; if (add_file.get_relative_path (item.file.file) != null) { critical ("Trying to add parent of existing project"); - item.closed (); + children.append (item); } else if (item.file.file.get_relative_path (add_file) != null) { critical ("Trying to add child of existing project"); - return; + parents.append (item); } } - //TODO Throw warning dialog? + if (parents.length () > 0 || children.length () > 0) { + var dialog = new Scratch.Dialogs.CloseProjectsConfirmationDialog ((MainWindow) get_toplevel ()); + + var close_projects = false; + dialog.response.connect ((res) => { + dialog.destroy (); + if (res == Gtk.ResponseType.ACCEPT) { + close_projects = true; + } + }); + + dialog.run (); + + if (close_projects) { + foreach (var item in parents) { + item.closed (); + } + + foreach (var item in children) { + item.closed (); + } + } else { + return; + } + + } // Process any closed signals emitted before proceeding Idle.add (() => { diff --git a/src/meson.build b/src/meson.build index 08497a110..b9778607f 100644 --- a/src/meson.build +++ b/src/meson.build @@ -20,6 +20,7 @@ code_files = files( 'Utils.vala', 'Dialogs/PreferencesDialog.vala', 'Dialogs/RestoreConfirmationDialog.vala', + 'Dialogs/CloseProjectsConfirmationDialog.vala', 'Dialogs/GlobalSearchDialog.vala', 'Dialogs/NewBranchDialog.vala', 'FolderManager/File.vala', From 20e4df2ebf91071e19edc76bb3c7d06bcba8980d Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Tue, 24 Dec 2024 18:33:36 +0000 Subject: [PATCH 3/5] Use ngettext for dialog text --- .../CloseProjectsConfirmationDialog.vala | 38 ++++++++++++++++--- src/FolderManager/FileView.vala | 13 +++++-- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/Dialogs/CloseProjectsConfirmationDialog.vala b/src/Dialogs/CloseProjectsConfirmationDialog.vala index c0bc85ba8..28ac4b5d4 100644 --- a/src/Dialogs/CloseProjectsConfirmationDialog.vala +++ b/src/Dialogs/CloseProjectsConfirmationDialog.vala @@ -18,22 +18,50 @@ public class Scratch.Dialogs.CloseProjectsConfirmationDialog : Granite.MessageDialog { - public CloseProjectsConfirmationDialog (MainWindow parent) { + public uint n_parents { get; construct; } + public uint n_children { get; construct; } + + public CloseProjectsConfirmationDialog (MainWindow parent, uint n_parents, uint n_children) { Object ( buttons: Gtk.ButtonsType.NONE, - transient_for: parent + transient_for: parent, + n_parents: n_parents, + n_children: n_children ); } construct { image_icon = new ThemedIcon ("dialog-warning"); + var button_label = ""; + // We can assume that either n_parents or n_children is zero (but not both). + // We can assume n_parents is either zero or one + if (n_children > 0) { + primary_text = ngettext ( + _("This folder is the parent of an open project"), + _("This folder is the parent of %u open projects").printf (n_children), + (ulong) n_children + ); + ; + secondary_text = ngettext ( + _("Opening this folder will close the child project"), + _("Opening this folder will close all child projects"), + (ulong) n_children + ); - primary_text = _("This folder is a parent or child of an existing open project"); - secondary_text = _("Opening this folder will close all parent or child projects"); + button_label = ngettext ( + _("Close Child Project"), + _("Close Child Projects"), + (ulong) n_children + ); + } else { + primary_text = _("This folder is a child of an open project"); + secondary_text = _("Opening this folder will close the parent project"); + button_label = _("Close Parent Project"); + } add_button (_("Don't Open"), Gtk.ResponseType.REJECT); - var ignore_button = (Gtk.Button) add_button (_("Close Other Projects"), Gtk.ResponseType.ACCEPT); + var ignore_button = (Gtk.Button) add_button (button_label, Gtk.ResponseType.ACCEPT); ignore_button.get_style_context ().add_class (Gtk.STYLE_CLASS_DESTRUCTIVE_ACTION); } } diff --git a/src/FolderManager/FileView.vala b/src/FolderManager/FileView.vala index 8b34d1470..49fed1bdf 100644 --- a/src/FolderManager/FileView.vala +++ b/src/FolderManager/FileView.vala @@ -522,16 +522,22 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane foreach (var child in root.children) { var item = (ProjectFolderItem) child; if (add_file.get_relative_path (item.file.file) != null) { - critical ("Trying to add parent of existing project"); + debug ("Trying to add parent of existing project"); children.append (item); } else if (item.file.file.get_relative_path (add_file) != null) { - critical ("Trying to add child of existing project"); + debug ("Trying to add child of existing project"); parents.append (item); } } if (parents.length () > 0 || children.length () > 0) { - var dialog = new Scratch.Dialogs.CloseProjectsConfirmationDialog ((MainWindow) get_toplevel ()); + assert (parents.length () <= 1); + assert (parents.length () == 0 || children.length () == 0); + var dialog = new Scratch.Dialogs.CloseProjectsConfirmationDialog ( + (MainWindow) get_toplevel (), + parents.length (), + children.length () + ); var close_projects = false; dialog.response.connect ((res) => { @@ -554,7 +560,6 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane } else { return; } - } // Process any closed signals emitted before proceeding From 1653d7ce70fa8e38cb86e24decc0f4f3346b697a Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Wed, 25 Dec 2024 10:51:43 +0000 Subject: [PATCH 4/5] Update POTFILES --- po/POTFILES | 1 + 1 file changed, 1 insertion(+) diff --git a/po/POTFILES b/po/POTFILES index 5b5c70006..044533f59 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -5,6 +5,7 @@ src/Dialogs/GlobalSearchDialog.vala src/Dialogs/NewBranchDialog.vala src/Dialogs/PreferencesDialog.vala src/Dialogs/RestoreConfirmationDialog.vala +src/Dialogs/CloseProjectsConfirmationDialog.vala src/FolderManager/File.vala src/FolderManager/FileItem.vala src/FolderManager/FileView.vala From c914f65ccffd839bb9f652818736c67addc25939 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Wed, 25 Dec 2024 10:53:43 +0000 Subject: [PATCH 5/5] Update copyright date on changed files --- src/Dialogs/CloseProjectsConfirmationDialog.vala | 2 +- src/FolderManager/FileView.vala | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Dialogs/CloseProjectsConfirmationDialog.vala b/src/Dialogs/CloseProjectsConfirmationDialog.vala index 28ac4b5d4..fde88a5a0 100644 --- a/src/Dialogs/CloseProjectsConfirmationDialog.vala +++ b/src/Dialogs/CloseProjectsConfirmationDialog.vala @@ -1,5 +1,5 @@ /* -* Copyright 2011-2019 elementary, Inc. (https://elementary.io) +* Copyright 2024 elementary, Inc. (https://elementary.io) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public diff --git a/src/FolderManager/FileView.vala b/src/FolderManager/FileView.vala index 49fed1bdf..98aaf3574 100644 --- a/src/FolderManager/FileView.vala +++ b/src/FolderManager/FileView.vala @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2017 - 2022 elementary LLC. (https://elementary.io), + * Copyright (c) 2017 - 2024 elementary LLC. (https://elementary.io), * 2013 Julien Spautz * * This program is free software: you can redistribute it and/or modify