2013-01-09 [mir] 3.9.0cvs53
[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_NAME                 0
42 #define ATTRIB_COL_VALUE                1
43 #define ATTRIB_N_COLS                   2
44 #define EMAIL_N_COLS                    3
45 #define ATTRIB_COL_WIDTH_NAME   120
46 #define ATTRIB_COL_WIDTH_VALUE  180
47
48 PersonEditDlg *personEditDlg;
49 gboolean attrib_adding = FALSE, attrib_saving = FALSE;
50
51 int get_attribute_index(const gchar *string_literal) {
52         int i = 0;
53         /*int count = sizeof(ATTRIBUTE) / sizeof(*ATTRIBUTE);*/
54         const gchar **attribute = ATTRIBUTE;
55
56         cm_return_val_if_fail(string_literal != NULL, -1);
57         while (*attribute) {
58                 debug_print("Comparing %s to %s\n", *attribute, string_literal);
59                 if (strcmp(*attribute++, string_literal) == 0)
60                         return i;
61                 i++;
62         }
63         return -1;
64 }
65
66 static void edit_person_status_show(gchar *msg) {
67         if (personEditDlg->statusbar != NULL) {
68                 gtk_statusbar_pop(GTK_STATUSBAR(personEditDlg->statusbar), personEditDlg->status_cid);
69                 if(msg) {
70                         gtk_statusbar_push(GTK_STATUSBAR(personEditDlg->statusbar), personEditDlg->status_cid, msg);
71                 }
72         }
73 }
74
75 static void edit_person_attrib_clear(gpointer data) {
76         gtk_combo_box_set_active(GTK_COMBO_BOX(personEditDlg->entry_atname), 0);
77         gtk_entry_set_text(GTK_ENTRY(personEditDlg->entry_atvalue), "");
78 }
79
80 static gboolean list_find_attribute(const gchar *attr)
81 {
82         GtkCMCList *clist = GTK_CMCLIST(personEditDlg->clist_attrib);
83         UserAttribute *attrib;
84         gint row = 0;
85         while((attrib = gtk_cmclist_get_row_data(clist, row))) {
86                 if (!g_ascii_strcasecmp(attrib->name, attr)) {
87                         gtk_cmclist_select_row(clist, row, 0);
88                         return TRUE;
89                 }
90                 row++;
91         }
92         return FALSE;
93 }
94
95 /*
96 * Comparison using cell contents (text in first column). Used for sort
97 * address index widget.
98 */
99 static gint edit_person_attrib_compare_func(GtkCMCList *clist, gconstpointer ptr1, gconstpointer ptr2) {
100         GtkCMCell *cell1 = ((GtkCMCListRow *)ptr1)->cell;
101         GtkCMCell *cell2 = ((GtkCMCListRow *)ptr2)->cell;
102         gchar *name1 = NULL, *name2 = NULL;
103
104         if (cell1) name1 = cell1->u.text;
105         if (cell2) name2 = cell2->u.text;
106         if (!name1) return (name2 != NULL);
107         if (!name2) return -1;
108         return g_utf8_collate(name1, name2);
109 }
110
111 static void edit_person_combo_box_changed(GtkComboBox *opt_menu, gpointer data)
112 {
113         GtkCMCList *clist = GTK_CMCLIST(data);
114         gint row = personEditDlg->rowIndAttrib;
115         UserAttribute *attrib = gtk_cmclist_get_row_data(clist, row);
116         gint option = gtk_combo_box_get_active(opt_menu);
117         const gchar *str = attrib ? attrib->name:"";
118
119         cm_return_if_fail (option < ATTRIBUTE_SIZE);
120         /* A corresponding attribute in contact does not match selected option */ 
121         if (strcmp(ATTRIBUTE[option], str) != 0) {
122                 gtk_widget_set_sensitive(personEditDlg->attrib_add, TRUE);
123                 gtk_widget_set_sensitive(personEditDlg->attrib_mod, TRUE);
124                 gtk_widget_set_sensitive(personEditDlg->attrib_del, FALSE);
125                 gtk_entry_set_text(GTK_ENTRY(personEditDlg->entry_atvalue), "");
126                 gtk_widget_grab_focus(personEditDlg->entry_atvalue);
127                 edit_person_status_show(NULL);
128         }
129 }
130
131 static void edit_person_attrib_list_selected(GtkCMCList *clist, gint row, gint column, GdkEvent *event, gpointer data) {
132         UserAttribute *attrib = gtk_cmclist_get_row_data(clist, row);
133         if (attrib && !personEditDlg->read_only) {
134                 int index = get_attribute_index(attrib->name);
135                 if (index == -1)
136                         index = 0;
137
138                 gtk_combo_box_set_active(GTK_COMBO_BOX(personEditDlg->entry_atname), index);
139                 gtk_entry_set_text( GTK_ENTRY(personEditDlg->entry_atvalue), attrib->value );
140                 gtk_widget_set_sensitive(personEditDlg->attrib_del, TRUE);
141         }
142         else {
143                 /*g_printerr("Row: %d -> empty attribute\n", row);*/
144                 gtk_entry_set_text( GTK_ENTRY(personEditDlg->entry_atvalue), "");       
145                 gtk_widget_set_sensitive(personEditDlg->attrib_del, FALSE);
146         }
147         personEditDlg->rowIndAttrib = row;
148         edit_person_status_show(NULL);
149 }
150
151 static void edit_person_attrib_delete(gpointer data) {
152         GtkCMCList *clist = GTK_CMCLIST(personEditDlg->clist_attrib);
153         gint row = personEditDlg->rowIndAttrib;
154         UserAttribute *attrib = gtk_cmclist_get_row_data(clist, row);
155         edit_person_attrib_clear(NULL);
156         if (attrib) {
157                 /* Remove list entry */
158                 gtk_cmclist_remove(clist, row);
159                 addritem_free_attribute(attrib);
160                 attrib = NULL;
161         }
162
163         /* Position hilite bar */
164         attrib = gtk_cmclist_get_row_data(clist, row);
165         if (!attrib) {
166                 personEditDlg->rowIndAttrib = -1 + row;
167         } 
168         
169         if (!personEditDlg->read_only)
170                 gtk_widget_set_sensitive(personEditDlg->attrib_del, gtk_cmclist_get_row_data(clist, 0) != NULL);
171         
172         edit_person_status_show(NULL);
173 }
174
175 static UserAttribute *edit_person_attrib_edit(gboolean *error, UserAttribute *attrib) {
176         UserAttribute *retVal = NULL;
177         gchar *sName, *sValue, *sName_, *sValue_;
178         gint index;
179
180         *error = TRUE;
181         index = gtk_combo_box_get_active(GTK_COMBO_BOX(personEditDlg->entry_atname));
182         sName_ = (gchar *) ATTRIBUTE[index];
183         sValue_ = gtk_editable_get_chars(GTK_EDITABLE(personEditDlg->entry_atvalue), 0, -1);
184         sName = mgu_email_check_empty(sName_);
185         sValue = mgu_email_check_empty(sValue_);
186         g_free(sValue_);
187
188         if (sName && sValue) {
189                 if (attrib == NULL) {
190                         attrib = addritem_create_attribute();
191                 }
192                 addritem_attrib_set_name(attrib, sName);
193                 addritem_attrib_set_value(attrib, sValue);
194                 retVal = attrib;
195                 *error = FALSE;
196         }
197         else {
198                 edit_person_status_show(N_( "A Name and Value must be supplied." ));
199                 gtk_widget_grab_focus(personEditDlg->entry_atvalue);            
200         }
201
202         g_free(sName);
203         g_free(sValue);
204
205         return retVal;
206 }
207
208 static void edit_person_attrib_modify(gpointer data) {
209         gboolean errFlg = FALSE;
210         GtkCMCList *clist = GTK_CMCLIST(personEditDlg->clist_attrib);
211         gint row = personEditDlg->rowIndAttrib;
212         UserAttribute *attrib = gtk_cmclist_get_row_data(clist, row);
213         if (attrib) {
214                 edit_person_attrib_edit(&errFlg, attrib);
215                 if (!errFlg) {
216                         gtk_cmclist_set_text(clist, row, ATTRIB_COL_NAME, attrib->name);
217                         gtk_cmclist_set_text(clist, row, ATTRIB_COL_VALUE, attrib->value);
218                         edit_person_attrib_clear(NULL);
219                 }
220         }
221 }
222
223 static void edit_person_attrib_add(gpointer data) {
224         GtkCMCList *clist = GTK_CMCLIST(personEditDlg->clist_attrib);
225         gboolean errFlg = FALSE;
226         UserAttribute *attrib = NULL;
227         gint row = personEditDlg->rowIndAttrib;
228         if (gtk_cmclist_get_row_data(clist, row) == NULL) row = 0;
229
230         attrib = edit_person_attrib_edit(&errFlg, NULL);
231         if (!errFlg) {
232                 gchar *text[EMAIL_N_COLS];
233                 text[ATTRIB_COL_NAME] = attrib->name;
234                 text[ATTRIB_COL_VALUE] = attrib->value;
235
236                 row = gtk_cmclist_insert(clist, 1 + row, text);
237                 gtk_cmclist_set_row_data(clist, row, attrib);
238                 gtk_cmclist_select_row(clist, row, 0);
239                 edit_person_attrib_clear(NULL);
240         }
241 }
242
243 static void edit_person_entry_att_changed (GtkWidget *entry, gpointer data)
244 {
245         gboolean non_empty = gtk_cmclist_get_row_data(GTK_CMCLIST(personEditDlg->clist_attrib), 0) != NULL;
246         const gchar *sName;
247         int index;
248
249         if (personEditDlg->read_only)
250                 return;
251
252         index = gtk_combo_box_get_active(GTK_COMBO_BOX(personEditDlg->entry_atname));
253         sName = ATTRIBUTE[index];
254         if (list_find_attribute(sName)) {
255                 gtk_widget_set_sensitive(personEditDlg->attrib_add,FALSE);
256                 gtk_widget_set_sensitive(personEditDlg->attrib_mod,non_empty);
257                 attrib_adding = FALSE;
258                 attrib_saving = non_empty;
259         } 
260         else {
261                 gtk_widget_set_sensitive(personEditDlg->attrib_add,TRUE);
262                 gtk_widget_set_sensitive(personEditDlg->attrib_mod,non_empty);
263                 attrib_adding = TRUE;
264                 attrib_saving = non_empty;
265         }
266 }
267
268 static gboolean edit_person_entry_att_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
269 {
270         if (event && (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter)) {
271                 if (attrib_saving)
272                         edit_person_attrib_modify(NULL);
273                 else if (attrib_adding)
274                         edit_person_attrib_add(NULL);
275         }
276         return FALSE;
277 }
278
279 void addressbook_edit_person_page_attrib_ldap(PersonEditDlg *dialog, gint pageNum, gchar *pageLbl) {
280         GtkWidget *combo_box;
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_cmclist_new_with_titles(ATTRIB_N_COLS, titles);
331         gtk_container_add(GTK_CONTAINER(clist_swin), clist);
332         gtk_cmclist_set_selection_mode(GTK_CMCLIST(clist), GTK_SELECTION_BROWSE);
333         gtk_cmclist_set_column_width(GTK_CMCLIST(clist), ATTRIB_COL_NAME, ATTRIB_COL_WIDTH_NAME);
334         gtk_cmclist_set_column_width(GTK_CMCLIST(clist), ATTRIB_COL_VALUE, ATTRIB_COL_WIDTH_VALUE);
335         gtk_cmclist_set_compare_func(GTK_CMCLIST(clist), edit_person_attrib_compare_func);
336         gtk_cmclist_set_auto_sort(GTK_CMCLIST(clist), TRUE);
337
338         for (i = 0; i < ATTRIB_N_COLS; i++)
339                 gtkut_widget_set_can_focus(GTK_CMCLIST(clist)->column[i].button, FALSE);
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         gchar **attribute = (gchar **) ATTRIBUTE;
355
356         combo_box = gtk_combo_box_new_text();
357
358         while (*attribute) {
359                 if (!strcmp(*attribute, "jpegPhoto")) {
360                         attribute++;
361                         continue;
362                 }
363                 gtk_combo_box_append_text(GTK_COMBO_BOX(combo_box), *attribute++);
364         }
365         gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), 0);
366
367         gtk_table_attach(GTK_TABLE(table), combo_box, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
368
369         /* Next row */
370         ++top;
371         label = gtk_label_new(N_("Value"));
372         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
373         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
374
375         entry_value = gtk_entry_new();
376         gtk_table_attach(GTK_TABLE(table), entry_value, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
377
378         /* Button box */
379         vboxb = gtk_vbox_new(FALSE, 4);
380         gtk_box_pack_start(GTK_BOX(hbox), vboxb, FALSE, FALSE, 2);
381
382         vbuttonbox = gtk_vbutton_box_new();
383         gtk_button_box_set_layout(GTK_BUTTON_BOX(vbuttonbox), GTK_BUTTONBOX_START);
384         gtk_box_set_spacing(GTK_BOX(vbuttonbox), 8);
385         gtk_container_set_border_width(GTK_CONTAINER(vbuttonbox), 4);
386         gtk_container_add(GTK_CONTAINER(vboxb), vbuttonbox);
387
388         /* Buttons */
389         buttonDel = gtk_button_new_from_stock(GTK_STOCK_DELETE);
390         gtk_container_add(GTK_CONTAINER(vbuttonbox), buttonDel);
391
392         buttonMod = gtk_button_new_from_stock(GTK_STOCK_SAVE);
393         gtk_container_add(GTK_CONTAINER(vbuttonbox), buttonMod);
394
395         buttonAdd = gtk_button_new_from_stock(GTK_STOCK_ADD);
396         gtk_container_add(GTK_CONTAINER(vbuttonbox), buttonAdd);
397         
398         gtk_widget_set_sensitive(buttonDel,FALSE);
399         gtk_widget_set_sensitive(buttonMod,FALSE);
400         gtk_widget_set_sensitive(buttonAdd,FALSE);
401
402         gtk_widget_show_all(vbox);
403         
404         /* Event handlers */
405         g_signal_connect(G_OBJECT(clist), "select_row",
406                           G_CALLBACK( edit_person_attrib_list_selected), NULL);
407         g_signal_connect(G_OBJECT(buttonDel), "clicked",
408                           G_CALLBACK(edit_person_attrib_delete), NULL);
409         g_signal_connect(G_OBJECT(buttonMod), "clicked",
410                           G_CALLBACK(edit_person_attrib_modify), NULL);
411         g_signal_connect(G_OBJECT(buttonAdd), "clicked",
412                           G_CALLBACK(edit_person_attrib_add), NULL);
413         g_signal_connect(G_OBJECT(combo_box), "changed",
414                          G_CALLBACK(edit_person_entry_att_changed), NULL);
415         g_signal_connect(G_OBJECT(entry_value), "key_press_event",
416                          G_CALLBACK(edit_person_entry_att_pressed), NULL);
417         g_signal_connect(G_OBJECT(combo_box), "changed",
418                          G_CALLBACK(edit_person_combo_box_changed), clist);
419
420         personEditDlg->clist_attrib  = clist;
421         personEditDlg->entry_atname  = combo_box;
422         personEditDlg->entry_atvalue = entry_value;
423         personEditDlg->attrib_add = buttonAdd;
424         personEditDlg->attrib_del = buttonDel;
425         personEditDlg->attrib_mod = buttonMod;
426 }
427
428 #endif  /* USE_LDAP */
429
430