[gitg] Improve support for extension command line arguments



commit dba244de0f46078ff01b2c481d69cb9e679ccacd
Author: Jesse van den Kieboom <jessevdk gnome org>
Date:   Wed Aug 5 19:14:40 2015 +0200

    Improve support for extension command line arguments

 gitg/gitg-application.vala              |   65 ++++++++++++++++------------
 gitg/gitg-ui-elements.vala              |    5 ++
 gitg/gitg-window.vala                   |   73 ++++++++++++++++++++++---------
 libgitg-ext/Makefile.am                 |    1 +
 libgitg-ext/gitg-ext-application.vala   |    3 +-
 libgitg-ext/gitg-ext-command-line.vala  |    2 +
 libgitg-ext/gitg-ext-command-lines.vala |   62 ++++++++++++++++++++++++++
 7 files changed, 161 insertions(+), 50 deletions(-)
---
diff --git a/gitg/gitg-application.vala b/gitg/gitg-application.vala
index 84bf4cd..7e89314 100644
--- a/gitg/gitg-application.vala
+++ b/gitg/gitg-application.vala
@@ -85,13 +85,20 @@ public class Application : Gtk.Application
                return true;
        }
 
-       private void parse_command_line(ref unowned string[] argv) throws OptionError
+       private GitgExt.CommandLines parse_command_line(ref unowned string[] argv) throws OptionError
        {
                var ctx = new OptionContext(_("- Git repository viewer"));
 
                ctx.add_main_entries(Options.entries, Config.GETTEXT_PACKAGE);
                ctx.add_group(Gtk.get_option_group(true));
 
+               var cmdexts = new GitgExt.CommandLine[0];
+
+               var historycmd = new GitgHistory.CommandLine();
+               cmdexts += historycmd;
+
+               ctx.add_group(historycmd.get_option_group());
+
                // Add any option groups from plugins
                var engine = PluginsEngine.get_default();
 
@@ -103,12 +110,18 @@ public class Application : Gtk.Application
 
                                if (ext != null)
                                {
+                                       cmdexts += ext;
                                        ctx.add_group(ext.get_option_group());
                                }
                        }
                }
 
                ctx.parse(ref argv);
+
+               var ret = new GitgExt.CommandLines(cmdexts);
+               ret.parse_finished();
+
+               return ret;
        }
 
        protected override bool local_command_line ([CCode (array_length = false, array_null_terminated = 
true)] ref unowned string[] arguments, out int exit_status)
@@ -144,10 +157,11 @@ public class Application : Gtk.Application
        {
                string[] arguments = cmd.get_arguments();
                unowned string[] argv = arguments;
+               GitgExt.CommandLines command_lines;
 
                try
                {
-                       parse_command_line(ref argv);
+                       command_lines = parse_command_line(ref argv);
                }
                catch (Error e)
                {
@@ -178,15 +192,14 @@ public class Application : Gtk.Application
                                files += File.new_for_commandline_arg(arg);
                        }
 
-                       open(files, Options.activity);
+                       open_command_line(files, Options.activity, command_lines);
                }
                else
                {
-                       activate();
+                       activate_command_line(command_lines);
                }
 
                Options.command_line = tmpcmd;
-
                return 1;
        }
 
@@ -344,22 +357,11 @@ public class Application : Gtk.Application
                base.shutdown();
        }
 
-       protected override void activate()
+       private void activate_command_line(GitgExt.CommandLines command_lines)
        {
-               /* Application gets activated when no command line arguments have
-                * been provided. However, gitg does something special in the case
-                * that it has been launched from the terminal. It will try to open
-                * the cwd as a repository. However, when not launched from the terminal
-                * this is undesired, and a --no-wd allows gitg to be launched without
-                * the implicit working directory opening of the repository. In the
-                * end, the following happens:
-                *
-                * 1) --no-wd: present the window
-                * 2) Get cwd from the commandline: open
-                */
                if (Options.no_wd)
                {
-                       present_window();
+                       present_window(Options.activity, command_lines);
                }
                else
                {
@@ -367,12 +369,13 @@ public class Application : Gtk.Application
                        string? wd = Options.command_line.get_cwd();
 
                        open(new File[] { File.new_for_path(wd) }, Options.activity);
-
-                       // Forcing present here covers the case where no window was opened
-                       // because wd is not an actual git repository
-                       present_window();
+                       present_window(Options.activity, command_lines);
                }
+       }
 
+       protected override void activate()
+       {
+               present_window();
                base.activate();
        }
 
