2014-03-15 19:36:03 +01:00
|
|
|
/* ============================================================
|
2017-08-25 17:11:29 +02:00
|
|
|
* Falkon - Qt web browser
|
2018-01-03 20:52:27 +01:00
|
|
|
* Copyright (C) 2014-2018 David Rosca <nowrep@gmail.com>
|
2014-03-15 19:36:03 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
|
|
|
#include "locationcompleterrefreshjob.h"
|
|
|
|
#include "locationcompletermodel.h"
|
|
|
|
#include "mainapplication.h"
|
|
|
|
#include "bookmarkitem.h"
|
2016-12-20 12:19:35 +01:00
|
|
|
#include "iconprovider.h"
|
2014-03-15 19:36:03 +01:00
|
|
|
#include "sqldatabase.h"
|
|
|
|
#include "qzsettings.h"
|
|
|
|
#include "bookmarks.h"
|
2014-06-13 21:22:44 +02:00
|
|
|
#include "qztools.h"
|
2014-03-15 19:36:03 +01:00
|
|
|
|
2014-09-26 19:28:24 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2014-03-15 19:36:03 +01:00
|
|
|
#include <QDateTime>
|
2014-03-15 23:22:35 +01:00
|
|
|
|
|
|
|
#include <QtConcurrent/QtConcurrentRun>
|
2014-03-15 19:36:03 +01:00
|
|
|
|
|
|
|
LocationCompleterRefreshJob::LocationCompleterRefreshJob(const QString &searchString)
|
|
|
|
: QObject()
|
|
|
|
, m_timestamp(QDateTime::currentMSecsSinceEpoch())
|
|
|
|
, m_searchString(searchString)
|
2014-06-08 13:56:20 +02:00
|
|
|
, m_jobCancelled(false)
|
2014-03-15 19:36:03 +01:00
|
|
|
{
|
|
|
|
m_watcher = new QFutureWatcher<void>(this);
|
|
|
|
connect(m_watcher, SIGNAL(finished()), this, SLOT(slotFinished()));
|
|
|
|
|
|
|
|
QFuture<void> future = QtConcurrent::run(this, &LocationCompleterRefreshJob::runJob);
|
|
|
|
m_watcher->setFuture(future);
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 LocationCompleterRefreshJob::timestamp() const
|
|
|
|
{
|
|
|
|
return m_timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString LocationCompleterRefreshJob::searchString() const
|
|
|
|
{
|
|
|
|
return m_searchString;
|
|
|
|
}
|
|
|
|
|
2017-10-23 20:03:13 +02:00
|
|
|
bool LocationCompleterRefreshJob::isCanceled() const
|
|
|
|
{
|
|
|
|
return m_jobCancelled;
|
|
|
|
}
|
|
|
|
|
2014-03-15 19:36:03 +01:00
|
|
|
QList<QStandardItem*> LocationCompleterRefreshJob::completions() const
|
|
|
|
{
|
|
|
|
return m_items;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString LocationCompleterRefreshJob::domainCompletion() const
|
|
|
|
{
|
|
|
|
return m_domainCompletion;
|
|
|
|
}
|
|
|
|
|
2014-06-08 13:56:20 +02:00
|
|
|
void LocationCompleterRefreshJob::jobCancelled()
|
|
|
|
{
|
|
|
|
m_jobCancelled = true;
|
|
|
|
}
|
|
|
|
|
2014-03-15 19:36:03 +01:00
|
|
|
void LocationCompleterRefreshJob::slotFinished()
|
|
|
|
{
|
|
|
|
emit finished();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool countBiggerThan(const QStandardItem* i1, const QStandardItem* i2)
|
|
|
|
{
|
2014-06-17 14:55:32 +02:00
|
|
|
int i1Count = i1->data(LocationCompleterModel::CountRole).toInt();
|
|
|
|
int i2Count = i2->data(LocationCompleterModel::CountRole).toInt();
|
|
|
|
return i1Count > i2Count;
|
2014-03-15 19:36:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocationCompleterRefreshJob::runJob()
|
|
|
|
{
|
2014-06-08 13:56:20 +02:00
|
|
|
if (m_jobCancelled || mApp->isClosing() || !mApp) {
|
2014-03-15 19:36:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_searchString.isEmpty()) {
|
|
|
|
completeMostVisited();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
completeFromHistory();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load all icons into QImage
|
|
|
|
foreach (QStandardItem* item, m_items) {
|
2014-06-08 13:56:20 +02:00
|
|
|
if (m_jobCancelled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-15 19:36:03 +01:00
|
|
|
const QUrl url = item->data(LocationCompleterModel::UrlRole).toUrl();
|
2016-12-20 12:19:35 +01:00
|
|
|
item->setData(IconProvider::imageForUrl(url), LocationCompleterModel::ImageRole);
|
2014-03-15 19:36:03 +01:00
|
|
|
}
|
|
|
|
|
2017-10-23 20:03:13 +02:00
|
|
|
if (m_jobCancelled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-15 19:36:03 +01:00
|
|
|
// Get domain completion
|
|
|
|
if (!m_searchString.isEmpty() && qzSettings->useInlineCompletion) {
|
|
|
|
QSqlQuery domainQuery = LocationCompleterModel::createDomainQuery(m_searchString);
|
2017-10-23 20:03:13 +02:00
|
|
|
if (!domainQuery.lastQuery().isEmpty()) {
|
2017-12-18 13:06:42 +01:00
|
|
|
domainQuery.exec();
|
2017-10-23 20:03:13 +02:00
|
|
|
if (domainQuery.next()) {
|
|
|
|
m_domainCompletion = createDomainCompletion(domainQuery.value(0).toUrl().host());
|
|
|
|
}
|
2014-03-15 19:36:03 +01:00
|
|
|
}
|
2017-10-23 20:03:13 +02:00
|
|
|
}
|
2014-03-15 19:36:03 +01:00
|
|
|
|
2017-10-23 20:03:13 +02:00
|
|
|
if (m_jobCancelled) {
|
|
|
|
return;
|
2014-03-15 19:36:03 +01:00
|
|
|
}
|
2017-08-11 12:26:50 +02:00
|
|
|
|
|
|
|
// Add search/visit item
|
|
|
|
if (!m_searchString.isEmpty()) {
|
|
|
|
QStandardItem* item = new QStandardItem();
|
|
|
|
item->setText(m_searchString);
|
|
|
|
item->setData(m_searchString, LocationCompleterModel::UrlRole);
|
|
|
|
item->setData(m_searchString, LocationCompleterModel::SearchStringRole);
|
2018-01-03 20:52:27 +01:00
|
|
|
item->setData(true, LocationCompleterModel::VisitSearchItemRole);
|
2017-08-11 12:26:50 +02:00
|
|
|
if (!m_domainCompletion.isEmpty()) {
|
|
|
|
const QUrl url = QUrl(QSL("http://%1").arg(m_domainCompletion));
|
|
|
|
item->setData(IconProvider::imageForDomain(url), LocationCompleterModel::ImageRole);
|
|
|
|
}
|
|
|
|
m_items.prepend(item);
|
|
|
|
}
|
2014-03-15 19:36:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocationCompleterRefreshJob::completeFromHistory()
|
|
|
|
{
|
|
|
|
QList<QUrl> urlList;
|
|
|
|
Type showType = (Type) qzSettings->showLocationSuggestions;
|
|
|
|
|
|
|
|
// Search in bookmarks
|
|
|
|
if (showType == HistoryAndBookmarks || showType == Bookmarks) {
|
|
|
|
const int bookmarksLimit = 10;
|
|
|
|
QList<BookmarkItem*> bookmarks = mApp->bookmarks()->searchBookmarks(m_searchString, bookmarksLimit);
|
|
|
|
|
|
|
|
foreach (BookmarkItem* bookmark, bookmarks) {
|
|
|
|
Q_ASSERT(bookmark->isUrl());
|
|
|
|
|
|
|
|
QStandardItem* item = new QStandardItem();
|
|
|
|
item->setText(bookmark->url().toEncoded());
|
|
|
|
item->setData(-1, LocationCompleterModel::IdRole);
|
|
|
|
item->setData(bookmark->title(), LocationCompleterModel::TitleRole);
|
|
|
|
item->setData(bookmark->url(), LocationCompleterModel::UrlRole);
|
|
|
|
item->setData(bookmark->visitCount(), LocationCompleterModel::CountRole);
|
2018-01-03 20:52:27 +01:00
|
|
|
item->setData(true, LocationCompleterModel::BookmarkRole);
|
2014-03-15 19:36:03 +01:00
|
|
|
item->setData(QVariant::fromValue<void*>(static_cast<void*>(bookmark)), LocationCompleterModel::BookmarkItemRole);
|
|
|
|
item->setData(m_searchString, LocationCompleterModel::SearchStringRole);
|
|
|
|
|
|
|
|
urlList.append(bookmark->url());
|
|
|
|
m_items.append(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-17 14:55:32 +02:00
|
|
|
// Sort by count
|
2014-09-25 14:13:26 +02:00
|
|
|
std::sort(m_items.begin(), m_items.end(), countBiggerThan);
|
2014-06-17 14:55:32 +02:00
|
|
|
|
2014-03-15 19:36:03 +01:00
|
|
|
// Search in history
|
|
|
|
if (showType == HistoryAndBookmarks || showType == History) {
|
|
|
|
const int historyLimit = 20;
|
|
|
|
QSqlQuery query = LocationCompleterModel::createHistoryQuery(m_searchString, historyLimit);
|
2017-12-18 13:06:42 +01:00
|
|
|
query.exec();
|
2014-03-15 19:36:03 +01:00
|
|
|
|
2016-12-20 11:22:31 +01:00
|
|
|
while (query.next()) {
|
|
|
|
const QUrl url = query.value(1).toUrl();
|
2014-03-15 19:36:03 +01:00
|
|
|
|
|
|
|
if (urlList.contains(url)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStandardItem* item = new QStandardItem();
|
|
|
|
item->setText(url.toEncoded());
|
2016-12-20 11:22:31 +01:00
|
|
|
item->setData(query.value(0), LocationCompleterModel::IdRole);
|
|
|
|
item->setData(query.value(2), LocationCompleterModel::TitleRole);
|
2014-03-15 19:36:03 +01:00
|
|
|
item->setData(url, LocationCompleterModel::UrlRole);
|
2016-12-20 11:22:31 +01:00
|
|
|
item->setData(query.value(3), LocationCompleterModel::CountRole);
|
2018-01-03 20:52:27 +01:00
|
|
|
item->setData(true, LocationCompleterModel::HistoryRole);
|
2014-03-15 19:36:03 +01:00
|
|
|
item->setData(m_searchString, LocationCompleterModel::SearchStringRole);
|
|
|
|
|
|
|
|
m_items.append(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationCompleterRefreshJob::completeMostVisited()
|
|
|
|
{
|
2017-12-18 13:06:42 +01:00
|
|
|
QSqlQuery query(SqlDatabase::instance()->database());
|
|
|
|
query.exec(QSL("SELECT id, url, title FROM history ORDER BY count DESC LIMIT 15"));
|
2014-03-15 19:36:03 +01:00
|
|
|
|
2016-12-20 11:22:31 +01:00
|
|
|
while (query.next()) {
|
2014-03-15 19:36:03 +01:00
|
|
|
QStandardItem* item = new QStandardItem();
|
2016-12-20 11:22:31 +01:00
|
|
|
const QUrl url = query.value(1).toUrl();
|
2014-03-15 19:36:03 +01:00
|
|
|
|
|
|
|
item->setText(url.toEncoded());
|
2016-12-20 11:22:31 +01:00
|
|
|
item->setData(query.value(0), LocationCompleterModel::IdRole);
|
|
|
|
item->setData(query.value(2), LocationCompleterModel::TitleRole);
|
2014-03-15 19:36:03 +01:00
|
|
|
item->setData(url, LocationCompleterModel::UrlRole);
|
|
|
|
|
|
|
|
m_items.append(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString LocationCompleterRefreshJob::createDomainCompletion(const QString &completion) const
|
|
|
|
{
|
2014-03-15 23:22:35 +01:00
|
|
|
// Make sure search string and completion matches
|
2014-03-15 19:36:03 +01:00
|
|
|
|
2014-03-15 23:22:35 +01:00
|
|
|
if (m_searchString.startsWith(QL1S("www.")) && !completion.startsWith(QL1S("www."))) {
|
|
|
|
return QL1S("www.") + completion;
|
2014-03-15 19:36:03 +01:00
|
|
|
}
|
|
|
|
|
2014-03-15 23:22:35 +01:00
|
|
|
if (!m_searchString.startsWith(QL1S("www.")) && completion.startsWith(QL1S("www."))) {
|
|
|
|
return completion.mid(4);
|
2014-03-15 19:36:03 +01:00
|
|
|
}
|
|
|
|
|
2014-03-15 23:22:35 +01:00
|
|
|
return completion;
|
2014-03-15 19:36:03 +01:00
|
|
|
}
|