aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/exchange-operations/exchange-permissions-dialog.c
blob: 41fe731a29cb2d21aba0041a971c4f9aa4ee1295 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/* Copyright (C) 2002-2004 Novell, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "exchange-permissions-dialog.h"
#include "exchange-hierarchy.h"
#include "exchange-user-dialog.h"

#include "e2k-context.h"
#include "e2k-global-catalog.h"
#include "e2k-propnames.h"
#include "e2k-sid.h"
#include "e2k-security-descriptor.h"

#include "e2k-uri.h"
#include "e2k-utils.h"
#include "e-folder-exchange.h"
#include "exchange-account.h"
#include "exchange-operations.h"

#include <e-util/e-dialog-utils.h>
#include <e-util/e-error.h>
#include <glade/glade-xml.h>
#include <gtk/gtkbox.h>
#include <gtk/gtkcellrenderertext.h>
#include <gtk/gtkliststore.h>
#include <gtk/gtkmenuitem.h>
#include <gtk/gtkmenushell.h>
#include <gtk/gtkstock.h>
#include <gtk/gtktogglebutton.h>
#include <gtk/gtktreeselection.h>
#include <gtk/gtktreeview.h>
#include <gtk/gtk.h>

#undef GTK_DISABLE_DEPRECATED

struct _ExchangePermissionsDialogPrivate {
    ExchangeAccount *account;
    char *base_uri, *folder_path;
    E2kSecurityDescriptor *sd;
    gboolean changed;
    gboolean frozen;

    /* The user list */
    GtkTreeView *list_view;
    GtkListStore *list_store;
    GtkTreeSelection *list_selection;
    E2kSid *selected_sid;

    /* The Role menu */
    GtkComboBox *role_optionmenu;

    /* Custom label is added or not */
    gboolean custom_added;
    
    GtkWidget *separator, *custom;
    E2kPermissionsRole selected_role;

    /* The toggles */
    GtkToggleButton *read_items_check, *create_items_check;
    GtkToggleButton *create_subfolders_check, *folder_visible_check;
    GtkToggleButton *folder_owner_check, *folder_contact_check;
    GtkToggleButton *edit_none_radio, *edit_own_radio, *edit_all_radio;
    GtkToggleButton *delete_none_radio, *delete_own_radio, *delete_all_radio;
    guint32 selected_perms;
};

enum {
    EXCHANGE_PERMISSIONS_DIALOG_NAME_COLUMN,
    EXCHANGE_PERMISSIONS_DIALOG_ROLE_COLUMN,
    EXCHANGE_PERMISSIONS_DIALOG_SID_COLUMN,

    EXCHANGE_PERMISSIONS_DIALOG_NUM_COLUMNS
};

#define PARENT_TYPE GTK_TYPE_DIALOG
static GtkDialogClass *parent_class = NULL;

