eeb871cb0dea25133623c8b58caa1ee3a9488320
[modest] / src / modest-tny-folder-tree-view.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 <glib/gi18n.h>
31 #include <string.h>
32
33 #include <tny-account-tree-model.h>
34 #include <tny-account-store-iface.h>
35 #include <tny-account-iface.h>
36 #include <tny-msg-folder-iface.h>
37 #include <tny-summary-window-iface.h>
38
39 #include "modest-tny-folder-tree-view.h"
40
41 #include <modest-icon-names.h>
42 #include "modest-icon-factory.h"
43
44
45 /* 'private'/'protected' functions */
46 static void modest_tny_folder_tree_view_class_init  (ModestTnyFolderTreeViewClass *klass);
47 static void modest_tny_folder_tree_view_init        (ModestTnyFolderTreeView *obj);
48 static void modest_tny_folder_tree_view_finalize    (GObject *obj);
49
50 //static void modest_tny_folder_tree_view_iface_init   (gpointer iface, gpointer data);
51 static void modest_tny_folder_tree_view_set_account_store (TnySummaryWindowIface *self,
52                                                            TnyAccountStoreIface *account_store);
53 static gboolean update_model (ModestTnyFolderTreeView *self,TnyAccountStoreIface *iface);
54 static gboolean update_model_empty (ModestTnyFolderTreeView *self);
55
56 static void selection_changed (GtkTreeSelection *sel, gpointer data);
57
58 enum {
59         FOLDER_SELECTED_SIGNAL,
60         LAST_SIGNAL
61 };
62
63 typedef struct _ModestTnyFolderTreeViewPrivate ModestTnyFolderTreeViewPrivate;
64 struct _ModestTnyFolderTreeViewPrivate {
65
66         TnyAccountStoreIface *tny_account_store;
67         TnyMsgFolderIface *cur_folder;
68         gboolean view_is_empty;
69
70         GMutex *lock;
71 };
72 #define MODEST_TNY_FOLDER_TREE_VIEW_GET_PRIVATE(o)                      \
73         (G_TYPE_INSTANCE_GET_PRIVATE((o),                               \
74                                      MODEST_TYPE_TNY_FOLDER_TREE_VIEW,  \
75                                      ModestTnyFolderTreeViewPrivate))
76 /* globals */
77 static GObjectClass *parent_class = NULL;
78
79 static guint signals[LAST_SIGNAL] = {0}; 
80
81 GType
82 modest_tny_folder_tree_view_get_type (void)
83 {
84         static GType my_type = 0;
85         if (!my_type) {
86                 static const GTypeInfo my_info = {
87                         sizeof(ModestTnyFolderTreeViewClass),
88                         NULL,           /* base init */
89                         NULL,           /* base finalize */
90                         (GClassInitFunc) modest_tny_folder_tree_view_class_init,
91                         NULL,           /* class finalize */
92                         NULL,           /* class data */
93                         sizeof(ModestTnyFolderTreeView),
94                         1,              /* n_preallocs */
95                         (GInstanceInitFunc) modest_tny_folder_tree_view_init,
96                 };
97                                 
98                 my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
99                                                   "ModestTnyFolderTreeView",
100                                                   &my_info, 0);         
101         }
102         return my_type;
103 }
104
105 static void
106 modest_tny_folder_tree_view_class_init (ModestTnyFolderTreeViewClass *klass)
107 {
108         GObjectClass *gobject_class;
109         gobject_class = (GObjectClass*) klass;
110
111         parent_class            = g_type_class_peek_parent (klass);
112         gobject_class->finalize = modest_tny_folder_tree_view_finalize;
113         
114         klass->update_model = modest_tny_folder_tree_view_update_model;
115
116         g_type_class_add_private (gobject_class,
117                                   sizeof(ModestTnyFolderTreeViewPrivate));
118         
119         signals[FOLDER_SELECTED_SIGNAL] = 
120                 g_signal_new ("folder_selected",
121                               G_TYPE_FROM_CLASS (gobject_class),
122                               G_SIGNAL_RUN_FIRST,
123                               G_STRUCT_OFFSET (ModestTnyFolderTreeViewClass,
124                                                folder_selected),
125                               NULL, NULL,
126                               g_cclosure_marshal_VOID__POINTER,
127                               G_TYPE_NONE, 1, G_TYPE_POINTER); 
128 }
129
130
131
132 static void
133 text_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
134                    GtkTreeModel *tree_model,  GtkTreeIter *iter,  gpointer data)
135 {
136         GObject *rendobj;
137         gchar *fname;
138         gint unread;
139         TnyMsgFolderType type;
140         
141         gtk_tree_model_get (tree_model, iter,
142                             TNY_ACCOUNT_TREE_MODEL_NAME_COLUMN, &fname,
143                             TNY_ACCOUNT_TREE_MODEL_TYPE_COLUMN, &type,
144                             TNY_ACCOUNT_TREE_MODEL_UNREAD_COLUMN, &unread, -1);
145         rendobj = G_OBJECT(renderer);
146
147         if (unread > 0) {
148                 gchar *folder_title = g_strdup_printf ("%s (%d)", fname, unread);
149                 g_object_set (rendobj,"text", folder_title,  "weight", 800, NULL);
150                 g_free (folder_title);
151         } else 
152                 g_object_set (rendobj,"text", fname, "weight", 400, NULL);
153                 
154         g_free (fname);
155 }
156
157 /* FIXME: move these to TnyMail */
158 enum {
159
160         TNY_MSG_FOLDER_TYPE_NOTES = TNY_MSG_FOLDER_TYPE_SENT + 1, /* urgh */
161         TNY_MSG_FOLDER_TYPE_DRAFTS,
162         TNY_MSG_FOLDER_TYPE_CONTACTS,
163         TNY_MSG_FOLDER_TYPE_CALENDAR
164 };
165         
166 static TnyMsgFolderType
167 guess_folder_type (const gchar* name)
168 {
169         TnyMsgFolderType type;
170         gchar *folder;
171
172         g_return_val_if_fail (name, TNY_MSG_FOLDER_TYPE_NORMAL);
173         
174         type = TNY_MSG_FOLDER_TYPE_NORMAL;
175         folder = g_utf8_strdown (name, strlen(name));
176
177         if (strcmp (folder, "inbox") == 0 ||
178             strcmp (folder, _("inbox")) == 0)
179                 type = TNY_MSG_FOLDER_TYPE_INBOX;
180         else if (strcmp (folder, "outbox") == 0 ||
181                  strcmp (folder, _("outbox")) == 0)
182                 type = TNY_MSG_FOLDER_TYPE_OUTBOX;
183         else if (g_str_has_prefix(folder, "junk") ||
184                  g_str_has_prefix(folder, _("junk")))
185                 type = TNY_MSG_FOLDER_TYPE_JUNK;
186         else if (g_str_has_prefix(folder, "trash") ||
187                  g_str_has_prefix(folder, _("trash")))
188                 type = TNY_MSG_FOLDER_TYPE_JUNK;
189         else if (g_str_has_prefix(folder, "sent") ||
190                  g_str_has_prefix(folder, _("sent")))
191                 type = TNY_MSG_FOLDER_TYPE_SENT;
192
193         /* these are not *really* TNY_ types */
194         else if (g_str_has_prefix(folder, "draft") ||
195                  g_str_has_prefix(folder, _("draft")))
196                 type = TNY_MSG_FOLDER_TYPE_DRAFTS;
197         else if (g_str_has_prefix(folder, "notes") ||
198                  g_str_has_prefix(folder, _("notes")))
199                 type = TNY_MSG_FOLDER_TYPE_NOTES;
200         else if (g_str_has_prefix(folder, "contacts") ||
201                  g_str_has_prefix(folder, _("contacts")))
202                 type = TNY_MSG_FOLDER_TYPE_CONTACTS;
203         else if (g_str_has_prefix(folder, "calendar") ||
204                  g_str_has_prefix(folder, _("calendar")))
205                 type = TNY_MSG_FOLDER_TYPE_CALENDAR;
206         
207         g_free (folder);
208         return type;
209 }
210
211
212 static void
213 icon_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
214                  GtkTreeModel *tree_model,  GtkTreeIter *iter, gpointer data)
215 {
216         GObject *rendobj;
217         GdkPixbuf *pixbuf;
218         TnyMsgFolderType type;
219         gchar *fname = NULL;
220         gint unread;
221         
222         rendobj = G_OBJECT(renderer);
223         gtk_tree_model_get (tree_model, iter,
224                             TNY_ACCOUNT_TREE_MODEL_TYPE_COLUMN, &type,
225                             TNY_ACCOUNT_TREE_MODEL_NAME_COLUMN, &fname,
226                             TNY_ACCOUNT_TREE_MODEL_UNREAD_COLUMN, &unread, -1);
227         rendobj = G_OBJECT(renderer);
228         
229         if (type == TNY_MSG_FOLDER_TYPE_NORMAL)
230                 type = guess_folder_type (fname);
231         
232         if (fname);
233                 g_free (fname);
234
235         switch (type) {
236         case TNY_MSG_FOLDER_TYPE_INBOX:
237                 pixbuf = modest_icon_factory_get_icon (MODEST_FOLDER_ICON_INBOX);
238                 break;
239         case TNY_MSG_FOLDER_TYPE_OUTBOX:
240                 pixbuf = modest_icon_factory_get_icon (MODEST_FOLDER_ICON_OUTBOX);
241                 break;
242         case TNY_MSG_FOLDER_TYPE_JUNK:
243                 pixbuf = modest_icon_factory_get_icon (MODEST_FOLDER_ICON_JUNK);
244                 break;
245         case TNY_MSG_FOLDER_TYPE_SENT:
246                 pixbuf = modest_icon_factory_get_icon (MODEST_FOLDER_ICON_SENT);
247                 break;
248         case TNY_MSG_FOLDER_TYPE_DRAFTS:
249                 pixbuf = modest_icon_factory_get_icon (MODEST_FOLDER_ICON_DRAFTS);
250                 break;
251         case TNY_MSG_FOLDER_TYPE_NOTES:
252                 pixbuf = modest_icon_factory_get_icon (MODEST_FOLDER_ICON_NOTES);
253                 break;
254         case TNY_MSG_FOLDER_TYPE_CALENDAR:
255                 pixbuf = modest_icon_factory_get_icon (MODEST_FOLDER_ICON_CALENDAR);
256                 break;
257         case TNY_MSG_FOLDER_TYPE_CONTACTS:
258                 pixbuf = modest_icon_factory_get_icon (MODEST_FOLDER_ICON_CONTACTS);
259                 break;
260         case TNY_MSG_FOLDER_TYPE_NORMAL:
261         default:
262                 pixbuf = modest_icon_factory_get_icon (MODEST_FOLDER_ICON_NORMAL);
263                 break;
264         }
265
266         g_object_set (rendobj,
267                       "pixbuf-expander-open",
268                       modest_icon_factory_get_icon (MODEST_FOLDER_ICON_OPEN),
269                       "pixbuf-expander-closed",
270                       modest_icon_factory_get_icon (MODEST_FOLDER_ICON_CLOSED),
271                       "pixbuf", pixbuf,
272                       NULL);
273 }
274
275 static void
276 modest_tny_folder_tree_view_init (ModestTnyFolderTreeView *obj)
277 {
278         ModestTnyFolderTreeViewPrivate *priv;
279         GtkTreeViewColumn *column;
280         GtkCellRenderer *renderer;
281         GtkTreeSelection *sel;
282         
283         priv =  MODEST_TNY_FOLDER_TREE_VIEW_GET_PRIVATE(obj);
284         
285         priv->view_is_empty     = TRUE;
286         priv->tny_account_store = NULL;
287         priv->cur_folder = NULL;
288
289         priv->lock = g_mutex_new ();
290         
291         column = gtk_tree_view_column_new ();
292         gtk_tree_view_column_set_title (column,
293                                         _("All Mail Folders"));
294         
295         gtk_tree_view_append_column (GTK_TREE_VIEW(obj),
296                                      column);
297         
298         renderer = gtk_cell_renderer_pixbuf_new();
299         gtk_tree_view_column_pack_start (column, renderer, FALSE);
300         gtk_tree_view_column_set_cell_data_func(column, renderer,
301                                                 icon_cell_data, NULL, NULL);
302         
303         renderer = gtk_cell_renderer_text_new();
304         gtk_tree_view_column_pack_start (column, renderer, FALSE);
305         gtk_tree_view_column_set_cell_data_func(column, renderer,
306                                                 text_cell_data, NULL, NULL);
307         
308         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(obj));
309         gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
310
311         gtk_tree_view_column_set_spacing (column, 2);
312         gtk_tree_view_column_set_resizable (column, TRUE);
313         gtk_tree_view_column_set_fixed_width (column, TRUE);            
314         gtk_tree_view_set_headers_visible   (GTK_TREE_VIEW(obj), TRUE);
315         gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(obj), FALSE);
316
317 }
318
319
320 static void
321 modest_tny_folder_tree_view_finalize (GObject *obj)
322 {
323         ModestTnyFolderTreeViewPrivate *priv;
324
325         g_return_if_fail (obj);
326         
327         priv =  MODEST_TNY_FOLDER_TREE_VIEW_GET_PRIVATE(obj);
328         if (priv->tny_account_store) {
329                 g_object_unref (G_OBJECT(priv->tny_account_store));
330                 priv->tny_account_store = NULL;
331         }
332
333
334         if (priv->lock) {
335                 g_mutex_free (priv->lock);
336                 priv->lock = NULL;
337         }
338         
339         (*parent_class->finalize)(obj);
340 }
341
342
343 static void
344 modest_tny_folder_tree_view_set_account_store (TnySummaryWindowIface *self,
345                                                TnyAccountStoreIface *account_store)
346 {
347         ModestTnyFolderTreeViewPrivate *priv;
348
349         g_return_if_fail (self);
350         g_return_if_fail (account_store);
351         
352         priv = MODEST_TNY_FOLDER_TREE_VIEW_GET_PRIVATE(self);
353         if (priv->tny_account_store) {
354                 g_object_unref (priv->tny_account_store);
355                 priv->tny_account_store = NULL;
356         }
357
358         g_object_ref (G_OBJECT(priv->tny_account_store = account_store));
359 }
360
361
362
363 static void
364 on_accounts_update (TnyAccountStoreIface *account_store, const gchar *account,
365                     gpointer user_data)
366 {
367         update_model_empty (MODEST_TNY_FOLDER_TREE_VIEW(user_data));
368         
369         if (!update_model (MODEST_TNY_FOLDER_TREE_VIEW(user_data), account_store))
370                 g_printerr ("modest: failed to update model for changes in '%s'",
371                             account);
372 }
373
374
375 GtkWidget*
376 modest_tny_folder_tree_view_new (TnyAccountStoreIface *account_store)
377 {
378         GObject *self;
379         ModestTnyFolderTreeViewPrivate *priv;
380         GtkTreeSelection *sel;
381
382         self = G_OBJECT(g_object_new(MODEST_TYPE_TNY_FOLDER_TREE_VIEW, NULL));
383         priv = MODEST_TNY_FOLDER_TREE_VIEW_GET_PRIVATE(self);
384
385         g_return_val_if_fail (account_store, NULL);
386         
387         if (!update_model (MODEST_TNY_FOLDER_TREE_VIEW(self), account_store))
388                 g_printerr ("modest: failed to update model");
389
390         g_signal_connect (G_OBJECT(account_store), "update_accounts",
391                           G_CALLBACK (on_accounts_update), self);
392         
393         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
394         g_signal_connect (sel, "changed",
395                           G_CALLBACK(selection_changed), self);
396                 
397         return self;
398 }
399
400
401
402
403 static gboolean
404 update_model_empty (ModestTnyFolderTreeView *self)
405 {
406         GtkTreeIter  iter;
407         GtkTreeStore *store;
408         ModestTnyFolderTreeViewPrivate *priv;
409         
410         g_return_val_if_fail (self, FALSE);
411         
412         store = gtk_tree_store_new (1, G_TYPE_STRING);
413         gtk_tree_store_append (store, &iter, NULL);
414
415         gtk_tree_store_set (store, &iter, 0,
416                             _("(empty)"), -1);
417
418         gtk_tree_view_set_model (GTK_TREE_VIEW(self),
419                                  GTK_TREE_MODEL(store));
420         g_object_unref (store);
421
422         priv = MODEST_TNY_FOLDER_TREE_VIEW_GET_PRIVATE(self);
423         priv->view_is_empty = TRUE;
424         
425         return TRUE;
426 }
427
428
429 static gboolean
430 update_model (ModestTnyFolderTreeView *self, TnyAccountStoreIface *account_store)
431 {
432         ModestTnyFolderTreeViewPrivate *priv;
433         TnyListIface     *account_list;
434         GtkTreeModel     *model, *sortable;
435         
436         g_return_val_if_fail (account_store, FALSE);
437         
438         priv =  MODEST_TNY_FOLDER_TREE_VIEW_GET_PRIVATE(self);
439
440         model        = GTK_TREE_MODEL(tny_account_tree_model_new ());
441         account_list = TNY_LIST_IFACE(model);
442
443         update_model_empty (self); /* cleanup */
444         priv->view_is_empty = TRUE;
445         
446         tny_account_store_iface_get_accounts (account_store, account_list,
447                                               TNY_ACCOUNT_STORE_IFACE_STORE_ACCOUNTS);
448         if (!account_list) /* no store accounts found */ 
449                 return TRUE;
450         
451         sortable = gtk_tree_model_sort_new_with_model (model);
452         gtk_tree_view_set_model (GTK_TREE_VIEW(self), sortable);
453
454         priv->view_is_empty = FALSE;    
455         g_object_unref (model);
456
457         return TRUE;
458
459
460
461 void
462 selection_changed (GtkTreeSelection *sel, gpointer user_data)
463 {
464         GtkTreeModel            *model;
465         TnyMsgFolderIface       *folder = NULL;
466         GtkTreeIter             iter;
467         ModestTnyFolderTreeView *tree_view;
468         ModestTnyFolderTreeViewPrivate *priv;
469
470         g_return_if_fail (sel);
471         g_return_if_fail (user_data);
472
473         priv = MODEST_TNY_FOLDER_TREE_VIEW_GET_PRIVATE(user_data);
474
475         /* is_empty means that there is only the 'empty' item */
476         if (priv->view_is_empty)
477                 return;
478         
479         /* folder was _un_selected if true */
480         if (!gtk_tree_selection_get_selected (sel, &model, &iter)) {
481                 priv->cur_folder = NULL;
482                 return; 
483         }
484
485         tree_view = MODEST_TNY_FOLDER_TREE_VIEW (user_data);
486
487         gtk_tree_model_get (model, &iter,
488                             TNY_ACCOUNT_TREE_MODEL_INSTANCE_COLUMN,
489                             &folder, -1);
490
491         if (priv->cur_folder) 
492                 tny_msg_folder_iface_expunge (priv->cur_folder);
493         priv->cur_folder = folder;
494
495         /* folder will not be defined if you click eg. on the root node */
496         if (folder)
497                 g_signal_emit (G_OBJECT(tree_view), signals[FOLDER_SELECTED_SIGNAL], 0,
498                                folder);
499 }
500
501
502 gboolean
503 modest_tny_folder_tree_view_update_model(ModestTnyFolderTreeView *self, 
504                                          TnyAccountStoreIface *iface)
505 {
506         g_return_val_if_fail (MODEST_IS_TNY_FOLDER_TREE_VIEW (self), FALSE);
507         
508         return update_model (self, iface);
509 }