Use custom get_default_font_size() and _name()
[claws.git] / src / editaddress_other_attributes_ldap.c
blob0662b2f0a9703b01d715533b5c4d341e2131fb77
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2021 Michael Rasmussen and the Claws Mail team
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.
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.
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/>.
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #include "claws-features.h"
22 #endif
24 #ifdef USE_LDAP
26 #include "defs.h"
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"
40 #define ATTRIB_COL_WIDTH_NAME 120
41 #define ATTRIB_COL_WIDTH_VALUE 180
43 PersonEditDlg *personEditDlg;
44 gboolean attrib_adding = FALSE, attrib_saving = FALSE;
46 int get_attribute_index(const gchar *string_literal) {
47 int i = 0;
48 /*int count = sizeof(ATTRIBUTE) / sizeof(*ATTRIBUTE);*/
49 const gchar **attribute = ATTRIBUTE;
51 cm_return_val_if_fail(string_literal != NULL, -1);
52 while (*attribute) {
53 debug_print("Comparing %s to %s\n", *attribute, string_literal);
54 if (strcmp(*attribute++, string_literal) == 0)
55 return i;
56 i++;
58 return -1;
61 static void edit_person_status_show(gchar *msg) {
62 if (personEditDlg->statusbar != NULL) {
63 gtk_statusbar_pop(GTK_STATUSBAR(personEditDlg->statusbar), personEditDlg->status_cid);
64 if(msg) {
65 gtk_statusbar_push(GTK_STATUSBAR(personEditDlg->statusbar), personEditDlg->status_cid, msg);
70 static void edit_person_attrib_clear(gpointer data) {
71 gtk_combo_box_set_active(GTK_COMBO_BOX(personEditDlg->entry_atname), 0);
72 gtk_entry_set_text(GTK_ENTRY(personEditDlg->entry_atvalue), "");
75 static gboolean list_find_attribute(const gchar *attr)
77 GtkWidget *view = personEditDlg->view_attrib;
78 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
79 GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
80 GtkTreeIter iter;
81 UserAttribute *attrib;
83 if (!gtk_tree_model_get_iter_first(model, &iter))
84 return FALSE;
86 do {
87 gtk_tree_model_get(model, &iter, ATTRIB_COL_PTR, &attrib, -1);
88 if (!g_ascii_strcasecmp(attrib->name, attr)) {
89 gtk_tree_selection_select_iter(sel, &iter);
90 return TRUE;
92 } while (gtk_tree_model_iter_next(model, &iter));
94 return FALSE;
97 static void edit_person_combo_box_changed(GtkComboBox *opt_menu, gpointer data)
99 GtkWidget *view = GTK_WIDGET(data);
100 UserAttribute *attrib = gtkut_tree_view_get_selected_pointer(
101 GTK_TREE_VIEW(view), ATTRIB_COL_PTR, NULL, NULL, NULL);
102 gint option = gtk_combo_box_get_active(opt_menu);
103 const gchar *str = attrib ? attrib->name:"";
105 cm_return_if_fail (option < ATTRIBUTE_SIZE);
106 /* A corresponding attribute in contact does not match selected option */
107 if (strcmp(ATTRIBUTE[option], str) != 0) {
108 gtk_widget_set_sensitive(personEditDlg->attrib_add, TRUE);
109 gtk_widget_set_sensitive(personEditDlg->attrib_mod, TRUE);
110 gtk_widget_set_sensitive(personEditDlg->attrib_del, FALSE);
111 gtk_entry_set_text(GTK_ENTRY(personEditDlg->entry_atvalue), "");
112 gtk_widget_grab_focus(personEditDlg->entry_atvalue);
113 edit_person_status_show(NULL);
117 static void edit_person_attrib_cursor_changed(GtkTreeView *view,
118 gpointer user_data)
120 UserAttribute *attrib = gtkut_tree_view_get_selected_pointer(
121 view, ATTRIB_COL_PTR, NULL, NULL, NULL);
123 if( attrib && !personEditDlg->read_only ) {
124 gint index = get_attribute_index(attrib->name);
126 if (index == -1)
127 index = 0;
129 gtk_combo_box_set_active(GTK_COMBO_BOX(personEditDlg->entry_atname), index);
130 gtk_entry_set_text(GTK_ENTRY(personEditDlg->entry_atvalue), attrib->value);
132 gtk_widget_set_sensitive(personEditDlg->attrib_del, TRUE);
133 } else {
134 gtk_widget_set_sensitive(personEditDlg->attrib_del, FALSE);
136 edit_person_status_show( NULL );
139 static void edit_person_attrib_delete(gpointer data) {
140 UserAttribute *attrib;
141 GtkTreeModel *model;
142 GtkTreeSelection *sel;
143 GtkTreeIter iter;
144 gboolean has_row = FALSE;
145 gint n;
147 edit_person_attrib_clear(NULL);
148 edit_person_status_show(NULL);
150 attrib = gtkut_tree_view_get_selected_pointer(
151 GTK_TREE_VIEW(personEditDlg->view_attrib), ATTRIB_COL_PTR,
152 &model, &sel, &iter);
154 if (attrib) {
155 /* Remove list entry, and set iter to next row, if any */
156 has_row = gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
157 addritem_free_attribute(attrib);
158 attrib = NULL;
161 /* Position hilite bar */
162 if (!has_row) {
163 /* The removed row was the last in the list, so iter is not
164 * valid. Find out if there is at least one row remaining
165 * in the list, and select the last one if so. */
166 n = gtk_tree_model_iter_n_children(model, NULL);
167 if (n > 0 && gtk_tree_model_iter_nth_child(model, &iter, NULL, n-1)) {
168 /* It exists. */
169 has_row = TRUE;
173 if (!has_row)
174 gtk_tree_selection_select_iter(sel, &iter);
176 edit_person_attrib_cursor_changed(
177 GTK_TREE_VIEW(personEditDlg->view_attrib), NULL);
180 static UserAttribute *edit_person_attrib_edit(gboolean *error, UserAttribute *attrib) {
181 UserAttribute *retVal = NULL;
182 gchar *sName, *sValue, *sName_, *sValue_;
183 gint index;
185 *error = TRUE;
186 index = gtk_combo_box_get_active(GTK_COMBO_BOX(personEditDlg->entry_atname));
187 sName_ = (gchar *) ATTRIBUTE[index];
188 sValue_ = gtk_editable_get_chars(GTK_EDITABLE(personEditDlg->entry_atvalue), 0, -1);
189 sName = mgu_email_check_empty(sName_);
190 sValue = mgu_email_check_empty(sValue_);
191 g_free(sValue_);
193 if (sName && sValue) {
194 debug_print("sname && svalue\n");
195 if (attrib == NULL) {
196 attrib = addritem_create_attribute();
198 addritem_attrib_set_name(attrib, sName);
199 addritem_attrib_set_value(attrib, sValue);
200 retVal = attrib;
201 *error = FALSE;
203 else {
204 edit_person_status_show(N_( "A Name and Value must be supplied." ));
205 gtk_widget_grab_focus(personEditDlg->entry_atvalue);
208 g_free(sName);
209 g_free(sValue);
211 return retVal;
214 static void edit_person_attrib_modify(gpointer data) {
215 gboolean errFlg = FALSE;
216 GtkTreeModel *model;
217 GtkTreeIter iter;
218 UserAttribute *attrib;
220 attrib = gtkut_tree_view_get_selected_pointer(
221 GTK_TREE_VIEW(personEditDlg->view_attrib), ATTRIB_COL_PTR,
222 &model, NULL, &iter);
223 if (attrib) {
224 edit_person_attrib_edit(&errFlg, attrib);
225 if (!errFlg) {
226 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
227 ATTRIB_COL_NAME, attrib->name,
228 ATTRIB_COL_VALUE, attrib->value,
229 -1);
230 edit_person_attrib_clear(NULL);
235 static void edit_person_attrib_add(gpointer data) {
236 gboolean errFlg = FALSE;
237 GtkTreeModel *model = gtk_tree_view_get_model(
238 GTK_TREE_VIEW(personEditDlg->view_attrib));
239 GtkTreeSelection *sel = gtk_tree_view_get_selection(
240 GTK_TREE_VIEW(personEditDlg->view_attrib));
241 GtkTreeIter iter, iter2;
242 UserAttribute *attrib, *prev_attrib;
244 attrib = edit_person_attrib_edit(&errFlg, NULL);
245 if (attrib == NULL) /* input for new attribute is not valid */
246 return;
248 prev_attrib = gtkut_tree_view_get_selected_pointer(
249 GTK_TREE_VIEW(personEditDlg->view_attrib), ATTRIB_COL_PTR,
250 &model, &sel, &iter);
252 if (prev_attrib == NULL) {
253 /* No row selected, or list empty, add it as first row. */
254 gtk_list_store_insert(GTK_LIST_STORE(model), &iter, 0);
255 } else {
256 /* Add it after the currently selected row. */
257 gtk_list_store_insert_after(GTK_LIST_STORE(model), &iter2,
258 &iter);
259 iter = iter2;
262 /* Fill out the new row. */
263 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
264 ATTRIB_COL_NAME, attrib->name,
265 ATTRIB_COL_VALUE, attrib->value,
266 ATTRIB_COL_PTR, attrib,
267 -1);
268 gtk_tree_selection_select_iter(sel, &iter);
269 edit_person_attrib_clear(NULL);
272 static void edit_person_entry_att_changed (GtkWidget *entry, gpointer data)
274 GtkTreeModel *model = gtk_tree_view_get_model(
275 GTK_TREE_VIEW(personEditDlg->view_attrib));
276 gboolean non_empty = (gtk_tree_model_iter_n_children(model, NULL) > 0);
277 const gchar *sName;
278 int index;
280 if (personEditDlg->read_only)
281 return;
283 index = gtk_combo_box_get_active(GTK_COMBO_BOX(personEditDlg->entry_atname));
284 sName = ATTRIBUTE[index];
285 if (list_find_attribute(sName)) {
286 gtk_widget_set_sensitive(personEditDlg->attrib_add,FALSE);
287 gtk_widget_set_sensitive(personEditDlg->attrib_mod,non_empty);
288 attrib_adding = FALSE;
289 attrib_saving = non_empty;
291 else {
292 gtk_widget_set_sensitive(personEditDlg->attrib_add,TRUE);
293 gtk_widget_set_sensitive(personEditDlg->attrib_mod,non_empty);
294 attrib_adding = TRUE;
295 attrib_saving = non_empty;
299 static gboolean edit_person_entry_att_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
301 if (event && (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter)) {
302 if (attrib_saving)
303 edit_person_attrib_modify(NULL);
304 else if (attrib_adding)
305 edit_person_attrib_add(NULL);
307 return FALSE;
310 void addressbook_edit_person_page_attrib_ldap(PersonEditDlg *dialog, gint pageNum, gchar *pageLbl) {
311 GtkWidget *combo_box;
312 GtkWidget *vbox;
313 GtkWidget *hbox;
314 GtkWidget *vboxl;
315 GtkWidget *vboxb;
316 GtkWidget *vbuttonbox;
317 GtkWidget *buttonDel;
318 GtkWidget *buttonMod;
319 GtkWidget *buttonAdd;
321 GtkWidget *table;
322 GtkWidget *label;
323 GtkWidget *scrollwin;
324 GtkWidget *view;
325 GtkWidget *entry_value;
326 GtkListStore *store;
327 GtkTreeViewColumn *col;
328 GtkCellRenderer *rdr;
329 GtkTreeSelection *sel;
331 personEditDlg = dialog;
333 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
334 gtk_widget_show(vbox);
335 gtk_container_add(GTK_CONTAINER(personEditDlg->notebook), vbox);
336 gtk_container_set_border_width(GTK_CONTAINER(vbox), BORDER_WIDTH);
338 label = gtk_label_new_with_mnemonic(pageLbl);
339 gtk_widget_show(label);
340 gtk_notebook_set_tab_label(
341 GTK_NOTEBOOK(personEditDlg->notebook),
342 gtk_notebook_get_nth_page(GTK_NOTEBOOK(personEditDlg->notebook), pageNum), label);
344 /* Split into two areas */
345 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
346 gtk_container_add(GTK_CONTAINER(vbox), hbox);
348 /* Attribute list */
349 vboxl = gtk_box_new(GTK_ORIENTATION_VERTICAL, 4);
350 gtk_container_add(GTK_CONTAINER(hbox), vboxl);
351 gtk_container_set_border_width(GTK_CONTAINER(vboxl), 4);
353 scrollwin = gtk_scrolled_window_new(NULL, NULL);
354 gtk_container_add(GTK_CONTAINER(vboxl), scrollwin);
355 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin),
356 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
358 store = gtk_list_store_new(ATTRIB_N_COLS,
359 G_TYPE_STRING, G_TYPE_STRING,
360 G_TYPE_POINTER, -1);
362 view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
363 g_object_unref(store);
364 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), TRUE);
365 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
366 gtk_tree_selection_set_mode(sel, GTK_SELECTION_BROWSE);
368 rdr = gtk_cell_renderer_text_new();
369 col = gtk_tree_view_column_new_with_attributes(_("Name"), rdr,
370 "markup", ATTRIB_COL_NAME, NULL);
371 gtk_tree_view_column_set_min_width(col, ATTRIB_COL_WIDTH_NAME);
372 gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
374 col = gtk_tree_view_column_new_with_attributes(_("Value"), rdr,
375 "markup", ATTRIB_COL_VALUE, NULL);
376 gtk_tree_view_column_set_min_width(col, ATTRIB_COL_WIDTH_VALUE);
377 gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
379 gtk_container_add(GTK_CONTAINER(scrollwin), view);
381 /* Data entry area */
382 table = gtk_grid_new();
383 gtk_box_pack_start(GTK_BOX(vboxl), table, FALSE, FALSE, 0);
384 gtk_container_set_border_width(GTK_CONTAINER(table), 4);
385 gtk_grid_set_row_spacing(GTK_GRID(table), 4);
386 gtk_grid_set_column_spacing(GTK_GRID(table), 4);
388 /* First row */
389 label = gtk_label_new(N_("Name"));
390 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
391 gtk_grid_attach(GTK_GRID(table), label, 0, 0, 1, 1);
393 gchar **attribute = (gchar **) ATTRIBUTE;
395 combo_box = gtk_combo_box_text_new();
397 while (*attribute) {
398 if (!strcmp(*attribute, "jpegPhoto")) {
399 attribute++;
400 continue;
402 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo_box), *attribute++);
404 gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), 0);
406 gtk_grid_attach(GTK_GRID(table), combo_box, 1, 0, 1, 1);
407 gtk_widget_set_hexpand(combo_box, TRUE);
408 gtk_widget_set_halign(combo_box, GTK_ALIGN_FILL);
410 /* Next row */
411 label = gtk_label_new(N_("Value"));
412 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
413 gtk_grid_attach(GTK_GRID(table), label, 0, 1, 1, 1);
415 entry_value = gtk_entry_new();
416 gtk_grid_attach(GTK_GRID(table), entry_value, 1, 1, 1, 1);
417 gtk_widget_set_hexpand(entry_value, TRUE);
418 gtk_widget_set_halign(entry_value, GTK_ALIGN_FILL);
420 /* Button box */
421 vboxb = gtk_box_new(GTK_ORIENTATION_VERTICAL, 4);
422 gtk_box_pack_start(GTK_BOX(hbox), vboxb, FALSE, FALSE, 2);
424 vbuttonbox = gtk_button_box_new(GTK_ORIENTATION_VERTICAL);
425 gtk_button_box_set_layout(GTK_BUTTON_BOX(vbuttonbox), GTK_BUTTONBOX_START);
426 gtk_box_set_spacing(GTK_BOX(vbuttonbox), 8);
427 gtk_container_set_border_width(GTK_CONTAINER(vbuttonbox), 4);
428 gtk_container_add(GTK_CONTAINER(vboxb), vbuttonbox);
430 /* Buttons */
431 buttonDel = gtkut_stock_button("edit-delete", _("D_elete"));
432 gtk_container_add(GTK_CONTAINER(vbuttonbox), buttonDel);
434 buttonMod = gtkut_stock_button("document-save", _("_Save"));
435 gtk_container_add(GTK_CONTAINER(vbuttonbox), buttonMod);
437 buttonAdd = gtkut_stock_button("list-add", _("_Add"));
438 gtk_container_add(GTK_CONTAINER(vbuttonbox), buttonAdd);
440 gtk_widget_set_sensitive(buttonDel,FALSE);
441 gtk_widget_set_sensitive(buttonMod,FALSE);
442 gtk_widget_set_sensitive(buttonAdd,FALSE);
444 gtk_widget_show_all(vbox);
446 /* Event handlers */
447 g_signal_connect(G_OBJECT(view), "cursor-changed",
448 G_CALLBACK(edit_person_attrib_cursor_changed), NULL);
449 g_signal_connect(G_OBJECT(buttonDel), "clicked",
450 G_CALLBACK(edit_person_attrib_delete), NULL);
451 g_signal_connect(G_OBJECT(buttonMod), "clicked",
452 G_CALLBACK(edit_person_attrib_modify), NULL);
453 g_signal_connect(G_OBJECT(buttonAdd), "clicked",
454 G_CALLBACK(edit_person_attrib_add), NULL);
455 g_signal_connect(G_OBJECT(combo_box), "changed",
456 G_CALLBACK(edit_person_entry_att_changed), NULL);
457 g_signal_connect(G_OBJECT(entry_value), "key_press_event",
458 G_CALLBACK(edit_person_entry_att_pressed), NULL);
459 g_signal_connect(G_OBJECT(combo_box), "changed",
460 G_CALLBACK(edit_person_combo_box_changed), view);
462 personEditDlg->view_attrib = view;
463 personEditDlg->entry_atname = combo_box;
464 personEditDlg->entry_atvalue = entry_value;
465 personEditDlg->attrib_add = buttonAdd;
466 personEditDlg->attrib_del = buttonDel;
467 personEditDlg->attrib_mod = buttonMod;
470 #endif /* USE_LDAP */