Fix for bug NB#81584.
[modest] / src / widgets / modest-mozembed-mime-part-view.cpp
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 #ifdef XPCOM_GLUE
31 #include <gtkmozembed_glue.cpp>
32 #endif
33
34 #include <widgets/modest-mozembed-mime-part-view.h>
35 #include <string.h>
36 #include <tny-stream.h>
37 #include <tny-mime-part-view.h>
38 #include <tny-gtk-text-buffer-stream.h>
39 #include <tny-moz-embed-html-mime-part-view.h>
40 #include <modest-text-utils.h>
41 #include <widgets/modest-mime-part-view.h>
42 #include <widgets/modest-zoomable.h>
43
44 /* gobject structure methods */
45 static void    modest_mozembed_mime_part_view_class_init (ModestMozembedMimePartViewClass *klass);
46 static void    modest_mime_part_view_init               (gpointer g, gpointer iface_data);
47 static void    modest_zoomable_init                     (gpointer g, gpointer iface_data);
48 static void    modest_isearch_view_init                 (gpointer g, gpointer iface_data);
49 static void    modest_mozembed_mime_part_view_init       (ModestMozembedMimePartView *self);
50 static void    modest_mozembed_mime_part_view_finalize   (GObject *self);
51
52 /* ModestMimePartView implementation */
53 static gboolean modest_mozembed_mime_part_view_is_empty (ModestMimePartView *self);
54 static gboolean modest_mozembed_mime_part_view_is_empty_default (ModestMimePartView *self);
55 /* ModestZoomable implementation */
56 static gdouble modest_mozembed_mime_part_view_get_zoom (ModestZoomable *self);
57 static void modest_mozembed_mime_part_view_set_zoom (ModestZoomable *self, gdouble value);
58 static gboolean modest_mozembed_mime_part_view_zoom_minus (ModestZoomable *self);
59 static gboolean modest_mozembed_mime_part_view_zoom_plus (ModestZoomable *self);
60 static gdouble modest_mozembed_mime_part_view_get_zoom_default (ModestZoomable *self);
61 static void modest_mozembed_mime_part_view_set_zoom_default (ModestZoomable *self, gdouble value);
62 static gboolean modest_mozembed_mime_part_view_zoom_minus_default (ModestZoomable *self);
63 static gboolean modest_mozembed_mime_part_view_zoom_plus_default (ModestZoomable *self);
64 /* ModestISearchView implementation */
65 static gboolean modest_mozembed_mime_part_view_search                    (ModestISearchView *self, const gchar *string);
66 static gboolean modest_mozembed_mime_part_view_search_next               (ModestISearchView *self);
67 static gboolean modest_mozembed_mime_part_view_get_selection_area        (ModestISearchView *self, gint *x, gint *y, 
68                                                                          gint *width, gint *height);
69 static gboolean modest_mozembed_mime_part_view_search_default            (ModestISearchView *self, const gchar *string);
70 static gboolean modest_mozembed_mime_part_view_search_next_default       (ModestISearchView *self);
71 static gboolean modest_mozembed_mime_part_view_get_selection_area_default (ModestISearchView *self, gint *x, gint *y, 
72                                                                           gint *width, gint *height);
73
74
75 /* internal api */
76 static gboolean      is_empty   (ModestMozembedMimePartView *self);
77 static void          set_zoom   (ModestMozembedMimePartView *self, gdouble zoom);
78 static gdouble       get_zoom   (ModestMozembedMimePartView *self);
79 static gboolean      search             (ModestMozembedMimePartView *self, const gchar *string);
80 static gboolean      search_next        (ModestMozembedMimePartView *self);
81 static gboolean      get_selection_area (ModestMozembedMimePartView *self, gint *x, gint *y,
82                                          gint *width, gint *height);
83
84 typedef struct _ModestMozembedMimePartViewPrivate ModestMozembedMimePartViewPrivate;
85 struct _ModestMozembedMimePartViewPrivate {
86         gdouble current_zoom;
87         gchar *last_search;
88 };
89
90 #define MODEST_MOZEMBED_MIME_PART_VIEW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
91                                                                                        MODEST_TYPE_MOZEMBED_MIME_PART_VIEW, \
92                                                                                        ModestMozembedMimePartViewPrivate))
93
94 static GtkMozEmbedClass *parent_class = NULL;
95
96 GtkWidget *
97 modest_mozembed_mime_part_view_new ()
98 {
99         return GTK_WIDGET(g_object_new (MODEST_TYPE_MOZEMBED_MIME_PART_VIEW, NULL));
100 }
101
102 /* GOBJECT IMPLEMENTATION */
103 GType
104 modest_mozembed_mime_part_view_get_type (void)
105 {
106         static GType my_type = 0;
107         if (!my_type) {
108                 static const GTypeInfo my_info = {
109                         sizeof(ModestMozembedMimePartViewClass),
110                         NULL,           /* base init */
111                         NULL,           /* base finalize */
112                         (GClassInitFunc) modest_mozembed_mime_part_view_class_init,
113                         NULL,           /* class finalize */
114                         NULL,           /* class data */
115                         sizeof(ModestMozembedMimePartView),
116                         1,              /* n_preallocs */
117                         (GInstanceInitFunc) modest_mozembed_mime_part_view_init,
118                         NULL
119                 };
120
121                 static const GInterfaceInfo modest_mime_part_view_info = 
122                 {
123                   (GInterfaceInitFunc) modest_mime_part_view_init, /* interface_init */
124                   NULL,         /* interface_finalize */
125                   NULL          /* interface_data */
126                 };
127
128                 static const GInterfaceInfo modest_zoomable_info = 
129                 {
130                   (GInterfaceInitFunc) modest_zoomable_init, /* interface_init */
131                   NULL,         /* interface_finalize */
132                   NULL          /* interface_data */
133                 };
134
135                 static const GInterfaceInfo modest_isearch_view_info = 
136                 {
137                   (GInterfaceInitFunc) modest_isearch_view_init, /* interface_init */
138                   NULL,         /* interface_finalize */
139                   NULL          /* interface_data */
140                 };
141
142                 my_type = g_type_register_static (TNY_TYPE_MOZ_EMBED_HTML_MIME_PART_VIEW,
143                                                   "ModestMozembedMimePartView",
144                                                   &my_info, GTypeFlags(0));
145
146                 g_type_add_interface_static (my_type, MODEST_TYPE_MIME_PART_VIEW, 
147                         &modest_mime_part_view_info);
148
149                 g_type_add_interface_static (my_type, MODEST_TYPE_ZOOMABLE, 
150                         &modest_zoomable_info);
151                 g_type_add_interface_static (my_type, MODEST_TYPE_ISEARCH_VIEW, 
152                         &modest_isearch_view_info);
153         }
154         return my_type;
155 }
156
157 static void
158 modest_mozembed_mime_part_view_class_init (ModestMozembedMimePartViewClass *klass)
159 {
160         GObjectClass *gobject_class;
161         gobject_class = (GObjectClass*) klass;
162
163         parent_class            = (GtkMozEmbedClass*) g_type_class_peek_parent (klass);
164         gobject_class->finalize = modest_mozembed_mime_part_view_finalize;
165         klass->is_empty_func = modest_mozembed_mime_part_view_is_empty_default;
166         klass->get_zoom_func = modest_mozembed_mime_part_view_get_zoom_default;
167         klass->set_zoom_func = modest_mozembed_mime_part_view_set_zoom_default;
168         klass->zoom_minus_func = modest_mozembed_mime_part_view_zoom_minus_default;
169         klass->zoom_plus_func = modest_mozembed_mime_part_view_zoom_plus_default;
170         klass->search_func = modest_mozembed_mime_part_view_search_default;
171         klass->search_next_func = modest_mozembed_mime_part_view_search_next_default;
172         klass->get_selection_area_func = modest_mozembed_mime_part_view_get_selection_area_default;
173         
174         g_type_class_add_private (gobject_class, sizeof(ModestMozembedMimePartViewPrivate));
175
176 }
177
178 static void    
179 modest_mozembed_mime_part_view_init (ModestMozembedMimePartView *self)
180 {
181         ModestMozembedMimePartViewPrivate *priv = MODEST_MOZEMBED_MIME_PART_VIEW_GET_PRIVATE (self);
182
183         priv->current_zoom = 1.0;
184         priv->last_search = NULL;
185
186 }
187
188 static void
189 modest_mozembed_mime_part_view_finalize (GObject *obj)
190 {
191         ModestMozembedMimePartViewPrivate *priv = MODEST_MOZEMBED_MIME_PART_VIEW_GET_PRIVATE (obj);
192
193         g_free (priv->last_search);
194         priv->last_search = NULL;
195
196         G_OBJECT_CLASS (parent_class)->finalize (obj);
197 }
198
199 /* INTERNAL API */
200
201 static gboolean      
202 is_empty   (ModestMozembedMimePartView *self)
203 {
204         /* TODO: not implemented yet */
205         return FALSE;
206 }
207
208 static void
209 set_zoom (ModestMozembedMimePartView *self, gdouble zoom)
210 {
211         ModestMozembedMimePartViewPrivate *priv;
212
213         g_return_if_fail (MODEST_IS_MOZEMBED_MIME_PART_VIEW (self));
214
215         priv = MODEST_MOZEMBED_MIME_PART_VIEW_GET_PRIVATE (self);
216         priv->current_zoom = zoom;
217         gtk_moz_embed_set_zoom_level (GTK_MOZ_EMBED(self), (gint) (zoom*100), NULL);
218
219         gtk_widget_queue_resize (GTK_WIDGET (self));
220 }
221
222 static gdouble
223 get_zoom (ModestMozembedMimePartView *self)
224 {
225         ModestMozembedMimePartViewPrivate *priv;
226
227         g_return_val_if_fail (MODEST_IS_MOZEMBED_MIME_PART_VIEW (self), 1.0);
228
229         priv = MODEST_MOZEMBED_MIME_PART_VIEW_GET_PRIVATE (self);
230
231         return priv->current_zoom;
232 }
233
234 static gboolean
235 search (ModestMozembedMimePartView *self, 
236         const gchar *string)
237 {
238         ModestMozembedMimePartViewPrivate *priv = MODEST_MOZEMBED_MIME_PART_VIEW_GET_PRIVATE (self);
239         if (priv->last_search != NULL) {
240                 g_free (priv->last_search);
241         }
242         priv->last_search = g_strdup (string);
243
244         return gtk_moz_embed_find_text (GTK_MOZ_EMBED (self), string, FALSE, FALSE,
245                                         FALSE, TRUE, 0);
246 }
247
248 static gboolean
249 search_next (ModestMozembedMimePartView *self)
250 {
251         ModestMozembedMimePartViewPrivate *priv = MODEST_MOZEMBED_MIME_PART_VIEW_GET_PRIVATE (self);
252         return gtk_moz_embed_find_text (GTK_MOZ_EMBED (self), priv->last_search, FALSE, FALSE,
253                                         FALSE, FALSE, 0);
254 }
255
256 static gboolean
257 get_selection_area (ModestMozembedMimePartView *self, 
258                     gint *x, gint *y,
259                     gint *width, gint *height)
260 {
261         /* TODO: Implement incremental search */
262         return FALSE;
263
264 }
265
266
267 /* MODEST MIME PART VIEW IMPLEMENTATION */
268
269 static void
270 modest_mime_part_view_init (gpointer g, gpointer iface_data)
271 {
272         ModestMimePartViewIface *klass = (ModestMimePartViewIface *)g;
273
274         klass->is_empty_func = modest_mozembed_mime_part_view_is_empty;
275
276         return;
277 }
278
279 static gboolean
280 modest_mozembed_mime_part_view_is_empty (ModestMimePartView *self)
281 {
282         return MODEST_MOZEMBED_MIME_PART_VIEW_GET_CLASS (self)->is_empty_func (self);
283 }
284
285 static gboolean
286 modest_mozembed_mime_part_view_is_empty_default (ModestMimePartView *self)
287 {
288         return is_empty (MODEST_MOZEMBED_MIME_PART_VIEW (self));
289 }
290
291
292 /* MODEST ZOOMABLE IMPLEMENTATION */
293 static void
294 modest_zoomable_init (gpointer g, gpointer iface_data)
295 {
296         ModestZoomableIface *klass = (ModestZoomableIface *)g;
297         
298         klass->get_zoom_func = modest_mozembed_mime_part_view_get_zoom;
299         klass->set_zoom_func = modest_mozembed_mime_part_view_set_zoom;
300         klass->zoom_minus_func = modest_mozembed_mime_part_view_zoom_minus;
301         klass->zoom_plus_func = modest_mozembed_mime_part_view_zoom_plus;
302
303         return;
304 }
305
306 static gdouble
307 modest_mozembed_mime_part_view_get_zoom (ModestZoomable *self)
308 {
309         return MODEST_MOZEMBED_MIME_PART_VIEW_GET_CLASS (self)->get_zoom_func (self);
310 }
311
312 static gdouble
313 modest_mozembed_mime_part_view_get_zoom_default (ModestZoomable *self)
314 {
315         return get_zoom (MODEST_MOZEMBED_MIME_PART_VIEW (self));
316 }
317
318 static void
319 modest_mozembed_mime_part_view_set_zoom (ModestZoomable *self, gdouble value)
320 {
321         MODEST_MOZEMBED_MIME_PART_VIEW_GET_CLASS (self)->set_zoom_func (self, value);
322 }
323
324 static void
325 modest_mozembed_mime_part_view_set_zoom_default (ModestZoomable *self, gdouble value)
326 {
327         set_zoom (MODEST_MOZEMBED_MIME_PART_VIEW (self), value);
328 }
329
330 static gboolean
331 modest_mozembed_mime_part_view_zoom_minus (ModestZoomable *self)
332 {
333         return MODEST_MOZEMBED_MIME_PART_VIEW_GET_CLASS (self)->zoom_minus_func (self);
334 }
335
336 static gboolean
337 modest_mozembed_mime_part_view_zoom_minus_default (ModestZoomable *self)
338 {
339         /* operation not supported in ModestMozembedMimePartView */
340         return FALSE;
341 }
342
343 static gboolean
344 modest_mozembed_mime_part_view_zoom_plus (ModestZoomable *self)
345 {
346         return MODEST_MOZEMBED_MIME_PART_VIEW_GET_CLASS (self)->zoom_plus_func (self);
347 }
348
349 static gboolean
350 modest_mozembed_mime_part_view_zoom_plus_default (ModestZoomable *self)
351 {
352         /* operation not supported in ModestMozembedMimePartView */
353         return FALSE;
354 }
355
356 /* ISEARCH VIEW IMPLEMENTATION */
357 static void
358 modest_isearch_view_init (gpointer g, gpointer iface_data)
359 {
360         ModestISearchViewIface *klass = (ModestISearchViewIface *)g;
361         
362         klass->search_func = modest_mozembed_mime_part_view_search;
363         klass->search_next_func = modest_mozembed_mime_part_view_search_next;
364         klass->get_selection_area_func = modest_mozembed_mime_part_view_get_selection_area;
365
366         return;
367 }
368
369 static gboolean 
370 modest_mozembed_mime_part_view_search (ModestISearchView *self, const gchar *string)
371 {
372         return MODEST_MOZEMBED_MIME_PART_VIEW_GET_CLASS (self)->search_func (self, string);
373 }
374
375 static gboolean 
376 modest_mozembed_mime_part_view_search_default (ModestISearchView *self, const gchar *string)
377 {
378         return search (MODEST_MOZEMBED_MIME_PART_VIEW (self), string);
379 }
380
381 static gboolean 
382 modest_mozembed_mime_part_view_search_next(ModestISearchView *self)
383 {
384         return MODEST_MOZEMBED_MIME_PART_VIEW_GET_CLASS (self)->search_next_func (self);
385 }
386
387 static gboolean 
388 modest_mozembed_mime_part_view_search_next_default (ModestISearchView *self)
389 {
390         return search_next (MODEST_MOZEMBED_MIME_PART_VIEW (self));
391 }
392
393 static gboolean 
394 modest_mozembed_mime_part_view_get_selection_area (ModestISearchView *self, gint *x, gint *y, 
395                                                   gint *width, gint *height)
396 {
397         return MODEST_MOZEMBED_MIME_PART_VIEW_GET_CLASS (self)->get_selection_area_func (self, x, y, width, height);
398 }
399
400 static gboolean 
401 modest_mozembed_mime_part_view_get_selection_area_default (ModestISearchView *self, gint *x, gint *y, 
402                                                           gint *width, gint *height)
403 {
404         return get_selection_area (MODEST_MOZEMBED_MIME_PART_VIEW (self), x, y, width, height);
405 }