]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/DEVEL/gtkmathview-bonobo/src/persist-stream.c
dir reorganization
[helm.git] / helm / DEVEL / gtkmathview-bonobo / src / persist-stream.c
diff --git a/helm/DEVEL/gtkmathview-bonobo/src/persist-stream.c b/helm/DEVEL/gtkmathview-bonobo/src/persist-stream.c
new file mode 100644 (file)
index 0000000..bef7d90
--- /dev/null
@@ -0,0 +1,221 @@
+/* This file is part of GtkMathView-Bonobo, a Bonobo wrapper for GtkMathView.
+ * Copyright (C) 2003 Luca Padovani <lpadovan@cs.unibo.it>
+ *                    Pouria Masoudi <pmasoudi@cs.unibo.it>
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * This library 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
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * For more information, please visit the project's home page
+ * http://helm.cs.unibo.it/gtkmathview-bonobo
+ * or send an email to <lpadovan@cs.unibo.it>
+ */
+
+#include <config.h>
+
+#include <bonobo.h>
+#include <gtkmathview.h>
+
+#include "persist-stream.h"
+#include "control-factory.h"
+
+#define DEBUG0
+
+static BonoboObjectClass *gtk_math_view_persist_stream_parent_class;
+
+static void load_implementation(PortableServer_Servant servant,
+                                const Bonobo_Stream stream,
+                                const CORBA_char *type,
+                                CORBA_Environment *ev);
+
+static void save_implementation(PortableServer_Servant servant,
+                                const Bonobo_Stream stream,
+                                const CORBA_char *type,
+                                CORBA_Environment *ev);
+
+static void finalize(GObject *object)
+{
+  GtkMathViewPersistStream *stream = GTK_MATH_VIEW_PERSIST_STREAM(object);
+
+  if (stream->math_view != NULL) 
+    {
+      g_object_unref(stream->math_view);
+      stream->math_view = NULL;
+    }
+
+  G_OBJECT_CLASS(gtk_math_view_persist_stream_parent_class)->finalize(object);
+}
+
+static Bonobo_Persist_ContentTypeList *
+get_content_types(BonoboPersist *persist,CORBA_Environment *ev)
+{
+  return bonobo_persist_generate_content_types(2, "application/mathml+xml", "text/mathml");
+}
+
+static void 
+gtk_math_view_persist_stream_class_init(GtkMathViewPersistStreamClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS(klass);
+  BonoboPersistClass *persist_class = BONOBO_PERSIST_CLASS(klass);
+  POA_Bonobo_PersistStream__epv *epv = &klass->epv;
+
+#ifdef DEBUG
+  printf("persist stream class init\n");
+#endif
+  gtk_math_view_persist_stream_parent_class = g_type_class_peek_parent(klass);
+
+  epv->load = load_implementation;
+  epv->save = save_implementation;
+
+  object_class->finalize = finalize;
+  persist_class->get_content_types = get_content_types;
+}
+
+GType
+gtk_math_view_persist_stream_get_type(void)
+{
+  static GType type = 0;
+#ifdef DEBUG
+  printf("persist stream get type\n");
+#endif
+  if (!type)
+    {
+      GTypeInfo info = 
+       {
+         sizeof(GtkMathViewPersistStreamClass),
+         (GBaseInitFunc) NULL,
+         (GBaseFinalizeFunc) NULL,
+         (GClassInitFunc) gtk_math_view_persist_stream_class_init,
+         NULL,   /*class finalize */
+         NULL,   /*class data */
+         sizeof(GtkMathViewPersistStream),
+         0,  /* n_preallocs */
+         (GInstanceInitFunc) NULL
+       };
+
+      type = bonobo_type_unique(BONOBO_TYPE_PERSIST,
+                               POA_Bonobo_PersistStream__init,POA_Bonobo_PersistStream__fini,
+                               G_STRUCT_OFFSET(GtkMathViewPersistStreamClass,epv),
+                               &info,"GtkMathViewPersistStream");
+    }
+
+  return type;
+}
+
+BonoboObject *
+gtk_math_view_persist_stream_new(GtkMathView *math_view)
+{
+  BonoboObject *stream;
+
+#ifdef DEBUG
+  printf("persist stream new\n");
+#endif
+  stream = g_object_new(gtk_math_view_persist_stream_get_type(),NULL);
+  bonobo_persist_construct(BONOBO_PERSIST(stream),CONTROL_FACTORY_ID);
+
+  g_object_ref(math_view);
+  GTK_MATH_VIEW_PERSIST_STREAM(stream)->math_view = math_view;
+
+  return stream;
+}
+
+static FILE*
+create_tmp_file(GtkMathViewPersistStream *persist)
+{
+  FILE *tmpfile;
+  int fd;
+    
+  persist->tmp_path_name = g_strconcat(g_get_tmp_dir(), "/gmvXXXXXX", NULL);
+  if ((fd = mkstemp(persist->tmp_path_name)) < 0)
+    {
+      g_free(persist->tmp_path_name),
+       persist->tmp_path_name = NULL;
+      return NULL;
+    }
+
+  tmpfile = fdopen(fd, "w");
+  if(!tmpfile)
+    {
+      close(fd);
+      return NULL;
+    }
+
+  return tmpfile;
+}
+
+
+static void 
+load_implementation(PortableServer_Servant servant,
+                    Bonobo_PersistStream stream,
+                    const CORBA_char *type,
+                    CORBA_Environment *ev)
+{
+  GtkMathViewPersistStream *persist = GTK_MATH_VIEW_PERSIST_STREAM (bonobo_object_from_servant (servant));
+  Bonobo_Stream_iobuf *buffer;
+  GtkMathViewPersistStream *handle;
+  CORBA_long len_read;
+  gboolean result;
+  FILE *tmpfile;
+
+#ifdef DEBUG
+  printf("persist stream loading\n");
+#endif
+    
+  if (strcmp (type, "application/mathml+xml") != 0)
+    {
+      CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
+                          ex_Bonobo_Persist_WrongDataType, NULL);
+      return;
+    }
+    
+  tmpfile = create_tmp_file(persist);
+  do
+    {
+      Bonobo_Stream_read (stream, 4096, &buffer, ev);
+      if (ev->_major != CORBA_NO_EXCEPTION)
+       goto clean;
+        
+      len_read = buffer->_length;
+
+      if (buffer->_buffer && len_read)
+       if (fwrite(buffer->_buffer, 1, len_read, tmpfile) != len_read)
+         {
+           CORBA_free (buffer);
+           goto clean;
+         }
+        
+      CORBA_free (buffer);
+    } while (len_read > 0);
+
+  fclose(tmpfile);
+
+  result = gtk_math_view_load_uri(persist->math_view,persist->tmp_path_name);
+  if(!result)
+    {
+      CORBA_exception_set(ev,CORBA_USER_EXCEPTION,ex_Bonobo_Persist_WrongDataType,NULL);
+    }
+  return ;
+
+ clean:
+  fclose (tmpfile);
+  return;
+}
+
+static void
+save_implementation(PortableServer_Servant servant,const Bonobo_Stream stream,const CORBA_char *type,CORBA_Environment *ev)
+{
+    bonobo_exception_set(ev,"save_exception");
+    bonobo_exception_add_handler_str("save_exception",
+                                     "Save option is not valid");
+    return;
+}