ldapconfigwidget.cpp 24.6 KB
Newer Older
1
/*
Allen Winter's avatar
Allen Winter committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  This file is part of libkldap.
  Copyright (c) 2004-2006 Szombathelyi György <gyurco@freemail.hu>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library 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
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA 02110-1301, USA.
19
20
*/

Allen Winter's avatar
Allen Winter committed
21
22
#include "ldapconfigwidget.h"
#include "ldapsearch.h"
23

Laurent Montel's avatar
Laurent Montel committed
24
#include <QProgressDialog>
Laurent Montel's avatar
Laurent Montel committed
25
#include <QComboBox>
Laurent Montel's avatar
Laurent Montel committed
26
#include "ldap_debug.h"
27
#include <KLocalizedString>
28
#include <kmessagebox.h>
Laurent Montel's avatar
Laurent Montel committed
29
#include <KPasswordLineEdit>
30

Laurent Montel's avatar
Laurent Montel committed
31
#include <QObject>
32
33
34
35
36
37
#include <QCheckBox>
#include <QGroupBox>
#include <QLabel>
#include <QPushButton>
#include <QRadioButton>
#include <QSpinBox>
Laurent Montel's avatar
Laurent Montel committed
38
#include <QGridLayout>
39
40
using namespace KLDAP;

