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