Cleanup code
[nautilus-actions.git] / src / nact / nact-assistant-import.c
blob5749a40693eccec21c6c57a5e5e5fe6855da1986
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 na_core_utils_slist_free( import_parms.uris );
699 window->private->results = import_parms.results;
701 /* then insert the list
702 * assuring that actions will be inserted in the same order as uris
704 imported_items = g_list_reverse( imported_items );
705 nact_iactions_list_bis_insert_items( NACT_IACTIONS_LIST( main_window ), imported_items, NULL );
706 na_object_unref_items( imported_items );
709 static NAObjectItem *
710 check_for_existence( const NAObjectItem *item, NactMainWindow *window )
712 static const gchar *thisfn = "nact_assistant_import_check_for_existence";
713 NAObjectItem *exists;
714 gchar *importing_id;
716 importing_id = na_object_get_id( item );
717 g_debug( "%s: item=%p (%s), importing_id=%s",
718 thisfn, ( void * ) item, G_OBJECT_TYPE_NAME( item ), importing_id );
720 exists = nact_main_window_get_item( window, importing_id );
722 g_free( importing_id );
724 return( exists );
727 static void
728 prepare_importdone( NactAssistantImport *window, GtkAssistant *assistant, GtkWidget *page )
730 static const gchar *thisfn = "nact_assistant_import_prepare_importdone";
731 gchar *text, *tmp, *text2;
732 gchar *bname, *uuid, *label;
733 GList *is;
734 GSList *im;
735 NAImporterResult *result;
736 GFile *file;
737 guint mode;
738 GtkTextView *summary_textview;
739 GtkTextBuffer *summary_buffer;
740 GtkTextTag *title_tag;
741 GtkTextIter start, end;
742 gint title_len;
744 g_debug( "%s: window=%p, assistant=%p, page=%p",
745 thisfn, ( void * ) window, ( void * ) assistant, ( void * ) page );
747 /* i18n: result of the import assistant */
748 text = g_strdup( _( "Selected files have been proceeded :" ));
749 title_len = g_utf8_strlen( text, -1 );
750 tmp = g_strdup_printf( "%s\n\n", text );
751 g_free( text );
752 text = tmp;
754 for( is = window->private->results ; is ; is = is->next ){
755 result = ( NAImporterResult * ) is->data;
757 file = g_file_new_for_uri( result->uri );
758 bname = g_file_get_basename( file );
759 g_object_unref( file );
760 tmp = g_strdup_printf( "%s\t%s\n", text, bname );
761 g_free( text );
762 text = tmp;
763 g_free( bname );
765 if( result->imported ){
766 /* i18n: indicate that the file has been successfully imported */
767 tmp = g_strdup_printf( "%s\t\t%s\n", text, _( "Import OK" ));
768 g_free( text );
769 text = tmp;
770 uuid = na_object_get_id( result->imported );
771 label = na_object_get_label( result->imported );
772 /* i18n: this is the globally unique identifier and the label of the newly imported action */
773 text2 = g_strdup_printf( _( "UUID: %s\t%s" ), uuid, label);
774 g_free( label );
775 g_free( uuid );
776 tmp = g_strdup_printf( "%s\t\t%s\n", text, text2 );
777 g_free( text );
778 text = tmp;
780 } else {
781 /* i18n: indicate that the file was not imported */
782 tmp = g_strdup_printf( "%s\t\t%s\n", text, _( "Not imported" ));
783 g_free( text );
784 text = tmp;
787 /* add messages if any */
788 for( im = result->messages ; im ; im = im->next ){
789 tmp = g_strdup_printf( "%s\t\t%s\n", text, ( const char * ) im->data );
790 g_free( text );
791 text = tmp;
794 /* add a blank line between two actions */
795 tmp = g_strdup_printf( "%s\n", text );
796 g_free( text );
797 text = tmp;
800 summary_textview = GTK_TEXT_VIEW( base_window_get_widget( BASE_WINDOW( window ), "AssistantImportSummaryTextView" ));
801 summary_buffer = gtk_text_view_get_buffer( summary_textview );
802 gtk_text_buffer_set_text( summary_buffer, text, -1 );
803 g_free( text );
805 title_tag = gtk_text_buffer_create_tag( summary_buffer, "title",
806 "weight", PANGO_WEIGHT_BOLD,
807 NULL );
809 gtk_text_buffer_get_iter_at_offset( summary_buffer, &start, 0 );
810 gtk_text_buffer_get_iter_at_offset( summary_buffer, &end, title_len );
811 gtk_text_buffer_apply_tag( summary_buffer, title_tag, &start, &end );
813 g_object_unref( title_tag );
815 mode = get_import_mode( window );
816 na_iprefs_set_import_mode( window->private->gconf, IPREFS_IMPORT_ITEMS_IMPORT_MODE, mode );
818 gtk_assistant_set_page_complete( assistant, page, TRUE );
819 base_assistant_set_warn_on_cancel( BASE_ASSISTANT( window ), FALSE );
820 base_assistant_set_warn_on_esc( BASE_ASSISTANT( window ), FALSE );
823 static void
824 free_results( GList *list )
826 GList *it;
828 for( it = list ; it ; it = it->next ){
829 na_importer_free_result(( NAImporterResult * ) it->data );
832 g_list_free( list );