41
class Q_DECL_HIDDEN LdapConfigWidget::Private
42
{
Laurent Montel's avatar
Laurent Montel committed
43
44
public:
    Private(LdapConfigWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
45
        : mParent(parent)
Tobias Koenig's avatar
Tobias Koenig committed
46
    {
Laurent Montel's avatar
Laurent Montel committed
47
        mainLayout = new QGridLayout(mParent);
Laurent Montel's avatar
Laurent Montel committed
48
        mainLayout->setContentsMargins(0, 0, 0, 0);
Tobias Koenig's avatar
Tobias Koenig committed
49
    }
50

Tobias Koenig's avatar
Tobias Koenig committed
51
52
    void setLDAPPort();
    void setLDAPSPort();
Laurent Montel's avatar
Laurent Montel committed
53
54
55
    void setAnonymous(bool on);
    void setSimple(bool on);
    void setSASL(bool on);
Tobias Koenig's avatar
Tobias Koenig committed
56
57
    void queryDNClicked();
    void queryMechClicked();
Laurent Montel's avatar
Laurent Montel committed
58
59
    void loadData(LdapSearch *search, const LdapObject &object);
    void loadResult(LdapSearch *search);
Tobias Koenig's avatar
Tobias Koenig committed
60
61
62
    void sendQuery();
    void initWidget();

Laurent Montel's avatar
Laurent Montel committed
63
    LdapConfigWidget *mParent = nullptr;
Laurent Montel's avatar
Laurent Montel committed
64
    WinFlags mFeatures = W_ALL;
Tobias Koenig's avatar
Tobias Koenig committed
65
66
67
    QStringList mQResult;
    QString mAttr;

Laurent Montel's avatar
Laurent Montel committed
68
69
70
    QLineEdit *mUser = nullptr;
    KPasswordLineEdit *mPassword = nullptr;
    QLineEdit *mHost = nullptr;
Laurent Montel's avatar
Laurent Montel committed
71
    QSpinBox *mPort = nullptr;
Laurent Montel's avatar
Laurent Montel committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    QSpinBox *mVersion = nullptr;
    QSpinBox *mSizeLimit = nullptr;
    QSpinBox *mTimeLimit = nullptr;
    QSpinBox *mPageSize = nullptr;
    QLineEdit *mDn = nullptr;
    QLineEdit *mBindDn = nullptr;
    QLineEdit *mRealm = nullptr;
    QLineEdit *mFilter = nullptr;
    QRadioButton *mAnonymous = nullptr;
    QRadioButton *mSimple = nullptr;
    QRadioButton *mSASL = nullptr;
    QCheckBox *mSubTree = nullptr;
    QPushButton *mEditButton = nullptr;
    QPushButton *mQueryMech = nullptr;
    QRadioButton *mSecNo = nullptr;
    QRadioButton *mSecTLS = nullptr;
    QRadioButton *mSecSSL = nullptr;
Laurent Montel's avatar
Laurent Montel committed
89
    QComboBox *mMech = nullptr;
Tobias Koenig's avatar
Tobias Koenig committed
90

Laurent Montel's avatar
Laurent Montel committed
91
92
    bool mCancelled = false;
    QProgressDialog *mProg = nullptr;
Tobias Koenig's avatar
Tobias Koenig committed
93

Laurent Montel's avatar
Laurent Montel committed
94
    QGridLayout *mainLayout = nullptr;
Tobias Koenig's avatar
Tobias Koenig committed
95
96
97
};

void LdapConfigWidget::Private::initWidget()
98
{
Laurent Montel's avatar
Laurent Montel committed
99
    QLabel *label = nullptr;
Laurent Montel's avatar
Laurent Montel committed
100

Laurent Montel's avatar
Laurent Montel committed
101
102
    mUser = mHost = mDn = mBindDn = mRealm = mFilter = nullptr;
    mPassword = nullptr;
Laurent Montel's avatar
Laurent Montel committed
103
104
    mPort = mVersion = mTimeLimit = mSizeLimit = nullptr;
    mAnonymous = mSimple = mSASL = mSecNo = mSecTLS = mSecSSL = nullptr;
Laurent Montel's avatar
Laurent Montel committed
105
    mEditButton = mQueryMech = nullptr;
Laurent Montel's avatar
Laurent Montel committed
106
107
    mPageSize = nullptr;
    mMech = nullptr;
Laurent Montel's avatar
Laurent Montel committed
108
109
110
111
112
113
    int row = 0;
    int col;

    if (mFeatures & W_USER) {
        label = new QLabel(i18n("User:"), mParent);
        mUser = new QLineEdit(mParent);
114
        mUser->setObjectName(QStringLiteral("kcfg_ldapuser"));
Laurent Montel's avatar
Laurent Montel committed
115
116
117
118
119
120
121
122
123

        mainLayout->addWidget(label, row, 0);
        mainLayout->addWidget(mUser, row, 1, 1, 3);
        row++;
    }

    if (mFeatures & W_BINDDN) {
        label = new QLabel(i18n("Bind DN:"), mParent);
        mBindDn = new QLineEdit(mParent);
124
        mBindDn->setObjectName(QStringLiteral("kcfg_ldapbinddn"));
Laurent Montel's avatar
Laurent Montel committed
125
126
127
128
129
130
131
132
133

        mainLayout->addWidget(label, row, 0);
        mainLayout->addWidget(mBindDn, row, 1, 1, 3);
        row++;
    }

    if (mFeatures & W_REALM) {
        label = new QLabel(i18n("Realm:"), mParent);
        mRealm = new QLineEdit(mParent);
134
        mRealm->setObjectName(QStringLiteral("kcfg_ldaprealm"));
Laurent Montel's avatar
Laurent Montel committed
135
136
137
138
139
140
141
142

        mainLayout->addWidget(label, row, 0);
        mainLayout->addWidget(mRealm, row, 1, 1, 3);
        row++;
    }

    if (mFeatures & W_PASS) {
        label = new QLabel(i18n("Password:"), mParent);
Laurent Montel's avatar
Laurent Montel committed
143
        mPassword = new KPasswordLineEdit(mParent);
144
        mPassword->setObjectName(QStringLiteral("kcfg_ldappassword"));
Laurent Montel's avatar
Laurent Montel committed
145
146
147
148
149
150
151
152
153

        mainLayout->addWidget(label, row, 0);
        mainLayout->addWidget(mPassword, row, 1, 1, 3);
        row++;
    }

    if (mFeatures & W_HOST) {
        label = new QLabel(i18n("Host:"), mParent);
        mHost = new QLineEdit(mParent);
154
        mHost->setObjectName(QStringLiteral("kcfg_ldaphost"));
155
        mParent->connect(mHost, &QLineEdit::textChanged, mParent, &LdapConfigWidget::hostNameChanged);
Laurent Montel's avatar
Laurent Montel committed
156
157
158
159
160
161
162
163
164
165
        mainLayout->addWidget(label, row, 0);
        mainLayout->addWidget(mHost, row, 1, 1, 3);
        row++;
    }

    col = 0;
    if (mFeatures & W_PORT) {
        label = new QLabel(i18n("Port:"), mParent);
        mPort = new QSpinBox(mParent);
        mPort->setRange(0, 65535);
166
        mPort->setObjectName(QStringLiteral("kcfg_ldapport"));
Laurent Montel's avatar
Laurent Montel committed
167
168
169
170
171
172
173
174
175
176
177
178
        mPort->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred));
        mPort->setValue(389);

        mainLayout->addWidget(label, row, col);
        mainLayout->addWidget(mPort, row, col + 1);
        col += 2;
    }

    if (mFeatures & W_VER) {
        label = new QLabel(i18n("LDAP version:"), mParent);
        mVersion = new QSpinBox(mParent);
        mVersion->setRange(2, 3);
179
        mVersion->setObjectName(QStringLiteral("kcfg_ldapver"));
Laurent Montel's avatar
Laurent Montel committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
        mVersion->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred));
        mVersion->setValue(3);
        mainLayout->addWidget(label, row, col);
        mainLayout->addWidget(mVersion, row, col + 1);
    }
    if (mFeatures & (W_PORT | W_VER)) {
        row++;
    }

    col = 0;
    if (mFeatures & W_SIZELIMIT) {
        label = new QLabel(i18n("Size limit:"), mParent);
        mSizeLimit = new QSpinBox(mParent);
        mSizeLimit->setRange(0, 9999999);
194
        mSizeLimit->setObjectName(QStringLiteral("kcfg_ldapsizelimit"));
Laurent Montel's avatar
Laurent Montel committed
195
196
197
198
199
200
201
202
203
204
205
206
        mSizeLimit->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred));
        mSizeLimit->setValue(0);
        mSizeLimit->setSpecialValueText(i18nc("default ldap size limit", "Default"));
        mainLayout->addWidget(label, row, col);
        mainLayout->addWidget(mSizeLimit, row, col + 1);
        col += 2;
    }

    if (mFeatures & W_TIMELIMIT) {
        label = new QLabel(i18n("Time limit:"), mParent);
        mTimeLimit = new QSpinBox(mParent);
        mTimeLimit->setRange(0, 9999999);
207
        mTimeLimit->setObjectName(QStringLiteral("kcfg_ldaptimelimit"));
Laurent Montel's avatar
Laurent Montel committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
        mTimeLimit->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred));
        mTimeLimit->setValue(0);
        mTimeLimit->setSuffix(i18n(" sec"));
        mTimeLimit->setSpecialValueText(i18nc("default ldap time limit", "Default"));
        mainLayout->addWidget(label, row, col);
        mainLayout->addWidget(mTimeLimit, row, col + 1);
    }
    if (mFeatures & (W_SIZELIMIT | W_TIMELIMIT)) {
        row++;
    }

    if (mFeatures & W_PAGESIZE) {
        label = new QLabel(i18n("Page size:"), mParent);
        mPageSize = new QSpinBox(mParent);
        mPageSize->setRange(0, 9999999);
223
        mPageSize->setObjectName(QStringLiteral("kcfg_ldappagesize"));
Laurent Montel's avatar
Laurent Montel committed
224
225
226
227
228
229
230
231
232
233
        mPageSize->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred));
        mPageSize->setValue(0);
        mPageSize->setSpecialValueText(i18n("No paging"));
        mainLayout->addWidget(label, row, 0);
        mainLayout->addWidget(mPageSize, row++, 1);
    }

    if (mFeatures & W_DN) {
        label = new QLabel(i18nc("Distinguished Name", "DN:"), mParent);
        mDn = new QLineEdit(mParent);
234
        mDn->setObjectName(QStringLiteral("kcfg_ldapdn"));
Laurent Montel's avatar
Laurent Montel committed
235
236
237
238
239
240

        mainLayout->addWidget(label, row, 0);
        mainLayout->addWidget(mDn, row, 1, 1, 1);
        //without host query doesn't make sense
        if (mHost) {
            QPushButton *dnquery = new QPushButton(i18n("Query Server"), mParent);
241
            dnquery->setEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
242
243
244
            connect(dnquery, &QPushButton::clicked, mParent, [this]() {
                queryDNClicked();
            });
245
246
247
            connect(mDn, &QLineEdit::textChanged, mParent, [dnquery](const QString &text) {
                dnquery->setEnabled(!text.trimmed().isEmpty());
            });
Laurent Montel's avatar
Laurent Montel committed
248
249
250
251
252
253
254
255
            mainLayout->addWidget(dnquery, row, 2, 1, 1);
        }
        row++;
    }

    if (mFeatures & W_FILTER) {
        label = new QLabel(i18n("Filter:"), mParent);
        mFilter = new QLineEdit(mParent);
256
        mFilter->setObjectName(QStringLiteral("kcfg_ldapfilter"));
Laurent Montel's avatar
Laurent Montel committed
257
258
259
260
261
262
263
264
265
266
267

        mainLayout->addWidget(label, row, 0);
        mainLayout->addWidget(mFilter, row, 1, 1, 3);
        row++;
    }

    if (mFeatures & W_SECBOX) {
        QGroupBox *btgroup = new QGroupBox(i18n("Security"), mParent);
        QHBoxLayout *hbox = new QHBoxLayout;
        btgroup->setLayout(hbox);
        mSecNo = new QRadioButton(i18nc("@option:radio set no security", "No"), btgroup);
268
        mSecNo->setObjectName(QStringLiteral("kcfg_ldapnosec"));
Laurent Montel's avatar
Laurent Montel committed
269
270
        hbox->addWidget(mSecNo);
        mSecTLS = new QRadioButton(i18nc("@option:radio use TLS security", "TLS"), btgroup);
271
        mSecTLS->setObjectName(QStringLiteral("kcfg_ldaptls"));
Laurent Montel's avatar
Laurent Montel committed
272
273
        hbox->addWidget(mSecTLS);
        mSecSSL = new QRadioButton(i18nc("@option:radio use SSL security", "SSL"), btgroup);
274
        mSecSSL->setObjectName(QStringLiteral("kcfg_ldapssl"));
Laurent Montel's avatar
Laurent Montel committed
275
276
277
        hbox->addWidget(mSecSSL);
        mainLayout->addWidget(btgroup, row, 0, 1, 4);

Laurent Montel's avatar
Laurent Montel committed
278
279
280
281
282
283
284
285
286
        connect(mSecNo, &QRadioButton::clicked, mParent, [this]() {
            setLDAPPort();
        });
        connect(mSecTLS, &QRadioButton::clicked, mParent, [this]() {
            setLDAPPort();
        });
        connect(mSecSSL, &QRadioButton::clicked, mParent, [this]() {
            setLDAPSPort();
        });
Laurent Montel's avatar
Laurent Montel committed
287
288
289
290
291
292

        mSecNo->setChecked(true);
        row++;
    }

    if (mFeatures & W_AUTHBOX) {
Laurent Montel's avatar
Laurent Montel committed
293
294
        QGroupBox *authbox
            = new QGroupBox(i18n("Authentication"), mParent);
Laurent Montel's avatar
Laurent Montel committed
295
296
297
298
299
        QVBoxLayout *vbox = new QVBoxLayout;
        authbox->setLayout(vbox);
        QHBoxLayout *hbox = new QHBoxLayout;
        vbox->addLayout(hbox);

Laurent Montel's avatar
Laurent Montel committed
300
301
        mAnonymous
            = new QRadioButton(i18nc("@option:radio anonymous authentication", "Anonymous"), authbox);
302
        mAnonymous->setObjectName(QStringLiteral("kcfg_ldapanon"));
Laurent Montel's avatar
Laurent Montel committed
303
        hbox->addWidget(mAnonymous);
Laurent Montel's avatar
Laurent Montel committed
304
305
        mSimple
            = new QRadioButton(i18nc("@option:radio simple authentication", "Simple"), authbox);
306
        mSimple->setObjectName(QStringLiteral("kcfg_ldapsimple"));
Laurent Montel's avatar
Laurent Montel committed
307
        hbox->addWidget(mSimple);
Laurent Montel's avatar
Laurent Montel committed
308
309
        mSASL
            = new QRadioButton(i18nc("@option:radio SASL authentication", "SASL"), authbox);
310
        mSASL->setObjectName(QStringLiteral("kcfg_ldapsasl"));
Laurent Montel's avatar
Laurent Montel committed
311
312
313
314
315
316
        hbox->addWidget(mSASL);

        hbox = new QHBoxLayout;
        vbox->addLayout(hbox);
        label = new QLabel(i18n("SASL mechanism:"), authbox);
        hbox->addWidget(label);
Laurent Montel's avatar
Laurent Montel committed
317
        mMech = new QComboBox(authbox);
318
319
320
321
        mMech->setObjectName(QStringLiteral("kcfg_ldapsaslmech"));
        mMech->addItem(QStringLiteral("DIGEST-MD5"));
        mMech->addItem(QStringLiteral("GSSAPI"));
        mMech->addItem(QStringLiteral("PLAIN"));
Laurent Montel's avatar
Laurent Montel committed
322
323
324
325
326
327
        hbox->addWidget(mMech);

        //without host query doesn't make sense
        if (mHost) {
            mQueryMech = new QPushButton(i18n("Query Server"), authbox);
            hbox->addWidget(mQueryMech);
Laurent Montel's avatar
Laurent Montel committed
328
329
330
            connect(mQueryMech, &QPushButton::clicked, mParent, [this]() {
                queryMechClicked();
            });
Laurent Montel's avatar
Laurent Montel committed
331
332
333
334
        }

        mainLayout->addWidget(authbox, row, 0, 2, 4);

Laurent Montel's avatar
Laurent Montel committed
335
336
337
338
339
340
341
342
343
        connect(mAnonymous, &QRadioButton::toggled, mParent, [this](bool b) {
            setAnonymous(b);
        });
        connect(mSimple, &QRadioButton::toggled, mParent, [this](bool b) {
            setSimple(b);
        });
        connect(mSASL, &QRadioButton::toggled, mParent, [this](bool b) {
            setSASL(b);
        });
Laurent Montel's avatar
Laurent Montel committed
344
345
346

        mAnonymous->setChecked(true);
    }
