File indexing completed on 2024-05-12 04:58:18

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-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 "popuplocationbar.h"
0019 #include "popupwebview.h"
0020 #include "toolbutton.h"
0021 #include "qztools.h"
0022 #include "iconprovider.h"
0023 #include "bookmarksicon.h"
0024 #include "autofillicon.h"
0025 #include "webpage.h"
0026 
0027 class FALKON_EXPORT PopupSiteIcon : public QWidget
0028 {
0029 public:
0030     explicit PopupSiteIcon(QWidget* parent = nullptr) : QWidget(parent) { }
0031     void setIcon(const QIcon &icon) {
0032         m_icon = QIcon(icon.pixmap(16));
0033         update();
0034     }
0035 
0036 private:
0037     QIcon m_icon;
0038 
0039     void paintEvent(QPaintEvent*) override {
0040         QPainter p(this);
0041         m_icon.paint(&p, rect());
0042     }
0043 };
0044 
0045 PopupLocationBar::PopupLocationBar(QWidget* parent)
0046     : LineEdit(parent)
0047     , m_view(nullptr)
0048 {
0049     m_siteIcon = new PopupSiteIcon(this);
0050     m_siteIcon->setIcon(IconProvider::emptyWebIcon());
0051     m_siteIcon->setFixedSize(26, 26);
0052 
0053     m_bookmarkIcon = new BookmarksIcon(this);
0054     m_autofillIcon = new AutoFillIcon(this);
0055 
0056     auto* rightSpacer = new QWidget(this);
0057     rightSpacer->setFixedWidth(3);
0058 
0059     addWidget(m_siteIcon, LineEdit::LeftSide);
0060     addWidget(m_autofillIcon, LineEdit::RightSide);
0061     addWidget(m_bookmarkIcon, LineEdit::RightSide);
0062     addWidget(rightSpacer, LineEdit::RightSide);
0063     setLeftMargin(24);
0064 
0065     setFixedHeight(26);
0066     setReadOnly(true);
0067 
0068     // Hide icons by default
0069     m_autofillIcon->hide();
0070 }
0071 
0072 void PopupLocationBar::setView(PopupWebView* view)
0073 {
0074     m_view = view;
0075 
0076     m_bookmarkIcon->setWebView(m_view);
0077     m_autofillIcon->setWebView(m_view);
0078 }
0079 
0080 void PopupLocationBar::startLoading()
0081 {
0082     m_autofillIcon->hide();
0083 
0084     updateTextMargins();
0085 }
0086 
0087 void PopupLocationBar::stopLoading()
0088 {
0089     m_bookmarkIcon->checkBookmark(m_view->url());
0090 
0091     auto* page = qobject_cast<WebPage*>(m_view->page());
0092 
0093     if (page && !page->autoFillUsernames().isEmpty()) {
0094         m_autofillIcon->setUsernames(page->autoFillUsernames());
0095         m_autofillIcon->show();
0096     }
0097 
0098     updateTextMargins();
0099 }
0100 
0101 void PopupLocationBar::showUrl(const QUrl &url)
0102 {
0103     setText(QzTools::urlEncodeQueryString(url));
0104     setCursorPosition(0);
0105 }
0106 
0107 void PopupLocationBar::showSiteIcon()
0108 {
0109     QIcon icon = IconProvider::emptyWebIcon();
0110     if (property("secured").toBool()) {
0111         icon = QIcon::fromTheme(QSL("document-encrypted"), icon);
0112     }
0113     m_siteIcon->setIcon(QIcon(icon.pixmap(16)));
0114 }
0115 
0116 void PopupLocationBar::setPrivacyState(bool state)
0117 {
0118     setProperty("secured", QVariant(state));
0119 }