[nautilus-actions] Export .desktop files



commit 652c501597317de8cf583aebca4307ed5d4f3b09
Author: Pierre Wieser <pwieser trychlos org>
Date:   Wed Jul 28 23:57:18 2010 +0200

    Export .desktop files

 ChangeLog                          |    8 ++
 src/io-desktop/nadp-desktop-file.c |   38 +++++++++++
 src/io-desktop/nadp-desktop-file.h |    2 +
 src/io-desktop/nadp-writer.c       |  126 +++++++++++++++++++++++-------------
 4 files changed, 129 insertions(+), 45 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index d9a0e15..241cfc5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2010-07-28 Pierre Wieser <pwieser trychlos org>
 
+	* src/io-desktop/nadp-desktop-file.c:
+	* src/io-desktop/nadp-desktop-file.h
+	(nadp_desktop_file_new, nadp_desktop_file_get_key_file):
+	New functions.
+
+	* src/io-desktop/nadp-writer.c
+	(nadp_writer_iexporter_export_to_buffer): Implement the function.
+
 	Import/export .desktop files.
 
 	* doc/export-format-id: Declare the new export format.
diff --git a/src/io-desktop/nadp-desktop-file.c b/src/io-desktop/nadp-desktop-file.c
index e44b00f..a6726cb 100644
--- a/src/io-desktop/nadp-desktop-file.c
+++ b/src/io-desktop/nadp-desktop-file.c
@@ -185,6 +185,21 @@ instance_finalize( GObject *object )
 }
 
 /**
+ * nadp_desktop_file_new:
+ *
+ * Retuns: a newly allocated #NadpDesktopFile object.
+ */
+NadpDesktopFile *
+nadp_desktop_file_new( void )
+{
+	NadpDesktopFile *ndf;
+
+	ndf = g_object_new( NADP_DESKTOP_FILE_TYPE, NULL );
+
+	return( ndf );
+}
+
+/**
  * nadp_desktop_file_new_from_path:
  * @path: the full pathname of a .desktop file.
  *
@@ -312,6 +327,29 @@ nadp_desktop_file_new_for_write( const gchar *path )
 }
 
 /**
+ * nadp_desktop_file_get_key_file:
+ * @ndf: the #NadpDesktopFile instance.
+ *
+ * Returns: a pointer to the internal #GKeyFile.
+ */
+GKeyFile *
+nadp_desktop_file_get_key_file( const NadpDesktopFile *ndf )
+{
+	GKeyFile *key_file;
+
+	g_return_val_if_fail( NADP_IS_DESKTOP_FILE( ndf ), NULL );
+
+	key_file = NULL;
+
+	if( !ndf->private->dispose_has_run ){
+
+		key_file = ndf->private->key_file;
+	}
+
+	return( key_file );
+}
+
+/**
  * nadp_desktop_file_get_key_file_uri:
  * @ndf: the #NadpDesktopFile instance.
  *
diff --git a/src/io-desktop/nadp-desktop-file.h b/src/io-desktop/nadp-desktop-file.h
index 812d019..f7aa0bc 100644
--- a/src/io-desktop/nadp-desktop-file.h
+++ b/src/io-desktop/nadp-desktop-file.h
@@ -74,10 +74,12 @@ typedef struct {
 
 GType            nadp_desktop_file_get_type         ( void );
 
+NadpDesktopFile *nadp_desktop_file_new              ( void );
 NadpDesktopFile *nadp_desktop_file_new_from_path    ( const gchar *path );
 NadpDesktopFile *nadp_desktop_file_new_from_uri     ( const gchar *uri );
 NadpDesktopFile *nadp_desktop_file_new_for_write    ( const gchar *path );
 
+GKeyFile        *nadp_desktop_file_get_key_file     ( const NadpDesktopFile *ndf );
 gchar           *nadp_desktop_file_get_key_file_uri ( const NadpDesktopFile *ndf );
 gboolean         nadp_desktop_file_write            ( NadpDesktopFile *ndf );
 
diff --git a/src/io-desktop/nadp-writer.c b/src/io-desktop/nadp-writer.c
index b8cbfa4..b74ad55 100644
--- a/src/io-desktop/nadp-writer.c
+++ b/src/io-desktop/nadp-writer.c
@@ -42,14 +42,33 @@
 
 #include "nadp-desktop-file.h"
 #include "nadp-desktop-provider.h"
+#include "nadp-formats.h"
 #include "nadp-keys.h"
 #include "nadp-utils.h"
 #include "nadp-writer.h"
 #include "nadp-xdg-dirs.h"
 
-static guint write_item( const NAIIOProvider *provider, const NAObjectItem *item, NadpDesktopFile *ndf, GSList **messages );
+/* the association between an export format and the functions
+ */
+typedef struct {
+	gchar  *format;
+	void   *foo;
+}
+	ExportFormatFn;
+
+static ExportFormatFn st_export_format_fn[] = {
+
+	{ NADP_FORMAT_DESKTOP_V1,
+					NULL },
 
-static void  desktop_weak_notify( NadpDesktopFile *ndf, GObject *item );
+	{ NULL }
+};
+
+static guint           write_item( const NAIIOProvider *provider, const NAObjectItem *item, NadpDesktopFile *ndf, GSList **messages );
+
+static void            desktop_weak_notify( NadpDesktopFile *ndf, GObject *item );
+
+static ExportFormatFn *find_export_format_fn( GQuark format );
 
 /*
  * This is implementation of NAIIOProvider::is_willing_to_write method
@@ -329,40 +348,41 @@ guint
 nadp_writer_iexporter_export_to_buffer( const NAIExporter *instance, NAIExporterBufferParms *parms )
 {
 	static const gchar *thisfn = "nadp_writer_iexporter_export_to_buffer";
-	guint code;
+	guint code, write_code;
+	ExportFormatFn *fmt;
+	GKeyFile *key_file;
+	NadpDesktopFile *ndf;
 
 	g_debug( "%s: instance=%p, parms=%p", thisfn, ( void * ) instance, ( void * ) parms );
 
+	parms->buffer = NULL;
 	code = NA_IEXPORTER_CODE_OK;
-	/*
-	NAXMLWriter *writer;
 
 	if( !parms->exported || !NA_IS_OBJECT_ITEM( parms->exported )){
 		code = NA_IEXPORTER_CODE_INVALID_ITEM;
 	}
 
 	if( code == NA_IEXPORTER_CODE_OK ){
-		writer = NAXML_WRITER( g_object_new( NAXML_WRITER_TYPE, NULL ));
-
-		writer->private->provider = ( NAIExporter * ) instance;
-		writer->private->exported = parms->exported;
-		writer->private->messages = parms->messages;
-		writer->private->fn_str = find_export_format_fn( parms->format );
-		writer->private->buffer = NULL;
+		fmt = find_export_format_fn( parms->format );
 
-		if( !writer->private->fn_str ){
+		if( !fmt ){
 			code = NA_IEXPORTER_CODE_INVALID_FORMAT;
 
 		} else {
-			code = writer_to_buffer( writer );
-			if( code == NA_IEXPORTER_CODE_OK ){
-				parms->buffer = writer->private->buffer;
+			ndf = nadp_desktop_file_new();
+			write_code = na_ifactory_provider_write_item( NA_IFACTORY_PROVIDER( instance ), ndf, NA_IFACTORY_OBJECT( parms->exported ), &parms->messages );
+
+			if( write_code != NA_IIO_PROVIDER_CODE_OK ){
+				code = NA_IEXPORTER_CODE_ERROR;
+
+			} else {
+				key_file = nadp_desktop_file_get_key_file( ndf );
+				parms->buffer = g_key_file_to_data( key_file, NULL, NULL );
 			}
-		}
 
-		g_object_unref( writer );
+			g_object_unref( ndf );
+		}
 	}
-	*/
 
 	g_debug( "%s: returning code=%u", thisfn, code );
 	return( code );