static void
finalize (GObject *object)
{
    ExchangePermissionsDialog *dialog =
        EXCHANGE_PERMISSIONS_DIALOG (object);

    g_free (dialog->priv->base_uri);
    g_free (dialog->priv->folder_path);

    if (dialog->priv->sd)
        g_object_unref (dialog->priv->sd);

    g_free (dialog->priv);

    G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
class_init (GObjectClass *object_class)
{
    parent_class = g_type_class_ref (PARENT_TYPE);

    /* virtual method override */
    object_class->finalize = finalize;
}

static void
init (GObject *object)
{
    ExchangePermissionsDialog *dialog =
        EXCHANGE_PERMISSIONS_DIALOG (object);

    dialog->priv = g_new0 (ExchangePermissionsDialogPrivate, 1);
}

E2K_MAKE_TYPE (exchange_permissions_dialog, ExchangePermissionsDialog, class_init, init, PARENT_TYPE)



static void get_widgets         (ExchangePermissionsDialog *dialog,
                 GladeXML *xml);
static void setup_user_list     (ExchangePermissionsDialog *dialog);
static void display_permissions (ExchangePermissionsDialog *dialog);
static void dialog_response     (ExchangePermissionsDialog *dialog,
                 int response, gpointer user_data);

static const char *sd_props[] = {
    E2K_PR_EXCHANGE_SD_BINARY,
    E2K_PR_EXCHANGE_SD_XML
};
static const int n_sd_props = sizeof (sd_props) / sizeof (sd_props[0]);

/**
 * exchange_permissions_dialog_new:
 * @account: an account
 * @folder: the folder whose permissions are to be editted
 * @parent: a widget in the dialog's parent window
 *
 * Creates and displays a modeless permissions editor dialog for @folder.
 **/
void
exchange_permissions_dialog_new (ExchangeAccount *account,
                 EFolder *folder,
                 GtkWidget *parent)
{
    ExchangePermissionsDialog *dialog;
    const char *base_uri, *folder_uri, *folder_path;
    E2kContext *ctx;
    ExchangeHierarchy *hier;
    GladeXML *xml;
    GtkWidget *box;
    char *title;
    E2kHTTPStatus status;
    E2kResult *results;
    int nresults = 0;
    xmlNode *xml_form;
    GByteArray *binary_form;

    g_return_if_fail (folder);

    ctx = exchange_account_get_context (account);
    g_return_if_fail (ctx);
    xml = glade_xml_new (
        CONNECTOR_GLADEDIR "/exchange-permissions-dialog.glade",
        "permissions_vbox", PACKAGE);
    g_return_if_fail (xml != NULL);

    /* Create the dialog */
    dialog = g_object_new (EXCHANGE_TYPE_PERMISSIONS_DIALOG, NULL);

    title = g_strdup_printf (_("Permissions for %s"),
                 e_folder_get_name (folder));
    gtk_window_set_title (GTK_WINDOW (dialog), title);
    g_free (title);

    gtk_dialog_add_buttons (GTK_DIALOG (dialog),
                GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                GTK_STOCK_OK, GTK_RESPONSE_OK,
                NULL);

    g_signal_connect (dialog, "response",
              G_CALLBACK (dialog_response), NULL);

    dialog->priv->changed = FALSE;

    /* Put the widgets from the glade file into it */
    box = glade_xml_get_widget (xml, "permissions_vbox");
    g_object_ref (box);
    gtk_widget_unparent (box);
    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
                box, TRUE, TRUE, 0);
    g_object_unref (box);

    get_widgets (dialog, xml);
    g_object_unref (xml);

    dialog->priv->account = account;
    g_object_ref (account);

    hier = e_folder_exchange_get_hierarchy (folder);
    base_uri = e_folder_exchange_get_internal_uri (hier->toplevel);
    dialog->priv->base_uri = g_strdup (base_uri);
    folder_uri = e_folder_exchange_get_internal_uri (folder);
    folder_path = e2k_uri_relative (dialog->priv->base_uri, folder_uri);
    dialog->priv->folder_path = g_strdup (folder_path);
        
    /* And fetch the security descriptor */
    status = e2k_context_propfind (ctx, NULL, folder_uri,
                       sd_props, n_sd_props,
                       &results, &nresults);
    if (!E2K_HTTP_STATUS_IS_SUCCESSFUL (status) || nresults < 1) {
    lose:
        e_error_run (GTK_WINDOW (parent), ERROR_DOMAIN ":perm-read-error", NULL);
        gtk_widget_destroy (GTK_WIDGET (dialog));
        if (nresults)
            e2k_results_free (results, nresults);
        return;
    }

    xml_form = e2k_properties_get_prop (results[0].props,
                        E2K_PR_EXCHANGE_SD_XML);
    binary_form = e2k_properties_get_prop (results[0].props,
                           E2K_PR_EXCHANGE_SD_BINARY);
    if (!xml_form || !binary_form)
        goto lose;

    dialog->priv->sd = e2k_security_descriptor_new (xml_form, binary_form);
    if (!dialog->priv->sd)
        goto lose;

    setup_user_list (dialog);
    gtk_widget_show (GTK_WIDGET (dialog));
    if (nresults)
        e2k_results_free (results, nresults);
}

