2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2014-01-11 16:11:42 +01:00
|
|
|
* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
|
2011-03-03 18:29:20 +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/>.
|
|
|
|
* ============================================================ */
|
2012-04-22 17:09:43 +02:00
|
|
|
#include "locationcompletermodel.h"
|
2014-02-09 13:34:52 +01:00
|
|
|
#include "mainapplication.h"
|
2011-04-25 20:56:45 +02:00
|
|
|
#include "iconprovider.h"
|
2014-02-09 13:34:52 +01:00
|
|
|
#include "bookmarkitem.h"
|
|
|
|
#include "bookmarks.h"
|
2012-08-10 21:16:43 +02:00
|
|
|
#include "qzsettings.h"
|
2012-12-04 14:29:27 +01:00
|
|
|
#include "qupzilla.h"
|
|
|
|
#include "tabwidget.h"
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QSqlQuery>
|
|
|
|
|
2012-04-22 17:09:43 +02:00
|
|
|
LocationCompleterModel::LocationCompleterModel(QObject* parent)
|
|
|
|
: QStandardItemModel(parent)
|
|
|
|
, m_lastCompletion(QChar(QChar::Nbsp))
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-01-22 18:12:21 +01:00
|
|
|
static bool countBiggerThan(const QStandardItem* i1, const QStandardItem* i2)
|
2012-09-02 15:36:20 +02:00
|
|
|
{
|
|
|
|
return i1->data(LocationCompleterModel::CountRole).toInt() >
|
|
|
|
i2->data(LocationCompleterModel::CountRole).toInt();
|
|
|
|
}
|
|
|
|
|
2012-04-22 17:09:43 +02:00
|
|
|
void LocationCompleterModel::refreshCompletions(const QString &string)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-04-22 17:09:43 +02:00
|
|
|
if (m_lastCompletion == string) {
|
2012-12-07 18:30:50 +01:00
|
|
|
refreshTabPositions();
|
2012-04-22 17:09:43 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-04-20 14:03:08 +02:00
|
|
|
|
2012-04-22 17:09:43 +02:00
|
|
|
m_lastCompletion = string;
|
2012-04-20 14:03:08 +02:00
|
|
|
|
2012-04-22 17:09:43 +02:00
|
|
|
if (string.isEmpty()) {
|
|
|
|
showMostVisited();
|
|
|
|
return;
|
2012-04-20 14:03:08 +02:00
|
|
|
}
|
2011-12-11 14:12:40 +01:00
|
|
|
|
2012-04-22 17:09:43 +02:00
|
|
|
clear();
|
2011-12-11 14:12:40 +01:00
|
|
|
|
2012-08-10 21:16:43 +02:00
|
|
|
Type showType = (Type) qzSettings->showLocationSuggestions;
|
2012-08-10 18:06:29 +02:00
|
|
|
|
2012-04-15 15:41:48 +02:00
|
|
|
int limit = string.size() < 3 ? 25 : 15;
|
2012-04-20 14:03:08 +02:00
|
|
|
QList<QUrl> urlList;
|
2012-09-02 15:36:20 +02:00
|
|
|
QList<QStandardItem*> itemList;
|
2012-04-15 15:41:48 +02:00
|
|
|
|
2012-08-10 18:06:29 +02:00
|
|
|
if (showType == HistoryAndBookmarks || showType == Bookmarks) {
|
2014-02-09 13:34:52 +01:00
|
|
|
QList<BookmarkItem*> bookmarks = mApp->bookmarks()->searchBookmarks(string);
|
|
|
|
|
|
|
|
foreach (BookmarkItem* bookmark, bookmarks) {
|
|
|
|
Q_ASSERT(bookmark->isUrl());
|
2012-08-10 18:06:29 +02:00
|
|
|
|
|
|
|
QStandardItem* item = new QStandardItem();
|
|
|
|
|
2014-02-09 13:34:52 +01:00
|
|
|
item->setIcon(_iconForUrl(bookmark->url()));
|
|
|
|
item->setText(bookmark->url().toEncoded());
|
|
|
|
item->setData(-1, IdRole);
|
|
|
|
item->setData(bookmark->title(), TitleRole);
|
|
|
|
item->setData(bookmark->visitCount(), CountRole);
|
2012-08-10 18:06:29 +02:00
|
|
|
item->setData(QVariant(true), BookmarkRole);
|
2012-08-25 13:08:06 +02:00
|
|
|
item->setData(string, SearchStringRole);
|
2014-02-09 13:34:52 +01:00
|
|
|
|
2012-12-10 14:59:12 +01:00
|
|
|
if (qzSettings->showSwitchTab) {
|
2014-02-09 13:34:52 +01:00
|
|
|
item->setData(QVariant::fromValue<TabPosition>(tabPositionForUrl(bookmark->url())), TabPositionRole);
|
2012-12-04 14:29:27 +01:00
|
|
|
}
|
2012-09-02 15:36:20 +02:00
|
|
|
|
2014-02-09 13:34:52 +01:00
|
|
|
urlList.append(bookmark->url());
|
2012-09-02 15:36:20 +02:00
|
|
|
itemList.append(item);
|
2012-08-10 18:06:29 +02:00
|
|
|
}
|
2012-04-20 14:03:08 +02:00
|
|
|
|
2014-02-09 13:34:52 +01:00
|
|
|
limit -= itemList.count();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-08-10 18:06:29 +02:00
|
|
|
if (showType == HistoryAndBookmarks || showType == History) {
|
2012-09-02 15:19:12 +02:00
|
|
|
QSqlQuery query = createQuery(string, "count DESC", urlList, limit);
|
2012-08-10 18:06:29 +02:00
|
|
|
query.exec();
|
2012-04-15 15:41:48 +02:00
|
|
|
|
2012-08-10 18:06:29 +02:00
|
|
|
while (query.next()) {
|
|
|
|
QStandardItem* item = new QStandardItem();
|
2013-12-30 13:43:48 +01:00
|
|
|
const QUrl url = query.value(1).toUrl();
|
2012-04-20 14:03:08 +02:00
|
|
|
|
2012-08-10 18:06:29 +02:00
|
|
|
item->setIcon(_iconForUrl(url));
|
|
|
|
item->setText(url.toEncoded());
|
|
|
|
item->setData(query.value(0), IdRole);
|
|
|
|
item->setData(query.value(2), TitleRole);
|
2012-09-02 15:36:20 +02:00
|
|
|
item->setData(query.value(3), CountRole);
|
2012-08-10 18:06:29 +02:00
|
|
|
item->setData(QVariant(false), BookmarkRole);
|
2012-08-25 13:08:06 +02:00
|
|
|
item->setData(string, SearchStringRole);
|
2012-12-10 14:59:12 +01:00
|
|
|
if (qzSettings->showSwitchTab) {
|
2012-12-04 14:29:27 +01:00
|
|
|
item->setData(QVariant::fromValue<TabPosition>(tabPositionForUrl(url)), TabPositionRole);
|
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-09-02 15:36:20 +02:00
|
|
|
itemList.append(item);
|
2012-08-10 18:06:29 +02:00
|
|
|
}
|
2012-04-22 17:09:43 +02:00
|
|
|
}
|
2012-09-02 15:36:20 +02:00
|
|
|
|
|
|
|
// Sort by count
|
|
|
|
qSort(itemList.begin(), itemList.end(), countBiggerThan);
|
|
|
|
|
|
|
|
appendColumn(itemList);
|
2012-04-22 17:09:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocationCompleterModel::showMostVisited()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
|
|
|
|
QSqlQuery query;
|
2012-05-05 16:06:24 +02:00
|
|
|
query.exec("SELECT id, url, title FROM history ORDER BY count DESC LIMIT 15");
|
2012-04-22 17:09:43 +02:00
|
|
|
|
|
|
|
while (query.next()) {
|
|
|
|
QStandardItem* item = new QStandardItem();
|
2013-12-30 13:43:48 +01:00
|
|
|
const QUrl url = query.value(1).toUrl();
|
2012-04-22 17:09:43 +02:00
|
|
|
|
|
|
|
item->setIcon(_iconForUrl(url));
|
|
|
|
item->setText(url.toEncoded());
|
2012-05-05 16:06:24 +02:00
|
|
|
item->setData(query.value(0), IdRole);
|
|
|
|
item->setData(query.value(2), TitleRole);
|
|
|
|
item->setData(QVariant(false), BookmarkRole);
|
2012-12-10 14:59:12 +01:00
|
|
|
if (qzSettings->showSwitchTab) {
|
2012-12-04 14:29:27 +01:00
|
|
|
item->setData(QVariant::fromValue<TabPosition>(tabPositionForUrl(url)), TabPositionRole);
|
|
|
|
}
|
2012-04-22 17:09:43 +02:00
|
|
|
|
|
|
|
appendRow(item);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
2012-08-25 13:08:06 +02:00
|
|
|
|
2013-05-10 22:33:36 +02:00
|
|
|
QString LocationCompleterModel::completeDomain(const QString &text)
|
|
|
|
{
|
|
|
|
if (text.isEmpty() || text == QLatin1String("www.")) {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool withoutWww = text.startsWith(QLatin1Char('w')) && !text.startsWith(QLatin1String("www."));
|
|
|
|
QString query = "SELECT url FROM history WHERE ";
|
|
|
|
|
|
|
|
if (withoutWww) {
|
|
|
|
query.append(QLatin1String("url NOT LIKE ? AND url NOT LIKE ? AND "));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
query.append(QLatin1String("url LIKE ? OR url LIKE ? OR "));
|
|
|
|
}
|
|
|
|
|
|
|
|
query.append(QLatin1String("(url LIKE ? OR url LIKE ?) ORDER BY count DESC LIMIT 1"));
|
|
|
|
|
|
|
|
QSqlQuery sqlQuery;
|
|
|
|
sqlQuery.prepare(query);
|
|
|
|
|
|
|
|
if (withoutWww) {
|
|
|
|
sqlQuery.addBindValue(QString("http://www.%"));
|
|
|
|
sqlQuery.addBindValue(QString("https://www.%"));
|
|
|
|
sqlQuery.addBindValue(QString("http://%1%").arg(text));
|
|
|
|
sqlQuery.addBindValue(QString("https://%1%").arg(text));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sqlQuery.addBindValue(QString("http://%1%").arg(text));
|
|
|
|
sqlQuery.addBindValue(QString("https://%1%").arg(text));
|
|
|
|
sqlQuery.addBindValue(QString("http://www.%1%").arg(text));
|
|
|
|
sqlQuery.addBindValue(QString("https://www.%1%").arg(text));
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlQuery.exec();
|
|
|
|
|
|
|
|
if (!sqlQuery.next()) {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return sqlQuery.value(0).toUrl().host();
|
|
|
|
}
|
|
|
|
|
2012-09-02 15:19:12 +02:00
|
|
|
QSqlQuery LocationCompleterModel::createQuery(const QString &searchString, const QString &orderBy,
|
2014-02-09 15:07:19 +01:00
|
|
|
const QList<QUrl> &alreadyFound, int limit, bool exactMatch)
|
2012-08-25 13:08:06 +02:00
|
|
|
{
|
|
|
|
QStringList searchList;
|
2014-02-09 17:35:43 +01:00
|
|
|
QString query = QLatin1String("SELECT id, url, title, count FROM history WHERE ");
|
2012-08-25 13:08:06 +02:00
|
|
|
|
|
|
|
if (exactMatch) {
|
2014-02-09 17:35:43 +01:00
|
|
|
query.append(QLatin1String("title LIKE ? OR url LIKE ? "));
|
2012-08-25 13:08:06 +02:00
|
|
|
}
|
|
|
|
else {
|
2012-09-04 12:42:45 +02:00
|
|
|
searchList = searchString.split(QLatin1Char(' '), QString::SkipEmptyParts);
|
2012-08-25 13:08:06 +02:00
|
|
|
const int slSize = searchList.size();
|
|
|
|
for (int i = 0; i < slSize; ++i) {
|
2014-02-09 17:35:43 +01:00
|
|
|
query.append(QLatin1String("(title LIKE ? OR url LIKE ?) "));
|
2012-08-25 13:08:06 +02:00
|
|
|
if (i < slSize - 1) {
|
2012-09-04 12:42:45 +02:00
|
|
|
query.append(QLatin1String("AND "));
|
2012-08-25 13:08:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-31 22:05:14 +02:00
|
|
|
for (int i = 0; i < alreadyFound.count(); i++) {
|
2014-02-09 17:35:43 +01:00
|
|
|
query.append(QLatin1String("AND (NOT url=?) "));
|
2012-08-31 22:05:14 +02:00
|
|
|
}
|
|
|
|
|
2014-02-09 17:35:43 +01:00
|
|
|
query.append(QLatin1String("GROUP BY url "));
|
2013-12-05 19:48:24 +01:00
|
|
|
|
2012-08-25 13:08:06 +02:00
|
|
|
if (!orderBy.isEmpty()) {
|
2014-02-09 17:35:43 +01:00
|
|
|
query.append(QLatin1String("ORDER BY ") + orderBy);
|
2012-08-25 13:08:06 +02:00
|
|
|
}
|
|
|
|
|
2012-09-04 12:42:45 +02:00
|
|
|
query.append(QLatin1String(" LIMIT ?"));
|
2012-08-25 13:08:06 +02:00
|
|
|
|
|
|
|
QSqlQuery sqlQuery;
|
|
|
|
sqlQuery.prepare(query);
|
|
|
|
|
|
|
|
if (exactMatch) {
|
|
|
|
sqlQuery.addBindValue(QString("%%1%").arg(searchString));
|
|
|
|
sqlQuery.addBindValue(QString("%%1%").arg(searchString));
|
|
|
|
}
|
|
|
|
else {
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const QString &str, searchList) {
|
2012-08-25 13:08:06 +02:00
|
|
|
sqlQuery.addBindValue(QString("%%1%").arg(str));
|
|
|
|
sqlQuery.addBindValue(QString("%%1%").arg(str));
|
|
|
|
}
|
|
|
|
}
|
2012-08-31 22:05:14 +02:00
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const QUrl &url, alreadyFound) {
|
2012-08-31 22:05:14 +02:00
|
|
|
sqlQuery.addBindValue(url);
|
|
|
|
}
|
|
|
|
|
2012-08-25 13:08:06 +02:00
|
|
|
sqlQuery.addBindValue(limit);
|
|
|
|
|
|
|
|
return sqlQuery;
|
|
|
|
}
|
2012-12-04 14:29:27 +01:00
|
|
|
|
2012-12-10 14:59:12 +01:00
|
|
|
TabPosition LocationCompleterModel::tabPositionForUrl(const QUrl &url) const
|
2012-12-07 18:30:50 +01:00
|
|
|
{
|
|
|
|
return tabPositionForEncodedUrl(url.toEncoded());
|
|
|
|
}
|
|
|
|
|
2012-12-10 14:59:12 +01:00
|
|
|
TabPosition LocationCompleterModel::tabPositionForEncodedUrl(const QString &encodedUrl) const
|
2012-12-04 14:29:27 +01:00
|
|
|
{
|
2012-12-07 23:34:38 +01:00
|
|
|
QList<QupZilla*> windows = mApp->mainWindows();
|
|
|
|
int currentWindowIdx = windows.indexOf(mApp->getWindow());
|
|
|
|
windows.prepend(mApp->getWindow());
|
2012-12-10 14:59:12 +01:00
|
|
|
for (int win = 0; win < windows.count(); ++win) {
|
2012-12-07 23:34:38 +01:00
|
|
|
QupZilla* mainWin = windows.at(win);
|
2012-12-04 14:29:27 +01:00
|
|
|
QList<WebTab*> tabs = mainWin->tabWidget()->allTabs();
|
2012-12-10 14:59:12 +01:00
|
|
|
for (int tab = 0; tab < tabs.count(); ++tab) {
|
|
|
|
if (tabs[tab]->url().toEncoded() == encodedUrl) {
|
2012-12-04 14:29:27 +01:00
|
|
|
TabPosition pos;
|
2012-12-10 14:59:12 +01:00
|
|
|
pos.windowIndex = win == 0 ? currentWindowIdx : win - 1;
|
2012-12-04 14:29:27 +01:00
|
|
|
pos.tabIndex = tab;
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TabPosition();
|
|
|
|
}
|
2012-12-07 18:30:50 +01:00
|
|
|
|
|
|
|
void LocationCompleterModel::refreshTabPositions()
|
|
|
|
{
|
|
|
|
if (!qzSettings->showSwitchTab) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int row = 0; row < rowCount(); ++row) {
|
|
|
|
QStandardItem* aItem = item(row);
|
|
|
|
if (!aItem) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
aItem->setData(QVariant::fromValue<TabPosition>(tabPositionForEncodedUrl(aItem->text())), TabPositionRole);
|
|
|
|
}
|
|
|
|
}
|