1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Michael Rasmussen and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24 
25 #ifdef USE_LDAP
26 
27 #include "defs.h"
28 
29 #include "mgutils.h"
30 #include "addressbook.h"
31 #include "addressitem.h"
32 #include "addritem.h"
33 #include "addrbook.h"
34 #include "manage_window.h"
35 #include "gtkutils.h"
36 #include "codeconv.h"
37 #include "editaddress.h"
38 #include "editaddress_other_attributes_ldap.h"
39 #include "prefs_common.h"
40 
41 #define ATTRIB_COL_WIDTH_NAME	120
42 #define ATTRIB_COL_WIDTH_VALUE	180
43 
44 PersonEditDlg *personEditDlg;
45 gboolean attrib_adding = FALSE, attrib_saving = FALSE;
46 
get_attribute_index(const gchar * string_literal)47 int get_attribute_index(const gchar *string_literal) {
48 	int i = 0;
49 	/*int count = sizeof(ATTRIBUTE) / sizeof(*ATTRIBUTE);*/
50 	const gchar **attribute = ATTRIBUTE;
51 
52 	cm_return_val_if_fail(string_literal != NULL, -1);
53 	while (*attribute) {
54 		debug_print("Comparing %s to %s\n", *attribute, string_literal);
55 		if (strcmp(*attribute++, string_literal) == 0)
56 			return i;
57 		i++;
58 	}
59 	return -1;
60 }
61 
edit_person_status_show(gchar * msg)62 static void edit_person_status_show(gchar *msg) {
63 	if (personEditDlg->statusbar != NULL) {
64 		gtk_statusbar_pop(GTK_STATUSBAR(personEditDlg->statusbar), personEditDlg->status_cid);
65 		if(msg) {
66 			gtk_statusbar_push(GTK_STATUSBAR(personEditDlg->statusbar), personEditDlg->status_cid, msg);
67 		}
68 	}
69 }
70 
edit_person_attrib_clear(gpointer data)71 static void edit_person_attrib_clear(gpointer data) {
72 	gtk_combo_box_set_active(GTK_COMBO_BOX(personEditDlg->entry_atname), 0);
73 	gtk_entry_set_text(GTK_ENTRY(personEditDlg->entry_atvalue), "");
74 }
75 
list_find_attribute(const gchar * attr)76 static gboolean list_find_attribute(const gchar *attr)
77 {
78 	GtkWidget *view = personEditDlg->view_attrib;
79 	GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
80 	GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
81 	GtkTreeIter iter;
82 	UserAttribute *attrib;
83 
84 	if (!gtk_tree_model_get_iter_first(model, &iter))
85 		return FALSE;
86 
87 	do {
88 		gtk_tree_model_get(model, &iter, ATTRIB_COL_PTR, &attrib, -1);
89 		if (!g_ascii_strcasecmp(attrib->name, attr)) {
90 			gtk_tree_selection_select_iter(sel, &iter);
91 			return TRUE;
92 		}
93 	} while (gtk_tree_model_iter_next(model, &iter));
94 
95 	return FALSE;
96 }
97 
edit_person_combo_box_changed(GtkComboBox * opt_menu,gpointer data)98 static void edit_person_combo_box_changed(GtkComboBox *opt_menu, gpointer data)
99 {
100 	GtkWidget *view = GTK_WIDGET(data);
101 	UserAttribute *attrib = gtkut_tree_view_get_selected_pointer(
102 			GTK_TREE_VIEW(view), ATTRIB_COL_PTR, NULL, NULL, NULL);
103 	gint option = gtk_combo_box_get_active(opt_menu);
104 	const gchar *str = attrib ? attrib->name:"";
105 
106 	cm_return_if_fail (option < ATTRIBUTE_SIZE);
107 	/* A corresponding attribute in contact does not match selected option */
108 	if (strcmp(ATTRIBUTE[option], str) != 0) {
109 		gtk_widget_set_sensitive(personEditDlg->attrib_add, TRUE);
110 		gtk_widget_set_sensitive(personEditDlg->attrib_mod, TRUE);
111 		gtk_widget_set_sensitive(personEditDlg->attrib_del, FALSE);
112 		gtk_entry_set_text(GTK_ENTRY(personEditDlg->entry_atvalue), "");
113 		gtk_widget_grab_focus(personEditDlg->entry_atvalue);
114 		edit_person_status_show(NULL);
115 	}
116 }
117 
edit_person_attrib_cursor_changed(GtkTreeView * view,gpointer user_data)118 static void edit_person_attrib_cursor_changed(GtkTreeView *view,
119 		gpointer user_data)
120 {
121 	UserAttribute *attrib = gtkut_tree_view_get_selected_pointer(
122 			view, ATTRIB_COL_PTR, NULL, NULL, NULL);
123 
124 	if( attrib && !personEditDlg->read_only ) {
125 		gint index = get_attribute_index(attrib->name);
126 
127 		if (index == -1)
128 			index = 0;
129 
130 		gtk_combo_box_set_active(GTK_COMBO_BOX(personEditDlg->entry_atname), index);
131 		gtk_entry_set_text(GTK_ENTRY(personEditDlg->entry_atvalue), attrib->value);
132 
133 		gtk_widget_set_sensitive(personEditDlg->attrib_del, TRUE);
134 	} else {
135 		gtk_widget_set_sensitive(personEditDlg->attrib_del, FALSE);
136 	}
137 	edit_person_status_show( NULL );
138 }
139 
edit_person_attrib_delete(gpointer data)140 static void edit_person_attrib_delete(gpointer data) {
141 	UserAttribute *attrib;
142 	GtkTreeModel *model;
143 	GtkTreeSelection *sel;
144 	GtkTreeIter iter;
145 	gboolean has_row = FALSE;
146 	gint n;
147 
148 	edit_person_attrib_clear(NULL);
149 	edit_person_status_show(NULL);
150 
151 	attrib = gtkut_tree_view_get_selected_pointer(
152 			GTK_TREE_VIEW(personEditDlg->view_attrib), ATTRIB_COL_PTR,
153 			&model, &sel, &iter);
154 
155 	if (attrib) {
156 		/* Remove list entry, and set iter to next row, if any */
157 		has_row = gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
158 		addritem_free_attribute(attrib);
159 		attrib = NULL;
160 	}
161 
162 	/* Position hilite bar */
163 	if (!has_row) {
164 		/* The removed row was the last in the list, so iter is not
165 		 * valid. Find out if there is at least one row remaining
166 		 * in the list, and select the last one if so. */
167 		n = gtk_tree_model_iter_n_children(model, NULL);
168 		if (n > 0 && gtk_tree_model_iter_nth_child(model, &iter, NULL, n-1)) {
169 			/* It exists. */
170 			has_row = TRUE;
171 		}
172 	}
173 
174 	if (!has_row)
175 		gtk_tree_selection_select_iter(sel, &iter);
176 
177 	edit_person_attrib_cursor_changed(
178 			GTK_TREE_VIEW(personEditDlg->view_attrib), NULL);
179 }
180 
edit_person_attrib_edit(gboolean * error,UserAttribute * attrib)181 static UserAttribute *edit_person_attrib_edit(gboolean *error, UserAttribute *attrib) {
182 	UserAttribute *retVal = NULL;
183 	gchar *sName, *sValue, *sName_, *sValue_;
184 	gint index;
185 
186 	*error = TRUE;
187 	index = gtk_combo_box_get_active(GTK_COMBO_BOX(personEditDlg->entry_atname));
188 	sName_ = (gchar *) ATTRIBUTE[index];
189 	sValue_ = gtk_editable_get_chars(GTK_EDITABLE(personEditDlg->entry_atvalue), 0, -1);
190 	sName = mgu_email_check_empty(sName_);
191 	sValue = mgu_email_check_empty(sValue_);
192 	g_free(sValue_);
193 
194 	if (sName && sValue) {
195 		debug_print("sname && svalue\n");
196 		if (attrib == NULL) {
197 			attrib = addritem_create_attribute();
198 		}
199 		addritem_attrib_set_name(attrib, sName);
200 		addritem_attrib_set_value(attrib, sValue);
201 		retVal = attrib;
202 		*error = FALSE;
203 	}
204 	else {
205 		edit_person_status_show(N_( "A Name and Value must be supplied." ));
206 		gtk_widget_grab_focus(personEditDlg->entry_atvalue);
207 	}
208 
209 	g_free(sName);
210 	g_free(sValue);
211 
212 	return retVal;
213 }
214 
edit_person_attrib_modify(gpointer data)215 static void edit_person_attrib_modify(gpointer data) {
216 	gboolean errFlg = FALSE;
217 	GtkTreeModel *model;
218 	GtkTreeIter iter;
219 	UserAttribute *attrib;
220 
221 	attrib = gtkut_tree_view_get_selected_pointer(
222 			GTK_TREE_VIEW(personEditDlg->view_attrib), ATTRIB_COL_PTR,
223 			&model, NULL, &iter);
224 	if (attrib) {
225 		edit_person_attrib_edit(&errFlg, attrib);
226 		if (!errFlg) {
227 			gtk_list_store_set(GTK_LIST_STORE(model), &iter,
228 					ATTRIB_COL_NAME, attrib->name,
229 					ATTRIB_COL_VALUE, attrib->value,
230 					-1);
231 			edit_person_attrib_clear(NULL);
232 		}
233 	}
234 }
235 
edit_person_attrib_add(gpointer data)236 static void edit_person_attrib_add(gpointer data) {
237 	gboolean errFlg = FALSE;
238 	GtkTreeModel *model = gtk_tree_view_get_model(
239 			GTK_TREE_VIEW(personEditDlg->view_attrib));
240 	GtkTreeSelection *sel = gtk_tree_view_get_selection(
241 			GTK_TREE_VIEW(personEditDlg->view_attrib));
242 	GtkTreeIter iter, iter2;
243 	UserAttribute *attrib, *prev_attrib;
244 
245 	attrib = edit_person_attrib_edit(&errFlg, NULL);
246 	if (attrib == NULL) /* input for new attribute is not valid */
247 		return;
248 
249 	prev_attrib = gtkut_tree_view_get_selected_pointer(
250 			GTK_TREE_VIEW(personEditDlg->view_attrib), ATTRIB_COL_PTR,
251 			&model, &sel, &iter);
252 
253 	if (prev_attrib == NULL) {
254 		/* No row selected, or list empty, add it as first row. */
255 		gtk_list_store_insert(GTK_LIST_STORE(model), &iter, 0);
256 	} else {
257 		/* Add it after the currently selected row. */
258 		gtk_list_store_insert_after(GTK_LIST_STORE(model), &iter2,
259 				&iter);
260 		iter = iter2;
261 	}
262 
263 	/* Fill out the new row. */
264 	gtk_list_store_set(GTK_LIST_STORE(model), &iter,
265 			ATTRIB_COL_NAME, attrib->name,
266 			ATTRIB_COL_VALUE, attrib->value,
267 			ATTRIB_COL_PTR, attrib,
268 			-1);
269 	gtk_tree_selection_select_iter(sel, &iter);
270 	edit_person_attrib_clear(NULL);
271 }
272 
edit_person_entry_att_changed(GtkWidget * entry,gpointer data)273 static void edit_person_entry_att_changed (GtkWidget *entry, gpointer data)
274 {
275 	GtkTreeModel *model = gtk_tree_view_get_model(
276 			GTK_TREE_VIEW(personEditDlg->view_attrib));
277 	gboolean non_empty = (gtk_tree_model_iter_n_children(model, NULL) > 0);
278 	const gchar *sName;
279 	int index;
280 
281 	if (personEditDlg->read_only)
282 		return;
283 
284 	index = gtk_combo_box_get_active(GTK_COMBO_BOX(personEditDlg->entry_atname));
285 	sName = ATTRIBUTE[index];
286 	if (list_find_attribute(sName)) {
287 		gtk_widget_set_sensitive(personEditDlg->attrib_add,FALSE);
288 		gtk_widget_set_sensitive(personEditDlg->attrib_mod,non_empty);
289 		attrib_adding = FALSE;
290 		attrib_saving = non_empty;
291 	}
292 	else {
293 		gtk_widget_set_sensitive(personEditDlg->attrib_add,TRUE);
294 		gtk_widget_set_sensitive(personEditDlg->attrib_mod,non_empty);
295 		attrib_adding = TRUE;
296 		attrib_saving = non_empty;
297 	}
298 }
299 
edit_person_entry_att_pressed(GtkWidget * widget,GdkEventKey * event,gpointer data)300 static gboolean edit_person_entry_att_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
301 {
302 	if (event && (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter)) {
303 		if (attrib_saving)
304 			edit_person_attrib_modify(NULL);
305 		else if (attrib_adding)
306 			edit_person_attrib_add(NULL);
307 	}
308 	return FALSE;
309 }
310 
addressbook_edit_person_page_attrib_ldap(PersonEditDlg * dialog,gint pageNum,gchar * pageLbl)311 void addressbook_edit_person_page_attrib_ldap(PersonEditDlg *dialog, gint pageNum, gchar *pageLbl) {
312 	GtkWidget *combo_box;
313 	GtkWidget *vbox;
314 	GtkWidget *hbox;
315 	GtkWidget *vboxl;
316 	GtkWidget *vboxb;
317 	GtkWidget *vbuttonbox;
318 	GtkWidget *buttonDel;
319 	GtkWidget *buttonMod;
320 	GtkWidget *buttonAdd;
321 
322 	GtkWidget *table;
323 	GtkWidget *label;
324 	GtkWidget *scrollwin;
325 	GtkWidget *view;
326 	GtkWidget *entry_value;
327 	gint top;
328 	GtkListStore *store;
329 	GtkTreeViewColumn *col;
330 	GtkCellRenderer *rdr;
331 	GtkTreeSelection *sel;
332 
333 	personEditDlg = dialog;
334 
335 	vbox = gtk_vbox_new(FALSE, 8);
336 	gtk_widget_show(vbox);
337 	gtk_container_add(GTK_CONTAINER(personEditDlg->notebook), vbox);
338 	gtk_container_set_border_width(GTK_CONTAINER(vbox), BORDER_WIDTH);
339 
340 	label = gtk_label_new_with_mnemonic(pageLbl);
341 	gtk_widget_show(label);
342 	gtk_notebook_set_tab_label(
343 		GTK_NOTEBOOK(personEditDlg->notebook),
344 		gtk_notebook_get_nth_page(GTK_NOTEBOOK(personEditDlg->notebook), pageNum), label);
345 
346 	/* Split into two areas */
347 	hbox = gtk_hbox_new(FALSE, 0);
348 	gtk_container_add(GTK_CONTAINER(vbox), hbox);
349 
350 	/* Attribute list */
351 	vboxl = gtk_vbox_new(FALSE, 4);
352 	gtk_container_add(GTK_CONTAINER(hbox), vboxl);
353 	gtk_container_set_border_width(GTK_CONTAINER(vboxl), 4);
354 
355 	scrollwin = gtk_scrolled_window_new(NULL, NULL);
356 	gtk_container_add(GTK_CONTAINER(vboxl), scrollwin);
357 	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin),
358 				       GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
359 
360 	store = gtk_list_store_new(ATTRIB_N_COLS,
361 			G_TYPE_STRING, G_TYPE_STRING,
362 			G_TYPE_POINTER, -1);
363 
364 	view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
365 	g_object_unref(store);
366 	gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), TRUE);
367 	sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
368 	gtk_tree_selection_set_mode(sel, GTK_SELECTION_BROWSE);
369 
370 	rdr = gtk_cell_renderer_text_new();
371 	col = gtk_tree_view_column_new_with_attributes(_("Name"), rdr,
372 			"markup", ATTRIB_COL_NAME, NULL);
373 	gtk_tree_view_column_set_min_width(col, ATTRIB_COL_WIDTH_NAME);
374 	gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
375 
376 	col = gtk_tree_view_column_new_with_attributes(_("Value"), rdr,
377 			"markup", ATTRIB_COL_VALUE, NULL);
378 	gtk_tree_view_column_set_min_width(col, ATTRIB_COL_WIDTH_VALUE);
379 	gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
380 
381 	gtk_container_add(GTK_CONTAINER(scrollwin), view);
382 
383 	/* Data entry area */
384 	table = gtk_table_new(4, 2, FALSE);
385 	gtk_box_pack_start(GTK_BOX(vboxl), table, FALSE, FALSE, 0);
386 	gtk_container_set_border_width(GTK_CONTAINER(table), 4);
387 	gtk_table_set_row_spacings(GTK_TABLE(table), 4);
388 	gtk_table_set_col_spacings(GTK_TABLE(table), 4);
389 
390 	/* First row */
391 	top = 0;
392 	label = gtk_label_new(N_("Name"));
393 	gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
394 	gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
395 
396 	gchar **attribute = (gchar **) ATTRIBUTE;
397 
398 	combo_box = gtk_combo_box_text_new();
399 
400 	while (*attribute) {
401 		if (!strcmp(*attribute, "jpegPhoto")) {
402 			attribute++;
403 			continue;
404 		}
405 		gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo_box), *attribute++);
406 	}
407 	gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), 0);
408 
409 	gtk_table_attach(GTK_TABLE(table), combo_box, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
410 
411 	/* Next row */
412 	++top;
413 	label = gtk_label_new(N_("Value"));
414 	gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
415 	gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
416 
417 	entry_value = gtk_entry_new();
418 	gtk_table_attach(GTK_TABLE(table), entry_value, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
419 
420 	/* Button box */
421 	vboxb = gtk_vbox_new(FALSE, 4);
422 	gtk_box_pack_start(GTK_BOX(hbox), vboxb, FALSE, FALSE, 2);
423 
424 	vbuttonbox = gtk_vbutton_box_new();
425 	gtk_button_box_set_layout(GTK_BUTTON_BOX(vbuttonbox), GTK_BUTTONBOX_START);
426 	gtk_box_set_spacing(GTK_BOX(vbuttonbox), 8);
427 	gtk_container_set_border_width(GTK_CONTAINER(vbuttonbox), 4);
428 	gtk_container_add(GTK_CONTAINER(vboxb), vbuttonbox);
429 
430 	/* Buttons */
431 	buttonDel = gtk_button_new_from_stock(GTK_STOCK_DELETE);
432 	gtk_container_add(GTK_CONTAINER(vbuttonbox), buttonDel);
433 
434 	buttonMod = gtk_button_new_from_stock(GTK_STOCK_SAVE);
435 	gtk_container_add(GTK_CONTAINER(vbuttonbox), buttonMod);
436 
437 	buttonAdd = gtk_button_new_from_stock(GTK_STOCK_ADD);
438 	gtk_container_add(GTK_CONTAINER(vbuttonbox), buttonAdd);
439 
440 	gtk_widget_set_sensitive(buttonDel,FALSE);
441 	gtk_widget_set_sensitive(buttonMod,FALSE);
442 	gtk_widget_set_sensitive(buttonAdd,FALSE);
443 
444 	gtk_widget_show_all(vbox);
445 
446 	/* Event handlers */
447 	g_signal_connect(G_OBJECT(view), "cursor-changed",
448 			G_CALLBACK(edit_person_attrib_cursor_changed), NULL);
449 	g_signal_connect(G_OBJECT(buttonDel), "clicked",
450 			  G_CALLBACK(edit_person_attrib_delete), NULL);
451 	g_signal_connect(G_OBJECT(buttonMod), "clicked",
452 			  G_CALLBACK(edit_person_attrib_modify), NULL);
453 	g_signal_connect(G_OBJECT(buttonAdd), "clicked",
454 			  G_CALLBACK(edit_person_attrib_add), NULL);
455 	g_signal_connect(G_OBJECT(combo_box), "changed",
456 			 G_CALLBACK(edit_person_entry_att_changed), NULL);
457 	g_signal_connect(G_OBJECT(entry_value), "key_press_event",
458 			 G_CALLBACK(edit_person_entry_att_pressed), NULL);
459 	g_signal_connect(G_OBJECT(combo_box), "changed",
460 			 G_CALLBACK(edit_person_combo_box_changed), view);
461 
462 	personEditDlg->view_attrib  = view;
463 	personEditDlg->entry_atname  = combo_box;
464 	personEditDlg->entry_atvalue = entry_value;
465 	personEditDlg->attrib_add = buttonAdd;
466 	personEditDlg->attrib_del = buttonDel;
467 	personEditDlg->attrib_mod = buttonMod;
468 }
469 
470 #endif	/* USE_LDAP */
471 
472 
473