8345a2788be59b520407e4ce85df067af8f9765a
[modest] / src / widgets / modest-mozembed-mime-part-view.c
1 /* Copyright (c) 2006, 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <widgets/modest-mozembed-mime-part-view.h>
31 #include <string.h>
32 #include <tny-stream.h>
33 #include <tny-mime-part-view.h>
34 #include <tny-gtk-text-buffer-stream.h>
35 #include <tny-moz-embed-html-mime-part-view.h>
36 #include <modest-text-utils.h>
37 #include <widgets/modest-mime-part-view.h>
38 #include <widgets/modest-zoomable.h>
39
40 /* gobject structure methods */
41 static void    modest_mozembed_mime_part_view_class_init (ModestMozembedMimePartViewClass *klass);
42 static void    modest_mime_part_view_init               (gpointer g, gpointer iface_data);
43 static void    modest_zoomable_init                     (gpointer g, gpointer iface_data);
44 static void    modest_isearch_view_init                 (gpointer g, gpointer iface_data);
45 static void    modest_mozembed_mime_part_view_init       (ModestMozembedMimePartView *self);
46 static void    modest_mozembed_mime_part_view_finalize   (GObject *self);
47
48 /* ModestMimePartView implementation */
49 static gboolean modest_mozembed_mime_part_view_is_empty (ModestMimePartView *self);
50 static gboolean modest_mozembed_mime_part_view_is_empty_default (ModestMimePartView *self);
51 /* ModestZoomable implementation */
52 static gdouble modest_mozembed_mime_part_view_get_zoom (ModestZoomable *self);
53 static void modest_mozembed_mime_part_view_set_zoom (ModestZoomable *self, gdouble value);
54 static gboolean modest_mozembed_mime_part_view_zoom_minus (ModestZoomable *self);
55 static gboolean modest_mozembed_mime_part_view_zoom_plus (ModestZoomable *self);
56 static gdouble modest_mozembed_mime_part_view_get_zoom_default (ModestZoomable *self);
57 static void modest_mozembed_mime_part_view_set_zoom_default (ModestZoomable *self, gdouble value);
58 static gboolean modest_mozembed_mime_part_view_zoom_minus_default (ModestZoomable *self);
59 static gboolean modest_mozembed_mime_part_view_zoom_plus_default (ModestZoomable *self);
60 /* ModestISearchView implementation */
61 static gboolean modest_mozembed_mime_part_view_search                    (ModestISearchView *self, const gchar *string);
62 static gboolean modest_mozembed_mime_part_view_search_next               (ModestISearchView *self);
63 static gboolean modest_mozembed_mime_part_view_get_selection_area        (ModestISearchView *self, gint *x, gint *y, 
64                                                                          gint *width, gint *height);
65 static gboolean modest_mozembed_mime_part_view_search_default            (ModestISearchView *self, const gchar *string);
66 static gboolean modest_mozembed_mime_part_view_search_next_default       (ModestISearchView *self);
67 static gboolean modest_mozembed_mime_part_view_get_selection_area_default (ModestISearchView *self, gint *x, gint *y, 
68                                                                           gint *width, gint *height);
69
70
71 /* internal api */
72 static gboolean      is_empty   (ModestMozembedMimePartView *self);
73 static void          set_zoom   (ModestMozembedMimePartView *self, gdouble zoom);
74 static gdouble       get_zoom   (ModestMozembedMimePartView *self);
75 static gboolean      search             (ModestMozembedMimePartView *self, const gchar *string);
76 static gboolean      search_next        (ModestMozembedMimePartView *self);
77 static gboolean      get_selection_area (ModestMozembedMimePartView *self, gint *x, gint *y,
78                                          gint *width, gint *height);
79
80 typedef struct _ModestMozembedMimePartViewPrivate ModestMozembedMimePartViewPrivate;
81 struct _ModestMozembedMimePartViewPrivate {
82         gdouble current_zoom;
83 };
84
85 #define MODEST_MOZEMBED_MIME_PART_VIEW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
86                                                                                        MODEST_TYPE_MOZEMBED_MIME_PART_VIEW, \
87                                                                                        ModestMozembedMimePartViewPrivate))
88
89 static GtkMozEmbedClass *parent_class = NULL;
90
91 GtkWidget *
92 modest_mozembed_mime_part_view_new ()
93 {
94         return g_object_new (MODEST_TYPE_MOZEMBED_MIME_PART_VIEW, NULL);
95 }
96
97 /* GOBJECT IMPLEMENTATION */
98 GType
99 modest_mozembed_mime_part_view_get_type (void)
100 {
101         static GType my_type = 0;
102         if (!my_type) {
103                 static const GTypeInfo my_info = {
104                         sizeof(ModestMozembedMimePartViewClass),
105                         NULL,           /* base init */
106                         NULL,           /* base finalize */
107                         (GClassInitFunc) modest_mozembed_mime_part_view_class_init,
108                         NULL,           /* class finalize */
109                         NULL,           /* class data */
110                         sizeof(ModestMozembedMimePartView),
111                         1,              /* n_preallocs */
112                         (GInstanceInitFunc) modest_mozembed_mime_part_view_init,
113                         NULL
114                 };
115
116                 static const GInterfaceInfo modest_mime_part_view_info = 
117                 {
118                   (GInterfaceInitFunc) modest_mime_part_view_init, /* interface_init */
119                   NULL,         /* interface_finalize */
120                   NULL          /* interface_data */
121                 };
122
123                 static const GInterfaceInfo modest_zoomable_info = 
124                 {
125                   (GInterfaceInitFunc) modest_zoomable_init, /* interface_init */
126                   NULL,         /* interface_finalize */
127                   NULL          /* interface_data */
128                 };
129
130                 static const GInterfaceInfo modest_isearch_view_info = 
131                 {
132                   (GInterfaceInitFunc) modest_isearch_view_init, /* interface_init */
133                   NULL,         /* interface_finalize */
134                   NULL          /* interface_data */
135                 };
136
137                 my_type = g_type_register_static (TNY_TYPE_MOZ_EMBED_HTML_MIME_PART_VIEW,
138                                                   "ModestMozembedMimePartView",
139                                                   &my_info, 0);
140
141                 g_type_add_interface_static (my_type, MODEST_TYPE_MIME_PART_VIEW, 
142                         &modest_mime_part_view_info);
143
144                 g_type_add_interface_static (my_type, MODEST_TYPE_ZOOMABLE, 
145                         &modest_zoomable_info);
146                 g_type_add_interface_static (my_type, MODEST_TYPE_ISEARCH_VIEW, 
147                         &modest_isearch_view_info);
148         }
149         return my_type;
150 }
151
152 static void
153 modest_mozembed_mime_part_view_class_init (ModestMozembedMimePartViewClass *klass)
154 {
155         GObjectClass *gobject_class;
156         gobject_class = (GObjectClass*) klass;
157
158         parent_class            = g_type_class_peek_parent (klass);
159         gobject_class->finalize = modest_mozembed_mime_part_view_finalize;
160         klass->is_empty_func = modest_mozembed_mime_part_view_is_empty_default;
161         klass->get_zoom_func = modest_mozembed_mime_part_view_get_zoom_default;
162         klass->set_zoom_func = modest_mozembed_mime_part_view_set_zoom_default;
163         klass->zoom_minus_func = modest_mozembed_mime_part_view_zoom_minus_default;
164         klass->zoom_plus_func = modest_mozembed_mime_part_view_zoom_plus_default;
165         klass->search_func = modest_mozembed_mime_part_view_search_default;
166         klass->search_next_func = modest_mozembed_mime_part_view_search_next_default;
167         klass->get_selection_area_func = modest_mozembed_mime_part_view_get_selection_area_default;
168         
169         g_type_class_add_private (gobject_class, sizeof(ModestMozembedMimePartViewPrivate));
170
171 }
172
173 static void    
174 modest_mozembed_mime_part_view_init (ModestMozembedMimePartView *self)
175 {
176         ModestMozembedMimePartViewPrivate *priv = MODEST_MOZEMBED_MIME_PART_VIEW_GET_PRIVATE (self);
177
178         priv->current_zoom = 1.0;
179
180 }
181
182 static void
183 modest_mozembed_mime_part_view_finalize (GObject *obj)
184 {
185         G_OBJECT_CLASS (parent_class)->finalize (obj);
186 }
187
188 /* INTERNAL API */
189
190 static gboolean      
191 is_empty   (ModestMozembedMimePartView *self)
192 {
193         /* TODO: not implemented yet */
194         return FALSE;
195 }
196
197 static void
198 set_zoom (ModestMozembedMimePartView *self, gdouble zoom)
199 {
200         ModestMozembedMimePartViewPrivate *priv;
201
202         g_return_if_fail (MODEST_IS_MOZEMBED_MIME_PART_VIEW (self));
203
204         priv = MODEST_MOZEMBED_MIME_PART_VIEW_GET_PRIVATE (self);
205         priv->current_zoom = zoom;
206         gtk_moz_embed_set_zoom_level (GTK_MOZ_EMBED(self), (gint) (zoom*100), NULL);
207
208         gtk_widget_queue_resize (GTK_WIDGET (self));
209 }
210
211 static gdouble
212 get_zoom (ModestMozembedMimePartView *self)
213 {
214         ModestMozembedMimePartViewPrivate *priv;
215
216         g_return_val_if_fail (MODEST_IS_MOZEMBED_MIME_PART_VIEW (self), 1.0);
217
218         priv = MODEST_MOZEMBED_MIME_PART_VIEW_GET_PRIVATE (self);
219
220         return priv->current_zoom;
221 }
222
223 static gboolean
224 search (ModestMozembedMimePartView *self, 
225         const gchar *string)
226 {
227         /* TODO: Implement incremental search */
228         return FALSE;
229 }
230
231 static gboolean
232 search_next (ModestMozembedMimePartView *self)
233 {
234         /* TODO: Implement incremental search */
235         return FALSE;
236 }
237
238 static gboolean
239 get_selection_area (ModestMozembedMimePartView *self, 
240                     gint *x, gint *y,
241                     gint *width, gint *height)
242 {
243         /* TODO: Implement incremental search */
244         return FALSE;
245
246 }
247
248
249 /* MODEST MIME PART VIEW IMPLEMENTATION */
250
251 static void
252 modest_mime_part_view_init (gpointer g, gpointer iface_data)
253 {
254         ModestMimePartViewIface *klass = (ModestMimePartViewIface *)g;
255
256         klass->is_empty_func = modest_mozembed_mime_part_view_is_empty;
257
258         return;
259 }
260
261 static gboolean
262 modest_mozembed_mime_part_view_is_empty (ModestMimePartView *self)
263 {
264         return MODEST_MOZEMBED_MIME_PART_VIEW_GET_CLASS (self)->is_empty_func (self);
265 }
266
267 static gboolean
268 modest_mozembed_mime_part_view_is_empty_default (ModestMimePartView *self)
269 {
270         return is_empty (MODEST_MOZEMBED_MIME_PART_VIEW (self));
271 }
272
273
274 /* MODEST ZOOMABLE IMPLEMENTATION */
275 static void
276 modest_zoomable_init (gpointer g, gpointer iface_data)
277 {
278         ModestZoomableIface *klass = (ModestZoomableIface *)g;
279         
280         klass->get_zoom_func = modest_mozembed_mime_part_view_get_zoom;
281         klass->set_zoom_func = modest_mozembed_mime_part_view_set_zoom;
282         klass->zoom_minus_func = modest_mozembed_mime_part_view_zoom_minus;
283         klass->zoom_plus_func = modest_mozembed_mime_part_view_zoom_plus;
284
285         return;
286 }
287
288 static gdouble
289 modest_mozembed_mime_part_view_get_zoom (ModestZoomable *self)
290 {
291         return MODEST_MOZEMBED_MIME_PART_VIEW_GET_CLASS (self)->get_zoom_func (self);
292 }
293
294 static gdouble
295 modest_mozembed_mime_part_view_get_zoom_default (ModestZoomable *self)
296 {
297         return get_zoom (MODEST_MOZEMBED_MIME_PART_VIEW (self));
298 }
299
300 static void
301 modest_mozembed_mime_part_view_set_zoom (ModestZoomable *self, gdouble value)
302 {
303         MODEST_MOZEMBED_MIME_PART_VIEW_GET_CLASS (self)->set_zoom_func (self, value);
304 }
305
306 static void
307 modest_mozembed_mime_part_view_set_zoom_default (ModestZoomable *self, gdouble value)
308 {
309         set_zoom (MODEST_MOZEMBED_MIME_PART_VIEW (self), value);
310 }
311
312 static gboolean
313 modest_mozembed_mime_part_view_zoom_minus (ModestZoomable *self)
314 {
315         return MODEST_MOZEMBED_MIME_PART_VIEW_GET_CLASS (self)->zoom_minus_func (self);
316 }
317
318 static gboolean
319 modest_mozembed_mime_part_view_zoom_minus_default (ModestZoomable *self)
320 {
321         /* operation not supported in ModestMozembedMimePartView */
322         return FALSE;
323 }
324
325 static gboolean
326 modest_mozembed_mime_part_view_zoom_plus (ModestZoomable *self)
327 {
328         return MODEST_MOZEMBED_MIME_PART_VIEW_GET_CLASS (self)->zoom_plus_func (self);
329 }
330
331 static gboolean
332 modest_mozembed_mime_part_view_zoom_plus_default (ModestZoomable *self)
333 {
334         /* operation not supported in ModestMozembedMimePartView */
335         return FALSE;
336 }
337
338 /* ISEARCH VIEW IMPLEMENTATION */
339 static void
340 modest_isearch_view_init (gpointer g, gpointer iface_data)
341 {
342         ModestISearchViewIface *klass = (ModestISearchViewIface *)g;
343         
344         klass->search_func = modest_mozembed_mime_part_view_search;
345         klass->search_next_func = modest_mozembed_mime_part_view_search_next;
346         klass->get_selection_area_func = modest_mozembed_mime_part_view_get_selection_area;
347
348         return;
349 }
350
351 static gboolean 
352 modest_mozembed_mime_part_view_search (ModestISearchView *self, const gchar *string)
353 {
354         return MODEST_MOZEMBED_MIME_PART_VIEW_GET_CLASS (self)->search_func (self, string);
355 }
356
357 static gboolean 
358 modest_mozembed_mime_part_view_search_default (ModestISearchView *self, const gchar *string)
359 {
360         return search (MODEST_MOZEMBED_MIME_PART_VIEW (self), string);
361 }
362
363 static gboolean 
364 modest_mozembed_mime_part_view_search_next(ModestISearchView *self)
365 {
366         return MODEST_MOZEMBED_MIME_PART_VIEW_GET_CLASS (self)->search_next_func (self);
367 }
368
369 static gboolean 
370 modest_mozembed_mime_part_view_search_next_default (ModestISearchView *self)
371 {
372         return search_next (MODEST_MOZEMBED_MIME_PART_VIEW (self));
373 }
374
375 static gboolean 
376 modest_mozembed_mime_part_view_get_selection_area (ModestISearchView *self, gint *x, gint *y, 
377                                                   gint *width, gint *height)
378 {
379         return MODEST_MOZEMBED_MIME_PART_VIEW_GET_CLASS (self)->get_selection_area_func (self, x, y, width, height);
380 }
381
382 static gboolean 
383 modest_mozembed_mime_part_view_get_selection_area_default (ModestISearchView *self, gint *x, gint *y, 
384                                                           gint *width, gint *height)
385 {
386         return get_selection_area (MODEST_MOZEMBED_MIME_PART_VIEW (self), x, y, width, height);
387 }