@@ -398,6 +401,12 @@ public class Application : Gtk.Application
 
        protected override void open(File[] files, string hint)
        {
+               open_command_line(files, hint);
+       }
+
+
+       private void open_command_line(File[] files, string? hint = null, GitgExt.CommandLines? command_lines 
= null)
+       {
                if (files.length == 0)
                {
                        return;
@@ -422,7 +431,7 @@ public class Application : Gtk.Application
                        {
                                // Present the window with this repository open
                                window.set_environment(Options.command_line.get_environ());
-                               window.present();
+                               window.present(hint, command_lines);
                                continue;
                        }
 
@@ -436,11 +445,11 @@ public class Application : Gtk.Application
                        catch { continue; }
 
                        // Finally, create a window for the repository
-                       new_window(repo, hint);
+                       new_window(repo, hint, command_lines);
                }
        }
 
-       private void new_window(Repository? repo = null, string? hint = null)
+       private void new_window(Repository? repo = null, string? hint = null, GitgExt.CommandLines? 
command_lines = null)
        {
                var window = Window.create_new(this, repo, hint);
 
@@ -449,10 +458,10 @@ public class Application : Gtk.Application
                        window.set_environment(Options.command_line.get_environ());
                }
 
-               present_window();
+               present_window(hint, command_lines);
        }
 
-       private void present_window()
+       private void present_window(string? activity = null, GitgExt.CommandLines? command_lines = null)
        {
                /* Present the last window in the windows registered on the
                 * application. If there are no windows, then create a new empty
@@ -469,7 +478,7 @@ public class Application : Gtk.Application
                var w = (Gitg.Window)windows.first().data;
 
                w.set_environment(Options.command_line.get_environ());
-               w.present();
+               w.present(activity, command_lines);
        }
 }
 
diff --git a/gitg/gitg-ui-elements.vala b/gitg/gitg-ui-elements.vala
index 1db47df..2b075c5 100644
--- a/gitg/gitg-ui-elements.vala
+++ b/gitg/gitg-ui-elements.vala
@@ -131,6 +131,11 @@ public class UIElements<T> : Object
                        return;
                }
 
+               if (d_current == element)
+               {
+                       return;
+               }
+
                d_current = element;
 
                if (d_stack != null)
diff --git a/gitg/gitg-window.vala b/gitg/gitg-window.vala
index f775251..db2a50e 100644
--- a/gitg/gitg-window.vala
+++ b/gitg/gitg-window.vala
@@ -337,7 +337,7 @@ public class Window : Gtk.ApplicationWindow, GitgExt.Application, Initable
        public GitgExt.Application open_new(Ggit.Repository repository, string? hint = null)
        {
                var window = Window.create_new(application, (Gitg.Repository)repository, hint);
-               window.present();
+               base.present();
 
                return window;
        }
@@ -535,45 +535,56 @@ public class Window : Gtk.ApplicationWindow, GitgExt.Application, Initable
                }
        }
 
-       private void activate_default_activity()
+       private bool activate_activity(string? action)
        {
-               GitgExt.Activity? def = null;
-               GitgExt.Activity? deffb = null;
-
                string default_activity;
 
-               if (d_action == null || d_action == "")
+               if (action == null || action == "")
                {
                        default_activity = d_interface_settings.get_string("default-activity");
                }
                else
                {
-                       default_activity = d_action;
+                       default_activity = action;
                }
 
-               d_activities.foreach((element) => {
-                               GitgExt.Activity activity = (GitgExt.Activity)element;
+               GitgExt.Activity? def = null;
 
-                               if (activity.is_default_for(default_activity))
-                               {
-                                       def = activity;
-                               }
+               d_activities.foreach((element) => {
+                       GitgExt.Activity activity = (GitgExt.Activity)element;
 
-                               if (activity.is_default_for(""))
-                               {
-                                       deffb = activity;
-                               }
+                       if (activity.is_default_for(default_activity))
+                       {
+                               def = activity;
+                       }
 
-                               return true;
+                       return true;
                });
 
                if (def != null)
                {
                        d_activities.current = def;
+                       return true;
                }
-               else if (deffb != null)
+
+               return false;
+       }
+
+       private void activate_default_activity()
+       {
+               if (!activate_activity(d_action))
                {
-                       d_activities.current = deffb;
+                       d_activities.foreach((element) => {
+                               GitgExt.Activity activity = (GitgExt.Activity)element;
+
+                               if (activity.is_default_for(""))
+                               {
+                                       d_activities.current = activity;
+                                       return false;
+                               }
+
+                               return true;
+                       });
                }
        }
 
@@ -665,7 +676,7 @@ public class Window : Gtk.ApplicationWindow, GitgExt.Application, Initable
        }
 
        /* public API implementation of GitgExt.Application */
-       public GitgExt.Activity? activity(string id)
+       public GitgExt.Activity? set_activity_by_id(string id)
        {
                GitgExt.Activity? v = d_activities.lookup(id);
 
@@ -684,6 +695,11 @@ public class Window : Gtk.ApplicationWindow, GitgExt.Application, Initable
                }
        }
 
