File indexing completed on 2024-05-12 16:40:53

0001 /* This file is part of the KDE project
0002    Copyright (C) 2003-2014 Jarosław Staniek <staniek@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "KexiDBPasswordDialog.h"
0021 #include <KexiIcon.h>
0022 #include <kexiutils/utils.h>
0023 
0024 #include <KDbConnectionData>
0025 
0026 #include <KLocalizedString>
0027 
0028 #include <QPushButton>
0029 #include <QLabel>
0030 #include <QLineEdit>
0031 
0032 class Q_DECL_HIDDEN KexiDBPasswordDialog::Private
0033 {
0034  public:
0035     explicit Private(KDbConnectionData* data);
0036     ~Private();
0037 
0038     KDbConnectionData *cdata;
0039     bool showConnectionDetailsRequested;
0040 };
0041 
0042 KexiDBPasswordDialog::Private::Private(KDbConnectionData* data)
0043     : cdata(data)
0044     , showConnectionDetailsRequested(false)
0045 {
0046 }
0047 
0048 KexiDBPasswordDialog::Private::~Private()
0049 {
0050 }
0051 
0052 KexiDBPasswordDialog::KexiDBPasswordDialog(QWidget *parent, KDbConnectionData& cdata,
0053                                            Flags flags)
0054         : KPasswordDialog(parent,
0055             ShowUsernameLine | ShowDomainLine | ((flags & ServerReadOnly) ? DomainReadOnly : KPasswordDialog::NoFlags))
0056         , d(new Private(&cdata))
0057 {
0058     setWindowTitle(xi18nc("@title:window", "Opening Database"));
0059     setPrompt(xi18nc("@info", "Supply a password below."));
0060 
0061     if ((flags & ShowDetailsButton)) {
0062         //! @todo KEXI3 OK?
0063         buttonBox()->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel
0064                                         | QDialogButtonBox::Help);
0065         QPushButton *detailsButton = buttonBox()->button(QDialogButtonBox::Help);
0066         connect(detailsButton, SIGNAL(clicked()),
0067                 this, SLOT(slotShowConnectionDetails()));
0068         detailsButton->setText(xi18n("&Details >>"));
0069         connect(detailsButton, SIGNAL(clicked()), this, SLOT(slotOkOrDetailsButtonClicked()));
0070     }
0071     /*  msg += cdata.username().isEmpty() ?
0072           "<p>"+xi18n("Please enter the password.")
0073           : "<p>"+xi18n("Please enter the password for user.").arg("<b>"+cdata.userName+"</b>");*/
0074 
0075     QString srv = cdata.toUserVisibleString(KDbConnectionData::UserVisibleStringOption::None);
0076 //    if (srv.isEmpty() || srv.toLower() == "localhost")
0077 //        srv = xi18n("local database server");
0078 
0079     QLabel *domainLabel = KexiUtils::findFirstChild<QLabel*>(this, "QLabel", "domainLabel");
0080     if (domainLabel) {
0081         domainLabel->setText(xi18n("Database server:"));
0082     }
0083     setDomain(srv);
0084 
0085     QString usr;
0086     if (cdata.userName().isEmpty()) {
0087         usr = xi18nc("unspecified user", "(unspecified)");
0088     } else {
0089         usr = cdata.userName();
0090     }
0091     setUsernameReadOnly(true);
0092     setUsername(usr);
0093 
0094     buttonBox()->button(QDialogButtonBox::Ok)->setText(xi18n("&Open"));
0095     connect(buttonBox()->button(QDialogButtonBox::Ok), SIGNAL(clicked()),
0096             this, SLOT(slotOkOrDetailsButtonClicked()));
0097     //! @todo KEXI3 buttonBox()->button(QDialogButtonBox::Ok)->setIcon(koIcon("document-open");
0098 }
0099 
0100 KexiDBPasswordDialog::~KexiDBPasswordDialog()
0101 {
0102     delete d;
0103 }
0104 
0105 bool KexiDBPasswordDialog::showConnectionDetailsRequested() const
0106 {
0107     return d->showConnectionDetailsRequested;
0108 }
0109 
0110 void KexiDBPasswordDialog::slotOkOrDetailsButtonClicked()
0111 {
0112     d->cdata->setPassword(password());
0113     QLineEdit *userEdit = KexiUtils::findFirstChild<QLineEdit*>(this, "QLineEdit", "userEdit");
0114     if (!userEdit->isReadOnly()) {
0115         d->cdata->setUserName(userEdit->text());
0116     }
0117 }
0118 
0119 void KexiDBPasswordDialog::slotShowConnectionDetails()
0120 {
0121     d->showConnectionDetailsRequested = true;
0122     close();
0123 }
0124 
0125 //static
0126 tristate KexiDBPasswordDialog::getPasswordIfNeeded(KDbConnectionData *data, QWidget *parent)
0127 {
0128     if (data->isPasswordNeeded() && data->password().isNull() /* null means missing password */) {
0129         //ask for password
0130         KexiDBPasswordDialog pwdDlg(parent, *data, KexiDBPasswordDialog::ServerReadOnly);
0131         return QDialog::Accepted == pwdDlg.exec() ? tristate(true): cancelled;
0132     }
0133     return false;
0134 }