Convert NASettings to a private singleton
[nautilus-actions.git] / src / core / na-importer.c
blobd700671f29f2230f3d953653c07da6b5cbfbb686
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 <string.h>
37 #include <api/na-core-utils.h>
38 #include <api/na-object-api.h>
40 #include "na-iprefs.h"
41 #include "na-importer.h"
42 #include "na-importer-ask.h"
44 typedef struct {
45 GList *just_imported;
46 NAIImporterCheckFn check_fn;
47 void *check_fn_data;
49 ImporterExistsStr;
51 extern gboolean iimporter_initialized; /* defined in na-iimporter.c */
52 extern gboolean iimporter_finalized; /* defined in na-iimporter.c */
54 static guint import_from_uri( const NAPivot *pivot, GList *modules, NAImporterParms *parms, const gchar *uri, NAImporterResult **result );
55 static NAObjectItem *is_importing_already_exists( const NAObjectItem *importing, ImporterExistsStr *parms );
56 static guint ask_user_for_mode( const NAObjectItem *importing, const NAObjectItem *existing, NAImporterAskUserParms *parms );
59 * na_importer_import_from_list:
60 * @pivot: the #NAPivot pivot for this application.
61 * @parms: a #NAImporterParms structure.
63 * Imports a list of URIs.
65 * For each URI to import, we search through the available #NAIImporter
66 * providers until the first which respond something different from
67 * "not_willing_to" code.
69 * Each import operation will have its corresponding newly allocated
70 * #NAImporterResult structure which will contain:
71 * - the imported URI
72 * - a #NAObjectItem item if import was successful, or %NULL
73 * - a list of error messages, or %NULL.
75 * If asked mode is 'ask', then ask the user at least the first time
76 * the 'keep my choice' is active or not, depending of the last time used
77 * then the 'keep my chose' is kept for other times
78 * so preferences are:
79 * - asked import mode (may be 'ask') -> import-mode
80 * - keep my choice -> import-keep-choice
81 * - last chosen import mode -> import-ask-user-last-mode
83 * Returns: the last import operation code.
85 * Since: 2.30
87 guint
88 na_importer_import_from_list( const NAPivot *pivot, NAImporterParms *parms )
90 static const gchar *thisfn = "na_importer_import_from_list";
91 GList *modules;
92 GSList *iuri;
93 NAImporterResult *result;
94 guint code;
96 g_return_val_if_fail( NA_IS_PIVOT( pivot ), IMPORTER_CODE_PROGRAM_ERROR );
98 code = IMPORTER_CODE_NOT_WILLING_TO;
99 parms->results = NULL;
101 if( iimporter_initialized && !iimporter_finalized ){
103 g_debug( "%s: pivot=%p, parms=%p", thisfn, ( void * ) pivot, ( void * ) parms );
105 modules = na_pivot_get_providers( pivot, NA_IIMPORTER_TYPE );
107 for( iuri = parms->uris ; iuri ; iuri = iuri->next ){
108 code = import_from_uri( pivot, modules, parms, ( const gchar * ) iuri->data, &result );
109 parms->results = g_list_prepend( parms->results, result );
112 na_pivot_free_providers( modules );
113 parms->results = g_list_reverse( parms->results );
116 return( code );
120 * na_importer_free_result:
121 * @result: the #NAImporterResult structure to be released.
123 * Release the structure.
125 void
126 na_importer_free_result( NAImporterResult *result )
128 g_free( result->uri );
129 na_core_utils_slist_free( result->messages );
131 g_free( result );
134 static guint
135 import_from_uri( const NAPivot *pivot, GList *modules, NAImporterParms *parms, const gchar *uri, NAImporterResult **result )
137 guint code;
138 GList *im;
139 NAIImporterImportFromUriParms provider_parms;
140 ImporterExistsStr exists_parms;
141 NAImporterAskUserParms ask_parms;
143 code = IMPORTER_CODE_NOT_WILLING_TO;
145 memset( &exists_parms, '\0', sizeof( ImporterExistsStr ));
146 exists_parms.just_imported = parms->results;
147 exists_parms.check_fn = parms->check_fn;
148 exists_parms.check_fn_data = parms->check_fn_data;
150 memset( &ask_parms, '\0', sizeof( NAImporterAskUserParms ));
151 ask_parms.parent = parms->parent;
152 ask_parms.uri = ( gchar * ) uri;
153 ask_parms.count = g_list_length( parms->results );
154 ask_parms.keep_choice = na_settings_get_boolean( NA_IPREFS_IMPORT_ASK_USER_KEEP_LAST_CHOICE, NULL, NULL );
155 ask_parms.pivot = pivot;
157 memset( &provider_parms, '\0', sizeof( NAIImporterImportFromUriParms ));
158 provider_parms.version = 1;
159 provider_parms.uri = ( gchar * ) uri;
160 provider_parms.asked_mode = parms->mode;
161 provider_parms.check_fn = ( NAIImporterCheckFn ) is_importing_already_exists;
162 provider_parms.check_fn_data = &exists_parms;
163 provider_parms.ask_fn = ( NAIImporterAskUserFn ) ask_user_for_mode;
164 provider_parms.ask_fn_data = &ask_parms;
166 for( im = modules ; im && code == IMPORTER_CODE_NOT_WILLING_TO ; im = im->next ){
167 code = na_iimporter_import_from_uri( NA_IIMPORTER( im->data ), &provider_parms );
170 *result = g_new0( NAImporterResult, 1 );
171 ( *result )->uri = g_strdup( uri );
172 ( *result )->mode = provider_parms.import_mode;
173 ( *result )->exist = provider_parms.exist;
174 ( *result )->imported = provider_parms.imported;
175 ( *result )->messages = provider_parms.messages;
177 return( code );
181 * to see if an imported item already exists, we have to check
182 * - the current list of just imported items
183 * - the main window (if any), which contains the in-memory list of items
184 * - the tree in pivot which contains the 'actual' items
186 static NAObjectItem *
187 is_importing_already_exists( const NAObjectItem *importing, ImporterExistsStr *parms )
189 static const gchar *thisfn = "na_importer_is_importing_already_exists";
190 NAObjectItem *exists;
191 GList *ip;
193 exists = NULL;
194 gchar *importing_id = na_object_get_id( importing );
195 g_debug( "%s: importing=%p, id=%s", thisfn, ( void * ) importing, importing_id );
197 /* is the importing item already in the current importation list ?
199 for( ip = parms->just_imported ; ip && !exists ; ip = ip->next ){
200 NAImporterResult *result = ( NAImporterResult * ) ip->data;
202 if( result->imported ){
203 gchar *id = na_object_get_id( result->imported );
204 if( !strcmp( importing_id, id )){
205 exists = NA_OBJECT_ITEM( result->imported );
207 g_free( id );
211 g_free( importing_id );
213 /* if not found in our current importation list,
214 * then check the existence via provided function and data
216 if( !exists ){
217 exists = parms->check_fn( importing, parms->check_fn_data );
220 return( exists );
223 static guint
224 ask_user_for_mode( const NAObjectItem *importing, const NAObjectItem *existing, NAImporterAskUserParms *parms )
226 guint mode;
228 if( parms->count == 0 || !parms->keep_choice ){
229 mode = na_importer_ask_user( importing, existing, parms );
231 } else {
232 mode = na_iprefs_get_import_mode( NA_IPREFS_IMPORT_ASK_USER_LAST_MODE, NULL );
235 return( mode );