* Changes in the autotools stuff affecting a lot of platform dependent
[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), 
140                       "text", text, 
141                       "weight", (flags & TNY_HEADER_FLAG_SEEN) ? PANGO_WEIGHT_NORMAL : PANGO_WEIGHT_ULTRABOLD,
142                       "strikethrough", (flags &TNY_HEADER_FLAG_DELETED) ? TRUE : FALSE,
143                       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 void
158 _modest_header_view_attach_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
159                                       GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer user_data)
160 {
161         TnyHeaderFlags flags;
162
163         gtk_tree_model_get (tree_model, iter, TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN,
164                             &flags, -1);
165
166         if (flags & TNY_HEADER_FLAG_ATTACHMENTS)
167                 g_object_set (G_OBJECT (renderer), "pixbuf",
168                               get_pixbuf_for_flag (TNY_HEADER_FLAG_ATTACHMENTS),
169                               NULL);
170 }
171
172 void
173 _modest_header_view_header_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
174                   GtkTreeModel *tree_model,  GtkTreeIter *iter,  gpointer user_data)
175 {
176         TnyHeaderFlags flags;
177         
178         gtk_tree_model_get (tree_model, iter, TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN,
179                             &flags, -1);
180         set_common_flags (renderer, flags);
181 }
182
183
184 void
185 _modest_header_view_date_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
186                                      GtkTreeModel *tree_model,  GtkTreeIter *iter,
187                                      gpointer user_data)
188 {
189         TnyHeaderFlags flags;
190         guint date, date_col;
191         gboolean received = GPOINTER_TO_INT(user_data);
192
193         if (received)
194                 date_col = TNY_GTK_HEADER_LIST_MODEL_DATE_RECEIVED_TIME_T_COLUMN;
195         else
196                 date_col = TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN;
197
198         gtk_tree_model_get (tree_model, iter,
199                             TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN, &flags,
200                             date_col, &date,
201                             -1);
202         
203         set_cell_text (renderer, modest_text_utils_get_display_date (date),
204                        flags, RENDER_CELL_STYLE_DEFAULT);
205 }
206
207 void
208 _modest_header_view_sender_receiver_cell_data  (GtkTreeViewColumn *column,  
209                                                 GtkCellRenderer *renderer,
210                                                 GtkTreeModel *tree_model,  
211                                                 GtkTreeIter *iter,  
212                                                 gboolean is_sender)
213 {
214         TnyHeaderFlags flags;
215         gchar *address;
216         gint sender_receiver_col;
217
218         if (is_sender)
219                 sender_receiver_col = TNY_GTK_HEADER_LIST_MODEL_FROM_COLUMN;
220         else
221                 sender_receiver_col = TNY_GTK_HEADER_LIST_MODEL_TO_COLUMN;
222                 
223         gtk_tree_model_get (tree_model, iter,
224                             sender_receiver_col,  &address,
225                             TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN, &flags,
226                             -1);
227         
228         modest_text_utils_get_display_address (address); /* string is changed in-place */
229         set_cell_text (renderer, address, flags, RENDER_CELL_STYLE_DEFAULT);
230         g_free (address);
231 }
232 /*
233  * this for both incoming and outgoing mail, depending on the the user_data
234  * parameter. in the incoming case, show 'From' and 'Received date', in the
235  * outgoing case, show 'To' and 'Sent date'
236  */
237 void
238 _modest_header_view_compact_header_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
239                                                GtkTreeModel *tree_model,  GtkTreeIter *iter,  gpointer user_data)
240 {
241         /* Note that GtkTreeModel is a GtkTreeModelFilter. */
242         
243         /* printf ("DEBUG: %s: tree_model gtype=%s\n", __FUNCTION__, G_OBJECT_TYPE_NAME (tree_model)); */
244         
245         TnyHeaderFlags flags = 0;
246         gchar *address = NULL;
247         gchar *subject = NULL;
248         time_t date;
249
250 #ifdef MAEMO_CHANGES
251 #ifdef HAVE_GTK_TREE_VIEW_COLUMN_GET_CELL_DATA_HINT
252         GtkTreeCellDataHint hint;
253 #endif
254 #endif
255         
256         GtkCellRenderer *recipient_cell, *date_or_status_cell, *subject_cell,
257                 *attach_cell, *priority_cell,
258                 *recipient_box, *subject_box = NULL;
259         TnyHeader *msg_header = NULL;
260         TnyHeaderFlags prio = 0;
261
262
263         g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (column));
264         g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
265         g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
266
267 #ifdef MAEMO_CHANGES
268 #ifdef HAVE_GTK_TREE_VIEW_COLUMN_GET_CELL_DATA_HINT
269         hint = gtk_tree_view_column_get_cell_data_hint (GTK_TREE_VIEW_COLUMN (column));
270
271         if (hint != GTK_TREE_CELL_DATA_HINT_ALL)
272                 return;
273 #endif
274 #endif
275         
276         recipient_box = GTK_CELL_RENDERER (g_object_get_data (G_OBJECT (renderer), "recpt-box-renderer"));
277         subject_box = GTK_CELL_RENDERER (g_object_get_data (G_OBJECT (renderer), "subject-box-renderer"));
278         priority_cell = GTK_CELL_RENDERER (g_object_get_data (G_OBJECT (subject_box), "priority-renderer"));
279         subject_cell = GTK_CELL_RENDERER (g_object_get_data (G_OBJECT (subject_box), "subject-renderer"));
280         attach_cell = GTK_CELL_RENDERER (g_object_get_data (G_OBJECT (recipient_box), "attach-renderer"));
281         recipient_cell = GTK_CELL_RENDERER (g_object_get_data (G_OBJECT (recipient_box), "recipient-renderer"));
282         date_or_status_cell = GTK_CELL_RENDERER (g_object_get_data (G_OBJECT (recipient_box), "date-renderer"));
283
284         ModestHeaderViewCompactHeaderMode header_mode = GPOINTER_TO_INT (user_data); 
285
286         if (header_mode == MODEST_HEADER_VIEW_COMPACT_HEADER_MODE_IN)
287                 gtk_tree_model_get (tree_model, iter,
288                                     TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN, &flags,
289                                     TNY_GTK_HEADER_LIST_MODEL_FROM_COLUMN,  &address,
290                                     TNY_GTK_HEADER_LIST_MODEL_SUBJECT_COLUMN, &subject,
291                                     TNY_GTK_HEADER_LIST_MODEL_DATE_RECEIVED_TIME_T_COLUMN, &date,
292                                     TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN, &msg_header,
293                                     -1);
294         else
295                 gtk_tree_model_get (tree_model, iter,
296                                     TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN, &flags,
297                                     TNY_GTK_HEADER_LIST_MODEL_TO_COLUMN,  &address,
298                                     TNY_GTK_HEADER_LIST_MODEL_SUBJECT_COLUMN, &subject,
299                                     TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN, &date,
300                                     TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN, &msg_header,
301                                     -1);        
302         /* flags */
303         /* FIXME: we might gain something by doing all the g_object_set's at once */
304         if (flags & TNY_HEADER_FLAG_ATTACHMENTS)
305                 g_object_set (G_OBJECT (attach_cell), "pixbuf",
306                               get_pixbuf_for_flag (TNY_HEADER_FLAG_ATTACHMENTS),
307                               NULL);
308         else
309                 g_object_set (G_OBJECT (attach_cell), "pixbuf",
310                               NULL, NULL);
311
312         if (msg_header)
313                 prio = tny_header_get_priority (msg_header);
314         g_object_set (G_OBJECT (priority_cell), "pixbuf",
315                       get_pixbuf_for_flag (prio), 
316                       NULL);
317
318         set_cell_text (subject_cell, (subject && subject[0] != 0)?subject:_("mail_va_no_subject"), 
319                        flags, RENDER_CELL_STYLE_DEFAULT);
320         g_free (subject);
321
322         /* FIXME: we hardcode the color to #666666; instead we should use SecondaryTextColour from the
323          * theme (gtkrc file) */
324         modest_text_utils_get_display_address (address); /* changed in-place */
325         set_cell_text (recipient_cell, address, flags, RENDER_CELL_STYLE_GREY);
326         g_free (address);
327         
328         if (header_mode == MODEST_HEADER_VIEW_COMPACT_HEADER_MODE_OUTBOX) {
329                 ModestTnySendQueueStatus status = MODEST_TNY_SEND_QUEUE_UNKNOWN;
330                 const gchar *status_str = "";
331                 if (msg_header != NULL) {
332                         status = modest_tny_all_send_queues_get_msg_status (msg_header);
333                         if (status == MODEST_TNY_SEND_QUEUE_SUSPENDED) {
334                                 tny_header_set_flag (msg_header, TNY_HEADER_FLAG_SUSPENDED);
335                         }
336                 }
337                 
338                 status_str = get_status_string (status);
339                 set_cell_text (date_or_status_cell, status_str, flags, RENDER_CELL_STYLE_GREY);
340         } else {                
341                 set_cell_text (date_or_status_cell, date ? modest_text_utils_get_display_date (date) : "",
342                                flags, RENDER_CELL_STYLE_GREY);
343         }
344         
345         if (msg_header != NULL)
346                 g_object_unref (msg_header);
347 }
348
349
350 void
351 _modest_header_view_size_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
352                                      GtkTreeModel *tree_model,  GtkTreeIter *iter,
353                                      gpointer user_data)
354 {
355         TnyHeaderFlags flags;
356         guint size;
357         gchar *size_str;
358         
359         gtk_tree_model_get (tree_model, iter,
360                            TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN, &flags,
361                            TNY_GTK_HEADER_LIST_MODEL_MESSAGE_SIZE_COLUMN, &size,
362                             -1);
363         
364         size_str = modest_text_utils_get_display_size (size);
365         
366         set_cell_text (renderer, size_str, flags, RENDER_CELL_STYLE_DEFAULT);
367
368         g_free (size_str);
369  }
370
371 void
372 _modest_header_view_status_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
373                                        GtkTreeModel *tree_model,  GtkTreeIter *iter,
374                                        gpointer user_data)
375 {
376         TnyHeaderFlags flags;
377         //guint status;
378         gchar *status_str;
379         
380         gtk_tree_model_get (tree_model, iter,
381                             TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN, &flags,
382                             -1);
383
384        if (flags & TNY_HEADER_FLAG_SUSPENDED)
385                status_str = g_strdup(_("mcen_li_outbox_suspended"));
386        else            
387                status_str = g_strdup(_("mcen_li_outbox_waiting"));
388        
389         set_cell_text (renderer, status_str, flags, RENDER_CELL_STYLE_DEFAULT);
390
391         g_free (status_str);
392  }
393