]> matita.cs.unibo.it Git - helm.git/blob - helm/gtkmathview-bonobo/src/persist-stream.c
* removed wrong unref
[helm.git] / helm / gtkmathview-bonobo / src / persist-stream.c
1 #include <bonobo.h>
2 #include <gtkmathview.h>
3 #include <glib.h>
4 #include "persist-stream.h"
5 #include "control-factory.h"
6
7 #define DEBUG
8
9 static BonoboObjectClass *gtk_math_view_persist_stream_parent_class;
10
11 static void load_implementation(PortableServer_Servant servant,
12                                 const Bonobo_Stream stream,
13                                 const CORBA_char *type,
14                                 CORBA_Environment *ev);
15
16 static void save_implementation(PortableServer_Servant servant,
17                                 const Bonobo_Stream stream,
18                                 const CORBA_char *type,
19                                 CORBA_Environment *ev);
20
21 static void finalize(GObject *object)
22 {
23     GtkMathViewPersistStream *stream = GTK_MATH_VIEW_PERSIST_STREAM(object);
24
25     if(stream->math_view){
26         g_object_unref(stream->math_view);
27         stream->math_view = NULL;
28     }
29
30     G_OBJECT_CLASS(gtk_math_view_persist_stream_parent_class)->finalize(object);
31 }
32
33 static Bonobo_Persist_ContentTypeList *
34 get_content_types(BonoboPersist *persist,CORBA_Environment *ev)
35 {
36   return bonobo_persist_generate_content_types(2, "application/mathml+xml", "text/mathml");
37 }
38
39 static void 
40 gtk_math_view_persist_stream_class_init(GtkMathViewPersistStreamClass *klass)
41 {
42     GObjectClass *object_class = G_OBJECT_CLASS(klass);
43     BonoboPersistClass *persist_class = BONOBO_PERSIST_CLASS(klass);
44     POA_Bonobo_PersistStream__epv *epv = &klass->epv;
45
46 #ifdef DEBUG
47     printf("persist stream class init\n");
48 #endif
49     gtk_math_view_persist_stream_parent_class = g_type_class_peek_parent(klass);
50
51     epv->load = load_implementation;
52     epv->save = save_implementation;
53
54     object_class->finalize = finalize;
55     persist_class->get_content_types = get_content_types;
56 }
57
58 GType
59 gtk_math_view_persist_stream_get_type(void)
60 {
61     static GType type = 0;
62 #ifdef DEBUG
63     printf("persist stream get type\n");
64 #endif
65     if(!type){
66         GTypeInfo info={
67             sizeof(GtkMathViewPersistStreamClass),
68             (GBaseInitFunc) NULL,
69             (GBaseFinalizeFunc) NULL,
70             (GClassInitFunc) gtk_math_view_persist_stream_class_init,
71             NULL,   /*class finalize */
72             NULL,   /*class data */
73             sizeof(GtkMathViewPersistStream),
74             0,  /* n_preallocs */
75             (GInstanceInitFunc) NULL
76         };
77
78         type = bonobo_type_unique(
79                                   BONOBO_TYPE_PERSIST,
80                                   POA_Bonobo_PersistStream__init,POA_Bonobo_PersistStream__fini,
81                                   G_STRUCT_OFFSET(GtkMathViewPersistStreamClass,epv),
82                                   &info,"GtkMathViewPersistStream");
83     }
84     return type;
85 }
86
87 BonoboObject *
88 gtk_math_view_persist_stream_new(GtkMathView *math_view)
89 {
90     BonoboObject *stream;
91
92 #ifdef DEBUG
93     printf("persist stream new\n");
94 #endif
95     stream = g_object_new(gtk_math_view_persist_stream_get_type(),NULL);
96     bonobo_persist_construct(BONOBO_PERSIST(stream),CONTROL_FACTORY_ID);
97
98     g_object_ref(math_view);
99     GTK_MATH_VIEW_PERSIST_STREAM(stream)->math_view = math_view;
100
101     return stream;
102 }
103
104 static FILE*
105 create_tmp_file(GtkMathViewPersistStream *persist)
106 {
107     FILE *tmpfile;
108     int fd;
109     
110     persist->tmp_path_name = g_strconcat(g_get_tmp_dir(), "/gmvXXXXXX", NULL);
111     if((fd = mkstemp(persist->tmp_path_name)) < 0) {
112         g_free(persist->tmp_path_name),
113         persist->tmp_path_name = NULL;
114         return NULL;
115     }
116     printf("salvo nel file %s\n", persist->tmp_path_name);
117     tmpfile = fdopen(fd, "w");
118     if(!tmpfile) {
119         close(fd);
120         return NULL;
121     }
122     return tmpfile;
123 }
124
125
126 static void 
127 load_implementation(PortableServer_Servant servant,
128                     Bonobo_PersistStream stream,
129                     const CORBA_char *type,
130                     CORBA_Environment *ev)
131 {
132     GtkMathViewPersistStream *persist = GTK_MATH_VIEW_PERSIST_STREAM (bonobo_object_from_servant (servant));
133     Bonobo_Stream_iobuf *buffer;
134     GtkMathViewPersistStream *handle;
135     CORBA_long len_read;
136     gboolean result;
137     FILE *tmpfile;
138
139 #ifdef DEBUG
140     printf("persist stream loading\n");
141 #endif
142     
143     printf("type = %s\n", type);
144
145     if (strcmp (type, "application/mathml+xml") != 0) {
146         CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
147                      ex_Bonobo_Persist_WrongDataType, NULL);
148         return;
149     }
150
151     tmpfile = create_tmp_file(persist);
152     do {
153         Bonobo_Stream_read (stream, 4096, &buffer, ev);
154         
155         if (ev->_major != CORBA_NO_EXCEPTION) {
156           printf("*** eccezione 1\n");
157           goto clean;
158         }
159
160         len_read = buffer->_length;
161         printf("letti %d bytes\n", len_read);
162
163         if (buffer->_buffer && len_read)
164             if(fwrite(buffer->_buffer, 1, len_read, tmpfile) != len_read) {
165                 CORBA_free (buffer);
166           printf("*** eccezione 2\n");
167                 goto clean;
168         }
169         
170         CORBA_free (buffer);
171     } while (len_read > 0);
172
173     fclose(tmpfile);
174     result = gtk_math_view_load_uri(persist->math_view,persist->tmp_path_name);
175     if(!result)
176     {
177         CORBA_exception_set(ev,CORBA_USER_EXCEPTION,ex_Bonobo_Persist_WrongDataType,NULL);
178     }
179     return ;
180
181 clean:
182     fclose (tmpfile);
183     return;
184 }
185
186 static void
187 save_implementation(PortableServer_Servant servant,const Bonobo_Stream stream,const CORBA_char *type,CORBA_Environment *ev)
188 {
189     bonobo_exception_set(ev,"save_exception");
190     bonobo_exception_add_handler_str("save_exception",
191                                      "Save option is not valid");
192     return;
193 }