347
348
}

Tobias Koenig's avatar
Tobias Koenig committed
349
void LdapConfigWidget::Private::sendQuery()
350
{
Laurent Montel's avatar
Laurent Montel committed
351
352
353
354
355
356
357
358
359
360
361
    LdapServer _server(mParent->server());

    mQResult.clear();
    mCancelled = true;

    if (mAttr == QLatin1String("supportedsaslmechanisms")) {
        _server.setAuth(LdapServer::Anonymous);
    }

    LdapUrl _url(_server.url());

Laurent Montel's avatar
Laurent Montel committed
362
    _url.setDn(LdapDN(QLatin1String("")));
Laurent Montel's avatar
Laurent Montel committed
363
364
365
    _url.setAttributes(QStringList(mAttr));
    _url.setScope(LdapUrl::Base);

Laurent Montel's avatar
Laurent Montel committed
366
    qCDebug(LDAP_LOG) << "sendQuery url:" << _url.toDisplayString();
Laurent Montel's avatar
Laurent Montel committed
367
368

    LdapSearch search;
Laurent Montel's avatar
Laurent Montel committed
369
370
371
372
373
374
    connect(&search, &LdapSearch::data, mParent, [this](KLDAP::LdapSearch *s, const KLDAP::LdapObject &obj) {
        loadData(s, obj);
    });
    connect(&search, &LdapSearch::result, mParent, [this](KLDAP::LdapSearch *s) {
        loadResult(s);
    });
Laurent Montel's avatar
Laurent Montel committed
375
376

    if (!search.search(_url)) {
377
        KMessageBox::error(mParent, search.errorString(), i18n("Check server"));
Laurent Montel's avatar
Laurent Montel committed
378
379
380
        return;
    }

Laurent Montel's avatar
Laurent Montel committed
381
    if (mProg == nullptr) {
Laurent Montel's avatar
Laurent Montel committed
382
        mProg = new QProgressDialog(mParent);
383
        mProg->setWindowTitle(i18nc("@title:window", "LDAP Query"));
Laurent Montel's avatar
Laurent Montel committed
384
385
386
387
388
389
390
391
        mProg->setModal(true);
    }
    mProg->setLabelText(_url.toDisplayString());
    mProg->setMaximum(1);
    mProg->setMinimum(0);
    mProg->setValue(0);
    mProg->exec();
    if (mCancelled) {
Laurent Montel's avatar
Laurent Montel committed
392
        qCDebug(LDAP_LOG) << "query canceled!";
Laurent Montel's avatar
Laurent Montel committed
393
394
395
396
397
398
399
400
401
402
        search.abandon();
    } else {
        if (search.error()) {
            if (search.errorString().isEmpty()) {
                KMessageBox::error(mParent, i18nc("%1 is a url to ldap server", "Unknown error connecting %1", _url.toDisplayString()));
            } else {
                KMessageBox::error(mParent, search.errorString());
            }
        }
    }
403
404
}