+       public GitgExt.Activity? get_activity_by_id(string id)
+       {
+               return d_activities.lookup(id);
+       }
+
        public void open_repository(File path)
        {
                File repo;
@@ -810,6 +826,21 @@ public class Window : Gtk.ApplicationWindow, GitgExt.Application, Initable
                }
        }
 
+       public new void present(string? hint, GitgExt.CommandLines? command_lines)
+       {
+               if (hint != null)
+               {
+                       activate_activity(hint);
+               }
+
+               if (command_lines != null)
+               {
+                       command_lines.apply(this);
+               }
+
+               base.present();
+       }
+
        private void on_select_activated(SimpleAction action)
        {
                var st = action.get_state().get_boolean();
diff --git a/libgitg-ext/Makefile.am b/libgitg-ext/Makefile.am
index df8e167..0e48656 100644
--- a/libgitg-ext/Makefile.am
+++ b/libgitg-ext/Makefile.am
@@ -47,6 +47,7 @@ libgitg_ext_libgitg_ext_1_0_la_VALASOURCES =          \
        libgitg-ext/gitg-ext-application.vala           \
        libgitg-ext/gitg-ext-assembly-info.vala         \
        libgitg-ext/gitg-ext-command-line.vala          \
+       libgitg-ext/gitg-ext-command-lines.vala         \
        libgitg-ext/gitg-ext-commit-action.vala         \
        libgitg-ext/gitg-ext-history-panel.vala         \
        libgitg-ext/gitg-ext-history.vala               \
diff --git a/libgitg-ext/gitg-ext-application.vala b/libgitg-ext/gitg-ext-application.vala
index 8a3120d..88df0ef 100644
--- a/libgitg-ext/gitg-ext-application.vala
+++ b/libgitg-ext/gitg-ext-application.vala
@@ -63,7 +63,8 @@ public interface Application : Object
         * @return the created new main activity, or ``null`` if no activity with the
         *         given id exists.
         */
-       public abstract GitgExt.Activity? activity(string id);
+       public abstract GitgExt.Activity? get_activity_by_id(string id);
+       public abstract GitgExt.Activity? set_activity_by_id(string id);
 
        public abstract void user_query(UserQuery query);
 
diff --git a/libgitg-ext/gitg-ext-command-line.vala b/libgitg-ext/gitg-ext-command-line.vala
index 958be26..0792104 100644
--- a/libgitg-ext/gitg-ext-command-line.vala
+++ b/libgitg-ext/gitg-ext-command-line.vala
@@ -22,6 +22,8 @@ namespace GitgExt
        public interface CommandLine : Object
        {
                public abstract OptionGroup get_option_group();
+               public abstract void parse_finished();
+               public abstract void apply(GitgExt.Application application);
        }
 }
 
diff --git a/libgitg-ext/gitg-ext-command-lines.vala b/libgitg-ext/gitg-ext-command-lines.vala
new file mode 100644
index 0000000..d0373d6
--- /dev/null
+++ b/libgitg-ext/gitg-ext-command-lines.vala
@@ -0,0 +1,62 @@
+/*
+ * 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 class CommandLines : Object
+       {
+               private CommandLine[] d_command_lines;
+
+               public CommandLines(CommandLine[] command_lines)
+               {
+                       d_command_lines = command_lines;
+               }
+
+               public T? get_for<T>()
+               {
+                       foreach (var cmd in d_command_lines)
+                       {
+                               if (cmd.get_type() == typeof(T))
+                               {
+                                       return (T)cmd;
+                               }
+                       }
+
+                       return null;
+               }
+
+               public void parse_finished()
+               {
+                       foreach (var cmd in d_command_lines)
+                       {
+                               cmd.parse_finished();
+                       }
+               }
+
+               public void apply(Application application)
+               {
+                       foreach (var cmd in d_command_lines)
+                       {
+                               cmd.apply(application);
+                       }
+               }
+       }
+}
+
+// vi:ts=4


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