]> matita.cs.unibo.it Git - helm.git/blob - helm/gtkmathview-bonobo/src/persist-stream.c
Bug fixed: when an axiom was asked, an exception was raised (since the
[helm.git] / helm / gtkmathview-bonobo / src / persist-stream.c
1 /* This file is part of GtkMathView-Bonobo, a Bonobo wrapper for GtkMathView.
2  * Copyright (C) 2003 Luca Padovani <lpadovan@cs.unibo.it>
3  *                    Pouria Masoudi <pmasoudi@cs.unibo.it>
4  * 
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * 
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  * 
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  * For more information, please visit the project's home page
20  * http://helm.cs.unibo.it/gtkmathview-bonobo
21  * or send an email to <lpadovan@cs.unibo.it>
22  */
23
24 #include <config.h>
25
26 #include <bonobo.h>
27 #include <gtkmathview.h>
28
29 #include "persist-stream.h"
30 #include "control-factory.h"
31
32 #define DEBUG0
33
34 static BonoboObjectClass *gtk_math_view_persist_stream_parent_class;
35
36 static void load_implementation(PortableServer_Servant servant,
37                                 const Bonobo_Stream stream,
38                                 const CORBA_char *type,
39                                 CORBA_Environment *ev);
40
41 static void save_implementation(PortableServer_Servant servant,
42                                 const Bonobo_Stream stream,
43                                 const CORBA_char *type,
44                                 CORBA_Environment *ev);
45
46 static void finalize(GObject *object)
47 {
48   GtkMathViewPersistStream *stream = GTK_MATH_VIEW_PERSIST_STREAM(object);
49
50   if (stream->math_view != NULL) 
51     {
52       g_object_unref(stream->math_view);
53       stream->math_view = NULL;
54     }
55
56   G_OBJECT_CLASS(gtk_math_view_persist_stream_parent_class)->finalize(object);
57 }
58
59 static Bonobo_Persist_ContentTypeList *
60 get_content_types(BonoboPersist *persist,CORBA_Environment *ev)
61 {
62   return bonobo_persist_generate_content_types(2, "application/mathml+xml", "text/mathml");
63 }
64
65 static void 
66 gtk_math_view_persist_stream_class_init(GtkMathViewPersistStreamClass *klass)
67 {
68   GObjectClass *object_class = G_OBJECT_CLASS(klass);
69   BonoboPersistClass *persist_class = BONOBO_PERSIST_CLASS(klass);
70   POA_Bonobo_PersistStream__epv *epv = &klass->epv;
71
72 #ifdef DEBUG
73   printf("persist stream class init\n");
74 #endif
75   gtk_math_view_persist_stream_parent_class = g_type_class_peek_parent(klass);
76
77   epv->load = load_implementation;
78   epv->save = save_implementation;
79
80   object_class->finalize = finalize;
81   persist_class->get_content_types = get_content_types;
82 }
83
84 GType
85 gtk_math_view_persist_stream_get_type(void)
86 {
87   static GType type = 0;
88 #ifdef DEBUG
89   printf("persist stream get type\n");
90 #endif
91   if (!type)
92     {
93       GTypeInfo info = 
94         {
95           sizeof(GtkMathViewPersistStreamClass),
96           (GBaseInitFunc) NULL,
97           (GBaseFinalizeFunc) NULL,
98           (GClassInitFunc) gtk_math_view_persist_stream_class_init,
99           NULL,   /*class finalize */
100           NULL,   /*class data */
101           sizeof(GtkMathViewPersistStream),
102           0,  /* n_preallocs */
103           (GInstanceInitFunc) NULL
104         };
105
106       type = bonobo_type_unique(BONOBO_TYPE_PERSIST,
107                                 POA_Bonobo_PersistStream__init,POA_Bonobo_PersistStream__fini,
108                                 G_STRUCT_OFFSET(GtkMathViewPersistStreamClass,epv),
109                                 &info,"GtkMathViewPersistStream");
110     }
111
112   return type;
113 }
114
115 BonoboObject *
116 gtk_math_view_persist_stream_new(GtkMathView *math_view)
117 {
118   BonoboObject *stream;
119
120 #ifdef DEBUG
121   printf("persist stream new\n");
122 #endif
123   stream = g_object_new(gtk_math_view_persist_stream_get_type(),NULL);
124   bonobo_persist_construct(BONOBO_PERSIST(stream),CONTROL_FACTORY_ID);
125
126   g_object_ref(math_view);
127   GTK_MATH_VIEW_PERSIST_STREAM(stream)->math_view = math_view;
128
129   return stream;
130 }
131
132 static FILE*
133 create_tmp_file(GtkMathViewPersistStream *persist)
134 {
135   FILE *tmpfile;
136   int fd;
137     
138   persist->tmp_path_name = g_strconcat(g_get_tmp_dir(), "/gmvXXXXXX", NULL);
139   if ((fd = mkstemp(persist->tmp_path_name)) < 0)
140     {
141       g_free(persist->tmp_path_name),
142         persist->tmp_path_name = NULL;
143       return NULL;
144     }
145
146   tmpfile = fdopen(fd, "w");
147   if(!tmpfile)
148     {
149       close(fd);
150       return NULL;
151     }
152
153   return tmpfile;
154 }
155
156
157 static void 
158 load_implementation(PortableServer_Servant servant,
159                     Bonobo_PersistStream stream,
160                     const CORBA_char *type,
161                     CORBA_Environment *ev)
162 {
163   GtkMathViewPersistStream *persist = GTK_MATH_VIEW_PERSIST_STREAM (bonobo_object_from_servant (servant));
164   Bonobo_Stream_iobuf *buffer;
165   GtkMathViewPersistStream *handle;
166   CORBA_long len_read;
167   gboolean result;
168   FILE *tmpfile;
169
170 #ifdef DEBUG
171   printf("persist stream loading\n");
172 #endif
173     
174   if (strcmp (type, "application/mathml+xml") != 0)
175     {
176       CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
177                            ex_Bonobo_Persist_WrongDataType, NULL);
178       return;
179     }
180     
181   tmpfile = create_tmp_file(persist);
182   do
183     {
184       Bonobo_Stream_read (stream, 4096, &buffer, ev);
185       if (ev->_major != CORBA_NO_EXCEPTION)
186         goto clean;
187         
188       len_read = buffer->_length;
189
190       if (buffer->_buffer && len_read)
191         if (fwrite(buffer->_buffer, 1, len_read, tmpfile) != len_read)
192           {
193             CORBA_free (buffer);
194             goto clean;
195           }
196         
197       CORBA_free (buffer);
198     } while (len_read > 0);
199
200   fclose(tmpfile);
201
202   result = gtk_math_view_load_uri(persist->math_view,persist->tmp_path_name);
203   if(!result)
204     {
205       CORBA_exception_set(ev,CORBA_USER_EXCEPTION,ex_Bonobo_Persist_WrongDataType,NULL);
206     }
207   return ;
208
209  clean:
210   fclose (tmpfile);
211   return;
212 }
213
214 static void
215 save_implementation(PortableServer_Servant servant,const Bonobo_Stream stream,const CORBA_char *type,CORBA_Environment *ev)
216 {
217     bonobo_exception_set(ev,"save_exception");
218     bonobo_exception_add_handler_str("save_exception",
219                                      "Save option is not valid");
220     return;
221 }