1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "evolution-config.h"
19 
20 #include <string.h>
21 #include <gtk/gtk.h>
22 #include <gdk/gdkkeysyms.h>
23 #include <glib/gi18n-lib.h>
24 
25 #include <libedataserver/libedataserver.h>
26 
27 #include "e-categories-dialog.h"
28 #include "e-categories-editor.h"
29 #include "e-categories-selector.h"
30 #include "e-category-completion.h"
31 #include "e-category-editor.h"
32 
33 #define E_CATEGORIES_DIALOG_GET_PRIVATE(obj) \
34 	(G_TYPE_INSTANCE_GET_PRIVATE \
35 	((obj), E_TYPE_CATEGORIES_DIALOG, ECategoriesDialogPrivate))
36 
37 G_DEFINE_TYPE (ECategoriesDialog, e_categories_dialog, GTK_TYPE_DIALOG)
38 
39 struct _ECategoriesDialogPrivate {
40 	GtkWidget *categories_editor;
41 };
42 
43 static void
entry_changed_cb(GtkEntry * entry,ECategoriesDialog * dialog)44 entry_changed_cb (GtkEntry *entry,
45                   ECategoriesDialog *dialog)
46 {
47 	gtk_dialog_set_response_sensitive (
48 		GTK_DIALOG (dialog), GTK_RESPONSE_OK, TRUE);
49 }
50 
51 static void
e_categories_dialog_class_init(ECategoriesDialogClass * class)52 e_categories_dialog_class_init (ECategoriesDialogClass *class)
53 {
54 	g_type_class_add_private (class, sizeof (ECategoriesDialogPrivate));
55 }
56 
57 static void
e_categories_dialog_init(ECategoriesDialog * dialog)58 e_categories_dialog_init (ECategoriesDialog *dialog)
59 {
60 	GtkWidget *dialog_content;
61 	GtkWidget *categories_editor;
62 
63 	dialog->priv = E_CATEGORIES_DIALOG_GET_PRIVATE (dialog);
64 
65 	categories_editor = e_categories_editor_new ();
66 	dialog->priv->categories_editor = categories_editor;
67 
68 	dialog_content = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
69 	gtk_container_set_border_width (GTK_CONTAINER (dialog), 12);
70 	gtk_box_pack_start (
71 		GTK_BOX (dialog_content), categories_editor, TRUE, TRUE, 0);
72 	gtk_box_set_spacing (GTK_BOX (dialog_content), 12);
73 
74 	g_signal_connect (
75 		categories_editor, "entry-changed",
76 		G_CALLBACK (entry_changed_cb), dialog);
77 
78 	gtk_dialog_add_buttons (
79 		GTK_DIALOG (dialog),
80 		_("_Cancel"), GTK_RESPONSE_CANCEL,
81 		_("_OK"), GTK_RESPONSE_OK, NULL);
82 	gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
83 	gtk_dialog_set_response_sensitive (
84 		GTK_DIALOG (dialog), GTK_RESPONSE_OK, FALSE);
85 	gtk_window_set_title (GTK_WINDOW (dialog), _("Categories"));
86 
87 	gtk_widget_show_all (categories_editor);
88 }
89 
90 /**
91  * e_categories_dialog_new:
92  * @categories: Comma-separated list of categories
93  *
94  * Creates a new #ECategoriesDialog widget and sets the initial selection
95  * to @categories.
96  *
97  * Returns: a new #ECategoriesDialog
98  **/
99 GtkWidget *
e_categories_dialog_new(const gchar * categories)100 e_categories_dialog_new (const gchar *categories)
101 {
102 	ECategoriesDialog *dialog;
103 
104 	dialog = g_object_new (E_TYPE_CATEGORIES_DIALOG, NULL);
105 
106 	if (categories)
107 		e_categories_dialog_set_categories (dialog, categories);
108 
109 	return GTK_WIDGET (dialog);
110 }
111 
112 /**
113  * e_categories_dialog_get_categories:
114  * @dialog: An #ECategoriesDialog
115  *
116  * Gets a comma-separated list of the categories currently selected
117  * in the dialog.
118  *
119  * Returns: a comma-separated list of categories. Free returned
120  * pointer with g_free().
121  **/
122 gchar *
e_categories_dialog_get_categories(ECategoriesDialog * dialog)123 e_categories_dialog_get_categories (ECategoriesDialog *dialog)
124 {
125 	gchar *categories;
126 
127 	g_return_val_if_fail (E_IS_CATEGORIES_DIALOG (dialog), NULL);
128 
129 	categories = e_categories_editor_get_categories (
130 		E_CATEGORIES_EDITOR (dialog->priv->categories_editor));
131 
132 	return categories;
133 }
134 
135 /**
136  * e_categories_dialog_set_categories:
137  * @dialog: An #ECategoriesDialog
138  * @categories: Comma-separated list of categories
139  *
140  * Sets the list of categories selected on the dialog.
141  **/
142 void
e_categories_dialog_set_categories(ECategoriesDialog * dialog,const gchar * categories)143 e_categories_dialog_set_categories (ECategoriesDialog *dialog,
144                                     const gchar *categories)
145 {
146 	g_return_if_fail (E_IS_CATEGORIES_DIALOG (dialog));
147 
148 	e_categories_editor_set_categories (
149 		E_CATEGORIES_EDITOR (dialog->priv->categories_editor),
150 		categories);
151 }
152