static void
dialog_response (ExchangePermissionsDialog *dialog, int response,
         gpointer user_data)
{
    E2kContext *ctx;
    GByteArray *binsd;
    E2kProperties *props;
    E2kResultIter *iter;
    E2kResult *result;
    E2kHTTPStatus status;

    if (response != GTK_RESPONSE_OK || !dialog->priv->changed) {
        gtk_widget_destroy (GTK_WIDGET (dialog));
        return;
    }

    ctx = exchange_account_get_context (dialog->priv->account);
    g_return_if_fail (ctx != NULL);

    binsd = e2k_security_descriptor_to_binary (dialog->priv->sd);
    if (!binsd) {
        e_error_run (GTK_WINDOW (dialog), ERROR_DOMAIN ":perm-update-error", "", NULL);
        return;
    }

    gtk_widget_set_sensitive (GTK_WIDGET (dialog), FALSE);

    props = e2k_properties_new ();
    e2k_properties_set_binary (props, E2K_PR_EXCHANGE_SD_BINARY, binsd);

    /* We use BPROPPATCH here instead of PROPPATCH, because
     * PROPPATCH seems to mysteriously fail in someone else's
     * folder hierarchy. #29726
     */
    iter = e2k_context_bproppatch_start (ctx, NULL, dialog->priv->base_uri,
                         (const char **)&dialog->priv->folder_path, 1,
                         props, FALSE);
    e2k_properties_free (props);

    result = e2k_result_iter_next (iter);
    if (result) {
        status = result->status;
        e2k_result_iter_free (iter);
    } else
        status = e2k_result_iter_free (iter);

    gtk_widget_set_sensitive (GTK_WIDGET (dialog), TRUE);

    if (!E2K_HTTP_STATUS_IS_SUCCESSFUL (status)) {
        e_error_run (GTK_WINDOW (dialog), ERROR_DOMAIN ":perm-update-error",
                 status == E2K_HTTP_UNAUTHORIZED ?
                 _("(Permission denied.)") : "", NULL);
        return;
    }

    if (response == GTK_RESPONSE_OK)
        gtk_widget_destroy (GTK_WIDGET (dialog));
}


static void
set_permissions (ExchangePermissionsDialog *dialog, guint32 perms)
{
    dialog->priv->selected_perms = perms;
    dialog->priv->selected_role = e2k_permissions_role_find (perms);
    e2k_security_descriptor_set_permissions (dialog->priv->sd,
                         dialog->priv->selected_sid,
                         dialog->priv->selected_perms);

    dialog->priv->changed = TRUE;
}


/* User list functions */

static void
list_view_selection_changed (GtkTreeSelection *selection, gpointer user_data)
{
    ExchangePermissionsDialog *dialog = user_data;
    GtkTreeModel *model;
    GtkTreeIter iter;
    E2kSid *sid;

    if (!gtk_tree_selection_get_selected (selection, &model, &iter))
        return;
    gtk_tree_model_get (model, &iter,
                EXCHANGE_PERMISSIONS_DIALOG_SID_COLUMN, &sid,
                -1);

    dialog->priv->selected_sid = sid;
    dialog->priv->selected_perms =
        e2k_security_descriptor_get_permissions (dialog->priv->sd, sid);
    dialog->priv->selected_role =
        e2k_permissions_role_find (dialog->priv->selected_perms);

    /* "Default" or "Anonymous" can't be a Folder contact, but any
     * real person can.
     */
    gtk_widget_set_sensitive (GTK_WIDGET (dialog->priv->folder_contact_check),
                  e2k_sid_get_sid_type (sid) != E2K_SID_TYPE_WELL_KNOWN_GROUP);

    /* Update role menu and permissions checkboxes */
    display_permissions (dialog);
}

static void
add_user_to_list (ExchangePermissionsDialog *dialog, E2kSid *sid, gboolean select)
{
    guint32 perms;
    E2kPermissionsRole role;
    GtkTreeIter iter;

    perms = e2k_security_descriptor_get_permissions (dialog->priv->sd,
                             sid);
    role = e2k_permissions_role_find (perms);

    if (e2k_sid_get_sid_type (sid) == E2K_SID_TYPE_WELL_KNOWN_GROUP)
        gtk_list_store_insert (dialog->priv->list_store, &iter, 1);
    else
        gtk_list_store_append (dialog->priv->list_store, &iter);

    gtk_list_store_set (dialog->priv->list_store, &iter,
                EXCHANGE_PERMISSIONS_DIALOG_NAME_COLUMN,
                e2k_sid_get_display_name (sid),
                EXCHANGE_PERMISSIONS_DIALOG_ROLE_COLUMN,
                e2k_permissions_role_get_name (role),
                EXCHANGE_PERMISSIONS_DIALOG_SID_COLUMN,
                sid,
                -1);

    if (select)
        gtk_tree_selection_select_iter (dialog->priv->list_selection, &iter);
}

