[aisleriot] app: Move command line handling back outside GApplication



commit 2ca62af5f432e22ef164124b0344a018ccd5a0c9
Author: Christian Persch <chpe gnome org>
Date:   Mon Mar 4 21:21:17 2013 +0100

    app: Move command line handling back outside GApplication
    
    GApplication's command line handling sucks, and it comes too late.

 src/ar-application.c |  122 +++++++++----------------------------------------
 src/ar-application.h |    3 +-
 src/sol.c            |  123 +++++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 142 insertions(+), 106 deletions(-)
---
diff --git a/src/ar-application.c b/src/ar-application.c
index f0b5090..1a945b2 100644
--- a/src/ar-application.c
+++ b/src/ar-application.c
@@ -66,28 +66,6 @@ struct _ArApplicationPrivate
 G_DEFINE_TYPE (ArApplication, ar_application, GTK_TYPE_APPLICATION)
 
 static void
-add_main_options (ArApplication *self,
-                  GOptionContext *context)
-{
-  const GOptionEntry aisleriot_options[] = {
-    { "variation", 'v', 0, G_OPTION_ARG_STRING, &self->priv->variation,
-      N_("Select the game type to play"), N_("NAME") },
-
-    /* Ignored option, for backward compat with saved session */
-    { "freecell", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &self->priv->freecell,
-      NULL, NULL },
-    { "seed", 's', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &self->priv->seed,
-      NULL, NULL },
-
-    { NULL }
-  };
-
-  g_option_context_add_main_entries (context,
-                                     aisleriot_options,
-                                     GETTEXT_PACKAGE);
-}
-
-static void
 action_new_game (GSimpleAction *action,
                  GVariant      *parameter,
                  gpointer       user_data)
@@ -176,75 +154,6 @@ action_quit (GSimpleAction *simple,
   gtk_widget_destroy (GTK_WIDGET (self->priv->window));
 }
 
-static int
-ar_application_command_line (GApplication *application,
-                             GApplicationCommandLine *command_line)
-{
-  ArApplication *self = AR_APPLICATION (application);
-  int argc;
-  char **argv;
-  int retval = 0;
-  GOptionContext *context;
-  GError *error = NULL;
-
-  argv = g_application_command_line_get_arguments (command_line, &argc);
-
-  context = g_option_context_new (NULL);
-
-  add_main_options (self, context);
-
-  g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
-  g_option_context_add_group (context, gtk_get_option_group (TRUE));
-
-#ifdef HAVE_CLUTTER
-  g_option_context_add_group (context, cogl_get_option_group ());
-  g_option_context_add_group (context, clutter_get_option_group_without_init ());
-  g_option_context_add_group (context, gtk_clutter_get_option_group ());
-#endif /* HAVE_CLUTTER */
-
-  retval = g_option_context_parse (context, &argc, &argv, &error);
-  g_option_context_free (context);
-
-  if (!retval)
-    {
-      g_print (_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
-               error->message, argv[0]);
-      g_error_free (error);
-      return retval;
-    }
-
-  g_strfreev (argv);
-
-  /* If we are asked for a specific game, check that it is valid. */
-  if (self->priv->variation != NULL) {
-    char *game_module = NULL;
-
-    if (self->priv->variation[0] != '\0') {
-      game_module = ar_filename_to_game_module (self->priv->variation);
-    }
-
-    g_free (self->priv->variation);
-    self->priv->variation = game_module;
-  }
-
-  if (self->priv->variation == NULL) {
-    char *pref;
-
-    pref = ar_conf_get_string_with_default (NULL, aisleriot_conf_get_key (CONF_VARIATION), 
DEFAULT_VARIATION);
-    self->priv->variation = ar_filename_to_game_module (pref);
-    g_free (pref);
-  }
-
-  g_assert (self->priv->variation != NULL);
-
-
-  aisleriot_window_set_game_module (self->priv->window, self->priv->variation, NULL);
-
-  gtk_widget_show (GTK_WIDGET (self->priv->window));
-
-  return retval;
-}
-
 static void
 ar_application_activate (GApplication *application)
 {
@@ -267,15 +176,17 @@ static void
 ar_application_startup (GApplication *application)
 {
   ArApplication *self = AR_APPLICATION (application);
+  ArApplicationPrivate *priv = self->priv;
   GMenu *menu;
   GMenu *section;
 
   G_APPLICATION_CLASS (ar_application_parent_class)->startup (application);
 
-  aisleriot_conf_init ();
   ar_sound_enable (FALSE);
   ar_stock_init ();
 
+  gtk_window_set_default_icon_name (priv->freecell ? "gnome-freecell" : "gnome-aisleriot");
+
   g_action_map_add_action_entries (G_ACTION_MAP (self),
                                    app_entries, G_N_ELEMENTS (app_entries),
                                    self);
@@ -309,7 +220,13 @@ ar_application_startup (GApplication *application)
 
   gtk_window_set_default_icon_name ("gnome-aisleriot");
 
-  self->priv->window = AISLERIOT_WINDOW (aisleriot_window_new (GTK_APPLICATION (application)));
+  priv->window = AISLERIOT_WINDOW (aisleriot_window_new (GTK_APPLICATION (application)));
+
+  if (priv->freecell) {
+    aisleriot_window_set_game_module (priv->window, FREECELL_VARIATION, NULL);
+  } else {
+    aisleriot_window_set_game_module (priv->window, priv->variation, NULL);
+  }
 }
 
 static void
@@ -341,17 +258,22 @@ ar_application_class_init (ArApplicationClass *class)
 
   application_class->activate = ar_application_activate;
   application_class->startup = ar_application_startup;
-  application_class->command_line = ar_application_command_line;
 
   g_type_class_add_private (class, sizeof (ArApplicationPrivate));
 }
 
 GtkApplication *
