]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/gtkmathview-bonobo/src/persist-stream.c
ocaml 3.09 transition
[helm.git] / helm / gtkmathview-bonobo / src / persist-stream.c
index 815606bde487a2764884bea46b79bcc4edea2f80..bef7d90cb6218bea4f7f953a955a2114137be7e2 100644 (file)
@@ -1,10 +1,35 @@
+/* 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 <glib.h>
+
 #include "persist-stream.h"
 #include "control-factory.h"
 
-#define DEBUG
+#define DEBUG0
 
 static BonoboObjectClass *gtk_math_view_persist_stream_parent_class;
 
@@ -20,106 +45,112 @@ static void save_implementation(PortableServer_Servant servant,
 
 static void finalize(GObject *object)
 {
-    GtkMathViewPersistStream *stream = GTK_MATH_VIEW_PERSIST_STREAM(object);
+  GtkMathViewPersistStream *stream = GTK_MATH_VIEW_PERSIST_STREAM(object);
 
-    if(stream->math_view){
-        g_object_unref(stream->math_view);
-        stream->math_view = NULL;
+  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);
+  G_OBJECT_CLASS(gtk_math_view_persist_stream_parent_class)->finalize(object);
 }
 
 static Bonobo_Persist_ContentTypeList *
 get_content_types(BonoboPersist *persist,CORBA_Environment *ev)
 {
-       //FIXME:il tipo di dato non e` noto
-       return bonobo_persist_generate_content_types(0);
+  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;
+  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");
+  printf("persist stream class init\n");
 #endif
-    gtk_math_view_persist_stream_parent_class = g_type_class_peek_parent(klass);
+  gtk_math_view_persist_stream_parent_class = g_type_class_peek_parent(klass);
 
-    epv->load = load_implementation;
-    epv->save = save_implementation;
+  epv->load = load_implementation;
+  epv->save = save_implementation;
 
-    object_class->finalize = finalize;
-    persist_class->get_content_types = get_content_types;
+  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;
+  static GType type = 0;
 #ifdef DEBUG
-    printf("persist stream get type\n");
+  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");
+  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;
+
+  return type;
 }
 
 BonoboObject *
 gtk_math_view_persist_stream_new(GtkMathView *math_view)
 {
-    BonoboObject *stream;
+  BonoboObject *stream;
 
 #ifdef DEBUG
-    printf("persist stream new\n");
+  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);
+  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;
+  g_object_ref(math_view);
+  GTK_MATH_VIEW_PERSIST_STREAM(stream)->math_view = math_view;
 
-    return stream;
+  return stream;
 }
 
 static FILE*
 create_tmp_file(GtkMathViewPersistStream *persist)
 {
-    FILE *tmpfile;
-    int fd;
+  FILE *tmpfile;
+  int fd;
     
-    persist->tmp_path_name = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
-    if((fd = mkstemp(persist->tmp_path_name)) < 0) {
-        g_free(persist->tmp_path_name),
-        persist->tmp_path_name = NULL;
-        return NULL;
+  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;
+
+  tmpfile = fdopen(fd, "w");
+  if(!tmpfile)
+    {
+      close(fd);
+      return NULL;
     }
-    return tmpfile;
+
+  return tmpfile;
 }
 
 
@@ -129,55 +160,55 @@ load_implementation(PortableServer_Servant servant,
                     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;
+  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");
+  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;
+  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);
+    
+  tmpfile = create_tmp_file(persist);
+  do
+    {
+      Bonobo_Stream_read (stream, 4096, &buffer, ev);
+      if (ev->_major != CORBA_NO_EXCEPTION)
+       goto clean;
         
-        if (ev->_major != CORBA_NO_EXCEPTION || buffer->_length <= 0) {
-            CORBA_free (buffer);
-            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;
-        }
+      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 (1);
+      CORBA_free (buffer);
+    } while (len_read > 0);
+
+  fclose(tmpfile);
 
-    fclose(tmpfile);
-    result = gtk_math_view_load_uri(persist->math_view,persist->tmp_path_name);
-    if(!result)
+  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);
+      CORBA_exception_set(ev,CORBA_USER_EXCEPTION,ex_Bonobo_Persist_WrongDataType,NULL);
     }
-    //FIXME non sono sicuro di questo
-    bonobo_object_unref(BONOBO_OBJECT(stream));
-    return ;
+  return ;
 
-clean:
-    fclose (tmpfile);
-    return;
+ clean:
+  fclose (tmpfile);
+  return;
 }
 
 static void