2010-11-17 [colin] 3.7.6cvs65
[claws.git] / src / editaddress_other_attributes_ldap.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2009 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 #endif
23
24 #ifdef USE_LDAP
25
26 #include "defs.h"
27
28 #include "mgutils.h"
29 #include "addressbook.h"
30 #include "addressitem.h"
31 #include "addritem.h"
32 #include "addrbook.h"
33 #include "manage_window.h"
34 #include "gtkutils.h"
35 #include "codeconv.h"
36 #include "editaddress.h"
37 #include "editaddress_other_attributes_ldap.h"
38 #include "prefs_common.h"
39
40 #define ATTRIB_COL_NAME                 0
41 #define ATTRIB_COL_VALUE                1
42 #define ATTRIB_N_COLS                   2
43 #define EMAIL_N_COLS                    3
44 #define ATTRIB_COL_WIDTH_NAME   120
45 #define ATTRIB_COL_WIDTH_VALUE  180
46
47 PersonEditDlg *personEditDlg;
48 gboolean attrib_adding = FALSE, attrib_saving = FALSE;
49
50 int get_attribute_index(const gchar *string_literal) {
51         int i = 0;
52         /*int count = sizeof(ATTRIBUTE) / sizeof(*ATTRIBUTE);*/
53         const gchar **attribute = ATTRIBUTE;
54
55         cm_return_val_if_fail(string_literal != NULL, -1);
56         while (*attribute) {
57                 debug_print("Comparing %s to %s\n", *attribute, string_literal);
58                 if (strcmp(*attribute++, string_literal) == 0)
59                         return i;
60                 i++;
61         }
62         return -1;
63 }
64
65 static void edit_person_status_show(gchar *msg) {
66         if (personEditDlg->statusbar != NULL) {
67                 gtk_statusbar_pop(GTK_STATUSBAR(personEditDlg->statusbar), personEditDlg->status_cid);
68                 if(msg) {
69                         gtk_statusbar_push(GTK_STATUSBAR(personEditDlg->statusbar), personEditDlg->status_cid, msg);
70                 }
71         }
72 }
73
74 static void edit_person_attrib_clear(gpointer data) {
75         gtk_combo_box_set_active(GTK_COMBO_BOX(personEditDlg->entry_atname), 0);
76         gtk_entry_set_text(GTK_ENTRY(personEditDlg->entry_atvalue), "");
77 }
78
79 static gboolean list_find_attribute(const gchar *attr)
80 {
81         GtkCMCList *clist = GTK_CMCLIST(personEditDlg->clist_attrib);
82         UserAttribute *attrib;
83         gint row = 0;
84         while((attrib = gtk_cmclist_get_row_data(clist, row))) {
85                 if (!g_ascii_strcasecmp(attrib->name, attr)) {
86                         gtk_cmclist_select_row(clist, row, 0);
87                         return TRUE;
88                 }
89                 row++;
90         }
91         return FALSE;
92 }
93
94 /*
95 * Comparison using cell contents (text in first column). Used for sort
96 * address index widget.
97 */
98 static gint edit_person_attrib_compare_func(GtkCMCList *clist, gconstpointer ptr1, gconstpointer ptr2) {
99         GtkCMCell *cell1 = ((GtkCMCListRow *)ptr1)->cell;
100         GtkCMCell *cell2 = ((GtkCMCListRow *)ptr2)->cell;
101         gchar *name1 = NULL, *name2 = NULL;
102
103         if (cell1) name1 = cell1->u.text;
104         if (cell2) name2 = cell2->u.text;
105         if (!name1) return (name2 != NULL);
106         if (!name2) return -1;
107         return g_utf8_collate(name1, name2);
108 }
109
110 static void edit_person_combo_box_changed(GtkComboBox *opt_menu, gpointer data)
111 {
112         GtkCMCList *clist = GTK_CMCLIST(data);
113         gint row = personEditDlg->rowIndAttrib;
114         UserAttribute *attrib = gtk_cmclist_get_row_data(clist, row);
115         gint option = gtk_combo_box_get_active(opt_menu);
116         const gchar *str = attrib ? attrib->name:"";
117
118         cm_return_if_fail (option < ATTRIBUTE_SIZE);
119         /* A corresponding attribute in contact does not match selected option */ 
120         if (strcmp(ATTRIBUTE[option], str) != 0) {
121                 gtk_widget_set_sensitive(personEditDlg->attrib_add, TRUE);
122                 gtk_widget_set_sensitive(personEditDlg->attrib_mod, TRUE);
123                 gtk_widget_set_sensitive(personEditDlg->attrib_del, FALSE);
124                 gtk_entry_set_text(GTK_ENTRY(personEditDlg->entry_atvalue), "");
125                 gtk_widget_grab_focus(personEditDlg->entry_atvalue);
126                 edit_person_status_show(NULL);
127         }
128 }
129
130 static void edit_person_attrib_list_selected(GtkCMCList *clist, gint row, gint column, GdkEvent *event, gpointer data) {
131         UserAttribute *attrib = gtk_cmclist_get_row_data(clist, row);
132         if (attrib && !personEditDlg->read_only) {
133                 int index = get_attribute_index(attrib->name);
134                 if (index == -1)
135                         index = 0;
136
137                 gtk_combo_box_set_active(GTK_COMBO_BOX(personEditDlg->entry_atname), index);
138                 gtk_entry_set_text( GTK_ENTRY(personEditDlg->entry_atvalue), attrib->value );
139                 gtk_widget_set_sensitive(personEditDlg->attrib_del, TRUE);
140         }
141         else {
142                 /*g_printerr("Row: %d -> empty attribute\n", row);*/
143                 gtk_entry_set_text( GTK_ENTRY(personEditDlg->entry_atvalue), "");       
144                 gtk_widget_set_sensitive(personEditDlg->attrib_del, FALSE);
145         }
146         personEditDlg->rowIndAttrib = row;
147         edit_person_status_show(NULL);
148 }
149
150 static void edit_person_attrib_delete(gpointer data) {
151         GtkCMCList *clist = GTK_CMCLIST(personEditDlg->clist_attrib);
152         gint row = personEditDlg->rowIndAttrib;
153         UserAttribute *attrib = gtk_cmclist_get_row_data(clist, row);
154         edit_person_attrib_clear(NULL);
155         if (attrib) {
156                 /* Remove list entry */
157                 gtk_cmclist_remove(clist, row);
158                 addritem_free_attribute(attrib);
159                 attrib = NULL;
160         }
161
162         /* Position hilite bar */
163         attrib = gtk_cmclist_get_row_data(clist, row);
164         if (!attrib) {
165                 personEditDlg->rowIndAttrib = -1 + row;
166         } 
167         
168         if (!personEditDlg->read_only)
169                 gtk_widget_set_sensitive(personEditDlg->attrib_del, gtk_cmclist_get_row_data(clist, 0) != NULL);
170         
171         edit_person_status_show(NULL);
172 }
173
174 static UserAttribute *edit_person_attrib_edit(gboolean *error, UserAttribute *attrib) {
175         UserAttribute *retVal = NULL;
176         gchar *sName, *sValue, *sName_, *sValue_;
177         gint index;
178
179         *error = TRUE;
180         index = gtk_combo_box_get_active(GTK_COMBO_BOX(personEditDlg->entry_atname));
181         sName_ = (gchar *) ATTRIBUTE[index];
182         sValue_ = gtk_editable_get_chars(GTK_EDITABLE(personEditDlg->entry_atvalue), 0, -1);
183         sName = mgu_email_check_empty(sName_);
184         sValue = mgu_email_check_empty(sValue_);
185         g_free(sValue_);
186
187         if (sName && sValue) {
188                 if (attrib == NULL) {
189                         attrib = addritem_create_attribute();
190                 }
191                 addritem_attrib_set_name(attrib, sName);
192                 addritem_attrib_set_value(attrib, sValue);
193                 retVal = attrib;
194                 *error = FALSE;
195         }
196         else {
197                 edit_person_status_show(N_( "A Name and Value must be supplied." ));
198                 gtk_widget_grab_focus(personEditDlg->entry_atvalue);            
199         }
200
201         g_free(sName);
202         g_free(sValue);
203
204         return retVal;
205 }
206
207 static void edit_person_attrib_modify(gpointer data) {
208         gboolean errFlg = FALSE;
209         GtkCMCList *clist = GTK_CMCLIST(personEditDlg->clist_attrib);
210         gint row = personEditDlg->rowIndAttrib;
211         UserAttribute *attrib = gtk_cmclist_get_row_data(clist, row);
212         if (attrib) {
213                 edit_person_attrib_edit(&errFlg, attrib);
214                 if (!errFlg) {
215                         gtk_cmclist_set_text(clist, row, ATTRIB_COL_NAME, attrib->name);
216                         gtk_cmclist_set_text(clist, row, ATTRIB_COL_VALUE, attrib->value);
217                         edit_person_attrib_clear(NULL);
218                 }
219         }
220 }
221
222 static void edit_person_attrib_add(gpointer data) {
223         GtkCMCList *clist = GTK_CMCLIST(personEditDlg->clist_attrib);
224         gboolean errFlg = FALSE;
225         UserAttribute *attrib = NULL;
226         gint row = personEditDlg->rowIndAttrib;
227         if (gtk_cmclist_get_row_data(clist, row) == NULL) row = 0;
228
229         attrib = edit_person_attrib_edit(&errFlg, NULL);
230         if (!errFlg) {
231                 gchar *text[EMAIL_N_COLS];
232                 text[ATTRIB_COL_NAME] = attrib->name;
233                 text[ATTRIB_COL_VALUE] = attrib->value;
234
235                 row = gtk_cmclist_insert(clist, 1 + row, text);
236                 gtk_cmclist_set_row_data(clist, row, attrib);
237                 gtk_cmclist_select_row(clist, row, 0);
238                 edit_person_attrib_clear(NULL);
239         }
240 }
241
242 static void edit_person_entry_att_changed (GtkWidget *entry, gpointer data)
243 {
244         gboolean non_empty = gtk_cmclist_get_row_data(GTK_CMCLIST(personEditDlg->clist_attrib), 0) != NULL;
245         const gchar *sName;
246         int index;
247
248         if (personEditDlg->read_only)
249                 return;
250
251         index = gtk_combo_box_get_active(GTK_COMBO_BOX(personEditDlg->entry_atname));
252         sName = ATTRIBUTE[index];
253         if (list_find_attribute(sName)) {
254                 gtk_widget_set_sensitive(personEditDlg->attrib_add,FALSE);
255                 gtk_widget_set_sensitive(personEditDlg->attrib_mod,non_empty);
256                 attrib_adding = FALSE;
257                 attrib_saving = non_empty;
258         } 
259         else {
260                 gtk_widget_set_sensitive(personEditDlg->attrib_add,TRUE);
261                 gtk_widget_set_sensitive(personEditDlg->attrib_mod,non_empty);
262                 attrib_adding = TRUE;
263                 attrib_saving = non_empty;
264         }
265 }
266
267 static gboolean edit_person_entry_att_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
268 {
269         if (event && (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter)) {
270                 if (attrib_saving)
271                         edit_person_attrib_modify(NULL);
272                 else if (attrib_adding)
273                         edit_person_attrib_add(NULL);
274         }
275         return FALSE;
276 }
277
278 void addressbook_edit_person_page_attrib_ldap(PersonEditDlg *dialog, gint pageNum, gchar *pageLbl) {
279         GtkWidget *combo_box;
280         GtkWidget *vbox;
281         GtkWidget *hbox;
282         GtkWidget *vboxl;
283         GtkWidget *vboxb;
284         GtkWidget *vbuttonbox;
285         GtkWidget *buttonDel;
286         GtkWidget *buttonMod;
287         GtkWidget *buttonAdd;
288
289         GtkWidget *table;
290         GtkWidget *label;
291         GtkWidget *clist_swin;
292         GtkWidget *clist;
293         GtkWidget *entry_value;
294         gint top;
295
296         personEditDlg = dialog;
297
298         gchar *titles[ATTRIB_N_COLS];
299         gint i;
300
301         titles[ATTRIB_COL_NAME] = N_("Name");
302         titles[ATTRIB_COL_VALUE] = N_("Value");
303
304         vbox = gtk_vbox_new(FALSE, 8);
305         gtk_widget_show(vbox);
306         gtk_container_add(GTK_CONTAINER(personEditDlg->notebook), vbox);
307         gtk_container_set_border_width(GTK_CONTAINER(vbox), BORDER_WIDTH);
308
309         label = gtk_label_new_with_mnemonic(pageLbl);
310         gtk_widget_show(label);
311         gtk_notebook_set_tab_label(
312                 GTK_NOTEBOOK(personEditDlg->notebook),
313                 gtk_notebook_get_nth_page(GTK_NOTEBOOK(personEditDlg->notebook), pageNum), label);
314
315         /* Split into two areas */
316         hbox = gtk_hbox_new(FALSE, 0);
317         gtk_container_add(GTK_CONTAINER(vbox), hbox);
318
319         /* Attribute list */
320         vboxl = gtk_vbox_new(FALSE, 4);
321         gtk_container_add(GTK_CONTAINER(hbox), vboxl);
322         gtk_container_set_border_width(GTK_CONTAINER(vboxl), 4);
323
324         clist_swin = gtk_scrolled_window_new(NULL, NULL);
325         gtk_container_add(GTK_CONTAINER(vboxl), clist_swin);
326         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
327                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
328
329         clist = gtk_cmclist_new_with_titles(ATTRIB_N_COLS, titles);
330         gtk_container_add(GTK_CONTAINER(clist_swin), clist);
331         gtk_cmclist_set_selection_mode(GTK_CMCLIST(clist), GTK_SELECTION_BROWSE);
332         gtk_cmclist_set_column_width(GTK_CMCLIST(clist), ATTRIB_COL_NAME, ATTRIB_COL_WIDTH_NAME);
333         gtk_cmclist_set_column_width(GTK_CMCLIST(clist), ATTRIB_COL_VALUE, ATTRIB_COL_WIDTH_VALUE);
334         gtk_cmclist_set_compare_func(GTK_CMCLIST(clist), edit_person_attrib_compare_func);
335         gtk_cmclist_set_auto_sort(GTK_CMCLIST(clist), TRUE);
336
337         for (i = 0; i < ATTRIB_N_COLS; i++)
338                 gtkut_widget_set_can_focus(GTK_CMCLIST(clist)->column[i].button, FALSE);
339
340         /* Data entry area */
341         table = gtk_table_new(4, 2, FALSE);
342         gtk_box_pack_start(GTK_BOX(vboxl), table, FALSE, FALSE, 0);
343         gtk_container_set_border_width(GTK_CONTAINER(table), 4);
344         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
345         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
346
347         /* First row */
348         top = 0;
349         label = gtk_label_new(N_("Name"));
350         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
351         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
352
353         gchar **attribute = (gchar **) ATTRIBUTE;
354
355         combo_box = gtk_combo_box_new_text();
356
357         while (*attribute) {
358                 if (!strcmp(*attribute, "jpegPhoto")) {
359                         attribute++;
360                         continue;
361                 }
362                 gtk_combo_box_append_text(GTK_COMBO_BOX(combo_box), *attribute++);
363         }
364         gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), 0);
365
366         gtk_table_attach(GTK_TABLE(table), combo_box, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
367
368         /* Next row */
369         ++top;
370         label = gtk_label_new(N_("Value"));
371         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
372         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
373
374         entry_value = gtk_entry_new();
375         gtk_table_attach(GTK_TABLE(table), entry_value, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
376
377         /* Button box */
378         vboxb = gtk_vbox_new(FALSE, 4);
379         gtk_box_pack_start(GTK_BOX(hbox), vboxb, FALSE, FALSE, 2);
380
381         vbuttonbox = gtk_vbutton_box_new();
382         gtk_button_box_set_layout(GTK_BUTTON_BOX(vbuttonbox), GTK_BUTTONBOX_START);
383         gtk_box_set_spacing(GTK_BOX(vbuttonbox), 8);
384         gtk_container_set_border_width(GTK_CONTAINER(vbuttonbox), 4);
385         gtk_container_add(GTK_CONTAINER(vboxb), vbuttonbox);
386
387         /* Buttons */
388         buttonDel = gtk_button_new_from_stock(GTK_STOCK_DELETE);
389         gtk_container_add(GTK_CONTAINER(vbuttonbox), buttonDel);
390
391         buttonMod = gtk_button_new_from_stock(GTK_STOCK_SAVE);
392         gtk_container_add(GTK_CONTAINER(vbuttonbox), buttonMod);
393
394         buttonAdd = gtk_button_new_from_stock(GTK_STOCK_ADD);
395         gtk_container_add(GTK_CONTAINER(vbuttonbox), buttonAdd);
396         
397         gtk_widget_set_sensitive(buttonDel,FALSE);
398         gtk_widget_set_sensitive(buttonMod,FALSE);
399         gtk_widget_set_sensitive(buttonAdd,FALSE);
400
401         gtk_widget_show_all(vbox);
402         
403         /* Event handlers */
404         g_signal_connect(G_OBJECT(clist), "select_row",
405                           G_CALLBACK( edit_person_attrib_list_selected), NULL);
406         g_signal_connect(G_OBJECT(buttonDel), "clicked",
407                           G_CALLBACK(edit_person_attrib_delete), NULL);
408         g_signal_connect(G_OBJECT(buttonMod), "clicked",
409                           G_CALLBACK(edit_person_attrib_modify), NULL);
410         g_signal_connect(G_OBJECT(buttonAdd), "clicked",
411                           G_CALLBACK(edit_person_attrib_add), NULL);
412         g_signal_connect(G_OBJECT(combo_box), "changed",
413                          G_CALLBACK(edit_person_entry_att_changed), NULL);
414         g_signal_connect(G_OBJECT(entry_value), "key_press_event",
415                          G_CALLBACK(edit_person_entry_att_pressed), NULL);
416         g_signal_connect(G_OBJECT(combo_box), "changed",
417                          G_CALLBACK(edit_person_combo_box_changed), clist);
418
419         personEditDlg->clist_attrib  = clist;
420         personEditDlg->entry_atname  = combo_box;
421         personEditDlg->entry_atvalue = entry_value;
422         personEditDlg->attrib_add = buttonAdd;
423         personEditDlg->attrib_del = buttonDel;
424         personEditDlg->attrib_mod = buttonMod;
425 }
426
427 #endif  /* USE_LDAP */
428
429