Update copyright message
[nautilus-actions.git] / src / core / na-iimporter.c
blob30453496b4165519d65f9e38ea0f72083bc194aa
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>
37 #include <api/na-core-utils.h>
38 #include <api/na-iimporter.h>
39 #include <api/na-object-api.h>
41 /* private interface data
43 struct _NAIImporterInterfacePrivate {
44 void *empty; /* so that gcc -pedantic is happy */
47 gboolean iimporter_initialized = FALSE;
48 gboolean iimporter_finalized = FALSE;
50 static GType register_type( void );
51 static void interface_base_init( NAIImporterInterface *klass );
52 static void interface_base_finalize( NAIImporterInterface *klass );
54 static guint iimporter_get_version( const NAIImporter *instance );
56 static void renumber_label_item( NAIImporterManageImportModeParms *parms );
58 /**
59 * na_iimporter_get_type:
61 * Returns: the #GType type of this interface.
63 GType
64 na_iimporter_get_type( void )
66 static GType type = 0;
68 if( !type ){
69 type = register_type();
72 return( type );
76 * na_iimporter_register_type:
78 * Registers this interface.
80 static GType
81 register_type( void )
83 static const gchar *thisfn = "na_iimporter_register_type";
84 GType type;
86 static const GTypeInfo info = {
87 sizeof( NAIImporterInterface ),
88 ( GBaseInitFunc ) interface_base_init,
89 ( GBaseFinalizeFunc ) interface_base_finalize,
90 NULL,
91 NULL,
92 NULL,
95 NULL
98 g_debug( "%s", thisfn );
100 type = g_type_register_static( G_TYPE_INTERFACE, "NAIImporter", &info, 0 );
102 g_type_interface_add_prerequisite( type, G_TYPE_OBJECT );
104 return( type );
107 static void
108 interface_base_init( NAIImporterInterface *klass )
110 static const gchar *thisfn = "na_iimporter_interface_base_init";
112 if( !iimporter_initialized ){
114 g_debug( "%s: klass%p (%s)", thisfn, ( void * ) klass, G_OBJECT_CLASS_NAME( klass ));
116 klass->private = g_new0( NAIImporterInterfacePrivate, 1 );
118 klass->get_version = iimporter_get_version;
119 klass->import_from_uri = NULL;
121 iimporter_initialized = TRUE;
125 static void
126 interface_base_finalize( NAIImporterInterface *klass )
128 static const gchar *thisfn = "na_iimporter_interface_base_finalize";
130 if( iimporter_initialized && !iimporter_finalized ){
132 g_debug( "%s: klass=%p", thisfn, ( void * ) klass );
134 iimporter_finalized = TRUE;
136 g_free( klass->private );
140 static guint
141 iimporter_get_version( const NAIImporter *instance )
143 return( 1 );
147 * na_iimporter_import_from_uri:
148 * @importer: this #NAIImporter instance.
149 * @parms: a #NAIImporterImportFromUriParms structure.
151 * Tries to import a #NAObjectItem from the URI specified in @parms, returning
152 * the result in <structfield>@parms->imported</structfield>.
154 * Returns: the return code of the operation.
156 * Since: 2.30
159 guint
160 na_iimporter_import_from_uri( const NAIImporter *importer, NAIImporterImportFromUriParms *parms )
162 static const gchar *thisfn = "na_iimporter_import_from_uri";
163 guint code;
165 g_return_val_if_fail( NA_IS_IIMPORTER( importer ), IMPORTER_CODE_PROGRAM_ERROR );
167 code = IMPORTER_CODE_NOT_WILLING_TO;
169 if( iimporter_initialized && !iimporter_finalized ){
171 g_debug( "%s: importer=%p, parms=%p", thisfn, ( void * ) importer, ( void * ) parms );
173 if( NA_IIMPORTER_GET_INTERFACE( importer )->import_from_uri ){
174 code = NA_IIMPORTER_GET_INTERFACE( importer )->import_from_uri( importer, parms );
178 return( code );
182 * na_iimporter_manage_import_mode:
183 * @parms: a NAIImporterManageImportModeParms struct.
185 * Returns: the #NAIImporterImportStatus status of the operation:
187 * <itemizedlist>
188 * <listitem>
189 * <para>
190 * IMPORTER_CODE_OK if we can safely insert the action:
191 * </para>
192 * <itemizedlist>
193 * <listitem>
194 * <para>the id doesn't already exist</para>
195 * </listitem>
196 * <listitem>
197 * <para>the id already exist, but import mode is renumber</para>
198 * </listitem>
199 * <listitem>
200 * <para>the id already exists, but import mode is override</para>
201 * </listitem>
202 * </itemizedlist>
203 * </listitem>
204 * <listitem>
205 * <para>
206 * IMPORTER_CODE_CANCELLED if user chooses to cancel the operation
207 * </para>
208 * </listitem>
209 * </itemizedlist>
211 * Since: 2.30
213 guint
214 na_iimporter_manage_import_mode( NAIImporterManageImportModeParms *parms )
216 guint code;
217 NAObjectItem *exists;
218 guint mode;
219 gchar *id;
221 g_return_val_if_fail( parms->imported != NULL, IMPORTER_CODE_CANCELLED );
223 code = IMPORTER_CODE_OK;
224 exists = NULL;
225 mode = 0;
227 if( parms->check_fn ){
228 exists = ( *parms->check_fn )( parms->imported, parms->check_fn_data );
230 } else {
231 renumber_label_item( parms );
232 na_core_utils_slist_add_message( &parms->messages, "%s", _( "Item was renumbered because the caller did not provide any check function." ));
233 parms->import_mode = IMPORTER_MODE_RENUMBER;
236 if( exists ){
237 parms->exist = TRUE;
239 if( parms->asked_mode == IMPORTER_MODE_ASK ){
240 if( parms->ask_fn ){
241 mode = ( *parms->ask_fn )( parms->imported, exists, parms->ask_fn_data );
243 } else {
244 renumber_label_item( parms );
245 na_core_utils_slist_add_message( &parms->messages, "%s", _( "Item was renumbered because the caller did not provide any ask user function." ));
246 parms->import_mode = IMPORTER_MODE_RENUMBER;
249 } else {
250 mode = parms->asked_mode;
254 /* mode is only set if asked mode is ask user and an ask function was provided
255 * or if asked mode was not ask user
257 if( mode ){
258 parms->import_mode = mode;
260 switch( mode ){
261 case IMPORTER_MODE_RENUMBER:
262 renumber_label_item( parms );
263 if( parms->asked_mode == IMPORTER_MODE_ASK ){
264 na_core_utils_slist_add_message( &parms->messages, "%s", _( "Item was renumbered due to user request." ));
266 break;
268 case IMPORTER_MODE_OVERRIDE:
269 if( parms->asked_mode == IMPORTER_MODE_ASK ){
270 na_core_utils_slist_add_message( &parms->messages, "%s", _( "Existing item was overriden due to user request." ));
272 break;
274 case IMPORTER_MODE_NO_IMPORT:
275 default:
276 id = na_object_get_id( parms->imported );
277 na_core_utils_slist_add_message( &parms->messages, _( "Item %s already exists." ), id );
278 if( parms->asked_mode == IMPORTER_MODE_ASK ){
279 na_core_utils_slist_add_message( &parms->messages, "%s", _( "Import was canceled due to user request." ));
281 g_free( id );
282 code = IMPORTER_CODE_CANCELLED;
286 return( code );
290 * renumber the item, and set a new label
292 static void
293 renumber_label_item( NAIImporterManageImportModeParms *parms )
295 gchar *label, *tmp;
297 na_object_set_new_id( parms->imported, NULL );
299 label = na_object_get_label( parms->imported );
301 /* i18n: the action has been renumbered during import operation */
302 tmp = g_strdup_printf( "%s %s", label, _( "(renumbered)" ));
304 na_object_set_label( parms->imported, tmp );
306 g_free( tmp );
307 g_free( label );