[vinagre/vala-rewrite: 1/3] Port application entry point to Vala



commit 5bdea21cec896c62ceaea10d52bea773909e2cf3
Author: David King <amigadave amigadave com>
Date:   Wed Jun 15 21:57:04 2011 +0200

    Port application entry point to Vala

 Makefile.am                      |    4 +-
 vinagre/vinagre-application.vala |  163 +++++++++++++++++++++++++++++++++
 vinagre/vinagre-main.c           |  184 --------------------------------------
 vinagre/vinagre-options.c        |  151 -------------------------------
 vinagre/vinagre-options.h        |   43 ---------
 5 files changed, 164 insertions(+), 381 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 791b194..1d197a4 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -37,8 +37,7 @@ ifaddrs_sources = \
 endif
 
 vinagre_vinagre_SOURCES = \
-	vinagre/vinagre-main.c \
-	vinagre/vinagre-options.c
+	vinagre/vinagre-application.vala
 
 vinagre_vinagre_LDADD = \
 	libvinagre.la \
@@ -69,7 +68,6 @@ noinst_vinagreh_headers = \
 	vinagre/vinagre-dirs.h \
 	vinagre/vinagre-dnd.h \
 	vinagre/vinagre-notebook.h \
-	vinagre/vinagre-options.h \
 	vinagre/vinagre-plugins-engine.h \
 	vinagre/vinagre-prefs.h \
 	vinagre/vinagre-protocol.h \
diff --git a/vinagre/vinagre-application.vala b/vinagre/vinagre-application.vala
new file mode 100644
index 0000000..f702dd7
--- /dev/null
+++ b/vinagre/vinagre-application.vala
@@ -0,0 +1,163 @@
+/*  Vinagre - GNOME Remote Desktop viewer
+ *
+ *  Copyright (C) 2011  David King <amigadave amigadave com>
+ *
+ *  This program 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 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using Gtk;
+
+public class Vinagre.Application : Gtk.Application {
+    private Vinagre.MainWindow main_window;
+    private Vinagre.CacheSettings cache_settings;
+    // FIXME: Place in optionstate struct.
+    private string geometry;
+    private bool fullscreen;
+    private bool new_window;
+    private string filenames[];
+    private string uris[];
+
+    // FIXME: Move into Vinagre.Options?
+    const OptionEntry[] options = {
+        {"geometry", 0, 0, OptionArg.STRING, ref geometry,
+            N_("Specify geometry of the main Vinagre window"), null},
+        {"fullscreen", 'f', 0, OptionArg.NONE, ref fullscreen,
+            N_("Open Vinagre in fullscreen mode"), null},
+        {"new-window", 'n', 0, OptionArg.NONE, ref new_window,
+            N_("Create a new toplevel window in an existing instance of Vinagre"),
+            null},
+        {"file", 'F', 0, OptionArg.FILENAME_ARRAY, ref filenames,
+            N_("Open a file regognized by Vinagre"), null},
+        // "" in this case is equivalent to G_OPTION_REMAINING.
+        {"", 0, 0, OptionArg.STRING_ARRAY, ref uris, null, N_("[server:port]")},
+        {null}
+    };
+
+    public Application (string app_id, ApplicationFlags flags) {
+        GLib.Object (application_id: app_id, flags: flags);
+    }
+
+    public void on_activate () {
+        if (get_windows() != null)
+	        main_window.present_with_time (Gdk.CURRENT_TIME);
+        else {
+	        main_window = new Vinagre.MainWindow ();
+            cache_settings = new Vinagre.CacheSettings ();
+
+            Environment.set_application_name (_("Remote Desktop Viewer");
+            Window.set_default_icon_name (Config.PACKAGE_TARNAME);
+
+            main_window.destroy.connect (Gtk.main_quit);
+            main_window.show ();
+        }
+    }
+
+    public int on_command_line (ApplicationCommandLine command_line) {
+        var arguments = command_line.arguments;
+
+        try {
+            // FIXME: make the string the same as above, and use printf.
+            var context = new OptionContext (_("â Remote Desktop Viewer"));
+            context.add_main_entries (options, Config.GETTEXT_PACKAGE);
+            context.add_group (Gtk.get_option_group (true));
+
+            // TODO: Register plugin stuff with the context here.
+            context.parse (ref arguments);
+            process_parsed_command_line ();
+        }
+        catch (OptionError error) {
+            command_line.printerr ("%s\n", error.message);
+            command_line.printerr (_("Run â%s --helpâ to see a full list of available command-line options.\n", arguments[0]);
+            // FIXME: EXIT_FAILURE?
+            command_line.set_exit_status (1);
+        }
+    }
+
+    // FIXME: Should accept a window parameter.
+    private void process_parsed_command_line () {
+        SList<string> errors;
+        SList<Connection> connections;
+
+        if (files != null) {
+            files.foreach ((file) => {
+                try {
+                    var connection = Connection.new_from_file (file);
+                    connections.append (connection);
+                } catch (Error error) {
+                    errors.append ("<i>%s</i>: %s", file, error.message);
+                }
+            });
+        }
+
+        if (uris != null) {
+            uris.foreach ((uri) => {
+            try {
+                var connection = Connection.new_from_string (uri);
+                connections.append (connection);
+            } catch (Error error) {
+                errors.append ("<i>%s</i>: %s", uri, error.message);
+            });
+        }
+
+        if (connections != null && new_window) {
+            // TODO: Replace with Application.new_window().
+            var window = new MainWindow();
+            MainWindow.show_all ();
+            MainWindow.set_application (this);
+        }
+
+        if (geometry != null) {
+            var windows = get_windows ();
+            if (!windows[0].parse_geometry (geometry))
+                errors.append (_("Invalid argument %s for --geometry"),
+                    geometry);
+
+            // FIXME: Why unmaximize?
+            windows[0].unmaximize ();
+        }
+
+        servers.foreach ((connection), {
+            connection.set_fullscreen (fullscreen);
+            Command.direct_connect (connection, windows[0]);
+        });
+
+        if (errors != null)
+            Utils.show_many_errors (ngettext
+                ("The following error has occurred:",
+                "The following errors have occurred:", errors.length (), errors,
+                windows[0]);
+
+        window[0].present ();
+    }
+}
+
+// Application entry point.
+public int main (string[] args) {
+    Intl.bindtextdomain (Vinagre.Config.GETTEXT_PACKAGE,
+        Vinagre.Config.PACKAGE_LOCALEDIR);
+    Intl.bind_textdomain_codeset (Vinagre.Config.GETTEXT_PACKAGE, "UTF-8");
+    Intl.textdomain (Vinagre.Config.GETTEXT_PACKAGE);
+
+    // This used to be "org.gnome.vinagre".
+    var app = new Vinagre.Application ("org.gnome.Vinagre",
+        ApplicationFlags.HANDLES_COMMAND_LINE);
+
+    app.activate.connect (on_activate);
+    app.command_line.connect (on_command_line);
+
+    // run() initialises GTK+, so there is no need to do it manually.
+    var result = app.run (args);
+
+    return result;
+}



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