-ar_application_new (void)
+ar_application_new (const char *variation,
+                    gboolean freecell)
 {
-  return g_object_new (AR_TYPE_APPLICATION,
-                       "application-id", "org.gnome.Aisleriot",
-                       "flags", G_APPLICATION_NON_UNIQUE |
-                                G_APPLICATION_HANDLES_COMMAND_LINE,
-                       NULL);
+  ArApplication *app;
+
+  app = g_object_new (AR_TYPE_APPLICATION,
+                      "application-id", "org.gnome.Aisleriot",
+                      "flags", G_APPLICATION_NON_UNIQUE,
+                      NULL);
+  app->priv->variation = g_strdup (variation);
+  app->priv->freecell = freecell != FALSE;
+
+  return GTK_APPLICATION (app);
 }
diff --git a/src/ar-application.h b/src/ar-application.h
index 0b8729c..f5ac430 100644
--- a/src/ar-application.h
+++ b/src/ar-application.h
@@ -35,7 +35,8 @@ typedef struct _ArApplicationClass   ArApplicationClass;
 
 GType ar_application_get_type (void);
 
-GtkApplication *ar_application_new (void);
+GtkApplication *ar_application_new (const char *variation,
+                                    gboolean freecell);
 
 G_END_DECLS
 
diff --git a/src/sol.c b/src/sol.c
index 18c4d24..0b82226 100644
--- a/src/sol.c
+++ b/src/sol.c
@@ -19,23 +19,138 @@
 
 #include <config.h>
 
+#include <string.h>
+
 #include <libguile.h>
 
 #include <glib.h>
 #include <glib/gi18n.h>
 
+#include <gtk/gtk.h>
+
+#ifdef HAVE_CLUTTER
+#include <cogl/cogl.h>
+#include <clutter/clutter.h>
+#include <clutter-gtk/clutter-gtk.h>
+#endif
+
+#include "ar-debug.h"
+#include "ar-stock.h"
 #include "ar-runtime.h"
