[gitg/vala] Added support for plugin command line options



commit 33368f1f27952a78d2b50790ee4d6ed9637207a6
Author: Jesse van den Kieboom <jesse vandenkieboom epfl ch>
Date:   Wed Jul 18 11:03:45 2012 +0200

    Added support for plugin command line options
    
    The history plugin now implements the --all flag

 gitg/gitg-application.vala                     |   62 +++++--------
 gitg/gitg-window.vala                          |    4 +-
 libgitg-ext/Makefile.am                        |    3 +-
 libgitg-ext/gitg-ext-command-line.vala         |   28 ++++++
 libgitg-ext/gitg-ext-view.vala                 |    2 +-
 plugins/history/Makefile.am                    |    3 +-
 plugins/history/gitg-history-command-line.vala |   47 +++++++++
 plugins/history/gitg-history-navigation.vala   |   25 +++++-
 plugins/history/gitg-history.vala              |    7 +-
 plugins/history/history.plugin                 |    1 +
 vapi/libpeas-1.0.vapi                          |  119 ++++++++++++++++++++++++
 11 files changed, 254 insertions(+), 47 deletions(-)
---
diff --git a/gitg/gitg-application.vala b/gitg/gitg-application.vala
index e47b1cf..3c1f8cc 100644
--- a/gitg/gitg-application.vala
+++ b/gitg/gitg-application.vala
@@ -33,8 +33,7 @@ public class Application : Gtk.Application
 	private struct Options
 	{
 		public static bool quit = false;
-		public static bool commit = false;
-		public static string? select = null;
+		public static string view;
 		public static bool startup = false;
 		public static bool no_wd = false;
 		public static ApplicationCommandLine command_line;
@@ -42,9 +41,8 @@ public class Application : Gtk.Application
 		public static const OptionEntry[] entries = {
 			{"version", 'v', OptionFlags.NO_ARG, OptionArg.CALLBACK,
 			 (void *)show_version_and_quit, N_("Show the application's version"), null},
-			{"commit", 'c', 0, OptionArg.NONE,
-			 ref commit, N_("Start gitg in commit mode"), null},
-			 {"select", 's', 0, OptionArg.STRING, ref select, N_("Select commit after loading the repository"), null},
+			{"view", '\0', 0, OptionArg.STRING,
+			 ref view, N_("Start gitg with a particular view"), null},
 			 {"no-wd", 0, 0, OptionArg.NONE,
 			 ref no_wd, N_("Do not try to load a repository from the current working directory"), null},
 			{null}
@@ -69,6 +67,22 @@ public class Application : Gtk.Application
 		ctx.add_main_entries(options.entries, Config.GETTEXT_PACKAGE);
 		ctx.add_group(Gtk.get_option_group(true));
 
+		// Add any option groups from plugins
+		var engine = PluginsEngine.get_default();
+
+		foreach (var info in engine.get_plugin_list())
+		{
+			if (info.get_external_data("CommandLine") != null)
+			{
+				var ext = engine.create_extension(info, typeof(GitgExt.CommandLine)) as GitgExt.CommandLine;
+
+				if (ext != null)
+				{
+					ctx.add_group(ext.get_option_group());
+				}
+			}
+		}
+
 		ctx.parse(ref argv);
 	}
 
@@ -78,8 +92,11 @@ public class Application : Gtk.Application
 		string[] cp = arguments;
 		unowned string[] argv = cp;
 
+		PluginsEngine.initialize();
+
 		try
 		{
+			// This is just for local things, like showing help
 			parse_command_line(ref argv);
 		}
 		catch (Error e)
@@ -135,7 +152,7 @@ public class Application : Gtk.Application
 				files += File.new_for_commandline_arg(arg);
 			}
 
-			open(files, open_hint_from_options);
+			open(files, options.view);
 		}
 		else
 		{
@@ -145,11 +162,6 @@ public class Application : Gtk.Application
 		return 1;
 	}
 
-	private string open_hint_from_options
-	{
-		get { return options.commit ? "commit" : "history"; }
-	}
-
 	private void on_app_new_window_activated()
 	{
 	}
