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

[PATCH] GNOME VFS support for dia



I wrote a quick patch for dia (CVS tree as of Nov 23) that adds support
for opening and manipulating GNOME VFS URIs.  I did this using libgnomevfs,
libgsf and the gsf_xml_parser_context function.  The new code is enabled
as long as the GNOME preprocessor variable is #define'd (--enable-gnome).

So now "dia file:///path/to/diagram.dia" should work, with the following
caveats:

1.  For some reason compression is not working (it only works with
uncompressed files).  If anyone has experience with gsf_input_gzip_new,
I could use your help!

2.  I have not yet looked at the import and export filters, but I suspect
they may need modification too.

Comments?

Here is the patch:

===============================================================================
diff --recursive -u dia-vanilla/app/app_procs.c dia/app/app_procs.c
--- dia-vanilla/app/app_procs.c	2003-11-17 15:41:24.000000000 -0600
+++ dia/app/app_procs.c	2003-11-23 19:51:28.000000000 -0600
@@ -39,6 +39,7 @@
 
 #ifdef GNOME
 #include <gnome.h>
+#include <libgnomevfs/gnome-vfs.h>
 #else
 #ifdef HAVE_POPT_H
 #include <popt.h>
@@ -221,6 +222,9 @@
   DDisplay *ddisp = NULL;
   Diagram *diagram = NULL;
   gboolean made_conversions = FALSE;
