Set Nautilus-Actions as being the actual official product name
[nautilus-actions.git] / src / nact / nact-application.c
blob9e62ac09c58140d986a5e63dd20c7b733e57f758
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 <gtk/gtk.h>
38 #include <api/na-core-utils.h>
40 #include <core/na-iabout.h>
41 #include <core/na-ipivot-consumer.h>
43 #include "nact-application.h"
44 #include "nact-main-window.h"
46 /* private class data
48 struct NactApplicationClassPrivate {
49 void *empty; /* so that gcc -pedantic is happy */
52 /* private instance data
54 struct NactApplicationPrivate {
55 gboolean dispose_has_run;
56 NAUpdater *updater;
59 /* private instance properties
61 enum {
62 NACT_APPLICATION_PROP_UPDATER_ID = 1
65 #define NACT_APPLICATION_PROP_UPDATER "nact-application-prop-updater"
67 static gboolean st_non_unique_opt = FALSE;
68 static gboolean st_version_opt = FALSE;
70 static GOptionEntry st_entries[] = {
71 { "non-unique", 'n', 0, G_OPTION_ARG_NONE, &st_non_unique_opt,
72 N_( "Set it to run multiple instances of the program [unique]" ), NULL },
73 { "version" , 'v', 0, G_OPTION_ARG_NONE, &st_version_opt,
74 N_( "Output the version number, and exit gracefully [no]" ), NULL },
75 { NULL }
78 static BaseApplicationClass *st_parent_class = NULL;
80 static GType register_type( void );
81 static void class_init( NactApplicationClass *klass );
82 static void instance_init( GTypeInstance *instance, gpointer klass );
83 static void instance_get_property( GObject *object, guint property_id, GValue *value, GParamSpec *spec );
84 static void instance_set_property( GObject *object, guint property_id, const GValue *value, GParamSpec *spec );
85 static void instance_dispose( GObject *application );
86 static void instance_finalize( GObject *application );
88 static gboolean appli_manage_options( BaseApplication *application );
89 static gboolean appli_initialize_unique_app( BaseApplication *application );
90 static gboolean appli_initialize_application( BaseApplication *application );
91 static gchar *appli_get_application_name( BaseApplication *application );
92 static gchar *appli_get_icon_name( BaseApplication *application );
93 static gchar *appli_get_unique_app_name( BaseApplication *application );
94 static gchar *appli_get_gtkbuilder_filename( BaseApplication *application );
95 static GObject *appli_get_main_window( BaseApplication *application );
97 GType
98 nact_application_get_type( void )
100 static GType application_type = 0;
102 if( !application_type ){
103 application_type = register_type();
106 return( application_type );
109 static GType
110 register_type( void )
112 static const gchar *thisfn = "nact_application_register_type";
113 GType type;
115 static GTypeInfo info = {
116 sizeof( NactApplicationClass ),
117 ( GBaseInitFunc ) NULL,
118 ( GBaseFinalizeFunc ) NULL,
119 ( GClassInitFunc ) class_init,
120 NULL,
121 NULL,
122 sizeof( NactApplication ),
124 ( GInstanceInitFunc ) instance_init
127 g_debug( "%s", thisfn );
129 type = g_type_register_static( BASE_APPLICATION_TYPE, "NactApplication", &info, 0 );
131 return( type );
134 static void
135 class_init( NactApplicationClass *klass )
137 static const gchar *thisfn = "nact_application_class_init";
138 GObjectClass *object_class;
139 GParamSpec *spec;
140 BaseApplicationClass *appli_class;
142 g_debug( "%s: klass=%p", thisfn, ( void * ) klass );
144 st_parent_class = BASE_APPLICATION_CLASS( g_type_class_peek_parent( klass ));
146 object_class = G_OBJECT_CLASS( klass );
147 object_class->dispose = instance_dispose;
148 object_class->finalize = instance_finalize;
149 object_class->get_property = instance_get_property;
150 object_class->set_property = instance_set_property;
152 spec = g_param_spec_pointer(
153 NACT_APPLICATION_PROP_UPDATER,
154 NACT_APPLICATION_PROP_UPDATER,
155 "NAUpdater object pointer",
156 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE );
157 g_object_class_install_property( object_class, NACT_APPLICATION_PROP_UPDATER_ID, spec );
159 klass->private = g_new0( NactApplicationClassPrivate, 1 );
161 appli_class = BASE_APPLICATION_CLASS( klass );
162 appli_class->manage_options = appli_manage_options;
163 appli_class->initialize_unique_app = appli_initialize_unique_app;
164 appli_class->initialize_application = appli_initialize_application;
165 appli_class->get_application_name = appli_get_application_name;
166 appli_class->get_icon_name = appli_get_icon_name;
167 appli_class->get_unique_app_name = appli_get_unique_app_name;
168 appli_class->get_ui_filename = appli_get_gtkbuilder_filename;
169 appli_class->get_main_window = appli_get_main_window;
172 static void
173 instance_init( GTypeInstance *application, gpointer klass )
175 static const gchar *thisfn = "nact_application_instance_init";
176 NactApplication *self;
178 g_debug( "%s: application=%p (%s), klass=%p",
179 thisfn, ( void * ) application, G_OBJECT_TYPE_NAME( application ), ( void * ) klass );
180 g_assert( NACT_IS_APPLICATION( application ));
181 self = NACT_APPLICATION( application );
183 self->private = g_new0( NactApplicationPrivate, 1 );
185 self->private->dispose_has_run = FALSE;
188 static void
189 instance_get_property( GObject *object, guint property_id, GValue *value, GParamSpec *spec )
191 NactApplication *self;
193 g_assert( NACT_IS_APPLICATION( object ));
194 self = NACT_APPLICATION( object );
196 if( !self->private->dispose_has_run ){
198 switch( property_id ){
199 case NACT_APPLICATION_PROP_UPDATER_ID:
200 g_value_set_pointer( value, self->private->updater );
201 break;
203 default:
204 G_OBJECT_WARN_INVALID_PROPERTY_ID( object, property_id, spec );
205 break;
210 static void
211 instance_set_property( GObject *object, guint property_id, const GValue *value, GParamSpec *spec )
213 NactApplication *self;
215 g_assert( NACT_IS_APPLICATION( object ));
216 self = NACT_APPLICATION( object );
218 if( !self->private->dispose_has_run ){
220 switch( property_id ){
221 case NACT_APPLICATION_PROP_UPDATER_ID:
222 self->private->updater = g_value_get_pointer( value );
223 break;
225 default:
226 G_OBJECT_WARN_INVALID_PROPERTY_ID( object, property_id, spec );
227 break;
232 static void
233 instance_dispose( GObject *application )
235 static const gchar *thisfn = "nact_application_instance_dispose";
236 NactApplication *self;
238 g_debug( "%s: application=%p (%s)", thisfn, ( void * ) application, G_OBJECT_TYPE_NAME( application ));
239 g_return_if_fail( NACT_IS_APPLICATION( application ));
240 self = NACT_APPLICATION( application );
242 if( !self->private->dispose_has_run ){
244 self->private->dispose_has_run = TRUE;
246 if( self->private->updater ){
247 g_object_unref( self->private->updater );
250 /* chain up to the parent class */
251 if( G_OBJECT_CLASS( st_parent_class )->dispose ){
252 G_OBJECT_CLASS( st_parent_class )->dispose( application );
257 static void
258 instance_finalize( GObject *application )
260 static const gchar *thisfn = "nact_application_instance_finalize";
261 NactApplication *self;
263 g_debug( "%s: application=%p", thisfn, ( void * ) application );
264 g_return_if_fail( NACT_IS_APPLICATION( application ));
265 self = NACT_APPLICATION( application );
267 g_free( self->private );
269 /* chain call to parent class */
270 if( G_OBJECT_CLASS( st_parent_class )->finalize ){
271 G_OBJECT_CLASS( st_parent_class )->finalize( application );
276 * Returns a newly allocated NactApplication object.
278 * @argc: count of command-line arguments.
280 * @argv: command-line arguments.
282 NactApplication *
283 nact_application_new_with_args( int argc, char **argv )
285 return(
286 g_object_new(
287 NACT_APPLICATION_TYPE,
288 BASE_APPLICATION_PROP_ARGC, argc,
289 BASE_APPLICATION_PROP_ARGV, argv,
290 BASE_APPLICATION_PROP_OPTIONS, st_entries,
291 NULL )
296 * nact_application_get_updater:
297 * @application: this NactApplication object.
299 * Returns a pointer on the #NAUpdater object.
301 * The returned pointer is owned by the #NactApplication object.
302 * It should not be g_free() not g_object_unref() by the caller.
304 NAUpdater *
305 nact_application_get_updater( NactApplication *application )
307 NAUpdater *updater = NULL;
309 g_return_val_if_fail( NACT_IS_APPLICATION( application ), NULL );
311 if( !application->private->dispose_has_run ){
313 updater = application->private->updater;
316 return( updater );
320 * overriden to manage command-line options
322 static gboolean
323 appli_manage_options( BaseApplication *application )
325 gboolean ok;
327 /* call parent class */
328 ok = BASE_APPLICATION_CLASS( st_parent_class )->manage_options( application );
330 if( ok ){
331 if( st_version_opt ){
332 na_core_utils_print_version();
333 ok = FALSE;
337 return( ok );
341 * overrided to provide a personalized error message
343 static gboolean
344 appli_initialize_unique_app( BaseApplication *application )
346 gboolean ok;
347 gchar *msg1, *msg2;
349 /* call parent class */
350 ok = BASE_APPLICATION_CLASS( st_parent_class )->initialize_unique_app( application );
352 if( !ok ){
353 msg1 = g_strdup( _( "Another instance of Nautilus-Actions Configuration Tool is already running." ));
354 /* i18n: another instance is already running: second line of error message */
355 msg2 = g_strdup( _( "Please switch back to it." ));
357 g_object_set( G_OBJECT( application ),
358 BASE_APPLICATION_PROP_EXIT_MESSAGE1, msg1,
359 BASE_APPLICATION_PROP_EXIT_MESSAGE2, msg2,
360 NULL );
362 g_free( msg2 );
363 g_free( msg1 );
366 return( ok );
370 * Overrided to complete the initialization of the application:
371 * - allocate the #NApivot here, so that it will be available when the
372 * #NactMainWindow will require it
373 * - do not register #NactApplication as a NAIPivotConsumer as this is
374 * essentially the NactMainwindow which will receive and deal with
375 * NAPivot notification messages
377 * At last, let the base class do its work, i.e. creating the main window.
379 * When the pivot will be empty, NAIDuplicable signals must yet be
380 * recorded in the system. Done here because :
381 * - I don't want do this in NAPivot which is also used by the plugin,
382 * - this is the last place where I'm pretty sure NAObject has not yet
383 * been registered.
384 * So we allocate a new NAObject-derived object to be sure the interface
385 * is correctly initialized.
387 static gboolean
388 appli_initialize_application( BaseApplication *application )
390 static const gchar *thisfn = "nact_application_appli_initialize_application";
391 gboolean ok;
393 g_debug( "%s: application=%p", thisfn, ( void * ) application );
395 NACT_APPLICATION( application )->private->updater = na_updater_new();
396 na_pivot_set_loadable( NA_PIVOT( NACT_APPLICATION( application )->private->updater ), PIVOT_LOAD_ALL );
397 na_pivot_load_items( NA_PIVOT( NACT_APPLICATION( application )->private->updater ));
399 /* call parent class */
400 ok = BASE_APPLICATION_CLASS( st_parent_class )->initialize_application( application );
402 return( ok );
405 static gchar *
406 appli_get_application_name( BaseApplication *application )
408 /*static const gchar *thisfn = "nact_application_appli_get_application_name";*/
410 /*g_debug( "%s: application=%p", thisfn, ( void * ) application );*/
412 /* i18n: this is the application name, used in window title */
413 return( g_strdup( _( "Nautilus-Actions Configuration Tool" )));
416 static gchar *
417 appli_get_icon_name( BaseApplication *application )
419 static const gchar *thisfn = "nact_application_appli_get_icon_name";
421 g_debug( "%s: application=%p", thisfn, ( void * ) application );
423 return( na_iabout_get_icon_name());
426 static gchar *
427 appli_get_unique_app_name( BaseApplication *application )
429 static const gchar *thisfn = "nact_application_appli_get_unique_app_name";
431 g_debug( "%s: application=%p", thisfn, ( void * ) application );
433 if( st_non_unique_opt ){
434 return( g_strdup( "" ));
437 return( g_strdup( "org.nautilus-actions.ConfigurationTool" ));
440 static gchar *
441 appli_get_gtkbuilder_filename( BaseApplication *application )
443 return( g_strdup( PKGDATADIR "/nautilus-actions-config-tool.ui" ));
447 * this should be called only once as base class is supposed to keep
448 * the pointer to the main BaseWindow window as a property
450 static GObject *
451 appli_get_main_window( BaseApplication *application )
453 static const gchar *thisfn = "nact_application_appli_get_main_window";
454 BaseWindow *window;
456 g_debug( "%s: application=%p", thisfn, ( void * ) application );
458 window = BASE_WINDOW( nact_main_window_new( application ));
460 na_pivot_register_consumer(
461 NA_PIVOT( nact_application_get_updater( NACT_APPLICATION( application ))),
462 NA_IPIVOT_CONSUMER( window ));
464 return( G_OBJECT( window ));