Replace GtkLabel vith GtkTextView in import and export assistants
[nautilus-actions.git] / src / nact / nact-assistant-import.c
blob9b991c77f8fca6dbd5dbf66453480e36107c36d8
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 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 <string.h>
38 #include <api/na-object-api.h>
39 #include <api/na-core-utils.h>
41 #include <core/na-importer.h>
42 #include <core/na-iprefs.h>
44 #include "nact-application.h"
45 #include "nact-iprefs.h"
46 #include "nact-iactions-list.h"
47 #include "nact-assistant-import.h"
48 #include "nact-main-window.h"
50 /* Import Assistant
52 * pos. type title
53 * --- ------- --------------------------------------------------
54 * 0 Intro Introduction
55 * 1 Content Selection of the files
56 * 2 Content Duplicate management: what to do with duplicates ?
57 * 2 Confirm Display the selected files before import
58 * 3 Summary Import is done: summary of the done operations
61 enum {
62 ASSIST_PAGE_INTRO = 0,
63 ASSIST_PAGE_FILES_SELECTION,
64 ASSIST_PAGE_DUPLICATES,
65 ASSIST_PAGE_CONFIRM,
66 ASSIST_PAGE_DONE
69 /* private class data
71 struct NactAssistantImportClassPrivate {
72 void *empty; /* so that gcc -pedantic is happy */
75 /* private instance data
77 struct NactAssistantImportPrivate {
78 gboolean dispose_has_run;
79 GConfClient *gconf;
80 GList *results;
83 static BaseAssistantClass *st_parent_class = NULL;
85 static GType register_type( void );
86 static void class_init( NactAssistantImportClass *klass );
87 static void instance_init( GTypeInstance *instance, gpointer klass );
88 static void instance_dispose( GObject *application );
89 static void instance_finalize( GObject *application );
91 static NactAssistantImport *assist_new( BaseWindow *parent );
93 static gchar *window_get_iprefs_window_id( const BaseWindow *window );
94 static gchar *window_get_dialog_name( const BaseWindow *dialog );
96 static void on_initial_load_dialog( NactAssistantImport *dialog, gpointer user_data );
97 static void on_runtime_init_dialog( NactAssistantImport *dialog, gpointer user_data );
98 static void runtime_init_intro( NactAssistantImport *window, GtkAssistant *assistant );
99 static void runtime_init_file_selector( NactAssistantImport *window, GtkAssistant *assistant );
100 static void on_file_selection_changed( GtkFileChooser *chooser, gpointer user_data );
101 static gboolean has_readable_files( GSList *uris );
102 static void runtime_init_duplicates( NactAssistantImport *window, GtkAssistant *assistant );
103 static void set_import_mode( NactAssistantImport *window, gint mode );
105 static void assistant_prepare( BaseAssistant *window, GtkAssistant *assistant, GtkWidget *page );
106 static void prepare_confirm( NactAssistantImport *window, GtkAssistant *assistant, GtkWidget *page );
107 static gint get_import_mode( NactAssistantImport *window );
108 static gchar *add_import_mode( NactAssistantImport *window, const gchar *text );
109 static void assistant_apply( BaseAssistant *window, GtkAssistant *assistant );
110 static NAObjectItem *check_for_existence( const NAObjectItem *, NactMainWindow *window );
111 static void prepare_importdone( NactAssistantImport *window, GtkAssistant *assistant, GtkWidget *page );
112 static void free_results( GList *list );
114 GType
115 nact_assistant_import_get_type( void )
117 static GType window_type = 0;
119 if( !window_type ){
120 window_type = register_type();
123 return( window_type );
126 static GType
127 register_type( void )
129 static const gchar *thisfn = "nact_assistant_import_register_type";
130 GType type;
132 static GTypeInfo info = {
133 sizeof( NactAssistantImportClass ),
134 ( GBaseInitFunc ) NULL,
135 ( GBaseFinalizeFunc ) NULL,
136 ( GClassInitFunc ) class_init,
137 NULL,
138 NULL,
139 sizeof( NactAssistantImport ),
141 ( GInstanceInitFunc ) instance_init
144 g_debug( "%s", thisfn );
146 type = g_type_register_static( BASE_ASSISTANT_TYPE, "NactAssistantImport", &info, 0 );
148 return( type );
151 static void
152 class_init( NactAssistantImportClass *klass )
154 static const gchar *thisfn = "nact_assistant_import_class_init";
155 GObjectClass *object_class;
156 BaseWindowClass *base_class;
157 BaseAssistantClass *assist_class;
159 g_debug( "%s: klass=%p", thisfn, ( void * ) klass );
161 st_parent_class = g_type_class_peek_parent( klass );
163 object_class = G_OBJECT_CLASS( klass );
164 object_class->dispose = instance_dispose;
165 object_class->finalize = instance_finalize;
167 klass->private = g_new0( NactAssistantImportClassPrivate, 1 );
169 base_class = BASE_WINDOW_CLASS( klass );
170 base_class->get_toplevel_name = window_get_dialog_name;
171 base_class->get_iprefs_window_id = window_get_iprefs_window_id;
173 assist_class = BASE_ASSISTANT_CLASS( klass );
174 assist_class->apply = assistant_apply;
175 assist_class->prepare = assistant_prepare;
178 static void
179 instance_init( GTypeInstance *instance, gpointer klass )
181 static const gchar *thisfn = "nact_assistant_import_instance_init";
182 NactAssistantImport *self;
184 g_debug( "%s: instance=%p (%s), klass=%p",
185 thisfn, ( void * ) instance, G_OBJECT_TYPE_NAME( instance ), ( void * ) klass );
186 g_return_if_fail( NACT_IS_ASSISTANT_IMPORT( instance ));
187 self = NACT_ASSISTANT_IMPORT( instance );
189 self->private = g_new0( NactAssistantImportPrivate, 1 );
191 self->private->results = NULL;
193 base_window_signal_connect(
194 BASE_WINDOW( instance ),
195 G_OBJECT( instance ),
196 BASE_WINDOW_SIGNAL_INITIAL_LOAD,
197 G_CALLBACK( on_initial_load_dialog ));
199 base_window_signal_connect(
200 BASE_WINDOW( instance ),
201 G_OBJECT( instance ),
202 BASE_WINDOW_SIGNAL_RUNTIME_INIT,
203 G_CALLBACK( on_runtime_init_dialog ));
205 self->private->gconf = gconf_client_get_default();
207 self->private->dispose_has_run = FALSE;
210 static void
211 instance_dispose( GObject *window )
213 static const gchar *thisfn = "nact_assistant_import_instance_dispose";
214 NactAssistantImport *self;
216 g_debug( "%s: window=%p (%s)", thisfn, ( void * ) window, G_OBJECT_TYPE_NAME( window ));
217 g_return_if_fail( NACT_IS_ASSISTANT_IMPORT( window ));
218 self = NACT_ASSISTANT_IMPORT( window );
220 if( !self->private->dispose_has_run ){
222 self->private->dispose_has_run = TRUE;
224 g_object_unref( self->private->gconf );
226 /* chain up to the parent class */
227 if( G_OBJECT_CLASS( st_parent_class )->dispose ){
228 G_OBJECT_CLASS( st_parent_class )->dispose( window );
233 static void
234 instance_finalize( GObject *window )
236 static const gchar *thisfn = "nact_assistant_import_instance_finalize";
237 NactAssistantImport *self;
239 g_debug( "%s: window=%p", thisfn, ( void * ) window );
240 g_return_if_fail( NACT_IS_ASSISTANT_IMPORT( window ));
241 self = NACT_ASSISTANT_IMPORT( window );
243 free_results( self->private->results );
245 g_free( self->private );
247 /* chain call to parent class */
248 if( G_OBJECT_CLASS( st_parent_class )->finalize ){
249 G_OBJECT_CLASS( st_parent_class )->finalize( window );
253 static NactAssistantImport *
254 assist_new( BaseWindow *parent )
256 return( g_object_new( NACT_ASSISTANT_IMPORT_TYPE, BASE_WINDOW_PROP_PARENT, parent, NULL ));
260 * nact_assistant_import_run:
261 * @main: the #NactMainWindow parent window of this assistant.
263 * Run the assistant.
265 void
266 nact_assistant_import_run( BaseWindow *main_window )
268 NactAssistantImport *assist;
270 assist = assist_new( main_window );
271 g_object_set( G_OBJECT( assist ), BASE_WINDOW_PROP_HAS_OWN_BUILDER, TRUE, NULL );
273 base_window_run( BASE_WINDOW( assist ));
276 static gchar *
277 window_get_iprefs_window_id( const BaseWindow *window )
279 return( g_strdup( "import-assistant" ));
282 static gchar *
283 window_get_dialog_name( const BaseWindow *dialog )
285 return( g_strdup( "ImportAssistant" ));
288 static void
289 on_initial_load_dialog( NactAssistantImport *dialog, gpointer user_data )
291 static const gchar *thisfn = "nact_assistant_import_on_initial_load_dialog";
292 NactApplication *application;
293 NAUpdater *updater;
294 gboolean esc_quit, esc_confirm;
296 g_debug( "%s: dialog=%p, user_data=%p", thisfn, ( void * ) dialog, ( void * ) user_data );
297 g_return_if_fail( NACT_IS_ASSISTANT_IMPORT( dialog ));
299 application = NACT_APPLICATION( base_window_get_application( BASE_WINDOW( dialog )));
300 updater = nact_application_get_updater( application );
301 esc_quit = na_iprefs_read_bool( NA_IPREFS( updater ), IPREFS_ASSIST_ESC_QUIT, TRUE );
302 base_assistant_set_cancel_on_esc( BASE_ASSISTANT( dialog ), esc_quit );
303 esc_confirm = na_iprefs_read_bool( NA_IPREFS( updater ), IPREFS_ASSIST_ESC_CONFIRM, TRUE );
304 base_assistant_set_warn_on_esc( BASE_ASSISTANT( dialog ), esc_confirm );
307 static void
308 on_runtime_init_dialog( NactAssistantImport *dialog, gpointer user_data )
310 static const gchar *thisfn = "nact_assistant_import_on_runtime_init_dialog";
311 GtkAssistant *assistant;
313 g_debug( "%s: dialog=%p, user_data=%p", thisfn, ( void * ) dialog, ( void * ) user_data );
314 g_return_if_fail( NACT_IS_ASSISTANT_IMPORT( dialog ));
316 if( !dialog->private->dispose_has_run ){
318 assistant = GTK_ASSISTANT( base_window_get_toplevel( BASE_WINDOW( dialog )));
320 runtime_init_intro( dialog, assistant );
321 runtime_init_file_selector( dialog, assistant );
322 runtime_init_duplicates( dialog, assistant );
326 static void
327 runtime_init_intro( NactAssistantImport *window, GtkAssistant *assistant )
329 static const gchar *thisfn = "nact_assistant_import_runtime_init_intro";
330 GtkWidget *page;
332 page = gtk_assistant_get_nth_page( assistant, ASSIST_PAGE_INTRO );
334 g_debug( "%s: window=%p, assistant=%p, page=%p",
335 thisfn, ( void * ) window, ( void * ) assistant, ( void * ) page );
337 gtk_assistant_set_page_complete( assistant, page, TRUE );
340 static void
341 runtime_init_file_selector( NactAssistantImport *window, GtkAssistant *assistant )
343 static const gchar *thisfn = "nact_assistant_import_runtime_init_file_selector";
344 NactApplication *application;
345 NAUpdater *updater;
346 GtkWidget *page;
347 GtkWidget *chooser;
348 gchar *uri;
350 page = gtk_assistant_get_nth_page( assistant, ASSIST_PAGE_FILES_SELECTION );
352 g_debug( "%s: window=%p, assistant=%p, page=%p",
353 thisfn, ( void * ) window, ( void * ) assistant, ( void * ) page );
355 application = NACT_APPLICATION( base_window_get_application( BASE_WINDOW( window )));
356 updater = nact_application_get_updater( application );
358 chooser = base_window_get_widget( BASE_WINDOW( window ), "ImportFileChooser" );
359 uri = na_iprefs_read_string( NA_IPREFS( updater ), IPREFS_IMPORT_ITEMS_FOLDER_URI, "file:///tmp" );
360 if( uri && strlen( uri )){
361 gtk_file_chooser_set_current_folder_uri( GTK_FILE_CHOOSER( chooser ), uri );
363 g_free( uri );
365 base_window_signal_connect(
366 BASE_WINDOW( window ),
367 G_OBJECT( chooser ),
368 "selection-changed",
369 G_CALLBACK( on_file_selection_changed ));
371 gtk_assistant_set_page_complete( assistant, page, FALSE );
374 static void
375 on_file_selection_changed( GtkFileChooser *chooser, gpointer user_data )
377 /*static const gchar *thisfn = "nact_assistant_import_on_file_selection_changed";
378 g_debug( "%s: chooser=%p, user_data=%p", thisfn, chooser, user_data );*/
380 GtkAssistant *assistant;
381 gint pos;
382 GSList *uris;
383 gboolean enabled;
384 gchar *folder;
385 GtkWidget *content;
387 g_assert( NACT_IS_ASSISTANT_IMPORT( user_data ));
388 assistant = GTK_ASSISTANT( base_window_get_toplevel( BASE_WINDOW( user_data )));
389 pos = gtk_assistant_get_current_page( assistant );
390 if( pos == ASSIST_PAGE_FILES_SELECTION ){
392 uris = gtk_file_chooser_get_uris( chooser );
393 enabled = has_readable_files( uris );
395 if( enabled ){
396 folder = gtk_file_chooser_get_current_folder_uri( GTK_FILE_CHOOSER( chooser ));
397 nact_iprefs_write_string( BASE_WINDOW( user_data ), IPREFS_IMPORT_ITEMS_FOLDER_URI, folder );
398 g_free( folder );
401 na_core_utils_slist_free( uris );
403 content = gtk_assistant_get_nth_page( assistant, pos );
404 gtk_assistant_set_page_complete( assistant, content, enabled );
405 gtk_assistant_update_buttons_state( assistant );
410 * enable forward button if current selection has at least one readable file
412 static gboolean
413 has_readable_files( GSList *uris )
415 static const gchar *thisfn = "nact_assistant_import_has_readable_files";
416 GSList *iuri;
417 int readables = 0;
418 gchar *uri;
419 GFile *file;
420 GFileInfo *info;
421 GFileType type;
422 GError *error = NULL;
423 gboolean readable;
425 for( iuri = uris ; iuri ; iuri = iuri->next ){
427 uri = ( gchar * ) iuri->data;
428 if( !strlen( uri )){
429 continue;
432 file = g_file_new_for_uri( uri );
433 info = g_file_query_info( file,
434 G_FILE_ATTRIBUTE_ACCESS_CAN_READ "," G_FILE_ATTRIBUTE_STANDARD_TYPE,
435 G_FILE_QUERY_INFO_NONE, NULL, &error );
437 if( error ){
438 g_warning( "%s: g_file_query_info error: %s", thisfn, error->message );
439 g_error_free( error );
440 g_object_unref( file );
441 continue;
444 type = g_file_info_get_file_type( info );
445 if( type != G_FILE_TYPE_REGULAR ){
446 g_debug( "%s: %s is not a file", thisfn, uri );
447 g_object_unref( info );
448 continue;
451 readable = g_file_info_get_attribute_boolean( info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ );
452 if( !readable ){
453 g_debug( "%s: %s is not readable", thisfn, uri );
454 g_object_unref( info );
455 continue;
458 readables += 1;
459 g_object_unref( info );
462 return( readables > 0 );
465 static void
466 runtime_init_duplicates( NactAssistantImport *window, GtkAssistant *assistant )
468 static const gchar *thisfn = "nact_assistant_import_runtime_init_duplicates";
469 GtkWidget *page;
470 guint mode;
472 g_debug( "%s: window=%p", thisfn, ( void * ) window );
474 mode = na_iprefs_get_import_mode( window->private->gconf, IPREFS_IMPORT_ITEMS_IMPORT_MODE );
475 set_import_mode( window, mode );
477 page = gtk_assistant_get_nth_page( assistant, ASSIST_PAGE_DUPLICATES );
478 gtk_assistant_set_page_complete( assistant, page, TRUE );
481 static void
482 set_import_mode( NactAssistantImport *window, gint mode )
484 GtkToggleButton *button;
486 switch( mode ){
487 case IMPORTER_MODE_ASK:
488 button = GTK_TOGGLE_BUTTON( base_window_get_widget( BASE_WINDOW( window ), "AskButton" ));
489 gtk_toggle_button_set_active( button, TRUE );
490 break;
492 case IMPORTER_MODE_RENUMBER:
493 button = GTK_TOGGLE_BUTTON( base_window_get_widget( BASE_WINDOW( window ), "RenumberButton" ));
494 gtk_toggle_button_set_active( button, TRUE );
495 break;
497 case IMPORTER_MODE_OVERRIDE:
498 button = GTK_TOGGLE_BUTTON( base_window_get_widget( BASE_WINDOW( window ), "OverrideButton" ));
499 gtk_toggle_button_set_active( button, TRUE );
500 break;
502 case IMPORTER_MODE_NO_IMPORT:
503 default:
504 button = GTK_TOGGLE_BUTTON( base_window_get_widget( BASE_WINDOW( window ), "NoImportButton" ));
505 gtk_toggle_button_set_active( button, TRUE );
506 break;
510 static void
511 assistant_prepare( BaseAssistant *window, GtkAssistant *assistant, GtkWidget *page )
513 static const gchar *thisfn = "nact_assistant_import_assistant_prepare";
514 GtkAssistantPageType type;
516 g_debug( "%s: window=%p, assistant=%p, page=%p",
517 thisfn, ( void * ) window, ( void * ) assistant, ( void * ) page );
519 type = gtk_assistant_get_page_type( assistant, page );
521 switch( type ){
522 case GTK_ASSISTANT_PAGE_CONFIRM:
523 prepare_confirm( NACT_ASSISTANT_IMPORT( window ), assistant, page );
524 break;
526 case GTK_ASSISTANT_PAGE_SUMMARY:
527 prepare_importdone( NACT_ASSISTANT_IMPORT( window ), assistant, page );
528 break;
530 default:
531 break;
535 static void
536 prepare_confirm( NactAssistantImport *window, GtkAssistant *assistant, GtkWidget *page )
538 static const gchar *thisfn = "nact_assistant_import_prepare_confirm";
539 gchar *text, *tmp;
540 GtkWidget *chooser;
541 GSList *uris, *is;
542 GtkLabel *confirm_label;
544 g_debug( "%s: window=%p, assistant=%p, page=%p",
545 thisfn, ( void * ) window, ( void * ) assistant, ( void * ) page );
547 /* i18n: the title of the confirm page of the import assistant */
548 text = g_strdup( _( "About to import selected files:" ));
549 tmp = g_strdup_printf( "<b>%s</b>\n\n", text );
550 g_free( text );
551 text = tmp;
553 chooser = base_window_get_widget( BASE_WINDOW( window ), "ImportFileChooser" );
554 uris = gtk_file_chooser_get_uris( GTK_FILE_CHOOSER( chooser ));
556 for( is = uris ; is ; is = is->next ){
557 tmp = g_strdup_printf( "%s\t%s\n", text, ( gchar * ) is->data );
558 g_free( text );
559 text = tmp;
562 tmp = add_import_mode( window, text );
563 g_free( text );
564 text = tmp;
566 confirm_label = GTK_LABEL( base_window_get_widget( BASE_WINDOW( window ), "AssistantImportConfirmLabel" ));
567 gtk_label_set_markup( confirm_label, text );
568 g_free( text );
570 gtk_assistant_set_page_complete( assistant, page, TRUE );
573 static gint
574 get_import_mode( NactAssistantImport *window )
576 GtkToggleButton *no_import_button;
577 GtkToggleButton *renumber_button;
578 GtkToggleButton *override_button;
579 GtkToggleButton *ask_button;
580 gint mode;
582 no_import_button = GTK_TOGGLE_BUTTON( base_window_get_widget( BASE_WINDOW( window ), "NoImportButton" ));
583 renumber_button = GTK_TOGGLE_BUTTON( base_window_get_widget( BASE_WINDOW( window ), "RenumberButton" ));
584 override_button = GTK_TOGGLE_BUTTON( base_window_get_widget( BASE_WINDOW( window ), "OverrideButton" ));
585 ask_button = GTK_TOGGLE_BUTTON( base_window_get_widget( BASE_WINDOW( window ), "AskButton" ));
587 mode = IMPORTER_MODE_NO_IMPORT;
588 if( gtk_toggle_button_get_active( renumber_button )){
589 mode = IMPORTER_MODE_RENUMBER;
591 } else if( gtk_toggle_button_get_active( override_button )){
592 mode = IMPORTER_MODE_OVERRIDE;
594 } else if( gtk_toggle_button_get_active( ask_button )){
595 mode = IMPORTER_MODE_ASK;
598 return( mode );
601 static gchar *
602 add_import_mode( NactAssistantImport *window, const gchar *text )
604 gint mode;
605 gchar *label1, *label2, *label3;
606 gchar *result;
608 mode = get_import_mode( window );
609 label1 = NULL;
610 label2 = NULL;
611 result = NULL;
613 switch( mode ){
614 case IMPORTER_MODE_NO_IMPORT:
615 label1 = na_core_utils_str_remove_char( gtk_button_get_label( GTK_BUTTON( base_window_get_widget( BASE_WINDOW( window ), "NoImportButton" ))), "_" );
616 label2 = g_strdup( gtk_label_get_text( GTK_LABEL( base_window_get_widget( BASE_WINDOW( window ), "NoImportLabel"))));
617 break;
619 case IMPORTER_MODE_RENUMBER:
620 label1 = na_core_utils_str_remove_char( gtk_button_get_label( GTK_BUTTON( base_window_get_widget( BASE_WINDOW( window ), "RenumberButton" ))), "_" );
621 label2 = g_strdup( gtk_label_get_text( GTK_LABEL( base_window_get_widget( BASE_WINDOW( window ), "RenumberLabel"))));
622 break;
624 case IMPORTER_MODE_OVERRIDE:
625 label1 = na_core_utils_str_remove_char( gtk_button_get_label( GTK_BUTTON( base_window_get_widget( BASE_WINDOW( window ), "OverrideButton" ))), "_" );
626 label2 = g_strdup( gtk_label_get_text( GTK_LABEL( base_window_get_widget( BASE_WINDOW( window ), "OverrideLabel"))));
627 break;
629 case IMPORTER_MODE_ASK:
630 label1 = na_core_utils_str_remove_char( gtk_button_get_label( GTK_BUTTON( base_window_get_widget( BASE_WINDOW( window ), "AskButton" ))), "_" );
631 label2 = g_strdup( gtk_label_get_text( GTK_LABEL( base_window_get_widget( BASE_WINDOW( window ), "AskLabel"))));
632 break;
634 default:
635 break;
638 if( label1 ){
639 label3 = na_core_utils_str_add_prefix( "\t", label2 );
640 g_free( label2 );
642 result = g_strdup_printf( "%s\n\n<b>%s</b>\n\n%s", text, label1, label3 );
643 g_free( label3 );
644 g_free( label1 );
647 return( result );
651 * do import here
653 static void
654 assistant_apply( BaseAssistant *wnd, GtkAssistant *assistant )
656 static const gchar *thisfn = "nact_assistant_import_assistant_apply";
657 NactAssistantImport *window;
658 NAImporterParms import_parms;
659 GtkWidget *chooser;
660 BaseWindow *main_window;
661 GList *it;
662 GList *imported_items;
663 NAImporterResult *result;
664 NactApplication *application;
665 NAUpdater *updater;
667 g_return_if_fail( NACT_IS_ASSISTANT_IMPORT( wnd ));
669 g_debug( "%s: window=%p, assistant=%p", thisfn, ( void * ) wnd, ( void * ) assistant );
670 window = NACT_ASSISTANT_IMPORT( wnd );
672 imported_items = NULL;
673 memset( &import_parms, '\0', sizeof( NAImporterParms ));
675 g_object_get( G_OBJECT( wnd ), BASE_WINDOW_PROP_PARENT, &main_window, NULL );
676 import_parms.parent = base_window_get_toplevel( main_window );
678 chooser = base_window_get_widget( BASE_WINDOW( window ), "ImportFileChooser" );
679 import_parms.uris = gtk_file_chooser_get_uris( GTK_FILE_CHOOSER( chooser ));
680 import_parms.mode = get_import_mode( window );
682 import_parms.check_fn = ( NAIImporterCheckFn ) check_for_existence;
683 import_parms.check_fn_data = main_window;
685 application = NACT_APPLICATION( base_window_get_application( main_window ));
686 updater = nact_application_get_updater( application );
687 na_importer_import_from_list( NA_PIVOT( updater ), &import_parms );
689 for( it = import_parms.results ; it ; it = it->next ){
690 result = ( NAImporterResult * ) it->data;
692 if( result->imported ){
693 na_object_check_status( result->imported );
694 imported_items = g_list_prepend( imported_items, result->imported );
698 /* import actions
699 * getting results in the same order than uris
700 * simultaneously building the actions list
702 #if 0
703 for( is = uris ; is ; is = is->next ){
705 parms.version = 1;
706 parms.uri = ( gchar * ) is->data;
707 parms.mode = mode;
708 parms.window = base_window_get_toplevel( base_application_get_main_window( BASE_APPLICATION( application )));
709 parms.messages = NULL;
710 parms.imported = NULL;
712 code = na_importer_import_from_uri( NA_PIVOT( updater ), &parms );
714 str = g_new0( ImportUriStruct, 1 );
715 str->uri = g_strdup( parms.uri );
716 str->item = parms.imported;
717 str->msg = na_core_utils_slist_duplicate( parms.messages );
718 na_core_utils_slist_free( parms.messages );
720 if( str->item ){
721 na_object_check_status( str->item );
722 imported_items = g_list_prepend( imported_items, str->item );
725 window->private->results = g_slist_prepend( window->private->results, str );
727 #endif
729 na_core_utils_slist_free( import_parms.uris );
730 window->private->results = import_parms.results;
732 /* then insert the list
733 * assuring that actions will be inserted in the same order as uris
735 imported_items = g_list_reverse( imported_items );
736 nact_iactions_list_bis_insert_items( NACT_IACTIONS_LIST( main_window ), imported_items, NULL );
737 na_object_unref_items( imported_items );
740 static NAObjectItem *
741 check_for_existence( const NAObjectItem *item, NactMainWindow *window )
743 static const gchar *thisfn = "nact_assistant_import_check_for_existence";
744 NAObjectItem *exists;
745 gchar *importing_id;
747 importing_id = na_object_get_id( item );
748 g_debug( "%s: item=%p (%s), importing_id=%s",
749 thisfn, ( void * ) item, G_OBJECT_TYPE_NAME( item ), importing_id );
751 exists = nact_main_window_get_item( window, importing_id );
753 g_free( importing_id );
755 return( exists );
758 static void
759 prepare_importdone( NactAssistantImport *window, GtkAssistant *assistant, GtkWidget *page )
761 static const gchar *thisfn = "nact_assistant_import_prepare_importdone";
762 gchar *text, *tmp, *text2;
763 gchar *bname, *uuid, *label;
764 GList *is;
765 GSList *im;
766 NAImporterResult *result;
767 GFile *file;
768 guint mode;
769 GtkTextView *summary_textview;
770 GtkTextBuffer *summary_buffer;
771 GtkTextTag *title_tag;
772 GtkTextIter start, end;
773 gint title_len;
775 g_debug( "%s: window=%p, assistant=%p, page=%p",
776 thisfn, ( void * ) window, ( void * ) assistant, ( void * ) page );
778 /* i18n: result of the import assistant */
779 text = g_strdup( _( "Selected files have been proceeded :" ));
780 title_len = g_utf8_strlen( text, -1 );
781 tmp = g_strdup_printf( "%s\n\n", text );
782 g_free( text );
783 text = tmp;
785 for( is = window->private->results ; is ; is = is->next ){
786 result = ( NAImporterResult * ) is->data;
788 file = g_file_new_for_uri( result->uri );
789 bname = g_file_get_basename( file );
790 g_object_unref( file );
791 tmp = g_strdup_printf( "%s\t%s\n", text, bname );
792 g_free( text );
793 text = tmp;
794 g_free( bname );
796 if( result->imported ){
797 /* i18n: indicate that the file has been successfully imported */
798 tmp = g_strdup_printf( "%s\t\t%s\n", text, _( "Import OK" ));
799 g_free( text );
800 text = tmp;
801 uuid = na_object_get_id( result->imported );
802 label = na_object_get_label( result->imported );
803 /* i18n: this is the globally unique identifier and the label of the newly imported action */
804 text2 = g_strdup_printf( _( "UUID: %s\t%s" ), uuid, label);
805 g_free( label );
806 g_free( uuid );
807 tmp = g_strdup_printf( "%s\t\t%s\n", text, text2 );
808 g_free( text );
809 text = tmp;
811 } else {
812 /* i18n: indicate that the file was not imported */
813 tmp = g_strdup_printf( "%s\t\t%s\n", text, _( "Not imported" ));
814 g_free( text );
815 text = tmp;
818 /* add messages if any */
819 for( im = result->messages ; im ; im = im->next ){
820 tmp = g_strdup_printf( "%s\t\t%s\n", text, ( const char * ) im->data );
821 g_free( text );
822 text = tmp;
825 /* add a blank line between two actions */
826 tmp = g_strdup_printf( "%s\n", text );
827 g_free( text );
828 text = tmp;
831 summary_textview = GTK_TEXT_VIEW( base_window_get_widget( BASE_WINDOW( window ), "AssistantImportSummaryTextView" ));
832 summary_buffer = gtk_text_view_get_buffer( summary_textview );
833 gtk_text_buffer_set_text( summary_buffer, text, -1 );
834 g_free( text );
836 title_tag = gtk_text_buffer_create_tag( summary_buffer, "title",
837 "weight", PANGO_WEIGHT_BOLD,
838 NULL );
840 gtk_text_buffer_get_iter_at_offset( summary_buffer, &start, 0 );
841 gtk_text_buffer_get_iter_at_offset( summary_buffer, &end, title_len );
842 gtk_text_buffer_apply_tag( summary_buffer, title_tag, &start, &end );
844 g_object_unref( title_tag );
846 mode = get_import_mode( window );
847 na_iprefs_set_import_mode( window->private->gconf, IPREFS_IMPORT_ITEMS_IMPORT_MODE, mode );
849 gtk_assistant_set_page_complete( assistant, page, TRUE );
850 base_assistant_set_warn_on_cancel( BASE_ASSISTANT( window ), FALSE );
851 base_assistant_set_warn_on_esc( BASE_ASSISTANT( window ), FALSE );
854 static void
855 free_results( GList *list )
857 GList *it;
859 for( it = list ; it ; it = it->next ){
860 na_importer_free_result(( NAImporterResult * ) it->data );
863 g_list_free( list );