+#ifdef GNOME
+  GnomeVFSURI * vfsURI;
+#endif
 
   if (export_file_format) {
     char *export_file_name = NULL;
@@ -243,7 +247,16 @@
   } else if (out_file_name) {
     made_conversions |= do_convert(in_file_name, out_file_name, NULL);
   } else {
+#ifdef GNOME
+    if (!gnome_vfs_init()) {
+      message_error(_("Error initializing GNOME VFS.\n"));
+      return made_conversions;
+    }
+    vfsURI = gnome_vfs_uri_new (in_file_name);
+    if (gnome_vfs_uri_exists(vfsURI)) {
+#else
     if (g_file_test(in_file_name, G_FILE_TEST_EXISTS)) {
+#endif
       diagram = diagram_load (in_file_name, NULL);
     } else
       diagram = new_diagram (in_file_name);
diff --recursive -u dia-vanilla/app/load_save.c dia/app/load_save.c
--- dia-vanilla/app/load_save.c	2003-11-02 04:58:30.000000000 -0600
+++ dia/app/load_save.c	2003-11-23 21:33:50.000000000 -0600
@@ -20,6 +20,13 @@
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
+#ifdef GNOME
+#include <libgnomevfs/gnome-vfs.h>
+#include <gsf/gsf.h>
+#include <gsf/gsf-libxml.h>
+#include <gsf/gsf-input-gzip.h>
+#include <gsf-gnome/gsf-input-gnomevfs.h>
+#endif
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -303,7 +310,6 @@
 diagram_data_load(const char *filename, DiagramData *data, void* user_data)
 {
   GHashTable *objects_hash;
-  int fd;
   GList *list;
   xmlDocPtr doc;
   xmlNodePtr diagramdata;
@@ -313,12 +319,42 @@
   Layer *layer;
   xmlNsPtr namespace;
   gchar firstchar;
+#ifdef GNOME
+  GError *err;
+  GnomeVFSHandle * vfsHandle;
+  GnomeVFSResult vfsResult;
+  GnomeVFSFileSize vfsBytesRead;
+  GsfInputGnomeVFS * vfsInputPtr;
+  xmlParserCtxt * docCtxt;
+#else
+  int fd;
+#endif
 
   if (g_file_test (filename, G_FILE_TEST_IS_DIR)) {
     message_error(_("You must specify a file, not a directory.\n"));
     return FALSE;
   }
 
+#ifdef GNOME
+  if (!gnome_vfs_init()){
+    message_error(_("Error initializing GNOME VFS.\n"));
+    return FALSE;
+  }
+  //vfsResult = gnome_vfs_open(&vfsHandle, filename, GNOME_VFS_OPEN_READ); 
+  vfsResult = gnome_vfs_open(&vfsHandle, filename, GNOME_VFS_OPEN_READ); 
+  if (vfsResult != GNOME_VFS_OK){
+    message_error(_("Couldn't open: '%s' for reading.\n"), filename);
+    return FALSE;
+  }
+  vfsResult = gnome_vfs_read(vfsHandle, &firstchar, 1, &vfsBytesRead);
+  if (vfsResult == GNOME_VFS_OK && vfsBytesRead == 1){
+    data->is_compressed = (firstchar != '<');
+  } else {
+    /* Couldn't read a single char?  Set to default. */
+    data->is_compressed = prefs.new_diagram.compress_save; 
+  }
+  gnome_vfs_close(vfsHandle);
+#else
   fd = open(filename, O_RDONLY);
 
   if (fd==-1) {
@@ -335,8 +371,35 @@
   
   /* Note that this closing and opening means we can't read from a pipe */
   close(fd);
+#endif
 
+#ifdef GNOME
+  gsf_init();
+  vfsInputPtr = gsf_input_gnomevfs_new(filename, &err);
+  if (vfsInputPtr == NULL){
+    message_error(_("Error loading diagram %s.\n%s."),filename, err->message);
+    return FALSE;
+  }
+  if (data->is_compressed){
+    GsfInputGZip * gzipInputPtr;
+    gzipInputPtr = gsf_input_gzip_new(GSF_INPUT(vfsInputPtr), &err);
+    if (gzipInputPtr == NULL){
+      message_error(_("Error loading diagram %s.\n%s."),filename, err->message);
+      return FALSE;
+    }
+    docCtxt = gsf_xml_parser_context(GSF_INPUT(gzipInputPtr)); 
+  } else {
+    docCtxt = gsf_xml_parser_context(GSF_INPUT(vfsInputPtr)); 
+  }
+  if (docCtxt == NULL){
+    message_error(_("Error loading diagram %s.\nError initializing parser."),filename);
+    return FALSE;
+  }
+  xmlParseDocument(docCtxt);
+  doc = docCtxt->myDoc;
+#else
   doc = xmlDiaParseFile(filename);
+#endif
 
   if (doc == NULL){
     message_error(_("Error loading diagram %s.\nUnknown file type."),filename);
@@ -836,21 +899,13 @@
 diagram_data_save(DiagramData *data, const char *filename)
 {
   FILE *file;
-  char *bakname,*tmpname,*dirname,*p;
+  char *bakname,*tmpname;
   int mode,_umask;
   int fildes;
   int ret;
    
   /* build the temporary and backup file names */
-  dirname = g_strdup(filename);
-  p = strrchr((char *)dirname,G_DIR_SEPARATOR);
-  if (p) {
-    *(p+1) = 0;
-  } else {
-    g_free(dirname);
-    dirname = g_strdup("." G_DIR_SEPARATOR_S);
-  }
-  tmpname = g_strconcat(dirname,"__diaXXXXXX",NULL);
+  tmpname = g_strdup("/tmp/__diaXXXXXX");
   bakname = g_strconcat(filename,"~",NULL);
 
   /* open a temporary name, and fix the modes to match what fopen() would have
@@ -876,17 +931,20 @@
        "filename" if it existed. */
     unlink(tmpname);
     g_free(tmpname);
-    g_free(dirname);
     g_free(bakname);
     return FALSE;
   }
   /* save succeeded. We kill the old backup file, move the old file into 
      backup, and the temp file into the new saved file. */
+#ifdef GNOME
+  gnome_vfs_move(filename,bakname,TRUE);
+  ret = gnome_vfs_move(tmpname,filename,FALSE) != GNOME_VFS_OK;
+#else
   unlink(bakname);
   rename(filename,bakname);
   ret = rename(tmpname,filename);
+#endif
   g_free(tmpname);
-  g_free(dirname);
   g_free(bakname);
   return (ret?FALSE:TRUE);
 }
diff --recursive -u dia-vanilla/configure.in dia/configure.in
--- dia-vanilla/configure.in	2003-11-02 04:58:29.000000000 -0600
+++ dia/configure.in	2003-11-23 16:06:06.000000000 -0600
@@ -104,7 +104,7 @@
 AC_ARG_ENABLE(gnome,[  --enable-gnome          enable gnome code],
               GNOME=$enableval, GNOME=no)
 if test "$GNOME" = "yes" ; then
-    GTK_MODULES="$GTK_MODULES libgnome-2.0 libgnomeui-2.0"
+    GTK_MODULES="$GTK_MODULES libgnome-2.0 libgnomeui-2.0 libgsf-1 libgsf-gnome-1"
     AC_DEFINE(GNOME,1,[Define if building with GNOME support])
     AC_DEFINE_UNQUOTED(GNOME_ICONDIR, "${prefix}/share/pixmaps", [GNOME icon directory])
     have_gnome=true
===============================================================================

-- 
Mike

:wq



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index] Mail converted by Mofo Magic and the Flying D

 
All trademarks and copyrights are the property of their respective owners.

Other Directory Sites: SeekWonder | Directory Owners Forum

GuideSMACK