2007-08-08 [wwp] 2.10.0cvs104
[claws.git] / src / editaddress_other_attributes_ldap.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 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         g_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_option_menu_set_history(GTK_OPTION_MENU(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         GtkCList *clist = GTK_CLIST(personEditDlg->clist_attrib);
82         UserAttribute *attrib;
83         gint row = 0;
84         while((attrib = gtk_clist_get_row_data(clist, row))) {
85                 if (!g_ascii_strcasecmp(attrib->name, attr)) {
86                         gtk_clist_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(GtkCList *clist, gconstpointer ptr1, gconstpointer ptr2) {
99         GtkCell *cell1 = ((GtkCListRow *)ptr1)->cell;
100         GtkCell *cell2 = ((GtkCListRow *)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_option_menu_changed(GtkOptionMenu *opt_menu, gpointer data) {
111         GtkCList *clist = GTK_CLIST(data);
112         gint row = personEditDlg->rowIndAttrib;
113         UserAttribute *attrib = gtk_clist_get_row_data(clist, row);
114         gint option = gtk_option_menu_get_history(opt_menu);
115         const gchar *str = attrib ? attrib->name:"";
116
117         g_return_if_fail (option < ATTRIBUTE_SIZE);
118         /* A corresponding attribute in contact does not match selected option */ 
119         if (strcmp(ATTRIBUTE[option], str) != 0) {
120                 gtk_widget_set_sensitive(personEditDlg->attrib_add, TRUE);
121                 gtk_widget_set_sensitive(personEditDlg->attrib_mod, TRUE);
122                 gtk_widget_set_sensitive(personEditDlg->attrib_del, FALSE);
123                 gtk_entry_set_text(GTK_ENTRY(personEditDlg->entry_atvalue), "");
124                 gtk_widget_grab_focus(personEditDlg->entry_atvalue);
125                 edit_person_status_show(NULL);
126         }
127 }
128
129 static void edit_person_attrib_list_selected(GtkCList *clist, gint row, gint column, GdkEvent *event, gpointer data) {
130         UserAttribute *attrib = gtk_clist_get_row_data(clist, row);
131         if (attrib && !personEditDlg->read_only) {
132                 int index = get_attribute_index(attrib->name);
133                 /*fprintf(stderr, "Row: %d ->  %s = %s\n", index, attrib->name, attrib->value);*/
134                 if (index == -1)
135                         index = 0;
136                 gtk_option_menu_set_history(GTK_OPTION_MENU(personEditDlg->entry_atname), index);
137                 gtk_entry_set_text( GTK_ENTRY(personEditDlg->entry_atvalue), attrib->value );
138                 gtk_widget_set_sensitive(personEditDlg->attrib_del, TRUE);
139         }
140         else {
141                 /*fprintf(stderr, "Row: %d -> empty attribute\n", row);*/
142                 gtk_entry_set_text( GTK_ENTRY(personEditDlg->entry_atvalue), "");       
143                 gtk_widget_set_sensitive(personEditDlg->attrib_del, FALSE);
144         }
145         personEditDlg->rowIndAttrib = row;
146         edit_person_status_show(NULL);
147 }
148
149 static void edit_person_attrib_delete(gpointer data) {
150         GtkCList *clist = GTK_CLIST(personEditDlg->clist_attrib);
151         gint row = personEditDlg->rowIndAttrib;
152         UserAttribute *attrib = gtk_clist_get_row_data(clist, row);
153         edit_person_attrib_clear(NULL);
154         if (attrib) {
155                 /* Remove list entry */
156                 gtk_clist_remove(clist, row);
157                 addritem_free_attribute(attrib);
158                 attrib = NULL;
159         }
160
161         /* Position hilite bar */
162         attrib = gtk_clist_get_row_data(clist, row);
163         if (!attrib) {
164                 personEditDlg->rowIndAttrib = -1 + row;
165         } 
166         
167         if (!personEditDlg->read_only)
168                 gtk_widget_set_sensitive(personEditDlg->attrib_del, gtk_clist_get_row_data(clist, 0) != NULL);
169         
170         edit_person_status_show(NULL);
171 }
172
173 static UserAttribute *edit_person_attrib_edit(gboolean *error, UserAttribute *attrib) {
174         UserAttribute *retVal = NULL;
175         gchar *sName, *sValue, *sName_, *sValue_;
176         gint index;
177
178         *error = TRUE;
179         index = gtk_option_menu_get_history(GTK_OPTION_MENU(personEditDlg->entry_atname));
180         sName_ = (gchar *) ATTRIBUTE[index];
181         sValue_ = gtk_editable_get_chars(GTK_EDITABLE(personEditDlg->entry_atvalue), 0, -1);
182         sName = mgu_email_check_empty(sName_);
183         sValue = mgu_email_check_empty(sValue_);
184         g_free(sValue_);
185
186         if (sName && sValue) {
187                 if (attrib == NULL) {
188                         attrib = addritem_create_attribute();
189                 }
190                 addritem_attrib_set_name(attrib, sName);
191                 addritem_attrib_set_value(attrib, sValue);
192                 retVal = attrib;
193                 *error = FALSE;
194         }
195         else {
196                 edit_person_status_show(N_( "A Name and Value must be supplied." ));
197                 gtk_widget_grab_focus(personEditDlg->entry_atvalue);            
198         }
199
200         g_free(sName);
201         g_free(sValue);
202
203         return retVal;
204 }
205
206 static void edit_person_attrib_modify(gpointer data) {
207         gboolean errFlg = FALSE;
208         GtkCList *clist = GTK_CLIST(personEditDlg->clist_attrib);
209         gint row = personEditDlg->rowIndAttrib;
210         UserAttribute *attrib = gtk_clist_get_row_data(clist, row);
211         if (attrib) {
212                 edit_person_attrib_edit(&errFlg, attrib);
213                 if (!errFlg) {
214                         gtk_clist_set_text(clist, row, ATTRIB_COL_NAME, attrib->name);
215                         gtk_clist_set_text(clist, row, ATTRIB_COL_VALUE, attrib->value);
216                         edit_person_attrib_clear(NULL);
217                 }
218         }
219 }
220
221 static void edit_person_attrib_add(gpointer data) {
222         GtkCList *clist = GTK_CLIST(personEditDlg->clist_attrib);
223         gboolean errFlg = FALSE;
224         UserAttribute *attrib = NULL;
225         gint row = personEditDlg->rowIndAttrib;
226         if (gtk_clist_get_row_data(clist, row) == NULL) row = 0;
227
228         attrib = edit_person_attrib_edit(&errFlg, NULL);
229         if (!errFlg) {
230                 gchar *text[EMAIL_N_COLS];
231                 text[ATTRIB_COL_NAME] = attrib->name;
232                 text[ATTRIB_COL_VALUE] = attrib->value;
233
234                 row = gtk_clist_insert(clist, 1 + row, text);
235                 gtk_clist_set_row_data(clist, row, attrib);
236                 gtk_clist_select_row(clist, row, 0);
237                 edit_person_attrib_clear(NULL);
238         }
239 }
240
241 static void edit_person_entry_att_changed (GtkWidget *entry, gpointer data)
242 {
243         gboolean non_empty = gtk_clist_get_row_data(GTK_CLIST(personEditDlg->clist_attrib), 0) != NULL;
244         const gchar *sName;
245         int index;
246
247         if (personEditDlg->read_only)
248                 return;
249
250         index = gtk_option_menu_get_history(GTK_OPTION_MENU(personEditDlg->entry_atname));
251         sName = ATTRIBUTE[index];
252         if (list_find_attribute(sName)) {
253                 gtk_widget_set_sensitive(personEditDlg->attrib_add,FALSE);
254                 gtk_widget_set_sensitive(personEditDlg->attrib_mod,non_empty);
255                 attrib_adding = FALSE;
256                 attrib_saving = non_empty;
257         } 
258         else {
259                 gtk_widget_set_sensitive(personEditDlg->attrib_add,TRUE);
260                 gtk_widget_set_sensitive(personEditDlg->attrib_mod,non_empty);
261                 attrib_adding = TRUE;
262                 attrib_saving = non_empty;
263         }
264 }
265
266 static gboolean edit_person_entry_att_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
267 {
268         if (event && (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter)) {
269                 if (attrib_saving)
270                         edit_person_attrib_modify(NULL);
271                 else if (attrib_adding)
272                         edit_person_attrib_add(NULL);
273         }
274         return FALSE;
275 }
276
277 void addressbook_edit_person_page_attrib_ldap(PersonEditDlg *dialog, gint pageNum, gchar *pageLbl) {
278         GtkWidget *option_menu;
279         GtkMenu *names_menu;
280
281         GtkWidget *vbox;
282         GtkWidget *hbox;
283         GtkWidget *vboxl;
284         GtkWidget *vboxb;
285         GtkWidget *vbuttonbox;
286         GtkWidget *buttonDel;
287         GtkWidget *buttonMod;
288         GtkWidget *buttonAdd;
289
290         GtkWidget *table;
291         GtkWidget *label;
292         GtkWidget *clist_swin;
293         GtkWidget *clist;
294         GtkWidget *entry_value;
295         gint top;
296
297         personEditDlg = dialog;
298
299         gchar *titles[ATTRIB_N_COLS];
300         gint i;
301
302         titles[ATTRIB_COL_NAME] = N_("Name");
303         titles[ATTRIB_COL_VALUE] = N_("Value");
304
305         vbox = gtk_vbox_new(FALSE, 8);
306         gtk_widget_show(vbox);
307         gtk_container_add(GTK_CONTAINER(personEditDlg->notebook), vbox);
308         gtk_container_set_border_width(GTK_CONTAINER(vbox), BORDER_WIDTH);
309
310         label = gtk_label_new_with_mnemonic(pageLbl);
311         gtk_widget_show(label);
312         gtk_notebook_set_tab_label(
313                 GTK_NOTEBOOK(personEditDlg->notebook),
314                 gtk_notebook_get_nth_page(GTK_NOTEBOOK(personEditDlg->notebook), pageNum), label);
315
316         /* Split into two areas */
317         hbox = gtk_hbox_new(FALSE, 0);
318         gtk_container_add(GTK_CONTAINER(vbox), hbox);
319
320         /* Attribute list */
321         vboxl = gtk_vbox_new(FALSE, 4);
322         gtk_container_add(GTK_CONTAINER(hbox), vboxl);
323         gtk_container_set_border_width(GTK_CONTAINER(vboxl), 4);
324
325         clist_swin = gtk_scrolled_window_new(NULL, NULL);
326         gtk_container_add(GTK_CONTAINER(vboxl), clist_swin);
327         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
328                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
329
330         clist = gtk_clist_new_with_titles(ATTRIB_N_COLS, titles);
331         gtk_container_add(GTK_CONTAINER(clist_swin), clist);
332         gtk_clist_set_selection_mode(GTK_CLIST(clist), GTK_SELECTION_BROWSE);
333         gtk_clist_set_column_width(GTK_CLIST(clist), ATTRIB_COL_NAME, ATTRIB_COL_WIDTH_NAME);
334         gtk_clist_set_column_width(GTK_CLIST(clist), ATTRIB_COL_VALUE, ATTRIB_COL_WIDTH_VALUE);
335         gtk_clist_set_compare_func(GTK_CLIST(clist), edit_person_attrib_compare_func);
336         gtk_clist_set_auto_sort(GTK_CLIST(clist), TRUE);
337
338         for (i = 0; i < ATTRIB_N_COLS; i++)
339                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist)->column[i].button, GTK_CAN_FOCUS);
340
341         /* Data entry area */
342         table = gtk_table_new(4, 2, FALSE);
343         gtk_box_pack_start(GTK_BOX(vboxl), table, FALSE, FALSE, 0);
344         gtk_container_set_border_width(GTK_CONTAINER(table), 4);
345         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
346         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
347
348         /* First row */
349         top = 0;
350         label = gtk_label_new(N_("Name"));
351         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
352         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
353
354         names_menu = g_object_new(GTK_TYPE_MENU, NULL);
355         gchar **attribute = (gchar **) ATTRIBUTE;
356
357         while (*attribute) {
358                 gtk_menu_shell_append(GTK_MENU_SHELL(names_menu),
359                         gtk_menu_item_new_with_label(*attribute++));
360         }
361         option_menu = GTK_WIDGET(g_object_new(GTK_TYPE_OPTION_MENU, "menu", names_menu, NULL));
362         gtk_option_menu_set_history(GTK_OPTION_MENU(option_menu), 0);
363
364         gtk_table_attach(GTK_TABLE(table), option_menu, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
365
366         /* Next row */
367         ++top;
368         label = gtk_label_new(N_("Value"));
369         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
370         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
371
372         entry_value = gtk_entry_new();
373         gtk_table_attach(GTK_TABLE(table), entry_value, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
374
375         /* Button box */
376         vboxb = gtk_vbox_new(FALSE, 4);
377         gtk_box_pack_start(GTK_BOX(hbox), vboxb, FALSE, FALSE, 2);
378
379         vbuttonbox = gtk_vbutton_box_new();
380         gtk_button_box_set_layout(GTK_BUTTON_BOX(vbuttonbox), GTK_BUTTONBOX_START);
381         gtk_box_set_spacing(GTK_BOX(vbuttonbox), 8);
382         gtk_container_set_border_width(GTK_CONTAINER(vbuttonbox), 4);
383         gtk_container_add(GTK_CONTAINER(vboxb), vbuttonbox);
384
385         /* Buttons */
386         buttonDel = gtk_button_new_from_stock(GTK_STOCK_DELETE);
387         gtk_container_add(GTK_CONTAINER(vbuttonbox), buttonDel);
388
389         buttonMod = gtk_button_new_from_stock(GTK_STOCK_SAVE);
390         gtk_container_add(GTK_CONTAINER(vbuttonbox), buttonMod);
391
392         buttonAdd = gtk_button_new_from_stock(GTK_STOCK_ADD);
393         gtk_container_add(GTK_CONTAINER(vbuttonbox), buttonAdd);
394         
395         gtk_widget_set_sensitive(buttonDel,FALSE);
396         gtk_widget_set_sensitive(buttonMod,FALSE);
397         gtk_widget_set_sensitive(buttonAdd,FALSE);
398
399         gtk_widget_show_all(vbox);
400         
401         /* Event handlers */
402         g_signal_connect(G_OBJECT(clist), "select_row",
403                           G_CALLBACK( edit_person_attrib_list_selected), NULL);
404         g_signal_connect(G_OBJECT(buttonDel), "clicked",
405                           G_CALLBACK(edit_person_attrib_delete), NULL);
406         g_signal_connect(G_OBJECT(buttonMod), "clicked",
407                           G_CALLBACK(edit_person_attrib_modify), NULL);
408         g_signal_connect(G_OBJECT(buttonAdd), "clicked",
409                           G_CALLBACK(edit_person_attrib_add), NULL);
410         g_signal_connect(G_OBJECT(option_menu), "changed",
411                          G_CALLBACK(edit_person_entry_att_changed), NULL);
412         g_signal_connect(G_OBJECT(entry_value), "key_press_event",
413                          G_CALLBACK(edit_person_entry_att_pressed), NULL);
414         g_signal_connect(G_OBJECT(option_menu), "changed",
415                          G_CALLBACK(edit_person_option_menu_changed), clist);
416
417         personEditDlg->clist_attrib  = clist;
418         personEditDlg->entry_atname  = option_menu;
419         personEditDlg->entry_atvalue = entry_value;
420         personEditDlg->attrib_add = buttonAdd;
421         personEditDlg->attrib_del = buttonDel;
422         personEditDlg->attrib_mod = buttonMod;
423 }
424
425 #endif  /* USE_LDAP */
426
427