@@ -248,8 +260,7 @@ public class Application : Gtk.Application
 			// Otherwise open repository from current dir
 			string? wd = options.command_line.get_cwd();
 
-			open(new File[] { File.new_for_path(wd) },
-			     open_hint_from_options);
+			open(new File[] { File.new_for_path(wd) }, options.view);
 		}
 
 		base.activate();
@@ -329,8 +340,6 @@ public class Application : Gtk.Application
 
 	private void app_init()
 	{
-		PluginsEngine.initialize();
-
 		Gtk.CssProvider? provider = Resource.load_css("style.css");
 
 		if (provider != null)
@@ -344,30 +353,9 @@ public class Application : Gtk.Application
 		theme.prepend_search_path(Path.build_filename(Config.GITG_DATADIR, "icons"));
 	}
 
-	private GitgExt.ViewAction get_action_from_hint(string? hint)
-	{
-		if (hint == null)
-		{
-			return GitgExt.ViewAction.DEFAULT;
-		}
-
-		EnumClass klass = (EnumClass)typeof(GitgExt.ViewAction).class_ref();
-		EnumValue? val = klass.get_value_by_nick(hint);
-
-		if (val == null)
-		{
-			return GitgExt.ViewAction.DEFAULT;
-		}
-		else
-		{
-			return (GitgExt.ViewAction)val.value;
-		}
-	}
-
 	private void new_window(Repository? repo = null, string? hint = null)
 	{
-		
-		add_window(Window.create_new(this, repo, get_action_from_hint(hint)));
+		add_window(Window.create_new(this, repo, hint));
 		present_window();
 	}
 
diff --git a/gitg/gitg-window.vala b/gitg/gitg-window.vala
index fcced2a..9cfb08b 100644
--- a/gitg/gitg-window.vala
+++ b/gitg/gitg-window.vala
@@ -24,7 +24,7 @@ public class Window : Gtk.ApplicationWindow, GitgExt.Application, Initable, Gtk.
 {
 	private Repository? d_repository;
 	private GitgExt.MessageBus d_message_bus;
-	private GitgExt.ViewAction d_action;
+	private string? d_action;
 
 	private UIElements<GitgExt.View> d_views;
 	private UIElements<GitgExt.Panel> d_panels;
@@ -154,7 +154,7 @@ public class Window : Gtk.ApplicationWindow, GitgExt.Application, Initable, Gtk.
 
 	public static Window? create_new(Gtk.Application app,
 	                                 Repository? repository,
-	                                 GitgExt.ViewAction action)
+	                                 string? action)
 	{
 		Window? ret = Resource.load_object<Window>("ui/gitg-window.ui", "window");
 
diff --git a/libgitg-ext/Makefile.am b/libgitg-ext/Makefile.am
index 7cb5dc4..c8c6c13 100644
--- a/libgitg-ext/Makefile.am
+++ b/libgitg-ext/Makefile.am
@@ -44,7 +44,8 @@ VALA_FILES =					\
 	gitg-ext-message-id.vala		\
 	gitg-ext-message.vala			\
 	gitg-ext-message-bus.vala		\
-	gitg-ext-object-selection.vala
+	gitg-ext-object-selection.vala		\
+	gitg-ext-command-line.vala
 
 libgitg_ext_1_0_la_SOURCES =		\
 	$(VALA_FILES)			\
diff --git a/libgitg-ext/gitg-ext-command-line.vala b/libgitg-ext/gitg-ext-command-line.vala
new file mode 100644
index 0000000..958be26
--- /dev/null
+++ b/libgitg-ext/gitg-ext-command-line.vala
@@ -0,0 +1,28 @@
+/*
+ * This file is part of gitg
+ *
+ * Copyright (C) 2012 - Jesse van den Kieboom
+ *
+ * gitg is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * gitg 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 General Public License
+ * along with gitg. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace GitgExt
+{
+	public interface CommandLine : Object
+	{
+		public abstract OptionGroup get_option_group();
+	}
+}
+
+// vi:ts=4
diff --git a/libgitg-ext/gitg-ext-view.vala b/libgitg-ext/gitg-ext-view.vala
index 0eb59b6..195bd96 100644
--- a/libgitg-ext/gitg-ext-view.vala
+++ b/libgitg-ext/gitg-ext-view.vala
@@ -84,7 +84,7 @@ public interface View : Object, UIElement
 	 * @return ``true`` if the view is a default for @action, ``false`` otherwise.
 	 *
 	 */
-	public abstract bool is_default_for(ViewAction action);
+	public abstract bool is_default_for(string action);
 }
 
 }
