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