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 () |
---|
42 | static EShortcutModelClass *parent_class = NULL; |
---|
43 | |
---|
44 | struct _EShortcutsViewModelPrivate { |
---|
45 | EShortcuts *shortcuts; |
---|
46 | }; |
---|
47 | |
---|
48 | |
---|
49 | /* Utility functions. */ |
---|
50 | |
---|
51 | static GdkPixbuf * |
---|
52 | get_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 | |
---|
81 | static char * |
---|
82 | get_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 | |
---|
90 | static void |
---|
91 | load_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 | |
---|
121 | static void |
---|
122 | load_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 | |
---|
146 | static void |
---|
147 | shortcuts_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 | |
---|
162 | static void |
---|
163 | shortcuts_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 | |
---|
173 | static void |
---|
174 | shortcuts_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 | |
---|
192 | static void |
---|
193 | shortcuts_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 | |
---|
219 | static void |
---|
220 | shortcuts_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 | |
---|
231 | static void |
---|
232 | shortcuts_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 | /* GtkObject methods. */ |
---|
260 | |
---|
261 | static void |
---|
262 | impl_destroy (GtkObject *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 | if (GTK_OBJECT_CLASS (parent_class)->destroy) |
---|
273 | (* GTK_OBJECT_CLASS (parent_class)->destroy) (object); |
---|
274 | } |
---|
275 | |
---|
276 | |
---|
277 | static void |
---|
278 | class_init (EShortcutsViewModelClass *klass) |
---|
279 | { |
---|
280 | GtkObjectClass *object_class; |
---|
281 | |
---|
282 | object_class = GTK_OBJECT_CLASS (klass); |
---|
283 | object_class->destroy = impl_destroy; |
---|
284 | |
---|
285 | parent_class = gtk_type_class (e_shortcut_model_get_type ()); |
---|
286 | } |
---|
287 | |
---|
288 | static void |
---|
289 | init (EShortcutsViewModel *shortcuts_view_model) |
---|
290 | { |
---|
291 | EShortcutsViewModelPrivate *priv; |
---|
292 | |
---|
293 | priv = g_new (EShortcutsViewModelPrivate, 1); |
---|
294 | priv->shortcuts = NULL; |
---|
295 | |
---|
296 | shortcuts_view_model->priv = priv; |
---|
297 | } |
---|
298 | |
---|
299 | |
---|
300 | void |
---|
301 | e_shortcuts_view_model_construct (EShortcutsViewModel *model, |
---|
302 | EShortcuts *shortcuts) |
---|
303 | { |
---|
304 | EShortcutsViewModelPrivate *priv; |
---|
305 | |
---|
306 | g_return_if_fail (model != NULL); |
---|
307 | g_return_if_fail (E_IS_SHORTCUTS_VIEW_MODEL (model)); |
---|
308 | g_return_if_fail (shortcuts != NULL); |
---|
309 | g_return_if_fail (E_IS_SHORTCUTS (shortcuts)); |
---|
310 | |
---|
311 | priv = model->priv; |
---|
312 | g_return_if_fail (priv->shortcuts == NULL); |
---|
313 | |
---|
314 | priv->shortcuts = shortcuts; |
---|
315 | |
---|
316 | load_all_shortcuts_into_model (model); |
---|
317 | |
---|
318 | gtk_signal_connect_while_alive (GTK_OBJECT (priv->shortcuts), |
---|
319 | "new_group", GTK_SIGNAL_FUNC (shortcuts_new_group_cb), model, |
---|
320 | GTK_OBJECT (model)); |
---|
321 | gtk_signal_connect_while_alive (GTK_OBJECT (priv->shortcuts), |
---|
322 | "remove_group", GTK_SIGNAL_FUNC (shortcuts_remove_group_cb), model, |
---|
323 | GTK_OBJECT (model)); |
---|
324 | gtk_signal_connect_while_alive (GTK_OBJECT (priv->shortcuts), |
---|
325 | "rename_group", GTK_SIGNAL_FUNC (shortcuts_rename_group_cb), model, |
---|
326 | GTK_OBJECT (model)); |
---|
327 | gtk_signal_connect_while_alive (GTK_OBJECT (priv->shortcuts), |
---|
328 | "new_shortcut", GTK_SIGNAL_FUNC (shortcuts_new_shortcut_cb), model, |
---|
329 | GTK_OBJECT (model)); |
---|
330 | gtk_signal_connect_while_alive (GTK_OBJECT (priv->shortcuts), |
---|
331 | "remove_shortcut", GTK_SIGNAL_FUNC (shortcuts_remove_shortcut_cb), model, |
---|
332 | GTK_OBJECT (model)); |
---|
333 | gtk_signal_connect_while_alive (GTK_OBJECT (priv->shortcuts), |
---|
334 | "update_shortcut", GTK_SIGNAL_FUNC (shortcuts_update_shortcut_cb), model, |
---|
335 | GTK_OBJECT (model)); |
---|
336 | } |
---|
337 | |
---|
338 | EShortcutsViewModel * |
---|
339 | e_shortcuts_view_model_new (EShortcuts *shortcuts) |
---|
340 | { |
---|
341 | EShortcutsViewModel *new; |
---|
342 | |
---|
343 | g_return_val_if_fail (shortcuts != NULL, NULL); |
---|
344 | g_return_val_if_fail (E_IS_SHORTCUTS (shortcuts), NULL); |
---|
345 | |
---|
346 | new = gtk_type_new (e_shortcuts_view_model_get_type ()); |
---|
347 | |
---|
348 | e_shortcuts_view_model_construct (new, shortcuts); |
---|
349 | |
---|
350 | return new; |
---|
351 | } |
---|
352 | |
---|
353 | |
---|
354 | E_MAKE_TYPE (e_shortcuts_view_model, "EShortcutsViewModel", EShortcutsViewModel, class_init, init, PARENT_TYPE) |
---|