[latexila/gnome-3] Main: open files from the command line



commit 602f196538f1b53ed0e86337144884b58c6885c8
Author: SÃbastien Wilmet <swilmet src gnome org>
Date:   Tue Apr 3 02:42:24 2012 +0200

    Main: open files from the command line
    
    And make Latexila.open_documents() more robust.

 src/latexila.vala    |   39 ++++++++++++++++++++++++++++++---------
 src/main.vala        |   33 +++++++++++++++++++++++++++++++--
 src/main_window.vala |   10 +++++++++-
 src/utils.vala       |   10 ----------
 4 files changed, 70 insertions(+), 22 deletions(-)
---
diff --git a/src/latexila.vala b/src/latexila.vala
index fbe4a2b..a8285c0 100644
--- a/src/latexila.vala
+++ b/src/latexila.vala
@@ -86,6 +86,25 @@ public class Latexila : Gtk.Application
             create_window ();
             release ();
         });
+
+        /* Open files */
+        VariantType strings_array = new VariantType ("as");
+        SimpleAction open_files_action = new SimpleAction ("open-files", strings_array);
+        add_action (open_files_action);
+
+        open_files_action.activate.connect ((param) =>
+        {
+            string[] uris = param.dup_strv ();
+            File[] files = {};
+
+            foreach (string uri in uris)
+            {
+                if (0 < uri.length)
+                    files += File.new_for_uri (uri);
+            }
+
+            open_documents (files);
+        });
     }
 
     public static Latexila get_instance ()
@@ -140,7 +159,14 @@ public class Latexila : Gtk.Application
                 new GLib.Settings ("org.gnome.latexila.state.window");
 
             string[] uris = window_settings.get_strv ("documents");
-            open_documents (uris);
+            File[] files = {};
+            foreach (string uri in uris)
+            {
+                if (0 < uri.length)
+                    files += File.new_for_uri (uri);
+            }
+
+            open_documents (files);
         }
     }
 
@@ -197,17 +223,12 @@ public class Latexila : Gtk.Application
         active_window.create_tab (true);
     }
 
-    public void open_documents (
-        [CCode (array_length = false, array_null_terminated = true)] string[] uris)
+    public void open_documents (File[] files)
     {
         bool jump_to = true;
-        foreach (string uri in uris)
+        foreach (File file in files)
         {
-            if (uri.length == 0)
-                continue;
-
-            File location = File.new_for_uri (uri);
-            active_window.open_document (location, jump_to);
+            active_window.open_document (file, jump_to);
             jump_to = false;
         }
     }
diff --git a/src/main.vala b/src/main.vala
index 0c9f453..70b6719 100644
--- a/src/main.vala
+++ b/src/main.vala
@@ -25,6 +25,7 @@ private struct CmdLineData
 {
     bool new_document;
     bool new_window;
+    Variant? files_to_open;
 }
 
 private void check_xdg_data_dirs ()
@@ -54,12 +55,16 @@ private void init_i18n ()
     Intl.textdomain (Config.GETTEXT_PACKAGE);
 }
 
+// Put this here because inside a function is not possible...
+[CCode (array_length = false, array_null_terminated = true)]
+string[] files_list;
+
 private CmdLineData parse_cmd_line_options (string[] args)
 {
     bool show_version = false;
-    string[] files_list;
     CmdLineData data = CmdLineData ();
 
+    /* Definition of the options */
     OptionEntry[] options = new OptionEntry[5];
 
     options[0] = { "version", 'V', 0, OptionArg.NONE, ref show_version,
@@ -76,7 +81,11 @@ private CmdLineData parse_cmd_line_options (string[] args)
 
     options[4] = { null };
 
-    OptionContext context = Utils.get_option_context (options);
+    /* Parse the command line and extract data */
+    OptionContext context =
+        new OptionContext (_("- Integrated LaTeX Environment for GNOME"));
+    context.add_main_entries (options, Config.GETTEXT_PACKAGE);
+    context.add_group (Gtk.get_option_group (false));
 
     try
     {
@@ -98,6 +107,23 @@ private CmdLineData parse_cmd_line_options (string[] args)
         Process.exit (0);
     }
 
+    if (files_list.length == 0)
+        data.files_to_open = null;
+    else
+    {
+        string[] uris = {};
+        foreach (string filename in files_list)
+        {
+            // Call File.new_for_commandline_arg() here (and not in the Latexila class)
+            // because relative path resolution needs the right current working directory,
+            // which can be different for the primary instance.
+            File file = File.new_for_commandline_arg (filename);
+            uris += file.get_uri ();
+        }
+
+        data.files_to_open = new Variant.strv (uris);
+    }
+
     return data;
 }
 
@@ -122,6 +148,9 @@ int main (string[] args)
     if (data.new_window)
         app.activate_action ("new-window", null);
 
+    if (data.files_to_open != null)
+        app.activate_action ("open-files", data.files_to_open);
+
     if (data.new_document)
         app.activate_action ("new-document", null);
 
diff --git a/src/main_window.vala b/src/main_window.vala
index 236d25d..901d056 100644
--- a/src/main_window.vala
+++ b/src/main_window.vala
@@ -433,7 +433,15 @@ public class MainWindow : Window
         drag_data_received.connect ((dc, x, y, selection_data, info, time) =>
         {
             Latexila app = Latexila.get_instance ();
-            app.open_documents (selection_data.get_uris ());
+
+            File[] files = {};
+            foreach (string uri in selection_data.get_uris ())
+            {
+                if (0 < uri.length)
+                    files += File.new_for_uri (uri);
+            }
+
+            app.open_documents (files);
             Gtk.drag_finish (dc, true, true, time);
         });
 
diff --git a/src/utils.vala b/src/utils.vala
index af72c6b..26549cd 100644
--- a/src/utils.vala
+++ b/src/utils.vala
@@ -394,16 +394,6 @@ namespace Utils
     /*************************************************************************/
     // Misc
 
-    public OptionContext get_option_context (OptionEntry[] options)
-    {
-        OptionContext context =
-            new OptionContext (_("- Integrated LaTeX Environment for GNOME"));
-        context.add_main_entries (options, Config.GETTEXT_PACKAGE);
-        context.add_group (Gtk.get_option_group (false));
-
-        return context;
-    }
-
     public void flush_queue ()
     {
         while (Gtk.events_pending ())



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