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)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-03-15 19:36:03 +01:00
|
|
|
void LocationCompleterModel::setCompletions(const QList<QStandardItem*> &items)
|
2012-09-02 15:36:20 +02:00
|
|
|
{
|
2014-03-15 19:36:03 +01:00
|
|
|
foreach (QStandardItem* item, items) {
|
|
|
|
item->setIcon(QPixmap::fromImage(item->data(ImageRole).value<QImage>()));
|
|
|
|
setTabPosition(item);
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2014-03-15 19:36:03 +01:00
|
|
|
if (item->icon().isNull()) {
|
|
|
|
item->setIcon(IconProvider::emptyWebIcon());
|
2012-08-10 18:06:29 +02:00
|
|
|
}
|
2012-04-22 17:09:43 +02:00
|
|
|
}
|
2012-09-02 15:36:20 +02:00
|
|
|
|
2014-03-15 19:36:03 +01:00
|
|
|
clear();
|
|
|
|
appendColumn(items);
|
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
|
|
|
|
2014-03-15 19:36:03 +01:00
|
|
|
QSqlQuery LocationCompleterModel::createDomainQuery(const QString &text)
|
2013-05-10 22:33:36 +02:00
|
|
|
{
|
|
|
|
if (text.isEmpty() || text == QLatin1String("www.")) {
|
2014-03-15 19:36:03 +01:00
|
|
|
return QSqlQuery();
|
2013-05-10 22:33:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2014-03-15 19:36:03 +01:00
|
|
|
return sqlQuery;
|
2013-05-10 22:33:36 +02:00
|
|
|
}
|
|
|
|
|
2014-03-15 19:36:03 +01:00
|
|
|
QSqlQuery LocationCompleterModel::createHistoryQuery(const QString &searchString, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|