Tobias Koenig's avatar
Tobias Koenig committed
405
void LdapConfigWidget::Private::queryMechClicked()
406
{
407
    mAttr = QStringLiteral("supportedsaslmechanisms");
Laurent Montel's avatar
Laurent Montel committed
408
409
410
411
412
413
    sendQuery();
    if (!mQResult.isEmpty()) {
        mQResult.sort();
        mMech->clear();
        mMech->addItems(mQResult);
    }
414
415
}

Tobias Koenig's avatar
Tobias Koenig committed
416
void LdapConfigWidget::Private::queryDNClicked()
417
{
418
    mAttr = QStringLiteral("namingcontexts");
Laurent Montel's avatar
Laurent Montel committed
419
420
    sendQuery();
    if (!mQResult.isEmpty()) {
421
        mDn->setText(mQResult.constFirst());
Laurent Montel's avatar
Laurent Montel committed
422
423
424
425
426
    }
}

void LdapConfigWidget::Private::loadData(LdapSearch *, const LdapObject &object)
{
Laurent Montel's avatar
Laurent Montel committed
427
    qCDebug(LDAP_LOG) << "object:" << object.toString();
Laurent Montel's avatar
Laurent Montel committed
428
429
430
    mProg->setValue(mProg->value() + 1);
    LdapAttrMap::ConstIterator end(object.attributes().constEnd());
    for (LdapAttrMap::ConstIterator it = object.attributes().constBegin();
Laurent Montel's avatar
Laurent Montel committed
431
         it != end; ++it) {
Laurent Montel's avatar
Laurent Montel committed
432
433
        LdapAttrValue::ConstIterator end2((*it).constEnd());
        for (LdapAttrValue::ConstIterator it2 = (*it).constBegin();
Laurent Montel's avatar
Laurent Montel committed
434
             it2 != end2; ++it2) {
Laurent Montel's avatar
Laurent Montel committed
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
            mQResult.push_back(QString::fromUtf8(*it2));
        }
    }
}

