source: trunk/third/evolution/shell/e-shortcuts-view-model.c @ 19541

Revision 19541, 9.2 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r19540, which included commits to RCS files with non-trunk default branches.
Line 
1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2/* e-shortcuts-view-model.c
3 *
4 * Copyright (C) 2000  Ximian, Inc.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
19 *
20 * Author: Ettore Perazzoli
21 */
22
23/* FIXME.  This really sucks.  We are using the model/view approach in the
24   dumbest possible way.  */
25
26#ifdef HAVE_CONFIG_H
27#include <config.h>
28#endif
29
30#include "e-shortcuts-view-model.h"
31
32#include "e-icon-factory.h"
33
34#include <glib.h>
35#include <gtk/gtksignal.h>
36#include <libgnome/gnome-i18n.h>
37
38#include <gal/util/e-util.h>
39
40
41#define PARENT_TYPE e_shortcut_model_get_type ()
42static EShortcutModelClass *parent_class = NULL;
43
44struct _EShortcutsViewModelPrivate {
45        EShortcuts *shortcuts;
46};
47
48
49/* Utility functions.  */
50
51static GdkPixbuf *
52get_icon_for_item (EShortcutsViewModel *shortcuts_view_model,
53                   const EShortcutItem *item,
54                   gboolean want_mini)
55{
56        EShortcutsViewModelPrivate *priv;
57
58        priv = shortcuts_view_model->priv;
59
60        if (item->custom_icon_name != NULL)
61                return e_icon_factory_get_icon (item->custom_icon_name, want_mini);
62
63        if (item->type != NULL) {
64                EStorageSet *storage_set;
65                EFolderTypeRegistry *folder_type_registry;
66
67                storage_set = e_shell_get_storage_set (e_shortcuts_get_shell (priv->shortcuts));
68                folder_type_registry = e_storage_set_get_folder_type_registry (storage_set);
69
70                return e_folder_type_registry_get_icon_for_type (folder_type_registry,
71                                                                 item->type,
72                                                                 want_mini);
73        }
74
75        return NULL;
76}
77
78
79/* View initialization.  */
80
81static char *
82get_name_with_unread (const EShortcutItem *item)
83{
84        if (item->unread_count > 0)
85                return g_strdup_printf ("%s (%d)", item->name, item->unread_count);
86        else
87                return g_strdup (item->name);
88}
89
90static void
91load_group_into_model (EShortcutsViewModel *shortcuts_view_model,
92                       int group_num)
93{
94        EShortcutsViewModelPrivate *priv;
95        const GSList *shortcut_list;
96        const GSList *p;
97
98        priv = shortcuts_view_model->priv;
99
100        shortcut_list = e_shortcuts_get_shortcuts_in_group (priv->shortcuts, group_num);
101        if (shortcut_list == NULL)
102                return;
103
104        for (p = shortcut_list; p != NULL; p = p->next) {
105                const EShortcutItem *item;
106                char *name_with_unread;
107
108                item = (const EShortcutItem *) p->data;
109                name_with_unread = get_name_with_unread (item);
110
111                e_shortcut_model_add_item (E_SHORTCUT_MODEL (shortcuts_view_model),
112                                           group_num, -1,
113                                           item->uri,
114                                           name_with_unread,
115                                           get_icon_for_item (shortcuts_view_model, item, FALSE));
116
117                g_free (name_with_unread);
118        }
119}
120
121static void
122load_all_shortcuts_into_model (EShortcutsViewModel *shortcuts_view_model)
123{
124        EShortcutsViewModelPrivate *priv;
125        const GSList *group_titles;
126        const GSList *p;
127        int group_num;
128
129        priv = shortcuts_view_model->priv;
130
131        group_titles = e_shortcuts_get_group_titles (priv->shortcuts);
132
133        for (p = group_titles; p != NULL; p = p->next) {
134                const char *group_title;
135
136                group_title = (const char *) p->data;
137                group_num = e_shortcut_model_add_group (E_SHORTCUT_MODEL (shortcuts_view_model), -1, group_title);
138
139                load_group_into_model (shortcuts_view_model, group_num);
140        }
141}
142
143
144/* EShortcuts callbacks.  */
145
146static void
147shortcuts_new_group_cb (EShortcuts *shortcuts,
148                        int group_num,
149                        void *data)
150{
151        EShortcutsViewModel *shortcuts_view_model;
152        EShortcutsViewModelPrivate *priv;
153        const char *title;
154
155        shortcuts_view_model = E_SHORTCUTS_VIEW_MODEL (data);
156        priv = shortcuts_view_model->priv;
157
158        title = e_shortcuts_get_group_title (priv->shortcuts, group_num);
159        e_shortcut_model_add_group (E_SHORTCUT_MODEL (shortcuts_view_model), group_num, title);
160}
161
162static void
163shortcuts_remove_group_cb (EShortcuts *shortcuts,
164                           int group_num,
165                           void *data)
166{
167        EShortcutsViewModel *shortcuts_view_model;
168
169        shortcuts_view_model = E_SHORTCUTS_VIEW_MODEL (data);
170        e_shortcut_model_remove_group (E_SHORTCUT_MODEL (shortcuts_view_model), group_num);
171}
172
173static void
174shortcuts_rename_group_cb (EShortcuts *shortcuts,
175                           int group_num,
176                           const char *new_title,
177                           void *data)
178{
179        EShortcutsViewModel *shortcuts_view_model;
180
181        shortcuts_view_model = E_SHORTCUTS_VIEW_MODEL (data);
182
183        /* FIXME: Ideally there should be an
184           e_shortcut_model_rename_group(), removing then re-add
185           actually causes a flip to the next group, which we work
186           around in e-shortcuts-view.c */
187        e_shortcut_model_remove_group (E_SHORTCUT_MODEL (shortcuts_view_model), group_num);
188        e_shortcut_model_add_group (E_SHORTCUT_MODEL (shortcuts_view_model), group_num, new_title);
189        load_group_into_model (shortcuts_view_model, group_num);
190}
191
192static void
193shortcuts_new_shortcut_cb (EShortcuts *shortcuts,
194                           int group_num,
195                           int item_num,
196                           void *data)
197{
198        EShortcutsViewModel *shortcuts_view_model;
199        EShortcutsViewModelPrivate *priv;
200        const EShortcutItem *shortcut_item;
201        char *name_with_unread;
202
203        shortcuts_view_model = E_SHORTCUTS_VIEW_MODEL (data);
204        priv = shortcuts_view_model->priv;
205
206        shortcut_item = e_shortcuts_get_shortcut (priv->shortcuts, group_num, item_num);
207        g_assert (shortcut_item != NULL);
208
209        name_with_unread = get_name_with_unread (shortcut_item);
210        e_shortcut_model_add_item (E_SHORTCUT_MODEL (shortcuts_view_model),
211                                   group_num, item_num,
212                                   shortcut_item->uri,
213                                   name_with_unread,
214                                   get_icon_for_item (shortcuts_view_model, shortcut_item, FALSE));
215
216        g_free (name_with_unread);
217}
218
219static void
220shortcuts_remove_shortcut_cb (EShortcuts *shortcuts,
221                              int group_num,
222                              int item_num,
223                              void *data)
224{
225        EShortcutsViewModel *shortcuts_view_model;
226
227        shortcuts_view_model = E_SHORTCUTS_VIEW_MODEL (data);
228        e_shortcut_model_remove_item (E_SHORTCUT_MODEL (shortcuts_view_model), group_num, item_num);
229}
230
231static void
232shortcuts_update_shortcut_cb (EShortcuts *shortcuts,
233                              int group_num,
234                              int item_num,
235                              void *data)
236{
237        EShortcutsViewModel *shortcuts_view_model;
238        EShortcutsViewModelPrivate *priv;
239        const EShortcutItem *shortcut_item;
240        char *name_with_unread;
241
242        shortcuts_view_model = E_SHORTCUTS_VIEW_MODEL (data);
243        priv = shortcuts_view_model->priv;
244
245        shortcut_item = e_shortcuts_get_shortcut (priv->shortcuts, group_num, item_num);
246        g_assert (shortcut_item != NULL);
247
248        name_with_unread = get_name_with_unread (shortcut_item);
249        e_shortcut_model_update_item (E_SHORTCUT_MODEL (shortcuts_view_model),
250                                      group_num, item_num,
251                                      shortcut_item->uri,
252                                      name_with_unread,
253                                      get_icon_for_item (shortcuts_view_model, shortcut_item, FALSE));
254
255        g_free (name_with_unread);
256}
257
258
259/* GObject methods.  */
260
261static void
262impl_finalize (GObject *object)
263{
264        EShortcutsViewModel *view_model;
265        EShortcutsViewModelPrivate *priv;
266
267        view_model = E_SHORTCUTS_VIEW_MODEL (object);
268        priv = view_model->priv;
269
270        g_free (priv);
271
272        (* G_OBJECT_CLASS (parent_class)->finalize) (object);
273}
274
275
276static void
277class_init (EShortcutsViewModelClass *klass)
278{
279        GObjectClass *object_class;
280
281        object_class = G_OBJECT_CLASS (klass);
282        object_class->finalize = impl_finalize;
283
284        parent_class = g_type_class_ref(e_shortcut_model_get_type ());
285}
286
287static void
288init (EShortcutsViewModel *shortcuts_view_model)
289{
290        EShortcutsViewModelPrivate *priv;
291
292        priv = g_new (EShortcutsViewModelPrivate, 1);
293        priv->shortcuts      = NULL;
294
295        shortcuts_view_model->priv = priv;
296}
297
298
299void
300e_shortcuts_view_model_construct (EShortcutsViewModel *model,
301                                  EShortcuts *shortcuts)
302{
303        EShortcutsViewModelPrivate *priv;
304
305        g_return_if_fail (model != NULL);
306        g_return_if_fail (E_IS_SHORTCUTS_VIEW_MODEL (model));
307        g_return_if_fail (shortcuts != NULL);
308        g_return_if_fail (E_IS_SHORTCUTS (shortcuts));
309
310        priv = model->priv;
311        g_return_if_fail (priv->shortcuts == NULL);
312
313        priv->shortcuts = shortcuts;
314
315        load_all_shortcuts_into_model (model);
316
317        g_signal_connect_object (priv->shortcuts, "new_group", G_CALLBACK (shortcuts_new_group_cb), model, 0);
318        g_signal_connect_object (priv->shortcuts, "remove_group", G_CALLBACK (shortcuts_remove_group_cb), model, 0);
319        g_signal_connect_object (priv->shortcuts, "rename_group", G_CALLBACK (shortcuts_rename_group_cb), model, 0);
320        g_signal_connect_object (priv->shortcuts, "new_shortcut", G_CALLBACK (shortcuts_new_shortcut_cb), model, 0);
321        g_signal_connect_object (priv->shortcuts, "remove_shortcut", G_CALLBACK (shortcuts_remove_shortcut_cb), model, 0);
322        g_signal_connect_object (priv->shortcuts, "update_shortcut", G_CALLBACK (shortcuts_update_shortcut_cb), model, 0);
323}
324
325EShortcutsViewModel *
326e_shortcuts_view_model_new (EShortcuts *shortcuts)
327{
328        EShortcutsViewModel *new;
329
330        g_return_val_if_fail (shortcuts != NULL, NULL);
331        g_return_val_if_fail (E_IS_SHORTCUTS (shortcuts), NULL);
332
333        new = g_object_new (e_shortcuts_view_model_get_type (), NULL);
334
335        e_shortcuts_view_model_construct (new, shortcuts);
336
337        return new;
338}
339
340
341E_MAKE_TYPE (e_shortcuts_view_model, "EShortcutsViewModel", EShortcutsViewModel, class_init, init, PARENT_TYPE)
Note: See TracBrowser for help on using the repository browser.