[nautilus-actions] Fix #353198 – Single window mode for Nact



commit d52d10e57d7fdfdebbccc2f13ff9f18bbc15167f
Author: Pierre Wieser <pwieser trychlos org>
Date:   Fri Jun 12 11:36:47 2009 +0200

    Fix #353198 â?? Single window mode for Nact

 ChangeLog                   |   18 ++++++
 configure.ac                |   36 +-----------
 src/nact/Makefile.am        |    3 +-
 src/nact/nact-application.c |  133 ++++++++++++++++++++++++++++++++++---------
 src/nact/nact-application.h |    2 +-
 src/nact/nact-main.c        |    6 ++
 src/nact/nact.c             |    4 +-
 src/nact/nact.h             |    8 ++-
 8 files changed, 145 insertions(+), 65 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index b0cc3c9..f12cb52 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,21 @@
+2009-06-12 Pierre Wieser <pwieser trychlos org>
+
+	Fix #353198 â?? Single window mode for Nact.
+
+	* configure.ac:
+	Add libunique dependancy.
+	Cleanup the code.
+
+	* src/nact/nact.c:
+	* src/nact/nact.h(nact_init_dialog):
+	Return the newly created GtkWindow.
+
+	* src/nact/nact-main.c (main):
+	Call gtk_init at the very beginning of the program.
+
+	* src/nact/nact-application.c:
+	Check for already running instance, and display a message if any.
+
 2009-06-11 Pierre Wieser <pwieser trychlos org>
 
 	* src/common/nact-pivot.c:
diff --git a/configure.ac b/configure.ac
index d4cbe77..f324b94 100644
--- a/configure.ac
+++ b/configure.ac
@@ -117,10 +117,6 @@ GCONF_REQUIRED=2.8.0
 LIBXML_REQUIRED=2.6.0
 GOBJECT_REQUIRED=2.4.0
 
-#AC_SUBST(GLIB_REQUIRED)
-#AC_SUBST(GTK_REQUIRED)
-#AC_SUBST(GLADE_REQUIRED)
-
 AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
 if test "${PKG_CONFIG}" = "no"; then
 	AC_MSG_ERROR([You need to install pkg-config])
@@ -136,7 +132,9 @@ PKG_CHECK_MODULES([NAUTILUS_ACTIONS], \
 	libgnomeui-2.0			>= ${GNOME_REQUIRED}		\
 	gconf-2.0				>= ${GCONF_REQUIRED}		\
 	libnautilus-extension	>= ${NAUTILUS_EXTENSION}	\
-	uuid)
+	uuid												\
+	unique-1.0
+)
 AC_SUBST([NAUTILUS_ACTIONS_CFLAGS])
 AC_SUBST([NAUTILUS_ACTIONS_LIBS])
 
@@ -154,34 +152,6 @@ NACT_IS_MAINTAINER_MODE
 AC_CHECK_LIB(nautilus-extension, nautilus_menu_item_new)
 AC_CHECK_FUNCS(nautilus_menu_provider_emit_items_updated_signal)
 
-# pwi 2009-05-15 disabled as useless or badly explained
-#if test "x${exec_prefix}" = "xNONE"; then
-#	if test "x${prefix}" = "xNONE"; then
-#		if test "x${bindir}" = "xNONE"; then
-#			BINDIR=`eval echo $ac_default_prefix/bin`
-#		else
-#			BINDIR=`eval echo $bindir`
-#		fi
-#	else
-#		BINDIR=`eval echo $prefix/bin`
-#	fi
-#else
-#	BINDIR=`eval echo $bindir`
-#fi
-#AC_SUBST(BINDIR)
-#	
-#if test "x${prefix}" = "xNONE"; then
-#  DATADIR="${ac_default_prefix}/${DATADIRNAME}" 
-#  LOCALEDIR="${DATADIR}/locale"
-#  AC_DEFINE_UNQUOTED(LOCALEDIR, "${LOCALEDIR}", [Locale dir])
-#else
-#  DATADIR="${prefix}/${DATADIRNAME}"
-#  LOCALEDIR="${DATADIR}/locale"
-#  AC_DEFINE_UNQUOTED(LOCALEDIR, "${LOCALEDIR}", [Locale dir])
-#fi
-#AC_SUBST(DATADIR)
-#AC_SUBST(LOCALEDIR)
-
 AC_DEFINE_UNQUOTED(NAUTILUS_ACTIONS_CONFIG_VERSION, "2.0", [Version of the configuration format])
 AC_DEFINE_UNQUOTED(NAUTILUS_ACTIONS_CONFIG_GCONF_BASEDIR, "/apps/nautilus-actions", [GConf base dir where all config will be stored])
 
diff --git a/src/nact/Makefile.am b/src/nact/Makefile.am
index 49e2b43..615a929 100644
--- a/src/nact/Makefile.am
+++ b/src/nact/Makefile.am
@@ -27,7 +27,8 @@
 #   ... and many others (see AUTHORS)
 
 bin_PROGRAMS = \
-	nautilus-actions-config
+	nautilus-actions-config								\
+	$(NULL)
 
 gladedir = $(datadir)/nautilus-actions
 
