Update copyright message
[nautilus-actions.git] / src / nact / base-application.c
blob78ff5c90442e14bbf09bd67fcc63076498c0a21f
1 /*
2 * Nautilus Actions
3 * A Nautilus extension which offers configurable context menu actions.
5 * Copyright (C) 2005 The GNOME Foundation
6 * Copyright (C) 2006, 2007, 2008 Frederic Ruaudel and others (see AUTHORS)
7 * Copyright (C) 2009, 2010, 2011 Pierre Wieser and others (see AUTHORS)
9 * This Program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This Program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public
20 * License along with this Library; see the file COPYING. If not,
21 * write to the Free Software Foundation, Inc., 59 Temple Place,
22 * Suite 330, Boston, MA 02111-1307, USA.
24 * Authors:
25 * Frederic Ruaudel <grumz@grumz.net>
26 * Rodrigo Moya <rodrigo@gnome-db.org>
27 * Pierre Wieser <pwieser@trychlos.org>
28 * ... and many others (see AUTHORS)
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
35 #include <glib/gi18n.h>
36 #include <glib/gprintf.h>
37 #include <string.h>
38 #include <unique/unique.h>
40 #include "base-application.h"
41 #include "base-window.h"
42 #include "egg-sm-client.h"
44 /* private class data
46 struct BaseApplicationClassPrivate {
47 void *empty; /* so that gcc -pedantic is happy */
50 /* private instance data
52 struct BaseApplicationPrivate {
53 gboolean dispose_has_run;
54 int argc;
55 gpointer argv;
56 GOptionEntry *options;
57 gboolean is_gtk_initialized;
58 UniqueApp *unique_app_handle;
59 int exit_code;
60 gchar *exit_message1;
61 gchar *exit_message2;
62 BaseBuilder *builder;
63 BaseWindow *main_window;
64 EggSMClient *sm_client;
67 /* instance properties
69 enum {
70 BASE_APPLICATION_PROP_ARGC_ID = 1,
71 BASE_APPLICATION_PROP_ARGV_ID,
72 BASE_APPLICATION_PROP_OPTIONS_ID,
73 BASE_APPLICATION_PROP_IS_GTK_INITIALIZED_ID,
74 BASE_APPLICATION_PROP_UNIQUE_APP_HANDLE_ID,
75 BASE_APPLICATION_PROP_EXIT_CODE_ID,
76 BASE_APPLICATION_PROP_EXIT_MESSAGE1_ID,
77 BASE_APPLICATION_PROP_EXIT_MESSAGE2_ID,
78 BASE_APPLICATION_PROP_BUILDER_ID,
79 BASE_APPLICATION_PROP_MAIN_WINDOW_ID
82 static GObjectClass *st_parent_class = NULL;
84 static GType register_type( void );
85 static void class_init( BaseApplicationClass *klass );
86 static void instance_init( GTypeInstance *instance, gpointer klass );
87 static void instance_get_property( GObject *object, guint property_id, GValue *value, GParamSpec *spec );
88 static void instance_set_property( GObject *object, guint property_id, const GValue *value, GParamSpec *spec );
89 static void instance_dispose( GObject *application );
90 static void instance_finalize( GObject *application );
92 static gboolean v_initialize( BaseApplication *application );
93 static gboolean v_initialize_i18n( BaseApplication *application );
94 static gboolean v_initialize_gtk( BaseApplication *application );
95 static gboolean v_manage_options( BaseApplication *application );
96 static gboolean v_initialize_application_name( BaseApplication *application );
97 static gboolean v_initialize_session_manager( BaseApplication *application );
98 static gboolean v_initialize_unique_app( BaseApplication *application );
99 static gboolean v_initialize_ui( BaseApplication *application );
100 static gboolean v_initialize_default_icon( BaseApplication *application );
101 static gboolean v_initialize_application( BaseApplication *application );
103 static int application_do_run( BaseApplication *application );
104 static gboolean application_do_initialize( BaseApplication *application );
105 static gboolean application_do_initialize_i18n( BaseApplication *application );
106 static gboolean application_do_initialize_gtk( BaseApplication *application );
107 static gboolean application_do_manage_options( BaseApplication *application );
108 static gboolean application_do_initialize_application_name( BaseApplication *application );
109 static gboolean application_do_initialize_session_manager( BaseApplication *application );
110 static gboolean application_do_initialize_unique_app( BaseApplication *application );
111 static gboolean application_do_initialize_ui( BaseApplication *application );
112 static gboolean application_do_initialize_default_icon( BaseApplication *application );
113 static gboolean application_do_initialize_application( BaseApplication *application );
115 static gboolean check_for_unique_app( BaseApplication *application );
116 /*static UniqueResponse on_unique_message_received( UniqueApp *app, UniqueCommand command, UniqueMessageData *message, guint time, gpointer user_data );*/
118 static void client_quit_cb( EggSMClient *client, BaseApplication *application );
119 static void client_quit_requested_cb( EggSMClient *client, BaseApplication *application );
120 static gint display_dlg( BaseApplication *application, GtkMessageType type_message, GtkButtonsType type_buttons, const gchar *first, const gchar *second );
121 static void display_error_message( BaseApplication *application );
122 static gboolean init_with_args( BaseApplication *application, int *argc, char ***argv, GOptionEntry *entries );
123 static void set_initialize_i18n_error( BaseApplication *application );
124 static void set_initialize_gtk_error( BaseApplication *application );
125 static void set_initialize_unique_app_error( BaseApplication *application );
126 static void set_initialize_ui_get_fname_error( BaseApplication *application );
127 static void set_initialize_ui_add_xml_error( BaseApplication *application, const gchar *filename, GError *error );
128 static void set_initialize_default_icon_error( BaseApplication *application );
129 static void set_initialize_application_error( BaseApplication *application );
131 GType
132 base_application_get_type( void )
134 static GType application_type = 0;
136 if( !application_type ){
137 application_type = register_type();
140 return( application_type );
143 static GType
144 register_type( void )
146 static const gchar *thisfn = "base_application_register_type";
147 GType type;
149 static GTypeInfo info = {
150 sizeof( BaseApplicationClass ),
151 ( GBaseInitFunc ) NULL,
152 ( GBaseFinalizeFunc ) NULL,
153 ( GClassInitFunc ) class_init,
154 NULL,
155 NULL,
156 sizeof( BaseApplication ),
158 ( GInstanceInitFunc ) instance_init
161 g_debug( "%s", thisfn );
163 g_type_init();
165 type = g_type_register_static( G_TYPE_OBJECT, "BaseApplication", &info, 0 );
167 return( type );
170 static void
171 class_init( BaseApplicationClass *klass )
173 static const gchar *thisfn = "base_application_class_init";
174 GObjectClass *object_class;
175 GParamSpec *spec;
177 g_debug( "%s: klass=%p", thisfn, ( void * ) klass );
179 st_parent_class = g_type_class_peek_parent( klass );
181 object_class = G_OBJECT_CLASS( klass );
182 object_class->dispose = instance_dispose;
183 object_class->finalize = instance_finalize;
184 object_class->get_property = instance_get_property;
185 object_class->set_property = instance_set_property;
187 spec = g_param_spec_int(
188 BASE_APPLICATION_PROP_ARGC,
189 "Command-line arguments count",
190 "Command-line arguments count", 0, 65535, 0,
191 G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE );
192 g_object_class_install_property( object_class, BASE_APPLICATION_PROP_ARGC_ID, spec );
194 spec = g_param_spec_pointer(
195 BASE_APPLICATION_PROP_ARGV,
196 "Command-line arguments",
197 "A pointer to command-line arguments",
198 G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE );
199 g_object_class_install_property( object_class, BASE_APPLICATION_PROP_ARGV_ID, spec );
201 spec = g_param_spec_pointer(
202 BASE_APPLICATION_PROP_OPTIONS,
203 "Command-line options",
204 "A pointer to command-line options",
205 G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE );
206 g_object_class_install_property( object_class, BASE_APPLICATION_PROP_OPTIONS_ID, spec );
208 spec = g_param_spec_boolean(
209 BASE_APPLICATION_PROP_IS_GTK_INITIALIZED,
210 "Gtk+ initialization flag",
211 "Has Gtk+ be initialized ?", FALSE,
212 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE );
213 g_object_class_install_property( object_class, BASE_APPLICATION_PROP_IS_GTK_INITIALIZED_ID, spec );
215 spec = g_param_spec_pointer(
216 BASE_APPLICATION_PROP_UNIQUE_APP_HANDLE,
217 "UniqueApp object pointer",
218 "A reference to the UniqueApp object if any",
219 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE );
220 g_object_class_install_property( object_class, BASE_APPLICATION_PROP_UNIQUE_APP_HANDLE_ID, spec );
222 spec = g_param_spec_int(
223 BASE_APPLICATION_PROP_EXIT_CODE,
224 "Exit code",
225 "Exit code of the application", 0, 65535, 0,
226 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE );
227 g_object_class_install_property( object_class, BASE_APPLICATION_PROP_EXIT_CODE_ID, spec );
229 spec = g_param_spec_string(
230 BASE_APPLICATION_PROP_EXIT_MESSAGE1,
231 "Error message",
232 "First line of the error message displayed when exit_code not nul", "",
233 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE );
234 g_object_class_install_property( object_class, BASE_APPLICATION_PROP_EXIT_MESSAGE1_ID, spec );
236 spec = g_param_spec_string(
237 BASE_APPLICATION_PROP_EXIT_MESSAGE2,
238 "Error message",
239 "Second line of the error message displayed when exit_code not nul", "",
240 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE );
241 g_object_class_install_property( object_class, BASE_APPLICATION_PROP_EXIT_MESSAGE2_ID, spec );
243 spec = g_param_spec_pointer(
244 BASE_APPLICATION_PROP_BUILDER,
245 "UI object pointer",
246 "A reference to the UI definition from GtkBuilder",
247 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE );
248 g_object_class_install_property( object_class, BASE_APPLICATION_PROP_BUILDER_ID, spec );
250 spec = g_param_spec_pointer(
251 BASE_APPLICATION_PROP_MAIN_WINDOW,
252 "Main BaseWindow object",
253 "A reference to the main BaseWindow object",
254 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE );
255 g_object_class_install_property( object_class, BASE_APPLICATION_PROP_MAIN_WINDOW_ID, spec );
257 klass->private = g_new0( BaseApplicationClassPrivate, 1 );
259 klass->run = application_do_run;
260 klass->initialize = application_do_initialize;
261 klass->initialize_i18n = application_do_initialize_i18n;
262 klass->initialize_gtk = application_do_initialize_gtk;
263 klass->manage_options = application_do_manage_options;
264 klass->initialize_application_name = application_do_initialize_application_name;
265 klass->initialize_session_manager = application_do_initialize_session_manager;
266 klass->initialize_unique_app = application_do_initialize_unique_app;
267 klass->initialize_ui = application_do_initialize_ui;
268 klass->initialize_default_icon = application_do_initialize_default_icon;
269 klass->initialize_application = application_do_initialize_application;
270 klass->get_application_name = NULL;
271 klass->get_icon_name = NULL;
272 klass->get_unique_app_name = NULL;
273 klass->get_ui_filename = NULL;
274 klass->get_main_window = NULL;
277 static void
278 instance_init( GTypeInstance *application, gpointer klass )
280 static const gchar *thisfn = "base_application_instance_init";
281 BaseApplication *self;
283 g_debug( "%s: application=%p (%s), klass=%p",
284 thisfn, ( void * ) application, G_OBJECT_TYPE_NAME( application ), ( void * ) klass );
286 g_return_if_fail( BASE_IS_APPLICATION( application ));
287 self = BASE_APPLICATION( application );
289 self->private = g_new0( BaseApplicationPrivate, 1 );
291 self->private->dispose_has_run = FALSE;
292 self->private->exit_code = 0;
295 static void
296 instance_get_property( GObject *object, guint property_id, GValue *value, GParamSpec *spec )
298 BaseApplication *self;
300 g_return_if_fail( BASE_IS_APPLICATION( object ));
301 self = BASE_APPLICATION( object );
303 if( !self->private->dispose_has_run ){
305 switch( property_id ){
306 case BASE_APPLICATION_PROP_ARGC_ID:
307 g_value_set_int( value, self->private->argc );
308 break;
310 case BASE_APPLICATION_PROP_ARGV_ID:
311 g_value_set_pointer( value, self->private->argv );
312 break;
314 case BASE_APPLICATION_PROP_OPTIONS_ID:
315 g_value_set_pointer( value, self->private->options );
316 break;
318 case BASE_APPLICATION_PROP_IS_GTK_INITIALIZED_ID:
319 g_value_set_boolean( value, self->private->is_gtk_initialized );
320 break;
322 case BASE_APPLICATION_PROP_UNIQUE_APP_HANDLE_ID:
323 g_value_set_pointer( value, self->private->unique_app_handle );
324 break;
326 case BASE_APPLICATION_PROP_EXIT_CODE_ID:
327 g_value_set_int( value, self->private->exit_code );
328 break;
330 case BASE_APPLICATION_PROP_EXIT_MESSAGE1_ID:
331 g_value_set_string( value, self->private->exit_message1 );
332 break;
334 case BASE_APPLICATION_PROP_EXIT_MESSAGE2_ID:
335 g_value_set_string( value, self->private->exit_message2 );
336 break;
338 case BASE_APPLICATION_PROP_BUILDER_ID:
339 g_value_set_pointer( value, self->private->builder );
340 break;
342 case BASE_APPLICATION_PROP_MAIN_WINDOW_ID:
343 g_value_set_pointer( value, self->private->main_window );
344 break;
346 default:
347 G_OBJECT_WARN_INVALID_PROPERTY_ID( object, property_id, spec );
348 break;
353 static void
354 instance_set_property( GObject *object, guint property_id, const GValue *value, GParamSpec *spec )
356 BaseApplication *self;
358 g_return_if_fail( BASE_IS_APPLICATION( object ));
359 self = BASE_APPLICATION( object );
361 if( !self->private->dispose_has_run ){
363 switch( property_id ){
364 case BASE_APPLICATION_PROP_ARGC_ID:
365 self->private->argc = g_value_get_int( value );
366 break;
368 case BASE_APPLICATION_PROP_ARGV_ID:
369 self->private->argv = g_value_get_pointer( value );
370 break;
372 case BASE_APPLICATION_PROP_OPTIONS_ID:
373 self->private->options = g_value_get_pointer( value );
374 break;
376 case BASE_APPLICATION_PROP_IS_GTK_INITIALIZED_ID:
377 self->private->is_gtk_initialized = g_value_get_boolean( value );
378 break;
380 case BASE_APPLICATION_PROP_UNIQUE_APP_HANDLE_ID:
381 self->private->unique_app_handle = g_value_get_pointer( value );
382 break;
384 case BASE_APPLICATION_PROP_EXIT_CODE_ID:
385 self->private->exit_code = g_value_get_int( value );
386 break;
388 case BASE_APPLICATION_PROP_EXIT_MESSAGE1_ID:
389 g_free( self->private->exit_message1 );
390 self->private->exit_message1 = g_value_dup_string( value );
391 break;
393 case BASE_APPLICATION_PROP_EXIT_MESSAGE2_ID:
394 g_free( self->private->exit_message2 );
395 self->private->exit_message2 = g_value_dup_string( value );
396 break;
398 case BASE_APPLICATION_PROP_BUILDER_ID:
399 self->private->builder = g_value_get_pointer( value );
400 break;
402 case BASE_APPLICATION_PROP_MAIN_WINDOW_ID:
403 self->private->main_window = g_value_get_pointer( value );
404 break;
406 default:
407 G_OBJECT_WARN_INVALID_PROPERTY_ID( object, property_id, spec );
408 break;
413 static void
414 instance_dispose( GObject *application )
416 static const gchar *thisfn = "base_application_instance_dispose";
417 BaseApplication *self;
419 g_debug( "%s: application=%p (%s)", thisfn, ( void * ) application, G_OBJECT_TYPE_NAME( application ));
420 g_return_if_fail( BASE_IS_APPLICATION( application ));
421 self = BASE_APPLICATION( application );
423 if( !self->private->dispose_has_run ){
425 self->private->dispose_has_run = TRUE;
427 if( UNIQUE_IS_APP( self->private->unique_app_handle )){
428 g_object_unref( self->private->unique_app_handle );
431 if( GTK_IS_BUILDER( self->private->builder )){
432 g_object_unref( self->private->builder );
435 if( self->private->sm_client ){
436 g_object_unref( self->private->sm_client );
439 /* chain up to the parent class */
440 if( G_OBJECT_CLASS( st_parent_class )->dispose ){
441 G_OBJECT_CLASS( st_parent_class )->dispose( application );
446 static void
447 instance_finalize( GObject *application )
449 static const gchar *thisfn = "base_application_instance_finalize";
450 BaseApplication *self;
452 g_debug( "%s: application=%p", thisfn, ( void * ) application );
453 g_return_if_fail( BASE_IS_APPLICATION( application ));
454 self = BASE_APPLICATION( application );
456 g_free( self->private->exit_message1 );
457 g_free( self->private->exit_message2 );
459 g_free( self->private );
461 /* chain call to parent class */
462 if( G_OBJECT_CLASS( st_parent_class )->finalize ){
463 G_OBJECT_CLASS( st_parent_class )->finalize( application );
468 * base_application_run:
469 * @application: this #BaseApplication instance.
471 * Starts and runs the application.
472 * Takes care of creating, initializing, and running the main window.
474 * All steps are implemented as virtual functions which provide some
475 * suitable defaults, and may be overriden by a derived class.
477 * Returns: an %int code suitable as an exit code for the program.
479 * Though it is defined as a virtual function itself, it should be very
480 * seldomly needed to override this in a derived class.
483 base_application_run( BaseApplication *application )
485 static const gchar *thisfn = "base_application_run";
486 int code = -1;
488 g_debug( "%s: application=%p", thisfn, ( void * ) application );
490 g_return_val_if_fail( BASE_IS_APPLICATION( application ), -1 );
492 if( !application->private->dispose_has_run ){
493 code = BASE_APPLICATION_GET_CLASS( application )->run( application );
496 return( code );
500 * base_application_get_application_name:
501 * @application: this #BaseApplication instance.
503 * Asks the #BaseApplication-derived class for its localized
504 * application name.
506 * Defaults to empty.
508 * Returns: a newly allocated string to be g_free() by the caller.
510 gchar *
511 base_application_get_application_name( BaseApplication *application )
513 /*static const gchar *thisfn = "base_application_get_application_name";
514 g_debug( "%s: application=%p", thisfn, application );*/
515 gchar *name = NULL;
517 g_return_val_if_fail( BASE_IS_APPLICATION( application ), NULL );
519 if( !application->private->dispose_has_run ){
520 if( BASE_APPLICATION_GET_CLASS( application )->get_application_name ){
521 name = BASE_APPLICATION_GET_CLASS( application )->get_application_name( application );
523 } else {
524 name = g_strdup( "" );
528 return( name );
532 * base_application_get_builder:
533 * @application: this #BaseApplication instance.
535 * Returns: the default #BaseBuilder object for the application.
537 BaseBuilder *
538 base_application_get_builder( BaseApplication *application )
540 /*static const gchar *thisfn = "base_application_get_application_name";
541 g_debug( "%s: application=%p", thisfn, application );*/
542 BaseBuilder *builder = NULL;
544 g_return_val_if_fail( BASE_IS_APPLICATION( application ), NULL );
546 if( !application->private->dispose_has_run ){
547 builder = application->private->builder;
550 return( builder );
554 * base_application_get_icon_name:
555 * @application: this #BaseApplication instance.
557 * Asks the #BaseApplication-derived class for its default icon name.
559 * Defaults to empty.
561 * Returns: a newly allocated string to be g_free() by the caller.
563 gchar *
564 base_application_get_icon_name( BaseApplication *application )
566 /*static const gchar *thisfn = "base_application_get_icon_name";
567 g_debug( "%s: icon=%p", thisfn, application );*/
568 gchar *name = NULL;
570 g_return_val_if_fail( BASE_IS_APPLICATION( application ), NULL );
572 if( !application->private->dispose_has_run ){
573 if( BASE_APPLICATION_GET_CLASS( application )->get_icon_name ){
574 name = BASE_APPLICATION_GET_CLASS( application )->get_icon_name( application );
576 } else {
577 name = g_strdup( "" );
581 return( name );
585 * base_application_get_unique_app_name:
586 * @application: this #BaseApplication instance.
588 * Asks the #BaseApplication-derived class for its UniqueApp name if any.
590 * Defaults to empty.
592 * Returns: a newly allocated string to be g_free() by the caller.
594 gchar *
595 base_application_get_unique_app_name( BaseApplication *application )
597 /*static const gchar *thisfn = "base_application_get_unique_app_name";
598 g_debug( "%s: icon=%p", thisfn, application );*/
599 gchar *name = NULL;
601 g_return_val_if_fail( BASE_IS_APPLICATION( application ), NULL );
603 if( !application->private->dispose_has_run ){
604 if( BASE_APPLICATION_GET_CLASS( application )->get_unique_app_name ){
605 name = BASE_APPLICATION_GET_CLASS( application )->get_unique_app_name( application );
607 } else {
608 name = g_strdup( "" );
612 return( name );
616 * base_application_get_ui_filename:
617 * @application: this #BaseApplication instance.
619 * Asks the #BaseApplication-derived class for the filename of the file
620 * which contains the XML definition of the user interface.
622 * Defaults to empty.
624 * Returns: a newly allocated string to be g_free() by the caller.
626 gchar *
627 base_application_get_ui_filename( BaseApplication *application )
629 /*static const gchar *thisfn = "base_application_get_ui_filename";
630 g_debug( "%s: icon=%p", thisfn, application );*/
631 gchar *name = NULL;
633 g_return_val_if_fail( BASE_IS_APPLICATION( application ), NULL );
635 if( !application->private->dispose_has_run ){
636 if( BASE_APPLICATION_GET_CLASS( application )->get_ui_filename ){
637 name = BASE_APPLICATION_GET_CLASS( application )->get_ui_filename( application );
639 } else {
640 name = g_strdup( "" );
644 return( name );
648 * base_application_get_main_window:
649 * @application: this #BaseApplication instance.
651 * Returns: a pointer to the #BaseWindow-derived object which serves as
652 * the main window of the application.
654 * The returned pointer is owned by @application, and thus should not be
655 * g_free() nor g_object_unref() by the caller.
657 * When first called, #BaseApplication asks for its derived class to
658 * allocate a new object. This same object is then returned on
659 * subsequent calls.
661 BaseWindow *
662 base_application_get_main_window( BaseApplication *application )
664 /*static const gchar *thisfn = "base_application_get_main_window";
665 g_debug( "%s: application=%p", thisfn, application );*/
667 g_return_val_if_fail( BASE_IS_APPLICATION( application ), NULL );
669 if( !application->private->dispose_has_run ){
670 if( !application->private->main_window &&
671 BASE_APPLICATION_GET_CLASS( application )->get_main_window ){
672 application->private->main_window = BASE_WINDOW( BASE_APPLICATION_GET_CLASS( application )->get_main_window( application ));
676 return( application->private->main_window );
680 * base_application_message_dlg:
681 * @application: this #BaseApplication instance.
682 * @message: the message to be displayed.
684 * Displays a dialog with only an OK button.
686 void
687 base_application_message_dlg( BaseApplication *application, GSList *msg )
689 GString *string;
690 GSList *im;
692 if( !application->private->dispose_has_run ){
694 string = g_string_new( "" );
695 for( im = msg ; im ; im = im->next ){
696 if( g_utf8_strlen( string->str, -1 )){
697 string = g_string_append( string, "\n" );
699 string = g_string_append( string, ( gchar * ) im->data );
701 display_dlg( application, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, string->str, NULL );
703 g_string_free( string, TRUE );
708 * base_application_error_dlg:
709 * @application: this #BaseApplication instance.
710 * @type:
711 * @primary: a first message.
712 * @secondaru: a second message.
714 * Displays an error dialog with only an OK button.
716 void
717 base_application_error_dlg( BaseApplication *application,
718 GtkMessageType type,
719 const gchar *first,
720 const gchar *second )
722 if( !application->private->dispose_has_run ){
723 display_dlg( application, type, GTK_BUTTONS_OK, first, second );
728 * base_application_yesno_dlg:
729 * @application: this #BaseApplication instance.
730 * @type:
731 * @primary: a first message.
732 * @secondaru: a second message.
734 * Displays a choice dialog, with Yes and No buttons.
735 * No button is the default.
737 * Returns: %TRUE if user has clicked on Yes button, %FALSE else.
739 gboolean
740 base_application_yesno_dlg( BaseApplication *application, GtkMessageType type, const gchar *first, const gchar *second )
742 gboolean ret = FALSE;
743 gint result;
745 if( !application->private->dispose_has_run ){
747 result = display_dlg( application, type, GTK_BUTTONS_YES_NO, first, second );
748 ret = ( result == GTK_RESPONSE_YES );
751 return( ret );
754 static gboolean
755 v_initialize( BaseApplication *application )
757 static const gchar *thisfn = "base_application_v_initialize";
759 g_debug( "%s: application=%p", thisfn, ( void * ) application );
761 return( BASE_APPLICATION_GET_CLASS( application )->initialize( application ));
764 static gboolean
765 v_initialize_i18n( BaseApplication *application )
767 static const gchar *thisfn = "base_application_v_initialize_i18n";
768 gboolean ok;
770 g_debug( "%s: application=%p", thisfn, ( void * ) application );
772 ok = BASE_APPLICATION_GET_CLASS( application )->initialize_i18n( application );
774 if( !ok ){
775 set_initialize_i18n_error( application );
778 return( ok );
781 static gboolean
782 v_initialize_gtk( BaseApplication *application )
784 static const gchar *thisfn = "base_application_v_initialize_gtk";
785 gboolean ok;
787 g_debug( "%s: application=%p", thisfn, ( void * ) application );
789 ok = BASE_APPLICATION_GET_CLASS( application )->initialize_gtk( application );
791 if( ok ){
792 application->private->is_gtk_initialized = TRUE;
794 } else {
795 set_initialize_gtk_error( application );
798 return( ok );
801 static gboolean
802 v_manage_options( BaseApplication *application )
804 static const gchar *thisfn = "base_application_v_manage_options";
805 gboolean ok;
807 g_debug( "%s: application=%p", thisfn, ( void * ) application );
809 ok = BASE_APPLICATION_GET_CLASS( application )->manage_options( application );
811 return( ok );
814 static gboolean
815 v_initialize_application_name( BaseApplication *application )
817 static const gchar *thisfn = "base_application_v_initialize_application_name";
818 gboolean ok;
820 g_debug( "%s: application=%p", thisfn, ( void * ) application );
822 ok = BASE_APPLICATION_GET_CLASS( application )->initialize_application_name( application );
824 return( ok );
827 static gboolean
828 v_initialize_session_manager( BaseApplication *application )
830 static const gchar *thisfn = "base_application_v_initialize_session_manager";
831 gboolean ok;
833 g_debug( "%s: application=%p", thisfn, ( void * ) application );
835 ok = BASE_APPLICATION_GET_CLASS( application )->initialize_session_manager( application );
837 return( ok );
840 static gboolean
841 v_initialize_unique_app( BaseApplication *application )
843 static const gchar *thisfn = "base_application_v_initialize_unique_app";
844 gboolean ok;
846 g_debug( "%s: application=%p", thisfn, ( void * ) application );
848 ok = BASE_APPLICATION_GET_CLASS( application )->initialize_unique_app( application );
850 if( !ok ){
851 set_initialize_unique_app_error( application );
854 return( ok );
857 static gboolean
858 v_initialize_ui( BaseApplication *application )
860 static const gchar *thisfn = "base_application_v_initialize_ui";
862 g_debug( "%s: application=%p", thisfn, ( void * ) application );
864 return( BASE_APPLICATION_GET_CLASS( application )->initialize_ui( application ));
867 static gboolean
868 v_initialize_default_icon( BaseApplication *application )
870 static const gchar *thisfn = "base_application_v_initialize_default_icon";
871 gboolean ok;
873 g_debug( "%s: application=%p", thisfn, ( void * ) application );
875 ok = BASE_APPLICATION_GET_CLASS( application )->initialize_default_icon( application );
877 if( !ok ){
878 set_initialize_default_icon_error( application );
881 return( ok );
884 static gboolean
885 v_initialize_application( BaseApplication *application )
887 static const gchar *thisfn = "base_application_v_initialize_application";
888 gboolean ok;
890 g_debug( "%s: application=%p", thisfn, ( void * ) application );
892 ok = BASE_APPLICATION_GET_CLASS( application )->initialize_application( application );
894 if( !ok ){
895 set_initialize_application_error( application );
898 return( ok );
901 static int
902 application_do_run( BaseApplication *application )
904 static const gchar *thisfn = "base_application_do_run";
905 GtkWindow *wnd;
907 g_debug( "%s: application=%p", thisfn, ( void * ) application );
909 if( v_initialize( application )){
911 g_return_val_if_fail( application->private->main_window, -1 );
912 g_return_val_if_fail( BASE_IS_WINDOW( application->private->main_window ), -1 );
914 if( base_window_init( application->private->main_window )){
916 wnd = base_window_get_toplevel( application->private->main_window );
917 g_assert( wnd );
918 g_assert( GTK_IS_WINDOW( wnd ));
920 if( application->private->unique_app_handle ){
921 unique_app_watch_window( application->private->unique_app_handle, wnd );
924 base_window_run( application->private->main_window );
928 display_error_message( application );
930 return( application->private->exit_code );
933 static gboolean
934 application_do_initialize( BaseApplication *application )
936 static const gchar *thisfn = "base_application_do_initialize";
938 g_debug( "%s: application=%p", thisfn, ( void * ) application );
940 return(
941 v_initialize_i18n( application ) &&
942 v_initialize_application_name( application ) &&
943 v_initialize_gtk( application ) &&
944 v_manage_options( application ) &&
945 v_initialize_session_manager( application ) &&
946 v_initialize_unique_app( application ) &&
947 v_initialize_ui( application ) &&
948 v_initialize_default_icon( application ) &&
949 v_initialize_application( application )
953 static gboolean
954 application_do_initialize_i18n( BaseApplication *application )
956 static const gchar *thisfn = "base_application_do_initialize_i18n";
958 g_debug( "%s: application=%p", thisfn, ( void * ) application );
960 #ifdef ENABLE_NLS
961 bindtextdomain( GETTEXT_PACKAGE, GNOMELOCALEDIR );
962 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
963 bind_textdomain_codeset( GETTEXT_PACKAGE, "UTF-8" );
964 # endif
965 textdomain( GETTEXT_PACKAGE );
966 #endif
967 return( TRUE );
970 static gboolean
971 application_do_initialize_gtk( BaseApplication *application )
973 static const gchar *thisfn = "base_application_do_initialize_gtk";
974 int argc;
975 gpointer argv;
976 gboolean ret;
977 GOptionEntry *options;
979 g_debug( "%s: application=%p", thisfn, ( void * ) application );
981 g_object_get( G_OBJECT( application ),
982 BASE_APPLICATION_PROP_ARGC, &argc,
983 BASE_APPLICATION_PROP_ARGV, &argv,
984 BASE_APPLICATION_PROP_OPTIONS, &options,
985 NULL );
987 if( options ){
988 ret = init_with_args( application, &argc, ( char *** ) &argv, options );
989 } else {
990 ret = gtk_init_check( &argc, ( char *** ) &argv );
993 if( ret ){
994 application->private->argc = argc;
995 application->private->argv = argv;
998 return( ret );
1001 static gboolean
1002 application_do_manage_options( BaseApplication *application )
1004 static const gchar *thisfn = "base_application_do_manage_options";
1006 g_debug( "%s: application=%p", thisfn, ( void * ) application );
1008 return( TRUE );
1011 static gboolean
1012 application_do_initialize_application_name( BaseApplication *application )
1014 static const gchar *thisfn = "base_application_do_initialize_application_name";
1015 gchar *name;
1017 g_debug( "%s: application=%p", thisfn, ( void * ) application );
1019 name = base_application_get_application_name( application );
1020 if( name && g_utf8_strlen( name, -1 )){
1021 g_set_application_name( name );
1023 g_free( name );
1025 return( TRUE );
1028 static gboolean
1029 application_do_initialize_session_manager( BaseApplication *application )
1031 static const gchar *thisfn = "base_application_do_initialize_session_manager";
1032 gboolean ret = TRUE;
1034 g_debug( "%s: application=%p", thisfn, ( void * ) application );
1036 egg_sm_client_set_mode( EGG_SM_CLIENT_MODE_NO_RESTART );
1037 application->private->sm_client = egg_sm_client_get();
1038 egg_sm_client_startup();
1039 g_debug( "%s: sm_client=%p", thisfn, ( void * ) application->private->sm_client );
1041 g_signal_connect(
1042 application->private->sm_client,
1043 "quit-requested",
1044 G_CALLBACK( client_quit_requested_cb ),
1045 application );
1047 g_signal_connect(
1048 application->private->sm_client,
1049 "quit",
1050 G_CALLBACK( client_quit_cb ),
1051 application );
1053 return( ret );
1056 static gboolean
1057 application_do_initialize_unique_app( BaseApplication *application )
1059 static const gchar *thisfn = "base_application_do_initialize_unique_app";
1060 gboolean ret = TRUE;
1061 gchar *name;
1063 g_debug( "%s: application=%p", thisfn, ( void * ) application );
1065 name = base_application_get_unique_app_name( application );
1067 if( name && strlen( name )){
1068 application->private->unique_app_handle = unique_app_new( name, NULL );
1069 ret = check_for_unique_app( application );
1072 g_free( name );
1073 return( ret );
1076 static gboolean
1077 application_do_initialize_ui( BaseApplication *application )
1079 static const gchar *thisfn = "base_application_do_initialize_ui";
1080 gboolean ret = TRUE;
1081 GError *error = NULL;
1082 gchar *name;
1084 g_debug( "%s: application=%p", thisfn, ( void * ) application );
1086 name = base_application_get_ui_filename( application );
1088 if( !name || !strlen( name )){
1089 ret = FALSE;
1090 set_initialize_ui_get_fname_error( application );
1092 } else {
1093 application->private->builder = base_builder_new();
1094 if( !base_builder_add_from_file( application->private->builder, name, &error )){
1095 ret = FALSE;
1096 set_initialize_ui_add_xml_error( application, name, error );
1097 if( error ){
1098 g_error_free( error );
1103 g_free( name );
1104 return( ret );
1107 static gboolean
1108 application_do_initialize_default_icon( BaseApplication *application )
1110 static const gchar *thisfn = "base_application_do_initialize_default_icon";
1111 gchar *name;
1113 g_debug( "%s: application=%p", thisfn, ( void * ) application );
1115 name = base_application_get_icon_name( application );
1117 if( name && strlen( name )){
1118 gtk_window_set_default_icon_name( name );
1121 g_free( name );
1123 return( TRUE );
1126 static gboolean
1127 application_do_initialize_application( BaseApplication *application )
1129 static const gchar *thisfn = "base_application_do_initialize_application";
1130 BaseWindow *window;
1132 g_debug( "%s: application=%p", thisfn, ( void * ) application );
1134 window = base_application_get_main_window( application );
1136 return( window != NULL );
1139 static gboolean
1140 check_for_unique_app( BaseApplication *application )
1142 static const gchar *thisfn = "base_application_check_for_unique_app";
1143 gboolean is_first = TRUE;
1145 g_debug( "%s: application=%p", thisfn, ( void * ) application );
1146 g_assert( BASE_IS_APPLICATION( application ));
1148 if( unique_app_is_running( application->private->unique_app_handle )){
1150 is_first = FALSE;
1152 unique_app_send_message( application->private->unique_app_handle, UNIQUE_ACTIVATE, NULL );
1154 /* default from libunique is actually to activate the first window
1155 * so we rely on the default..
1157 /*} else {
1158 g_signal_connect(
1159 application->private->unique,
1160 "message-received",
1161 G_CALLBACK( on_unique_message_received ),
1162 application
1163 );*/
1166 return( is_first );
1169 /*static UniqueResponse
1170 on_unique_message_received(
1171 UniqueApp *app, UniqueCommand command, UniqueMessageData *message, guint time, gpointer user_data )
1173 static const gchar *thisfn = "base_application_check_for_unique_app";
1174 UniqueResponse resp = UNIQUE_RESPONSE_OK;
1176 switch( command ){
1177 case UNIQUE_ACTIVATE:
1178 g_debug( "%s: received message UNIQUE_ACTIVATE", thisfn );
1179 break;
1180 default:
1181 resp = UNIQUE_RESPONSE_PASSTHROUGH;
1182 break;
1185 return( resp );
1188 static void
1189 client_quit_cb( EggSMClient *client, BaseApplication *application )
1191 static const gchar *thisfn = "base_application_client_quit_cb";
1193 g_debug( "%s: client=%p, application=%p", thisfn, ( void * ) client, ( void * ) application );
1195 if( BASE_IS_WINDOW( application->private->main_window )){
1196 g_object_unref( application->private->main_window );
1197 application->private->main_window = NULL;
1201 static void
1202 client_quit_requested_cb( EggSMClient *client, BaseApplication *application )
1204 static const gchar *thisfn = "base_application_client_quit_requested_cb";
1205 gboolean willing_to = TRUE;
1207 g_debug( "%s: client=%p, application=%p", thisfn, ( void * ) client, ( void * ) application );
1209 if( BASE_IS_WINDOW( application->private->main_window )){
1210 willing_to = base_window_is_willing_to_quit( application->private->main_window );
1213 egg_sm_client_will_quit( client, willing_to );
1216 static gint
1217 display_dlg( BaseApplication *application, GtkMessageType type_message, GtkButtonsType type_buttons, const gchar *first, const gchar *second )
1219 GtkWidget *dialog;
1220 const gchar *name;
1221 gint result;
1223 g_assert( BASE_IS_APPLICATION( application ));
1225 dialog = gtk_message_dialog_new( NULL, GTK_DIALOG_MODAL, type_message, type_buttons, "%s", first );
1227 if( second && g_utf8_strlen( second, -1 )){
1228 gtk_message_dialog_format_secondary_text( GTK_MESSAGE_DIALOG( dialog ), "%s", second );
1231 name = g_get_application_name();
1233 g_object_set( G_OBJECT( dialog ) , "title", name, NULL );
1235 result = gtk_dialog_run( GTK_DIALOG( dialog ));
1237 gtk_widget_destroy( dialog );
1239 return( result );
1242 static void
1243 display_error_message( BaseApplication *application )
1245 if( application->private->exit_message1 && g_utf8_strlen( application->private->exit_message1, -1 )){
1247 if( application->private->is_gtk_initialized ){
1248 base_application_error_dlg(
1249 application,
1250 GTK_MESSAGE_INFO,
1251 application->private->exit_message1,
1252 application->private->exit_message2 );
1254 } else {
1255 g_printf( "%s\n", application->private->exit_message1 );
1256 if( application->private->exit_message2 && g_utf8_strlen( application->private->exit_message2, -1 )){
1257 g_printf( "%s\n", application->private->exit_message2 );
1263 static gboolean
1264 init_with_args( BaseApplication *application, int *argc, char ***argv, GOptionEntry *entries )
1266 static const gchar *thisfn = "base_application_init_with_args";
1267 gboolean ret;
1268 char *parameter_string;
1269 GError *error;
1271 parameter_string = g_strdup( g_get_application_name());
1272 error = NULL;
1274 ret = gtk_init_with_args( argc, argv, parameter_string, entries, GETTEXT_PACKAGE, &error );
1275 if( error ){
1276 g_warning( "%s: %s", thisfn, error->message );
1277 g_error_free( error );
1278 ret = FALSE;
1281 g_free( parameter_string );
1283 return( ret );
1286 static void
1287 set_initialize_i18n_error( BaseApplication *application )
1289 application->private->exit_code = BASE_APPLICATION_ERROR_I18N;
1291 application->private->exit_message1 =
1292 g_strdup( _( "Unable to initialize the internationalization environment." ));
1295 static void
1296 set_initialize_gtk_error( BaseApplication *application )
1298 application->private->exit_code = BASE_APPLICATION_ERROR_GTK;
1300 application->private->exit_message1 =
1301 g_strdup( _( "Unable to initialize the Gtk+ user interface." ));
1304 static void
1305 set_initialize_unique_app_error( BaseApplication *application )
1307 application->private->exit_code = BASE_APPLICATION_ERROR_UNIQUE_APP;
1309 application->private->exit_message1 =
1310 g_strdup( _( "Another instance of the application is already running." ));
1313 static void
1314 set_initialize_ui_get_fname_error( BaseApplication *application )
1316 application->private->exit_code = BASE_APPLICATION_ERROR_UI_FNAME;
1318 application->private->exit_message1 =
1319 g_strdup( _( "No filename provided for the UI XML definition." ));
1322 static void
1323 set_initialize_ui_add_xml_error( BaseApplication *application, const gchar *filename, GError *error )
1325 application->private->exit_code = BASE_APPLICATION_ERROR_UI_LOAD;
1327 application->private->exit_message1 =
1328 /* i18n: Unable to load the XML definition from <filename> */
1329 g_strdup_printf( _( "Unable to load the XML definition from %s." ), filename );
1331 if( error && error->message ){
1332 application->private->exit_message2 = g_strdup( error->message );
1336 static void
1337 set_initialize_default_icon_error( BaseApplication *application )
1339 application->private->exit_code = BASE_APPLICATION_ERROR_DEFAULT_ICON;
1341 application->private->exit_message1 =
1342 g_strdup( _( "Unable to set the default icon for the application." ));
1345 static void
1346 set_initialize_application_error( BaseApplication *application )
1348 application->private->exit_code = BASE_APPLICATION_ERROR_MAIN_WINDOW;
1350 application->private->exit_message1 =
1351 g_strdup( _( "Unable to get the main window of the application." ));