static void
add_clicked (GtkButton *button, gpointer user_data)
{
    ExchangePermissionsDialog *dialog = user_data;
    E2kGlobalCatalog *gc;
    E2kGlobalCatalogStatus status;
    E2kGlobalCatalogEntry *entry;
    E2kSid *sid2;
    GtkTreeIter iter;
    GtkWidget *user_dialog;
    const guint8 *bsid, *bsid2;
    GList *email_list = NULL;
    GList *l = NULL;
    char *email = NULL;
    gboolean valid;
    gint result;

    gc = exchange_account_get_global_catalog (dialog->priv->account);

    if (!gc) {
        e_error_run (GTK_WINDOW (dialog), ERROR_DOMAIN ":acl-no-gcs-error", 
                 NULL);
        return;
    }

    user_dialog = e2k_user_dialog_new (GTK_WIDGET (dialog), _("Add User:"), _("Add User"));
    result = gtk_dialog_run (GTK_DIALOG (user_dialog));

    if (result == GTK_RESPONSE_OK)
        email_list = e2k_user_dialog_get_user_list (E2K_USER_DIALOG (user_dialog));
    gtk_widget_destroy (user_dialog);

    if (email_list == NULL)
        return;

    for (l = email_list; l; l = g_list_next (l)) {
        email = l->data;
        status = e2k_global_catalog_lookup (
            gc, NULL, /* FIXME: cancellable */
            E2K_GLOBAL_CATALOG_LOOKUP_BY_EMAIL, email,
            E2K_GLOBAL_CATALOG_LOOKUP_SID, &entry);
        switch (status) {
            case E2K_GLOBAL_CATALOG_OK:
                break;
            case E2K_GLOBAL_CATALOG_NO_SUCH_USER:
                e_error_run (GTK_WINDOW (dialog), ERROR_DOMAIN ":no-user-error", email, NULL);
                break;
            case E2K_GLOBAL_CATALOG_NO_DATA:
                e_error_run (GTK_WINDOW (dialog), ERROR_DOMAIN ":acl-add-error", email, NULL);
                break;
            default:
                e_error_run (GTK_WINDOW (dialog), ERROR_DOMAIN ":perm-unknown-error", email, NULL);
                break;
        }
        if (status != E2K_GLOBAL_CATALOG_OK)
            return;

        /* Make sure the user isn't already there. */
        bsid = e2k_sid_get_binary_sid (entry->sid);
        valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (dialog->priv->list_store), &iter);
        while (valid) {
            gtk_tree_model_get (GTK_TREE_MODEL (dialog->priv->list_store), &iter,
                        EXCHANGE_PERMISSIONS_DIALOG_SID_COLUMN, &sid2,
                        -1);
            bsid2 = e2k_sid_get_binary_sid (sid2);
            if (e2k_sid_binary_sid_equal (bsid, bsid2)) {
                e_error_run (GTK_WINDOW (dialog), ERROR_DOMAIN ":perm-existing-error", 
                         entry->display_name, NULL);
                e2k_global_catalog_entry_free (gc, entry);
                gtk_tree_selection_select_iter (dialog->priv->list_selection, &iter);
                return;
            }

            valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (dialog->priv->list_store), &iter);
        }

        add_user_to_list (dialog, entry->sid, TRUE);

        /* Calling set_permissions will cause the sd to take a
         * ref on the sid, allowing us to unref it.
         */
        set_permissions (dialog, 0);
        e2k_global_catalog_entry_free (gc, entry);
    }
    g_list_free (email_list);
}

static void
remove_clicked (GtkButton *button, gpointer user_data)
{
    ExchangePermissionsDialog *dialog = user_data;
    E2kSid *sid;
    GdkModifierType modifiers;
    GtkTreeModel *model;
    GtkTreeIter iter;

    if (!gtk_tree_selection_get_selected (dialog->priv->list_selection,
                          &model, &iter))
        return;
    gtk_tree_model_get (model, &iter,
                EXCHANGE_PERMISSIONS_DIALOG_SID_COLUMN, &sid,
                -1);
    gdk_window_get_pointer (NULL, NULL, NULL, &modifiers);

    if (e2k_sid_get_sid_type (sid) == E2K_SID_TYPE_WELL_KNOWN_GROUP &&
        !(modifiers & GDK_SHIFT_MASK)) {
        /* You shouldn't normally delete "Default" or "Anonymous". */
        set_permissions (dialog, 0);
    } else {
        gtk_list_store_remove (dialog->priv->list_store, &iter);
        e2k_security_descriptor_remove_sid (dialog->priv->sd, sid);

        if (!gtk_list_store_iter_is_valid (dialog->priv->list_store, &iter)) {
            /* Select the new last row. Love that API... */
            gtk_tree_model_iter_nth_child (model, &iter, NULL,
                               gtk_tree_model_iter_n_children (model, NULL) - 1);
        }
        gtk_tree_selection_select_iter (dialog->priv->list_selection, &iter);

        dialog->priv->changed = TRUE;
    }
}