void LdapConfigWidget::Private::loadResult(LdapSearch *search)
{
    Q_UNUSED(search);
    mCancelled = false;
    mProg->close();
}

void LdapConfigWidget::Private::setAnonymous(bool on)
{
    if (!on) {
        return;
    }
    if (mUser) {
        mUser->setEnabled(false);
    }
    if (mPassword) {
        mPassword->setEnabled(false);
    }
    if (mBindDn) {
        mBindDn->setEnabled(false);
    }
    if (mRealm) {
        mRealm->setEnabled(false);
    }
    if (mMech) {
        mMech->setEnabled(false);
    }
    if (mQueryMech) {
        mQueryMech->setEnabled(false);
    }
}

void LdapConfigWidget::Private::setSimple(bool on)
{
    if (!on) {
        return;
    }
    if (mUser) {
        mUser->setEnabled(false);
    }
    if (mPassword) {
        mPassword->setEnabled(true);
    }
    if (mBindDn) {
        mBindDn->setEnabled(true);
    }
    if (mRealm) {
        mRealm->setEnabled(false);
    }
    if (mMech) {
        mMech->setEnabled(false);
    }
    if (mQueryMech) {
        mQueryMech->setEnabled(false);
    }
}

void LdapConfigWidget::Private::setSASL(bool on)
{
    if (!on) {
        return;
    }
    if (mUser) {
        mUser->setEnabled(true);
    }
    if (mPassword) {
        mPassword->setEnabled(true);
    }
    if (mBindDn) {
        mBindDn->setEnabled(true);
    }
    if (mRealm) {
        mRealm->setEnabled(true);
    }
    if (mMech) {
        mMech->setEnabled(true);
    }
    if (mQueryMech) {
        mQueryMech->setEnabled(true);
    }
520
521
}

Tobias Koenig's avatar
Tobias Koenig committed
522
void LdapConfigWidget::Private::setLDAPPort()
523
{
Laurent Montel's avatar
Laurent Montel committed
524
525
526
    if (mPort) {
        mPort->setValue(389);
    }
527
528
}

