[goobox] Ported to GtkApplication



commit 2e8046baed5510ab6def62e090d33c26cd2d7aef
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Sun Jun 5 14:15:20 2011 +0200

    Ported to GtkApplication

 configure.ac     |    1 -
 src/goo-window.c |   10 +-
 src/gth-window.c |    6 +-
 src/main.c       |  617 ++++++++++++++++++++++++++++++------------------------
 src/main.h       |    7 +-
 5 files changed, 354 insertions(+), 287 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 67381ad..e276649 100644
--- a/configure.ac
+++ b/configure.ac
@@ -35,7 +35,6 @@ PKG_CHECK_MODULES(GOO, [
 	libbrasero-media3
 	libmusicbrainz3 >= $LIBMUSICBRAINZ3_REQUIRED
 	libdiscid
-	unique-3.0
 ])
 AC_SUBST(GOO_CFLAGS)
 AC_SUBST(GOO_LIBS)
diff --git a/src/goo-window.c b/src/goo-window.c
index f5c6a9b..7d54b6d 100644
--- a/src/goo-window.c
+++ b/src/goo-window.c
@@ -978,7 +978,7 @@ first_time_idle (gpointer callback_data)
 	GooWindow *window = callback_data;
 
 	g_source_remove (window->priv->first_time_event);
-	/* goo_player_update (window->priv->player); FIXME */
+	goo_player_update (window->priv->player);
 
 	return FALSE;
 }
@@ -990,10 +990,10 @@ goo_window_show (GtkWidget *widget)
 	GooWindow *window = GOO_WINDOW (widget);
 	gboolean   view_foobar;
 
-	if (! HideShow)
+	if (! arg_toggle_visibility)
 		GTK_WIDGET_CLASS (goo_window_parent_class)->show (widget);
 	else
-		HideShow = FALSE;
+		arg_toggle_visibility = FALSE;
 
 	view_foobar = g_settings_get_boolean (window->priv->settings_ui, PREF_UI_TOOLBAR);
 	set_active (window, "ViewToolbar", view_foobar);
