aclentrydialog.cpp 6 KB
Newer Older
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
/*
 * Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
 * Copyright (c) 2010 Tobias Koenig <tokoe@kdab.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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.
 */

#include "aclentrydialog_p.h"
#include "aclutils_p.h"

#include "libkdepim/addresseelineedit.h"

#include <Akonadi/Contact/EmailAddressSelectionDialog>

#include <KLocalizedString>

#include <QButtonGroup>
#include <QGridLayout>
#include <QGroupBox>
#include <QLabel>
#include <QPushButton>
#include <QRadioButton>
#include <QVBoxLayout>
#include <KConfigGroup>
#include <QDialogButtonBox>

using namespace PimCommon;

class AclEntryDialog::Private
{
public:
    Private(AclEntryDialog *qq)
        : q(qq),
          mButtonGroup(Q_NULLPTR),
          mUserIdLineEdit(Q_NULLPTR),
          mButtonLayout(Q_NULLPTR),
          mOkButton(Q_NULLPTR)
    {
    }

    void slotChanged();
    void slotSelectAddresses();

    AclEntryDialog *q;
    QButtonGroup *mButtonGroup;
    KPIM::AddresseeLineEdit *mUserIdLineEdit;
    QVBoxLayout *mButtonLayout;
    KIMAP::Acl::Rights mCustomPermissions;
    QPushButton *mOkButton;
};

void AclEntryDialog::Private::slotChanged()
{
    mOkButton->setEnabled(!mUserIdLineEdit->text().trimmed().isEmpty() && mButtonGroup->checkedButton() != Q_NULLPTR);
}

void AclEntryDialog::Private::slotSelectAddresses()
{
    Akonadi::EmailAddressSelectionDialog dlg;

    if (!dlg.exec()) {
        return;
    }

    const QString text = !dlg.selectedAddresses().isEmpty() ?
                         dlg.selectedAddresses().at(0).quotedEmail() :
                         QString();

    mUserIdLineEdit->setText(text);
}

AclEntryDialog::AclEntryDialog(QWidget *parent)
    : QDialog(parent), d(new Private(this))
{
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    QVBoxLayout *mainLayout = new QVBoxLayout;
    setLayout(mainLayout);
    d->mOkButton = buttonBox->button(QDialogButtonBox::Ok);
    d->mOkButton->setDefault(true);
    d->mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &AclEntryDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &AclEntryDialog::reject);

    QWidget *page = new QWidget(this);
    mainLayout->addWidget(page);
    mainLayout->addWidget(buttonBox);

    QGridLayout *layout = new QGridLayout(page);
    layout->setMargin(0);

    QLabel *label = new QLabel(i18n("&User identifier:"), page);
    layout->addWidget(label, 0, 0);

    d->mUserIdLineEdit = new KPIM::AddresseeLineEdit(page);
    layout->addWidget(d->mUserIdLineEdit, 0, 1);
    label->setBuddy(d->mUserIdLineEdit);
    d->mUserIdLineEdit->setWhatsThis(
        i18nc("@info:whatsthis",
              "The User Identifier is the login of the user on the IMAP server. "
              "This can be a simple user name or the full email address of the user; "
              "the login for your own account on the server will tell you which one it is."));

    QPushButton *button = new QPushButton(i18nc("select an email address", "Se&lect..."), page);
    layout->addWidget(button, 0, 2);

    QGroupBox *groupBox = new QGroupBox(i18n("Permissions"), page);
    d->mButtonLayout = new QVBoxLayout(groupBox);

    d->mButtonGroup = new QButtonGroup(groupBox);

    for (unsigned int i = 0; i < AclUtils::standardPermissionsCount(); ++i) {
        const KIMAP::Acl::Rights permissions = AclUtils::permissionsForIndex(i);

        QRadioButton *radioButton =
            new QRadioButton(AclUtils::permissionsToUserString(permissions), groupBox);
        d->mButtonLayout->addWidget(radioButton);
        d->mButtonGroup->addButton(radioButton, permissions);
    }

    d->mButtonLayout->addStretch(1);
    layout->addWidget(groupBox, 1, 0, 1, 3);

    label =
        new QLabel(
        i18n("<b>Note: </b>Renaming requires write permissions on the parent folder."), page);
    layout->addWidget(label, 2, 0, 1, 3);
    layout->setRowStretch(2, 10);

    connect(d->mUserIdLineEdit, SIGNAL(textChanged(QString)), SLOT(slotChanged()));
    connect(button, SIGNAL(clicked()), SLOT(slotSelectAddresses()));
    connect(d->mButtonGroup, SIGNAL(buttonClicked(int)), SLOT(slotChanged()));

    d->mOkButton->setEnabled(false);

    d->mUserIdLineEdit->setFocus();

}

AclEntryDialog::~AclEntryDialog()
{
    delete d;
}

void AclEntryDialog::setUserId(const QString &userId)
{
    d->mUserIdLineEdit->setText(userId);

    d->mOkButton->setEnabled(!userId.isEmpty());
}

QString AclEntryDialog::userId() const
{
    return d->mUserIdLineEdit->text();
}

void AclEntryDialog::setPermissions(KIMAP::Acl::Rights permissions)
{
    QAbstractButton *button = d->mButtonGroup->button(KIMAP::Acl::normalizedRights(permissions));

    if (button) {
        button->setChecked(true);
    } else {
        QRadioButton *radioButton =
            new QRadioButton(AclUtils::permissionsToUserString(permissions));

        d->mButtonLayout->addWidget(radioButton);
        d->mButtonGroup->addButton(radioButton, permissions);
    }

    d->mCustomPermissions = permissions;
}

KIMAP::Acl::Rights AclEntryDialog::permissions() const
{
    QAbstractButton *button = d->mButtonGroup->checkedButton();

    if (!button) {
        return d->mCustomPermissions;
    }

    return KIMAP::Acl::denormalizedRights(
               static_cast<KIMAP::Acl::Rights>(d->mButtonGroup->id(button)));
}

#include "moc_aclentrydialog_p.cpp"