File: fma-iimporter.c

package info (click to toggle)
filemanager-actions 3.4-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 27,428 kB
  • sloc: ansic: 48,857; xml: 17,767; sh: 4,662; makefile: 1,375; lisp: 9
file content (318 lines) | stat: -rw-r--r-- 8,483 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 * FileManager-Actions
 * A file-manager extension which offers configurable context menu actions.
 *
 * Copyright (C) 2005 The GNOME Foundation
 * Copyright (C) 2006-2008 Frederic Ruaudel and others (see AUTHORS)
 * Copyright (C) 2009-2015 Pierre Wieser and others (see AUTHORS)
 *
 * FileManager-Actions is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * FileManager-Actions is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with FileManager-Actions; see the file COPYING. If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *   Frederic Ruaudel <grumz@grumz.net>
 *   Rodrigo Moya <rodrigo@gnome-db.org>
 *   Pierre Wieser <pwieser@trychlos.org>
 *   ... and many others (see AUTHORS)
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib/gi18n.h>

#include <api/fma-core-utils.h>
#include <api/fma-iimporter.h>
#include <api/fma-object-api.h>

/* private interface data
 */
struct _FMAIImporterInterfacePrivate {
	void *empty;						/* so that gcc -pedantic is happy */
};

static guint st_initializations = 0;	/* interface initialization count */

static GType register_type( void );
static void  interface_base_init( FMAIImporterInterface *klass );
static void  interface_base_finalize( FMAIImporterInterface *klass );
static guint iimporter_get_version( const FMAIImporter *instance );

#ifdef FMA_ENABLE_DEPRECATED
static void  renumber_label_item( FMAIImporterManageImportModeParms *parms );
#endif

/**
 * fma_iimporter_get_type:
 *
 * Returns: the #GType type of this interface.
 */
GType
fma_iimporter_get_type( void )
{
	static GType type = 0;

	if( !type ){
		type = register_type();
	}

	return( type );
}

/*
 * fma_iimporter_register_type:
 *
 * Registers this interface.
 */
static GType
register_type( void )
{
	static const gchar *thisfn = "fma_iimporter_register_type";
	GType type;

	static const GTypeInfo info = {
		sizeof( FMAIImporterInterface ),
		( GBaseInitFunc ) interface_base_init,
		( GBaseFinalizeFunc ) interface_base_finalize,
		NULL,
		NULL,
		NULL,
		0,
		0,
		NULL
	};

	g_debug( "%s", thisfn );

	type = g_type_register_static( G_TYPE_INTERFACE, "FMAIImporter", &info, 0 );

	g_type_interface_add_prerequisite( type, G_TYPE_OBJECT );

	return( type );
}

static void
interface_base_init( FMAIImporterInterface *klass )
{
	static const gchar *thisfn = "fma_iimporter_interface_base_init";

	if( !st_initializations ){

		g_debug( "%s: klass%p (%s)", thisfn, ( void * ) klass, G_OBJECT_CLASS_NAME( klass ));

		klass->private = g_new0( FMAIImporterInterfacePrivate, 1 );

		klass->get_version = iimporter_get_version;
		klass->import_from_uri = NULL;
	}

	st_initializations += 1;
}

static void
interface_base_finalize( FMAIImporterInterface *klass )
{
	static const gchar *thisfn = "fma_iimporter_interface_base_finalize";

	st_initializations -= 1;

	if( !st_initializations ){

		g_debug( "%s: klass=%p", thisfn, ( void * ) klass );

		g_free( klass->private );
	}
}

static guint
iimporter_get_version( const FMAIImporter *instance )
{
	return( 1 );
}

/**
 * fma_iimporter_import_from_uri:
 * @importer: this #FMAIImporter instance.
 * @parms: a #FMAIImporterImportFromUriParmsv2 structure.
 *
 * Tries to import a #FMAObjectItem from the URI specified in @parms, returning
 * the result in <structfield>@parms->imported</structfield>.
 *
 * Note that, starting with &prodname; 3.2, the @parms argument is no more a
 * #FMAIImporterImportFromUriParms pointer, but a #FMAIImporterImportFromUriParmsv2
 * one.
 *
 * Returns: the return code of the operation.
 *
 * Since: 2.30
 */

guint
fma_iimporter_import_from_uri( const FMAIImporter *importer, FMAIImporterImportFromUriParmsv2 *parms )
{
	static const gchar *thisfn = "fma_iimporter_import_from_uri";
	guint code;

	g_return_val_if_fail( FMA_IS_IIMPORTER( importer ), IMPORTER_CODE_PROGRAM_ERROR );
	g_return_val_if_fail( parms && parms->version == 2, IMPORTER_CODE_PROGRAM_ERROR );

	code = IMPORTER_CODE_NOT_WILLING_TO;

	g_debug( "%s: importer=%p (%s), parms=%p", thisfn,
			( void * ) importer, G_OBJECT_TYPE_NAME( importer), ( void * ) parms );

	if( FMA_IIMPORTER_GET_INTERFACE( importer )->import_from_uri ){
		code = FMA_IIMPORTER_GET_INTERFACE( importer )->import_from_uri( importer, parms );
	}

	return( code );
}

