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>
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.
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.
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
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>
27 #include <gtkmathview.h>
29 #include "persist-stream.h"
30 #include "control-factory.h"
34 static BonoboObjectClass *gtk_math_view_persist_stream_parent_class;
36 static void load_implementation(PortableServer_Servant servant,
37 const Bonobo_Stream stream,
38 const CORBA_char *type,
39 CORBA_Environment *ev);
41 static void save_implementation(PortableServer_Servant servant,
42 const Bonobo_Stream stream,
43 const CORBA_char *type,
44 CORBA_Environment *ev);
46 static void finalize(GObject *object)
48 GtkMathViewPersistStream *stream = GTK_MATH_VIEW_PERSIST_STREAM(object);
50 if (stream->math_view != NULL)
52 g_object_unref(stream->math_view);
53 stream->math_view = NULL;
56 G_OBJECT_CLASS(gtk_math_view_persist_stream_parent_class)->finalize(object);
59 static Bonobo_Persist_ContentTypeList *
60 get_content_types(BonoboPersist *persist,CORBA_Environment *ev)
62 return bonobo_persist_generate_content_types(2, "application/mathml+xml", "text/mathml");
66 gtk_math_view_persist_stream_class_init(GtkMathViewPersistStreamClass *klass)
68 GObjectClass *object_class = G_OBJECT_CLASS(klass);
69 BonoboPersistClass *persist_class = BONOBO_PERSIST_CLASS(klass);
70 POA_Bonobo_PersistStream__epv *epv = &klass->epv;
73 printf("persist stream class init\n");
75 gtk_math_view_persist_stream_parent_class = g_type_class_peek_parent(klass);
77 epv->load = load_implementation;
78 epv->save = save_implementation;
80 object_class->finalize = finalize;
81 persist_class->get_content_types = get_content_types;
85 gtk_math_view_persist_stream_get_type(void)
87 static GType type = 0;
89 printf("persist stream get type\n");
95 sizeof(GtkMathViewPersistStreamClass),
97 (GBaseFinalizeFunc) NULL,
98 (GClassInitFunc) gtk_math_view_persist_stream_class_init,
99 NULL, /*class finalize */
100 NULL, /*class data */
101 sizeof(GtkMathViewPersistStream),
103 (GInstanceInitFunc) NULL
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");
116 gtk_math_view_persist_stream_new(GtkMathView *math_view)
118 BonoboObject *stream;
121 printf("persist stream new\n");
123 stream = g_object_new(gtk_math_view_persist_stream_get_type(),NULL);
124 bonobo_persist_construct(BONOBO_PERSIST(stream),CONTROL_FACTORY_ID);
126 g_object_ref(math_view);
127 GTK_MATH_VIEW_PERSIST_STREAM(stream)->math_view = math_view;
133 create_tmp_file(GtkMathViewPersistStream *persist)
138 persist->tmp_path_name = g_strconcat(g_get_tmp_dir(), "/gmvXXXXXX", NULL);
139 if ((fd = mkstemp(persist->tmp_path_name)) < 0)
141 g_free(persist->tmp_path_name),
142 persist->tmp_path_name = NULL;
146 tmpfile = fdopen(fd, "w");
158 load_implementation(PortableServer_Servant servant,
159 Bonobo_PersistStream stream,
160 const CORBA_char *type,
161 CORBA_Environment *ev)
163 GtkMathViewPersistStream *persist = GTK_MATH_VIEW_PERSIST_STREAM (bonobo_object_from_servant (servant));
164 Bonobo_Stream_iobuf *buffer;
165 GtkMathViewPersistStream *handle;
171 printf("persist stream loading\n");
174 if (strcmp (type, "application/mathml+xml") != 0)
176 CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
177 ex_Bonobo_Persist_WrongDataType, NULL);
181 tmpfile = create_tmp_file(persist);
184 Bonobo_Stream_read (stream, 4096, &buffer, ev);
185 if (ev->_major != CORBA_NO_EXCEPTION)
188 len_read = buffer->_length;
190 if (buffer->_buffer && len_read)
191 if (fwrite(buffer->_buffer, 1, len_read, tmpfile) != len_read)
198 } while (len_read > 0);
202 result = gtk_math_view_load_uri(persist->math_view,persist->tmp_path_name);
205 CORBA_exception_set(ev,CORBA_USER_EXCEPTION,ex_Bonobo_Persist_WrongDataType,NULL);
215 save_implementation(PortableServer_Servant servant,const Bonobo_Stream stream,const CORBA_char *type,CORBA_Environment *ev)
217 bonobo_exception_set(ev,"save_exception");
218 bonobo_exception_add_handler_str("save_exception",
219 "Save option is not valid");