]> matita.cs.unibo.it Git - helm.git/blob - helm/gtkmathview-bonobo/src/persist-stream.c
* added first version of persist stream implementation (not yet working)
[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        //FIXME:il tipo di dato non e` noto
37        return bonobo_persist_generate_content_types(0);
38 }
39
40 static void 
41 gtk_math_view_persist_stream_class_init(GtkMathViewPersistStreamClass *klass)
42 {
43     GObjectClass *object_class = G_OBJECT_CLASS(klass);
44     BonoboPersistClass *persist_class = BONOBO_PERSIST_CLASS(klass);
45     POA_Bonobo_PersistStream__epv *epv = &klass->epv;
46
47 #ifdef DEBUG
48     printf("persist stream class init\n");
49 #endif
50     gtk_math_view_persist_stream_parent_class = g_type_class_peek_parent(klass);
51
52     epv->load = load_implementation;
53     epv->save = save_implementation;
54
55     object_class->finalize = finalize;
56     persist_class->get_content_types = get_content_types;
57 }
58
59 GType
60 gtk_math_view_persist_stream_get_type(void)
61 {
62     static GType type = 0;
63 #ifdef DEBUG
64     printf("persist stream get type\n");
65 #endif
66     if(!type){
67         GTypeInfo info={
68             sizeof(GtkMathViewPersistStreamClass),
69             (GBaseInitFunc) NULL,
70             (GBaseFinalizeFunc) NULL,
71             (GClassInitFunc) gtk_math_view_persist_stream_class_init,
72             NULL,   /*class finalize */
73             NULL,   /*class data */
74             sizeof(GtkMathViewPersistStream),
75             0,  /* n_preallocs */
76             (GInstanceInitFunc) NULL
77         };
78
79         type = bonobo_type_unique(
80                                   BONOBO_TYPE_PERSIST,
81                                   POA_Bonobo_PersistStream__init,POA_Bonobo_PersistStream__fini,
82                                   G_STRUCT_OFFSET(GtkMathViewPersistStreamClass,epv),
83                                   &info,"GtkMathViewPersistStream");
84     }
85     return type;
86 }
87
88 BonoboObject *
89 gtk_math_view_persist_stream_new(GtkMathView *math_view)
90 {
91     BonoboObject *stream;
92
93 #ifdef DEBUG
94     printf("persist stream new\n");
95 #endif
96     stream = g_object_new(gtk_math_view_persist_stream_get_type(),NULL);
97     bonobo_persist_construct(BONOBO_PERSIST(stream),CONTROL_FACTORY_ID);
98
99     g_object_ref(math_view);
100     GTK_MATH_VIEW_PERSIST_STREAM(stream)->math_view = math_view;
101
102     return stream;
103 }
104
105 static FILE*
106 create_tmp_file(GtkMathViewPersistStream *persist)
107 {
108     FILE *tmpfile;
109     int fd;
110     
111     persist->tmp_path_name = g_strconcat(g_get_tmp_dir(), "/ggvXXXXXX", NULL);
112     if((fd = mkstemp(persist->tmp_path_name)) < 0) {
113         g_free(persist->tmp_path_name),
114         persist->tmp_path_name = NULL;
115         return NULL;
116     }
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     if (strcmp (type, "application/mathml+xml") != 0) {
144         CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
145                      ex_Bonobo_Persist_WrongDataType, NULL);
146         return;
147     }
148
149     tmpfile = create_tmp_file(persist);
150     do {
151         Bonobo_Stream_read (stream, 4096, &buffer, ev);
152         
153         if (ev->_major != CORBA_NO_EXCEPTION || buffer->_length <= 0) {
154             CORBA_free (buffer);
155             goto clean;
156             }
157
158         len_read = buffer->_length;
159         if (buffer->_buffer && len_read)
160             if(fwrite(buffer->_buffer, 1, len_read, tmpfile) != len_read) {
161                 CORBA_free (buffer);
162                 goto clean;
163         }
164         
165         CORBA_free (buffer);
166     } while (1);
167
168     fclose(tmpfile);
169     result = gtk_math_view_load_uri(persist->math_view,persist->tmp_path_name);
170     if(!result)
171     {
172         CORBA_exception_set(ev,CORBA_USER_EXCEPTION,ex_Bonobo_Persist_WrongDataType,NULL);
173     }
174     //FIXME non sono sicuro di questo
175     bonobo_object_unref(BONOBO_OBJECT(stream));
176     return ;
177
178 clean:
179     fclose (tmpfile);
180     return;
181 }
182
183 static void
184 save_implementation(PortableServer_Servant servant,const Bonobo_Stream stream,const CORBA_char *type,CORBA_Environment *ev)
185 {
186     bonobo_exception_set(ev,"save_exception");
187     bonobo_exception_add_handler_str("save_exception",
188                                      "Save option is not valid");
189     return;
190 }