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