@@ -1641,8 +1641,8 @@ player_done_cb (GooPlayer       *player,
 		goo_window_update_cover (window);
 		window_update_title (window);
 		set_current_track_icon (window, NULL);
-		if (AutoPlay || g_settings_get_boolean (window->priv->settings_general, PREF_GENERAL_AUTOPLAY)) {
-			AutoPlay = FALSE;
+		if (arg_auto_play || g_settings_get_boolean (window->priv->settings_general, PREF_GENERAL_AUTOPLAY)) {
+			arg_auto_play = FALSE;
 			g_timeout_add (AUTOPLAY_DELAY, autoplay_cb, window);
 		}
 		break;
diff --git a/src/gth-window.c b/src/gth-window.c
index 458aac4..7223f10 100644
--- a/src/gth-window.c
+++ b/src/gth-window.c
@@ -24,6 +24,7 @@
 #include <gtk/gtk.h>
 #include "gth-window.h"
 #include "gtk-utils.h"
+#include "main.h"
 
 
 #define GTH_WINDOW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTH_TYPE_WINDOW, GthWindowPrivate))
@@ -127,12 +128,9 @@ gth_window_finalize (GObject *object)
 	g_free (window->priv->toolbars);
 	g_free (window->priv->contents);
 
-	window_list = g_list_remove (window_list, window);
+	gtk_application_remove_window (main_application, GTK_WINDOW (window));
 
 	G_OBJECT_CLASS (parent_class)->finalize (object);
-
-	if (window_list == NULL)
-		gtk_main_quit ();
 }
 
 
diff --git a/src/main.c b/src/main.c
index 1eef80e..a8e2871 100644
--- a/src/main.c
+++ b/src/main.c
@@ -7,8 +7,8 @@
  *
  *  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 2 of the License, or
- *  (at your option) any later version.
+ *  the Free Software Foundation; either arg_version 2 of the License, or
+ *  (at your option) any later arg_version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -24,7 +24,8 @@
 #include <brasero3/brasero-medium-monitor.h>
 #include <gst/gst.h>
 #include <glib.h>
-#include <unique/unique.h>
+#include <glib/gprintf.h>
+#include <gtk/gtk.h>
 #include "eggsmclient.h"
 #include "goo-stock.h"
 #include "goo-window.h"
@@ -33,7 +34,6 @@
 #include "main.h"
 #include "gtk-utils.h"
 #include "glib-utils.h"
-
 #ifdef ENABLE_NOTIFICATION
 #include <libnotify/notify.h>
 #ifndef NOTIFY_CHECK_VERSION
@@ -42,234 +42,207 @@
 static NotifyNotification *notification = NULL;
 #endif /* ENABLE_NOTIFICATION */
 
+
 #define VOLUME_STEP 0.10
 
-enum {
-	COMMAND_UNUSED,
-	COMMAND_PLAY,
-	COMMAND_PLAY_PAUSE,
-	COMMAND_STOP,
-	COMMAND_NEXT_TRACK,
-	COMMAND_PREVIOUS_TRACK,
-	COMMAND_EJECT_DISK,
-	COMMAND_HIDE_SHOW,
-	COMMAND_VOLUME_UP,
-	COMMAND_VOLUME_DOWN,
-	COMMAND_QUIT,
-	COMMAND_PRESENT,
-	COMMAND_SET_DEVICE
-};
 
-GtkWidget *main_window = NULL;
-int        AutoPlay = FALSE;
-int        PlayPause = FALSE;
-int        Stop = FALSE;
-int        Next = FALSE;
-int        Prev = FALSE;
-int        Eject = FALSE;
-int        HideShow = FALSE;
-int        VolumeUp = FALSE;
-int        VolumeDown = FALSE;
-int        Quit = FALSE;
-
-static void release_data (void);
-
-static UniqueApp  *application = NULL;
-static const char *program_argv0; /* argv[0] from main(); used as the command to restart the program */
-static char       *default_device = NULL;
-static gboolean    version = FALSE;
+/* -- command line arguments -- */
+
+
+GtkApplication        *main_application = NULL;
+int                    arg_auto_play = FALSE;
+int                    arg_toggle_visibility = FALSE;
+static int             arg_toggle_play = FALSE;
+static int             arg_stop = FALSE;
+static int             arg_next = FALSE;
+static int             arg_prev = FALSE;
+static int             arg_eject = FALSE;
+static int             arg_volume_up = FALSE;
+static int             arg_volume_down = FALSE;
+static int             arg_quit = FALSE;
+static gboolean        arg_version = FALSE;
+static char           *arg_device = NULL;
+static const char     *program_argv0; /* argv[0] from main(); used as the command to restart the program */
+
 
 static const GOptionEntry options[] = {
-	{ "device", 'd',  0, G_OPTION_ARG_STRING, &default_device,
+	{ "device", 'd',  0, G_OPTION_ARG_STRING, &arg_device,
 	  N_("CD device to be used"),
 	  N_("DEVICE_PATH") },
-	{ "play", '\0', 0, G_OPTION_ARG_NONE, &AutoPlay,
+	{ "play", '\0', 0, G_OPTION_ARG_NONE, &arg_auto_play,
           N_("Play the CD on startup"),
           0 },
-	{ "play-pause", '\0', 0, G_OPTION_ARG_NONE, &PlayPause,
-          N_("Play/Pause"),
+	{ "toggle-play", '\0', 0, G_OPTION_ARG_NONE, &arg_toggle_play,
+          N_("Toggle play"),
           0 },
-        { "stop", '\0', 0, G_OPTION_ARG_NONE, &Stop,
+        { "stop", '\0', 0, G_OPTION_ARG_NONE, &arg_stop,
           N_("Stop playing"),
           0 },
-	{ "next", '\0', 0, G_OPTION_ARG_NONE, &Next,
+	{ "next", '\0', 0, G_OPTION_ARG_NONE, &arg_next,
           N_("Play the next track"),
           0 },
-	{ "previous", '\0', 0, G_OPTION_ARG_NONE, &Prev,
+	{ "previous", '\0', 0, G_OPTION_ARG_NONE, &arg_prev,
           N_("Play the previous track"),
           0 },
-	{ "eject", '\0', 0, G_OPTION_ARG_NONE, &Eject,
+	{ "eject", '\0', 0, G_OPTION_ARG_NONE, &arg_eject,
           N_("Eject the CD"),
           0 },
-	{ "hide-show", '\0', 0, G_OPTION_ARG_NONE, &HideShow,
-          N_("Hide/Show the main window"),
+	{ "toggle-visibility", '\0', 0, G_OPTION_ARG_NONE, &arg_toggle_visibility,
+          N_("Toggle the main window visibility"),
           0 },
-	{ "volume-up", '\0', 0, G_OPTION_ARG_NONE, &VolumeUp,
+	{ "volume-up", '\0', 0, G_OPTION_ARG_NONE, &arg_volume_up,
           N_("Volume Up"),
           0 },
-	{ "volume-down", '\0', 0, G_OPTION_ARG_NONE, &VolumeDown,
+	{ "volume-down", '\0', 0, G_OPTION_ARG_NONE, &arg_volume_down,
           N_("Volume Down"),
           0 },
-	{ "quit", '\0', 0, G_OPTION_ARG_NONE, &Quit,
+	{ "quit", '\0', 0, G_OPTION_ARG_NONE, &arg_quit,
           N_("Quit the application"),
           0 },
-
-          { "version", 'v', 0, G_OPTION_ARG_NONE, &version,
-  	  N_("Show version"), NULL },
-
+        { "version", 'v', 0, G_OPTION_ARG_NONE, &arg_version,
+    	  N_("Show version"),
+    	  0 },
 	{ NULL }
 };
 
 
-/* session management */
+/* -- session management -- */
 
 
 static void
-goo_save_state (EggSMClient *client,
-		GKeyFile    *state,
-		gpointer     user_data)
+client_save_state (EggSMClient *client,
+		   GKeyFile    *state,
+		   gpointer     user_data)
 {
-	const char *argv[2] = { NULL };
+	GApplication *application = user_data;
+	const char   *argv[2] = { NULL };
+	guint         i;
+	GList        *scan;
 
 	argv[0] = program_argv0;
 	argv[1] = NULL;
 	egg_sm_client_set_restart_command (client, 1, argv);
 
-	g_key_file_set_string (state, "Session", "/device", goo_player_get_device (goo_window_get_player (GOO_WINDOW (main_window))));
-}
+	i = 0;
+	for (scan = gtk_application_get_windows (GTK_APPLICATION (application)); scan; scan = scan->next) {
+		GtkWidget *window = scan->data;
+		char      *key;
 
+		key = g_strdup_printf ("device%d", ++i);
+		g_key_file_set_string (state,
+				       "Session",
+				       key,
+				       goo_player_get_device (goo_window_get_player (GOO_WINDOW (window))));
 
-static void
-goo_session_manager_init (void)
-{
-	EggSMClient *client = NULL;
+		g_free (key);
+	}
 
-	client = egg_sm_client_get ();
-	g_signal_connect (client, "save-state", G_CALLBACK (goo_save_state), NULL);
+	g_key_file_set_integer (state, "Session", "devices", i);
 }
 
 
 static void
-goo_restore_session (EggSMClient *client)
+client_quit_requested_cb (EggSMClient *client,
+			  gpointer     data)
 {
-	GKeyFile     *state = NULL;
-	char         *device;
-	BraseroDrive *drive;
-
-	state = egg_sm_client_get_state_file (client);
+	egg_sm_client_will_quit (client, TRUE);
+}
 
-	device = g_key_file_get_string (state, "Session", "device", NULL);
-	drive = main_get_drive_for_device (device);
-	main_window = goo_window_new (drive);
-	gtk_widget_show (main_window);
 
-	g_object_unref (drive);
-	g_free (device);
+static void
+client_quit_cb (EggSMClient *client,
+		gpointer     data)
+{
+	gtk_main_quit ();
 }
 
 
-static UniqueResponse
-unique_app_message_received_cb (UniqueApp         *unique_app,
-				UniqueCommand      command,
-				UniqueMessageData *message,
-				guint              time_,
-				gpointer           user_data)
+static void
+goo_session_manager_init (GApplication *application)
 {
-	UniqueResponse  res;
+	EggSMClient *client = NULL;
 
-	res = UNIQUE_RESPONSE_OK;
+	client = egg_sm_client_get ();
+	g_signal_connect (client,
+			  "save_state",
+			  G_CALLBACK (client_save_state),
+			  application);
+	g_signal_connect (client,
+			  "quit_requested",
+			  G_CALLBACK (client_quit_requested_cb),
+			  application);
+	g_signal_connect (client,
+			  "quit",
+			  G_CALLBACK (client_quit_cb),
+			  application);
+}
 
-	switch (command) {
-	case UNIQUE_OPEN:
-	case UNIQUE_NEW:
-		/* FIXME */
-		break;
 
-	case COMMAND_PLAY:
-		goo_window_play (GOO_WINDOW (main_window));
-		break;
+static void
+goo_restore_session (EggSMClient  *client,
+		     GApplication *application)
+{
+	GKeyFile *state = NULL;
+	guint     i;
 
-	case COMMAND_PLAY_PAUSE:
-		goo_window_toggle_play (GOO_WINDOW (main_window));
-		break;
+	state = egg_sm_client_get_state_file (client);
+	i = g_key_file_get_integer (state, "Session", "devices", NULL);
+	g_assert (i > 0);
+	for (/* void */; i > 0; i--) {
+		char         *key;
+		char         *device;
+		BraseroDrive *drive;
+		GtkWidget    *window;
+
+		key = g_strdup_printf ("device%d", i);
+		device = g_key_file_get_string (state, "Session", key, NULL);
+		g_free (key);
+
+		g_assert (device != NULL);
+
+		drive = main_get_drive_for_device (device);
+		window = goo_window_new (drive);
+		gtk_window_set_application (GTK_WINDOW (window), GTK_APPLICATION (application));
+		gtk_widget_show (window);
+
+		g_object_unref (drive);
+		g_free (device);
+	}
+}
 
-	case COMMAND_STOP:
-		goo_window_stop (GOO_WINDOW (main_window));
-		break;
 
-	case COMMAND_NEXT_TRACK:
-		goo_window_next (GOO_WINDOW (main_window));
-		break;
+/* -- main application -- */
 
-	case COMMAND_PREVIOUS_TRACK:
-		goo_window_prev (GOO_WINDOW (main_window));
-		break;
 
-	case COMMAND_EJECT_DISK:
-		goo_window_eject (GOO_WINDOW (main_window));
-		break;
+typedef GtkApplication      GooApplication;
+typedef GtkApplicationClass GooApplicationClass;
 
-	case COMMAND_HIDE_SHOW:
-		goo_window_toggle_visibility (GOO_WINDOW (main_window));
-		break;
 
-	case COMMAND_VOLUME_UP:
-		{
-			double volume;
+G_DEFINE_TYPE (GooApplication, goo_application, GTK_TYPE_APPLICATION)
 
-			volume = goo_window_get_volume (GOO_WINDOW (main_window));
-			goo_window_set_volume (GOO_WINDOW (main_window), volume + VOLUME_STEP);
-		}
-		break;
 
-	case COMMAND_VOLUME_DOWN:
-		{
-			double volume;
+static void
+goo_application_finalize (GObject *object)
+{
+	G_OBJECT_CLASS (goo_application_parent_class)->finalize (object);
+}
 
-			volume = goo_window_get_volume (GOO_WINDOW (main_window));
-			goo_window_set_volume (GOO_WINDOW (main_window), volume - VOLUME_STEP);
-		}
-		break;
 
-	case COMMAND_QUIT:
-		goo_window_close (GOO_WINDOW (main_window));
-		break;
+static void
+goo_application_init (GooApplication *app)
+{
+	g_set_application_name (_("CD Player"));
+}
 
-	case COMMAND_PRESENT:
-		if (gtk_widget_get_visible (GTK_WIDGET (main_window)))
-			gtk_window_present (GTK_WINDOW (main_window));
-		else
-			goo_window_toggle_visibility (GOO_WINDOW (main_window));
-		break;
-
-	case COMMAND_SET_DEVICE:
-		{
-			char *device;
-
-			device = unique_message_data_get_text (message);
-			if (*device == '\0')
-				device = NULL;
-			if (device != NULL) {
-				BraseroDrive *drive;
-
-				drive = main_get_drive_for_device (device);
-				main_window = main_get_window_from_device (device);
-				if (main_window == NULL)
-					main_window = goo_window_new (drive);
-				else
-					goo_window_set_drive (GOO_WINDOW (main_window), drive);
-
-				g_object_unref (drive);
-			}
-		}
-		break;
 
-	default:
-		res = UNIQUE_RESPONSE_PASSTHROUGH;
-		break;
-	}
+static void
+goo_application_activate (GApplication *application)
+{
+	GtkWidget *window;
 
-	return res;
+	window = goo_window_new (NULL);
+	gtk_window_set_application (GTK_WINDOW (window), GTK_APPLICATION (application));
+	gtk_widget_show (window);
 }
 
 
@@ -296,19 +269,18 @@ required_gstreamer_plugins_available (void)
 }
 
 
-static void
-release_data (void)
-{
-	_g_object_unref (application);
-}
-
-
-static void
-prepare_application (void)
+static gboolean
+init_application (GApplication *application)
 {
 	GtkSettings *gtk_settings;
 	EggSMClient *client = NULL;
 
+        gtk_settings = gtk_settings_get_default ();
+        g_object_set (G_OBJECT (gtk_settings), "gtk-application-prefer-dark-theme", TRUE, NULL);
+
+        gtk_window_set_default_icon_name ("goobox");
+        goo_stock_init ();
+
 	if (! required_gstreamer_plugins_available ()) {
 		GtkWidget *d;
 		d = _gtk_message_dialog_new (NULL,
@@ -323,88 +295,210 @@ prepare_application (void)
 				  NULL);
 		gtk_widget_show (d);
 
-		return;
+		return FALSE;
 	}
 
-        gtk_settings = gtk_settings_get_default ();
-        g_object_set (G_OBJECT (gtk_settings), "gtk-application-prefer-dark-theme", TRUE, NULL);
+#ifdef ENABLE_NOTIFICATION
+	if (! notify_init (g_get_application_name ()))
+                g_warning ("Cannot initialize notification system.");
+#endif /* ENABLE_NOTIFICATION */
 
-	application = unique_app_new_with_commands ("org.gnome.goobox", NULL,
-						    "auto-play", COMMAND_PLAY,
-						    "play-pause", COMMAND_PLAY_PAUSE,
-						    "stop", COMMAND_STOP,
-						    "next", COMMAND_NEXT_TRACK,
-						    "prev", COMMAND_PREVIOUS_TRACK,
-						    "eject", COMMAND_EJECT_DISK,
-						    "hide-show", COMMAND_HIDE_SHOW,
-						    "volume-up", COMMAND_VOLUME_UP,
-						    "volume-down", COMMAND_VOLUME_DOWN,
-						    "quit", COMMAND_QUIT,
-						    "present", COMMAND_PRESENT,
-						    "set-device", COMMAND_SET_DEVICE,
-						    NULL);
-
-	if (unique_app_is_running (application)) {
-		if (default_device != NULL) {
-			UniqueMessageData *data;
-
-			data = unique_message_data_new ();
-			unique_message_data_set_text (data, default_device, -1);
-			unique_app_send_message (application, COMMAND_SET_DEVICE, data);
-
-			unique_message_data_free (data);
-		}
+	goo_session_manager_init (application);
 
-		if (AutoPlay)
-			unique_app_send_message (application, COMMAND_PLAY, NULL);
-		else if (PlayPause)
-			unique_app_send_message (application, COMMAND_PLAY_PAUSE, NULL);
-		else if (Stop)
-			unique_app_send_message (application, COMMAND_STOP, NULL);
-		else if (Next)
-			unique_app_send_message (application, COMMAND_NEXT_TRACK, NULL);
-		else if (Prev)
-			unique_app_send_message (application, COMMAND_PREVIOUS_TRACK, NULL);
-		else if (Eject)
-			unique_app_send_message (application, COMMAND_EJECT_DISK, NULL);
-		else if (HideShow)
-			unique_app_send_message (application, COMMAND_HIDE_SHOW, NULL);
-		else if (VolumeUp)
-			unique_app_send_message (application, COMMAND_VOLUME_UP, NULL);
-		else if (VolumeDown)
-			unique_app_send_message (application, COMMAND_VOLUME_DOWN, NULL);
-		else if (Quit)
-			unique_app_send_message (application, COMMAND_QUIT, NULL);
+	client = egg_sm_client_get ();
+	if (egg_sm_client_is_resumed (client)) {
+		goo_restore_session (client, application);
+		return TRUE;
+	}
 
-		return;
+	return TRUE;
+}
+
+
+static int
+goo_application_command_line (GApplication            *application,
+			      GApplicationCommandLine *command_line)
+{
+	char           **argv;
+	int              argc;
+	GOptionContext  *options_context;
+	GError          *error = NULL;
+	GList           *window_list;
+	GtkWidget       *window;
+
+	argv = g_application_command_line_get_arguments (command_line, &argc);
+
+	options_context = g_option_context_new (N_("Play CDs and save the tracks to disk as files"));
+	g_option_context_set_translation_domain (options_context, GETTEXT_PACKAGE);
+	g_option_context_add_main_entries (options_context, options, GETTEXT_PACKAGE);
+	g_option_context_add_group (options_context, gtk_get_option_group (TRUE));
+	g_option_context_add_group (options_context, egg_sm_client_get_option_group ());
+	g_option_context_add_group (options_context, gst_init_get_option_group ());
+	g_option_context_set_ignore_unknown_options (options_context, TRUE);
+	if (! g_option_context_parse (options_context, &argc, &argv, &error)) {
+		g_critical ("Failed to parse arguments: %s", error->message);
+		g_error_free (error);
+		return EXIT_FAILURE;
 	}
 
-	if (! unique_app_is_running (application)) {
-	        g_set_application_name (_("CD Player"));
-	        gtk_window_set_default_icon_name ("goobox");
-	        goo_stock_init ();
-		g_signal_connect (application,
-				  "message-received",
-				  G_CALLBACK (unique_app_message_received_cb),
-				  NULL);
+	window_list = gtk_application_get_windows (GTK_APPLICATION (application));
+	if (window_list == NULL) {
+		if (! init_application (application))
+			return 0;
+		window = goo_window_new (NULL);
+		gtk_window_set_application (GTK_WINDOW (window), GTK_APPLICATION (application));
+		gtk_widget_show (window);
 	}
+	else
+		window = window_list->data;
 
-	client = egg_sm_client_get ();
-	if (egg_sm_client_is_resumed (client)) {
-		goo_restore_session (client);
-		return;
+	if (arg_auto_play) {
+		goo_window_play (GOO_WINDOW (window));
+	}
+	else if (arg_toggle_play) {
+		goo_window_toggle_play (GOO_WINDOW (window));
+	}
+	else if (arg_stop) {
+		goo_window_stop (GOO_WINDOW (window));
+	}
+	else if (arg_next) {
+		goo_window_next (GOO_WINDOW (window));
+	}
+	else if (arg_prev) {
+		goo_window_prev (GOO_WINDOW (window));
+	}
+	else if (arg_eject) {
+		goo_window_eject (GOO_WINDOW (window));
+	}
+	else if (arg_toggle_visibility) {
+		goo_window_toggle_visibility (GOO_WINDOW (window));
+	}
+	else if (arg_volume_up) {
+		double volume;
+
+		volume = goo_window_get_volume (GOO_WINDOW (window));
+		goo_window_set_volume (GOO_WINDOW (window), volume + VOLUME_STEP);
+	}
+	else if (arg_volume_down) {
+		double volume;
+
+		volume = goo_window_get_volume (GOO_WINDOW (window));
+		goo_window_set_volume (GOO_WINDOW (window), volume - VOLUME_STEP);
+	}
+	else if (arg_quit) {
+		goo_window_close (GOO_WINDOW (window));
+	}
+	else if (arg_device != NULL) {
+		BraseroDrive *drive;
+
+		drive = main_get_drive_for_device (arg_device);
+		window = main_get_window_from_device (arg_device);
+		if (window == NULL) {
+			window = goo_window_new (drive);
+			gtk_window_set_application (GTK_WINDOW (window), GTK_APPLICATION (application));
+			gtk_widget_show (window);
+		}
+		else
+			goo_window_set_drive (GOO_WINDOW (window), drive);
+
+		g_object_unref (drive);
+		g_free (arg_device);
+		arg_device = NULL;
+	}
+
+	/* reset arguments */
+
+	arg_auto_play = FALSE;
+	arg_toggle_play = FALSE;
+	arg_stop = FALSE;
+	arg_next = FALSE;
+	arg_prev = FALSE;
+	arg_eject = FALSE;
+	arg_toggle_visibility = FALSE;
+	arg_volume_up = FALSE;
+	arg_volume_down = FALSE;
+	arg_quit = FALSE;
+	g_free (arg_device);
+	arg_device = NULL;
+
+	return 0;
+}
+
+
+static gboolean
+goo_application_local_command_line (GApplication   *application,
+				    char         ***arguments,
+				    int            *exit_status)
+{
+	char           **local_argv;
+	int              local_argc;
+	GOptionContext  *options_context;
+	GError          *error = NULL;
+	gboolean         handled_locally = FALSE;
+
+	local_argv = g_strdupv (*arguments);
+	local_argc = g_strv_length (local_argv);
+
+	*exit_status = 0;
+
+	options_context = g_option_context_new (N_("Play CDs and save the tracks to disk as files"));
+	g_option_context_set_translation_domain (options_context, GETTEXT_PACKAGE);
+	g_option_context_add_main_entries (options_context, options, GETTEXT_PACKAGE);
+	g_option_context_add_group (options_context, gtk_get_option_group (TRUE));
+	g_option_context_add_group (options_context, egg_sm_client_get_option_group ());
+	g_option_context_add_group (options_context, gst_init_get_option_group ());
+	g_option_context_set_ignore_unknown_options (options_context, TRUE);
+	if (! g_option_context_parse (options_context, &local_argc, &local_argv, &error)) {
+		*exit_status = EXIT_FAILURE;
+		g_critical ("Failed to parse arguments: %s", error->message);
+		g_clear_error (&error);
+		handled_locally = TRUE;
+	}
+
+	if (arg_version) {
+		g_printf ("%s %s, Copyright © 2001-2011 Free Software Foundation, Inc.\n", PACKAGE_NAME, PACKAGE_VERSION);
+		handled_locally = TRUE;
 	}
 
-	main_window = goo_window_new (NULL);
-	gtk_widget_show (main_window);
+	g_strfreev (local_argv);
+
+	return handled_locally;
+}
+
+
+static void
+goo_application_class_init (GooApplicationClass *klass)
+{
+	GObjectClass      *object_class;
+	GApplicationClass *application_class;
+
+	object_class = G_OBJECT_CLASS (klass);
+	object_class->finalize = goo_application_finalize;
+
+	application_class = G_APPLICATION_CLASS (klass);
+	application_class->activate = goo_application_activate;
+	application_class->command_line = goo_application_command_line;
+	application_class->local_command_line = goo_application_local_command_line;
+}
+
+
+static GtkApplication *
+goo_application_new (void)
+{
+	g_type_init ();
+
+	return g_object_new (goo_application_get_type (),
+	                     "application-id", "org.gnome.Goobox",
+	                     "flags", 0,
+	                     NULL);
 }
 
 
 int
-main (int argc, char **argv)
+main (int argc, char *argv[])
 {
-	GOptionContext *context = NULL;
-	GError         *error = NULL;
+	GtkApplication *application;
+	int             status;
 
 	program_argv0 = argv[0];
 
@@ -413,48 +507,25 @@ main (int argc, char **argv)
 		gdk_threads_init ();
 	}
 
+	/* text domain */
+
 	bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
 	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 	textdomain (GETTEXT_PACKAGE);
 
-        context = g_option_context_new (N_("Play CDs and save the tracks to disk as files"));
-        g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
-        g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
-	g_option_context_add_group (context, gtk_get_option_group (TRUE));
-	g_option_context_add_group (context, egg_sm_client_get_option_group ());
-	g_option_context_add_group (context, gst_init_get_option_group ());
-  	g_option_context_set_ignore_unknown_options (context, TRUE);
-	if (! g_option_context_parse (context, &argc, &argv, &error)) {
-		g_critical ("Failed to parse arguments: %s", error->message);
-		g_error_free (error);
-		g_option_context_free (context);
-		return EXIT_FAILURE;
-	}
-	g_option_context_free (context);
-
-	if (version) {
-		g_print ("%s %s, Copyright (C) 2004-2009 Free Software Foundation, Inc.\n", PACKAGE_NAME, PACKAGE_VERSION);
-		return 0;
-	}
-
-#ifdef ENABLE_NOTIFICATION
-	if (! notify_init ("goobox"))
-                g_warning ("Cannot initialize notification system.");
-#endif /* ENABLE_NOTIFICATION */
+	/* run the main application */
 
-	goo_session_manager_init ();
-	prepare_application ();
+	application = main_application = goo_application_new ();
+	gdk_threads_enter ();
+	status = g_application_run (G_APPLICATION (application), argc, argv);
+	gdk_threads_leave ();
+	g_object_unref (application);
 
-	if ((application == NULL) || ! unique_app_is_running (application)) {
-		gdk_threads_enter ();
-		gtk_main ();
-		gdk_threads_leave ();
-	}
+	return status;
+}
 
-	release_data ();
 
-	return 0;
-}
+/* -- utility functions -- */
 
 
 GtkWidget *
@@ -465,7 +536,7 @@ main_get_window_from_device (const char *device)
 	if (device == NULL)
 		return NULL;
 
-	for (scan = window_list; scan; scan = scan->next) {
+	for (scan = gtk_application_get_windows (main_application); scan; scan = scan->next) {
 		GooWindow *window = scan->data;
 
 		if (g_strcmp0 (goo_player_get_device (goo_window_get_player (window)), device) == 0)
diff --git a/src/main.h b/src/main.h
index 153eeef..9767f5a 100644
--- a/src/main.h
+++ b/src/main.h
@@ -26,10 +26,9 @@
 #include <brasero3/brasero-drive.h>
 #include "goo-window.h"
 
-extern GtkWidget *main_window;
-extern GList     *window_list;
-extern int        AutoPlay;
-extern int        HideShow;
+extern GtkApplication *main_application;
+extern int             arg_auto_play;
+extern int             arg_toggle_visibility;
 
 GtkWidget *     main_get_window_from_device (const char *device);
 BraseroDrive *  main_get_most_likely_drive  (void);



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