@@ -379,52 +399,49 @@ guint
 nadp_writer_iexporter_export_to_file( const NAIExporter *instance, NAIExporterFileParms *parms )
 {
 	static const gchar *thisfn = "nadp_writer_iexporter_export_to_file";
-	guint code;
+	guint code, write_code;
+	gchar *id, *folder_path, *dest_path;
+	ExportFormatFn *fmt;
+	NadpDesktopFile *ndf;
 
 	g_debug( "%s: instance=%p, parms=%p", thisfn, ( void * ) instance, ( void * ) parms );
 
+	parms->basename = NULL;
 	code = NA_IEXPORTER_CODE_OK;
 
-	/*
-	NAXMLWriter *writer;
-	gchar *filename;
-
 	if( !parms->exported || !NA_IS_OBJECT_ITEM( parms->exported )){
 		code = NA_IEXPORTER_CODE_INVALID_ITEM;
 	}
 
 	if( code == NA_IEXPORTER_CODE_OK ){
-		writer = NAXML_WRITER( g_object_new( NAXML_WRITER_TYPE, NULL ));
+		fmt = find_export_format_fn( parms->format );
 
-		writer->private->provider = ( NAIExporter * ) instance;
-		writer->private->exported = parms->exported;
-		writer->private->messages = parms->messages;
-		writer->private->fn_str = find_export_format_fn( parms->format );
-		writer->private->buffer = NULL;
-
-		if( !writer->private->fn_str ){
+		if( !fmt ){
 			code = NA_IEXPORTER_CODE_INVALID_FORMAT;
 
 		} else {
-			code = writer_to_buffer( writer );
+			id = na_object_get_id( parms->exported );
+			parms->basename = g_strdup_printf( "%s%s", id, NADP_DESKTOP_FILE_SUFFIX );
+			g_free( id );
 
-			if( code == NA_IEXPORTER_CODE_OK ){
-				filename = get_output_fname( parms->exported, parms->folder, parms->format );
+			folder_path = g_filename_from_uri( parms->folder, NULL, NULL );
+			dest_path = g_strdup_printf( "%s/%s", folder_path, parms->basename );
+			g_free( folder_path );
 
-				if( filename ){
-					parms->basename = g_path_get_basename( filename );
-					output_xml_to_file(
-							writer->private->buffer, filename, parms->messages ? &writer->private->messages : NULL );
-					g_free( filename );
-				}
+			ndf = nadp_desktop_file_new_for_write( dest_path );
+			write_code = na_ifactory_provider_write_item( NA_IFACTORY_PROVIDER( instance ), ndf, NA_IFACTORY_OBJECT( parms->exported ), &parms->messages );
+
+			if( write_code != NA_IIO_PROVIDER_CODE_OK ){
+				code = NA_IEXPORTER_CODE_ERROR;
+
+			} else if( !nadp_desktop_file_write( ndf )){
+				code = NA_IEXPORTER_CODE_UNABLE_TO_WRITE;
 			}
 
-			g_free( writer->private->buffer );
+			g_free( dest_path );
+			g_object_unref( ndf );
 		}
-
-		g_object_unref( writer );
 	}
-	*/
 
 	g_debug( "%s: returning code=%u", thisfn, code );
 	return( code );
@@ -575,3 +592,22 @@ nadp_writer_ifactory_provider_write_done( const NAIFactoryProvider *provider, vo
 
 	return( NA_IIO_PROVIDER_CODE_OK );
 }
+
+static ExportFormatFn *
+find_export_format_fn( GQuark format )
+{
+	ExportFormatFn *found;
+	ExportFormatFn *i;
+
+	found = NULL;
+	i = st_export_format_fn;
+
+	while( i->format && !found ){
+		if( g_quark_from_string( i->format ) == format ){
+			found = i;
+		}
+		i++;
+	}
+
+	return( found );
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]