Remove BaseApplication::initialize_ui() virtual method
[nautilus-actions.git] / src / nact / base-application.c
blob67df73712f3c6f07af728ea8a934388c2ef7febe
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;
55 /* properties
57 int argc;
58 GStrv argv;
59 GOptionEntry *options;
60 gchar *application_name;
61 gchar *icon_name;
62 gchar *unique_app_name;
64 /* internals
66 EggSMClient *sm_client;
67 BaseBuilder *builder;
68 BaseWindow *main_window;
70 gboolean is_gtk_initialized;
71 UniqueApp *unique_app_handle;
72 int exit_code;
73 gchar *exit_message1;
74 gchar *exit_message2;
77 /* instance properties
79 enum {
80 BASE_PROP_0,
82 BASE_PROP_ARGC_ID,
83 BASE_PROP_ARGV_ID,
84 BASE_PROP_OPTIONS_ID,
85 BASE_PROP_APPLICATION_NAME_ID,
86 BASE_PROP_ICON_NAME_ID,
87 BASE_PROP_UNIQUE_APP_NAME_ID,
88 BASE_APPLICATION_PROP_IS_GTK_INITIALIZED_ID,
89 BASE_APPLICATION_PROP_UNIQUE_APP_HANDLE_ID,
90 BASE_APPLICATION_PROP_EXIT_CODE_ID,
91 BASE_APPLICATION_PROP_EXIT_MESSAGE1_ID,
92 BASE_APPLICATION_PROP_EXIT_MESSAGE2_ID,
93 BASE_APPLICATION_PROP_BUILDER_ID,
94 BASE_APPLICATION_PROP_MAIN_WINDOW_ID,
96 BASE_PROP_N_PROPERTIES
99 static GObjectClass *st_parent_class = NULL;
101 static GType register_type( void );
102 static void class_init( BaseApplicationClass *klass );
103 static void instance_init( GTypeInstance *instance, gpointer klass );
104 static void instance_get_property( GObject *object, guint property_id, GValue *value, GParamSpec *spec );
105 static void instance_set_property( GObject *object, guint property_id, const GValue *value, GParamSpec *spec );
106 static void instance_dispose( GObject *application );
107 static void instance_finalize( GObject *application );
109 static gboolean appli_initialize_i18n( BaseApplication *application, int *code );
110 static gboolean appli_initialize_application_name( BaseApplication *application, int *code );
111 static gboolean appli_initialize_gtk( BaseApplication *application, int *code );
112 static gboolean appli_initialize_manage_options( const BaseApplication *application, int *code );
113 static gboolean appli_initialize_unique_app( BaseApplication *application, int *code );
114 #if 0
115 static UniqueResponse on_unique_message_received( UniqueApp *app, UniqueCommand command, UniqueMessageData *message, guint time, gpointer user_data );
116 #endif
117 static gboolean appli_initialize_session_manager( BaseApplication *application, int *code );
118 static void session_manager_client_quit_cb( EggSMClient *client, BaseApplication *application );
119 static void session_manager_client_quit_requested_cb( EggSMClient *client, BaseApplication *application );
120 static gboolean appli_initialize_application_icon( BaseApplication *application, int *code );
121 static gboolean appli_initialize_builder( BaseApplication *application, int *code );
123 static gint display_dlg( BaseApplication *application, GtkMessageType type_message, GtkButtonsType type_buttons, const gchar *first, const gchar *second );
124 #if 0
125 static void display_error_message( 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 #endif
130 GType
131 base_application_get_type( void )
133 static GType application_type = 0;
135 if( !application_type ){
136 application_type = register_type();
139 return( application_type );
142 static GType
143 register_type( void )
145 static const gchar *thisfn = "base_application_register_type";
146 GType type;
148 static GTypeInfo info = {
149 sizeof( BaseApplicationClass ),
150 ( GBaseInitFunc ) NULL,
151 ( GBaseFinalizeFunc ) NULL,
152 ( GClassInitFunc ) class_init,
153 NULL,
154 NULL,
155 sizeof( BaseApplication ),
157 ( GInstanceInitFunc ) instance_init
160 g_debug( "%s", thisfn );
162 g_type_init();
164 type = g_type_register_static( G_TYPE_OBJECT, "BaseApplication", &info, 0 );
166 return( type );
169 static void
170 class_init( BaseApplicationClass *klass )
172 static const gchar *thisfn = "base_application_class_init";
173 GObjectClass *object_class;
174 GParamSpec *spec;
176 g_debug( "%s: klass=%p", thisfn, ( void * ) klass );
178 st_parent_class = g_type_class_peek_parent( klass );
180 object_class = G_OBJECT_CLASS( klass );
181 object_class->dispose = instance_dispose;
182 object_class->finalize = instance_finalize;
183 object_class->get_property = instance_get_property;
184 object_class->set_property = instance_set_property;
186 g_object_class_install_property( object_class, BASE_PROP_ARGC_ID,
187 g_param_spec_int(
188 BASE_PROP_ARGC,
189 _( "Arguments count" ),
190 _( "The count of command-line arguments" ),
191 0, 65535, 0,
192 G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE ));
194 g_object_class_install_property( object_class, BASE_PROP_ARGV_ID,
195 g_param_spec_boxed(
196 BASE_PROP_ARGV,
197 _( "Arguments" ),
198 _( "The array of command-line arguments" ),
199 G_TYPE_STRV,
200 G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE ));
202 g_object_class_install_property( object_class, BASE_PROP_OPTIONS_ID,
203 g_param_spec_pointer(
204 BASE_PROP_OPTIONS,
205 _( "Option entries" ),
206 _( "The array of command-line option definitions" ),
207 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE ));
209 g_object_class_install_property( object_class, BASE_PROP_APPLICATION_NAME_ID,
210 g_param_spec_string(
211 BASE_PROP_APPLICATION_NAME,
212 _( "Application name" ),
213 _( "The name of the application" ),
215 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE ));
217 g_object_class_install_property( object_class, BASE_PROP_ICON_NAME_ID,
218 g_param_spec_string(
219 BASE_PROP_ICON_NAME,
220 _( "Icon name" ),
221 _( "The name of the icon of the application" ),
223 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE ));
225 g_object_class_install_property( object_class, BASE_PROP_UNIQUE_APP_NAME_ID,
226 g_param_spec_string(
227 BASE_PROP_UNIQUE_APP_NAME,
228 _( "UniqueApp name" ),
229 _( "The Unique name of the application" ),
231 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE ));
233 spec = g_param_spec_boolean(
234 BASE_APPLICATION_PROP_IS_GTK_INITIALIZED,
235 "Gtk+ initialization flag",
236 "Has Gtk+ be initialized ?", FALSE,
237 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE );
238 g_object_class_install_property( object_class, BASE_APPLICATION_PROP_IS_GTK_INITIALIZED_ID, spec );
240 spec = g_param_spec_pointer(
241 BASE_APPLICATION_PROP_UNIQUE_APP_HANDLE,
242 "UniqueApp object pointer",
243 "A reference to the UniqueApp object if any",
244 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE );
245 g_object_class_install_property( object_class, BASE_APPLICATION_PROP_UNIQUE_APP_HANDLE_ID, spec );
247 spec = g_param_spec_int(
248 BASE_APPLICATION_PROP_EXIT_CODE,
249 "Exit code",
250 "Exit code of the application", 0, 65535, 0,
251 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE );
252 g_object_class_install_property( object_class, BASE_APPLICATION_PROP_EXIT_CODE_ID, spec );
254 spec = g_param_spec_string(
255 BASE_APPLICATION_PROP_EXIT_MESSAGE1,
256 "Error message",
257 "First line of the error message displayed when exit_code not nul", "",
258 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE );
259 g_object_class_install_property( object_class, BASE_APPLICATION_PROP_EXIT_MESSAGE1_ID, spec );
261 spec = g_param_spec_string(
262 BASE_APPLICATION_PROP_EXIT_MESSAGE2,
263 "Error message",
264 "Second line of the error message displayed when exit_code not nul", "",
265 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE );
266 g_object_class_install_property( object_class, BASE_APPLICATION_PROP_EXIT_MESSAGE2_ID, spec );
268 spec = g_param_spec_pointer(
269 BASE_APPLICATION_PROP_BUILDER,
270 "UI object pointer",
271 "A reference to the UI definition from GtkBuilder",
272 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE );
273 g_object_class_install_property( object_class, BASE_APPLICATION_PROP_BUILDER_ID, spec );
275 spec = g_param_spec_pointer(
276 BASE_APPLICATION_PROP_MAIN_WINDOW,
277 "Main BaseWindow object",
278 "A reference to the main BaseWindow object",
279 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE );
280 g_object_class_install_property( object_class, BASE_APPLICATION_PROP_MAIN_WINDOW_ID, spec );
282 klass->private = g_new0( BaseApplicationClassPrivate, 1 );
284 klass->manage_options = NULL;
285 klass->main_window_new = NULL;
288 static void
289 instance_init( GTypeInstance *application, gpointer klass )
291 static const gchar *thisfn = "base_application_instance_init";
292 BaseApplication *self;
294 g_return_if_fail( BASE_IS_APPLICATION( application ));
296 g_debug( "%s: application=%p (%s), klass=%p",
297 thisfn, ( void * ) application, G_OBJECT_TYPE_NAME( application ), ( void * ) klass );
299 self = BASE_APPLICATION( application );
301 self->private = g_new0( BaseApplicationPrivate, 1 );
303 self->private->dispose_has_run = FALSE;
306 static void
307 instance_get_property( GObject *object, guint property_id, GValue *value, GParamSpec *spec )
309 BaseApplication *self;
311 g_return_if_fail( BASE_IS_APPLICATION( object ));
312 self = BASE_APPLICATION( object );
314 if( !self->private->dispose_has_run ){
316 switch( property_id ){
317 case BASE_PROP_ARGC_ID:
318 g_value_set_int( value, self->private->argc );
319 break;
321 case BASE_PROP_ARGV_ID:
322 g_value_set_boxed( value, self->private->argv );
323 break;
325 case BASE_PROP_OPTIONS_ID:
326 g_value_set_pointer( value, self->private->options );
327 break;
329 case BASE_PROP_APPLICATION_NAME_ID:
330 g_value_set_string( value, self->private->application_name );
331 break;
333 case BASE_PROP_ICON_NAME_ID:
334 g_value_set_string( value, self->private->icon_name );
335 break;
337 case BASE_PROP_UNIQUE_APP_NAME_ID:
338 g_value_set_string( value, self->private->unique_app_name );
339 break;
341 case BASE_APPLICATION_PROP_IS_GTK_INITIALIZED_ID:
342 g_value_set_boolean( value, self->private->is_gtk_initialized );
343 break;
345 case BASE_APPLICATION_PROP_UNIQUE_APP_HANDLE_ID:
346 g_value_set_pointer( value, self->private->unique_app_handle );
347 break;
349 case BASE_APPLICATION_PROP_EXIT_CODE_ID:
350 g_value_set_int( value, self->private->exit_code );
351 break;
353 case BASE_APPLICATION_PROP_EXIT_MESSAGE1_ID:
354 g_value_set_string( value, self->private->exit_message1 );
355 break;
357 case BASE_APPLICATION_PROP_EXIT_MESSAGE2_ID:
358 g_value_set_string( value, self->private->exit_message2 );
359 break;
361 case BASE_APPLICATION_PROP_BUILDER_ID:
362 g_value_set_pointer( value, self->private->builder );
363 break;
365 case BASE_APPLICATION_PROP_MAIN_WINDOW_ID:
366 g_value_set_pointer( value, self->private->main_window );
367 break;
369 default:
370 G_OBJECT_WARN_INVALID_PROPERTY_ID( object, property_id, spec );
371 break;
376 static void
377 instance_set_property( GObject *object, guint property_id, const GValue *value, GParamSpec *spec )
379 BaseApplication *self;
381 g_return_if_fail( BASE_IS_APPLICATION( object ));
382 self = BASE_APPLICATION( object );
384 if( !self->private->dispose_has_run ){
386 switch( property_id ){
387 case BASE_PROP_ARGC_ID:
388 self->private->argc = g_value_get_int( value );
389 break;
391 case BASE_PROP_ARGV_ID:
392 if( self->private->argv ){
393 g_boxed_free( G_TYPE_STRV, self->private->argv );
395 self->private->argv = g_value_dup_boxed( value );
396 break;
398 case BASE_PROP_OPTIONS_ID:
399 self->private->options = g_value_get_pointer( value );
400 break;
402 case BASE_PROP_APPLICATION_NAME_ID:
403 g_free( self->private->application_name );
404 self->private->application_name = g_value_dup_string( value );
405 break;
407 case BASE_PROP_ICON_NAME_ID:
408 g_free( self->private->icon_name );
409 self->private->icon_name = g_value_dup_string( value );
410 break;
412 case BASE_PROP_UNIQUE_APP_NAME_ID:
413 g_free( self->private->unique_app_name );
414 self->private->unique_app_name = g_value_dup_string( value );
415 break;
417 case BASE_APPLICATION_PROP_IS_GTK_INITIALIZED_ID:
418 self->private->is_gtk_initialized = g_value_get_boolean( value );
419 break;
421 case BASE_APPLICATION_PROP_UNIQUE_APP_HANDLE_ID:
422 self->private->unique_app_handle = g_value_get_pointer( value );
423 break;
425 case BASE_APPLICATION_PROP_EXIT_CODE_ID:
426 self->private->exit_code = g_value_get_int( value );
427 break;
429 case BASE_APPLICATION_PROP_EXIT_MESSAGE1_ID:
430 g_free( self->private->exit_message1 );
431 self->private->exit_message1 = g_value_dup_string( value );
432 break;
434 case BASE_APPLICATION_PROP_EXIT_MESSAGE2_ID:
435 g_free( self->private->exit_message2 );
436 self->private->exit_message2 = g_value_dup_string( value );
437 break;
439 case BASE_APPLICATION_PROP_BUILDER_ID:
440 self->private->builder = g_value_get_pointer( value );
441 break;
443 case BASE_APPLICATION_PROP_MAIN_WINDOW_ID:
444 self->private->main_window = g_value_get_pointer( value );
445 break;
447 default:
448 G_OBJECT_WARN_INVALID_PROPERTY_ID( object, property_id, spec );
449 break;
454 static void
455 instance_dispose( GObject *application )
457 static const gchar *thisfn = "base_application_instance_dispose";
458 BaseApplication *self;
460 g_return_if_fail( BASE_IS_APPLICATION( application ));
462 self = BASE_APPLICATION( application );
464 if( !self->private->dispose_has_run ){
466 g_debug( "%s: application=%p (%s)", thisfn, ( void * ) application, G_OBJECT_TYPE_NAME( application ));
468 self->private->dispose_has_run = TRUE;
470 if( UNIQUE_IS_APP( self->private->unique_app_handle )){
471 g_object_unref( self->private->unique_app_handle );
474 if( GTK_IS_BUILDER( self->private->builder )){
475 g_object_unref( self->private->builder );
478 if( self->private->sm_client ){
479 g_object_unref( self->private->sm_client );
482 /* chain up to the parent class */
483 if( G_OBJECT_CLASS( st_parent_class )->dispose ){
484 G_OBJECT_CLASS( st_parent_class )->dispose( application );
489 static void
490 instance_finalize( GObject *application )
492 static const gchar *thisfn = "base_application_instance_finalize";
493 BaseApplication *self;
495 g_return_if_fail( BASE_IS_APPLICATION( application ));
497 g_debug( "%s: application=%p (%s)", thisfn, ( void * ) application, G_OBJECT_TYPE_NAME( application ));
499 self = BASE_APPLICATION( application );
501 g_free( self->private->application_name );
502 g_free( self->private->icon_name );
503 g_free( self->private->unique_app_name );
505 g_free( self->private->exit_message1 );
506 g_free( self->private->exit_message2 );
508 g_free( self->private );
510 /* chain call to parent class */
511 if( G_OBJECT_CLASS( st_parent_class )->finalize ){
512 G_OBJECT_CLASS( st_parent_class )->finalize( application );
517 * base_application_run:
518 * @application: this #BaseApplication -derived instance.
520 * Starts and runs the application.
521 * Takes care of creating, initializing, and running the main window.
523 * All steps are implemented as virtual functions which provide some
524 * suitable defaults, and may be overriden by a derived class.
526 * Returns: an %int code suitable as an exit code for the program.
528 * Though it is defined as a virtual function itself, it should be very
529 * seldomly needed to override this in a derived class.
532 base_application_run( BaseApplication *application )
534 static const gchar *thisfn = "base_application_run";
535 int code;
536 GtkWindow *gtk_toplevel;
538 g_return_val_if_fail( BASE_IS_APPLICATION( application ), BASE_EXIT_CODE_START_FAIL );
540 code = BASE_EXIT_CODE_START_FAIL;
542 if( !application->private->dispose_has_run ){
543 g_debug( "%s: application=%p", thisfn, ( void * ) application );
545 code = BASE_EXIT_CODE_OK;
546 application->private->main_window = NULL;
548 if( appli_initialize_i18n( application, &code ) &&
549 appli_initialize_application_name( application, &code ) &&
550 appli_initialize_gtk( application, &code ) &&
551 appli_initialize_manage_options( application, &code ) &&
552 appli_initialize_unique_app( application, &code ) &&
553 appli_initialize_session_manager( application, &code ) &&
554 appli_initialize_application_icon( application, &code ) &&
555 appli_initialize_builder( application, &code )){
558 if( BASE_APPLICATION_GET_CLASS( application )->main_window_new ){
559 application->private->main_window = ( BaseWindow * ) BASE_APPLICATION_GET_CLASS( application )->main_window_new( application, &application->private->exit_code );
560 } else {
561 code = BASE_EXIT_CODE_MAIN_WINDOW;
564 if( application->private->main_window ){
565 g_return_val_if_fail( BASE_IS_WINDOW( application->private->main_window ), BASE_EXIT_CODE_START_FAIL );
567 if( base_window_init( application->private->main_window )){
568 gtk_toplevel = base_window_get_toplevel( application->private->main_window );
569 g_return_val_if_fail( gtk_toplevel, BASE_EXIT_CODE_START_FAIL );
570 g_return_val_if_fail( GTK_IS_WINDOW( gtk_toplevel ), BASE_EXIT_CODE_START_FAIL );
572 if( application->private->unique_app_handle ){
573 unique_app_watch_window( application->private->unique_app_handle, gtk_toplevel );
576 code = base_window_run( application->private->main_window );
582 return( code );
585 static gboolean
586 appli_initialize_i18n( BaseApplication *application, int *code )
588 static const gchar *thisfn = "base_application_appli_initialize_i18n";
590 g_debug( "%s: application=%p, code=%p (%d)", thisfn, ( void * ) application, ( void * ) code, *code );
592 #ifdef ENABLE_NLS
593 bindtextdomain( GETTEXT_PACKAGE, GNOMELOCALEDIR );
594 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
595 bind_textdomain_codeset( GETTEXT_PACKAGE, "UTF-8" );
596 # endif
597 textdomain( GETTEXT_PACKAGE );
598 #endif
600 gtk_set_locale();
602 return( TRUE );
605 static gboolean
606 appli_initialize_application_name( BaseApplication *application, int *code )
608 static const gchar *thisfn = "base_application_appli_initialize_application_name";
609 gchar *name;
611 g_debug( "%s: application=%p, code=%p (%d)", thisfn, ( void * ) application, ( void * ) code, *code );
613 name = base_application_get_application_name( application );
614 if( name && g_utf8_strlen( name, -1 )){
615 g_set_application_name( name );
617 g_free( name );
619 return( TRUE );
622 static gboolean
623 appli_initialize_gtk( BaseApplication *application, int *code )
625 static const gchar *thisfn = "base_application_appli_initialize_gtk";
626 gboolean ret;
627 char *parameter_string;
628 GError *error;
630 g_debug( "%s: application=%p, code=%p (%d)", thisfn, ( void * ) application, ( void * ) code, *code );
632 if( application->private->options ){
633 parameter_string = g_strdup( g_get_application_name());
634 error = NULL;
635 ret = gtk_init_with_args(
636 &application->private->argc, ( char *** ) &application->private->argv,
637 parameter_string, application->private->options, GETTEXT_PACKAGE, &error );
639 if( error ){
640 g_warning( "%s: %s", thisfn, error->message );
641 g_error_free( error );
642 ret = FALSE;
645 g_free( parameter_string );
647 } else {
648 ret = gtk_init_check( &application->private->argc, ( char *** ) &application->private->argv );
651 return( ret );
654 static gboolean
655 appli_initialize_manage_options( const BaseApplication *application, int *code )
657 static const gchar *thisfn = "base_application_appli_initialize_manage_options";
658 gboolean ret;
660 g_debug( "%s: application=%p, code=%p (%d)", thisfn, ( void * ) application, ( void * ) code, *code );
662 ret = TRUE;
664 if( BASE_APPLICATION_GET_CLASS( application )->manage_options ){
665 ret = BASE_APPLICATION_GET_CLASS( application )->manage_options( application, code );
668 return( ret );
671 static gboolean
672 appli_initialize_unique_app( BaseApplication *application, int *code )
674 static const gchar *thisfn = "base_application_appli_initialize_unique_app";
675 gboolean ret;
676 gboolean is_first;
677 gchar *msg;
679 g_debug( "%s: application=%p, code=%p (%d)", thisfn, ( void * ) application, ( void * ) code, *code );
681 ret = TRUE;
683 if( application->private->unique_app_name &&
684 strlen( application->private->unique_app_name )){
686 application->private->unique_app_handle = unique_app_new( application->private->unique_app_name, NULL );
687 is_first = unique_app_is_running( application->private->unique_app_handle );
689 if( !is_first ){
690 unique_app_send_message( application->private->unique_app_handle, UNIQUE_ACTIVATE, NULL );
691 /* i18n: application name */
692 msg = g_strdup_printf(
693 _( "Another instance of %s is already running.\n"
694 "Please switch back to it." ), application->private->application_name );
695 base_window_display_error_dlg( NULL, _( "The application is not unique" ), msg );
696 g_free( msg );
697 ret = FALSE;
698 *code = BASE_EXIT_CODE_UNIQUE_APP;
699 #if 0
700 /* default from libunique is actually to activate the first window
701 * so we rely on the default..
703 } else {
704 g_signal_connect( application->private->unique_app_handle,
705 "message-received", G_CALLBACK( on_unique_message_received ), application );
706 #endif
710 return( ret );
713 #if 0
714 static UniqueResponse
715 on_unique_message_received(
716 UniqueApp *app, UniqueCommand command, UniqueMessageData *message, guint time, gpointer user_data )
718 static const gchar *thisfn = "base_application_check_for_unique_app";
719 UniqueResponse resp = UNIQUE_RESPONSE_OK;
721 switch( command ){
722 case UNIQUE_ACTIVATE:
723 g_debug( "%s: received message UNIQUE_ACTIVATE", thisfn );
724 break;
725 default:
726 resp = UNIQUE_RESPONSE_PASSTHROUGH;
727 break;
730 return( resp );
732 #endif
734 static gboolean
735 appli_initialize_session_manager( BaseApplication *application, int *code )
737 static const gchar *thisfn = "base_application_do_initialize_session_manager";
739 g_debug( "%s: application=%p, code=%p (%d)", thisfn, ( void * ) application, ( void * ) code, *code );
741 egg_sm_client_set_mode( EGG_SM_CLIENT_MODE_NO_RESTART );
742 application->private->sm_client = egg_sm_client_get();
743 egg_sm_client_startup();
744 g_debug( "%s: sm_client=%p", thisfn, ( void * ) application->private->sm_client );
746 g_signal_connect( application->private->sm_client,
747 "quit-requested", G_CALLBACK( session_manager_client_quit_requested_cb ), application );
749 g_signal_connect( application->private->sm_client,
750 "quit", G_CALLBACK( session_manager_client_quit_cb ), application );
752 return( TRUE );
755 static void
756 session_manager_client_quit_cb( EggSMClient *client, BaseApplication *application )
758 static const gchar *thisfn = "base_application_session_manager_client_quit_cb";
760 g_return_if_fail( BASE_IS_APPLICATION( application ));
762 g_debug( "%s: client=%p, application=%p", thisfn, ( void * ) client, ( void * ) application );
764 if( application->private->main_window &&
765 BASE_IS_WINDOW( application->private->main_window )){
767 g_object_unref( application->private->main_window );
768 application->private->main_window = NULL;
772 static void
773 session_manager_client_quit_requested_cb( EggSMClient *client, BaseApplication *application )
775 static const gchar *thisfn = "base_application_session_manager_client_quit_requested_cb";
776 gboolean willing_to = TRUE;
778 g_return_if_fail( BASE_IS_APPLICATION( application ));
780 g_debug( "%s: client=%p, application=%p", thisfn, ( void * ) client, ( void * ) application );
782 if( application->private->main_window &&
783 BASE_IS_WINDOW( application->private->main_window )){
785 willing_to = base_window_is_willing_to_quit( application->private->main_window );
788 egg_sm_client_will_quit( client, willing_to );
791 static gboolean
792 appli_initialize_application_icon( BaseApplication *application, int *code )
794 static const gchar *thisfn = "base_application_appli_initialize_application_icon";
796 g_debug( "%s: application=%p, code=%p (%d)", thisfn, ( void * ) application, ( void * ) code, *code );
798 if( application->private->icon_name &&
799 g_utf8_strlen( application->private->icon_name, -1 )){
801 gtk_window_set_default_icon_name( application->private->icon_name );
804 return( TRUE );
807 static gboolean
808 appli_initialize_builder( BaseApplication *application, int *code )
810 static const gchar *thisfn = "base_application_appli_initialize_builder";
812 g_debug( "%s: application=%p, code=%p (%d)", thisfn, ( void * ) application, ( void * ) code, *code );
814 application->private->builder = base_builder_new();
816 return( TRUE );
820 * base_application_get_application_name:
821 * @application: this #BaseApplication instance.
823 * Returns: the application name as a newly allocated string which should
824 * be be g_free() by the caller.
826 gchar *
827 base_application_get_application_name( const BaseApplication *application )
829 gchar *name = NULL;
831 g_return_val_if_fail( BASE_IS_APPLICATION( application ), NULL );
833 if( !application->private->dispose_has_run ){
835 name = g_strdup( application->private->application_name );
838 return( name );
842 * base_application_get_builder:
843 * @application: this #BaseApplication instance.
845 * Returns: the default #BaseBuilder object for the application.
847 BaseBuilder *
848 base_application_get_builder( BaseApplication *application )
850 /*static const gchar *thisfn = "base_application_get_application_name";
851 g_debug( "%s: application=%p", thisfn, application );*/
852 BaseBuilder *builder = NULL;
854 g_return_val_if_fail( BASE_IS_APPLICATION( application ), NULL );
856 if( !application->private->dispose_has_run ){
857 builder = application->private->builder;
860 return( builder );
864 * base_application_get_ui_filename:
865 * @application: this #BaseApplication instance.
867 * Asks the #BaseApplication-derived class for the filename of the file
868 * which contains the XML definition of the user interface.
870 * Defaults to empty.
872 * Returns: a newly allocated string to be g_free() by the caller.
874 gchar *
875 base_application_get_ui_filename( BaseApplication *application )
877 /*static const gchar *thisfn = "base_application_get_ui_filename";
878 g_debug( "%s: icon=%p", thisfn, application );*/
879 gchar *name = NULL;
881 g_return_val_if_fail( BASE_IS_APPLICATION( application ), NULL );
883 if( !application->private->dispose_has_run ){
884 if( BASE_APPLICATION_GET_CLASS( application )->get_ui_filename ){
885 name = BASE_APPLICATION_GET_CLASS( application )->get_ui_filename( application );
887 } else {
888 name = g_strdup( "" );
892 return( name );
896 * base_application_message_dlg:
897 * @application: this #BaseApplication instance.
898 * @message: the message to be displayed.
900 * Displays a dialog with only an OK button.
902 void
903 base_application_message_dlg( BaseApplication *application, GSList *msg )
905 GString *string;
906 GSList *im;
908 if( !application->private->dispose_has_run ){
910 string = g_string_new( "" );
911 for( im = msg ; im ; im = im->next ){
912 if( g_utf8_strlen( string->str, -1 )){
913 string = g_string_append( string, "\n" );
915 string = g_string_append( string, ( gchar * ) im->data );
917 display_dlg( application, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, string->str, NULL );
919 g_string_free( string, TRUE );
924 * base_application_error_dlg:
925 * @application: this #BaseApplication instance.
926 * @type:
927 * @primary: a first message.
928 * @secondaru: a second message.
930 * Displays an error dialog with only an OK button.
932 void
933 base_application_error_dlg( BaseApplication *application,
934 GtkMessageType type,
935 const gchar *first,
936 const gchar *second )
938 if( !application->private->dispose_has_run ){
939 display_dlg( application, type, GTK_BUTTONS_OK, first, second );
944 * base_application_yesno_dlg:
945 * @application: this #BaseApplication instance.
946 * @type:
947 * @primary: a first message.
948 * @secondaru: a second message.
950 * Displays a choice dialog, with Yes and No buttons.
951 * No button is the default.
953 * Returns: %TRUE if user has clicked on Yes button, %FALSE else.
955 gboolean
956 base_application_yesno_dlg( BaseApplication *application, GtkMessageType type, const gchar *first, const gchar *second )
958 gboolean ret = FALSE;
959 gint result;
961 if( !application->private->dispose_has_run ){
963 result = display_dlg( application, type, GTK_BUTTONS_YES_NO, first, second );
964 ret = ( result == GTK_RESPONSE_YES );
967 return( ret );
970 #if 0
971 static gboolean
972 application_do_initialize_ui( BaseApplication *application )
974 static const gchar *thisfn = "base_application_do_initialize_ui";
975 gboolean ret = TRUE;
976 GError *error = NULL;
977 gchar *name;
979 g_debug( "%s: application=%p", thisfn, ( void * ) application );
981 name = base_application_get_ui_filename( application );
983 if( !name || !strlen( name )){
984 ret = FALSE;
985 set_initialize_ui_get_fname_error( application );
987 } else {
988 application->private->builder = base_builder_new();
989 if( !base_builder_add_from_file( application->private->builder, name, &error )){
990 ret = FALSE;
991 set_initialize_ui_add_xml_error( application, name, error );
992 if( error ){
993 g_error_free( error );
998 g_free( name );
999 return( ret );
1001 #endif
1003 static gint
1004 display_dlg( BaseApplication *application, GtkMessageType type_message, GtkButtonsType type_buttons, const gchar *first, const gchar *second )
1006 GtkWidget *dialog;
1007 const gchar *name;
1008 gint result;
1009 GtkWindow *parent;
1011 g_assert( BASE_IS_APPLICATION( application ));
1013 parent = NULL;
1014 if( application->private->main_window ){
1015 parent = base_window_get_toplevel( application->private->main_window );
1018 dialog = gtk_message_dialog_new( parent, GTK_DIALOG_MODAL, type_message, type_buttons, "%s", first );
1020 if( second && g_utf8_strlen( second, -1 )){
1021 gtk_message_dialog_format_secondary_text( GTK_MESSAGE_DIALOG( dialog ), "%s", second );
1024 name = g_get_application_name();
1026 g_object_set( G_OBJECT( dialog ) , "title", name, NULL );
1028 result = gtk_dialog_run( GTK_DIALOG( dialog ));
1030 gtk_widget_destroy( dialog );
1032 return( result );
1035 #if 0
1036 static void
1037 display_error_message( BaseApplication *application )
1039 if( application->private->exit_message1 && g_utf8_strlen( application->private->exit_message1, -1 )){
1041 if( application->private->is_gtk_initialized ){
1042 base_application_error_dlg(
1043 application,
1044 GTK_MESSAGE_INFO,
1045 application->private->exit_message1,
1046 application->private->exit_message2 );
1048 } else {
1049 g_printf( "%s\n", application->private->exit_message1 );
1050 if( application->private->exit_message2 && g_utf8_strlen( application->private->exit_message2, -1 )){
1051 g_printf( "%s\n", application->private->exit_message2 );
1057 static void
1058 set_initialize_ui_get_fname_error( BaseApplication *application )
1060 application->private->exit_code = BASE_APPLICATION_ERROR_UI_FNAME;
1062 application->private->exit_message1 =
1063 g_strdup( _( "No filename provided for the UI XML definition." ));
1066 static void
1067 set_initialize_ui_add_xml_error( BaseApplication *application, const gchar *filename, GError *error )
1069 application->private->exit_code = BASE_APPLICATION_ERROR_UI_LOAD;
1071 application->private->exit_message1 =
1072 /* i18n: Unable to load the XML definition from <filename> */
1073 g_strdup_printf( _( "Unable to load the XML definition from %s." ), filename );
1075 if( error && error->message ){
1076 application->private->exit_message2 = g_strdup( error->message );
1079 #endif