File indexing completed on 2024-04-28 03:59:18

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2007 Olivier Goffart <ogoffart at kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include <QApplication>
0009 #include <QMap>
0010 
0011 #include <kpassworddialog.h>
0012 
0013 #include <iostream>
0014 
0015 // We can't depend on i18n in this test, but still -- show good practice (other than the line below) :-)
0016 #define i18n QString::fromLatin1
0017 
0018 int main(int argc, char *argv[])
0019 {
0020     QApplication::setApplicationName(QStringLiteral("KNewPasswordDialogTest"));
0021     QApplication a(argc, argv);
0022 
0023     // step 1  simple password
0024     {
0025         KPasswordDialog dlg(nullptr, KPasswordDialog::ShowKeepPassword);
0026         dlg.setPrompt(
0027             i18n("This is a long prompt line. It is important it to be long so we can test the dialog does not get broken because of multiline labels. Please "
0028                  "enter a password:"));
0029         dlg.addCommentLine(i18n("This is a rather large left comment line"),
0030                            i18n("Right part of the comment line has to be long too so be test the layouting works really ok. Please visit http://www.kde.org"));
0031 
0032         if (dlg.exec()) {
0033             std::cout << "Entered password: " << qPrintable(dlg.password()) << std::endl;
0034         } else {
0035             std::cout << "No password" << std::endl;
0036             return -1;
0037         }
0038     }
0039 
0040     // step 2 readonly username
0041     {
0042         KPasswordDialog dlg(nullptr, KPasswordDialog::ShowUsernameLine | KPasswordDialog::UsernameReadOnly);
0043         dlg.setPrompt(i18n("Enter a password for the test"));
0044         dlg.setUsername(QStringLiteral("konqui"));
0045         dlg.addCommentLine(i18n("Site"), i18n("http://www.kde.org"));
0046 
0047         if (dlg.exec()) {
0048             std::cout << "Entered password: " << qPrintable(dlg.password()) << std::endl;
0049         } else {
0050             std::cout << "No password" << std::endl;
0051             return -1;
0052         }
0053     }
0054 
0055     // step 3 with some username preset
0056     {
0057         KPasswordDialog dlg(nullptr, KPasswordDialog::ShowUsernameLine);
0058         dlg.setPrompt(i18n("Enter a password for the test"));
0059         QMap<QString, QString> logins;
0060         logins.insert(QStringLiteral("konqui"), QStringLiteral("foo"));
0061         logins.insert(QStringLiteral("watson"), QStringLiteral("bar"));
0062         logins.insert(QStringLiteral("ogoffart"), QString());
0063 
0064         dlg.setKnownLogins(logins);
0065 
0066         if (dlg.exec()) {
0067             std::cout << "Entered password: " << qPrintable(dlg.password()) << " for username " << qPrintable(dlg.username()) << std::endl;
0068         } else {
0069             std::cout << "No password" << std::endl;
0070             return -1;
0071         }
0072     }
0073 
0074     return 0;
0075 }