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

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-2017 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 "qztools.h"
0019 #include "siteinfowidget.h"
0020 #include "ui_siteinfowidget.h"
0021 #include "browserwindow.h"
0022 #include "mainapplication.h"
0023 #include "webpage.h"
0024 #include "tabbedwebview.h"
0025 #include "sqldatabase.h"
0026 #include "protocolhandlermanager.h"
0027 
0028 #include <QToolTip>
0029 
0030 SiteInfoWidget::SiteInfoWidget(BrowserWindow* window, QWidget* parent)
0031     : LocationBarPopup(parent)
0032     , ui(new Ui::SiteInfoWidget)
0033     , m_window(window)
0034 {
0035     this->setAttribute(Qt::WA_DeleteOnClose);
0036     ui->setupUi(this);
0037 
0038     setPopupAlignment(Qt::AlignLeft);
0039 
0040     WebView* view = m_window->weView();
0041 
0042     ui->titleLabel->setText(tr("<b>Site %1<b/>").arg(view->url().host()));
0043 
0044     if (view->url().scheme() == QL1S("https")) {
0045         ui->secureLabel->setText(tr("Your connection to this site is <b>secured</b>."));
0046         ui->secureIcon->setPixmap(QPixmap(QSL(":/icons/locationbar/safe.png")));
0047     }
0048     else {
0049         ui->secureLabel->setText(tr("Your connection to this site is <b>unsecured</b>."));
0050         ui->secureIcon->setPixmap(QPixmap(QSL(":/icons/locationbar/unsafe.png")));
0051     }
0052 
0053     QString scheme = view->url().scheme();
0054     QString host = view->url().host();
0055 
0056     QSqlQuery query(SqlDatabase::instance()->database());
0057     query.prepare(QSL("SELECT sum(count) FROM history WHERE url LIKE ?"));
0058     query.addBindValue(QSL("%1://%2%").arg(scheme, host));
0059     query.exec();
0060 
0061     if (query.next()) {
0062         int count = query.value(0).toInt();
0063         if (count > 3) {
0064             ui->historyLabel->setText(tr("This is your <b>%1</b> visit of this site.").arg(QString::number(count) + QLatin1Char('.')));
0065             ui->historyIcon->setPixmap(QPixmap(QSL(":/icons/locationbar/visit3.png")));
0066         }
0067         else if (count == 0) {
0068             ui->historyLabel->setText(tr("You have <b>never</b> visited this site before."));
0069             ui->historyIcon->setPixmap(QPixmap(QSL(":/icons/locationbar/visit1.png")));
0070         }
0071         else {
0072             ui->historyIcon->setPixmap(QPixmap(QSL(":/icons/locationbar/visit2.png")));
0073             QString text;
0074             if (count == 1) {
0075                 text = tr("first");
0076             }
0077             else if (count == 2) {
0078                 text = tr("second");
0079             }
0080             else if (count == 3) {
0081                 text = tr("third");
0082             }
0083             ui->historyLabel->setText(tr("This is your <b>%1</b> visit of this site.").arg(text));
0084         }
0085     }
0086 
0087     updateProtocolHandler();
0088 
0089     connect(ui->pushButton, &QAbstractButton::clicked, m_window->action(QSL("Tools/SiteInfo")), &QAction::trigger);
0090     connect(ui->protocolHandlerButton, &QPushButton::clicked, this, &SiteInfoWidget::protocolHandlerButtonClicked);
0091 }
0092 
0093 SiteInfoWidget::~SiteInfoWidget()
0094 {
0095     delete ui;
0096 }
0097 
0098 void SiteInfoWidget::updateProtocolHandler()
0099 {
0100     WebPage *page = m_window->weView()->page();
0101 
0102     const QString scheme = page->registerProtocolHandlerRequestScheme();
0103     const QUrl registeredUrl = mApp->protocolHandlerManager()->protocolHandlers().value(scheme);
0104 
0105     if (!scheme.isEmpty() && registeredUrl != page->registerProtocolHandlerRequestUrl()) {
0106         ui->protocolHandlerLabel->setText(tr("Register as <b>%1</b> links handler").arg(page->registerProtocolHandlerRequestScheme()));
0107         ui->protocolHandlerButton->setText(tr("Register"));
0108     } else {
0109         ui->protocolHandlerLabel->hide();
0110         ui->protocolHandlerButton->hide();
0111         ui->protocolHandlerLine->hide();
0112     }
0113 }
0114 
0115 void SiteInfoWidget::protocolHandlerButtonClicked()
0116 {
0117     WebPage *page = m_window->weView()->page();
0118 
0119     mApp->protocolHandlerManager()->addProtocolHandler(page->registerProtocolHandlerRequestScheme(), page->registerProtocolHandlerRequestUrl());
0120     close();
0121 }