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