root/trunk/libtinymailui-gtk/tny-gtk-image-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-gtk - The Tiny Mail UI library for Gtk+
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  * TnyGtkImageMimePartView:
22  *
23  * A #TnyMimePartView that can render a #TnyMimePart that is an image. It's
24  * recommended to use this type together with a #TnyGtkExpanderMimePartView.
25  *
26  * free-function: g_object_unref
27  **/
28
29 #include <config.h>
30
31 #include <glib/gi18n-lib.h>
32
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39
40 #include <string.h>
41 #include <gtk/gtk.h>
42
43 #include <tny-gtk-image-mime-part-view.h>
44 #include <tny-gtk-pixbuf-stream.h>
45
46 #ifdef GNOME
47 #include <tny-vfs-stream.h>
48 #include <libgnomevfs/gnome-vfs.h>
49 #include <libgnomevfs/gnome-vfs-utils.h>
50 #else
51 #include <tny-fs-stream.h>
52 #endif
53
54
55 static GObjectClass *parent_class = NULL;
56
57 typedef struct _TnyGtkImageMimePartViewPriv TnyGtkImageMimePartViewPriv;
58
59 struct _TnyGtkImageMimePartViewPriv
60 {
61         TnyMimePart *part;
62         TnyStatusCallback status_callback;
63         gpointer status_user_data;
64 };
65
66 #define TNY_GTK_IMAGE_MIME_PART_VIEW_GET_PRIVATE(o) \
67         (G_TYPE_INSTANCE_GET_PRIVATE ((o), TNY_TYPE_GTK_IMAGE_MIME_PART_VIEW, TnyGtkImageMimePartViewPriv))
68
69
70 static TnyMimePart*
71 tny_gtk_image_mime_part_view_get_part (TnyMimePartView *self)
72 {
73         return TNY_GTK_IMAGE_MIME_PART_VIEW_GET_CLASS (self)->get_part(self);
74 }
75
76 static TnyMimePart*
77 tny_gtk_image_mime_part_view_get_part_default (TnyMimePartView *self)
78 {
79         TnyGtkImageMimePartViewPriv *priv = TNY_GTK_IMAGE_MIME_PART_VIEW_GET_PRIVATE (self);
80         return (priv->part)?TNY_MIME_PART (g_object_ref (priv->part)):NULL;
81 }
82
83 static void
84 tny_gtk_image_mime_part_view_set_part (TnyMimePartView *self, TnyMimePart *part)
85 {
86         TNY_GTK_IMAGE_MIME_PART_VIEW_GET_CLASS (self)->set_part(self, part);
87         return;
88 }
89
90 static void
91 on_mime_part_decoded (TnyMimePart *part, gboolean canceled, TnyStream *dest, GError *err, gpointer user_data)
92 {
93         TnyMimePartView *self = (TnyMimePartView *) user_data;
94         GdkPixbuf *pixbuf;
95         tny_stream_reset (dest);
96         pixbuf = tny_gtk_pixbuf_stream_get_pixbuf (TNY_GTK_PIXBUF_STREAM (dest));
97         gtk_image_set_from_pixbuf (GTK_IMAGE (self), pixbuf);
98         g_object_unref (self);
99 }
100
101 static void
102 on_status (GObject *part, TnyStatus *status, gpointer user_data)
103 {
104         TnyMimePartView *self = (TnyMimePartView *) user_data;
105         TnyGtkImageMimePartViewPriv *priv = TNY_GTK_IMAGE_MIME_PART_VIEW_GET_PRIVATE (self);
106         if (priv->status_callback)
107                 priv->status_callback ((GObject *) self, status, priv->status_user_data);
108         return;
109 }
110
111 static void
112 tny_gtk_image_mime_part_view_set_part_default (TnyMimePartView *self, TnyMimePart *part)
113 {
114         TnyGtkImageMimePartViewPriv *priv = TNY_GTK_IMAGE_MIME_PART_VIEW_GET_PRIVATE (self);
115
116         g_assert (TNY_IS_MIME_PART (part));
117
118         if (priv->part)
119                 g_object_unref (priv->part);
120
121         if (part) {
122                 TnyStream *dest = tny_gtk_pixbuf_stream_new (tny_mime_part_get_content_type (part));
123                 tny_stream_reset (dest);
124                 tny_mime_part_decode_to_stream_async (part, dest, on_mime_part_decoded,
125                                 on_status, g_object_ref (self));
126                 g_object_unref (dest);
127                 priv->part = g_object_ref (part);
128         }
129
130         return;
131 }
132
133 static void
134 tny_gtk_image_mime_part_view_clear (TnyMimePartView *self)
135 {
136         TNY_GTK_IMAGE_MIME_PART_VIEW_GET_CLASS (self)->clear(self);
137         return;
138 }
139
140 static void
141 tny_gtk_image_mime_part_view_clear_default (TnyMimePartView *self)
142 {
143         return;
144 }
145
146 /**
147  * tny_gtk_image_mime_part_view_new:
148  * @status_callback: (null-ok): a #TnyStatusCallback or NULL
149  * @status_user_data: (null-ok): user data for @status_callback
150  *
151  * Create a new #TnyMimePartView for displaying image #TnyMimePartView instances.
152  * The returned value inherits #GtkImage. It's recommended to use a
153  * #TnyGtkExpanderMimePartView to wrap this type.
154  *
155  * Whenever data must be retrieved or takes long to load, @status_callback will
156  * be called to let the outside world know about what this compenent is doing.
157  *
158  * returns: (caller-owns): a new #TnyMimePartView for images
159  * since: 1.0
160  * audience: application-developer
161  **/
162 TnyMimePartView*
163 tny_gtk_image_mime_part_view_new (TnyStatusCallback status_callback, gpointer status_user_data)
164 {
165         TnyGtkImageMimePartView *self = g_object_new (TNY_TYPE_GTK_IMAGE_MIME_PART_VIEW, NULL);
166         TnyGtkImageMimePartViewPriv *priv = TNY_GTK_IMAGE_MIME_PART_VIEW_GET_PRIVATE (self);
167
168         priv->status_callback = status_callback;
169         priv->status_user_data = status_user_data;
170
171         return TNY_MIME_PART_VIEW (self);
172 }
173
174 static void
175 tny_gtk_image_mime_part_view_instance_init (GTypeInstance *instance, gpointer g_class)
176 {
177         TnyGtkImageMimePartView *self = (TnyGtkImageMimePartView *)instance;
178         TnyGtkImageMimePartViewPriv *priv = TNY_GTK_IMAGE_MIME_PART_VIEW_GET_PRIVATE (self);
179
180         priv->status_callback = NULL;
181         priv->status_user_data = NULL;
182
183         return;
184 }
185
186 static void
187 tny_gtk_image_mime_part_view_finalize (GObject *object)
188 {
189         TnyGtkImageMimePartView *self = (TnyGtkImageMimePartView *)object;
190         TnyGtkImageMimePartViewPriv *priv = TNY_GTK_IMAGE_MIME_PART_VIEW_GET_PRIVATE (self);
191
192         if (priv->part)
193                 g_object_unref (priv->part);
194
195         (*parent_class->finalize) (object);
196
197         return;
198 }
199
200 static void
201 tny_mime_part_view_init (gpointer g, gpointer iface_data)
202 {
203         TnyMimePartViewIface *klass = (TnyMimePartViewIface *)g;
204
205         klass->get_part= tny_gtk_image_mime_part_view_get_part;
206         klass->set_part= tny_gtk_image_mime_part_view_set_part;
207         klass->clear= tny_gtk_image_mime_part_view_clear;
208
209         return;
210 }
211
212 static void
213 tny_gtk_image_mime_part_view_class_init (TnyGtkImageMimePartViewClass *class)
214 {
215         GObjectClass *object_class;
216
217         parent_class = g_type_class_peek_parent (class);
218         object_class = (GObjectClass*) class;
219
220         class->get_part= tny_gtk_image_mime_part_view_get_part_default;
221         class->set_part= tny_gtk_image_mime_part_view_set_part_default;
222         class->clear= tny_gtk_image_mime_part_view_clear_default;
223
224         object_class->finalize = tny_gtk_image_mime_part_view_finalize;
225
226         g_type_class_add_private (object_class, sizeof (TnyGtkImageMimePartViewPriv));
227
228         return;
229 }
230
231 static gpointer
232 tny_gtk_image_mime_part_view_register_type (gpointer notused)
233 {
234         GType type = 0;
235
236         static const GTypeInfo info =
237                 {
238                   sizeof (TnyGtkImageMimePartViewClass),
239                   NULL,   /* base_init */
240                   NULL,   /* base_finalize */
241                   (GClassInitFunc) tny_gtk_image_mime_part_view_class_init,   /* class_init */
242                   NULL,   /* class_finalize */
243                   NULL,   /* class_data */
244                   sizeof (TnyGtkImageMimePartView),
245                   0,      /* n_preallocs */
246                   tny_gtk_image_mime_part_view_instance_init,    /* instance_init */
247                   NULL
248                 };
249
250         static const GInterfaceInfo tny_mime_part_view_info =
251                 {
252                   (GInterfaceInitFunc) tny_mime_part_view_init, /* interface_init */
253                   NULL,         /* interface_finalize */
254                   NULL          /* interface_data */
255                 };
256
257         type = g_type_register_static (GTK_TYPE_IMAGE,
258                                        "TnyGtkImageMimePartView",
259                                        &info, 0);
260
261         g_type_add_interface_static (type, TNY_TYPE_MIME_PART_VIEW,
262                                      &tny_mime_part_view_info);
263
264         return GUINT_TO_POINTER (type);
265 }
266
267 GType
268 tny_gtk_image_mime_part_view_get_type (void)
269 {
270         static GOnce once = G_ONCE_INIT;
271         g_once (&once, tny_gtk_image_mime_part_view_register_type, NULL);
272         return GPOINTER_TO_UINT (once.retval);
273 }
Note: See TracBrowser for help on using the browser.