diff --git a/src/nact/nact-application.c b/src/nact/nact-application.c
index a4aee3f..495bbe1 100644
--- a/src/nact/nact-application.c
+++ b/src/nact/nact-application.c
@@ -34,6 +34,7 @@
 
 #include <glib/gi18n.h>
 #include <gtk/gtk.h>
+#include <unique/unique.h>
 
 #include "nact.h"
 #include "nact-application.h"
@@ -46,9 +47,11 @@ struct NactApplicationClassPrivate {
 /* private instance data
  */
 struct NactApplicationPrivate {
-	gboolean dispose_has_run;
-	int      argc;
-	gpointer argv;
+	gboolean   dispose_has_run;
+	int        argc;
+	gpointer   argv;
+	UniqueApp *unique;
+	GtkWindow *main;
 };
 
 /* private instance properties
@@ -63,18 +66,21 @@ enum {
 
 static GObjectClass *st_parent_class = NULL;
 
-static GType    register_type( void );
-static void     class_init( NactApplicationClass *klass );
-static void     instance_init( GTypeInstance *instance, gpointer klass );
-static void     instance_get_property( GObject *object, guint property_id, GValue *value, GParamSpec *spec );
-static void     instance_set_property( GObject *object, guint property_id, const GValue *value, GParamSpec *spec );
-static void     instance_dispose( GObject *application );
-static void     instance_finalize( GObject *application );
-
-static void     initialize_i18n( NactApplication *application );
-static gboolean startup_appli( NactApplication *application );
-static int      run_appli( NactApplication *application );
-static void     finish_appli( NactApplication *application );
+static GType          register_type( void );
+static void           class_init( NactApplicationClass *klass );
+static void           instance_init( GTypeInstance *instance, gpointer klass );
+static void           instance_get_property( GObject *object, guint property_id, GValue *value, GParamSpec *spec );
+static void           instance_set_property( GObject *object, guint property_id, const GValue *value, GParamSpec *spec );
+static void           instance_dispose( GObject *application );
+static void           instance_finalize( GObject *application );
+
+/*static UniqueResponse on_unique_message_received( UniqueApp *app, UniqueCommand command, UniqueMessageData *message, guint time, gpointer user_data );*/
+static void           warn_other_instance( NactApplication *application );
+static gboolean       check_for_unique_app( NactApplication *application );
+static void           initialize_i18n( NactApplication *application );
+static gboolean       startup_appli( NactApplication *application );
+static int            run_appli( NactApplication *application );
+static void           finish_appli( NactApplication *application );
 
 GType
 nact_application_get_type( void )
@@ -94,8 +100,6 @@ register_type( void )
 	static const gchar *thisfn = "nact_application_register_type";
 	g_debug( "%s", thisfn );
 
-	g_type_init();
-
 	static GTypeInfo info = {
 		sizeof( NactApplicationClass ),
 		( GBaseInitFunc ) NULL,
@@ -122,8 +126,8 @@ class_init( NactApplicationClass *klass )
 	GObjectClass *object_class = G_OBJECT_CLASS( klass );
 	object_class->dispose = instance_dispose;
 	object_class->finalize = instance_finalize;
-	object_class->set_property = instance_set_property;
 	object_class->get_property = instance_get_property;
+	object_class->set_property = instance_set_property;
 
 	GParamSpec *spec;
 	spec = g_param_spec_int(
@@ -155,6 +159,8 @@ instance_init( GTypeInstance *instance, gpointer klass )
 	self->private = g_new0( NactApplicationPrivate, 1 );
 
 	self->private->dispose_has_run = FALSE;
+
+	self->private->unique = unique_app_new( "org.nautilus-actions.Config", NULL );
 }
 
 static void
@@ -212,6 +218,8 @@ instance_dispose( GObject *application )
 
 		self->private->dispose_has_run = TRUE;
 
+		g_object_unref( self->private->unique );
+
 		/* chain up to the parent class */
 		G_OBJECT_CLASS( st_parent_class )->dispose( application );
 	}
@@ -249,11 +257,83 @@ nact_application_new( void )
  * @argv: command-line arguments.
  */
 NactApplication *
-nact_application_new_with_args( int argc, char *argv[] )
+nact_application_new_with_args( int argc, char **argv )
 {
 	return( g_object_new( NACT_APPLICATION_TYPE, PROP_ARGC_STR, argc, PROP_ARGV_STR, argv, NULL ));
 }
 
+/*static UniqueResponse
+on_unique_message_received(
+		UniqueApp *app, UniqueCommand command, UniqueMessageData *message, guint time, gpointer user_data )
+{
+	static const gchar *thisfn = "nact_application_check_for_unique_app";
+	UniqueResponse resp = UNIQUE_RESPONSE_OK;
+
+	switch( command ){
+		case UNIQUE_ACTIVATE:
+			g_debug( "%s: received message UNIQUE_ACTIVATE", thisfn );
+			break;
+		default:
+			resp = UNIQUE_RESPONSE_PASSTHROUGH;
+			break;
+	}
+
+	return( resp );
+}*/
+
+static void
+warn_other_instance( NactApplication *application )
+{
+	g_assert( NACT_IS_APPLICATION( application ));
+
+	GtkWidget *dialog = gtk_message_dialog_new_with_markup(
+			NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_OK,
+			_( "<b>Another instance of Nautilus Actions Configurator is already running.</b>\n\nPlease switch back to it." ));
+
+	g_object_set( G_OBJECT( dialog ) , "title", _( "Nautilus Actions" ), NULL );
+
+	gtk_dialog_run( GTK_DIALOG( dialog ));
+	gtk_widget_destroy( dialog );
+}
+
+/*
+ * returns TRUE if we are the first instance
+ */
+static gboolean
+check_for_unique_app( NactApplication *application )
+{
+	gboolean is_first = TRUE;
+
+	g_assert( NACT_IS_APPLICATION( application ));
+
+	if( unique_app_is_running( application->private->unique )){
+
+		is_first = FALSE;
+
+		unique_app_send_message( application->private->unique, UNIQUE_ACTIVATE, NULL );
+
+		/* the screen is not actually modified, nor the main window is
+		 * switched back to the current screen ; the icon in the deskbar
+		 * applet is just highlighted
+		 * so a message is not too much !
+		 */
+		warn_other_instance( application );
+
+	/* default from libunique is actually to activate the first window
+	 * so we rely on the default..
+	 */
+	/*} else {
+		g_signal_connect(
+				application->private->unique,
+				"message-received",
+				G_CALLBACK( on_unique_message_received ),
+				application
+		);*/
+	}
+
+	return( is_first );
+}
+
 static void
 initialize_i18n( NactApplication *application )
 {
@@ -269,21 +349,15 @@ initialize_i18n( NactApplication *application )
 static gboolean
 startup_appli( NactApplication *application )
 {
-	int argc;
-	char **argv;
-	int ret = TRUE;
+	int ret;
 
 	initialize_i18n( application );
 
-	argc = application->private->argc;
-	argv = ( char ** ) application->private->argv;
-	gtk_init( &argc, &argv );
-
 	g_set_application_name( PACKAGE );
+
 	gtk_window_set_default_icon_name( PACKAGE );
 
-	/* create main dialog */
-	nact_init_dialog ();
+	ret = check_for_unique_app( application );
 
 	return( ret );
 }
@@ -293,6 +367,9 @@ run_appli( NactApplication *application )
 {
 	int code = 0;
 
+	application->private->main = nact_init_dialog();
+	unique_app_watch_window( application->private->unique, application->private->main );
+
 	gtk_main();
 
 	return( code );
diff --git a/src/nact/nact-application.h b/src/nact/nact-application.h
index 7a8ad00..be9ee78 100644
--- a/src/nact/nact-application.h
+++ b/src/nact/nact-application.h
@@ -67,7 +67,7 @@ typedef struct {
 GType            nact_application_get_type( void );
 
 NactApplication *nact_application_new( void );
-NactApplication *nact_application_new_with_args( int argc, char *argv[] );
+NactApplication *nact_application_new_with_args( int argc, char **argv );
 
 int              nact_application_run( NactApplication *application );
 
diff --git a/src/nact/nact-main.c b/src/nact/nact-main.c
index 3d0d63c..d1ae147 100644
--- a/src/nact/nact-main.c
+++ b/src/nact/nact-main.c
@@ -32,11 +32,17 @@
 #include <config.h>
 #endif
 
+#include <gtk/gtk.h>
+
 #include "nact-application.h"
 
 int
 main( int argc, char *argv[] )
 {
+	/* need to gtk_init before unique_app_new (see Bug #585536)
+	 */
+	gtk_init( &argc, &argv );
+
 	NactApplication *app = nact_application_new_with_args( argc, argv );
 
 	int ret = nact_application_run( app );
diff --git a/src/nact/nact.c b/src/nact/nact.c
index 8f4ea4e..672790f 100644
--- a/src/nact/nact.c
+++ b/src/nact/nact.c
@@ -357,7 +357,7 @@ setup_actions_list (GtkWidget *list)
 
 }
 
-/*static */void
+GtkWindow *
 nact_init_dialog (void)
 {
 	gint width, height, x, y;
@@ -412,4 +412,6 @@ nact_init_dialog (void)
 	/* display the dialog */
 	gtk_widget_show (nact_dialog);
 	g_object_unref (gui);
+
+	return( GTK_WINDOW( nact_dialog ));
 }
diff --git a/src/nact/nact.h b/src/nact/nact.h
index f5e11a2..21af99f 100644
--- a/src/nact/nact.h
+++ b/src/nact/nact.h
@@ -31,6 +31,10 @@
 #ifndef __NACT_H__
 #define __NACT_H__
 
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
 enum {
 	MENU_ICON_COLUMN = 0,
 	MENU_LABEL_COLUMN,
@@ -38,6 +42,8 @@ enum {
 	N_COLUMN
 };
 
-void nact_init_dialog( void );
+GtkWindow *nact_init_dialog( void );
+
+G_END_DECLS
 
 #endif /* __NACT_H__ */



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