[baobab] Chart: synchronize popup menu with treeview one



commit 73a99f591683830de32cfaba0f7a01b9993c9036
Author: Stefano Facchini <stefano facchini gmail com>
Date:   Sat Jul 20 14:28:35 2013 +0200

    Chart: synchronize popup menu with treeview one
    
    https://bugzilla.gnome.org/show_bug.cgi?id=620216

 src/baobab-chart.vala  |   27 +++++++++++++++++++-
 src/baobab-menu.ui     |   14 ++++++++++
 src/baobab-window.vala |   66 ++++++++++++++++++++++++++++-------------------
 3 files changed, 79 insertions(+), 28 deletions(-)
---
diff --git a/src/baobab-chart.vala b/src/baobab-chart.vala
index 41cb0fb..682dfc4 100644
--- a/src/baobab-chart.vala
+++ b/src/baobab-chart.vala
@@ -205,6 +205,9 @@ namespace Baobab {
         SimpleActionGroup action_group;
 
         const ActionEntry[] action_entries = {
+            { "open-file", open_file },
+            { "copy-path", copy_path },
+            { "trash-file", trash_file },
             { "move-up", move_up_root },
             { "zoom-in", zoom_in },
             { "zoom-out", zoom_out }
@@ -279,7 +282,9 @@ namespace Baobab {
         }
 
         public override bool leave_notify_event (Gdk.EventCrossing event) {
-            highlighted_item = null;
+            if (!context_menu.visible) {
+                highlighted_item = null;
+            }
 
             return false;
         }
@@ -554,6 +559,18 @@ namespace Baobab {
             return false;
         }
 
+        public void open_file () {
+            (get_toplevel () as Window).open_item (highlighted_item.iter);
+        }
+
+        public void copy_path () {
+            (get_toplevel () as Window).copy_path (highlighted_item.iter);
+        }
+
+        public void trash_file () {
+            (get_toplevel () as Window).trash_file (highlighted_item.iter);
+        }
+
         public void move_up_root () {
             Gtk.TreeIter iter, parent_iter;
 
@@ -596,6 +613,14 @@ namespace Baobab {
         void show_popup_menu (Gdk.EventButton? event) {
             ensure_context_menu ();
 
+            var enable = highlighted_item != null;
+            var action = action_group.lookup_action ("open-file") as SimpleAction;
+            action.set_enabled (enable);
+            action = action_group.lookup_action ("copy-path") as SimpleAction;
+            action.set_enabled (enable);
+            action = action_group.lookup_action ("trash-file") as SimpleAction;
+            action.set_enabled (enable);
+
             if (event != null) {
                 context_menu.popup (null, null, null, event.button, event.time);
             } else {
diff --git a/src/baobab-menu.ui b/src/baobab-menu.ui
index e1426a0..1a5302b 100644
--- a/src/baobab-menu.ui
+++ b/src/baobab-menu.ui
@@ -24,6 +24,20 @@
   <menu id="chartmenu">
     <section>
       <item>
+        <attribute name="label" translatable="yes">_Open Folder</attribute>
+        <attribute name="action">chart.open-file</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">_Copy Path to Clipboard</attribute>
+        <attribute name="action">chart.copy-path</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">Mo_ve to Trash</attribute>
+        <attribute name="action">chart.trash-file</attribute>
+      </item>
+    </section>
+    <section>
+      <item>
         <attribute name="label" translatable="yes">_Move to parent folder</attribute>
         <attribute name="action">chart.move-up</attribute>
       </item>
diff --git a/src/baobab-window.vala b/src/baobab-window.vala
index adc9de9..bea02c0 100644
--- a/src/baobab-window.vala
+++ b/src/baobab-window.vala
@@ -388,6 +388,42 @@ namespace Baobab {
             return true;
         }
 
+        public void open_item (Gtk.TreeIter iter) {
+            string parse_name;
+            active_location.scanner.get (iter, Scanner.Columns.PARSE_NAME, out parse_name);
+            var file = File.parse_name (parse_name);
+            try {
+                var info = file.query_info (FileAttribute.STANDARD_CONTENT_TYPE, 0, null);
+                var content = info.get_content_type ();
+                var appinfo = AppInfo.get_default_for_type (content, true);
+                var files = new List<File>();
+                files.append (file);
+                appinfo.launch(files, null);
+            } catch (Error e) {
+                message (_("Failed to open file"), e.message, Gtk.MessageType.ERROR);
+            }
+        }
+
+        public void copy_path (Gtk.TreeIter iter) {
+            string parse_name;
+            active_location.scanner.get (iter, Scanner.Columns.PARSE_NAME, out parse_name);
+            var clipboard = Gtk.Clipboard.get (Gdk.SELECTION_CLIPBOARD);
+            clipboard.set_text (parse_name, -1);
+            clipboard.store ();
+        }
+
+        public void trash_file (Gtk.TreeIter iter) {
+            string parse_name;
+            active_location.scanner.get (iter, Scanner.Columns.PARSE_NAME, out parse_name);
+            var file = File.parse_name (parse_name);
+            try {
+                file.trash ();
+                active_location.scanner.remove (ref iter);
+            } catch (Error e) {
+                message (_("Failed to move file to the trash"), e.message, Gtk.MessageType.ERROR);
+            }
+        }
+
         void setup_treeview () {
             treeview.button_press_event.connect ((event) => {
                 if (((Gdk.Event) (&event)).triggers_context_menu ()) {
@@ -405,19 +441,7 @@ namespace Baobab {
                 var selection = treeview.get_selection ();
                 Gtk.TreeIter iter;
                 if (selection.get_selected (null, out iter)) {
-                    string parse_name;
-                    active_location.scanner.get (iter, Scanner.Columns.PARSE_NAME, out parse_name);
-                    var file = File.parse_name (parse_name);
-                    try {
-                        var info = file.query_info (FileAttribute.STANDARD_CONTENT_TYPE, 0, null);
-                        var content = info.get_content_type ();
-                        var appinfo = AppInfo.get_default_for_type (content, true);
-                        var files = new List<File>();
-                        files.append (file);
-                        appinfo.launch(files, null);
-                    } catch (Error e) {
-                        message (_("Failed to open file"), e.message, Gtk.MessageType.ERROR);
-                    }
+                    open_item (iter);
                 }
             });
 
@@ -425,11 +449,7 @@ namespace Baobab {
                 var selection = treeview.get_selection ();
                 Gtk.TreeIter iter;
                 if (selection.get_selected (null, out iter)) {
-                    string parse_name;
-                    active_location.scanner.get (iter, Scanner.Columns.PARSE_NAME, out parse_name);
-                    var clipboard = Gtk.Clipboard.get (Gdk.SELECTION_CLIPBOARD);
-                    clipboard.set_text (parse_name, -1);
-                    clipboard.store ();
+                    copy_path (iter);
                 }
             });
 
@@ -437,15 +457,7 @@ namespace Baobab {
                 var selection = treeview.get_selection ();
                 Gtk.TreeIter iter;
                 if (selection.get_selected (null, out iter)) {
-                    string parse_name;
-                    active_location.scanner.get (iter, Scanner.Columns.PARSE_NAME, out parse_name);
-                    var file = File.parse_name (parse_name);
-                    try {
-                        file.trash ();
-                        active_location.scanner.remove (ref iter);
-                    } catch (Error e) {
-                        message (_("Failed to move file to the trash"), e.message, Gtk.MessageType.ERROR);
-                    }
+                    trash_file (iter);
                 }
             });
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]