fd30044878070299d8f90bdc5f7265d3fde31c03
[modest] / src / widgets / modest-attachments-view.c
1 /* Copyright (c) 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 <config.h>
31
32 //#include <glib/gi18n-lib.h>
33
34 #include <string.h>
35 #include <gtk/gtk.h>
36
37 #include <tny-list.h>
38 #include <tny-simple-list.h>
39
40 #include <modest-platform.h>
41 #include <modest-runtime.h>
42 #include <modest-attachment-view.h>
43 #include <modest-attachments-view.h>
44
45 static GObjectClass *parent_class = NULL;
46
47 /* signals */
48 enum {
49         ACTIVATE_SIGNAL,
50         LAST_SIGNAL
51 };
52
53 typedef struct _ModestAttachmentsViewPriv ModestAttachmentsViewPriv;
54
55 struct _ModestAttachmentsViewPriv
56 {
57         TnyMsg *msg;
58 };
59
60 #define MODEST_ATTACHMENTS_VIEW_GET_PRIVATE(o)  \
61         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_ATTACHMENTS_VIEW, ModestAttachmentsViewPriv))
62
63 static guint signals[LAST_SIGNAL] = {0};
64
65 static void
66 activate_attachment (ModestAttachmentView *attachment_view,
67                      gpointer userdata)
68 {
69         TnyMimePart *mime_part;
70       
71         mime_part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (attachment_view));
72         g_signal_emit (G_OBJECT (userdata), signals[ACTIVATE_SIGNAL], 0, mime_part);
73         g_object_unref (mime_part);
74 }
75
76 /**
77  * modest_attachments_view_new:
78  * @msg: a #TnyMsg
79  *
80  * Constructor for attachments view widget.
81  *
82  * Return value: a new #ModestAttachmentsView instance implemented for Gtk+
83  **/
84 GtkWidget*
85 modest_attachments_view_new (TnyMsg *msg)
86 {
87         ModestAttachmentsView *self = g_object_new (MODEST_TYPE_ATTACHMENTS_VIEW, 
88                                                     "homogeneous", FALSE,
89                                                     "spacing", 0,
90                                                     "resize-mode", GTK_RESIZE_PARENT,
91                                                     NULL);
92
93         modest_attachments_view_set_message (self, msg);
94
95         return GTK_WIDGET (self);
96 }
97
98 void
99 modest_attachments_view_set_message (ModestAttachmentsView *attachments_view, TnyMsg *msg)
100 {
101         ModestAttachmentsViewPriv *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (attachments_view);
102         TnyList *parts;
103         TnyIterator *iter;
104         
105         if (msg == priv->msg) return;
106
107         if (priv->msg) 
108                 g_object_unref (priv->msg);
109         if (msg)
110                 g_object_ref (G_OBJECT(msg));
111         
112         priv->msg = msg;
113
114         gtk_container_foreach (GTK_CONTAINER (attachments_view), (GtkCallback) gtk_widget_destroy, NULL);
115         
116         if (priv->msg == NULL) {
117                 return;
118         }
119
120         parts = TNY_LIST (tny_simple_list_new ());
121         tny_mime_part_get_parts (TNY_MIME_PART (priv->msg), parts);
122         iter = tny_list_create_iterator (parts);
123
124         while (!tny_iterator_is_done (iter)) {
125                 TnyMimePart *part;
126
127                 part = TNY_MIME_PART (tny_iterator_get_current (iter));
128                 if (tny_mime_part_is_attachment (part)) {
129                         modest_attachments_view_add_attachment (attachments_view, part);
130                 }
131                 g_object_unref (part);
132                 tny_iterator_next (iter);
133         }
134
135         gtk_widget_queue_draw (GTK_WIDGET (attachments_view));
136
137 }
138
139 void
140 modest_attachments_view_add_attachment (ModestAttachmentsView *attachments_view, TnyMimePart *part)
141 {
142         GtkWidget *att_view = NULL;
143
144         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (attachments_view));
145         g_return_if_fail (TNY_IS_MIME_PART (part));
146         g_return_if_fail (tny_mime_part_is_attachment (part));
147
148         att_view = modest_attachment_view_new (part);
149         gtk_box_pack_end (GTK_BOX (attachments_view), att_view, FALSE, FALSE, 0);
150         gtk_widget_show_all (att_view);
151         g_signal_connect (G_OBJECT (att_view), "activate", G_CALLBACK (activate_attachment), (gpointer) attachments_view);
152 }
153
154 static void
155 modest_attachments_view_instance_init (GTypeInstance *instance, gpointer g_class)
156 {
157         ModestAttachmentsViewPriv *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (instance);
158
159         priv->msg = NULL;
160
161         return;
162 }
163
164 static void
165 modest_attachments_view_finalize (GObject *object)
166 {
167         ModestAttachmentsViewPriv *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (object);
168
169         if (priv->msg) {
170                 g_object_unref (priv->msg);
171                 priv->msg = NULL;
172         }
173
174         (*parent_class->finalize) (object);
175
176         return;
177 }
178
179 static void 
180 modest_attachments_view_class_init (ModestAttachmentsViewClass *klass)
181 {
182         GObjectClass *object_class;
183         GtkWidgetClass *widget_class;
184
185         parent_class = g_type_class_peek_parent (klass);
186         object_class = (GObjectClass*) klass;
187         widget_class = GTK_WIDGET_CLASS (klass);
188
189         object_class->finalize = modest_attachments_view_finalize;
190
191         klass->activate = NULL;
192
193         g_type_class_add_private (object_class, sizeof (ModestAttachmentsViewPriv));
194
195         signals[ACTIVATE_SIGNAL] =
196                 g_signal_new ("activate",
197                               G_TYPE_FROM_CLASS (object_class),
198                               G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
199                               G_STRUCT_OFFSET(ModestAttachmentsViewClass, activate),
200                               NULL, NULL,
201                               g_cclosure_marshal_VOID__OBJECT,
202                               G_TYPE_NONE, 1, G_TYPE_OBJECT);
203         
204         return;
205 }
206
207 GType 
208 modest_attachments_view_get_type (void)
209 {
210         static GType type = 0;
211
212         if (G_UNLIKELY(type == 0))
213         {
214                 static const GTypeInfo info = 
215                 {
216                   sizeof (ModestAttachmentsViewClass),
217                   NULL,   /* base_init */
218                   NULL,   /* base_finalize */
219                   (GClassInitFunc) modest_attachments_view_class_init,   /* class_init */
220                   NULL,   /* class_finalize */
221                   NULL,   /* class_data */
222                   sizeof (ModestAttachmentsView),
223                   0,      /* n_preallocs */
224                   modest_attachments_view_instance_init    /* instance_init */
225                 };
226
227                 type = g_type_register_static (GTK_TYPE_VBOX,
228                         "ModestAttachmentsView",
229                         &info, 0);
230
231         }
232
233         return type;
234 }