* more private data for the editor window: identity and transport
[modest] / src / modest-tny-stream-gtkhtml.c
1 /* modest-tny-stream-gtkhtml.c */
2
3 /* insert (c)/licensing information) */
4
5 #include "modest-tny-stream-gtkhtml.h"
6 #include <gtkhtml/gtkhtml-stream.h>
7 #include <gtkhtml/gtkhtml-search.h>
8
9 /* 'private'/'protected' functions */
10 static void  modest_tny_stream_gtkhtml_class_init   (ModestTnyStreamGtkhtmlClass *klass);
11 static void  modest_tny_stream_gtkhtml_init         (ModestTnyStreamGtkhtml *obj);
12 static void  modest_tny_stream_gtkhtml_finalize     (GObject *obj);
13
14 static void  modest_tny_stream_gtkhml_iface_init (gpointer g_iface, gpointer iface_data);
15
16 /* list my signals */
17 enum {
18         /* MY_SIGNAL_1, */
19         /* MY_SIGNAL_2, */
20         LAST_SIGNAL
21 };
22
23 typedef struct _ModestTnyStreamGtkhtmlPrivate ModestTnyStreamGtkhtmlPrivate;
24 struct _ModestTnyStreamGtkhtmlPrivate {
25         GtkHTMLStream *stream;
26 };
27 #define MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
28                                                        MODEST_TYPE_TNY_STREAM_GTKHTML, \
29                                                        ModestTnyStreamGtkhtmlPrivate))
30 /* globals */
31 static GObjectClass *parent_class = NULL;
32
33 /* uncomment the following if you have defined any signals */
34 /* static guint signals[LAST_SIGNAL] = {0}; */
35
36 GType
37 modest_tny_stream_gtkhtml_get_type (void)
38 {
39         static GType my_type = 0;
40         if (!my_type) {
41                 static const GTypeInfo my_info = {
42                         sizeof(ModestTnyStreamGtkhtmlClass),
43                         NULL,           /* base init */
44                         NULL,           /* base finalize */
45                         (GClassInitFunc) modest_tny_stream_gtkhtml_class_init,
46                         NULL,           /* class finalize */
47                         NULL,           /* class data */
48                         sizeof(ModestTnyStreamGtkhtml),
49                         1,              /* n_preallocs */
50                         (GInstanceInitFunc) modest_tny_stream_gtkhtml_init,
51                 };
52
53                 static const GInterfaceInfo iface_info = {
54                         (GInterfaceInitFunc) modest_tny_stream_gtkhml_iface_init,
55                         NULL,         /* interface_finalize */
56                         NULL          /* interface_data */
57                 };
58
59                 my_type = g_type_register_static (G_TYPE_OBJECT,
60                                                   "ModestTnyStreamGtkhtml",
61                                                   &my_info, 0);
62
63                 g_type_add_interface_static (my_type, TNY_TYPE_STREAM_IFACE,
64                                              &iface_info);
65
66         }
67         return my_type;
68 }
69
70 static void
71 modest_tny_stream_gtkhtml_class_init (ModestTnyStreamGtkhtmlClass *klass)
72 {
73         GObjectClass *gobject_class;
74         gobject_class = (GObjectClass*) klass;
75
76         parent_class            = g_type_class_peek_parent (klass);
77         gobject_class->finalize = modest_tny_stream_gtkhtml_finalize;
78
79         g_type_class_add_private (gobject_class, sizeof(ModestTnyStreamGtkhtmlPrivate));
80 }
81
82 static void
83 modest_tny_stream_gtkhtml_init (ModestTnyStreamGtkhtml *obj)
84 {
85         ModestTnyStreamGtkhtmlPrivate *priv;
86         priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(obj);
87
88         priv->stream  = NULL;
89 }
90
91 static void
92 modest_tny_stream_gtkhtml_finalize (GObject *obj)
93 {
94         ModestTnyStreamGtkhtmlPrivate *priv;
95
96         priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(obj);
97         priv->stream = NULL;
98 }
99
100 GObject*
101 modest_tny_stream_gtkhtml_new (GtkHTMLStream *stream)
102 {
103         GObject *obj;
104         ModestTnyStreamGtkhtmlPrivate *priv;
105         
106         obj  = G_OBJECT(g_object_new(MODEST_TYPE_TNY_STREAM_GTKHTML, NULL));
107         priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(obj);
108
109         g_return_val_if_fail (stream, NULL);
110         
111         priv->stream = stream;
112
113         return obj;
114 }
115
116
117 /* the rest are interface functions */
118
119
120 static ssize_t
121 gtkhtml_read (TnyStreamIface *self, char *buffer, size_t n)
122 {
123         return -1; /* we cannot read */
124 }
125
126
127 static ssize_t
128 gtkhtml_write (TnyStreamIface *self, const char *buffer, size_t n)
129 {
130         ModestTnyStreamGtkhtmlPrivate *priv;
131         
132         priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(self);
133
134         if (!priv->stream) {
135                 g_warning ("cannot write to closed stream");
136                 return 0;
137         }
138
139         if (n == 0)
140                 return 0;
141                 
142         gtk_html_stream_write (priv->stream, buffer, n);
143         return n; /* hmmm */
144 }
145
146         
147 static gint
148 gtkhtml_flush (TnyStreamIface *self)
149 {
150         return 0;
151 }
152         
153
154 static gint
155 gtkhtml_close (TnyStreamIface *self)
156 {
157         ModestTnyStreamGtkhtmlPrivate *priv;
158         g_return_val_if_fail (self, 0);
159         priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(self);
160         
161         gtk_html_stream_close   (priv->stream, GTK_HTML_STREAM_OK);
162         priv->stream = NULL;
163
164         return 0;
165 }
166
167
168 static gboolean
169 gtkhtml_eos (TnyStreamIface *self)
170 {
171         return TRUE;
172 }
173
174
175         
176 static gint
177 gtkhtml_reset (TnyStreamIface *self)
178 {
179         return 0;
180 }
181
182         
183 static ssize_t
184 gtkhtml_write_to_stream (TnyStreamIface *self, TnyStreamIface *output)
185 {
186         return 0;
187 }
188
189
190 static void
191 modest_tny_stream_gtkhml_iface_init (gpointer g_iface, gpointer iface_data)
192 {
193         TnyStreamIfaceClass *klass;
194         
195         g_return_if_fail (g_iface);
196
197         klass = (TnyStreamIfaceClass *)g_iface;
198         
199         klass->read_func  = gtkhtml_read;
200         klass->write_func = gtkhtml_write;
201         klass->flush_func = gtkhtml_flush;
202         klass->close_func = gtkhtml_close;
203         klass->eos_func   = gtkhtml_eos;
204         klass->reset_func = gtkhtml_reset;
205         klass->write_to_stream_func = gtkhtml_write_to_stream;
206 }