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"
|
2014-02-19 22:07:21 +01:00
|
|
|
#include "browserwindow.h"
|
2012-12-04 14:29:27 +01:00
|
|
|
#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
|
|
|
{
|
2014-02-18 16:17:22 +01:00
|
|
|
// Move bookmarks up
|
|
|
|
bool i1Bookmark = i1->data(LocationCompleterModel::BookmarkRole).toBool();
|
|
|
|
bool i2Bookmark = i2->data(LocationCompleterModel::BookmarkRole).toBool();
|
|
|
|
|
|
|
|
if (i1Bookmark && i2Bookmark) {
|
|
|
|
return i1->data(LocationCompleterModel::CountRole).toInt() >
|
|
|
|
i2->data(LocationCompleterModel::CountRole).toInt();
|
|
|
|
}
|
|
|
|
return i1Bookmark;
|
2012-09-02 15:36:20 +02:00
|
|
|
}
|
|
|
|
|
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-04-20 14:03:08 +02:00
|
|
|
QList<QUrl> urlList;
|
2012-09-02 15:36:20 +02:00
|
|
|
QList<QStandardItem*> itemList;
|
2014-02-18 16:17:22 +01:00
|
|
|
Type showType = (Type) qzSettings->showLocationSuggestions;
|
2012-04-15 15:41:48 +02:00
|
|
|
|
2012-08-10 18:06:29 +02:00
|
|
|
if (showType == HistoryAndBookmarks || showType == Bookmarks) {
|
2014-02-22 15:23:46 +01:00
|
|
|
const int bookmarksLimit = 10;
|
2014-02-18 16:17:22 +01:00
|
|
|
QList<BookmarkItem*> bookmarks = mApp->bookmarks()->searchBookmarks(string, bookmarksLimit);
|
2014-02-09 13:34:52 +01:00
|
|
|
|
|
|
|
foreach (BookmarkItem* bookmark, bookmarks) {
|
|
|
|
Q_ASSERT(bookmark->isUrl());
|
2012-08-10 18:06:29 +02:00
|
|
|
|
|
|
|
QStandardItem* item = new QStandardItem();
|
2014-02-10 16:03:29 +01:00
|
|
|
item->setIcon(bookmark->icon());
|
2014-02-09 13:34:52 +01:00
|
|
|
item->setText(bookmark->url().toEncoded());
|
|
|
|
item->setData(-1, IdRole);
|
|
|
|
item->setData(bookmark->title(), TitleRole);
|
2014-03-01 13:02:57 +01:00
|
|
|
item->setData(bookmark->url(), UrlRole);
|
2014-02-09 13:34:52 +01:00
|
|
|
item->setData(bookmark->visitCount(), CountRole);
|
2012-08-10 18:06:29 +02:00
|
|
|
item->setData(QVariant(true), BookmarkRole);
|
2014-03-01 13:02:57 +01:00
|
|
|
item->setData(QVariant::fromValue<void*>(static_cast<void*>(bookmark)), BookmarkItemRole);
|
2012-08-25 13:08:06 +02:00
|
|
|
item->setData(string, SearchStringRole);
|
2014-03-01 13:02:57 +01:00
|
|
|
setTabPosition(item);
|
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
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-08-10 18:06:29 +02:00
|
|
|
if (showType == HistoryAndBookmarks || showType == History) {
|
2014-02-22 15:23:46 +01:00
|
|
|
const int historyLimit = 20;
|
2014-02-18 16:17:22 +01:00
|
|
|
QSqlQuery query = createQuery(string, historyLimit);
|
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()) {
|
2013-12-30 13:43:48 +01:00
|
|
|
const QUrl url = query.value(1).toUrl();
|
2012-04-20 14:03:08 +02:00
|
|
|
|
2014-02-18 16:17:22 +01:00
|
|
|
if (urlList.contains(url)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStandardItem* item = new QStandardItem();
|
2014-03-07 18:03:42 +01:00
|
|
|
item->setIcon(IconProvider::iconForUrl(url));
|
2012-08-10 18:06:29 +02:00
|
|
|
item->setText(url.toEncoded());
|
|
|
|
item->setData(query.value(0), IdRole);
|
|
|
|
item->setData(query.value(2), TitleRole);
|
2014-03-01 13:02:57 +01:00
|
|
|
item->setData(url, UrlRole);
|
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);
|
2014-03-01 13:02:57 +01:00
|
|
|
setTabPosition(item);
|
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
|
|
|
|
2014-03-07 18:03:42 +01:00
|
|
|
item->setIcon(IconProvider::iconForUrl(url));
|
2012-04-22 17:09:43 +02:00
|
|
|
item->setText(url.toEncoded());
|
2012-05-05 16:06:24 +02:00
|
|
|
item->setData(query.value(0), IdRole);
|
|
|
|
item->setData(query.value(2), TitleRole);
|
2014-03-01 13:02:57 +01:00
|
|
|
item->setData(url, UrlRole);
|
2012-05-05 16:06:24 +02:00
|
|
|
item->setData(QVariant(false), BookmarkRole);
|
2014-03-01 13:02:57 +01:00
|
|
|
setTabPosition(item);
|
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();
|
|
|
|
}
|
|
|
|
|
2014-03-01 13:02:57 +01:00
|
|
|
QSqlQuery LocationCompleterModel::createQuery(const QString &searchString, int limit, bool exactMatch) const
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-18 16:17:22 +01: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
|
|
|
|
2012-08-25 13:08:06 +02:00
|
|
|
sqlQuery.addBindValue(limit);
|
|
|
|
|
|
|
|
return sqlQuery;
|
|
|
|
}
|
2012-12-04 14:29:27 +01:00
|
|
|
|
2014-03-01 13:02:57 +01:00
|
|
|
void LocationCompleterModel::setTabPosition(QStandardItem* item) const
|
2012-12-07 18:30:50 +01:00
|
|
|
{
|
2014-03-01 13:02:57 +01:00
|
|
|
Q_ASSERT(item);
|
2012-12-07 18:30:50 +01:00
|
|
|
|
2014-03-01 13:02:57 +01:00
|
|
|
if (!qzSettings->showSwitchTab) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QUrl url = item->data(UrlRole).toUrl();
|
2014-03-10 00:47:07 +01:00
|
|
|
const QList<BrowserWindow*> windows = mApp->windows();
|
2014-03-01 13:02:57 +01:00
|
|
|
|
|
|
|
foreach (BrowserWindow* window, windows) {
|
|
|
|
QList<WebTab*> tabs = window->tabWidget()->allTabs();
|
|
|
|
for (int i = 0; i < tabs.count(); ++i) {
|
|
|
|
WebTab* tab = tabs.at(i);
|
|
|
|
if (tab->url() == url) {
|
|
|
|
item->setData(QVariant::fromValue<void*>(static_cast<void*>(window)), TabPositionWindowRole);
|
|
|
|
item->setData(i, TabPositionTabRole);
|
|
|
|
return;
|
2012-12-04 14:29:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-01 13:02:57 +01:00
|
|
|
|
|
|
|
// Tab wasn't found
|
|
|
|
item->setData(QVariant::fromValue<void*>(static_cast<void*>(0)), TabPositionWindowRole);
|
|
|
|
item->setData(-1, TabPositionTabRole);
|
2012-12-04 14:29:27 +01:00
|
|
|
}
|
2012-12-07 18:30:50 +01:00
|
|
|
|
2014-03-01 13:02:57 +01:00
|
|
|
void LocationCompleterModel::refreshTabPositions() const
|
2012-12-07 18:30:50 +01:00
|
|
|
{
|
|
|
|
for (int row = 0; row < rowCount(); ++row) {
|
2014-03-01 13:02:57 +01:00
|
|
|
QStandardItem* itm = item(row);
|
|
|
|
if (itm) {
|
|
|
|
setTabPosition(itm);
|
2012-12-07 18:30:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|