[tracker/tracker-needle-improved-tagging: 3/4] tracker-needle: Add initial context menu for views and launching parent dirs



commit f08cd8510e2071f3243eab7015d1e8947eb2fb19
Author: Martyn Russell <martyn lanedo com>
Date:   Mon Jun 27 14:15:56 2011 +0100

    tracker-needle: Add initial context menu for views and launching parent dirs
    
    This means it is now possible to launch nautilus with the parent folder of a given uri

 src/tracker-needle/tracker-needle.vala |   54 +---------------
 src/tracker-needle/tracker-utils.vala  |   64 +++++++++++++++++++
 src/tracker-needle/tracker-view.vala   |  108 +++++++++++++++++++++++++++++--
 3 files changed, 168 insertions(+), 58 deletions(-)
---
diff --git a/src/tracker-needle/tracker-needle.vala b/src/tracker-needle/tracker-needle.vala
index 22cf1b4..d76aba2 100644
--- a/src/tracker-needle/tracker-needle.vala
+++ b/src/tracker-needle/tracker-needle.vala
@@ -320,8 +320,7 @@ public class Tracker.Needle {
 		view_categories.set_active (true);
 	}
 
-	private bool window_key_press_event (Gtk.Widget   widget,
-	                                     Gdk.EventKey event) {
+	private bool window_key_press_event (Gtk.Widget widget, Gdk.EventKey event) {
 		// Add Ctrl+W close window semantics
 		if (Gdk.ModifierType.CONTROL_MASK in event.state && Gdk.keyval_name (event.keyval) == "w") {
 			widget.destroy();
@@ -561,61 +560,14 @@ public class Tracker.Needle {
 		}
 	}
 
-	private void launch_selected (TreeModel model, TreePath path, int col) {
-		TreeIter iter;
-		model.get_iter (out iter, path);
-
-		weak string uri;
-		model.get (iter, col, out uri);
-
-		if (uri == null) {
-			return;
-		}
-
-		debug ("Selected uri:'%s'", uri);
-
-		// Bit of a hack for now if there is no URI scheme, we assume that
-		// the uri is actually a command line to launch.
-		if (uri.index_of ("://") < 1) {
-			var command = uri.split (" ");
-			debug ("Attempting to spawn_async() '%s'", command[0]);
-
-			Pid child_pid;
-			string[] argv = new string[1];
-			argv[0] = command[0];
-
-			try {
-				Process.spawn_async ("/usr/bin",
-				                     argv,
-				                     null, // environment
-				                     SpawnFlags.SEARCH_PATH,
-				                     null, // child_setup
-				                     out child_pid);
-			} catch (Error e) {
-				warning ("Could not launch '%s', %d->%s", command[0], e.code, GLib.strerror (e.code));
-				return;
-			}
-
-			debug ("Launched application with PID:%d", child_pid);
-			return;
-		}
-
-		try {
-			debug ("Attempting to launch application for uri:'%s'", uri);
-			AppInfo.launch_default_for_uri (uri, null);
-		} catch (GLib.Error e) {
-			warning ("Could not launch application: " + e.message);
-		}
-	}
-
 	private void view_row_selected (TreeView view, TreePath path, TreeViewColumn column) {
 		var model = view.get_model ();
-		launch_selected (model, path, 1);
+		tracker_model_launch_selected (model, path, 1);
 	}
 
 	private void icon_item_selected (IconView view, TreePath path) {
 		var model = view.get_model ();
-		launch_selected (model, path, 1);
+		tracker_model_launch_selected (model, path, 1);
 	}
 
 	private void show_tags_clicked () {
diff --git a/src/tracker-needle/tracker-utils.vala b/src/tracker-needle/tracker-utils.vala
index 95e0bfe..5482611 100644
--- a/src/tracker-needle/tracker-utils.vala
+++ b/src/tracker-needle/tracker-utils.vala
@@ -21,6 +21,70 @@ using Gtk;
 
 private const int secs_per_day = 60 * 60 * 24;
 
+private string? uri_get_selected (TreeModel model, TreePath path, int col) {
+	TreeIter iter;
+	model.get_iter (out iter, path);
+
+	weak string uri;
+	model.get (iter, col, out uri);
+
+	return uri;
+}
+
+private void uri_launch (string uri) {
+	// Bit of a hack for now if there is no URI scheme, we assume that
+	// the uri is actually a command line to launch.
+	if (uri.index_of ("://") < 1) {
+		var command = uri.split (" ");
+		debug ("Attempting to spawn_async() '%s'", command[0]);
+
+		Pid child_pid;
+		string[] argv = new string[1];
+		argv[0] = command[0];
+
+		try {
+			Process.spawn_async ("/usr/bin",
+			                     argv,
+			                     null, // environment
+			                     SpawnFlags.SEARCH_PATH,
+			                     null, // child_setup
+			                     out child_pid);
+		} catch (Error e) {
+			warning ("Could not launch '%s', %d->%s", command[0], e.code, GLib.strerror (e.code));
+			return;
+		}
+
+		debug ("Launched application with PID:%d", child_pid);
+		return;
+	}
+
+	try {
+		debug ("Attempting to launch application for uri:'%s'", uri);
+		AppInfo.launch_default_for_uri (uri, null);
+	} catch (GLib.Error e) {
+		warning ("Could not launch application: " + e.message);
+	}
+}
+
+public void tracker_model_launch_selected (TreeModel model, TreePath path, int col) {
+	string uri = uri_get_selected (model, path, col);
+	debug ("Selected uri:'%s'", uri);
+
+	uri_launch (uri);
+}
+
+public void tracker_model_launch_selected_parent_dir (TreeModel model, TreePath path, int col) {
+	string uri = uri_get_selected (model, path, col);
+	debug ("Selected uri:'%s'", uri);
+
+	File f = File.new_for_uri (uri);
+	File p = f.get_parent ();
+	string parent_uri = p.get_uri ();
+
+	debug ("Parent uri:'%s'", parent_uri);
+	uri_launch (parent_uri);
+}
+
 public string tracker_time_format_from_iso8601 (string s) {
 	GLib.Time t = GLib.Time ();
 	t.strptime (s, "%FT%T");
diff --git a/src/tracker-needle/tracker-view.vala b/src/tracker-needle/tracker-view.vala
index 4718599..0c51af3 100644
--- a/src/tracker-needle/tracker-view.vala
+++ b/src/tracker-needle/tracker-view.vala
@@ -58,6 +58,7 @@ public class Tracker.View : ScrolledWindow {
 	}
 
 	private Widget view = null;
+	private Menu context_menu;
 
 	private void store_row_changed (TreeModel model,
 	                                TreePath  path,
@@ -130,6 +131,7 @@ public class Tracker.View : ScrolledWindow {
 		} else {
 			add (view);
 			setup_model ();
+			setup_menus ();
 		}
 
 		base.show_all ();
@@ -180,7 +182,7 @@ public class Tracker.View : ScrolledWindow {
 			col.set_title (_("File"));
 			col.set_resizable (true);
 			col.set_expand (true);
-			col.set_cell_data_func (renderer1, renderer_background_func);
+			col.set_cell_data_func (renderer1, background_renderer_func);
 			col.set_cell_data_func (renderer2, text_renderer_func);
 			tv.append_column (col);
 
@@ -227,7 +229,7 @@ public class Tracker.View : ScrolledWindow {
 			var renderer1 = new CellRendererPixbuf ();
 			col.pack_start (renderer1, false);
 			col.add_attribute (renderer1, "pixbuf", 6);
-			col.set_cell_data_func (renderer1, renderer_background_func);
+			col.set_cell_data_func (renderer1, background_renderer_func);
 			renderer1.xpad = 5;
 			renderer1.ypad = 5;
 
@@ -266,7 +268,7 @@ public class Tracker.View : ScrolledWindow {
 		}
 	}
 
-	private void renderer_background_func (CellLayout   cell_layout,
+	private void background_renderer_func (CellLayout   cell_layout,
 	                                       CellRenderer cell,
 	                                       TreeModel    tree_model,
 	                                       TreeIter     iter) {
@@ -312,7 +314,7 @@ public class Tracker.View : ScrolledWindow {
 		string markup = null;
 		int n_children;
 
-		renderer_background_func (cell_layout, cell, tree_model, iter);
+		background_renderer_func (cell_layout, cell, tree_model, iter);
 		n_children = tree_model.iter_n_children (iter);
 
 		if (n_children > 0) {
@@ -373,7 +375,7 @@ public class Tracker.View : ScrolledWindow {
 	                                      TreeIter     iter) {
 		string size;
 
-		renderer_background_func (cell_layout, cell, tree_model, iter);
+		background_renderer_func (cell_layout, cell, tree_model, iter);
 		tree_model.get (iter, 4, out size, -1);
 
 		if (size != null) {
@@ -389,7 +391,7 @@ public class Tracker.View : ScrolledWindow {
 	                                      TreeIter     iter) {
 		string date;
 
-		renderer_background_func (cell_layout, cell, tree_model, iter);
+		background_renderer_func (cell_layout, cell, tree_model, iter);
 		tree_model.get (iter, 5, out date, -1);
 
 		if (date != null) {
@@ -407,7 +409,7 @@ public class Tracker.View : ScrolledWindow {
 		string markup = null;
 		string detail;
 
-		renderer_background_func (cell_layout, cell, tree_model, iter);
+		background_renderer_func (cell_layout, cell, tree_model, iter);
 		tree_model.get (iter, 4, out detail, 7, out category, -1);
 
 		if (detail == null) {
@@ -436,4 +438,96 @@ public class Tracker.View : ScrolledWindow {
 		markup = "<span color='grey'><small>%s</small></span>".printf (Markup.escape_text (detail));
 		cell.set ("markup", markup);
 	}
+
+	private void setup_menus () {
+		// Set up context menu
+		view.button_press_event.connect (view_button_press_event);
+
+		context_menu = new Menu ();
+
+		var item = new MenuItem.with_mnemonic (_("_Show Parent Directory"));
+		item.activate.connect (context_menu_directory_clicked);
+		context_menu.append (item);
+
+		var separator = new SeparatorMenuItem ();
+		context_menu.append (separator);
+
+		item = new MenuItem.with_mnemonic (_("_Add Tag..."));
+		item.activate.connect (context_menu_tag_add_clicked);
+		item.sensitive = false;
+		context_menu.append (item);
+
+		item = new MenuItem.with_mnemonic (_("_Remove Tag..."));
+		item.activate.connect (context_menu_tag_remove_clicked);
+		item.sensitive = false;
+		context_menu.append (item);
+
+		context_menu.show_all ();
+	}
+
+	private bool view_button_press_event (Gtk.Widget widget, Gdk.EventButton event) {
+		if (event.button == 3) {
+			if (get_selected_path () != null) {
+				context_menu.popup (null, null, null, event.button, event.time);
+			}
+		}
+
+		return false;
+	}
+
+	private TreeModel? get_model () {
+		switch (display) {
+		case Display.CATEGORIES:
+		case Display.FILE_LIST:
+			TreeView v = (TreeView) view;
+			return v.get_model ();
+
+		case Display.FILE_ICONS:
+			IconView v = (IconView) view;
+			return v.get_model ();
+		default:
+			break;
+		}
+
+		return null;
+	}
+
+	private TreePath? get_selected_path () {
+		switch (display) {
+		case Display.CATEGORIES:
+		case Display.FILE_LIST:
+			TreeView v = (TreeView) view;
+			TreeSelection s = v.get_selection ();
+			List<TreePath> selected = s.get_selected_rows (null);
+
+			return selected.nth_data (0);
+
+		case Display.FILE_ICONS:
+			IconView v = (IconView) view;
+			List<TreePath> selected = v.get_selected_items ();
+
+			return selected.nth_data (0);
+
+		default:
+			break;
+		}
+
+		return null;
+	}
+
+	private void context_menu_directory_clicked () {
+		TreeModel model = get_model ();
+		TreePath path = get_selected_path ();
+
+		tracker_model_launch_selected_parent_dir (model, path, 1);
+	}
+
+	private void context_menu_tag_add_clicked () {
+		warning ("Not yet implemented");
+	}
+
+	private void context_menu_tag_remove_clicked () {
+		warning ("Not yet implemented");
+	}
+
 }



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