Tobias Koenig's avatar
Tobias Koenig committed
529
void LdapConfigWidget::Private::setLDAPSPort()
530
{
Laurent Montel's avatar
Laurent Montel committed
531
532
533
    if (mPort) {
        mPort->setValue(636);
    }
534
535
}

Laurent Montel's avatar
Laurent Montel committed
536
LdapConfigWidget::LdapConfigWidget(QWidget *parent, Qt::WindowFlags fl)
Laurent Montel's avatar
Laurent Montel committed
537
538
    : QWidget(parent, fl)
    , d(new Private(this))
Tobias Koenig's avatar
Tobias Koenig committed
539
540
541
{
}

Laurent Montel's avatar
Laurent Montel committed
542
543
544
LdapConfigWidget::LdapConfigWidget(LdapConfigWidget::WinFlags flags, QWidget *parent, Qt::WindowFlags fl)
    : QWidget(parent, fl)
    , d(new Private(this))
Tobias Koenig's avatar
Tobias Koenig committed
545
{
Laurent Montel's avatar
Laurent Montel committed
546
    d->mFeatures = flags;
Tobias Koenig's avatar
Tobias Koenig committed
547

Laurent Montel's avatar
Laurent Montel committed
548
    d->initWidget();
Tobias Koenig's avatar
Tobias Koenig committed
549
550
551
552
}

LdapConfigWidget::~LdapConfigWidget()
{
Laurent Montel's avatar
Laurent Montel committed
553
    delete d;
Tobias Koenig's avatar
Tobias Koenig committed
554
555
}

556
557
LdapUrl LdapConfigWidget::url() const
{
Laurent Montel's avatar
Laurent Montel committed
558
    return server().url();
Szombathelyi György's avatar
Szombathelyi György committed
559
}
560

Laurent Montel's avatar
Laurent Montel committed
561
void LdapConfigWidget::setUrl(const LdapUrl &url)
Szombathelyi György's avatar
Szombathelyi György committed
562
{
Laurent Montel's avatar
Laurent Montel committed
563
564
565
    LdapServer _server;
    _server.setUrl(url);
    setServer(_server);
Szombathelyi György's avatar
Szombathelyi György committed
566
567
568
569
}

LdapServer LdapConfigWidget::server() const
{
Laurent Montel's avatar
Laurent Montel committed
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
    LdapServer _server;
    if (d->mSecSSL && d->mSecSSL->isChecked()) {
        _server.setSecurity(LdapServer::SSL);
    } else if (d->mSecTLS && d->mSecTLS->isChecked()) {
        _server.setSecurity(LdapServer::TLS);
    } else {
        _server.setSecurity(LdapServer::None);
    }

    if (d->mUser) {
        _server.setUser(d->mUser->text());
    }
    if (d->mBindDn) {
        _server.setBindDn(d->mBindDn->text());
    }
    if (d->mPassword) {
Laurent Montel's avatar
Laurent Montel committed
586
        _server.setPassword(d->mPassword->password());
Laurent Montel's avatar
Laurent Montel committed
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
    }
    if (d->mRealm) {
        _server.setRealm(d->mRealm->text());
    }
    if (d->mHost) {
        _server.setHost(d->mHost->text());
    }
    if (d->mPort) {
        _server.setPort(d->mPort->value());
    }
    if (d->mDn) {
        _server.setBaseDn(LdapDN(d->mDn->text()));
    }
    if (d->mFilter) {
        _server.setFilter(d->mFilter->text());
    }
    if (d->mVersion) {
        _server.setVersion(d->mVersion->value());
    }
    if (d->mSizeLimit && d->mSizeLimit->value() != 0) {
        _server.setSizeLimit(d->mSizeLimit->value());
    }
    if (d->mTimeLimit && d->mTimeLimit->value() != 0) {
        _server.setTimeLimit(d->mTimeLimit->value());
    }
    if (d->mPageSize && d->mPageSize->value() != 0) {
        _server.setPageSize(d->mPageSize->value());
    }
    if (d->mAnonymous && d->mAnonymous->isChecked()) {
        _server.setAuth(LdapServer::Anonymous);
    } else if (d->mSimple && d->mSimple->isChecked()) {
        _server.setAuth(LdapServer::Simple);
    } else if (d->mSASL && d->mSASL->isChecked()) {
        _server.setAuth(LdapServer::SASL);
        _server.setMech(d->mMech->currentText());
    }
    return _server;
}

void LdapConfigWidget::setServer(const LdapServer &server)
{
    switch (server.security()) {
    case LdapServer::SSL:
        if (d->mSecSSL) {
            d->mSecSSL->setChecked(true);
        }
        break;
    case LdapServer::TLS:
        if (d->mSecTLS) {
            d->mSecTLS->setChecked(true);
        }
        break;
    case LdapServer::None:
        if (d->mSecNo) {
            d->mSecNo->setChecked(true);
        }
        break;
    }

    switch (server.auth()) {
    case LdapServer::Anonymous:
        if (d->mAnonymous) {
            d->mAnonymous->setChecked(true);
        }
        break;
    case LdapServer::Simple:
        if (d->mSimple) {
            d->mSimple->setChecked(true);
        }
        break;
    case LdapServer::SASL:
        if (d->mSASL) {
            d->mSASL->setChecked(true);
        }
        break;
    }

    setUser(server.user());
    setBindDn(server.bindDn());
    setPassword(server.password());
    setRealm(server.realm());
    setHost(server.host());
    setPort(server.port());
    setFilter(server.filter());
    setDn(server.baseDn());
    setVersion(server.version());
    setSizeLimit(server.sizeLimit());
    setTimeLimit(server.timeLimit());
    setPageSize(server.pageSize());
    setMech(server.mech());
}