diff --git a/plugins/history/Makefile.am b/plugins/history/Makefile.am
index 96f2d07..4225569 100644
--- a/plugins/history/Makefile.am
+++ b/plugins/history/Makefile.am
@@ -13,7 +13,8 @@ plugin_DATA = history.plugin
 VALAFLAGS = $(GITG_PLUGIN_VALAFLAGS)
 VALA_SOURCES =								\
 	gitg-history.vala						\
-	gitg-history-navigation.vala
+	gitg-history-navigation.vala					\
+	gitg-history-command-line.vala
 
 libhistory_la_LDFLAGS = $(GITG_PLUGIN_LIBTOOL_FLAGS)
 libhistory_la_LIBADD = $(GITG_PLUGIN_LIBS)
diff --git a/plugins/history/gitg-history-command-line.vala b/plugins/history/gitg-history-command-line.vala
new file mode 100644
index 0000000..eb64ede
--- /dev/null
+++ b/plugins/history/gitg-history-command-line.vala
@@ -0,0 +1,47 @@
+/*
+ * This file is part of gitg
+ *
+ * Copyright (C) 2012 - Jesse van den Kieboom
+ *
+ * gitg is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * gitg 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 General Public License
+ * along with gitg. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace GitgHistory
+{
+	public class CommandLine : Object, GitgExt.CommandLine
+	{
+		// Do this to pull in config.h before glib.h (for gettext...)
+		private const string version = Gitg.Config.VERSION;
+
+		public static bool all;
+
+		private static const OptionEntry[] entries = {
+			{"all", 'a', 0, OptionArg.NONE,
+			 ref all, N_("Show all history"), null},
+			{null}
+		};
+
+		public OptionGroup get_option_group()
+		{
+			var ret = new OptionGroup("history",
+			                          N_("Show gitg history options"),
+			                          N_("gitg history options"));
+
+			ret.add_entries(entries);
+			return ret;
+		}
+	}
+}
+
+// vi:ts=4
diff --git a/plugins/history/gitg-history-navigation.vala b/plugins/history/gitg-history-navigation.vala
index 6fe8d37..b3c927c 100644
--- a/plugins/history/gitg-history-navigation.vala
+++ b/plugins/history/gitg-history-navigation.vala
@@ -116,24 +116,43 @@ namespace GitgHistory
 			foreach (var item in branches)
 			{
 				var it = item;
+				string? icon = null;
+				bool isdef = false;
 
 				if (head != null && item.get_name() == head.get_target())
 				{
+					icon = "object-select-symbolic";
+
+					if (!CommandLine.all)
+					{
+						isdef = true;
+					}
+				}
+
+				if (isdef)
+				{
 					model.append_default(item.parsed_name.shortname,
-					                     "object-select-symbolic",
+					                     icon,
 					                     (nc) => ref_activated(it));
 				}
 				else
 				{
 					model.append(item.parsed_name.shortname,
-					             null,
+					             icon,
 					             (nc) => ref_activated(it));
 				}
 			}
 
 			model.separator();
 
-			model.append(_("All"), null, (nc) => ref_activated(null));
+			if (CommandLine.all)
+			{
+				model.append_default(_("All"), null, (nc) => ref_activated(null));
+			}
+			else
+			{
+				model.append(_("All"), null, (nc) => ref_activated(null));
+			}
 
 			model.end_header();
 
diff --git a/plugins/history/gitg-history.vala b/plugins/history/gitg-history.vala
index 8e60fb3..4493183 100644
--- a/plugins/history/gitg-history.vala
+++ b/plugins/history/gitg-history.vala
@@ -144,9 +144,9 @@ namespace GitgHistory
 			}
 		}
 
-		public bool is_default_for(GitgExt.ViewAction action)
+		public bool is_default_for(string action)
 		{
-			return application.repository != null && action == GitgExt.ViewAction.HISTORY;
+			return application.repository != null && (action == "" || action == "history");
 		}
 
 		private void on_ref_activated(Navigation n, Gitg.Ref? r)
@@ -270,6 +270,9 @@ public void peas_register_types(TypeModule module)
 
 	mod.register_extension_type(typeof(GitgExt.View),
 	                            typeof(GitgHistory.View));
+
+	mod.register_extension_type(typeof(GitgExt.CommandLine),
+	                            typeof(GitgHistory.CommandLine));
 }
 
 // ex: ts=4 noet
diff --git a/plugins/history/history.plugin b/plugins/history/history.plugin
index 33d800a..2177148 100644
--- a/plugins/history/history.plugin
+++ b/plugins/history/history.plugin
@@ -9,3 +9,4 @@ Website=
 Hidden=1
 Builtin=1
 Version=1.0
+X-CommandLine=1
diff --git a/vapi/libpeas-1.0.vapi b/vapi/libpeas-1.0.vapi
new file mode 100644
index 0000000..073622e
--- /dev/null
+++ b/vapi/libpeas-1.0.vapi
@@ -0,0 +1,119 @@
+/* libpeas-1.0.vapi generated by vapigen, do not modify. */
+
+[CCode (cprefix = "Peas", gir_namespace = "Peas", gir_version = "1.0", lower_case_cprefix = "peas_")]
+namespace Peas {
+	[CCode (cheader_filename = "libpeas/peas.h", type_id = "peas_engine_get_type ()")]
+	public class Engine : GLib.Object {
+		[CCode (has_construct_function = false)]
+		public Engine ();
+		public void add_search_path (string module_dir, string? data_dir);
+		public Peas.Extension create_extension (Peas.PluginInfo info, GLib.Type extension_type, ...);
+		public Peas.Extension create_extensionv (Peas.PluginInfo info, GLib.Type extension_type, [CCode (array_length_cname = "n_parameters", array_length_pos = 2.5, array_length_type = "guint")] GLib.Parameter[]? parameters);
+		public void enable_loader (string loader_id);
+		public void garbage_collect ();
+		public static unowned Peas.Engine get_default ();
+		[CCode (array_length = false, array_null_terminated = true)]
+		public string[] get_loaded_plugins ();
+		public unowned Peas.PluginInfo get_plugin_info (string plugin_name);
+		public unowned GLib.List<Peas.PluginInfo> get_plugin_list ();
+		public bool provides_extension (Peas.PluginInfo info, GLib.Type extension_type);
+		public void rescan_plugins ();
+		public void set_loaded_plugins ([CCode (array_length = false, array_null_terminated = true)] string[]? plugin_names);
+		[CCode (cname = "peas_engine_load_plugin")]
+		public bool try_load_plugin (Peas.PluginInfo info);
+		[CCode (cname = "peas_engine_unload_plugin")]
+		public bool try_unload_plugin (Peas.PluginInfo info);
+		[CCode (array_length = false, array_null_terminated = true)]
+		public string[] loaded_plugins { owned get; set; }
+		public void* plugin_list { get; }
+		public virtual signal void load_plugin (Peas.PluginInfo info);
+		public virtual signal void unload_plugin (Peas.PluginInfo info);
+	}
+	[CCode (cheader_filename = "libpeas/peas.h")]
+	public class Extension : GLib.Object {
+		[CCode (has_construct_function = false)]
+		protected Extension ();
+	}
+	[CCode (cheader_filename = "libpeas/peas.h", type_id = "peas_extension_base_get_type ()")]
+	public abstract class ExtensionBase : GLib.Object {
+		[CCode (has_construct_function = false)]
+		protected ExtensionBase ();
+		public string get_data_dir ();
+		public unowned Peas.PluginInfo get_plugin_info ();
+		public string data_dir { owned get; }
+		public Peas.PluginInfo plugin_info { get; construct; }
+	}
+	[CCode (cheader_filename = "libpeas/peas.h", type_id = "peas_extension_set_get_type ()")]
+	public class ExtensionSet : GLib.Object {
+		[CCode (has_construct_function = false)]
+		public ExtensionSet (Peas.Engine engine, GLib.Type exten_type, ...);
+		public void @foreach (Peas.ExtensionSetForeachFunc func);
+		public unowned Peas.Extension get_extension (Peas.PluginInfo info);
+		[CCode (has_construct_function = false)]
+		public ExtensionSet.newv (Peas.Engine? engine, GLib.Type exten_type, [CCode (array_length_cname = "n_parameters", array_length_pos = 2.5, array_length_type = "guint")] GLib.Parameter[] parameters);
+		public void* construct_properties { construct; }
+		[NoAccessorMethod]
+		public Peas.Engine engine { owned get; construct; }
+		[NoAccessorMethod]
+		public GLib.Type extension_type { get; construct; }
+		public virtual signal void extension_added (Peas.PluginInfo info, GLib.Object exten);
+		public virtual signal void extension_removed (Peas.PluginInfo info, GLib.Object exten);
+	}
+	[CCode (cheader_filename = "libpeas/peas.h", type_id = "peas_object_module_get_type ()")]
+	public class ObjectModule : GLib.TypeModule, GLib.TypePlugin {
+		[CCode (has_construct_function = false)]
+		protected ObjectModule ();
+		public void register_extension_factory (GLib.Type iface_type, owned Peas.FactoryFunc factory_func);
+		public void register_extension_type (GLib.Type iface_type, GLib.Type extension_type);
+		[NoAccessorMethod]
+		public string module_name { owned get; construct; }
+		[NoAccessorMethod]
+		public string path { owned get; construct; }
+		[NoAccessorMethod]
+		public bool resident { get; construct; }
+	}
+	[CCode (cheader_filename = "libpeas/peas.h", copy_function = "g_boxed_copy", free_function = "g_boxed_free", type_id = "peas_plugin_info_get_type ()")]
+	[Compact]
+	public class PluginInfo {
+		[CCode (array_length = false, array_null_terminated = true)]
+		public unowned string[] get_authors ();
+		public unowned string get_copyright ();
+		public unowned string get_data_dir ();
+		[CCode (array_length = false, array_null_terminated = true)]
+		public unowned string[] get_dependencies ();
+		public unowned string get_description ();
+		public unowned string? get_external_data(string name);
+		public unowned string get_help_uri ();
+		public unowned string get_icon_name ();
+		public unowned string get_module_dir ();
+		public unowned string get_module_name ();
+		public unowned string get_name ();
+		public unowned string get_version ();
+		public unowned string get_website ();
+		public bool has_dependency (string module_name);
+		public bool is_available () throws GLib.Error;
+		public bool is_builtin ();
+		public bool is_hidden ();
+		public bool is_loaded ();
+	}
+	[CCode (cheader_filename = "libpeas/peas.h", type_cname = "PeasActivatableInterface", type_id = "peas_activatable_get_type ()")]
+	public interface Activatable : GLib.Object {
+		public abstract void activate ();
+		public abstract void deactivate ();
+		public abstract void update_state ();
+		[NoAccessorMethod]
+		public abstract GLib.Object object { owned get; construct; }
+	}
+	[CCode (cheader_filename = "libpeas/peas.h", cprefix = "PEAS_PLUGIN_INFO_ERROR_")]
+	public errordomain PluginInfoError {
+		LOADING_FAILED,
+		LOADER_NOT_FOUND,
+		DEP_NOT_FOUND,
+		DEP_LOADING_FAILED;
+		public static GLib.Quark quark ();
+	}
+	[CCode (cheader_filename = "libpeas/peas.h", instance_pos = 3.9)]
+	public delegate void ExtensionSetForeachFunc (Peas.ExtensionSet @set, Peas.PluginInfo info, Peas.Extension exten);
+	[CCode (cheader_filename = "libpeas/peas.h", instance_pos = 1.9)]
+	public delegate GLib.Object FactoryFunc ([CCode (array_length_cname = "n_parameters", array_length_pos = 0.5, array_length_type = "guint")] GLib.Parameter[] parameters);
+}



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