#ifdef FMA_ENABLE_DEPRECATED
/**
 * fma_iimporter_manage_import_mode:
 * @parms: a #FMAIImporterManageImportModeParms struct.
 *
 * Returns: the #FMAIImporterImportStatus status of the operation:
 *
 * <itemizedlist>
 *   <listitem>
 *     <para>
 *       IMPORTER_CODE_OK if we can safely insert the action:
 *     </para>
 *     <itemizedlist>
 *       <listitem>
 *         <para>the id doesn't already exist</para>
 *       </listitem>
 *       <listitem>
 *         <para>or the id already exists, but import mode is renumber</para>
 *       </listitem>
 *       <listitem>
 *         <para>or the id already exists, but import mode is override</para>
 *       </listitem>
 *     </itemizedlist>
 *   </listitem>
 *   <listitem>
 *     <para>
 *       IMPORTER_CODE_CANCELLED if user chooses to cancel the operation
 *     </para>
 *   </listitem>
 * </itemizedlist>
 *
 * Since: 2.30
 * Deprecated: 3.2
 */
guint
fma_iimporter_manage_import_mode( FMAIImporterManageImportModeParms *parms )
{
	static const gchar *thisfn = "fma_iimporter_manage_import_mode";
	guint code;
	FMAObjectItem *exists;
	guint mode;
	gchar *id;

	g_return_val_if_fail( parms->imported != NULL, IMPORTER_CODE_CANCELLED );

	code = IMPORTER_CODE_OK;
	exists = NULL;
	mode = 0;
	parms->exist = FALSE;
	parms->import_mode = parms->asked_mode;

	if( parms->check_fn ){
		exists = ( *parms->check_fn )( parms->imported, parms->check_fn_data );

	} else {
		renumber_label_item( parms );
		fma_core_utils_slist_add_message( &parms->messages, "%s", _( "Item was renumbered because the caller did not provide any check function." ));
		parms->import_mode = IMPORTER_MODE_RENUMBER;
	}

	g_debug( "%s: exists=%p", thisfn, exists );

	if( exists ){
		parms->exist = TRUE;

		if( parms->asked_mode == IMPORTER_MODE_ASK ){
			if( parms->ask_fn ){
				mode = ( *parms->ask_fn )( parms->imported, exists, parms->ask_fn_data );

			} else {
				renumber_label_item( parms );
				fma_core_utils_slist_add_message( &parms->messages, "%s", _( "Item was renumbered because the caller did not provide any ask user function." ));
				parms->import_mode = IMPORTER_MODE_RENUMBER;
			}

		} else {
			mode = parms->asked_mode;
		}
	}

	/* mode is only set if asked mode is ask user and an ask function was provided
	 * or if asked mode was not ask user
	 */
	if( mode ){
		parms->import_mode = mode;

		switch( mode ){
			case IMPORTER_MODE_RENUMBER:
				renumber_label_item( parms );
				if( parms->asked_mode == IMPORTER_MODE_ASK ){
					fma_core_utils_slist_add_message( &parms->messages, "%s", _( "Item was renumbered due to user request." ));
				}
				break;

			case IMPORTER_MODE_OVERRIDE:
				if( parms->asked_mode == IMPORTER_MODE_ASK ){
					fma_core_utils_slist_add_message( &parms->messages, "%s", _( "Existing item was overridden due to user request." ));
				}
				break;

			case IMPORTER_MODE_NO_IMPORT:
			default:
				id = fma_object_get_id( parms->imported );
				fma_core_utils_slist_add_message( &parms->messages, _( "Item %s already exists." ), id );
				if( parms->asked_mode == IMPORTER_MODE_ASK ){
					fma_core_utils_slist_add_message( &parms->messages, "%s", _( "Import was canceled due to user request." ));
				}
				g_free( id );
				code = IMPORTER_CODE_CANCELLED;
		}
	}

	return( code );
}

/*
 * renumber the item, and set a new label
 */
static void
renumber_label_item( FMAIImporterManageImportModeParms *parms )
{
	gchar *label, *tmp;

	fma_object_set_new_id( parms->imported, NULL );

	label = fma_object_get_label( parms->imported );

	/* i18n: the action has been renumbered during import operation */
	tmp = g_strdup_printf( "%s %s", label, _( "(renumbered)" ));

	fma_object_set_label( parms->imported, tmp );

	g_free( tmp );
	g_free( label );
}
#endif /* FMA_ENABLE_DEPRECATED */