addritem+person_get_picture now returns something useful: a copy
[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, *prev_attrib;
260
261         attrib = edit_person_attrib_edit(&errFlg, NULL);
262         if (attrib == NULL) /* input for new attribute is not valid */
263                 return;
264
265         prev_attrib = gtkut_tree_view_get_selected_pointer(
266                         GTK_TREE_VIEW(personEditDlg->view_attrib), ATTRIB_COL_PTR,
267                         &model, &sel, &iter);
268
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);
272         } else {
273                 /* Add it after the currently selected row. */
274                 gtk_list_store_insert_after(GTK_LIST_STORE(model), &iter2,
275                                 &iter);
276                 iter = iter2;
277         }
278
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,
284                         -1);
285         gtk_tree_selection_select_iter(sel, &iter);
286         edit_person_attrib_clear(NULL);
287 }
288
289 static void edit_person_entry_att_changed (GtkWidget *entry, gpointer data)
290 {
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);
294         const gchar *sName;
295         int index;
296
297         if (personEditDlg->read_only)
298                 return;
299
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;
307         } 
308         else {
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;
313         }
314 }
315
316 static gboolean edit_person_entry_att_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
317 {
318         if (event && (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter)) {
319                 if (attrib_saving)
320                         edit_person_attrib_modify(NULL);
321                 else if (attrib_adding)
322                         edit_person_attrib_add(NULL);
323         }
324         return FALSE;
325 }
326
327 void addressbook_edit_person_page_attrib_ldap(PersonEditDlg *dialog, gint pageNum, gchar *pageLbl) {
328         GtkWidget *combo_box;
329         GtkWidget *vbox;
330         GtkWidget *hbox;
331         GtkWidget *vboxl;
332         GtkWidget *vboxb;
333         GtkWidget *vbuttonbox;
334         GtkWidget *buttonDel;
335         GtkWidget *buttonMod;
336         GtkWidget *buttonAdd;
337
338         GtkWidget *table;
339         GtkWidget *label;
340         GtkWidget *scrollwin;
341         GtkWidget *view;
342         GtkWidget *entry_value;
343         gint top;
344         GtkListStore *store;
345         GtkTreeViewColumn *col;
346         GtkCellRenderer *rdr;
347         GtkTreeSelection *sel;
348
349         personEditDlg = dialog;
350
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);
355
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);
361
362         /* Split into two areas */
363         hbox = gtk_hbox_new(FALSE, 0);
364         gtk_container_add(GTK_CONTAINER(vbox), hbox);
365
366         /* Attribute list */
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);
370
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);
375
376         store = gtk_list_store_new(ATTRIB_N_COLS,
377                         G_TYPE_STRING, G_TYPE_STRING,
378                         G_TYPE_POINTER, -1);
379
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);
385
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);
391
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);
396
397         gtk_container_add(GTK_CONTAINER(scrollwin), view);
398
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);
405
406         /* First row */
407         top = 0;
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);
411
412         gchar **attribute = (gchar **) ATTRIBUTE;
413
414         combo_box = gtk_combo_box_text_new();
415
416         while (*attribute) {
417                 if (!strcmp(*attribute, "jpegPhoto")) {
418                         attribute++;
419                         continue;
420                 }
421                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo_box), *attribute++);
422         }
423         gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), 0);
424
425         gtk_table_attach(GTK_TABLE(table), combo_box, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
426
427         /* Next row */
428         ++top;
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);
432
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);
435
436         /* Button box */
437         vboxb = gtk_vbox_new(FALSE, 4);
438         gtk_box_pack_start(GTK_BOX(hbox), vboxb, FALSE, FALSE, 2);
439
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);
445
446         /* Buttons */
447         buttonDel = gtk_button_new_from_stock(GTK_STOCK_DELETE);
448         gtk_container_add(GTK_CONTAINER(vbuttonbox), buttonDel);
449
450         buttonMod = gtk_button_new_from_stock(GTK_STOCK_SAVE);
451         gtk_container_add(GTK_CONTAINER(vbuttonbox), buttonMod);
452
453         buttonAdd = gtk_button_new_from_stock(GTK_STOCK_ADD);
454         gtk_container_add(GTK_CONTAINER(vbuttonbox), buttonAdd);
455         
456         gtk_widget_set_sensitive(buttonDel,FALSE);
457         gtk_widget_set_sensitive(buttonMod,FALSE);
458         gtk_widget_set_sensitive(buttonAdd,FALSE);
459
460         gtk_widget_show_all(vbox);
461         
462         /* Event handlers */
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);
477
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;
484 }
485
486 #endif  /* USE_LDAP */
487
488