Set Nautilus-Actions as being the actual official product name
[nautilus-actions.git] / src / core / na-importer-ask.c
blob21c57acf183134982a38905f3c3ea7f258278f68
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 <gconf/gconf-client.h>
36 #include <glib/gi18n.h>
37 #include <gtk/gtk.h>
39 #include <api/na-gconf-utils.h>
40 #include <api/na-iimporter.h>
41 #include <api/na-object-api.h>
43 #include "na-gtk-utils.h"
44 #include "na-iprefs.h"
45 #include "na-importer-ask.h"
47 /* private class data
49 struct _NAImporterAskClassPrivate {
50 void *empty; /* so that gcc -pedantic is happy */
53 /* private instance data
55 struct _NAImporterAskPrivate {
56 gboolean dispose_has_run;
57 GtkBuilder *builder;
58 GtkWindow *toplevel;
59 NAObjectItem *importing;
60 NAObjectItem *existing;
61 NAImporterAskUserParms *parms;
62 guint mode;
63 GConfClient *gconf;
64 gint dialog_code;
67 static GtkDialogClass *st_parent_class = NULL;
69 static GType register_type( void );
70 static void class_init( NAImporterAskClass *klass );
71 static void instance_init( GTypeInstance *instance, gpointer klass );
72 static void instance_dispose( GObject *dialog );
73 static void instance_finalize( GObject *dialog );
75 static NAImporterAsk *import_ask_new();
77 static void init_dialog( NAImporterAsk *editor );
78 static void on_cancel_clicked( GtkButton *button, NAImporterAsk *editor );
79 static void on_ok_clicked( GtkButton *button, NAImporterAsk *editor );
80 static void get_selected_mode( NAImporterAsk *editor );
81 static gboolean on_dialog_response( NAImporterAsk *editor, gint code );
83 GType
84 na_importer_ask_get_type( void )
86 static GType dialog_type = 0;
88 if( !dialog_type ){
89 dialog_type = register_type();
92 return( dialog_type );
95 static GType
96 register_type( void )
98 static const gchar *thisfn = "na_importer_ask_register_type";
99 GType type;
101 static GTypeInfo info = {
102 sizeof( NAImporterAskClass ),
103 ( GBaseInitFunc ) NULL,
104 ( GBaseFinalizeFunc ) NULL,
105 ( GClassInitFunc ) class_init,
106 NULL,
107 NULL,
108 sizeof( NAImporterAsk ),
110 ( GInstanceInitFunc ) instance_init
113 g_debug( "%s", thisfn );
115 type = g_type_register_static( GTK_TYPE_DIALOG, "NAImporterAsk", &info, 0 );
117 return( type );
120 static void
121 class_init( NAImporterAskClass *klass )
123 static const gchar *thisfn = "na_importer_ask_class_init";
124 GObjectClass *object_class;
126 g_debug( "%s: klass=%p", thisfn, ( void * ) klass );
128 st_parent_class = g_type_class_peek_parent( klass );
130 object_class = G_OBJECT_CLASS( klass );
131 object_class->dispose = instance_dispose;
132 object_class->finalize = instance_finalize;
134 klass->private = g_new0( NAImporterAskClassPrivate, 1 );
137 static void
138 instance_init( GTypeInstance *instance, gpointer klass )
140 static const gchar *thisfn = "na_importer_ask_instance_init";
141 NAImporterAsk *self;
142 GError *error;
144 g_return_if_fail( NA_IS_IMPORTER_ASK( instance ));
146 g_debug( "%s: instance=%p (%s), klass=%p",
147 thisfn, ( void * ) instance, G_OBJECT_TYPE_NAME( instance ), ( void * ) klass );
149 self = NA_IMPORTER_ASK( instance );
151 self->private = g_new0( NAImporterAskPrivate, 1 );
153 self->private->builder = gtk_builder_new();
155 error = NULL;
156 gtk_builder_add_from_file( self->private->builder, PKGDATADIR "/na-importer-ask.ui", &error );
157 if( error ){
158 g_warning( "%s: %s", thisfn, error->message );
159 g_error_free( error );
161 } else {
162 self->private->toplevel = GTK_WINDOW( gtk_builder_get_object( self->private->builder, "ImporterAskDialog" ));
165 self->private->gconf = gconf_client_get_default();
167 self->private->dispose_has_run = FALSE;
170 static void
171 instance_dispose( GObject *dialog )
173 static const gchar *thisfn = "na_importer_ask_instance_dispose";
174 NAImporterAsk *self;
176 g_return_if_fail( NA_IS_IMPORTER_ASK( dialog ));
178 self = NA_IMPORTER_ASK( dialog );
180 if( !self->private->dispose_has_run ){
182 g_debug( "%s: dialog=%p (%s)", thisfn, ( void * ) dialog, G_OBJECT_TYPE_NAME( dialog ));
184 self->private->dispose_has_run = TRUE;
186 g_object_unref( self->private->gconf );
187 g_object_unref( self->private->builder );
189 /* chain up to the parent class */
190 if( G_OBJECT_CLASS( st_parent_class )->dispose ){
191 G_OBJECT_CLASS( st_parent_class )->dispose( dialog );
196 static void
197 instance_finalize( GObject *dialog )
199 static const gchar *thisfn = "na_importer_ask_instance_finalize";
200 NAImporterAsk *self;
202 g_return_if_fail( NA_IS_IMPORTER_ASK( dialog ));
204 g_debug( "%s: dialog=%p", thisfn, ( void * ) dialog );
206 self = NA_IMPORTER_ASK( dialog );
208 g_free( self->private );
210 /* chain call to parent class */
211 if( G_OBJECT_CLASS( st_parent_class )->finalize ){
212 G_OBJECT_CLASS( st_parent_class )->finalize( dialog );
217 * Returns a newly allocated NAImporterAsk object.
219 static NAImporterAsk *
220 import_ask_new()
222 return( g_object_new( NA_IMPORTER_ASK_TYPE, NULL ));
226 * na_importer_ask_user:
227 * @importing: the #NAObjectItem-derived object being currently imported.
228 * @existing: the #NAObjectItem-derived already existing object with the same ID.
229 * @parms: a #NAIImporterUriParms structure.
231 * Ask the user for what to do when an imported item has the same ID
232 * that an already existing one.
234 * Returns: the definitive import mode.
236 * When the user selects 'Keep same choice without asking me', this choice
237 * becomes his preference import mode.
239 guint
240 na_importer_ask_user( const NAObjectItem *importing, const NAObjectItem *existing, NAImporterAskUserParms *parms )
242 static const gchar *thisfn = "na_importer_ask_user";
243 NAImporterAsk *dialog;
244 guint mode;
245 gint code;
247 g_return_val_if_fail( NA_IS_OBJECT_ITEM( importing ), IMPORTER_MODE_NO_IMPORT );
248 g_return_val_if_fail( NA_IS_OBJECT_ITEM( existing ), IMPORTER_MODE_NO_IMPORT );
250 g_debug( "%s: importing=%p, existing=%p, parms=%p",
251 thisfn, ( void * ) importing, ( void * ) existing, ( void * ) parms );
253 mode = IMPORTER_MODE_NO_IMPORT;
254 dialog = import_ask_new();
256 if( dialog->private->toplevel ){
258 dialog->private->importing = ( NAObjectItem * ) importing;
259 dialog->private->existing = ( NAObjectItem * ) existing;
260 dialog->private->parms = parms;
261 dialog->private->mode = na_iprefs_get_import_mode( dialog->private->gconf, IPREFS_IMPORT_ASK_LAST_MODE );
263 init_dialog( dialog );
264 /* toplevel is modal, not dialog
265 g_debug( "dialog is modal: %s", gtk_window_get_modal( GTK_WINDOW( dialog )) ? "True":"False" );
266 g_debug( "toplevel is modal: %s", gtk_window_get_modal( dialog->private->toplevel ) ? "True":"False" );*/
267 do {
268 code = gtk_dialog_run( GTK_DIALOG( dialog->private->toplevel ));
269 } while ( !on_dialog_response( dialog, code ));
271 mode = dialog->private->mode;
273 gtk_widget_hide( GTK_WIDGET( dialog->private->toplevel ));
274 gtk_widget_destroy( GTK_WIDGET( dialog->private->toplevel ));
276 } else {
277 g_object_unref( dialog );
280 return( mode );
283 static void
284 init_dialog( NAImporterAsk *editor )
286 static const gchar *thisfn = "na_importer_ask_init_dialog";
287 gchar *imported_label, *existing_label;
288 gchar *label;
289 GtkWidget *widget;
290 GtkWidget *button;
292 g_return_if_fail( NA_IS_IMPORTER_ASK( editor ));
294 g_debug( "%s: editor=%p", thisfn, ( void * ) editor );
296 imported_label = na_object_get_label( editor->private->importing );
297 existing_label = na_object_get_label( editor->private->existing );
299 if( NA_IS_OBJECT_ACTION( editor->private->importing )){
300 /* i18n: The action <action_label> imported from <file> has the same id than <existing_label> */
301 label = g_strdup_printf(
302 _( "The action \"%s\" imported from \"%s\" has the same identifiant than the already existing \"%s\"." ),
303 imported_label, editor->private->parms->uri, existing_label );
305 } else {
306 /* i18n: The menu <menu_label> imported from <file> has the same id than <existing_label> */
307 label = g_strdup_printf(
308 _( "The menu \"%s\" imported from \"%s\" has the same identifiant than the already existing \"%s\"." ),
309 imported_label, editor->private->parms->uri, existing_label );
312 widget = na_gtk_utils_search_for_child_widget( GTK_CONTAINER( editor->private->toplevel ), "ImporterAskLabel" );
313 gtk_label_set_text( GTK_LABEL( widget ), label );
314 g_free( label );
316 switch( editor->private->mode ){
317 case IMPORTER_MODE_RENUMBER:
318 button = na_gtk_utils_search_for_child_widget( GTK_CONTAINER( editor->private->toplevel ), "AskRenumberButton" );
319 break;
321 case IMPORTER_MODE_OVERRIDE:
322 button = na_gtk_utils_search_for_child_widget( GTK_CONTAINER( editor->private->toplevel ), "AskOverrideButton" );
323 break;
325 case IMPORTER_MODE_NO_IMPORT:
326 default:
327 button = na_gtk_utils_search_for_child_widget( GTK_CONTAINER( editor->private->toplevel ), "AskNoImportButton" );
328 break;
330 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( button ), TRUE );
332 button = na_gtk_utils_search_for_child_widget( GTK_CONTAINER( editor->private->toplevel ), "AskKeepChoiceButton" );
333 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( button ), editor->private->parms->keep_choice );
335 button = na_gtk_utils_search_for_child_widget( GTK_CONTAINER( editor->private->toplevel ), "OKButton" );
336 g_signal_connect(
337 G_OBJECT( button ),
338 "clicked",
339 G_CALLBACK( on_ok_clicked ),
340 editor );
342 button = na_gtk_utils_search_for_child_widget( GTK_CONTAINER( editor->private->toplevel ), "CancelButton" );
343 g_signal_connect(
344 G_OBJECT( button ),
345 "clicked",
346 G_CALLBACK( on_cancel_clicked ),
347 editor );
349 if( editor->private->parms->parent ){
350 gtk_window_set_transient_for( editor->private->toplevel, editor->private->parms->parent );
353 gtk_widget_show_all( GTK_WIDGET( editor->private->toplevel ));
356 static void
357 on_cancel_clicked( GtkButton *button, NAImporterAsk *editor )
359 g_debug( "na_importer_ask_on_cancel_clicked" );
361 editor->private->dialog_code = GTK_RESPONSE_CANCEL;
363 gtk_dialog_response( GTK_DIALOG( editor ), GTK_RESPONSE_CANCEL );
366 static void
367 on_ok_clicked( GtkButton *button, NAImporterAsk *editor )
369 g_debug( "na_importer_ask_on_ok_clicked" );
371 editor->private->dialog_code = GTK_RESPONSE_OK;
373 gtk_dialog_response( GTK_DIALOG( editor ), GTK_RESPONSE_OK );
376 static void
377 get_selected_mode( NAImporterAsk *editor )
379 guint import_mode;
380 GtkWidget *button;
381 gboolean keep;
382 gchar *path;
384 import_mode = IMPORTER_MODE_NO_IMPORT;
386 button = na_gtk_utils_search_for_child_widget( GTK_CONTAINER( editor->private->toplevel ), "AskRenumberButton" );
387 if( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( button ))){
388 import_mode = IMPORTER_MODE_RENUMBER;
390 } else {
391 button = na_gtk_utils_search_for_child_widget( GTK_CONTAINER( editor->private->toplevel ), "AskOverrideButton" );
392 if( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( button ))){
393 import_mode = IMPORTER_MODE_OVERRIDE;
397 editor->private->mode = import_mode;
398 na_iprefs_set_import_mode( editor->private->gconf, IPREFS_IMPORT_ASK_LAST_MODE, editor->private->mode );
400 button = na_gtk_utils_search_for_child_widget( GTK_CONTAINER( editor->private->toplevel ), "AskKeepChoiceButton" );
401 keep = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( button ));
402 path = gconf_concat_dir_and_key( IPREFS_GCONF_PREFS_PATH, IPREFS_IMPORT_KEEP_CHOICE );
403 na_gconf_utils_write_bool( editor->private->gconf, path, keep, NULL );
404 g_free( path );
408 * very curiously, the code sent by on_cancel_clicked() and
409 * on_ok_clicked() arrives here at zero ! we so rely on a private one !
411 static gboolean
412 on_dialog_response( NAImporterAsk *editor, gint code )
414 static const gchar *thisfn = "na_importer_ask_on_dialog_response";
416 g_return_val_if_fail( NA_IS_IMPORTER_ASK( editor ), FALSE );
418 g_debug( "%s: editor=%p, code=%d", thisfn, ( void * ) editor, code );
420 switch( editor->private->dialog_code ){
421 case GTK_RESPONSE_CANCEL:
422 editor->private->mode = IMPORTER_MODE_NO_IMPORT;
423 return( TRUE );
424 break;
426 case GTK_RESPONSE_OK:
427 get_selected_mode( editor );
428 return( TRUE );
429 break;
431 case GTK_RESPONSE_NONE:
432 case GTK_RESPONSE_DELETE_EVENT:
433 case GTK_RESPONSE_CLOSE:
434 break;
437 return( FALSE );