static void
setup_user_list (ExchangePermissionsDialog *dialog)
{
    E2kSecurityDescriptor *sd = dialog->priv->sd;
    E2kSid *default_entry;
    GList *sids;

    /* FIXME */
    /* gtk_clist_freeze (dialog->priv->clist); */

    /* Always put "Default" first. */
    default_entry = e2k_security_descriptor_get_default (sd);
    add_user_to_list (dialog, default_entry, TRUE);

    sids = e2k_security_descriptor_get_sids (sd);
    while (sids) {
        if (sids->data != default_entry)
            add_user_to_list (dialog, sids->data, FALSE);
        sids = sids->next;
    }
    g_list_free (sids);

    /* gtk_clist_thaw (dialog->priv->clist); */
}


/* Role option menu functions */

static void
role_changed (GtkWidget *role_combo, gpointer user_data)
{
    ExchangePermissionsDialog *dialog = user_data;
    int role;

    if (dialog->priv->frozen)
        return;

    role = gtk_combo_box_get_active (GTK_COMBO_BOX (role_combo));
    if (role == dialog->priv->selected_role)
        return;
    if (role >= E2K_PERMISSIONS_ROLE_NUM_ROLES) {
        /* The user selected "Custom". Since "Custom" will
         * only be there to select when it's already
         * selected, this is a no-op.
         */
        return;
    }

    set_permissions (dialog, e2k_permissions_role_get_perms (role));
    display_permissions (dialog);
}

static void
display_role (ExchangePermissionsDialog *dialog)
{
    int role = dialog->priv->selected_role;
    GtkTreeModel *model;
    GtkTreeIter iter;

    if (!gtk_tree_selection_get_selected (dialog->priv->list_selection,
                          &model, &iter))
        return;
    gtk_list_store_set (dialog->priv->list_store, &iter,
                EXCHANGE_PERMISSIONS_DIALOG_ROLE_COLUMN,
                e2k_permissions_role_get_name (role),
                -1);

    if (role == E2K_PERMISSIONS_ROLE_CUSTOM) {
        if (dialog->priv->custom_added == FALSE) {
            gtk_combo_box_append_text (GTK_COMBO_BOX (dialog->priv->role_optionmenu), _("Custom"));
            dialog->priv->custom_added = TRUE;
        }
        role = E2K_PERMISSIONS_ROLE_NUM_ROLES;
    }
    else    {
        if (dialog->priv->custom_added) {
            gtk_combo_box_remove_text (GTK_COMBO_BOX (dialog->priv->role_optionmenu), E2K_PERMISSIONS_ROLE_NUM_ROLES);
            dialog->priv->custom_added = FALSE;
        }
    }
        
    gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->priv->role_optionmenu), role);
}



/* Toggle buttons */
static void
check_toggled (GtkToggleButton *toggle, gpointer user_data)
{
    ExchangePermissionsDialog *dialog = user_data;
    ExchangePermissionsDialogPrivate *priv = dialog->priv;
    guint32 new_perms, value;

    if (dialog->priv->frozen)
        return;

    value = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (toggle), "mapi_permission"));

    if (gtk_toggle_button_get_active (toggle))
        new_perms = priv->selected_perms | value;
    else
        new_perms = priv->selected_perms & ~value;

    if (new_perms == priv->selected_perms)
        return;

    set_permissions (dialog, new_perms);
    display_role (dialog);
}

