df8f9a0038fa71a410ea195ec959c6b36ab7a4aa
[modest] / src / widgets / modest-header-view-render.c
1 /* Copyright (c) 2006, 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 <tny-simple-list.h>
31 #include <modest-header-view.h>
32 #include <modest-header-view-priv.h>
33 #include <modest-icon-names.h>
34 #include <modest-text-utils.h>
35 #include <modest-tny-account-store.h>
36 #include <modest-tny-send-queue.h>
37 #include <modest-tny-folder.h>
38 #include <modest-tny-account.h>
39 #include <modest-runtime.h>
40 #include <glib/gi18n.h>
41 #include <modest-platform.h>
42 #include <string.h>
43
44 typedef enum {
45         RENDER_CELL_STYLE_DEFAULT=0,
46         RENDER_CELL_STYLE_GREY
47 } RenderCellStyle;
48
49 static const gchar *
50 get_status_string (ModestTnySendQueueStatus status)
51 {
52         switch (status) {
53         case MODEST_TNY_SEND_QUEUE_WAITING:
54                 return _("mcen_li_outbox_waiting");
55                 break;
56         case MODEST_TNY_SEND_QUEUE_SENDING:
57                 return _("mcen_li_outbox_sending");
58                 break;
59         case MODEST_TNY_SEND_QUEUE_SUSPENDED:
60                 return _("mcen_li_outbox_suspended");
61                 break;
62         case MODEST_TNY_SEND_QUEUE_FAILED:
63                 return _("mcen_li_outbox_failed");
64                 break;
65         default:
66                 return "";
67                 break;
68         }
69 }
70
71 static GdkPixbuf*
72 get_pixbuf_for_flag (TnyHeaderFlags flag)
73 {
74         /* optimization */
75         static GdkPixbuf *deleted_pixbuf          = NULL;
76         static GdkPixbuf *seen_pixbuf             = NULL;
77         static GdkPixbuf *unread_pixbuf           = NULL;
78         static GdkPixbuf *attachments_pixbuf      = NULL;
79         static GdkPixbuf *high_pixbuf             = NULL;
80         static GdkPixbuf *low_pixbuf             = NULL;
81         
82         switch (flag) {
83         case TNY_HEADER_FLAG_DELETED:
84                 if (G_UNLIKELY(!deleted_pixbuf))
85                         deleted_pixbuf = modest_platform_get_icon (MODEST_HEADER_ICON_DELETED,
86                                                                    MODEST_ICON_SIZE_SMALL);
87                 return deleted_pixbuf;
88         case TNY_HEADER_FLAG_SEEN:
89                 if (G_UNLIKELY(!seen_pixbuf))
90                         seen_pixbuf = modest_platform_get_icon (MODEST_HEADER_ICON_READ,
91                                                                 MODEST_ICON_SIZE_SMALL);
92                 return seen_pixbuf;
93         case TNY_HEADER_FLAG_ATTACHMENTS:
94                 if (G_UNLIKELY(!attachments_pixbuf))
95                         attachments_pixbuf = modest_platform_get_icon (MODEST_HEADER_ICON_ATTACH,
96                                                                        MODEST_ICON_SIZE_SMALL);
97                 return attachments_pixbuf;
98         case TNY_HEADER_FLAG_HIGH_PRIORITY:
99                 if (G_UNLIKELY(!high_pixbuf))
100                         high_pixbuf = modest_platform_get_icon (MODEST_HEADER_ICON_HIGH,
101                                                                 MODEST_ICON_SIZE_SMALL);
102                 return high_pixbuf;
103         case TNY_HEADER_FLAG_LOW_PRIORITY:
104                 if (G_UNLIKELY(!low_pixbuf))
105                         low_pixbuf = modest_platform_get_icon (MODEST_HEADER_ICON_LOW,
106                                                                MODEST_ICON_SIZE_SMALL);
107                 return low_pixbuf;
108         case TNY_HEADER_FLAG_NORMAL_PRIORITY:
109                 return NULL;
110         default:
111                 if (G_UNLIKELY(!unread_pixbuf))
112                         unread_pixbuf = modest_platform_get_icon (MODEST_HEADER_ICON_UNREAD,
113                                                                   MODEST_ICON_SIZE_SMALL);
114                 return unread_pixbuf;
115         }
116 }
117
118 static void
119 set_common_flags (GtkCellRenderer *renderer, TnyHeaderFlags flags)
120 {
121         g_object_set (G_OBJECT(renderer),
122                       "weight", (flags & TNY_HEADER_FLAG_SEEN) ? PANGO_WEIGHT_NORMAL: PANGO_WEIGHT_ULTRABOLD,
123                       "strikethrough",  (flags & TNY_HEADER_FLAG_DELETED) ?  TRUE:FALSE,
124                       NULL);    
125 }
126
127 static void
128 set_cell_text (GtkCellRenderer *renderer, 
129                const gchar *text, 
130                TnyHeaderFlags flags,
131                RenderCellStyle style)
132 {
133         PangoWeight weight;
134         gboolean strikethrough;
135
136         weight =  (flags & TNY_HEADER_FLAG_SEEN) ? PANGO_WEIGHT_NORMAL: PANGO_WEIGHT_ULTRABOLD;
137         strikethrough = (flags & TNY_HEADER_FLAG_DELETED) ?  TRUE:FALSE;
138         g_object_freeze_notify (G_OBJECT (renderer));
139         g_object_set (G_OBJECT (renderer), "text", text, NULL);
140         if (!(flags & TNY_HEADER_FLAG_SEEN))
141                 g_object_set (G_OBJECT (renderer), "weight", PANGO_WEIGHT_ULTRABOLD, NULL);
142         if (flags & TNY_HEADER_FLAG_DELETED)
143                 g_object_set (G_OBJECT (renderer), "strikethrough", TRUE, NULL);
144         switch (style) {
145         case RENDER_CELL_STYLE_GREY:
146                 g_object_set (G_OBJECT (renderer), 
147                               "foreground", "#666666",
148                               "scale", PANGO_SCALE_SMALL,
149                               NULL);
150                 break;
151         case RENDER_CELL_STYLE_DEFAULT:
152                 break;
153         }
154         g_object_thaw_notify (G_OBJECT (renderer));
155 }
156
157
158 void
159 _modest_header_view_msgtype_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
160                    GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer user_data)
161 {
162         TnyHeaderFlags flags;
163                 
164         gtk_tree_model_get (tree_model, iter, TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN,
165                             &flags, -1);
166
167         if (flags & TNY_HEADER_FLAG_DELETED)
168                 g_object_set (G_OBJECT (renderer), "pixbuf",
169                               get_pixbuf_for_flag (TNY_HEADER_FLAG_DELETED), NULL);           
170         else if (flags & TNY_HEADER_FLAG_SEEN)
171                 g_object_set (G_OBJECT (renderer), "pixbuf",
172                               get_pixbuf_for_flag (TNY_HEADER_FLAG_SEEN), NULL);              
173         else 
174                 g_object_set (G_OBJECT (renderer), "pixbuf",
175                               get_pixbuf_for_flag (0), NULL); /* ughh, FIXME */               
176 }
177
178 void
179 _modest_header_view_attach_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
180                                       GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer user_data)
181 {
182         TnyHeaderFlags flags;
183
184         gtk_tree_model_get (tree_model, iter, TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN,
185                             &flags, -1);
186
187         if (flags & TNY_HEADER_FLAG_ATTACHMENTS)
188                 g_object_set (G_OBJECT (renderer), "pixbuf",
189                               get_pixbuf_for_flag (TNY_HEADER_FLAG_ATTACHMENTS),
190                               NULL);
191 }
192
193 void
194 _modest_header_view_header_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
195                   GtkTreeModel *tree_model,  GtkTreeIter *iter,  gpointer user_data)
196 {
197         TnyHeaderFlags flags;
198         
199         gtk_tree_model_get (tree_model, iter, TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN,
200                             &flags, -1);
201         set_common_flags (renderer, flags);
202 }
203
204
205 void
206 _modest_header_view_date_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
207                                      GtkTreeModel *tree_model,  GtkTreeIter *iter,
208                                      gpointer user_data)
209 {
210         TnyHeaderFlags flags;
211         guint date, date_col;
212         gboolean received = GPOINTER_TO_INT(user_data);
213
214         if (received)
215                 date_col = TNY_GTK_HEADER_LIST_MODEL_DATE_RECEIVED_TIME_T_COLUMN;
216         else
217                 date_col = TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN;
218
219         gtk_tree_model_get (tree_model, iter,
220                             TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN, &flags,
221                             date_col, &date,
222                             -1);
223         
224         set_cell_text (renderer, modest_text_utils_get_display_date (date),
225                        flags, RENDER_CELL_STYLE_DEFAULT);
226 }
227
228 void
229 _modest_header_view_sender_receiver_cell_data  (GtkTreeViewColumn *column,  
230                                                 GtkCellRenderer *renderer,
231                                                 GtkTreeModel *tree_model,  
232                                                 GtkTreeIter *iter,  
233                                                 gboolean is_sender)
234 {
235         TnyHeaderFlags flags;
236         gchar *address;
237         gint sender_receiver_col;
238
239         if (is_sender)
240                 sender_receiver_col = TNY_GTK_HEADER_LIST_MODEL_FROM_COLUMN;
241         else
242                 sender_receiver_col = TNY_GTK_HEADER_LIST_MODEL_TO_COLUMN;
243                 
244         gtk_tree_model_get (tree_model, iter,
245                             sender_receiver_col,  &address,
246                             TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN, &flags,
247                             -1);
248         
249         modest_text_utils_get_display_address (address); /* string is changed in-place */
250         set_cell_text (renderer, address, flags, RENDER_CELL_STYLE_DEFAULT);
251         g_free (address);
252 }
253 /*
254  * this for both incoming and outgoing mail, depending on the the user_data
255  * parameter. in the incoming case, show 'From' and 'Received date', in the
256  * outgoing case, show 'To' and 'Sent date'
257  */
258 void
259 _modest_header_view_compact_header_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
260                                                GtkTreeModel *tree_model,  GtkTreeIter *iter,  gpointer user_data)
261 {
262         /* Note that GtkTreeModel is a GtkTreeModelFilter. */
263         
264         /* printf ("DEBUG: %s: tree_model gtype=%s\n", __FUNCTION__, G_OBJECT_TYPE_NAME (tree_model)); */
265         
266         TnyHeaderFlags flags = 0;
267         gchar *address = NULL;
268         gchar *subject = NULL;
269         time_t date;
270         
271         GtkCellRenderer *recipient_cell, *date_or_status_cell, *subject_cell,
272                 *attach_cell, *priority_cell,
273                 *recipient_box, *subject_box = NULL;
274         TnyHeader *msg_header = NULL;
275         TnyHeaderFlags prio = 0;
276
277
278         g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (column));
279         g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
280         g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
281         
282         recipient_box = GTK_CELL_RENDERER (g_object_get_data (G_OBJECT (renderer), "recpt-box-renderer"));
283         subject_box = GTK_CELL_RENDERER (g_object_get_data (G_OBJECT (renderer), "subject-box-renderer"));
284         priority_cell = GTK_CELL_RENDERER (g_object_get_data (G_OBJECT (subject_box), "priority-renderer"));
285         subject_cell = GTK_CELL_RENDERER (g_object_get_data (G_OBJECT (subject_box), "subject-renderer"));
286         attach_cell = GTK_CELL_RENDERER (g_object_get_data (G_OBJECT (recipient_box), "attach-renderer"));
287         recipient_cell = GTK_CELL_RENDERER (g_object_get_data (G_OBJECT (recipient_box), "recipient-renderer"));
288         date_or_status_cell = GTK_CELL_RENDERER (g_object_get_data (G_OBJECT (recipient_box), "date-renderer"));
289
290         ModestHeaderViewCompactHeaderMode header_mode = GPOINTER_TO_INT (user_data); 
291
292         if (header_mode == MODEST_HEADER_VIEW_COMPACT_HEADER_MODE_IN)
293                 gtk_tree_model_get (tree_model, iter,
294                                     TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN, &flags,
295                                     TNY_GTK_HEADER_LIST_MODEL_FROM_COLUMN,  &address,
296                                     TNY_GTK_HEADER_LIST_MODEL_SUBJECT_COLUMN, &subject,
297                                     TNY_GTK_HEADER_LIST_MODEL_DATE_RECEIVED_TIME_T_COLUMN, &date,
298                                     TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN, &msg_header,
299                                     -1);
300         else
301                 gtk_tree_model_get (tree_model, iter,
302                                     TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN, &flags,
303                                     TNY_GTK_HEADER_LIST_MODEL_TO_COLUMN,  &address,
304                                     TNY_GTK_HEADER_LIST_MODEL_SUBJECT_COLUMN, &subject,
305                                     TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN, &date,
306                                     TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN, &msg_header,
307                                     -1);        
308         /* flags */
309         /* FIXME: we might gain something by doing all the g_object_set's at once */
310         if (flags & TNY_HEADER_FLAG_ATTACHMENTS)
311                 g_object_set (G_OBJECT (attach_cell), "pixbuf",
312                               get_pixbuf_for_flag (TNY_HEADER_FLAG_ATTACHMENTS),
313                               NULL);
314         else
315                 g_object_set (G_OBJECT (attach_cell), "pixbuf",
316                               NULL, NULL);
317
318         if (msg_header)
319                 prio = tny_header_get_priority (msg_header);
320         g_object_set (G_OBJECT (priority_cell), "pixbuf",
321                       get_pixbuf_for_flag (prio), 
322                       NULL);
323
324         set_cell_text (subject_cell, (subject && subject[0] != 0)?subject:_("mail_va_no_subject"), 
325                        flags, RENDER_CELL_STYLE_DEFAULT);
326         g_free (subject);
327
328         /* FIXME: we hardcode the color to #666666; instead we should use SecondaryTextColour from the
329          * theme (gtkrc file) */
330         modest_text_utils_get_display_address (address); /* changed in-place */
331         set_cell_text (recipient_cell, address, flags, RENDER_CELL_STYLE_GREY);
332         
333         if (header_mode == MODEST_HEADER_VIEW_COMPACT_HEADER_MODE_OUTBOX) {
334                 ModestTnySendQueueStatus status = MODEST_TNY_SEND_QUEUE_UNKNOWN;
335                 const gchar *status_str = "";
336                 if (msg_header != NULL) {
337                         status = modest_tny_all_send_queues_get_msg_status (msg_header);
338                         if (status == MODEST_TNY_SEND_QUEUE_SUSPENDED) {
339                                 tny_header_set_flag (msg_header, TNY_HEADER_FLAG_SUSPENDED);
340                         }
341                 }
342                 
343                 status_str = get_status_string (status);
344                 set_cell_text (date_or_status_cell, status_str, flags, RENDER_CELL_STYLE_GREY);
345         } else {                
346                 set_cell_text (date_or_status_cell, date ? modest_text_utils_get_display_date (date) : "",
347                                flags, RENDER_CELL_STYLE_GREY);
348         }
349         
350         if (msg_header != NULL)
351                 g_object_unref (msg_header);
352 }
353
354
355 void
356 _modest_header_view_size_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
357                                      GtkTreeModel *tree_model,  GtkTreeIter *iter,
358                                      gpointer user_data)
359 {
360         TnyHeaderFlags flags;
361         guint size;
362         gchar *size_str;
363         
364         gtk_tree_model_get (tree_model, iter,
365                            TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN, &flags,
366                            TNY_GTK_HEADER_LIST_MODEL_MESSAGE_SIZE_COLUMN, &size,
367                             -1);
368         
369         size_str = modest_text_utils_get_display_size (size);
370         
371         set_cell_text (renderer, size_str, flags, RENDER_CELL_STYLE_DEFAULT);
372
373         g_free (size_str);
374  }
375
376 void
377 _modest_header_view_status_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
378                                        GtkTreeModel *tree_model,  GtkTreeIter *iter,
379                                        gpointer user_data)
380 {
381         TnyHeaderFlags flags;
382         //guint status;
383         gchar *status_str;
384         
385         gtk_tree_model_get (tree_model, iter,
386                             TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN, &flags,
387                             -1);
388
389        if (flags & TNY_HEADER_FLAG_SUSPENDED)
390                status_str = g_strdup(_("mcen_li_outbox_suspended"));
391        else            
392                status_str = g_strdup(_("mcen_li_outbox_waiting"));
393        
394         set_cell_text (renderer, status_str, flags, RENDER_CELL_STYLE_DEFAULT);
395
396         g_free (status_str);
397  }
398