void LdapConfigWidget::setUser(const QString &user)
{
    if (d->mUser) {
        d->mUser->setText(user);
    }
684
685
686
687
}

QString LdapConfigWidget::user() const
{
Laurent Montel's avatar
Laurent Montel committed
688
    return d->mUser ? d->mUser->text() : QString();
689
690
}

Laurent Montel's avatar
Laurent Montel committed
691
void LdapConfigWidget::setPassword(const QString &password)
692
{
Laurent Montel's avatar
Laurent Montel committed
693
    if (d->mPassword) {
Laurent Montel's avatar
Laurent Montel committed
694
        d->mPassword->setPassword(password);
Laurent Montel's avatar
Laurent Montel committed
695
    }
696
697
698
699
}

QString LdapConfigWidget::password() const
{
Laurent Montel's avatar
Laurent Montel committed
700
    return d->mPassword ? d->mPassword->password() : QString();
701
702
}

Laurent Montel's avatar
Laurent Montel committed
703
void LdapConfigWidget::setBindDn(const QString &binddn)
704
{
Laurent Montel's avatar
Laurent Montel committed
705
706
707
    if (d->mBindDn) {
        d->mBindDn->setText(binddn);
    }
708
709
710
711
}

QString LdapConfigWidget::bindDn() const
{
Laurent Montel's avatar
Laurent Montel committed
712
    return d->mBindDn ? d->mBindDn->text() : QString();
713
714
}

Laurent Montel's avatar
Laurent Montel committed
715
void LdapConfigWidget::setRealm(const QString &realm)
716
{
Laurent Montel's avatar
Laurent Montel committed
717
718
719
    if (d->mRealm) {
        d->mRealm->setText(realm);
    }
720
721
722
723
}

QString LdapConfigWidget::realm() const
{
Laurent Montel's avatar
Laurent Montel committed
724
    return d->mRealm ? d->mRealm->text() : QString();
725
726
}

Laurent Montel's avatar
Laurent Montel committed
727
void LdapConfigWidget::setHost(const QString &host)
728
{
Laurent Montel's avatar
Laurent Montel committed
729
730
731
    if (d->mHost) {
        d->mHost->setText(host);
    }
732
733
734
735
}

QString LdapConfigWidget::host() const
{
Laurent Montel's avatar
Laurent Montel committed
736
    return d->mHost ? d->mHost->text() : QString();
737
738
}

Laurent Montel's avatar
Laurent Montel committed
739
void LdapConfigWidget::setPort(int port)
740
{
Laurent Montel's avatar
Laurent Montel committed
741
742
743
    if (d->mPort) {
        d->mPort->setValue(port);
    }
744
745
746
747
}

int LdapConfigWidget::port() const
{
Laurent Montel's avatar
Laurent Montel committed
748
    return d->mPort ? d->mPort->value() : 389;
749
750
}

Laurent Montel's avatar
Laurent Montel committed
751
void LdapConfigWidget::setVersion(int version)
752
{
Laurent Montel's avatar
Laurent Montel committed
753
754
755
    if (d->mVersion) {
        d->mVersion->setValue(version);
    }
756
757
758
759
}

int LdapConfigWidget::version() const
{
Laurent Montel's avatar
Laurent Montel committed
760
    return d->mVersion ? d->mVersion->value() : 3;
761
762
}

Laurent Montel's avatar
Laurent Montel committed
763
void LdapConfigWidget::setDn(const LdapDN &dn)
764
{
Laurent Montel's avatar
Laurent Montel committed
765
766
767
    if (d->mDn) {
        d->mDn->setText(dn.toString());
    }
768
769
}

770
LdapDN LdapConfigWidget::dn() const
771
{
Laurent Montel's avatar
Laurent Montel committed
772
    return d->mDn ? LdapDN(d->mDn->text()) : LdapDN();
773
774
}

Laurent Montel's avatar
Laurent Montel committed
775
void LdapConfigWidget::setFilter(const QString &filter)
776
{
Laurent Montel's avatar
Laurent Montel committed
777
778
779
    if (d->mFilter) {
        d->mFilter->setText(filter);
    }
780
781
782
783
}

QString LdapConfigWidget::filter() const
{
Laurent Montel's avatar
Laurent Montel committed
784
    return d->mFilter ? d->mFilter->text() : QString();
785
786
}

