Set Nautilus-Actions as being the actual official product name
[nautilus-actions.git] / src / core / na-exporter.c
blob5c3b22c6bd8770ffcc412bf0d55228d72298297d
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 "na-exporter.h"
38 #include "na-export-format.h"
40 extern gboolean iexporter_initialized;
41 extern gboolean iexporter_finalized;
43 static const NAIExporterFormat *exporter_get_formats( const NAIExporter *exporter );
44 static gchar *exporter_get_name( const NAIExporter *exporter );
45 static NAIExporter *find_exporter_for_format( const NAPivot *pivot, GQuark format );
48 * na_exporter_get_formats:
49 * @pivot: the #NAPivot instance.
51 * Returns: a list of #NAExportFormat objects, each of them addressing an
52 * available export format, i.e. a format provided by a module which
53 * implement the #NAIExporter interface.
55 * The returned list should later be na_exporter_free_formats() by the caller.
57 GList *
58 na_exporter_get_formats( const NAPivot *pivot )
60 GList *iexporters, *imod;
61 GList *formats;
62 const NAIExporterFormat *str;
63 NAExportFormat *format;
65 g_return_val_if_fail( NA_IS_PIVOT( pivot ), NULL );
67 formats = NULL;
69 if( iexporter_initialized && !iexporter_finalized ){
71 iexporters = na_pivot_get_providers( pivot, NA_IEXPORTER_TYPE );
72 for( imod = iexporters ; imod ; imod = imod->next ){
74 str = exporter_get_formats( NA_IEXPORTER( imod->data ));
75 while( str->format ){
77 format = na_export_format_new( str, NA_IEXPORTER( imod->data ));
78 formats = g_list_prepend( formats, format );
79 str++;
83 na_pivot_free_providers( iexporters );
86 return( formats );
90 * na_exporter_free_formats:
91 * @formats: a list of available export formats, as returned by
92 * #na_exporter_get_formats().
94 * Release the @formats #GList.
96 void
97 na_exporter_free_formats( GList *formats )
99 g_list_foreach( formats, ( GFunc ) g_object_unref, NULL );
100 g_list_free( formats );
104 * na_exporter_to_buffer:
105 * @pivot: the #NAPivot pivot for the running application.
106 * @item: a #NAObjectItem-derived object.
107 * @format: the #GQuark target format.
108 * @messages: a pointer to a #GSList list of strings; the provider
109 * may append messages to this list, but shouldn't reinitialize it.
111 * Exports the specified @item in the required @format.
113 * Returns: the output buffer, as a newly allocated string which should
114 * be g_free() by the caller, or %NULL if an error has been detected.
116 gchar *
117 na_exporter_to_buffer( const NAPivot *pivot, const NAObjectItem *item, GQuark format, GSList **messages )
119 static const gchar *thisfn = "na_exporter_to_buffer";
120 gchar *buffer;
121 NAIExporterBufferParms parms;
122 NAIExporter *exporter;
123 gchar *name;
124 gchar *msg;
126 g_return_val_if_fail( NA_IS_PIVOT( pivot ), NULL );
127 g_return_val_if_fail( NA_IS_OBJECT_ITEM( item ), NULL );
129 buffer = NULL;
131 if( iexporter_initialized && !iexporter_finalized ){
133 g_debug( "%s: pivot=%p, item=%p (%s), format=%u (%s), messages=%p",
134 thisfn,
135 ( void * ) pivot,
136 ( void * ) item, G_OBJECT_TYPE_NAME( item ),
137 ( guint ) format, g_quark_to_string( format ),
138 ( void * ) messages );
140 exporter = find_exporter_for_format( pivot, format );
141 g_debug( "%s: exporter=%p (%s)", thisfn, ( void * ) exporter, G_OBJECT_TYPE_NAME( exporter ));
143 if( exporter ){
144 parms.version = 1;
145 parms.exported = ( NAObjectItem * ) item;
146 parms.format = format;
147 parms.buffer = NULL;
148 parms.messages = messages ? *messages : NULL;
150 if( NA_IEXPORTER_GET_INTERFACE( exporter )->to_buffer ){
151 NA_IEXPORTER_GET_INTERFACE( exporter )->to_buffer( exporter, &parms );
153 if( parms.buffer ){
154 buffer = parms.buffer;
157 } else {
158 name = exporter_get_name( exporter );
159 msg = g_strdup_printf( _( "NAIExporter %s doesn't implement 'to_buffer' interface." ), name );
160 *messages = g_slist_append( *messages, msg );
161 g_free( name );
164 } else {
165 msg = g_strdup_printf(
166 _( "No NAIExporter implementation found for %s format." ), g_quark_to_string( format ));
167 *messages = g_slist_append( *messages, msg );
171 return( buffer );
175 * na_exporter_to_file:
176 * @pivot: the #NAPivot pivot for the running application.
177 * @item: a #NAObjectItem-derived object.
178 * @folder_uri: the URI of the target folder.
179 * @format: the #GQuark target format.
180 * @messages: a pointer to a #GSList list of strings; the provider
181 * may append messages to this list, but shouldn't reinitialize it.
183 * Exports the specified @item to the target @uri in the required @format.
185 * Returns: the URI of the exported file, as a newly allocated string which
186 * should be g_free() by the caller, or %NULL if an error has been detected.
188 gchar *
189 na_exporter_to_file( const NAPivot *pivot, const NAObjectItem *item, const gchar *folder_uri, GQuark format, GSList **messages )
191 static const gchar *thisfn = "na_exporter_to_file";
192 gchar *export_uri;
193 NAIExporterFileParms parms;
194 NAIExporter *exporter;
195 gchar *msg;
196 gchar *name;
198 g_return_val_if_fail( NA_IS_PIVOT( pivot ), NULL );
199 g_return_val_if_fail( NA_IS_OBJECT_ITEM( item ), NULL );
201 export_uri = NULL;
203 if( iexporter_initialized && !iexporter_finalized ){
205 g_debug( "%s: pivot=%p, item=%p (%s), folder_uri=%s, format=%u (%s), messages=%p",
206 thisfn,
207 ( void * ) pivot,
208 ( void * ) item, G_OBJECT_TYPE_NAME( item ),
209 folder_uri,
210 ( guint ) format, g_quark_to_string( format ),
211 ( void * ) messages );
213 exporter = find_exporter_for_format( pivot, format );
215 if( exporter ){
216 parms.version = 1;
217 parms.exported = ( NAObjectItem * ) item;
218 parms.folder = ( gchar * ) folder_uri;
219 parms.format = format;
220 parms.basename = NULL;
221 parms.messages = messages ? *messages : NULL;
223 if( NA_IEXPORTER_GET_INTERFACE( exporter )->to_file ){
224 NA_IEXPORTER_GET_INTERFACE( exporter )->to_file( exporter, &parms );
226 if( parms.basename ){
227 export_uri = g_strdup_printf( "%s%s%s", folder_uri, G_DIR_SEPARATOR_S, parms.basename );
230 } else {
231 name = exporter_get_name( exporter );
232 msg = g_strdup_printf( _( "NAIExporter %s doesn't implement 'to_file' interface." ), name );
233 *messages = g_slist_append( *messages, msg );
234 g_free( name );
237 } else {
238 msg = g_strdup_printf(
239 _( "No NAIExporter implementation found for %s format." ), g_quark_to_string( format ));
240 *messages = g_slist_append( *messages, msg );
244 return( export_uri );
247 static const NAIExporterFormat *
248 exporter_get_formats( const NAIExporter *exporter )
250 const NAIExporterFormat *str;
252 str = NULL;
254 if( NA_IEXPORTER_GET_INTERFACE( exporter )->get_formats ){
255 str = NA_IEXPORTER_GET_INTERFACE( exporter )->get_formats( exporter );
258 return( str );
261 static gchar *
262 exporter_get_name( const NAIExporter *exporter )
264 gchar *name;
266 name = NULL;
268 if( NA_IEXPORTER_GET_INTERFACE( exporter )->get_name ){
269 name = NA_IEXPORTER_GET_INTERFACE( exporter )->get_name( exporter );
272 return( name );
275 static NAIExporter *
276 find_exporter_for_format( const NAPivot *pivot, GQuark format )
278 NAIExporter *exporter;
279 GList *formats, *ifmt;
281 exporter = NULL;
282 formats = na_exporter_get_formats( pivot );
284 for( ifmt = formats ; ifmt && !exporter ; ifmt = ifmt->next ){
286 if( na_export_format_get_quark( NA_EXPORT_FORMAT( ifmt->data )) == format ){
287 exporter = na_export_format_get_exporter( NA_EXPORT_FORMAT( ifmt->data ));
291 na_exporter_free_formats( formats );
293 return( exporter );