root/trunk/libtinymailui-webkit/tny-webkit-html-mime-part-view.c

Revision 3666 (checked in by jdapena, 7 months ago)

* Use GOnce registering all types in tinymail to make

registering thread-safe.

Line 
1 /* libtinymailui-webkit - The Tiny Mail UI library for Webkit
2  * Copyright (C) 2006-2007 Philip Van Hoof <pvanhoof@gnome.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with self library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20  
21 #include <config.h>
22 #include <glib/gi18n-lib.h>
23
24 #include "mozilla-preferences.h"
25
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32
33 #include <string.h>
34 #include <gtk/gtk.h>
35
36 #include <tny-webkit-html-mime-part-view.h>
37 #include <tny-webkit-stream.h>
38
39 #ifdef GNOME
40 #include <tny-vfs-stream.h>
41 #include <libgnomevfs/gnome-vfs.h>
42 #include <libgnomevfs/gnome-vfs-utils.h>
43 #else
44 #include <tny-fs-stream.h>
45 #endif
46
47 static GObjectClass *parent_class = NULL;
48
49 typedef struct _TnyWebkitHtmlMimePartViewPriv TnyWebkitHtmlMimePartViewPriv;
50
51 struct _TnyWebkitHtmlMimePartViewPriv {
52         TnyMimePart *part;
53         TnyStatusCallback status_callback;
54         gpointer status_user_data;
55 };
56
57 #define TNY_WEBKIT_HTML_MIME_PART_VIEW_GET_PRIVATE(o) \
58         (G_TYPE_INSTANCE_GET_PRIVATE ((o), TNY_TYPE_WEBKIT_HTML_MIME_PART_VIEW, TnyWebkitHtmlMimePartViewPriv))
59
60
61 static TnyMimePart*
62 tny_webkit_html_mime_part_view_get_part (TnyMimePartView *self)
63 {
64         TnyWebkitHtmlMimePartViewPriv *priv = TNY_WEBKIT_HTML_MIME_PART_VIEW_GET_PRIVATE (self);
65         return (priv->part)?TNY_MIME_PART (g_object_ref (priv->part)):NULL;
66 }
67
68 /**
69  * tny_webkit_html_mime_part_view_set_part:
70  * @self: a #TnyWebkitMimePartView instance
71  * @part: a #TnyMimePart instance
72  *
73  * This is non-public API documentation
74  *
75  * The implementation simply decodes the part to a stream that is implemented in
76  * such a way that it will forward the stream to the GtkWebkit stream API.
77  **/
78 static void
79 tny_webkit_html_mime_part_view_set_part (TnyMimePartView *self, TnyMimePart *part)
80 {
81         TnyWebkitHtmlMimePartViewPriv *priv = TNY_WEBKIT_HTML_MIME_PART_VIEW_GET_PRIVATE (self);
82
83         if (priv->part)
84                 g_object_unref (priv->part);
85
86         if (part) {
87                 TnyStream *dest;
88
89                 dest = tny_webkit_stream_new (self);
90                 tny_stream_reset (dest);
91                 tny_mime_part_decode_to_stream_async (part, dest, NULL,
92                         priv->status_callback, priv->status_user_data);
93                 g_object_unref (dest);
94                 priv->part = (TnyMimePart *) g_object_ref (part);
95         }
96
97         return;
98 }
99
100 static void
101 tny_webkit_html_mime_part_view_clear (TnyMimePartView *self)
102 {
103
104         webkit_web_view_load_html_string (WEBKIT_WEB_VIEW (self), "", "");
105
106         return;
107 }
108
109 /**
110  * tny_webkit_html_mime_part_view_new:
111  *
112  * Create a #TnyMimePartView that can display HTML mime parts
113  *
114  * Return value: a new #TnyMimePartView instance implemented for Gtk+
115  **/
116 TnyMimePartView*
117 tny_webkit_html_mime_part_view_new (TnyStatusCallback status_callback, gpointer status_user_data)
118 {
119         TnyWebkitHtmlMimePartView *self = g_object_new (TNY_TYPE_WEBKIT_HTML_MIME_PART_VIEW, NULL);
120         TnyWebkitHtmlMimePartViewPriv *priv = TNY_WEBKIT_HTML_MIME_PART_VIEW_GET_PRIVATE (self);
121
122         priv->status_callback = status_callback;
123         priv->status_user_data = status_user_data;
124
125         return TNY_MIME_PART_VIEW (self);
126 }
127
128 static void
129 tny_webkit_html_mime_part_view_instance_init (GTypeInstance *instance, gpointer g_class)
130 {
131         TnyWebkitHtmlMimePartView *self  = (TnyWebkitHtmlMimePartView*) instance;
132         TnyWebkitHtmlMimePartViewPriv *priv = TNY_WEBKIT_HTML_MIME_PART_VIEW_GET_PRIVATE (self);
133         priv->part = NULL;
134         return;
135 }
136
137 static void
138 tny_webkit_html_mime_part_view_finalize (GObject *object)
139 {
140         TnyWebkitHtmlMimePartView *self = (TnyWebkitHtmlMimePartView *)object; 
141         TnyWebkitHtmlMimePartViewPriv *priv = TNY_WEBKIT_HTML_MIME_PART_VIEW_GET_PRIVATE (self);
142         if (priv->part)
143                 g_object_unref (priv->part);
144         (*parent_class->finalize) (object);
145         return;
146 }
147
148 static void
149 tny_mime_part_view_init (gpointer g, gpointer iface_data)
150 {
151         TnyMimePartViewIface *klass = (TnyMimePartViewIface *)g;
152
153         klass->get_part= tny_webkit_html_mime_part_view_get_part;
154         klass->set_part= tny_webkit_html_mime_part_view_set_part;
155         klass->clear= tny_webkit_html_mime_part_view_clear;
156
157         return;
158 }
159
160 static void
161 tny_webkit_html_mime_part_view_class_init (TnyWebkitHtmlMimePartViewClass *class)
162 {
163         GObjectClass *object_class;
164
165         parent_class = g_type_class_peek_parent (class);
166         object_class = (GObjectClass*) class;
167
168         object_class->finalize = tny_webkit_html_mime_part_view_finalize;
169
170         g_type_class_add_private (object_class, sizeof (TnyWebkitHtmlMimePartViewPriv));
171
172         return;
173 }
174
175 static gpointer
176 tny_webkit_html_mime_part_view_get_type (gpointer notused)
177 {
178         GType type = 0;
179
180         static const GTypeInfo info =
181                 {
182                   sizeof (TnyWebkitHtmlMimePartViewClass),
183                   NULL,   /* base_init */
184                   NULL,   /* base_finalize */
185                   (GClassInitFunc) tny_webkit_html_mime_part_view_class_init,   /* class_init */
186                   NULL,   /* class_finalize */
187                   NULL,   /* class_data */
188                   sizeof (TnyWebkitHtmlMimePartView),
189                   0,      /* n_preallocs */
190                   tny_webkit_html_mime_part_view_instance_init,    /* instance_init */
191                   NULL
192                 };
193
194         static const GInterfaceInfo tny_mime_part_view_info =
195                 {
196                   (GInterfaceInitFunc) tny_mime_part_view_init, /* interface_init */
197                   NULL,         /* interface_finalize */
198                   NULL          /* interface_data */
199                 };
200
201         type = g_type_register_static (GTK_TYPE_WEBKIT,
202                                        "TnyWebkitHtmlMimePartView",
203                                        &info, 0);
204        
205         g_type_add_interface_static (type, TNY_TYPE_MIME_PART_VIEW,
206                                      &tny_mime_part_view_info);
207
208         return GUINT_TO_POINTER (type);
209 }
210
211 GType
212 tny_webkit_html_mime_part_view_get_type (void)
213 {
214         static GOnce once = G_ONCE_INIT;
215         g_once (&once, tny_webkit_html_mime_part_view_register_type, NULL);
216         return GPOINTER_TO_UINT (once.retval);
217 }
Note: See TracBrowser for help on using the browser.