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-editor.h"
28 #include "e-categories-selector.h"
29 #include "e-category-completion.h"
30 #include "e-category-editor.h"
31 #include "e-dialog-widgets.h"
32 #include "e-misc-utils.h"
33 
34 #define E_CATEGORIES_EDITOR_GET_PRIVATE(obj) \
35 	(G_TYPE_INSTANCE_GET_PRIVATE \
36 	((obj), E_TYPE_CATEGORIES_EDITOR, ECategoriesEditorPrivate))
37 
38 struct _ECategoriesEditorPrivate {
39 	ECategoriesSelector *categories_list;
40 	GtkWidget *categories_entry;
41 	GtkWidget *categories_entry_label;
42 	GtkWidget *new_button;
43 	GtkWidget *edit_button;
44 	GtkWidget *delete_button;
45 
46 	guint ignore_category_changes : 1;
47 	gulong category_checked_handler_id;
48 	gulong entry_changed_handler_id;
49 };
50 
51 enum {
52 	COLUMN_ACTIVE,
53 	COLUMN_ICON,
54 	COLUMN_CATEGORY,
55 	N_COLUMNS
56 };
57 
58 enum {
59 	PROP_0,
60 	PROP_ENTRY_VISIBLE
61 };
62 
63 enum {
64 	ENTRY_CHANGED,
65 	LAST_SIGNAL
66 };
67 
68 static gint signals[LAST_SIGNAL] = {0};
69 
G_DEFINE_TYPE(ECategoriesEditor,e_categories_editor,GTK_TYPE_GRID)70 G_DEFINE_TYPE (ECategoriesEditor, e_categories_editor, GTK_TYPE_GRID)
71 
72 static void
73 entry_changed_cb (GtkEntry *entry,
74                   ECategoriesEditor *editor)
75 {
76 	g_signal_handler_block (editor->priv->categories_list, editor->priv->category_checked_handler_id);
77 	e_categories_selector_set_checked (editor->priv->categories_list, gtk_entry_get_text (entry));
78 	g_signal_handler_unblock (editor->priv->categories_list, editor->priv->category_checked_handler_id);
79 
80 	g_signal_emit (editor, signals[ENTRY_CHANGED], 0);
81 }
82 
83 static void
categories_editor_selection_changed_cb(ECategoriesEditor * editor,GtkTreeSelection * selection)84 categories_editor_selection_changed_cb (ECategoriesEditor *editor,
85                                         GtkTreeSelection *selection)
86 {
87 	GtkWidget *widget;
88 	gint n_rows;
89 
90 	n_rows = gtk_tree_selection_count_selected_rows (selection);
91 
92 	widget = editor->priv->edit_button;
93 	gtk_widget_set_sensitive (widget, n_rows == 1);
94 
95 	widget = editor->priv->delete_button;
96 	gtk_widget_set_sensitive (widget, n_rows >= 1);
97 }
98 
99 static void
categories_editor_update_entry(ECategoriesEditor * editor)100 categories_editor_update_entry (ECategoriesEditor *editor)
101 {
102 	GtkEntry *entry;
103 	gchar *categories;
104 
105 	entry = GTK_ENTRY (editor->priv->categories_entry);
106 	categories = e_categories_selector_get_checked (editor->priv->categories_list);
107 
108 	g_signal_handler_block (entry, editor->priv->entry_changed_handler_id);
109 	gtk_entry_set_text (entry, categories);
110 	g_signal_handler_unblock (entry, editor->priv->entry_changed_handler_id);
111 
112 	g_free (categories);
113 }
114 
115 static void
categories_editor_category_checked_cb(ECategoriesEditor * editor)116 categories_editor_category_checked_cb (ECategoriesEditor *editor)
117 {
118 	categories_editor_update_entry (editor);
119 	g_signal_emit (editor, signals[ENTRY_CHANGED], 0);
120 }
121 
122 static void
new_button_clicked_cb(GtkButton * button,ECategoriesEditor * editor)123 new_button_clicked_cb (GtkButton *button,
124                        ECategoriesEditor *editor)
125 {
126 	GtkWidget *toplevel, *parent;
127 	ECategoryEditor *cat_editor;
128 
129 	toplevel = gtk_widget_get_toplevel (GTK_WIDGET (editor));
130 	if (GTK_IS_WINDOW (toplevel))
131 		parent = toplevel;
132 	else
133 		parent = NULL;
134 
135 	cat_editor = g_object_new (E_TYPE_CATEGORY_EDITOR,
136 		"transient-for", parent,
137 		NULL);
138 
139 	e_category_editor_create_category (cat_editor);
140 
141 	gtk_widget_destroy (GTK_WIDGET (cat_editor));
142 }
143 
144 static void
edit_button_clicked_cb(GtkButton * button,ECategoriesEditor * editor)145 edit_button_clicked_cb (GtkButton *button,
146                         ECategoriesEditor *editor)
147 {
148 	GtkWidget *toplevel, *parent;
149 	ECategoryEditor *cat_editor;
150 	gchar *category;
151 
152 	toplevel = gtk_widget_get_toplevel (GTK_WIDGET (editor));
153 	if (GTK_IS_WINDOW (toplevel))
154 		parent = toplevel;
155 	else
156 		parent = NULL;
157 
158 	cat_editor = g_object_new (E_TYPE_CATEGORY_EDITOR,
159 		"transient-for", parent,
160 		NULL);
161 
162 	category = e_categories_selector_get_selected (
163 		editor->priv->categories_list);
164 
165 	e_category_editor_edit_category (cat_editor, category);
166 
167 	gtk_widget_destroy (GTK_WIDGET (cat_editor));
168 	g_free (category);
169 }
170 
171 static void
delete_button_clicked_cb(GtkButton * button,ECategoriesEditor * editor)172 delete_button_clicked_cb (GtkButton *button,
173 			  ECategoriesEditor *editor)
174 {
175 	e_categories_selector_delete_selection (editor->priv->categories_list);
176 	categories_editor_update_entry (editor);
177 }
178 
179 static void
categories_editor_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)180 categories_editor_set_property (GObject *object,
181                                 guint property_id,
182                                 const GValue *value,
183                                 GParamSpec *pspec)
184 {
185 	switch (property_id) {
186 		case PROP_ENTRY_VISIBLE:
187 			e_categories_editor_set_entry_visible (
188 				E_CATEGORIES_EDITOR (object),
189 				g_value_get_boolean (value));
190 			return;
191 	}
192 
193 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
194 }
195 
196 static void
categories_editor_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)197 categories_editor_get_property (GObject *object,
198                                 guint property_id,
199                                 GValue *value,
200                                 GParamSpec *pspec)
201 {
202 	switch (property_id) {
203 		case PROP_ENTRY_VISIBLE:
204 			g_value_set_boolean (
205 				value, e_categories_editor_get_entry_visible (
206 				E_CATEGORIES_EDITOR (object)));
207 			return;
208 	}
209 
210 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
211 }
212 
213 static void
e_categories_editor_class_init(ECategoriesEditorClass * class)214 e_categories_editor_class_init (ECategoriesEditorClass *class)
215 {
216 	GObjectClass *object_class;
217 
218 	g_type_class_add_private (class, sizeof (ECategoriesEditorPrivate));
219 
220 	object_class = G_OBJECT_CLASS (class);
221 	object_class->set_property = categories_editor_set_property;
222 	object_class->get_property = categories_editor_get_property;
223 
224 	g_object_class_install_property (
225 		object_class,
226 		PROP_ENTRY_VISIBLE,
227 		g_param_spec_boolean (
228 			"entry-visible",
229 			NULL,
230 			NULL,
231 			TRUE,
232 			G_PARAM_READWRITE));
233 
234 	signals[ENTRY_CHANGED] = g_signal_new (
235 		"entry-changed",
236 		G_TYPE_FROM_CLASS (class),
237 		G_SIGNAL_RUN_FIRST,
238 		G_STRUCT_OFFSET (ECategoriesEditorClass, entry_changed),
239 		NULL, NULL,
240 		g_cclosure_marshal_VOID__VOID,
241 		G_TYPE_NONE, 0);
242 }
243 
244 static void
e_categories_editor_init(ECategoriesEditor * editor)245 e_categories_editor_init (ECategoriesEditor *editor)
246 {
247 	GtkEntryCompletion *completion;
248 	GtkGrid *grid;
249 	GtkWidget *entry_categories;
250 	GtkWidget *label_header;
251 	GtkWidget *label2;
252 	GtkWidget *scrolledwindow1;
253 	GtkWidget *categories_list;
254 	GtkWidget *hbuttonbox1;
255 	GtkWidget *button_new;
256 	GtkWidget *button_edit;
257 	GtkWidget *button_delete;
258 
259 	editor->priv = E_CATEGORIES_EDITOR_GET_PRIVATE (editor);
260 
261 	gtk_widget_set_size_request (GTK_WIDGET (editor), -1, 400);
262 
263 	grid = GTK_GRID (editor);
264 
265 	gtk_grid_set_row_spacing (grid, 6);
266 	gtk_grid_set_column_spacing (grid, 6);
267 
268 	label_header = gtk_label_new_with_mnemonic (
269 		_("Currently _used categories:"));
270 	gtk_widget_set_halign (label_header, GTK_ALIGN_FILL);
271 	gtk_grid_attach (grid, label_header, 0, 0, 1, 1);
272 	gtk_label_set_justify (GTK_LABEL (label_header), GTK_JUSTIFY_CENTER);
273 	gtk_misc_set_alignment (GTK_MISC (label_header), 0, 0.5);
274 
275 	entry_categories = gtk_entry_new ();
276 	gtk_widget_set_hexpand (entry_categories, TRUE);
277 	gtk_widget_set_halign (entry_categories, GTK_ALIGN_FILL);
278 	gtk_grid_attach (grid, entry_categories, 0, 1, 1, 1);
279 
280 	label2 = gtk_label_new_with_mnemonic (_("_Available Categories:"));
281 	gtk_widget_set_halign (label2, GTK_ALIGN_FILL);
282 	gtk_grid_attach (grid, label2, 0, 2, 1, 1);
283 	gtk_label_set_justify (GTK_LABEL (label2), GTK_JUSTIFY_CENTER);
284 	gtk_misc_set_alignment (GTK_MISC (label2), 0, 0.5);
285 
286 	scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
287 	g_object_set (
288 		G_OBJECT (scrolledwindow1),
289 		"hexpand", TRUE,
290 		"halign", GTK_ALIGN_FILL,
291 		"vexpand", TRUE,
292 		"valign", GTK_ALIGN_FILL,
293 		NULL);
294 	gtk_grid_attach (grid, scrolledwindow1, 0, 3, 1, 1);
295 	gtk_scrolled_window_set_policy (
296 		GTK_SCROLLED_WINDOW (scrolledwindow1),
297 		GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
298 	gtk_scrolled_window_set_shadow_type (
299 		GTK_SCROLLED_WINDOW (scrolledwindow1), GTK_SHADOW_IN);
300 
301 	categories_list = GTK_WIDGET (e_categories_selector_new ());
302 	gtk_container_add (GTK_CONTAINER (scrolledwindow1), categories_list);
303 	gtk_widget_set_size_request (categories_list, -1, 350);
304 	gtk_tree_view_set_headers_visible (
305 		GTK_TREE_VIEW (categories_list), FALSE);
306 	gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (categories_list), TRUE);
307 	editor->priv->category_checked_handler_id = g_signal_connect_swapped (
308 		G_OBJECT (categories_list), "category-checked",
309 		G_CALLBACK (categories_editor_category_checked_cb), editor);
310 
311 	hbuttonbox1 = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
312 	g_object_set (
313 		G_OBJECT (hbuttonbox1),
314 		"hexpand", TRUE,
315 		"halign", GTK_ALIGN_FILL,
316 		NULL);
317 	gtk_grid_attach (grid, hbuttonbox1, 0, 4, 1, 1);
318 	gtk_box_set_spacing (GTK_BOX (hbuttonbox1), 6);
319 
320 	button_new = e_dialog_button_new_with_icon ("document-new", C_("category", "_New"));
321 	gtk_container_add (GTK_CONTAINER (hbuttonbox1), button_new);
322 	gtk_widget_set_can_default (button_new, TRUE);
323 
324 	button_edit = gtk_button_new_with_mnemonic (C_("category", "_Edit"));
325 	gtk_container_add (GTK_CONTAINER (hbuttonbox1), button_edit);
326 	gtk_widget_set_can_default (button_edit, TRUE);
327 
328 	button_delete = e_dialog_button_new_with_icon ("edit-delete", C_("category", "_Delete"));
329 	gtk_container_add (GTK_CONTAINER (hbuttonbox1), button_delete);
330 	gtk_widget_set_can_default (button_delete, TRUE);
331 
332 	gtk_label_set_mnemonic_widget (
333 		GTK_LABEL (label_header), entry_categories);
334 	gtk_label_set_mnemonic_widget (
335 		GTK_LABEL (label2), categories_list);
336 
337 	editor->priv->categories_list = E_CATEGORIES_SELECTOR (categories_list);
338 	editor->priv->categories_entry = entry_categories;
339 	editor->priv->categories_entry_label = label_header;
340 
341 	g_signal_connect_swapped (
342 		editor->priv->categories_list, "selection-changed",
343 		G_CALLBACK (categories_editor_selection_changed_cb), editor);
344 
345 	completion = e_category_completion_new ();
346 	gtk_entry_set_completion (
347 		GTK_ENTRY (editor->priv->categories_entry), completion);
348 	g_object_unref (completion);
349 
350 	editor->priv->new_button = button_new;
351 	g_signal_connect (
352 		editor->priv->new_button, "clicked",
353 		G_CALLBACK (new_button_clicked_cb), editor);
354 
355 	editor->priv->edit_button = button_edit;
356 	g_signal_connect (
357 		editor->priv->edit_button, "clicked",
358 		G_CALLBACK (edit_button_clicked_cb), editor);
359 
360 	editor->priv->delete_button = button_delete;
361 	g_signal_connect (
362 		editor->priv->delete_button, "clicked",
363 		G_CALLBACK (delete_button_clicked_cb),
364 		editor);
365 
366 	editor->priv->entry_changed_handler_id = g_signal_connect (
367 		editor->priv->categories_entry, "changed",
368 		G_CALLBACK (entry_changed_cb), editor);
369 
370 	gtk_widget_show_all (GTK_WIDGET (editor));
371 }
372 
373 /**
374  * e_categories_editor_new:
375  *
376  * Creates a new #ECategoriesEditor widget.
377  *
378  * Returns: a new #ECategoriesEditor
379  *
380  * Since: 3.2
381  **/
382 GtkWidget *
e_categories_editor_new(void)383 e_categories_editor_new (void)
384 {
385 	return g_object_new (E_TYPE_CATEGORIES_EDITOR, NULL);
386 }
387 
388 /**
389  * e_categories_editor_get_categories:
390  * @editor: an #ECategoriesEditor
391  *
392  * Gets a comma-separated list of the categories currently selected
393  * in the editor.
394  *
395  * Returns: a comma-separated list of categories. Free returned
396  * pointer with g_free().
397  *
398  * Since: 3.2
399  **/
400 gchar *
e_categories_editor_get_categories(ECategoriesEditor * editor)401 e_categories_editor_get_categories (ECategoriesEditor *editor)
402 {
403 	g_return_val_if_fail (E_IS_CATEGORIES_EDITOR (editor), NULL);
404 
405 	if (e_categories_editor_get_entry_visible (editor)) {
406 		GString *categories;
407 		gchar **split;
408 		gint ii;
409 
410 		categories = g_string_new ("");
411 
412 		split = g_strsplit (gtk_entry_get_text (GTK_ENTRY (editor->priv->categories_entry)), ",", 0);
413 
414 		if (split) {
415 			GHashTable *known;
416 			GSList *items = NULL, *link;
417 
418 			known = g_hash_table_new (g_str_hash, g_str_equal);
419 
420 			for (ii = 0; split[ii] != NULL; ii++) {
421 				gchar *value = g_strstrip (split[ii]);
422 
423 				if (*value && g_hash_table_insert (known, value, GINT_TO_POINTER (1)))
424 					items = g_slist_prepend (items, value);
425 			}
426 
427 			items = g_slist_sort (items, e_collate_compare);
428 
429 			for (link = items; link; link = g_slist_next (link)) {
430 				if (categories->len)
431 					g_string_append_c (categories, ',');
432 				g_string_append (categories, link->data);
433 			}
434 
435 			g_hash_table_destroy (known);
436 			g_slist_free (items);
437 			g_strfreev (split);
438 		}
439 
440 		return g_string_free (categories, FALSE);
441 	}
442 
443 	return e_categories_selector_get_checked (editor->priv->categories_list);
444 }
445 
446 /**
447  * e_categories_editor_set_categories:
448  * @editor: an #ECategoriesEditor
449  * @categories: comma-separated list of categories
450  *
451  * Sets the list of categories selected on the editor.
452  *
453  * Since: 3.2
454  **/
455 void
e_categories_editor_set_categories(ECategoriesEditor * editor,const gchar * categories)456 e_categories_editor_set_categories (ECategoriesEditor *editor,
457                                     const gchar *categories)
458 {
459 	g_return_if_fail (E_IS_CATEGORIES_EDITOR (editor));
460 
461 	e_categories_selector_set_checked (editor->priv->categories_list, categories);
462 	categories_editor_update_entry (editor);
463 }
464 
465 /**
466  * e_categories_editor_get_entry_visible:
467  * @editor: an #ECategoriesEditor
468  *
469  * Return the visibility of the category input entry.
470  *
471  * Returns: whether the entry is visible
472  *
473  * Since: 3.2
474  **/
475 gboolean
e_categories_editor_get_entry_visible(ECategoriesEditor * editor)476 e_categories_editor_get_entry_visible (ECategoriesEditor *editor)
477 {
478 	g_return_val_if_fail (E_IS_CATEGORIES_EDITOR (editor), TRUE);
479 
480 	return gtk_widget_get_visible (editor->priv->categories_entry);
481 }
482 
483 /**
484  * e_categories_editor_set_entry_visible:
485  * @editor: an #ECategoriesEditor
486  * @entry_visible: whether to make the entry visible
487  *
488  * Sets the visibility of the category input entry.
489  *
490  * Since: 3.2
491  **/
492 void
e_categories_editor_set_entry_visible(ECategoriesEditor * editor,gboolean entry_visible)493 e_categories_editor_set_entry_visible (ECategoriesEditor *editor,
494                                        gboolean entry_visible)
495 {
496 	g_return_if_fail (E_IS_CATEGORIES_EDITOR (editor));
497 
498 	if ((gtk_widget_get_visible (editor->priv->categories_entry) ? 1 : 0) ==
499 	    (entry_visible ? 1 : 0))
500 		return;
501 
502 	gtk_widget_set_visible (
503 		editor->priv->categories_entry, entry_visible);
504 	gtk_widget_set_visible (
505 		editor->priv->categories_entry_label, entry_visible);
506 	e_categories_selector_set_items_checkable (
507 		editor->priv->categories_list, entry_visible);
508 
509 	g_object_notify (G_OBJECT (editor), "entry-visible");
510 }
511