No issues found
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2 of the License, or (at your option) version 3.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with the program; if not, see <http://www.gnu.org/licenses/>
14 *
15 *
16 * Authors:
17 * Rodrigo Moya <rodrigo@ximian.com>
18 *
19 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
20 *
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include "e-categories-config.h"
28
29 #include <string.h>
30 #include <gtk/gtk.h>
31 #include <glib/gi18n.h>
32
33 #include <libedataserverui/libedataserverui.h>
34
35 #include "e-util/e-util.h"
36
37 static GHashTable *pixbufs_cache = NULL;
38
39 static void
40 categories_changed_cb (gpointer object,
41 gpointer user_data)
42 {
43 if (pixbufs_cache)
44 g_hash_table_remove_all (pixbufs_cache);
45 }
46
47 static void
48 free_pixbuf_cb (gpointer ptr)
49 {
50 GdkPixbuf *pixbuf = ptr;
51
52 if (pixbuf)
53 g_object_unref (pixbuf);
54 }
55
56 /**
57 * e_categories_config_get_icon_for:
58 * @category: Category for which to get the icon.
59 * @pixbuf: A pointer to where the pixbuf will be returned.
60 *
61 * Returns the icon configured for the given category.
62 *
63 * Returns: the icon configured for the given category
64 */
65 gboolean
66 e_categories_config_get_icon_for (const gchar *category,
67 GdkPixbuf **pixbuf)
68 {
69 const gchar *icon_file;
70
71 g_return_val_if_fail (pixbuf != NULL, FALSE);
72 g_return_val_if_fail (category != NULL, FALSE);
73
74 if (!pixbufs_cache) {
75 pixbufs_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, free_pixbuf_cb);
76 e_categories_add_change_hook (
77 (GHookFunc) categories_changed_cb, NULL);
78 } else {
79 gpointer key = NULL, value = NULL;
80
81 if (g_hash_table_lookup_extended (pixbufs_cache, category, &key, &value)) {
82 *pixbuf = value;
83 if (*pixbuf)
84 g_object_ref (*pixbuf);
85 return *pixbuf != NULL;
86 }
87 }
88
89 icon_file = e_categories_get_icon_file_for (category);
90 if (!icon_file) {
91 *pixbuf = NULL;
92 } else {
93 /* load the icon in our list */
94 *pixbuf = gdk_pixbuf_new_from_file (icon_file, NULL);
95 }
96
97 g_hash_table_insert (pixbufs_cache, g_strdup (category), *pixbuf == NULL ? NULL : g_object_ref (*pixbuf));
98
99 return *pixbuf != NULL;
100 }
101
102 /**
103 * e_categories_config_open_dialog_for_entry:
104 * @entry: a #GtkEntry on which to get/set the categories list
105 *
106 * This is a self-contained function that lets you open a popup dialog for
107 * the user to select a list of categories.
108 *
109 * The @entry parameter is used, at initialization time, as the list of
110 * initial categories that are selected in the categories selection dialog.
111 * Then, when the user commits its changes, the list of selected categories
112 * is put back on the entry widget.
113 */
114 void
115 e_categories_config_open_dialog_for_entry (GtkEntry *entry)
116 {
117 GtkDialog *dialog;
118 const gchar *text;
119 gint result;
120
121 g_return_if_fail (entry != NULL);
122 g_return_if_fail (GTK_IS_ENTRY (entry));
123
124 text = gtk_entry_get_text (GTK_ENTRY (entry));
125 dialog = GTK_DIALOG (e_categories_dialog_new (text));
126
127 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (entry))));
128
129 /* run the dialog */
130 result = gtk_dialog_run (dialog);
131
132 if (result == GTK_RESPONSE_OK) {
133 gchar *categories;
134
135 categories = e_categories_dialog_get_categories (E_CATEGORIES_DIALOG (dialog));
136 gtk_entry_set_text (GTK_ENTRY (entry), categories);
137 g_free (categories);
138 }
139
140 gtk_widget_destroy (GTK_WIDGET (dialog));
141 }