7c15c79d60b3fc8a028f4c60b8a8982e6e55cc00
[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 (priv->msg) 
106                 g_object_unref (priv->msg);
107         if (msg)
108                 g_object_ref (G_OBJECT(msg));
109         
110         priv->msg = msg;
111
112         gtk_container_foreach (GTK_CONTAINER (attachments_view), (GtkCallback) gtk_widget_destroy, NULL);
113         
114         if (priv->msg == NULL) {
115                 return;
116         }
117
118         parts = TNY_LIST (tny_simple_list_new ());
119         tny_mime_part_get_parts (TNY_MIME_PART (priv->msg), parts);
120         iter = tny_list_create_iterator (parts);
121
122         while (!tny_iterator_is_done (iter)) {
123                 TnyMimePart *part;
124
125                 part = TNY_MIME_PART (tny_iterator_get_current (iter));
126                 if (tny_mime_part_is_attachment (part)) {
127                         modest_attachments_view_add_attachment (attachments_view, part);
128                 }
129                 g_object_unref (part);
130                 tny_iterator_next (iter);
131         }
132
133         gtk_widget_queue_draw (GTK_WIDGET (attachments_view));
134
135 }
136
137 void
138 modest_attachments_view_add_attachment (ModestAttachmentsView *attachments_view, TnyMimePart *part)
139 {
140         GtkWidget *att_view = NULL;
141
142         g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (attachments_view));
143         g_return_if_fail (TNY_IS_MIME_PART (part));
144         g_return_if_fail (tny_mime_part_is_attachment (part));
145
146         att_view = modest_attachment_view_new (part);
147         gtk_box_pack_end (GTK_BOX (attachments_view), att_view, FALSE, FALSE, 0);
148         gtk_widget_show_all (att_view);
149         g_signal_connect (G_OBJECT (att_view), "activate", G_CALLBACK (activate_attachment), (gpointer) attachments_view);
150 }
151
152 static void
153 modest_attachments_view_instance_init (GTypeInstance *instance, gpointer g_class)
154 {
155         ModestAttachmentsViewPriv *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (instance);
156
157         priv->msg = NULL;
158
159         return;
160 }
161
162 static void
163 modest_attachments_view_finalize (GObject *object)
164 {
165         ModestAttachmentsViewPriv *priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (object);
166
167         if (priv->msg) {
168                 g_object_unref (priv->msg);
169                 priv->msg = NULL;
170         }
171
172         (*parent_class->finalize) (object);
173
174         return;
175 }
176
177 static void 
178 modest_attachments_view_class_init (ModestAttachmentsViewClass *klass)
179 {
180         GObjectClass *object_class;
181         GtkWidgetClass *widget_class;
182
183         parent_class = g_type_class_peek_parent (klass);
184         object_class = (GObjectClass*) klass;
185         widget_class = GTK_WIDGET_CLASS (klass);
186
187         object_class->finalize = modest_attachments_view_finalize;
188
189         klass->activate = NULL;
190
191         g_type_class_add_private (object_class, sizeof (ModestAttachmentsViewPriv));
192
193         signals[ACTIVATE_SIGNAL] =
194                 g_signal_new ("activate",
195                               G_TYPE_FROM_CLASS (object_class),
196                               G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
197                               G_STRUCT_OFFSET(ModestAttachmentsViewClass, activate),
198                               NULL, NULL,
199                               g_cclosure_marshal_VOID__OBJECT,
200                               G_TYPE_NONE, 1, G_TYPE_OBJECT);
201         
202         return;
203 }
204
205 GType 
206 modest_attachments_view_get_type (void)
207 {
208         static GType type = 0;
209
210         if (G_UNLIKELY(type == 0))
211         {
212                 static const GTypeInfo info = 
213                 {
214                   sizeof (ModestAttachmentsViewClass),
215                   NULL,   /* base_init */
216                   NULL,   /* base_finalize */
217                   (GClassInitFunc) modest_attachments_view_class_init,   /* class_init */
218                   NULL,   /* class_finalize */
219                   NULL,   /* class_data */
220                   sizeof (ModestAttachmentsView),
221                   0,      /* n_preallocs */
222                   modest_attachments_view_instance_init    /* instance_init */
223                 };
224
225                 type = g_type_register_static (GTK_TYPE_VBOX,
226                         "ModestAttachmentsView",
227                         &info, 0);
228
229         }
230
231         return type;
232 }