source: trunk/third/evolution/shell/e-folder-type-registry.c @ 18142

Revision 18142, 15.3 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18141, 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-folder-type-registry.c
3 *
4 * Copyright (C) 2000, 2001 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#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif
26
27#include <glib.h>
28#include <gtk/gtktypeutils.h>
29
30#include <gal/util/e-util.h>
31
32#include "e-shell-utils.h"
33
34#include "e-folder-type-registry.h"
35
36
37#define PARENT_TYPE GTK_TYPE_OBJECT
38static GtkObjectClass *parent_class = NULL;
39
40struct _FolderType {
41        char *name;
42        char *icon_name;
43
44        char *display_name;
45        char *description;
46
47        gboolean user_creatable;
48
49        GList *exported_dnd_types; /* char * */
50        GList *accepted_dnd_types; /* char * */
51
52        EvolutionShellComponentClient *handler;
53
54        /* The icon, standard (48x48) and mini (16x16) versions.  */
55        GdkPixbuf *icon_pixbuf;
56        GdkPixbuf *mini_icon_pixbuf;
57};
58typedef struct _FolderType FolderType;
59
60struct _EFolderTypeRegistryPrivate {
61        GHashTable *name_to_type;
62};
63
64
65/* FolderType handling.  */
66
67static FolderType *
68folder_type_new (const char *name,
69                 const char *icon_name,
70                 const char *display_name,
71                 const char *description,
72                 gboolean user_creatable,
73                 int num_exported_dnd_types,
74                 const char **exported_dnd_types,
75                 int num_accepted_dnd_types,
76                 const char **accepted_dnd_types)
77{
78        FolderType *new;
79        char *icon_path;
80        int i;
81
82        new = g_new (FolderType, 1);
83
84        new->name           = g_strdup (name);
85        new->icon_name      = g_strdup (icon_name);
86        new->display_name   = g_strdup (display_name);
87        new->description    = g_strdup (description);
88
89        new->user_creatable = user_creatable;
90
91        new->exported_dnd_types = NULL;
92        for (i = 0; i < num_exported_dnd_types; i++)
93                new->exported_dnd_types = g_list_prepend (new->exported_dnd_types,
94                                                          g_strdup (exported_dnd_types[i]));
95        new->exported_dnd_types = g_list_reverse (new->exported_dnd_types);
96
97        new->accepted_dnd_types = NULL;
98        for (i = 0; i < num_accepted_dnd_types; i++)
99                new->accepted_dnd_types = g_list_prepend (new->accepted_dnd_types,
100                                                          g_strdup (accepted_dnd_types[i]));
101        new->accepted_dnd_types = g_list_reverse (new->accepted_dnd_types);
102
103        new->handler = NULL;
104
105        icon_path = e_shell_get_icon_path (icon_name, FALSE);
106        if (icon_path == NULL)
107                new->icon_pixbuf = NULL;
108        else
109                new->icon_pixbuf = gdk_pixbuf_new_from_file (icon_path);
110
111        g_free (icon_path);
112
113        icon_path = e_shell_get_icon_path (icon_name, TRUE);
114        if (icon_path != NULL) {
115                new->mini_icon_pixbuf = gdk_pixbuf_new_from_file (icon_path);
116        } else {
117                if (new->icon_pixbuf != NULL)
118                        new->mini_icon_pixbuf = gdk_pixbuf_ref (new->icon_pixbuf);
119                else
120                        new->mini_icon_pixbuf = NULL;
121        }
122
123        g_free (icon_path);
124
125        return new;
126}
127
128static void
129folder_type_free (FolderType *folder_type)
130{
131        g_free (folder_type->name);
132        g_free (folder_type->icon_name);
133        g_free (folder_type->display_name);
134        g_free (folder_type->description);
135
136        if (folder_type->icon_pixbuf != NULL)
137                gdk_pixbuf_unref (folder_type->icon_pixbuf);
138        if (folder_type->mini_icon_pixbuf != NULL)
139                gdk_pixbuf_unref (folder_type->mini_icon_pixbuf);
140
141        if (folder_type->handler != NULL)
142                bonobo_object_unref (BONOBO_OBJECT (folder_type->handler));
143
144        g_free (folder_type);
145}
146
147static FolderType *
148get_folder_type (EFolderTypeRegistry *folder_type_registry,
149                 const char *type_name)
150{
151        EFolderTypeRegistryPrivate *priv;
152
153        priv = folder_type_registry->priv;
154
155        return g_hash_table_lookup (priv->name_to_type, type_name);
156}
157
158static gboolean
159register_folder_type (EFolderTypeRegistry *folder_type_registry,
160                      const char *name,
161                      const char *icon_name,
162                      const char *display_name,
163                      const char *description,
164                      gboolean user_creatable,
165                      int num_exported_dnd_types,
166                      const char **exported_dnd_types,
167                      int num_accepted_dnd_types,
168                      const char **accepted_dnd_types)
169{
170        EFolderTypeRegistryPrivate *priv;
171        FolderType *folder_type;
172
173        priv = folder_type_registry->priv;
174
175        /* Make sure we don't add the same type twice.  */
176        if (get_folder_type (folder_type_registry, name) != NULL)
177                return FALSE;
178
179        folder_type = folder_type_new (name, icon_name,
180                                       display_name, description,
181                                       user_creatable,
182                                       num_exported_dnd_types, exported_dnd_types,
183                                       num_accepted_dnd_types, accepted_dnd_types);
184        g_hash_table_insert (priv->name_to_type, folder_type->name, folder_type);
185
186        return TRUE;
187}
188
189static gboolean
190set_handler (EFolderTypeRegistry *folder_type_registry,
191             const char *name,
192             EvolutionShellComponentClient *handler)
193{
194        EFolderTypeRegistryPrivate *priv;
195        FolderType *folder_type;
196
197        priv = folder_type_registry->priv;
198
199        folder_type = get_folder_type (folder_type_registry, name);
200        if (folder_type == NULL)
201                return FALSE;
202        if (folder_type->handler != NULL) {
203                g_warning ("Folder type already has a handler -- %s",
204                           folder_type->name);
205                return FALSE;
206        }
207
208        bonobo_object_ref (BONOBO_OBJECT (handler));
209        folder_type->handler = handler;
210
211        return TRUE;
212}
213
214
215/* GtkObject methods.  */
216
217static void
218hash_forall_free_folder_type (gpointer key,
219                              gpointer value,
220                              gpointer data)
221{
222        FolderType *folder_type;
223
224        folder_type = (FolderType *) value;
225        folder_type_free (folder_type);
226}
227
228static void
229destroy (GtkObject *object)
230{
231        EFolderTypeRegistry *folder_type_registry;
232        EFolderTypeRegistryPrivate *priv;
233
234        folder_type_registry = E_FOLDER_TYPE_REGISTRY (object);
235        priv = folder_type_registry->priv;
236
237        g_hash_table_foreach (priv->name_to_type,
238                              hash_forall_free_folder_type, NULL);
239        g_hash_table_destroy (priv->name_to_type);
240
241        g_free (priv);
242
243        (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
244}
245
246
247static void
248class_init (EFolderTypeRegistryClass *class)
249{
250        GtkObjectClass *object_class;
251
252        object_class = GTK_OBJECT_CLASS (class);
253        object_class->destroy = destroy;
254
255        parent_class = gtk_type_class (gtk_object_get_type ());
256}
257
258static void
259init (EFolderTypeRegistry *folder_type_registry)
260{
261        EFolderTypeRegistryPrivate *priv;
262
263        priv = g_new (EFolderTypeRegistryPrivate, 1);
264        priv->name_to_type = g_hash_table_new (g_str_hash, g_str_equal);
265
266        folder_type_registry->priv = priv;
267}
268
269
270void
271e_folder_type_registry_construct (EFolderTypeRegistry *folder_type_registry)
272{
273        g_return_if_fail (folder_type_registry != NULL);
274        g_return_if_fail (E_IS_FOLDER_TYPE_REGISTRY (folder_type_registry));
275
276        GTK_OBJECT_UNSET_FLAGS (GTK_OBJECT (folder_type_registry), GTK_FLOATING);
277}
278
279EFolderTypeRegistry *
280e_folder_type_registry_new (void)
281{
282        EFolderTypeRegistry *new;
283
284        new = gtk_type_new (e_folder_type_registry_get_type ());
285
286        e_folder_type_registry_construct (new);
287
288        return new;
289}
290
291
292gboolean
293e_folder_type_registry_register_type (EFolderTypeRegistry *folder_type_registry,
294                                      const char *type_name,
295                                      const char *icon_name,
296                                      const char *display_name,
297                                      const char *description,
298                                      gboolean user_creatable,
299                                      int num_exported_dnd_types,
300                                      const char **exported_dnd_types,
301                                      int num_accepted_dnd_types,
302                                      const char **accepted_dnd_types)
303{
304        g_return_val_if_fail (folder_type_registry != NULL, FALSE);
305        g_return_val_if_fail (E_IS_FOLDER_TYPE_REGISTRY (folder_type_registry), FALSE);
306        g_return_val_if_fail (type_name != NULL, FALSE);
307        g_return_val_if_fail (icon_name != NULL, FALSE);
308
309        return register_folder_type (folder_type_registry, type_name, icon_name,
310                                     display_name, description, user_creatable,
311                                     num_exported_dnd_types, exported_dnd_types,
312                                     num_accepted_dnd_types, accepted_dnd_types);
313}
314
315gboolean
316e_folder_type_registry_set_handler_for_type  (EFolderTypeRegistry *folder_type_registry,
317                                              const char *type_name,
318                                              EvolutionShellComponentClient *handler)
319{
320        g_return_val_if_fail (folder_type_registry != NULL, FALSE);
321        g_return_val_if_fail (E_IS_FOLDER_TYPE_REGISTRY (folder_type_registry), FALSE);
322        g_return_val_if_fail (handler != NULL, FALSE);
323        g_return_val_if_fail (BONOBO_IS_OBJECT_CLIENT (handler), FALSE);
324
325        return set_handler (folder_type_registry, type_name, handler);
326}
327
328
329gboolean
330e_folder_type_registry_type_registered  (EFolderTypeRegistry *folder_type_registry,
331                                         const char *type_name)
332{
333        EFolderTypeRegistryPrivate *priv;
334
335        g_return_val_if_fail (folder_type_registry != NULL, FALSE);
336        g_return_val_if_fail (E_IS_FOLDER_TYPE_REGISTRY (folder_type_registry), FALSE);
337        g_return_val_if_fail (type_name != NULL, FALSE);
338
339        priv = folder_type_registry->priv;
340
341        if (get_folder_type (folder_type_registry, type_name) == NULL)
342                return FALSE;
343
344        return TRUE;
345}
346
347void
348e_folder_type_registry_unregister_type (EFolderTypeRegistry *folder_type_registry,
349                                        const char *type_name)
350{
351        EFolderTypeRegistryPrivate *priv;
352        FolderType *folder_type;
353
354        g_return_if_fail (folder_type_registry != NULL);
355        g_return_if_fail (E_IS_FOLDER_TYPE_REGISTRY (folder_type_registry));
356        g_return_if_fail (type_name != NULL);
357
358        priv = folder_type_registry->priv;
359
360        folder_type = get_folder_type (folder_type_registry, type_name);
361        if (folder_type == NULL) {
362                g_warning ("e_folder_type_registry_unregister_type(): cannot find type `%s'\n",
363                           type_name);
364                return;
365        }
366
367        g_hash_table_remove (priv->name_to_type, folder_type->name);
368        folder_type_free (folder_type);
369}
370
371
372static void
373get_type_names_hash_forall (void *key,
374                            void *value,
375                            void *data)
376{
377        GList **type_name_list;
378
379        type_name_list = (GList **) data;
380
381        *type_name_list = g_list_prepend (*type_name_list, g_strdup ((const char *) key));
382}
383
384GList *
385e_folder_type_registry_get_type_names (EFolderTypeRegistry *folder_type_registry)
386{
387        GList *type_name_list;
388        EFolderTypeRegistryPrivate *priv;
389
390        g_return_val_if_fail (folder_type_registry != NULL, NULL);
391        g_return_val_if_fail (E_IS_FOLDER_TYPE_REGISTRY (folder_type_registry), NULL);
392
393        priv = folder_type_registry->priv;
394
395        type_name_list = NULL;
396        g_hash_table_foreach (priv->name_to_type, get_type_names_hash_forall, &type_name_list);
397
398        return type_name_list;
399}
400
401
402const char *
403e_folder_type_registry_get_icon_name_for_type (EFolderTypeRegistry *folder_type_registry,
404                                               const char *type_name)
405{
406        const FolderType *folder_type;
407
408        g_return_val_if_fail (folder_type_registry != NULL, NULL);
409        g_return_val_if_fail (E_IS_FOLDER_TYPE_REGISTRY (folder_type_registry), NULL);
410        g_return_val_if_fail (type_name != NULL, NULL);
411
412        folder_type = get_folder_type (folder_type_registry, type_name);
413        if (folder_type == NULL) {
414                g_warning ("e_folder_type_registry_get_icon_name_for_type() -- Unknown type `%s'", type_name);
415                return NULL;
416        }
417
418        return folder_type->icon_name;
419}
420
421GdkPixbuf *
422e_folder_type_registry_get_icon_for_type (EFolderTypeRegistry *folder_type_registry,
423                                          const char *type_name,
424                                          gboolean mini)
425{
426        const FolderType *folder_type;
427
428        g_return_val_if_fail (folder_type_registry != NULL, NULL);
429        g_return_val_if_fail (E_IS_FOLDER_TYPE_REGISTRY (folder_type_registry), NULL);
430        g_return_val_if_fail (type_name != NULL, NULL);
431
432        folder_type = get_folder_type (folder_type_registry, type_name);
433        if (folder_type == NULL) {
434                g_warning ("e_folder_type_registry_get_icon_for_type() -- Unknown type `%s'", type_name);
435                return NULL;
436        }
437
438        if (mini)
439                return folder_type->mini_icon_pixbuf;
440        else
441                return folder_type->icon_pixbuf;
442}
443
444EvolutionShellComponentClient *
445e_folder_type_registry_get_handler_for_type (EFolderTypeRegistry *folder_type_registry,
446                                             const char *type_name)
447{
448        const FolderType *folder_type;
449
450        g_return_val_if_fail (folder_type_registry != NULL, NULL);
451        g_return_val_if_fail (E_IS_FOLDER_TYPE_REGISTRY (folder_type_registry), NULL);
452        g_return_val_if_fail (type_name != NULL, NULL);
453
454        folder_type = get_folder_type (folder_type_registry, type_name);
455        if (folder_type == NULL) {
456                g_warning ("e_folder_type_registry_get_handler_for_type() -- Unknown type `%s'", type_name);
457                return NULL;
458        }
459
460        return folder_type->handler;
461}
462
463gboolean
464e_folder_type_registry_type_is_user_creatable  (EFolderTypeRegistry *folder_type_registry,
465                                                const char          *type_name)
466{
467        const FolderType *folder_type;
468
469        g_return_val_if_fail (folder_type_registry != NULL, FALSE);
470        g_return_val_if_fail (E_IS_FOLDER_TYPE_REGISTRY (folder_type_registry), FALSE);
471        g_return_val_if_fail (type_name != NULL, FALSE);
472
473        folder_type = get_folder_type (folder_type_registry, type_name);
474        if (folder_type == NULL) {
475                g_warning ("e_folder_type_registry_type_is_user_creatable() -- Unknown type `%s'", type_name);
476                return FALSE;
477        }
478
479        return folder_type->user_creatable;
480}
481
482const char *
483e_folder_type_registry_get_display_name_for_type (EFolderTypeRegistry *folder_type_registry,
484                                                  const char *type_name)
485{
486        const FolderType *folder_type;
487
488        g_return_val_if_fail (folder_type_registry != NULL, NULL);
489        g_return_val_if_fail (E_IS_FOLDER_TYPE_REGISTRY (folder_type_registry), NULL);
490        g_return_val_if_fail (type_name != NULL, NULL);
491
492        folder_type = get_folder_type (folder_type_registry, type_name);
493        if (folder_type == NULL) {
494                g_warning ("e_folder_type_registry_type_get_display_name_for_type() -- Unknown type `%s'", type_name);
495                return FALSE;
496        }
497
498        return folder_type->display_name;
499}
500
501const char *
502e_folder_type_registry_get_description_for_type (EFolderTypeRegistry *folder_type_registry,
503                                                 const char *type_name)
504{
505        const FolderType *folder_type;
506
507        g_return_val_if_fail (folder_type_registry != NULL, NULL);
508        g_return_val_if_fail (E_IS_FOLDER_TYPE_REGISTRY (folder_type_registry), NULL);
509        g_return_val_if_fail (type_name != NULL, NULL);
510
511        folder_type = get_folder_type (folder_type_registry, type_name);
512        if (folder_type == NULL) {
513                g_warning ("e_folder_type_registry_get_description_for_type() -- Unknown type `%s'", type_name);
514                return FALSE;
515        }
516
517        return folder_type->description;
518}
519
520
521GList *
522e_folder_type_registry_get_exported_dnd_types_for_type (EFolderTypeRegistry *folder_type_registry,
523                                                        const char *type_name)
524{
525        const FolderType *folder_type;
526
527        g_return_val_if_fail (folder_type_registry != NULL, NULL);
528        g_return_val_if_fail (E_IS_FOLDER_TYPE_REGISTRY (folder_type_registry), NULL);
529        g_return_val_if_fail (type_name != NULL, NULL);
530
531        folder_type = get_folder_type (folder_type_registry, type_name);
532        if (folder_type == NULL) {
533                g_warning ("e_folder_type_registry_get_exported_dnd_types_for_type() -- Unknown type `%s'", type_name);
534                return NULL;
535        }
536
537        return folder_type->exported_dnd_types;
538}
539
540GList *
541e_folder_type_registry_get_accepted_dnd_types_for_type (EFolderTypeRegistry *folder_type_registry,
542                                                        const char *type_name)
543{
544        const FolderType *folder_type;
545
546        g_return_val_if_fail (folder_type_registry != NULL, NULL);
547        g_return_val_if_fail (E_IS_FOLDER_TYPE_REGISTRY (folder_type_registry), NULL);
548        g_return_val_if_fail (type_name != NULL, NULL);
549
550        folder_type = get_folder_type (folder_type_registry, type_name);
551        if (folder_type == NULL) {
552                g_warning ("e_folder_type_registry_get_accepted_dnd_types_for_type() -- Unknown type `%s'", type_name);
553                return NULL;
554        }
555
556        return folder_type->accepted_dnd_types;
557}
558
559
560E_MAKE_TYPE (e_folder_type_registry, "EFolderTypeRegistry", EFolderTypeRegistry,
561             class_init, init, PARENT_TYPE)
Note: See TracBrowser for help on using the repository browser.