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

Revision 16787, 9.4 KB checked in by ghudson, 23 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r16786, 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 <glib.h>
33#include <gtk/gtksignal.h>
34#include <libgnome/gnome-i18n.h>
35
36#include <gal/util/e-util.h>
37
38
39#define PARENT_TYPE e_shortcut_model_get_type ()
40static EShortcutModelClass *parent_class = NULL;
41
42struct _EShortcutsViewModelPrivate {
43        EShortcuts *shortcuts;
44};
45
46
47/* Utility functions.  */
48
49static GdkPixbuf *
50get_icon_for_item (EShortcutsViewModel *shortcuts_view_model,
51                   const EShortcutItem *item,
52                   gboolean want_mini)
53{
54        EShortcutsViewModelPrivate *priv;
55
56        priv = shortcuts_view_model->priv;
57
58        if (item->type != NULL) {
59                EStorageSet *storage_set;
60                EFolderTypeRegistry *folder_type_registry;
61
62                storage_set = e_shortcuts_get_storage_set (priv->shortcuts);
63                folder_type_registry = e_storage_set_get_folder_type_registry (storage_set);
64
65                return e_folder_type_registry_get_icon_for_type (folder_type_registry,
66                                                                 item->type,
67                                                                 want_mini);
68        }
69
70        return NULL;
71}
72
73
74/* View initialization.  */
75
76static char *
77get_name_with_unread (const EShortcutItem *item)
78{
79        if (item->unread_count > 0)
80                return g_strdup_printf ("%s (%d)", item->name, item->unread_count);
81        else
82                return g_strdup (item->name);
83}
84
85static void
86load_group_into_model (EShortcutsViewModel *shortcuts_view_model,
87                       int group_num)
88{
89        EShortcutsViewModelPrivate *priv;
90        const GSList *shortcut_list;
91        const GSList *p;
92
93        priv = shortcuts_view_model->priv;
94
95        shortcut_list = e_shortcuts_get_shortcuts_in_group (priv->shortcuts, group_num);
96        if (shortcut_list == NULL)
97                return;
98
99        for (p = shortcut_list; p != NULL; p = p->next) {
100                const EShortcutItem *item;
101                char *name_with_unread;
102
103                item = (const EShortcutItem *) p->data;
104                name_with_unread = get_name_with_unread (item);
105
106                e_shortcut_model_add_item (E_SHORTCUT_MODEL (shortcuts_view_model),
107                                           group_num, -1,
108                                           item->uri,
109                                           name_with_unread,
110                                           get_icon_for_item (shortcuts_view_model, item, FALSE));
111
112                g_free (name_with_unread);
113        }
114}
115
116static void
117load_all_shortcuts_into_model (EShortcutsViewModel *shortcuts_view_model)
118{
119        EShortcutsViewModelPrivate *priv;
120        const GSList *group_titles;
121        const GSList *p;
122        int group_num;
123
124        priv = shortcuts_view_model->priv;
125
126        group_titles = e_shortcuts_get_group_titles (priv->shortcuts);
127
128        for (p = group_titles; p != NULL; p = p->next) {
129                const char *group_title;
130
131                group_title = (const char *) p->data;
132                group_num = e_shortcut_model_add_group (E_SHORTCUT_MODEL (shortcuts_view_model), -1, group_title);
133
134                load_group_into_model (shortcuts_view_model, group_num);
135        }
136}
137
138
139/* EShortcuts callbacks.  */
140
141static void
142shortcuts_new_group_cb (EShortcuts *shortcuts,
143                        int group_num,
144                        void *data)
145{
146        EShortcutsViewModel *shortcuts_view_model;
147        EShortcutsViewModelPrivate *priv;
148        const char *title;
149
150        shortcuts_view_model = E_SHORTCUTS_VIEW_MODEL (data);
151        priv = shortcuts_view_model->priv;
152
153        title = e_shortcuts_get_group_title (priv->shortcuts, group_num);
154        e_shortcut_model_add_group (E_SHORTCUT_MODEL (shortcuts_view_model), group_num, title);
155}
156
157static void
158shortcuts_remove_group_cb (EShortcuts *shortcuts,
159                           int group_num,
160                           void *data)
161{
162        EShortcutsViewModel *shortcuts_view_model;
163
164        shortcuts_view_model = E_SHORTCUTS_VIEW_MODEL (data);
165        e_shortcut_model_remove_group (E_SHORTCUT_MODEL (shortcuts_view_model), group_num);
166}
167
168static void
169shortcuts_rename_group_cb (EShortcuts *shortcuts,
170                           int group_num,
171                           const char *new_title,
172                           void *data)
173{
174        EShortcutsViewModel *shortcuts_view_model;
175
176        shortcuts_view_model = E_SHORTCUTS_VIEW_MODEL (data);
177
178        /* FIXME: Ideally there should be an
179           e_shortcut_model_rename_group(), removing then re-add
180           actually causes a flip to the next group, which we work
181           around in e-shortcuts-view.c */
182        e_shortcut_model_remove_group (E_SHORTCUT_MODEL (shortcuts_view_model), group_num);
183        e_shortcut_model_add_group (E_SHORTCUT_MODEL (shortcuts_view_model), group_num, new_title);
184        load_group_into_model (shortcuts_view_model, group_num);
185}
186
187static void
188shortcuts_new_shortcut_cb (EShortcuts *shortcuts,
189                           int group_num,
190                           int item_num,
191                           void *data)
192{
193        EShortcutsViewModel *shortcuts_view_model;
194        EShortcutsViewModelPrivate *priv;
195        const EShortcutItem *shortcut_item;
196        char *name_with_unread;
197
198        shortcuts_view_model = E_SHORTCUTS_VIEW_MODEL (data);
199        priv = shortcuts_view_model->priv;
200
201        shortcut_item = e_shortcuts_get_shortcut (priv->shortcuts, group_num, item_num);
202        g_assert (shortcut_item != NULL);
203
204        name_with_unread = get_name_with_unread (shortcut_item);
205        e_shortcut_model_add_item (E_SHORTCUT_MODEL (shortcuts_view_model),
206                                   group_num, item_num,
207                                   shortcut_item->uri,
208                                   name_with_unread,
209                                   get_icon_for_item (shortcuts_view_model, shortcut_item, FALSE));
210
211        g_free (name_with_unread);
212}
213
214static void
215shortcuts_remove_shortcut_cb (EShortcuts *shortcuts,
216                              int group_num,
217                              int item_num,
218                              void *data)
219{
220        EShortcutsViewModel *shortcuts_view_model;
221
222        shortcuts_view_model = E_SHORTCUTS_VIEW_MODEL (data);
223        e_shortcut_model_remove_item (E_SHORTCUT_MODEL (shortcuts_view_model), group_num, item_num);
224}
225
226static void
227shortcuts_update_shortcut_cb (EShortcuts *shortcuts,
228                              int group_num,
229                              int item_num,
230                              void *data)
231{
232        EShortcutsViewModel *shortcuts_view_model;
233        EShortcutsViewModelPrivate *priv;
234        const EShortcutItem *shortcut_item;
235        char *name_with_unread;
236
237        shortcuts_view_model = E_SHORTCUTS_VIEW_MODEL (data);
238        priv = shortcuts_view_model->priv;
239
240        shortcut_item = e_shortcuts_get_shortcut (priv->shortcuts, group_num, item_num);
241        g_assert (shortcut_item != NULL);
242
243        name_with_unread = get_name_with_unread (shortcut_item);
244        e_shortcut_model_update_item (E_SHORTCUT_MODEL (shortcuts_view_model),
245                                      group_num, item_num,
246                                      shortcut_item->uri,
247                                      name_with_unread,
248                                      get_icon_for_item (shortcuts_view_model, shortcut_item, FALSE));
249
250        g_free (name_with_unread);
251}
252
253
254/* GtkObject methods.  */
255
256static void
257impl_destroy (GtkObject *object)
258{
259        EShortcutsViewModel *view_model;
260        EShortcutsViewModelPrivate *priv;
261
262        view_model = E_SHORTCUTS_VIEW_MODEL (object);
263        priv = view_model->priv;
264
265        g_free (priv);
266
267        if (GTK_OBJECT_CLASS (parent_class)->destroy)
268                (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
269}
270
271
272static void
273class_init (EShortcutsViewModelClass *klass)
274{
275        GtkObjectClass *object_class;
276
277        object_class = GTK_OBJECT_CLASS (klass);
278        object_class->destroy = impl_destroy;
279
280        parent_class = gtk_type_class (e_shortcut_model_get_type ());
281}
282
283static void
284init (EShortcutsViewModel *shortcuts_view_model)
285{
286        EShortcutsViewModelPrivate *priv;
287
288        priv = g_new (EShortcutsViewModelPrivate, 1);
289        priv->shortcuts      = NULL;
290
291        shortcuts_view_model->priv = priv;
292}
293
294
295void
296e_shortcuts_view_model_construct (EShortcutsViewModel *model,
297                                  EShortcuts *shortcuts)
298{
299        EShortcutsViewModelPrivate *priv;
300
301        g_return_if_fail (model != NULL);
302        g_return_if_fail (E_IS_SHORTCUTS_VIEW_MODEL (model));
303        g_return_if_fail (shortcuts != NULL);
304        g_return_if_fail (E_IS_SHORTCUTS (shortcuts));
305
306        priv = model->priv;
307        g_return_if_fail (priv->shortcuts == NULL);
308
309        priv->shortcuts = shortcuts;
310
311        load_all_shortcuts_into_model (model);
312
313        gtk_signal_connect_while_alive (GTK_OBJECT (priv->shortcuts),
314                                        "new_group", GTK_SIGNAL_FUNC (shortcuts_new_group_cb), model,
315                                        GTK_OBJECT (model));
316        gtk_signal_connect_while_alive (GTK_OBJECT (priv->shortcuts),
317                                        "remove_group", GTK_SIGNAL_FUNC (shortcuts_remove_group_cb), model,
318                                        GTK_OBJECT (model));
319        gtk_signal_connect_while_alive (GTK_OBJECT (priv->shortcuts),
320                                        "rename_group", GTK_SIGNAL_FUNC (shortcuts_rename_group_cb), model,
321                                        GTK_OBJECT (model));
322        gtk_signal_connect_while_alive (GTK_OBJECT (priv->shortcuts),
323                                        "new_shortcut", GTK_SIGNAL_FUNC (shortcuts_new_shortcut_cb), model,
324                                        GTK_OBJECT (model));
325        gtk_signal_connect_while_alive (GTK_OBJECT (priv->shortcuts),
326                                        "remove_shortcut", GTK_SIGNAL_FUNC (shortcuts_remove_shortcut_cb), model,
327                                        GTK_OBJECT (model));
328        gtk_signal_connect_while_alive (GTK_OBJECT (priv->shortcuts),
329                                        "update_shortcut", GTK_SIGNAL_FUNC (shortcuts_update_shortcut_cb), model,
330                                        GTK_OBJECT (model));
331}
332
333EShortcutsViewModel *
334e_shortcuts_view_model_new (EShortcuts *shortcuts)
335{
336        EShortcutsViewModel *new;
337
338        g_return_val_if_fail (shortcuts != NULL, NULL);
339        g_return_val_if_fail (E_IS_SHORTCUTS (shortcuts), NULL);
340
341        new = gtk_type_new (e_shortcuts_view_model_get_type ());
342
343        e_shortcuts_view_model_construct (new, shortcuts);
344
345        return new;
346}
347
348
349E_MAKE_TYPE (e_shortcuts_view_model, "EShortcutsViewModel", EShortcutsViewModel, class_init, init, PARENT_TYPE)
Note: See TracBrowser for help on using the repository browser.