* small cleanups, refactor, remove debug spam
[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 <modest-header-view.h>
31 #include <modest-header-view-priv.h>
32 #include <modest-icon-names.h>
33 #include <modest-text-utils.h>
34 #include <modest-runtime.h>
35 #include <glib/gi18n.h>
36
37 static GdkPixbuf*
38 get_cached_icon (const gchar *name)
39 {
40         GError *err = NULL;
41         gpointer pixbuf;
42         gpointer orig_key;
43         static GHashTable *icon_cache = NULL;
44         
45         g_return_val_if_fail (name, NULL);
46
47         if (G_UNLIKELY(!icon_cache))
48                 icon_cache = modest_cache_mgr_get_cache (modest_runtime_get_cache_mgr(),
49                                                          MODEST_CACHE_MGR_CACHE_TYPE_PIXBUF);
50         
51         if (!icon_cache || !g_hash_table_lookup_extended (icon_cache, name, &orig_key,&pixbuf)) {
52                 pixbuf = (gpointer)gdk_pixbuf_new_from_file (name, &err);
53                 if (!pixbuf) {
54                         g_printerr ("modest: error while loading '%s': %s\n",
55                                     name, err->message);
56                         g_error_free (err);
57                 }
58                 /* if we cannot find it, we still insert (if we have a cache), so we get the error
59                  * only once */
60                 if (icon_cache)
61                         g_hash_table_insert (icon_cache, g_strdup(name),(gpointer)pixbuf);
62         }
63         return GDK_PIXBUF(pixbuf);
64 }
65
66
67
68 /*
69  * optimization
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         
80         switch (flag) {
81         case TNY_HEADER_FLAG_DELETED:
82                 if (G_UNLIKELY(!deleted_pixbuf))
83                         deleted_pixbuf = get_cached_icon (MODEST_HEADER_ICON_DELETED);
84                 return deleted_pixbuf;
85         case TNY_HEADER_FLAG_SEEN:
86                 if (G_UNLIKELY(!seen_pixbuf))
87                         seen_pixbuf = get_cached_icon (MODEST_HEADER_ICON_READ);
88                 return seen_pixbuf;
89         case TNY_HEADER_FLAG_ATTACHMENTS:
90                 if (G_UNLIKELY(!attachments_pixbuf))
91                         attachments_pixbuf = get_cached_icon (MODEST_HEADER_ICON_ATTACH);
92                 return attachments_pixbuf;
93         default:
94                 if (G_UNLIKELY(!unread_pixbuf))
95                         unread_pixbuf = get_cached_icon (MODEST_HEADER_ICON_UNREAD);
96                 return unread_pixbuf;
97         }
98 }
99
100 static void
101 set_common_flags (GtkCellRenderer *renderer, TnyHeaderFlags flags)
102 {
103         g_object_set (G_OBJECT(renderer),
104                       "weight", (flags & TNY_HEADER_FLAG_SEEN) ? 400: 800,
105                       "strikethrough",  (flags & TNY_HEADER_FLAG_DELETED) ?  TRUE:FALSE,
106                       NULL);    
107 }
108
109
110 void
111 _modest_header_view_msgtype_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
112                    GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer user_data)
113 {
114         TnyHeaderFlags flags;
115                 
116         gtk_tree_model_get (tree_model, iter, TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN,
117                             &flags, -1);
118
119         if (flags & TNY_HEADER_FLAG_DELETED)
120                 g_object_set (G_OBJECT (renderer), "pixbuf",
121                               get_pixbuf_for_flag (TNY_HEADER_FLAG_DELETED), NULL);           
122         else if (flags & TNY_HEADER_FLAG_SEEN)
123                 g_object_set (G_OBJECT (renderer), "pixbuf",
124                               get_pixbuf_for_flag (TNY_HEADER_FLAG_SEEN), NULL);              
125         else 
126                 g_object_set (G_OBJECT (renderer), "pixbuf",
127                               get_pixbuf_for_flag (0), NULL); /* ughh, FIXME */               
128 }
129
130 void
131 _modest_header_view_attach_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
132                                       GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer user_data)
133 {
134         TnyHeaderFlags flags;
135
136         gtk_tree_model_get (tree_model, iter, TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN,
137                             &flags, -1);
138
139         if (flags & TNY_HEADER_FLAG_ATTACHMENTS)
140                 g_object_set (G_OBJECT (renderer), "pixbuf",
141                               get_pixbuf_for_flag (TNY_HEADER_FLAG_ATTACHMENTS),
142                               NULL);
143 }
144
145 void
146 _modest_header_view_header_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
147                   GtkTreeModel *tree_model,  GtkTreeIter *iter,  gpointer user_data)
148 {
149         TnyHeaderFlags flags;
150         
151         gtk_tree_model_get (tree_model, iter, TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN,
152                             &flags, -1);
153         set_common_flags (renderer, flags);
154 }
155
156
157 void
158 _modest_header_view_date_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
159                                      GtkTreeModel *tree_model,  GtkTreeIter *iter,
160                                      gpointer user_data)
161 {
162         TnyHeaderFlags flags;
163         guint date, date_col;
164         gchar *display_date;
165         gboolean received = GPOINTER_TO_INT(user_data);
166
167         if (received)
168                 date_col = TNY_GTK_HEADER_LIST_MODEL_DATE_RECEIVED_TIME_T_COLUMN;
169         else
170                 date_col = TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN;
171
172         gtk_tree_model_get (tree_model, iter,
173                             TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN, &flags,
174                             date_col, &date,
175                             -1);
176
177         display_date = modest_text_utils_get_display_date (date);
178         g_object_set (G_OBJECT(renderer),
179                       "text",    display_date,
180                       NULL);
181
182         set_common_flags (renderer, flags);
183         g_free (display_date);
184 }
185
186
187 void
188 _modest_header_view_sender_receiver_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
189                             GtkTreeModel *tree_model,  GtkTreeIter *iter,  gboolean is_sender)
190 {
191         TnyHeaderFlags flags;
192         gchar *address;
193         gint sender_receiver_col;
194
195         if (is_sender)
196                 sender_receiver_col = TNY_GTK_HEADER_LIST_MODEL_FROM_COLUMN;
197         else
198                 sender_receiver_col = TNY_GTK_HEADER_LIST_MODEL_TO_COLUMN;
199                 
200         gtk_tree_model_get (tree_model, iter,
201                             sender_receiver_col,  &address,
202                             TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN, &flags,
203                             -1);
204         
205         g_object_set (G_OBJECT(renderer),
206                       "text",
207                       modest_text_utils_get_display_address (address),
208                       NULL);
209         g_free (address);
210         set_common_flags (renderer, flags);
211 }
212 /*
213  * this for both incoming and outgoing mail, depending on the the user_data
214  * parameter. in the incoming case, show 'From' and 'Received date', in the
215  * outgoing case, show 'To' and 'Sent date'
216  */
217 void
218 _modest_header_view_compact_header_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
219                                                GtkTreeModel *tree_model,  GtkTreeIter *iter,  gpointer user_data)
220 {
221         GObject *rendobj;
222         TnyHeaderFlags flags;
223         gchar *address, *subject, *header, *display_date;
224         time_t date;
225         gboolean is_incoming;
226
227         is_incoming = GPOINTER_TO_INT(user_data); /* GPOINTER_TO_BOOLEAN is not available
228                                                    * in older versions of glib...*/
229
230         if (is_incoming)
231                 gtk_tree_model_get (tree_model, iter,
232                                     TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN, &flags,
233                                     TNY_GTK_HEADER_LIST_MODEL_FROM_COLUMN,  &address,
234                                     TNY_GTK_HEADER_LIST_MODEL_SUBJECT_COLUMN, &subject,
235                                     TNY_GTK_HEADER_LIST_MODEL_DATE_RECEIVED_TIME_T_COLUMN, &date,   
236                                     -1);
237         else
238                 gtk_tree_model_get (tree_model, iter,
239                                     TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN, &flags,
240                                     TNY_GTK_HEADER_LIST_MODEL_TO_COLUMN,  &address,
241                                     TNY_GTK_HEADER_LIST_MODEL_SUBJECT_COLUMN, &subject,
242                                     TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN, &date,   
243                                     -1);
244         
245         rendobj = G_OBJECT(renderer);
246         display_date = modest_text_utils_get_display_date (date);
247         header = g_strdup_printf ("%s %s\n%s",
248                                   modest_text_utils_get_display_address (address),
249                                   display_date, subject);
250         g_free (address);
251         g_free (subject);
252         g_free (display_date);
253         
254         g_object_set (G_OBJECT(renderer), "text", header, NULL);        
255         set_common_flags (renderer, flags);
256         
257         g_free (header);
258 }
259
260
261 void
262 _modest_header_view_size_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
263                                      GtkTreeModel *tree_model,  GtkTreeIter *iter,
264                                      gpointer user_data)
265 {
266         TnyHeaderFlags flags;
267         guint size;
268         gchar *size_str;
269         
270         gtk_tree_model_get (tree_model, iter,
271                            TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN, &flags,
272                            TNY_GTK_HEADER_LIST_MODEL_MESSAGE_SIZE_COLUMN, &size,
273                             -1);
274         
275         size_str = modest_text_utils_get_display_size (size);
276         
277         g_object_set (G_OBJECT(renderer), "text", size_str, NULL);
278         set_common_flags (renderer, flags);
279
280         g_free (size_str);
281  }