2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 Michael Rasmussen and the Claws Mail team
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.
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.
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/>.
22 #include "claws-features.h"
30 #include "addressbook.h"
31 #include "addressitem.h"
34 #include "manage_window.h"
37 #include "editaddress.h"
38 #include "editaddress_other_attributes_ldap.h"
39 #include "prefs_common.h"
41 #define ATTRIB_COL_WIDTH_NAME 120
42 #define ATTRIB_COL_WIDTH_VALUE 180
44 PersonEditDlg *personEditDlg;
45 gboolean attrib_adding = FALSE, attrib_saving = FALSE;
47 int get_attribute_index(const gchar *string_literal) {
49 /*int count = sizeof(ATTRIBUTE) / sizeof(*ATTRIBUTE);*/
50 const gchar **attribute = ATTRIBUTE;
52 cm_return_val_if_fail(string_literal != NULL, -1);
54 debug_print("Comparing %s to %s\n", *attribute, string_literal);
55 if (strcmp(*attribute++, string_literal) == 0)
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);
66 gtk_statusbar_push(GTK_STATUSBAR(personEditDlg->statusbar), personEditDlg->status_cid, msg);
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), "");
76 static gboolean list_find_attribute(const gchar *attr)
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));
82 UserAttribute *attrib;
84 if (!gtk_tree_model_get_iter_first(model, &iter))
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);
93 } while (gtk_tree_model_iter_next(model, &iter));
99 * Comparison using cell contents (text in first column). Used for sort
100 * address index widget.
102 static gint edit_person_attrib_compare_func(GtkCMCList *clist, gconstpointer ptr1, gconstpointer ptr2) {
103 GtkCMCell *cell1 = ((GtkCMCListRow *)ptr1)->cell;
104 GtkCMCell *cell2 = ((GtkCMCListRow *)ptr2)->cell;
105 gchar *name1 = NULL, *name2 = NULL;
107 if (cell1) name1 = cell1->u.text;
108 if (cell2) name2 = cell2->u.text;
109 if (!name1) return (name2 != NULL);
110 if (!name2) return -1;
111 return g_utf8_collate(name1, name2);
114 static void edit_person_combo_box_changed(GtkComboBox *opt_menu, gpointer data)
116 GtkWidget *view = GTK_WIDGET(data);
117 UserAttribute *attrib = gtkut_tree_view_get_selected_pointer(
118 GTK_TREE_VIEW(view), ATTRIB_COL_PTR, NULL, NULL, NULL);
119 gint option = gtk_combo_box_get_active(opt_menu);
120 const gchar *str = attrib ? attrib->name:"";
122 cm_return_if_fail (option < ATTRIBUTE_SIZE);
123 /* A corresponding attribute in contact does not match selected option */
124 if (strcmp(ATTRIBUTE[option], str) != 0) {
125 gtk_widget_set_sensitive(personEditDlg->attrib_add, TRUE);
126 gtk_widget_set_sensitive(personEditDlg->attrib_mod, TRUE);
127 gtk_widget_set_sensitive(personEditDlg->attrib_del, FALSE);
128 gtk_entry_set_text(GTK_ENTRY(personEditDlg->entry_atvalue), "");
129 gtk_widget_grab_focus(personEditDlg->entry_atvalue);
130 edit_person_status_show(NULL);
134 static void edit_person_attrib_cursor_changed(GtkTreeView *view,
137 UserAttribute *attrib = gtkut_tree_view_get_selected_pointer(
138 view, ATTRIB_COL_PTR, NULL, NULL, NULL);
140 if( attrib && !personEditDlg->read_only ) {
141 gint index = get_attribute_index(attrib->name);
146 gtk_combo_box_set_active(GTK_COMBO_BOX(personEditDlg->entry_atname), index);
147 gtk_entry_set_text(GTK_ENTRY(personEditDlg->entry_atvalue), attrib->value);
149 gtk_widget_set_sensitive(personEditDlg->attrib_del, TRUE);
151 gtk_widget_set_sensitive(personEditDlg->attrib_del, FALSE);
153 edit_person_status_show( NULL );
156 static void edit_person_attrib_delete(gpointer data) {
157 UserAttribute *attrib;
159 GtkTreeSelection *sel;
161 gboolean has_row = FALSE;
164 edit_person_attrib_clear(NULL);
165 edit_person_status_show(NULL);
167 attrib = gtkut_tree_view_get_selected_pointer(
168 GTK_TREE_VIEW(personEditDlg->view_attrib), ATTRIB_COL_PTR,
169 &model, &sel, &iter);
172 /* Remove list entry, and set iter to next row, if any */
173 has_row = gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
174 addritem_free_attribute(attrib);
178 /* Position hilite bar */
180 /* The removed row was the last in the list, so iter is not
181 * valid. Find out if there is at least one row remaining
182 * in the list, and select the last one if so. */
183 n = gtk_tree_model_iter_n_children(model, NULL);
184 if (n > 0 && gtk_tree_model_iter_nth_child(model, &iter, NULL, n-1)) {
191 gtk_tree_selection_select_iter(sel, &iter);
193 edit_person_attrib_cursor_changed(
194 GTK_TREE_VIEW(personEditDlg->view_attrib), NULL);
197 static UserAttribute *edit_person_attrib_edit(gboolean *error, UserAttribute *attrib) {
198 UserAttribute *retVal = NULL;
199 gchar *sName, *sValue, *sName_, *sValue_;
203 index = gtk_combo_box_get_active(GTK_COMBO_BOX(personEditDlg->entry_atname));
204 sName_ = (gchar *) ATTRIBUTE[index];
205 sValue_ = gtk_editable_get_chars(GTK_EDITABLE(personEditDlg->entry_atvalue), 0, -1);
206 sName = mgu_email_check_empty(sName_);
207 sValue = mgu_email_check_empty(sValue_);
210 if (sName && sValue) {
211 debug_print("sname && svalue\n");
212 if (attrib == NULL) {
213 attrib = addritem_create_attribute();
215 addritem_attrib_set_name(attrib, sName);
216 addritem_attrib_set_value(attrib, sValue);
221 edit_person_status_show(N_( "A Name and Value must be supplied." ));
222 gtk_widget_grab_focus(personEditDlg->entry_atvalue);
231 static void edit_person_attrib_modify(gpointer data) {
232 gboolean errFlg = FALSE;
235 UserAttribute *attrib;
237 attrib = gtkut_tree_view_get_selected_pointer(
238 GTK_TREE_VIEW(personEditDlg->view_attrib), ATTRIB_COL_PTR,
239 &model, NULL, &iter);
241 edit_person_attrib_edit(&errFlg, attrib);
243 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
244 ATTRIB_COL_NAME, attrib->name,
245 ATTRIB_COL_VALUE, attrib->value,
247 edit_person_attrib_clear(NULL);
252 static void edit_person_attrib_add(gpointer data) {
253 gboolean errFlg = FALSE;
254 GtkTreeModel *model = gtk_tree_view_get_model(
255 GTK_TREE_VIEW(personEditDlg->view_attrib));
256 GtkTreeSelection *sel = gtk_tree_view_get_selection(
257 GTK_TREE_VIEW(personEditDlg->view_attrib));
258 GtkTreeIter iter, iter2;
259 UserAttribute *attrib, *prev_attrib;
261 attrib = edit_person_attrib_edit(&errFlg, NULL);
262 if (attrib == NULL) /* input for new attribute is not valid */
265 prev_attrib = gtkut_tree_view_get_selected_pointer(
266 GTK_TREE_VIEW(personEditDlg->view_attrib), ATTRIB_COL_PTR,
267 &model, &sel, &iter);
269 if (prev_attrib == NULL) {
270 /* No row selected, or list empty, add it as first row. */
271 gtk_list_store_insert(GTK_LIST_STORE(model), &iter, 0);
273 /* Add it after the currently selected row. */
274 gtk_list_store_insert_after(GTK_LIST_STORE(model), &iter2,
279 /* Fill out the new row. */
280 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
281 ATTRIB_COL_NAME, attrib->name,
282 ATTRIB_COL_VALUE, attrib->value,
283 ATTRIB_COL_PTR, attrib,
285 gtk_tree_selection_select_iter(sel, &iter);
286 edit_person_attrib_clear(NULL);
289 static void edit_person_entry_att_changed (GtkWidget *entry, gpointer data)
291 GtkTreeModel *model = gtk_tree_view_get_model(
292 GTK_TREE_VIEW(personEditDlg->view_attrib));
293 gboolean non_empty = (gtk_tree_model_iter_n_children(model, NULL) > 0);
297 if (personEditDlg->read_only)
300 index = gtk_combo_box_get_active(GTK_COMBO_BOX(personEditDlg->entry_atname));
301 sName = ATTRIBUTE[index];
302 if (list_find_attribute(sName)) {
303 gtk_widget_set_sensitive(personEditDlg->attrib_add,FALSE);
304 gtk_widget_set_sensitive(personEditDlg->attrib_mod,non_empty);
305 attrib_adding = FALSE;
306 attrib_saving = non_empty;
309 gtk_widget_set_sensitive(personEditDlg->attrib_add,TRUE);
310 gtk_widget_set_sensitive(personEditDlg->attrib_mod,non_empty);
311 attrib_adding = TRUE;
312 attrib_saving = non_empty;
316 static gboolean edit_person_entry_att_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
318 if (event && (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter)) {
320 edit_person_attrib_modify(NULL);
321 else if (attrib_adding)
322 edit_person_attrib_add(NULL);
327 void addressbook_edit_person_page_attrib_ldap(PersonEditDlg *dialog, gint pageNum, gchar *pageLbl) {
328 GtkWidget *combo_box;
333 GtkWidget *vbuttonbox;
334 GtkWidget *buttonDel;
335 GtkWidget *buttonMod;
336 GtkWidget *buttonAdd;
340 GtkWidget *scrollwin;
342 GtkWidget *entry_value;
345 GtkTreeViewColumn *col;
346 GtkCellRenderer *rdr;
347 GtkTreeSelection *sel;
349 personEditDlg = dialog;
351 vbox = gtk_vbox_new(FALSE, 8);
352 gtk_widget_show(vbox);
353 gtk_container_add(GTK_CONTAINER(personEditDlg->notebook), vbox);
354 gtk_container_set_border_width(GTK_CONTAINER(vbox), BORDER_WIDTH);
356 label = gtk_label_new_with_mnemonic(pageLbl);
357 gtk_widget_show(label);
358 gtk_notebook_set_tab_label(
359 GTK_NOTEBOOK(personEditDlg->notebook),
360 gtk_notebook_get_nth_page(GTK_NOTEBOOK(personEditDlg->notebook), pageNum), label);
362 /* Split into two areas */
363 hbox = gtk_hbox_new(FALSE, 0);
364 gtk_container_add(GTK_CONTAINER(vbox), hbox);
367 vboxl = gtk_vbox_new(FALSE, 4);
368 gtk_container_add(GTK_CONTAINER(hbox), vboxl);
369 gtk_container_set_border_width(GTK_CONTAINER(vboxl), 4);
371 scrollwin = gtk_scrolled_window_new(NULL, NULL);
372 gtk_container_add(GTK_CONTAINER(vboxl), scrollwin);
373 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin),
374 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
376 store = gtk_list_store_new(ATTRIB_N_COLS,
377 G_TYPE_STRING, G_TYPE_STRING,
380 view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
381 g_object_unref(store);
382 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), TRUE);
383 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
384 gtk_tree_selection_set_mode(sel, GTK_SELECTION_BROWSE);
386 rdr = gtk_cell_renderer_text_new();
387 col = gtk_tree_view_column_new_with_attributes(_("Name"), rdr,
388 "markup", ATTRIB_COL_NAME, NULL);
389 gtk_tree_view_column_set_min_width(col, ATTRIB_COL_WIDTH_NAME);
390 gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
392 col = gtk_tree_view_column_new_with_attributes(_("Value"), rdr,
393 "markup", ATTRIB_COL_VALUE, NULL);
394 gtk_tree_view_column_set_min_width(col, ATTRIB_COL_WIDTH_VALUE);
395 gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
397 gtk_container_add(GTK_CONTAINER(scrollwin), view);
399 /* Data entry area */
400 table = gtk_table_new(4, 2, FALSE);
401 gtk_box_pack_start(GTK_BOX(vboxl), table, FALSE, FALSE, 0);
402 gtk_container_set_border_width(GTK_CONTAINER(table), 4);
403 gtk_table_set_row_spacings(GTK_TABLE(table), 4);
404 gtk_table_set_col_spacings(GTK_TABLE(table), 4);
408 label = gtk_label_new(N_("Name"));
409 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
410 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
412 gchar **attribute = (gchar **) ATTRIBUTE;
414 combo_box = gtk_combo_box_text_new();
417 if (!strcmp(*attribute, "jpegPhoto")) {
421 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo_box), *attribute++);
423 gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), 0);
425 gtk_table_attach(GTK_TABLE(table), combo_box, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
429 label = gtk_label_new(N_("Value"));
430 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
431 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
433 entry_value = gtk_entry_new();
434 gtk_table_attach(GTK_TABLE(table), entry_value, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
437 vboxb = gtk_vbox_new(FALSE, 4);
438 gtk_box_pack_start(GTK_BOX(hbox), vboxb, FALSE, FALSE, 2);
440 vbuttonbox = gtk_vbutton_box_new();
441 gtk_button_box_set_layout(GTK_BUTTON_BOX(vbuttonbox), GTK_BUTTONBOX_START);
442 gtk_box_set_spacing(GTK_BOX(vbuttonbox), 8);
443 gtk_container_set_border_width(GTK_CONTAINER(vbuttonbox), 4);
444 gtk_container_add(GTK_CONTAINER(vboxb), vbuttonbox);
447 buttonDel = gtk_button_new_from_stock(GTK_STOCK_DELETE);
448 gtk_container_add(GTK_CONTAINER(vbuttonbox), buttonDel);
450 buttonMod = gtk_button_new_from_stock(GTK_STOCK_SAVE);
451 gtk_container_add(GTK_CONTAINER(vbuttonbox), buttonMod);
453 buttonAdd = gtk_button_new_from_stock(GTK_STOCK_ADD);
454 gtk_container_add(GTK_CONTAINER(vbuttonbox), buttonAdd);
456 gtk_widget_set_sensitive(buttonDel,FALSE);
457 gtk_widget_set_sensitive(buttonMod,FALSE);
458 gtk_widget_set_sensitive(buttonAdd,FALSE);
460 gtk_widget_show_all(vbox);
463 g_signal_connect(G_OBJECT(view), "cursor-changed",
464 G_CALLBACK(edit_person_attrib_cursor_changed), NULL);
465 g_signal_connect(G_OBJECT(buttonDel), "clicked",
466 G_CALLBACK(edit_person_attrib_delete), NULL);
467 g_signal_connect(G_OBJECT(buttonMod), "clicked",
468 G_CALLBACK(edit_person_attrib_modify), NULL);
469 g_signal_connect(G_OBJECT(buttonAdd), "clicked",
470 G_CALLBACK(edit_person_attrib_add), NULL);
471 g_signal_connect(G_OBJECT(combo_box), "changed",
472 G_CALLBACK(edit_person_entry_att_changed), NULL);
473 g_signal_connect(G_OBJECT(entry_value), "key_press_event",
474 G_CALLBACK(edit_person_entry_att_pressed), NULL);
475 g_signal_connect(G_OBJECT(combo_box), "changed",
476 G_CALLBACK(edit_person_combo_box_changed), view);
478 personEditDlg->view_attrib = view;
479 personEditDlg->entry_atname = combo_box;
480 personEditDlg->entry_atvalue = entry_value;
481 personEditDlg->attrib_add = buttonAdd;
482 personEditDlg->attrib_del = buttonDel;
483 personEditDlg->attrib_mod = buttonMod;
486 #endif /* USE_LDAP */