[nautilus-actions] Rewrite BaseApplication base class
- From: Pierre Wieser <pwieser src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [nautilus-actions] Rewrite BaseApplication base class
- Date: Tue, 18 Aug 2009 19:06:25 +0000 (UTC)
commit f6bc305d078d11ac7d37629c80ae0720d4d157ec
Author: Pierre Wieser <pwieser trychlos org>
Date: Tue Aug 18 21:06:52 2009 +0200
Rewrite BaseApplication base class
Document all public and virtual functions.
Define custom exit codes when an error is detected at initialization time.
Define standard error messages.
ChangeLog | 17 ++
src/nact/base-application-class.h | 116 ++++++---
src/nact/base-application.c | 479 ++++++++++++++++++++++---------------
src/nact/base-application.h | 59 ++++-
src/nact/nact-application.c | 108 ++++++---
src/nact/nact-main-window.c | 2 +-
src/nact/nact-xml-reader.c | 3 +-
7 files changed, 504 insertions(+), 280 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index ae67beb..c5b571e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2009-08-18 Pierre Wieser <pwieser trychlos org>
+
+ * src/nact/base-application-class.h:
+ * src/nact/base-application.c:
+ * src/nact/base-application.h:
+ Rewrite BaseApplication base class, and document all public
+ and virtual functions.
+ Define exit codes when an error occurs.
+ Define some standard error messages.
+
+ * src/nact/nact-application.c:
+ * src/nact/nact-application.h
+ (appli_initialize_unique_app): Customize error message.
+ (appli_initialize_application): Complete application initialization.
+
+ * src/nact/nact-xml-reader.c: Add nact-main-window.h header.
+
2009-08-11 Pierre Wieser <pwieser trychlos org>
* src/nact/nact-icommand-tab.c:
diff --git a/src/nact/base-application-class.h b/src/nact/base-application-class.h
index 4d0da40..51e8c08 100644
--- a/src/nact/base-application-class.h
+++ b/src/nact/base-application-class.h
@@ -146,36 +146,12 @@ typedef struct {
* parse some command-line parameters.
*
* If failed, the base class implementation sets #exit_code to
- * APPLICATION_ERROR_GTK, and prepares a short #exit_message to be
+ * %APPLICATION_ERROR_GTK, and prepares a short #exit_message to be
* written to stdout.
*/
gboolean ( *application_initialize_gtk ) ( BaseApplication *appli );
/**
- * application_initialize_application:
- * @appli: this #BaseApplication instance.
- *
- * Initializes the derived-class application.
- *
- * When this function successfully returns, the #BaseApplication
- * instance must have a valid pointer to the #BaseWindow-derived
- * object which will be used as a main window for the application.
- *
- * Returns: %TRUE if at least all mandatory informations have been
- * collected, %FALSE else.
- *
- * The base class implementation asks the derived class to
- * allocates and provides the BaseWindow-derived object which will
- * be the main window of the application
- * (cf. application_get_main_window()). This step is mandatory.
- *
- * If failed, this function sets #exit_code to the value which is
- * pertinent depending of the missing information, and prepares a
- * short #exit_message to be displayed in a dialog box.
- */
- gboolean ( *application_initialize_application )( BaseApplication *appli );
-
- /**
* application_initialize_unique_app:
* @appli: this #BaseApplication instance.
*
@@ -187,7 +163,7 @@ typedef struct {
* instance is already running.
*
* If failed, this function sets #exit_code to
- * APPLICATION_ERROR_UNIQUE_APP, and prepares a short #exit_message
+ * %APPLICATION_ERROR_UNIQUE_APP, and prepares a short #exit_message
* to be displayed in a dialog box.
*
* The base class implementation asks the #BaseApplication-derived
@@ -212,7 +188,7 @@ typedef struct {
* Returns: %TRUE if the UI description has been successfully
* loaded, %FALSE else.
*
- * If failed, this function sets #exit_code to APPLICATION_ERROR_UI,
+ * If failed, this function sets #exit_code to %APPLICATION_ERROR_UI,
* and prepares a short #exit_message to be displayed in a dialog
* box.
*
@@ -223,17 +199,39 @@ typedef struct {
gboolean ( *application_initialize_ui ) ( BaseApplication *appli );
/**
+ * application_initialize_application:
+ * @appli: this #BaseApplication instance.
+ *
+ * Initializes the derived-class application.
+ *
+ * When this function successfully returns, the #BaseApplication
+ * instance must have a valid pointer to the #BaseWindow-derived
+ * object which will be used as a main window for the application.
+ *
+ * Returns: %TRUE if at least all mandatory informations have been
+ * collected, %FALSE else.
+ *
+ * The base class implementation asks the derived class to
+ * allocates and provides the BaseWindow-derived object which will
+ * be the main window of the application
+ * (cf. application_get_main_window()). This step is mandatory.
+ *
+ * If failed, this function sets #exit_code to the value which is
+ * pertinent depending of the missing information, and prepares a
+ * short #exit_message to be displayed in a dialog box.
+ */
+ gboolean ( *application_initialize_application )( BaseApplication *appli );
+
+ /**
* application_get_application_name:
* @appli: this #BaseApplication instance.
*
* Asks the derived class for the application name.
*
- * A copy of this name is stored to be provided to any future
- * request.
- *
* It is typically used as the primary title of the main window.
*
- * If not provided by the application, name defaults to empty.
+ * If not provided by the derived class, application name defaults
+ * to empty.
*
* Returns: the application name, to be g_free() by the caller.
*/
@@ -243,20 +241,62 @@ typedef struct {
* application_get_icon_name:
* @appli: this #BaseApplication instance.
*
- * Asks the derived class for the application name.
+ * Asks the derived class for the name of the default icon.
*
- * A copy of this name is stored to be provided to any future
- * request.
- *
- * It is typically used as the primary title of the main window.
+ * It is typically used as the icon of the main window.
*
- * If not provided by the application, name defaults to empty.
+ * No default is provided by the base class.
*
- * Returns: the application name, to be g_free() by the caller.
+ * Returns: the default icon name for the application, to be
+ * g_free() by the caller.
*/
gchar * ( *application_get_icon_name ) ( BaseApplication *appli );
+
+ /**
+ * application_get_unique_app_name:
+ * @appli: this #BaseApplication instance.
+ *
+ * Asks the derived class for the UniqueApp name of this application.
+ *
+ * A UniqueApp name is typically of the form
+ * "com.mydomain.MyApplication.MyName". It is registered in DBus
+ * system by each running instance of the application, and is then
+ * used to check if another instance of the application is already
+ * running.
+ *
+ * No default is provided by the base class, which means that the
+ * base class defaults to not check for another instance.
+ *
+ * Returns: the UniqueApp name of the application, to be g_free()
+ * by the caller.
+ */
gchar * ( *application_get_unique_app_name ) ( BaseApplication *appli );
+
+ /**
+ * application_get_ui_filename:
+ * @appli: this #BaseApplication instance.
+ *
+ * Asks the derived class for the filename of the XML definition of
+ * the user interface. This XML definition must be suitable in order
+ * to be loaded via GtkBuilder.
+ *
+ * No default is provided by the base class. If the base class does
+ * not provide one, then the program stops and exits with the code
+ * %APPLICATION_ERROR_UI_FNAME.
+ *
+ * Returns: the filename of the XML definition, to be g_free() by
+ * the caller.
+ */
gchar * ( *application_get_ui_filename ) ( BaseApplication *appli );
+
+ /**
+ * application_get_main_window:
+ * @appli: this #BaseApplication instance.
+ *
+ * Returns: a pointer to the #BaseWindow-derived main window of the
+ * application. This pointer is owned by the @appli, and should not
+ * be g_free() not g_object_unref() by the caller.
+ */
GObject * ( *application_get_main_window ) ( BaseApplication *appli );
}
BaseApplicationClass;
diff --git a/src/nact/base-application.c b/src/nact/base-application.c
index b5ede01..26cf7b1 100644
--- a/src/nact/base-application.c
+++ b/src/nact/base-application.c
@@ -33,6 +33,7 @@
#endif
#include <glib/gi18n.h>
+#include <glib/gprintf.h>
#include <string.h>
#include <unique/unique.h>
@@ -48,56 +49,14 @@ struct BaseApplicationClassPrivate {
*/
struct BaseApplicationPrivate {
gboolean dispose_has_run;
-
- /**
- * @argc: count of arguments in command-line.
- * @argv: list of command-line arguments
- *
- * These two variables must be provided before running the
- * initialization process ; they are required in order to correctly
- * initialize the Gtk+ user interface.
- */
int argc;
gpointer argv;
-
- /**
- * @is_gtk_initialized: set to %TRUE after successfully returning
- * from the application_initialize_gtk() virtual function.
- *
- * While this flag is not %TRUE, error messages are printed to
- * stdout. When %TRUE, error messages are displayed with a dialog
- * box.
- */
gboolean is_gtk_initialized;
-
- /**
- * @unique_app_handle: the UniqueApp object allocated if the
- * derived-class has provided a @unique_app_name. Rather for
- * internal use.
- */
UniqueApp *unique_app_handle;
-
- /**
- * @exit_code: the code which will be returned by the program to
- * the operating system.
- * @exit_message: the message which will be displayed at program
- * terminaison if @exit_code is not zero.
- *
- * See @is_gtk_initialized for how the @exit_message is actually
- * displayed.
- */
int exit_code;
- gchar *exit_message;
-
- /**
- * @ui_xml: the GtkBuilder object allocated to handle the UI
- * definition. Rather for internal use.
- */
+ gchar *exit_message1;
+ gchar *exit_message2;
GtkBuilder *ui_xml;
-
- /**
- * @main_window: as its name says...
- */
BaseWindow *main_window;
};
@@ -109,7 +68,8 @@ enum {
PROP_APPLICATION_IS_GTK_INITIALIZED_ID,
PROP_APPLICATION_UNIQUE_APP_HANDLE_ID,
PROP_APPLICATION_EXIT_CODE_ID,
- PROP_APPLICATION_EXIT_MESSAGE_ID,
+ PROP_APPLICATION_EXIT_MESSAGE1_ID,
+ PROP_APPLICATION_EXIT_MESSAGE2_ID,
PROP_APPLICATION_UI_XML_ID,
PROP_APPLICATION_MAIN_WINDOW_ID
};
@@ -127,19 +87,17 @@ static void instance_finalize( GObject *application );
static gboolean v_initialize( BaseApplication *application );
static gboolean v_initialize_i18n( BaseApplication *application );
static gboolean v_initialize_gtk( BaseApplication *application );
-static gboolean v_initialize_application( BaseApplication *application );
static gboolean v_initialize_unique_app( BaseApplication *application );
static gboolean v_initialize_ui( BaseApplication *application );
-static gchar *v_get_unique_app_name( BaseApplication *application );
-static gchar *v_get_ui_filename( BaseApplication *application );
+static gboolean v_initialize_application( BaseApplication *application );
static int do_application_run( BaseApplication *application );
-static void do_application_initialize( BaseApplication *application );
-static void do_application_initialize_i18n( BaseApplication *application );
-static void do_application_initialize_gtk( BaseApplication *application );
-static void do_application_initialize_application( BaseApplication *application );
-static void do_application_initialize_unique_app( BaseApplication *application );
-static void do_application_initialize_ui( BaseApplication *application );
+static gboolean do_application_initialize( BaseApplication *application );
+static gboolean do_application_initialize_i18n( BaseApplication *application );
+static gboolean do_application_initialize_gtk( BaseApplication *application );
+static gboolean do_application_initialize_unique_app( BaseApplication *application );
+static gboolean do_application_initialize_ui( BaseApplication *application );
+static gboolean do_application_initialize_application( BaseApplication *application );
static gboolean check_for_unique_app( BaseApplication *application );
/*static UniqueResponse on_unique_message_received( UniqueApp *app, UniqueCommand command, UniqueMessageData *message, guint time, gpointer user_data );*/
@@ -147,6 +105,15 @@ static gint display_dlg( BaseApplication *application, GtkMessageType
static GtkWidget *recursive_search_for_child( BaseApplication *application, GtkWindow *toplevel, const gchar *name );
static GtkWidget *search_for_child_widget( GtkContainer *container, const gchar *name );
+static void display_error_message( BaseApplication *application );
+static void set_get_dialog_error( BaseApplication *application, const gchar *dialog );
+static void set_initialize_i18n_error( BaseApplication *application );
+static void set_initialize_gtk_error( BaseApplication *application );
+static void set_initialize_application_error( BaseApplication *application );
+static void set_initialize_unique_app_error( BaseApplication *application );
+static void set_initialize_ui_get_fname_error( BaseApplication *application );
+static void set_initialize_ui_add_xml_error( BaseApplication *application, const gchar *filename, GError *error );
+
GType
base_application_get_type( void )
{
@@ -230,14 +197,21 @@ class_init( BaseApplicationClass *klass )
"Exit code",
"Exit code of the application", 0, 65535, 0,
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE );
- g_object_class_install_property( object_class, PROP_APPLICATION_CODE_ID, spec );
+ g_object_class_install_property( object_class, PROP_APPLICATION_EXIT_CODE_ID, spec );
spec = g_param_spec_string(
- PROP_APPLICATION_EXIT_MESSAGE,
+ PROP_APPLICATION_EXIT_MESSAGE1,
"Error message",
- "Error message displayed when exit_code not nul", "",
+ "First line of the error message displayed when exit_code not nul", "",
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE );
- g_object_class_install_property( object_class, PROP_APPLICATION_EXIT_MESSAGE_ID, spec );
+ g_object_class_install_property( object_class, PROP_APPLICATION_EXIT_MESSAGE1_ID, spec );
+
+ spec = g_param_spec_string(
+ PROP_APPLICATION_EXIT_MESSAGE2,
+ "Error message",
+ "Second line of the error message displayed when exit_code not nul", "",
+ G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE );
+ g_object_class_install_property( object_class, PROP_APPLICATION_EXIT_MESSAGE2_ID, spec );
spec = g_param_spec_pointer(
PROP_APPLICATION_UI_XML,
@@ -281,7 +255,7 @@ instance_init( GTypeInstance *instance, gpointer klass )
self->private = g_new0( BaseApplicationPrivate, 1 );
self->private->dispose_has_run = FALSE;
- self->private->code = 0;
+ self->private->exit_code = 0;
}
static void
@@ -311,8 +285,12 @@ instance_get_property( GObject *object, guint property_id, GValue *value, GParam
g_value_set_int( value, self->private->exit_code );
break;
- case PROP_APPLICATION_EXIT_MESSAGE_ID:
- g_value_set_string( value, self->private->exit_message );
+ case PROP_APPLICATION_EXIT_MESSAGE1_ID:
+ g_value_set_string( value, self->private->exit_message1 );
+ break;
+
+ case PROP_APPLICATION_EXIT_MESSAGE2_ID:
+ g_value_set_string( value, self->private->exit_message2 );
break;
case PROP_APPLICATION_UI_XML_ID:
@@ -345,7 +323,7 @@ instance_set_property( GObject *object, guint property_id, const GValue *value,
break;
case PROP_APPLICATION_IS_GTK_INITIALIZED_ID:
- self->private->argv = g_value_get_boolean( value );
+ self->private->is_gtk_initialized = g_value_get_boolean( value );
break;
case PROP_APPLICATION_UNIQUE_APP_HANDLE_ID:
@@ -356,9 +334,14 @@ instance_set_property( GObject *object, guint property_id, const GValue *value,
self->private->exit_code = g_value_get_int( value );
break;
- case PROP_APPLICATION_EXIT_MESSAGE_ID:
- g_free( self->private->exit_message );
- self->private->exit_message = g_value_dup_string( value );
+ case PROP_APPLICATION_EXIT_MESSAGE1_ID:
+ g_free( self->private->exit_message1 );
+ self->private->exit_message1 = g_value_dup_string( value );
+ break;
+
+ case PROP_APPLICATION_EXIT_MESSAGE2_ID:
+ g_free( self->private->exit_message2 );
+ self->private->exit_message2 = g_value_dup_string( value );
break;
case PROP_APPLICATION_UI_XML_ID:
@@ -392,7 +375,9 @@ instance_dispose( GObject *application )
g_object_unref( self->private->unique_app_handle );
}
- g_object_unref( self->private->ui_xml );
+ if( GTK_IS_BUILDER( self->private->ui_xml )){
+ g_object_unref( self->private->ui_xml );
+ }
/* chain up to the parent class */
G_OBJECT_CLASS( st_parent_class )->dispose( application );
@@ -408,7 +393,8 @@ instance_finalize( GObject *application )
g_assert( BASE_IS_APPLICATION( application ));
BaseApplication *self = ( BaseApplication * ) application;
- g_free( self->private->exit_message );
+ g_free( self->private->exit_message1 );
+ g_free( self->private->exit_message2 );
g_free( self->private );
@@ -417,6 +403,7 @@ instance_finalize( GObject *application )
G_OBJECT_CLASS( st_parent_class )->finalize( application );
}
}
+
/**
* base_application_run:
* @application: this #BaseApplication instance.
@@ -437,14 +424,10 @@ base_application_run( BaseApplication *application )
{
static const gchar *thisfn = "base_application_run";
g_debug( "%s: application=%p", thisfn, application );
- g_assert( BASE_IS_APPLICATION( application ));
- if( BASE_APPLICATION_GET_CLASS( application )){
- return( BASE_APPLICATION_GET_CLASS( application )->application_run( application ));
- }
+ g_assert( BASE_IS_APPLICATION( application ));
- g_assert_not_reached();
- return( 0 ); /* so that gcc is happy */
+ return( BASE_APPLICATION_GET_CLASS( application )->application_run( application ));
}
/**
@@ -509,16 +492,77 @@ base_application_get_icon_name( BaseApplication *application )
}
/**
+ * base_application_get_unique_app_name:
+ * @application: this #BaseApplication instance.
+ *
+ * Asks the #BaseApplication-derived class for its UniqueApp name if any.
+ *
+ * Defaults to empty.
+ *
+ * Returns: a newly allocated string to be g_free() by the caller.
+ */
+gchar *
+base_application_get_unique_app_name( BaseApplication *application )
+{
+ /*static const gchar *thisfn = "base_application_get_unique_app_name";
+ g_debug( "%s: icon=%p", thisfn, application );*/
+
+ g_assert( BASE_IS_APPLICATION( application ));
+
+ gchar *name = NULL;
+
+ if( BASE_APPLICATION_GET_CLASS( application )->application_get_unique_app_name( application )){
+ name = BASE_APPLICATION_GET_CLASS( application )->application_get_unique_app_name( application );
+
+ } else {
+ name = g_strdup( "" );
+ }
+
+ return( name );
+}
+
+/**
+ * base_application_get_ui_filename:
+ * @application: this #BaseApplication instance.
+ *
+ * Asks the #BaseApplication-derived class for the filename of the file
+ * which contains the XML definition of the user interface.
+ *
+ * Defaults to empty.
+ *
+ * Returns: a newly allocated string to be g_free() by the caller.
+ */
+gchar *
+base_application_get_ui_filename( BaseApplication *application )
+{
+ /*static const gchar *thisfn = "base_application_get_ui_filename";
+ g_debug( "%s: icon=%p", thisfn, application );*/
+
+ g_assert( BASE_IS_APPLICATION( application ));
+
+ gchar *name = NULL;
+
+ if( BASE_APPLICATION_GET_CLASS( application )->application_get_ui_filename( application )){
+ name = BASE_APPLICATION_GET_CLASS( application )->application_get_ui_filename( application );
+
+ } else {
+ name = g_strdup( "" );
+ }
+
+ return( name );
+}
+
+/**
* base_application_get_main_window:
* @application: this #BaseApplication instance.
*
* Returns: a pointer to the #BaseWindow-derived object which serves as
* the main window of the application.
*
- * The returned pointer should not be g_free() nor g_object_unref() by
- * the caller.
+ * The returned pointer is owned by @application, and thus should not be
+ * g_free() nor g_object_unref() by the caller.
*
- * When first calling, #BaseApplication asks for its derived class to
+ * When first called, #BaseApplication asks for its derived class to
* allocate a new object. This same object is then returned on
* subsequent calls.
*/
@@ -559,11 +603,7 @@ base_application_get_dialog( BaseApplication *application, const gchar *name )
GtkWindow *dialog = GTK_WINDOW( gtk_builder_get_object( application->private->ui_xml, name ));
if( !dialog ){
- gchar *msg = g_strdup_printf(
- _( "Unable to load %s dialog from %s glade file." ), name, application->private->ui_fname );
- base_application_error_dlg( application, GTK_MESSAGE_ERROR, msg, NULL );
- g_free( msg );
- g_object_set( G_OBJECT( application ), PROP_APPLICATION_CODE_STR, 1, NULL );
+ set_get_dialog_error( application, name );
} else {
g_assert( GTK_IS_WINDOW( dialog ));
@@ -634,9 +674,9 @@ base_application_search_for_widget( BaseApplication *application, GtkWindow *win
*/
void
base_application_error_dlg(
- BaseApplication *application, GtkMessageType type, const gchar *primary, const gchar *secondary )
+ BaseApplication *application, GtkMessageType type, const gchar *first, const gchar *second )
{
- display_dlg( application, type, GTK_BUTTONS_OK, primary, secondary );
+ display_dlg( application, type, GTK_BUTTONS_OK, first, second );
}
/**
@@ -664,12 +704,9 @@ v_initialize( BaseApplication *application )
static const gchar *thisfn = "base_application_v_initialize";
g_debug( "%s: application=%p", thisfn, application );
- if( BASE_APPLICATION_GET_CLASS( application )->application_initialize ){
- return( BASE_APPLICATION_GET_CLASS( application )->application_initialize( application ));
- }
+ g_assert( BASE_IS_APPLICATION( application ));
- g_assert_not_reached();
- return( FALSE ); /* so that gcc is happy */
+ return( BASE_APPLICATION_GET_CLASS( application )->application_initialize( application ));
}
static gboolean
@@ -678,12 +715,15 @@ v_initialize_i18n( BaseApplication *application )
static const gchar *thisfn = "base_application_v_initialize_i18n";
g_debug( "%s: application=%p", thisfn, application );
- if( BASE_APPLICATION_GET_CLASS( application )->application_initialize_i18n ){
- return( BASE_APPLICATION_GET_CLASS( application )->application_initialize_i18n( application ));
+ g_assert( BASE_IS_APPLICATION( application ));
+
+ gboolean ok = BASE_APPLICATION_GET_CLASS( application )->application_initialize_i18n( application );
+
+ if( !ok ){
+ set_initialize_i18n_error( application );
}
- g_assert_not_reached();
- return( FALSE ); /* so that gcc is happy */
+ return( ok );
}
static gboolean
@@ -692,30 +732,18 @@ v_initialize_gtk( BaseApplication *application )
static const gchar *thisfn = "base_application_v_initialize_gtk";
g_debug( "%s: application=%p", thisfn, application );
- if( BASE_APPLICATION_GET_CLASS( application )->application_initialize_gtk ){
- gboolean ret = BASE_APPLICATION_GET_CLASS( application )->application_initialize_gtk( application );
- if( ret ){
- application->private->is_gtk_initialized = TRUE;
- }
- return( ret );
- }
+ g_assert( BASE_IS_APPLICATION( application ));
- g_assert_not_reached();
- return( FALSE ); /* so that gcc is happy */
-}
+ gboolean ok = BASE_APPLICATION_GET_CLASS( application )->application_initialize_gtk( application );
-static gboolean
-v_initialize_application( BaseApplication *application )
-{
- static const gchar *thisfn = "base_application_v_initialize_application_name";
- g_debug( "%s: application=%p", thisfn, application );
+ if( ok ){
+ application->private->is_gtk_initialized = TRUE;
- if( BASE_APPLICATION_GET_CLASS( application )->application_initialize_application ){
- return( BASE_APPLICATION_GET_CLASS( application )->application_initialize_application( application ));
+ } else {
+ set_initialize_gtk_error( application );
}
- g_assert_not_reached();
- return( FALSE ); /* so that gcc is happy */
+ return( ok );
}
static gboolean
@@ -724,12 +752,15 @@ v_initialize_unique_app( BaseApplication *application )
static const gchar *thisfn = "base_application_v_initialize_unique_app";
g_debug( "%s: application=%p", thisfn, application );
- if( BASE_APPLICATION_GET_CLASS( application )->application_initialize_unique_app ){
- return( BASE_APPLICATION_GET_CLASS( application )->application_initialize_unique_app( application ));
+ g_assert( BASE_IS_APPLICATION( application ));
+
+ gboolean ok = BASE_APPLICATION_GET_CLASS( application )->application_initialize_unique_app( application );
+
+ if( !ok ){
+ set_initialize_unique_app_error( application );
}
- g_assert_not_reached();
- return( FALSE ); /* so that gcc is happy */
+ return( ok );
}
static gboolean
@@ -738,52 +769,26 @@ v_initialize_ui( BaseApplication *application )
static const gchar *thisfn = "base_application_v_initialize_ui";
g_debug( "%s: application=%p", thisfn, application );
- if( BASE_APPLICATION_GET_CLASS( application )->application_initialize_ui ){
- return( BASE_APPLICATION_GET_CLASS( application )->application_initialize_ui( application ));
- }
-
- g_assert_not_reached();
- return( FALSE ); /* so that gcc is happy */
-}
-
-static gchar *
-v_get_unique_app_name( BaseApplication *application )
-{
- static const gchar *thisfn = "base_application_v_get_unique_name";
- g_debug( "%s: application=%p", thisfn, application );
-
g_assert( BASE_IS_APPLICATION( application ));
- gchar *name = NULL;
-
- if( BASE_APPLICATION_GET_CLASS( application )->application_get_unique_app_name( application )){
- name = BASE_APPLICATION_GET_CLASS( application )->application_get_unique_app_name( application );
-
- } else {
- name = g_strdup( "" );
- }
-
- return( name );
+ return( BASE_APPLICATION_GET_CLASS( application )->application_initialize_ui( application ));
}
-static gchar *
-v_get_ui_filename( BaseApplication *application )
+static gboolean
+v_initialize_application( BaseApplication *application )
{
- static const gchar *thisfn = "base_application_v_get_ui_filename";
+ static const gchar *thisfn = "base_application_v_initialize_application_name";
g_debug( "%s: application=%p", thisfn, application );
g_assert( BASE_IS_APPLICATION( application ));
- gchar *name = NULL;
-
- if( BASE_APPLICATION_GET_CLASS( application )->application_get_ui_filename( application )){
- name = BASE_APPLICATION_GET_CLASS( application )->application_get_ui_filename( application );
+ gboolean ok = BASE_APPLICATION_GET_CLASS( application )->application_initialize_application( application );
- } else {
- name = g_strdup( "" );
+ if( !ok ){
+ set_initialize_application_error( application );
}
- return( name );
+ return( ok );
}
static int
@@ -797,19 +802,21 @@ do_application_run( BaseApplication *application )
g_assert( application->private->main_window );
g_assert( BASE_IS_WINDOW( application->private->main_window ));
- base_window_init( window );
+ base_window_init( application->private->main_window );
- GtkWindow *wnd = base_window_get_toplevel_dialog( window );
+ GtkWindow *wnd = base_window_get_toplevel_dialog( application->private->main_window );
g_assert( wnd );
g_assert( GTK_IS_WINDOW( wnd ));
- if( application->private->unique_app ){
+ if( application->private->unique_app_handle ){
unique_app_watch_window( application->private->unique_app_handle, wnd );
}
- base_window_run( window );
+ base_window_run( application->private->main_window );
}
+ display_error_message( application );
+
return( application->private->exit_code );
}
@@ -819,9 +826,9 @@ do_application_initialize( BaseApplication *application )
return(
v_initialize_i18n( application ) &&
v_initialize_gtk( application ) &&
- v_initialize_application( application ) &&
v_initialize_unique_app( application ) &&
- v_initialize_ui( application )
+ v_initialize_ui( application ) &&
+ v_initialize_application( application )
);
}
@@ -851,28 +858,6 @@ do_application_initialize_gtk( BaseApplication *application )
if( ret ){
g_object_set( G_OBJECT( application ), PROP_APPLICATION_ARGC, argc, PROP_APPLICATION_ARGV, argv, NULL );
-
- } else {
- application->private->exit_code = APPLICATION_ERROR_GTK;
- application->private->exit_message =
- g_strdup( _( "Unable to initialize the Gtk+ user interface." ));
- }
-
- return( ret );
-}
-
-static gboolean
-do_application_initialize_application( BaseApplication *application )
-{
- BaseWindow *window;
- gboolean ret = TRUE;
-
- window = base_application_get_main_window( application );
- if( !window ){
- ret = FALSE;
- application->private->exit_code = APPLICATION_ERROR_MAIN_WINDOW;
- application->private->exit_message =
- g_strdup( _( "Unable to get the main window of the application." ));
}
return( ret );
@@ -883,18 +868,14 @@ do_application_initialize_unique_app( BaseApplication *application )
{
gboolean ret = TRUE;
- gchar *name = v_get_unique_app_name( application );
+ gchar *name = base_application_get_unique_app_name( application );
+
if( name && strlen( name )){
application->private->unique_app_handle = unique_app_new( name, NULL );
- if( !check_for_unique_app( application )){
- ret = FALSE;
- application->private->exit_code = APPLICATION_ERROR_UNIQUE_APP;
- application->private->exit_message =
- g_strdup( _( "Another instance of the application is already running." ));
- }
+ ret = check_for_unique_app( application );
}
- g_free( name );
+ g_free( name );
return( ret );
}
@@ -904,42 +885,48 @@ do_application_initialize_ui( BaseApplication *application )
gboolean ret = TRUE;
GError *error = NULL;
- gchar *name = v_get_ui_filename( application );
+ gchar *name = base_application_get_ui_filename( application );
+
if( !name || !strlen( name )){
ret = FALSE;
- application->private->exit_code = APPLICATION_ERROR_UI_FNAME;
- application->private->exit_message =
- g_strdup( _( "No filename provided for the UI XML definition." ));
+ set_initialize_ui_get_fname_error( application );
} else {
application->private->ui_xml = gtk_builder_new();
- gtk_builder_add_from_file( application->private->ui_xml, name, &error );
- if( error ){
+ guint retint = gtk_builder_add_from_file( application->private->ui_xml, name, &error );
+ if( error || retint == 0 ){
ret = FALSE;
- application->private->exit_code = APPLICATION_ERROR_UI_LOAD;
- application->private->exit_message =
- /* i18n: Unable to load the XML definition from <filename> */
- g_strdup( _( "Unable to load the XML definition from %s." ), name );
+ set_initialize_ui_add_xml_error( application, name, error );
g_error_free( error );
}
+ }
g_free( name );
-
return( ret );
}
static gboolean
+do_application_initialize_application( BaseApplication *application )
+{
+ BaseWindow *window;
+
+ window = base_application_get_main_window( application );
+
+ return( window != NULL );
+}
+
+static gboolean
check_for_unique_app( BaseApplication *application )
{
gboolean is_first = TRUE;
g_assert( BASE_IS_APPLICATION( application ));
- if( unique_app_is_running( application->private->unique_app )){
+ if( unique_app_is_running( application->private->unique_app_handle )){
is_first = FALSE;
- unique_app_send_message( application->private->unique_app, UNIQUE_ACTIVATE, NULL );
+ unique_app_send_message( application->private->unique_app_handle, UNIQUE_ACTIVATE, NULL );
/* default from libunique is actually to activate the first window
* so we rely on the default..
@@ -1035,3 +1022,99 @@ search_for_child_widget( GtkContainer *container, const gchar *name )
g_list_free( children );
return( found );
}
+
+static void
+display_error_message( BaseApplication *application )
+{
+ if( application->private->exit_message1 && g_utf8_strlen( application->private->exit_message1, -1 )){
+
+ if( application->private->is_gtk_initialized ){
+ base_application_error_dlg(
+ application,
+ GTK_MESSAGE_INFO,
+ application->private->exit_message1,
+ application->private->exit_message2 );
+
+ } else {
+ g_printf( "%s\n", application->private->exit_message1 );
+ if( application->private->exit_message2 && g_utf8_strlen( application->private->exit_message2, -1 )){
+ g_printf( "%s\n", application->private->exit_message2 );
+ }
+ }
+ }
+}
+
+static void
+set_get_dialog_error( BaseApplication *application, const gchar *dialog )
+{
+ application->private->exit_code = APPLICATION_ERROR_DIALOG_LOAD;
+
+ gchar *fname = base_application_get_ui_filename( application );
+ gchar *msg = g_strdup_printf(
+ /* i18n: unable to load <dialog_name> dialog from XML definition in <filename> */
+ _( "Unable to load %s dialog from XML definition in %s." ), dialog, fname );
+
+ base_application_error_dlg( application, GTK_MESSAGE_ERROR, msg, NULL );
+
+ g_free( msg );
+ g_free( fname );
+}
+
+static void
+set_initialize_i18n_error( BaseApplication *application )
+{
+ application->private->exit_code = APPLICATION_ERROR_I18N;
+
+ application->private->exit_message1 =
+ g_strdup( _( "Unable to initialize the internationalization environment." ));
+}
+
+static void
+set_initialize_gtk_error( BaseApplication *application )
+{
+ application->private->exit_code = APPLICATION_ERROR_GTK;
+
+ application->private->exit_message1 =
+ g_strdup( _( "Unable to initialize the Gtk+ user interface." ));
+}
+
+static void
+set_initialize_application_error( BaseApplication *application )
+{
+ application->private->exit_code = APPLICATION_ERROR_MAIN_WINDOW;
+
+ application->private->exit_message1 =
+ g_strdup( _( "Unable to get the main window of the application." ));
+}
+
+static void
+set_initialize_unique_app_error( BaseApplication *application )
+{
+ application->private->exit_code = APPLICATION_ERROR_UNIQUE_APP;
+
+ application->private->exit_message1 =
+ g_strdup( _( "Another instance of the application is already running." ));
+}
+
+static void
+set_initialize_ui_get_fname_error( BaseApplication *application )
+{
+ application->private->exit_code = APPLICATION_ERROR_UI_FNAME;
+
+ application->private->exit_message1 =
+ g_strdup( _( "No filename provided for the UI XML definition." ));
+}
+
+static void
+set_initialize_ui_add_xml_error( BaseApplication *application, const gchar *filename, GError *error )
+{
+ application->private->exit_code = APPLICATION_ERROR_UI_LOAD;
+
+ application->private->exit_message1 =
+ /* i18n: Unable to load the XML definition from <filename> */
+ g_strdup_printf( _( "Unable to load the XML definition from %s." ), filename );
+
+ if( error->message ){
+ application->private->exit_message2 = g_strdup( error->message );
+ }
+}
diff --git a/src/nact/base-application.h b/src/nact/base-application.h
index fb5e4a4..fce8ff3 100644
--- a/src/nact/base-application.h
+++ b/src/nact/base-application.h
@@ -45,35 +45,82 @@
G_BEGIN_DECLS
enum {
- APPLICATION_ERROR_GTK = 1, /* gtk+ initialization error */
+ APPLICATION_ERROR_I18N = 1, /* i18n initialization error */
+ APPLICATION_ERROR_GTK, /* gtk+ initialization error */
APPLICATION_ERROR_MAIN_WINDOW, /* unable to obtain the main window */
APPLICATION_ERROR_UNIQUE_APP, /* another instance is running */
APPLICATION_ERROR_UI_FNAME, /* empty XML filename */
- APPLICATION_ERROR_UI_LOAD /* unable to load the XML definition */
+ APPLICATION_ERROR_UI_LOAD, /* unable to load the XML definition of the UI */
+ APPLICATION_ERROR_DIALOG_LOAD /* unable to load a dialog from the XML definition */
};
-/* instance properties
+/**
+ * @PROP_APPLICATION_ARGC: count of arguments in command-line.
+ * @PROP_APPLICATION_ARGV: list of command-line arguments
+ *
+ * These two variables must be provided before running the
+ * initialization process ; they are required in order to correctly
+ * initialize the Gtk+ user interface.
*/
#define PROP_APPLICATION_ARGC "base-application-argc"
#define PROP_APPLICATION_ARGV "base-application-argv"
+
+/**
+ * @PROP_APPLICATION_IS_GTK_INITIALIZED: set to %TRUE after successfully
+ * returning from the application_initialize_gtk() virtual function.
+ *
+ * While this flag is not %TRUE, error messages are printed to
+ * stdout. When %TRUE, error messages are displayed with a dialog
+ * box.
+ */
#define PROP_APPLICATION_IS_GTK_INITIALIZED "base-application-is-gtk-initialized"
+
+/**
+ * @PROP_APPLICATION_UNIQUE_APP_HANDLE: the UniqueApp object allocated
+ * if the derived-class has provided a UniqueApp name (see
+ * #application_get_unique_app_name). Rather for internal use.
+ */
#define PROP_APPLICATION_UNIQUE_APP_HANDLE "base-application-unique-app-handle"
+
+/**
+ * @PROP_APPLICATION_EXIT_CODE: the code which will be returned by the
+ * program to the operating system.
+ * @PROP_APPLICATION_EXIT_MESSAGE1:
+ * @PROP_APPLICATION_EXIT_MESSAGE2: the message which will be displayed
+ * at program terminaison if @PROP_APPLICATION_EXIT_CODE is not zero.
+ * When in graphical mode, the first line is displayed as bold.
+ *
+ * See @PROP_APPLICATION_IS_GTK_INITIALIZED for how the
+ * @PROP_APPLICATION_EXIT_MESSAGE is actually displayed.
+ */
#define PROP_APPLICATION_EXIT_CODE "base-application-exit-code"
-#define PROP_APPLICATION_EXIT_MESSAGE "base-application-exit-message"
+#define PROP_APPLICATION_EXIT_MESSAGE1 "base-application-exit-message1"
+#define PROP_APPLICATION_EXIT_MESSAGE2 "base-application-exit-message2"
+
+/**
+ * @PROP_APPLICATION_UI_XML: the GtkBuilder object allocated to handle
+ * the user interface XML definition. Rather for internal use.
+ */
#define PROP_APPLICATION_UI_XML "base-application-ui-xml"
+
+/**
+ * @PROP_APPLICATION_MAIN_WINDOW: as its name says: a pointer to the
+ * #BaseWindow-derived main window of the application.
+ */
#define PROP_APPLICATION_MAIN_WINDOW "base-application-main-window"
int base_application_run( BaseApplication *application );
-
gchar *base_application_get_application_name( BaseApplication *application );
gchar *base_application_get_icon_name( BaseApplication *application );
+gchar *base_application_get_unique_app_name( BaseApplication *application );
+gchar *base_application_get_ui_filename( BaseApplication *application );
BaseWindow *base_application_get_main_window( BaseApplication *application );
GtkWindow *base_application_get_dialog( BaseApplication *application, const gchar *name );
GtkWidget *base_application_get_widget( BaseApplication *application, BaseWindow *window, const gchar *name );
GtkWidget *base_application_search_for_widget( BaseApplication *application, GtkWindow *window, const gchar *name );
-void base_application_error_dlg( BaseApplication *application, GtkMessageType type, const gchar *primary, const gchar *secondary );
+void base_application_error_dlg( BaseApplication *application, GtkMessageType type, const gchar *first, const gchar *second );
gboolean base_application_yesno_dlg( BaseApplication *application, GtkMessageType type, const gchar *first, const gchar *second );
G_END_DECLS
diff --git a/src/nact/nact-application.c b/src/nact/nact-application.c
index 060c7e1..dc6b33d 100644
--- a/src/nact/nact-application.c
+++ b/src/nact/nact-application.c
@@ -36,8 +36,9 @@
#include <gtk/gtk.h>
#include "nact-application.h"
+#include "nact-main-window.h"
-#define GLADE_FILENAME GLADEDIR "/nautilus-actions-config.ui"
+#define GLADE_FILENAME GLADEDIR "/nautilus-actions-config-tool.ui"
/* private class data
*/
@@ -59,7 +60,7 @@ enum {
#define PROP_PIVOT_STR "nact-application-pivot"
-static GObjectClass *st_parent_class = NULL;
+static BaseApplicationClass *st_parent_class = NULL;
static GType register_type( void );
static void class_init( NactApplicationClass *klass );
@@ -69,12 +70,13 @@ static void instance_set_property( GObject *object, guint property_id, const
static void instance_dispose( GObject *application );
static void instance_finalize( GObject *application );
-static void warn_other_instance( BaseApplication *application );
-static gchar *get_application_name( BaseApplication *application );
-static gchar *get_icon_name( BaseApplication *application );
-static gchar *get_unique_name( BaseApplication *application );
-static gchar *get_glade_file( BaseApplication *application );
-static GObject *get_main_window( BaseApplication *application );
+static gboolean appli_initialize_unique_app( BaseApplication *application );
+static gboolean appli_initialize_application( BaseApplication *application );
+static gchar *appli_get_application_name( BaseApplication *application );
+static gchar *appli_get_icon_name( BaseApplication *application );
+static gchar *appli_get_unique_app_name( BaseApplication *application );
+static gchar *appli_get_gtkbuilder_filename( BaseApplication *application );
+static GObject *appli_get_main_window( BaseApplication *application );
GType
nact_application_get_type( void )
@@ -115,7 +117,7 @@ class_init( NactApplicationClass *klass )
static const gchar *thisfn = "nact_application_class_init";
g_debug( "%s: klass=%p", thisfn, klass );
- st_parent_class = g_type_class_peek_parent( klass );
+ st_parent_class = BASE_APPLICATION_CLASS( g_type_class_peek_parent( klass ));
GObjectClass *object_class = G_OBJECT_CLASS( klass );
object_class->dispose = instance_dispose;
@@ -135,12 +137,13 @@ class_init( NactApplicationClass *klass )
BaseApplicationClass *appli_class = BASE_APPLICATION_CLASS( klass );
- appli_class->advertise_not_willing_to_run = warn_other_instance;
- appli_class->get_application_name = get_application_name;
- appli_class->get_icon_name = get_icon_name;
- appli_class->get_unique_name = get_unique_name;
- appli_class->get_ui_filename = get_glade_file;
- appli_class->get_main_window = get_main_window;
+ appli_class->application_initialize_unique_app = appli_initialize_unique_app;
+ appli_class->application_initialize_application = appli_initialize_application;
+ appli_class->application_get_application_name = appli_get_application_name;
+ appli_class->application_get_icon_name = appli_get_icon_name;
+ appli_class->application_get_unique_app_name = appli_get_unique_app_name;
+ appli_class->application_get_ui_filename = appli_get_gtkbuilder_filename;
+ appli_class->application_get_main_window = appli_get_main_window;
}
static void
@@ -155,8 +158,6 @@ instance_init( GTypeInstance *instance, gpointer klass )
self->private = g_new0( NactApplicationPrivate, 1 );
self->private->dispose_has_run = FALSE;
-
- self->private->pivot = na_pivot_new( NULL );
}
static void
@@ -206,7 +207,9 @@ instance_dispose( GObject *application )
self->private->dispose_has_run = TRUE;
- g_object_unref( self->private->pivot );
+ if( self->private->pivot ){
+ g_object_unref( self->private->pivot );
+ }
/* chain up to the parent class */
G_OBJECT_CLASS( st_parent_class )->dispose( application );
@@ -225,7 +228,7 @@ instance_finalize( GObject *application )
g_free( self->private );
/* chain call to parent class */
- if( st_parent_class->finalize ){
+ if( G_OBJECT_CLASS( st_parent_class )->finalize ){
G_OBJECT_CLASS( st_parent_class )->finalize( application );
}
}
@@ -264,22 +267,51 @@ nact_application_get_pivot( NactApplication *application )
return( NA_PIVOT( application->private->pivot ));
}
-static void
-warn_other_instance( BaseApplication *application )
+/*
+ * overrided to provide a personalized error message
+ */
+static gboolean
+appli_initialize_unique_app( BaseApplication *application )
{
- g_assert( NACT_IS_APPLICATION( application ));
+ /* call parent class */
+ gboolean ok = st_parent_class->application_initialize_unique_app( application );
+
+ if( !ok ){
+ gchar *msg1 = g_strdup( _( "Another instance of Nautilus Actions Configuration Tool is already running." ));
+ /* i18n: another instance is already running: second line of error message */
+ gchar *msg2 = g_strdup( _( "Please switch back to it." ));
+
+ g_object_set( G_OBJECT( application ),
+ PROP_APPLICATION_EXIT_MESSAGE1, msg1,
+ PROP_APPLICATION_EXIT_MESSAGE2, msg2,
+ NULL );
+
+ g_free( msg2 );
+ g_free( msg1 );
+ }
- base_application_error_dlg(
- application,
- GTK_MESSAGE_INFO,
- _( "Another instance of Nautilus Actions Configuration Tool is already running." ),
- _( "Please switch back to it." ));
+ return( ok );
+}
+
+/*
+ * overrided to complete the initialization of the application
+ * note that pivot must be initialized _before_ the main window be created
+ */
+static gboolean
+appli_initialize_application( BaseApplication *application )
+{
+ NACT_APPLICATION( application )->private->pivot = na_pivot_new( NULL );
+
+ /* call parent class */
+ gboolean ok = st_parent_class->application_initialize_application( application );
+
+ return( ok );
}
static gchar *
-get_application_name( BaseApplication *application )
+appli_get_application_name( BaseApplication *application )
{
- static const gchar *thisfn = "nact_application_get_application_name";
+ static const gchar *thisfn = "nact_application_appli_get_application_name";
g_debug( "%s: application=%p", thisfn, application );
/* i18n: this is the application name, used in window title */
@@ -287,33 +319,37 @@ get_application_name( BaseApplication *application )
}
static gchar *
-get_icon_name( BaseApplication *application )
+appli_get_icon_name( BaseApplication *application )
{
- static const gchar *thisfn = "nact_application_get_icon_name";
+ static const gchar *thisfn = "nact_application_appli_get_icon_name";
g_debug( "%s: application=%p", thisfn, application );
return( g_strdup( PACKAGE ));
}
static gchar *
-get_unique_name( BaseApplication *application )
+appli_get_unique_app_name( BaseApplication *application )
{
- static const gchar *thisfn = "nact_application_get_unique_name";
+ static const gchar *thisfn = "nact_application_appli_get_unique_app_name";
g_debug( "%s: application=%p", thisfn, application );
return( g_strdup( "org.nautilus-actions.ConfigurationTool" ));
}
static gchar *
-get_glade_file( BaseApplication *application )
+appli_get_gtkbuilder_filename( BaseApplication *application )
{
return( g_strdup( GLADE_FILENAME ));
}
+/*
+ * this should be called only once as base class is supposed to keep
+ * the pointer to the main BaseWindow window as a property
+ */
static GObject *
-get_main_window( BaseApplication *application )
+appli_get_main_window( BaseApplication *application )
{
- static const gchar *thisfn = "nact_application_get_main_window";
+ static const gchar *thisfn = "nact_application_appli_get_main_window";
g_debug( "%s: application=%p", thisfn, application );
BaseWindow *window = BASE_WINDOW( nact_main_window_new( application ));
diff --git a/src/nact/nact-main-window.c b/src/nact/nact-main-window.c
index 53cc657..2561c18 100644
--- a/src/nact/nact-main-window.c
+++ b/src/nact/nact-main-window.c
@@ -593,7 +593,7 @@ static void
setup_dialog_title( NactWindow *window )
{
BaseApplication *appli = BASE_APPLICATION( base_window_get_application( BASE_WINDOW( window )));
- gchar *title = base_application_get_name( appli );
+ gchar *title = base_application_get_application_name( appli );
if( NACT_MAIN_WINDOW( window )->private->edited_action ){
gchar *label = na_action_get_label( NACT_MAIN_WINDOW( window )->private->edited_action );
diff --git a/src/nact/nact-xml-reader.c b/src/nact/nact-xml-reader.c
index 86cc9d7..98d3bc5 100644
--- a/src/nact/nact-xml-reader.c
+++ b/src/nact/nact-xml-reader.c
@@ -43,8 +43,9 @@
#include <common/na-xml-names.h>
#include "nact-application.h"
-#include "nact-xml-reader.h"
+#include "nact-main-window.h"
#include "nact-assistant.h"
+#include "nact-xml-reader.h"
/* private class data
*/
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]