Laurent Montel's avatar
Laurent Montel committed
787
void LdapConfigWidget::setMech(const QString &mech)
788
{
Laurent Montel's avatar
Laurent Montel committed
789
    if (d->mMech == nullptr) {
Laurent Montel's avatar
Laurent Montel committed
790
        return;
791
    }
Laurent Montel's avatar
Laurent Montel committed
792
793
794
795
796
797
798
799
800
801
802
803
    if (!mech.isEmpty()) {
        int i = 0;
        while (i < d->mMech->count()) {
            if (d->mMech->itemText(i) == mech) {
                break;
            }
            i++;
        }
        if (i == d->mMech->count()) {
            d->mMech->addItem(mech);
        }
        d->mMech->setCurrentIndex(i);
Allen Winter's avatar
Allen Winter committed
804
    }
805
806
807
808
}

QString LdapConfigWidget::mech() const
{
Laurent Montel's avatar
Laurent Montel committed
809
    return d->mMech ? d->mMech->currentText() : QString();
810
811
}

Laurent Montel's avatar
Laurent Montel committed
812
void LdapConfigWidget::setSecurity(Security security)
813
{
Laurent Montel's avatar
Laurent Montel committed
814
815
816
817
818
819
820
821
822
823
824
    switch (security) {
    case None:
        d->mSecNo->setChecked(true);
        break;
    case SSL:
        d->mSecSSL->setChecked(true);
        break;
    case TLS:
        d->mSecTLS->setChecked(true);
        break;
    }
825
826
827
828
}

LdapConfigWidget::Security LdapConfigWidget::security() const
{
Laurent Montel's avatar
Laurent Montel committed
829
830
831
832
833
834
835
    if (d->mSecTLS->isChecked()) {
        return TLS;
    }
    if (d->mSecSSL->isChecked()) {
        return SSL;
    }
    return None;
836
837
}

Laurent Montel's avatar
Laurent Montel committed
838
void LdapConfigWidget::setAuth(Auth auth)
839
{
Laurent Montel's avatar
Laurent Montel committed
840
841
842
843
844
845
846
847
848
849
850
    switch (auth) {
    case Anonymous:
        d->mAnonymous->setChecked(true);
        break;
    case Simple:
        d->mSimple->setChecked(true);
        break;
    case SASL:
        d->mSASL->setChecked(true);
        break;
    }
851
852
853
854
}

LdapConfigWidget::Auth LdapConfigWidget::auth() const
{
Laurent Montel's avatar
Laurent Montel committed
855
856
857
858
859
860
861
    if (d->mSimple->isChecked()) {
        return Simple;
    }
    if (d->mSASL->isChecked()) {
        return SASL;
    }
    return Anonymous;
862
863
}

Laurent Montel's avatar
Laurent Montel committed
864
void LdapConfigWidget::setSizeLimit(int sizelimit)
865
{
Laurent Montel's avatar
Laurent Montel committed
866
867
868
    if (d->mSizeLimit) {
        d->mSizeLimit->setValue(sizelimit);
    }
869
870
871
872
}

int LdapConfigWidget::sizeLimit() const
{
Laurent Montel's avatar
Laurent Montel committed
873
    return d->mSizeLimit ? d->mSizeLimit->value() : 0;
874
875
}

Laurent Montel's avatar
Laurent Montel committed
876
void LdapConfigWidget::setTimeLimit(int timelimit)
877
{
Laurent Montel's avatar
Laurent Montel committed
878
879
880
    if (d->mTimeLimit) {
        d->mTimeLimit->setValue(timelimit);
    }
881
882
883
884
}

int LdapConfigWidget::timeLimit() const
{
Laurent Montel's avatar
Laurent Montel committed
885
    return d->mTimeLimit ? d->mTimeLimit->value() : 0;
886
887
}

Laurent Montel's avatar
Laurent Montel committed
888
void LdapConfigWidget::setPageSize(int pagesize)
889
{
Laurent Montel's avatar
Laurent Montel committed
890
891
892
    if (d->mPageSize) {
        d->mPageSize->setValue(pagesize);
    }
893
894
895
896
}

int LdapConfigWidget::pageSize() const
{
Laurent Montel's avatar
Laurent Montel committed
897
    return d->mPageSize ? d->mPageSize->value() : 0;
898
899
900
901
}

LdapConfigWidget::WinFlags LdapConfigWidget::features() const
{
Laurent Montel's avatar
Laurent Montel committed
902
    return d->mFeatures;
903
904
}

Laurent Montel's avatar
Laurent Montel committed
905
void LdapConfigWidget::setFeatures(LdapConfigWidget::WinFlags features)
906
{
Laurent Montel's avatar
Laurent Montel committed
907
    d->mFeatures = features;
908

Laurent Montel's avatar
Laurent Montel committed
909
910
911
912
913
    // First delete all the child widgets.
    // FIXME: I hope it's correct
    QList<QObject *> ch = children();
    const int numberOfChild(ch.count());
    for (int i = 0; i < numberOfChild; ++i) {
Laurent Montel's avatar
Laurent Montel committed
914
        QWidget *widget = qobject_cast<QWidget *>(ch[ i ]);
Laurent Montel's avatar
Laurent Montel committed
915
916
917
        if (widget && widget->parent() == this) {
            delete(widget);
        }
Allen Winter's avatar
Allen Winter committed
918
    }
919

Laurent Montel's avatar
Laurent Montel committed
920
921
    // Re-create child widgets according to the new flags
    d->initWidget();
922
923
}

924
#include "moc_ldapconfigwidget.cpp"