Replace GtkSCTree in addressbook_foldersel.c with GtkTreeView.
[claws.git] / src / editaddress_other_attributes_ldap.c
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
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
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
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
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
98 /*
99 * Comparison using cell contents (text in first column). Used for sort
100 * address index widget.
101 */
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;
106
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);
112 }
113
114 static void edit_person_combo_box_changed(GtkComboBox *opt_menu, gpointer data)
115 {
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:"";
121
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);
131         }
132 }
133
134 static void edit_person_attrib_cursor_changed(GtkTreeView *view,
135                 gpointer user_data)
136 {
137         UserAttribute *attrib = gtkut_tree_view_get_selected_pointer(
138                         view, ATTRIB_COL_PTR, NULL, NULL, NULL);
139
140         if( attrib && !personEditDlg->read_only ) {
141                 gint index = get_attribute_index(attrib->name);
142
143                 if (index == -1)
144                         index = 0;
145
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);
148
149                 gtk_widget_set_sensitive(personEditDlg->attrib_del, TRUE);
150         } else {
151                 gtk_widget_set_sensitive(personEditDlg->attrib_del, FALSE);
152         }
153         edit_person_status_show( NULL );
154 }
155
156 static void edit_person_attrib_delete(gpointer data) {
157         UserAttribute *attrib;
158         GtkTreeModel *model;
159         GtkTreeSelection *sel;
160         GtkTreeIter iter;
161         gboolean has_row = FALSE;
162         gint n;
163
164         edit_person_attrib_clear(NULL);
165         edit_person_status_show(NULL);
166
167         attrib = gtkut_tree_view_get_selected_pointer(
168                         GTK_TREE_VIEW(personEditDlg->view_attrib), ATTRIB_COL_PTR,
169                         &model, &sel, &iter);
170
171         if (attrib) {
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);
175                 attrib = NULL;
176         }
177
178         /* Position hilite bar */
179         if (!has_row) {
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)) {
185                         /* It exists. */
186                         has_row = TRUE;
187                 }
188         }
189
190         if (!has_row)
191                 gtk_tree_selection_select_iter(sel, &iter);
192
193         edit_person_attrib_cursor_changed(
194                         GTK_TREE_VIEW(personEditDlg->view_attrib), NULL);
195 }
196
197 static UserAttribute *edit_person_attrib_edit(gboolean *error, UserAttribute *attrib) {
198         UserAttribute *retVal = NULL;
199         gchar *sName, *sValue, *sName_, *sValue_;
200         gint index;
201
202         *error = TRUE;
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_);
208         g_free(sValue_);
209
210         if (sName && sValue) {
211                 debug_print("sname && svalue\n");
212                 if (attrib == NULL) {
213                         attrib = addritem_create_attribute();
214                 }
215                 addritem_attrib_set_name(attrib, sName);
216                 addritem_attrib_set_value(attrib, sValue);
217                 retVal = attrib;
218                 *error = FALSE;
219         }
220         else {
221                 edit_person_status_show(N_( "A Name and Value must be supplied." ));
222                 gtk_widget_grab_focus(personEditDlg->entry_atvalue);            
223         }
224
225         g_free(sName);
226         g_free(sValue);
227
228         return retVal;
229 }
230
231 static void edit_person_attrib_modify(gpointer data) {
232         gboolean errFlg = FALSE;
233         GtkTreeModel *model;
234         GtkTreeIter iter;
235         UserAttribute *attrib;
236
237         attrib = gtkut_tree_view_get_selected_pointer(
238                         GTK_TREE_VIEW(personEditDlg->view_attrib), ATTRIB_COL_PTR,
239                         &model, NULL, &iter);
240         if (attrib) {
241                 edit_person_attrib_edit(&errFlg, attrib);
242                 if (!errFlg) {
243                         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
244                                         ATTRIB_COL_NAME, attrib->name,
245                                         ATTRIB_COL_VALUE, attrib->value,
246                                         -1);
247                         edit_person_attrib_clear(NULL);
248                 }
249         }
250 }
251
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;
260
261         if (model != NULL) {
262                 /* No row selected, or list empty, add it as first row. */
263                 gtk_list_store_insert(GTK_LIST_STORE(model), &iter, 0);
264         } else {
265                 /* Add it after the currently selected row. */
266                 gtk_list_store_insert_after(GTK_LIST_STORE(model), &iter2,
267                                 &iter);
268                 iter = iter2;
269         }
270
271         /* Grab the values from text entries, and fill out the new row. */
272         attrib = edit_person_attrib_edit(&errFlg, NULL);
273         debug_print("after edit\n");
274         if (!errFlg) {
275                 debug_print("!errflg\n");
276                 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
277                                 ATTRIB_COL_NAME, attrib->name,
278                                 ATTRIB_COL_VALUE, attrib->value,
279                                 ATTRIB_COL_PTR, attrib,
280                                 -1);
281                 gtk_tree_selection_select_iter(sel, &iter);
282                 edit_person_attrib_clear(NULL);
283         }
284 }
285
286 static void edit_person_entry_att_changed (GtkWidget *entry, gpointer data)
287 {
288         GtkTreeModel *model = gtk_tree_view_get_model(
289                         GTK_TREE_VIEW(personEditDlg->view_attrib));
290         gboolean non_empty = (gtk_tree_model_iter_n_children(model, NULL) > 0);
291         const gchar *sName;
292         int index;
293
294         if (personEditDlg->read_only)
295                 return;
296
297         index = gtk_combo_box_get_active(GTK_COMBO_BOX(personEditDlg->entry_atname));
298         sName = ATTRIBUTE[index];
299         if (list_find_attribute(sName)) {
300                 gtk_widget_set_sensitive(personEditDlg->attrib_add,FALSE);
301                 gtk_widget_set_sensitive(personEditDlg->attrib_mod,non_empty);
302                 attrib_adding = FALSE;
303                 attrib_saving = non_empty;
304         } 
305         else {
306                 gtk_widget_set_sensitive(personEditDlg->attrib_add,TRUE);
307                 gtk_widget_set_sensitive(personEditDlg->attrib_mod,non_empty);
308                 attrib_adding = TRUE;
309                 attrib_saving = non_empty;
310         }
311 }
312
313 static gboolean edit_person_entry_att_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
314 {
315         if (event && (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter)) {
316                 if (attrib_saving)
317                         edit_person_attrib_modify(NULL);
318                 else if (attrib_adding)
319                         edit_person_attrib_add(NULL);
320         }
321         return FALSE;
322 }
323
324 void addressbook_edit_person_page_attrib_ldap(PersonEditDlg *dialog, gint pageNum, gchar *pageLbl) {
325         GtkWidget *combo_box;
326         GtkWidget *vbox;
327         GtkWidget *hbox;
328         GtkWidget *vboxl;
329         GtkWidget *vboxb;
330         GtkWidget *vbuttonbox;
331         GtkWidget *buttonDel;
332         GtkWidget *buttonMod;
333         GtkWidget *buttonAdd;
334
335         GtkWidget *table;
336         GtkWidget *label;
337         GtkWidget *scrollwin;
338         GtkWidget *view;
339         GtkWidget *entry_value;
340         gint top;
341         GtkListStore *store;
342         GtkTreeViewColumn *col;
343         GtkCellRenderer *rdr;
344         GtkTreeSelection *sel;
345
346         personEditDlg = dialog;
347
348         vbox = gtk_vbox_new(FALSE, 8);
349         gtk_widget_show(vbox);
350         gtk_container_add(GTK_CONTAINER(personEditDlg->notebook), vbox);
351         gtk_container_set_border_width(GTK_CONTAINER(vbox), BORDER_WIDTH);
352
353         label = gtk_label_new_with_mnemonic(pageLbl);
354         gtk_widget_show(label);
355         gtk_notebook_set_tab_label(
356                 GTK_NOTEBOOK(personEditDlg->notebook),
357                 gtk_notebook_get_nth_page(GTK_NOTEBOOK(personEditDlg->notebook), pageNum), label);
358
359         /* Split into two areas */
360         hbox = gtk_hbox_new(FALSE, 0);
361         gtk_container_add(GTK_CONTAINER(vbox), hbox);
362
363         /* Attribute list */
364         vboxl = gtk_vbox_new(FALSE, 4);
365         gtk_container_add(GTK_CONTAINER(hbox), vboxl);
366         gtk_container_set_border_width(GTK_CONTAINER(vboxl), 4);
367
368         scrollwin = gtk_scrolled_window_new(NULL, NULL);
369         gtk_container_add(GTK_CONTAINER(vboxl), scrollwin);
370         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin),
371                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
372
373         store = gtk_list_store_new(ATTRIB_N_COLS,
374                         G_TYPE_STRING, G_TYPE_STRING,
375                         G_TYPE_POINTER, -1);
376
377         view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
378         g_object_unref(store);
379         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), TRUE);
380         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
381         gtk_tree_selection_set_mode(sel, GTK_SELECTION_BROWSE);
382
383         rdr = gtk_cell_renderer_text_new();
384         col = gtk_tree_view_column_new_with_attributes(_("Name"), rdr,
385                         "markup", ATTRIB_COL_NAME, NULL);
386         gtk_tree_view_column_set_min_width(col, ATTRIB_COL_WIDTH_NAME);
387         gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
388
389         col = gtk_tree_view_column_new_with_attributes(_("Value"), rdr,
390                         "markup", ATTRIB_COL_VALUE, NULL);
391         gtk_tree_view_column_set_min_width(col, ATTRIB_COL_WIDTH_VALUE);
392         gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
393
394         gtk_container_add(GTK_CONTAINER(scrollwin), view);
395
396         /* Data entry area */
397         table = gtk_table_new(4, 2, FALSE);
398         gtk_box_pack_start(GTK_BOX(vboxl), table, FALSE, FALSE, 0);
399         gtk_container_set_border_width(GTK_CONTAINER(table), 4);
400         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
401         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
402
403         /* First row */
404         top = 0;
405         label = gtk_label_new(N_("Name"));
406         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
407         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
408
409         gchar **attribute = (gchar **) ATTRIBUTE;
410
411         combo_box = gtk_combo_box_text_new();
412
413         while (*attribute) {
414                 if (!strcmp(*attribute, "jpegPhoto")) {
415                         attribute++;
416                         continue;
417                 }
418                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo_box), *attribute++);
419         }
420         gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), 0);
421
422         gtk_table_attach(GTK_TABLE(table), combo_box, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
423
424         /* Next row */
425         ++top;
426         label = gtk_label_new(N_("Value"));
427         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
428         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
429
430         entry_value = gtk_entry_new();
431         gtk_table_attach(GTK_TABLE(table), entry_value, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
432
433         /* Button box */
434         vboxb = gtk_vbox_new(FALSE, 4);
435         gtk_box_pack_start(GTK_BOX(hbox), vboxb, FALSE, FALSE, 2);
436
437         vbuttonbox = gtk_vbutton_box_new();
438         gtk_button_box_set_layout(GTK_BUTTON_BOX(vbuttonbox), GTK_BUTTONBOX_START);
439         gtk_box_set_spacing(GTK_BOX(vbuttonbox), 8);
440         gtk_container_set_border_width(GTK_CONTAINER(vbuttonbox), 4);
441         gtk_container_add(GTK_CONTAINER(vboxb), vbuttonbox);
442
443         /* Buttons */
444         buttonDel = gtk_button_new_from_stock(GTK_STOCK_DELETE);
445         gtk_container_add(GTK_CONTAINER(vbuttonbox), buttonDel);
446
447         buttonMod = gtk_button_new_from_stock(GTK_STOCK_SAVE);
448         gtk_container_add(GTK_CONTAINER(vbuttonbox), buttonMod);
449
450         buttonAdd = gtk_button_new_from_stock(GTK_STOCK_ADD);
451         gtk_container_add(GTK_CONTAINER(vbuttonbox), buttonAdd);
452         
453         gtk_widget_set_sensitive(buttonDel,FALSE);
454         gtk_widget_set_sensitive(buttonMod,FALSE);
455         gtk_widget_set_sensitive(buttonAdd,FALSE);
456
457         gtk_widget_show_all(vbox);
458         
459         /* Event handlers */
460         g_signal_connect(G_OBJECT(view), "cursor-changed",
461                         G_CALLBACK(edit_person_attrib_cursor_changed), NULL);
462         g_signal_connect(G_OBJECT(buttonDel), "clicked",
463                           G_CALLBACK(edit_person_attrib_delete), NULL);
464         g_signal_connect(G_OBJECT(buttonMod), "clicked",
465                           G_CALLBACK(edit_person_attrib_modify), NULL);
466         g_signal_connect(G_OBJECT(buttonAdd), "clicked",
467                           G_CALLBACK(edit_person_attrib_add), NULL);
468         g_signal_connect(G_OBJECT(combo_box), "changed",
469                          G_CALLBACK(edit_person_entry_att_changed), NULL);
470         g_signal_connect(G_OBJECT(entry_value), "key_press_event",
471                          G_CALLBACK(edit_person_entry_att_pressed), NULL);
472         g_signal_connect(G_OBJECT(combo_box), "changed",
473                          G_CALLBACK(edit_person_combo_box_changed), view);
474
475         personEditDlg->view_attrib  = view;
476         personEditDlg->entry_atname  = combo_box;
477         personEditDlg->entry_atvalue = entry_value;
478         personEditDlg->attrib_add = buttonAdd;
479         personEditDlg->attrib_del = buttonDel;
480         personEditDlg->attrib_mod = buttonMod;
481 }
482
483 #endif  /* USE_LDAP */
484
485