static void
radio_toggled (GtkToggleButton *toggle, gpointer user_data)
{
    ExchangePermissionsDialog *dialog = user_data;
    ExchangePermissionsDialogPrivate *priv = dialog->priv;
    guint32 new_perms, value, mask;

    if (dialog->priv->frozen || !gtk_toggle_button_get_active (toggle))
        return;

    value = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (toggle), "mapi_permission"));
    mask = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (toggle), "mapi_mask"));

    new_perms = (priv->selected_perms & ~mask) | value;
    if (new_perms == priv->selected_perms)
        return;

    set_permissions (dialog, new_perms);
    display_role (dialog);
}

static void
rv_toggle (GtkToggleButton *toggled, gpointer user_data)
{
    ExchangePermissionsDialog *dialog = user_data;
    GtkToggleButton *visible = dialog->priv->folder_visible_check;
    GtkToggleButton *read = dialog->priv->read_items_check;

    if (dialog->priv->frozen)
        return;

    /* If you turn off "Folder visible", then "Read items" turns
     * off too. Contrariwise, if you turn on "Read items", then
     * "Folder visible" turns on too.
     */
    if (toggled == visible && !gtk_toggle_button_get_active (toggled))
        gtk_toggle_button_set_active (read, FALSE);
    else if (toggled == read && gtk_toggle_button_get_active (toggled))
        gtk_toggle_button_set_active (visible, TRUE);
}

static void
display_permissions (ExchangePermissionsDialog *dialog)
{
    GtkToggleButton *radio;
    guint32 perms = dialog->priv->selected_perms;

    dialog->priv->frozen = TRUE;

    /* Set up check boxes */
    gtk_toggle_button_set_active (dialog->priv->read_items_check,
                      perms & E2K_PERMISSION_READ_ANY);
    gtk_toggle_button_set_active (dialog->priv->create_items_check,
                      perms & E2K_PERMISSION_CREATE);
    gtk_toggle_button_set_active (dialog->priv->create_subfolders_check,
                      perms & E2K_PERMISSION_CREATE_SUBFOLDER);
    gtk_toggle_button_set_active (dialog->priv->folder_owner_check,
                      perms & E2K_PERMISSION_OWNER);
    gtk_toggle_button_set_active (dialog->priv->folder_contact_check,
                      (perms & E2K_PERMISSION_CONTACT) &&
                      GTK_WIDGET_SENSITIVE (dialog->priv->folder_contact_check));
    gtk_toggle_button_set_active (dialog->priv->folder_visible_check,
                      perms & E2K_PERMISSION_FOLDER_VISIBLE);

    /* Set up radio buttons */
    if (perms & E2K_PERMISSION_EDIT_ANY)
        radio = dialog->priv->edit_all_radio;
    else if (perms & E2K_PERMISSION_EDIT_OWNED)
        radio = dialog->priv->edit_own_radio;
    else
        radio = dialog->priv->edit_none_radio;
    gtk_toggle_button_set_active (radio, TRUE);

    if (perms & E2K_PERMISSION_DELETE_ANY)
        radio = dialog->priv->delete_all_radio;
    else if (perms & E2K_PERMISSION_DELETE_OWNED)
        radio = dialog->priv->delete_own_radio;
    else
        radio = dialog->priv->delete_none_radio;
    gtk_toggle_button_set_active (radio, TRUE);

    /* And role menu */
    display_role (dialog);

    dialog->priv->frozen = FALSE;
}