+#include "ar-sound.h"
+
+#include "ar-string-utils.h"
+#include "conf.h"
+#include "window.h"
+#include "game.h"
+#include "util.h"
 #include "ar-application.h"
 
+#if 0
+/* String reserve */
+N_("Solitaire")
+N_("GNOME Solitaire")
+N_("About Solitaire")
+#endif /* 0 */
+
+typedef struct {
+  char *variation;
+  gint seed; /* unused */
+  gboolean freecell;
+} AppData;
+
+static void
+add_main_options (GOptionContext *option_context,
+                  AppData *data)
+{
+  const GOptionEntry aisleriot_options[] = {
+    { "variation", 'v', 0, G_OPTION_ARG_STRING, &data->variation,
+      N_("Select the game type to play"), N_("NAME") },
+    { "freecell", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &data->freecell,
+      NULL, NULL },
+
+    /* Ignored option, for backward compat with saved session */
+    { "seed", 's', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &data->seed,
+      NULL, NULL },
+
+    { NULL }
+  };
+
+  g_option_context_add_main_entries (option_context,
+                                    aisleriot_options, GETTEXT_PACKAGE);
+}
+
 static void
 main_prog (void *closure, int argc, char *argv[])
 {
+  AppData data;
+  GOptionContext *option_context;
+  GError *error = NULL;
+  gboolean retval;
   GtkApplication *application;
-  int status;
+  int status G_GNUC_UNUSED;
+
+  memset (&data, 0, sizeof (AppData));
+
+  option_context = g_option_context_new (NULL);
+  g_option_context_set_translation_domain (option_context, GETTEXT_PACKAGE);
+
+  add_main_options (option_context, &data);
+
+  g_option_context_add_group (option_context, gtk_get_option_group (TRUE));
 
-  application = ar_application_new ();
-  status = g_application_run (G_APPLICATION (application), argc, argv);
+#ifdef HAVE_CLUTTER
+  g_option_context_add_group (option_context, cogl_get_option_group ());
+  g_option_context_add_group (option_context, clutter_get_option_group_without_init ());
+  g_option_context_add_group (option_context, gtk_clutter_get_option_group ());
+#endif /* HAVE_CLUTTER */
+
+  retval = g_option_context_parse (option_context, &argc, &argv, &error);
+  g_option_context_free (option_context);
+
+  if (!retval) {
+    g_printerr ("%s\n", error->message);
+    g_error_free (error);
+    goto cleanup;
+  }
+
+  g_set_application_name (data.freecell ? _("FreeCell Solitaire") : _("AisleRiot"));
+
+  aisleriot_conf_init ();
+
+  /* If we are asked for a specific game, check that it is valid. */
+  if (!data.freecell &&
+      data.variation != NULL) {
+    char *game_module = NULL;
+
+    if (data.variation[0] != '\0') {
+      game_module = ar_filename_to_game_module (data.variation);
+    }
+
+    g_free (data.variation);
+    data.variation = game_module;
+  }
+
+  if (!data.freecell && !data.variation) {
+    char *pref;
+
+    pref = ar_conf_get_string_with_default (NULL, aisleriot_conf_get_key (CONF_VARIATION), 
DEFAULT_VARIATION);
+    data.variation = ar_filename_to_game_module (pref);
+    g_free (pref);
+  }
+
+  g_assert (data.variation != NULL || data.freecell);
+
+  application = ar_application_new (data.variation, data.freecell);
+  status = g_application_run (G_APPLICATION (application), 0, NULL);
   g_object_unref (application);
+
+  aisleriot_conf_shutdown ();
+
+cleanup:
+  g_free (data.variation);
+
+  ar_runtime_shutdown ();
 }
 
 int
@@ -51,7 +166,5 @@ main (int argc, char *argv[])
 
   scm_boot_guile (argc, argv, main_prog, NULL); /* no return */
 
-  ar_runtime_shutdown ();
-
   return 0;
 }


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