File indexing completed on 2024-05-12 04:57:55

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2013-2018 David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "autofillwidget.h"
0019 #include "ui_autofillwidget.h"
0020 #include "autofill.h"
0021 #include "webview.h"
0022 #include "webpage.h"
0023 #include "scripts.h"
0024 #include "mainapplication.h"
0025 #include "passwordmanager.h"
0026 
0027 #include <QPushButton>
0028 
0029 AutoFillWidget::AutoFillWidget(WebView* view, QWidget* parent)
0030     : LocationBarPopup(parent)
0031     , ui(new Ui::AutoFillWidget)
0032     , m_view(view)
0033 {
0034     ui->setupUi(this);
0035 }
0036 
0037 void AutoFillWidget::setUsernames(const QStringList &usernames)
0038 {
0039     int i = 0;
0040     for (const QString &username : usernames) {
0041         if (username.isEmpty()) {
0042             continue;
0043         }
0044 
0045         auto* button = new QPushButton(this);
0046         button->setIcon(QIcon(QSL(":icons/other/login.png")));
0047         button->setStyleSheet(QSL("text-align:left;font-weight:bold;"));
0048         button->setText(username);
0049         button->setFlat(true);
0050 
0051         ui->gridLayout->addWidget(button, i++, 0);
0052         connect(button, &QPushButton::clicked, this, [=]() {
0053             const auto entries = mApp->autoFill()->getFormData(m_view->url());
0054             PasswordEntry entry;
0055             // Find exact username match
0056             for (const PasswordEntry &e : entries) {
0057                 if (e.username == username) {
0058                     entry = e;
0059                     break;
0060                 }
0061             }
0062             // Find by index
0063             // This is needed for DatabaseEncryptedPasswordBackend because it also encrypts usernames.
0064             if (!entry.isValid()) {
0065                 entry = entries.value(i - 1);
0066             }
0067             if (entry.isValid()) {
0068                 mApp->autoFill()->updateLastUsed(entry);
0069                 m_view->page()->runJavaScript(Scripts::completeFormData(entry.data), WebPage::SafeJsWorld);
0070             }
0071             close();
0072         });
0073     }
0074 }
0075 
0076 AutoFillWidget::~AutoFillWidget()
0077 {
0078     delete ui;
0079 }