static void
get_widgets (ExchangePermissionsDialog *dialog, GladeXML *xml)
{
    GtkWidget *button;
    GtkTreeViewColumn *column;

#define GET_WIDGET(name, type) dialog->priv->name = type (glade_xml_get_widget (xml, #name))

    GET_WIDGET (list_view, GTK_TREE_VIEW);
    column = gtk_tree_view_column_new_with_attributes (
        _("Name"), gtk_cell_renderer_text_new (),
        "text", EXCHANGE_PERMISSIONS_DIALOG_NAME_COLUMN, NULL);
    gtk_tree_view_append_column (dialog->priv->list_view, column);
    column = gtk_tree_view_column_new_with_attributes (
        _("Role"), gtk_cell_renderer_text_new (),
        "text", EXCHANGE_PERMISSIONS_DIALOG_ROLE_COLUMN, NULL);
    gtk_tree_view_append_column (dialog->priv->list_view, column);

    dialog->priv->list_selection = gtk_tree_view_get_selection (dialog->priv->list_view);
    gtk_tree_selection_set_mode (dialog->priv->list_selection,
                     GTK_SELECTION_SINGLE);
    g_signal_connect (dialog->priv->list_selection, "changed",
              G_CALLBACK (list_view_selection_changed), dialog);

    dialog->priv->list_store = gtk_list_store_new (
        EXCHANGE_PERMISSIONS_DIALOG_NUM_COLUMNS,
        G_TYPE_STRING, G_TYPE_STRING, E2K_TYPE_SID);
    gtk_tree_view_set_model (dialog->priv->list_view,
                 GTK_TREE_MODEL (dialog->priv->list_store));

    button = glade_xml_get_widget (xml, "add_button");
    g_signal_connect (button, "clicked",
              G_CALLBACK (add_clicked), dialog);
    button = glade_xml_get_widget (xml, "remove_button");
    g_signal_connect (button, "clicked",
              G_CALLBACK (remove_clicked), dialog);

    GET_WIDGET (role_optionmenu, GTK_COMBO_BOX);
    g_signal_connect (dialog->priv->role_optionmenu, "changed",
              G_CALLBACK (role_changed), dialog);

    dialog->priv->custom_added = FALSE;

#define GET_TOGGLE(name, value, callback) \
    GET_WIDGET (name, GTK_TOGGLE_BUTTON); \
    g_object_set_data (G_OBJECT (dialog->priv->name), \
               "mapi_permission", \
               GUINT_TO_POINTER (value)); \
    g_signal_connect (dialog->priv->name, "toggled", \
              G_CALLBACK (callback), dialog)

#define GET_CHECK(name, value) \
    GET_TOGGLE (name, value, check_toggled)

#define GET_RADIO(name, value, mask) \
    GET_TOGGLE (name, value, radio_toggled); \
    g_object_set_data (G_OBJECT (dialog->priv->name), \
               "mapi_mask", \
               GUINT_TO_POINTER (mask))

    GET_CHECK (read_items_check, E2K_PERMISSION_READ_ANY);
    GET_CHECK (create_items_check, E2K_PERMISSION_CREATE);
    GET_RADIO (edit_none_radio, 0, E2K_PERMISSION_EDIT_MASK);
    GET_RADIO (delete_none_radio, 0, E2K_PERMISSION_DELETE_MASK);
    GET_RADIO (edit_own_radio, E2K_PERMISSION_EDIT_OWNED, E2K_PERMISSION_EDIT_MASK);
    GET_RADIO (delete_own_radio, E2K_PERMISSION_DELETE_OWNED, E2K_PERMISSION_DELETE_MASK);
    GET_RADIO (edit_all_radio, (E2K_PERMISSION_EDIT_ANY | E2K_PERMISSION_EDIT_OWNED), E2K_PERMISSION_EDIT_MASK);
    GET_RADIO (delete_all_radio, (E2K_PERMISSION_DELETE_ANY | E2K_PERMISSION_DELETE_OWNED), E2K_PERMISSION_DELETE_MASK);
    GET_CHECK (create_subfolders_check, E2K_PERMISSION_CREATE_SUBFOLDER);
    GET_CHECK (folder_owner_check, E2K_PERMISSION_OWNER);
    GET_CHECK (folder_contact_check, E2K_PERMISSION_CONTACT);
    GET_CHECK (folder_visible_check, E2K_PERMISSION_FOLDER_VISIBLE);

    g_signal_connect (dialog->priv->folder_visible_check,
              "toggled", G_CALLBACK (rv_toggle), dialog);
    g_signal_connect (dialog->priv->read_items_check,
              "toggled", G_CALLBACK (rv_toggle), dialog);
}

GtkWidget *exchange_permissions_role_optionmenu_new (char *widget_name, char *string1, char *string2, int int1, int int2);

GtkWidget *
exchange_permissions_role_optionmenu_new (char *widget_name, char *string1, char *string2, int int1, int int2)
{
    GtkWidget *menu;
    const char **roles;
    int role;

    menu = gtk_combo_box_new_text ();
    roles = g_new (const char *, E2K_PERMISSIONS_ROLE_NUM_ROLES + 1);
    for (role = 0; role < E2K_PERMISSIONS_ROLE_NUM_ROLES; role++) {
        roles[role] = e2k_permissions_role_get_name (role);
        gtk_combo_box_append_text (GTK_COMBO_BOX (menu), roles[role]);
    }
        
    roles[role] = NULL;

    g_free (roles);

    gtk_widget_show (menu);
    return menu;
}