File indexing completed on 2025-01-05 05:09:30

0001 /*
0002     SPDX-FileCopyrightText: 2010-2012 Daniel Nicoletti <dantti12@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "KCupsPasswordDialog.h"
0008 
0009 #include <QPointer>
0010 
0011 #include <KLocalizedString>
0012 #include <KPasswordDialog>
0013 #include <KWindowSystem>
0014 #include <KX11Extras>
0015 
0016 KCupsPasswordDialog::KCupsPasswordDialog(QObject *parent)
0017     : QObject(parent)
0018     , m_accepted(false)
0019     , m_mainwindow(0)
0020     ,
0021     // default text, can be overridden using setPromptText()
0022     m_promptText(i18n("Enter an username and a password to complete the task"))
0023 {
0024 }
0025 
0026 void KCupsPasswordDialog::setMainWindow(WId mainwindow)
0027 {
0028     m_mainwindow = mainwindow;
0029 }
0030 
0031 void KCupsPasswordDialog::setPromptText(const QString &text)
0032 {
0033     m_promptText = text;
0034 }
0035 
0036 void KCupsPasswordDialog::exec(const QString &username, bool wrongPassword)
0037 {
0038     QPointer<KPasswordDialog> dialog = new KPasswordDialog(nullptr, KPasswordDialog::ShowUsernameLine);
0039     dialog->setPrompt(m_promptText);
0040     dialog->setModal(false);
0041     dialog->setUsername(username);
0042     if (wrongPassword) {
0043         dialog->showErrorMessage(QString(), KPasswordDialog::UsernameError);
0044         dialog->showErrorMessage(i18n("Wrong username or password"), KPasswordDialog::PasswordError);
0045     }
0046 
0047     dialog->show();
0048     if (m_mainwindow) {
0049         dialog->setAttribute(Qt::WA_NativeWindow, true);
0050         KWindowSystem::setMainWindow(dialog->windowHandle(), m_mainwindow);
0051     }
0052     if (KWindowSystem::isPlatformX11()) {
0053         KX11Extras::forceActiveWindow(dialog->winId());
0054     }
0055 
0056     // Do not return from this method now
0057     dialog->exec();
0058 
0059     if (dialog) {
0060         m_accepted = dialog->result() == QDialog::Accepted;
0061         m_username = dialog->username();
0062         m_password = dialog->password();
0063         dialog->deleteLater();
0064     }
0065 }
0066 
0067 bool KCupsPasswordDialog::accepted() const
0068 {
0069     return m_accepted;
0070 }
0071 
0072 QString KCupsPasswordDialog::username() const
0073 {
0074     return m_username;
0075 }
0076 
0077 QString KCupsPasswordDialog::password() const
0078 {
0079     return m_password;
0080 }
0081 
0082 #include "moc_KCupsPasswordDialog.cpp"