2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2012-01-01 15:29:55 +01:00
|
|
|
* Copyright (C) 2010-2012 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"
|
2011-04-25 20:56:45 +02:00
|
|
|
#include "iconprovider.h"
|
|
|
|
#include "mainapplication.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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
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-15 15:41:48 +02:00
|
|
|
int limit = string.size() < 3 ? 25 : 15;
|
|
|
|
QString searchString = QString("%%1%").arg(string);
|
2012-04-20 14:03:08 +02:00
|
|
|
QList<QUrl> urlList;
|
2012-04-15 15:41:48 +02:00
|
|
|
|
|
|
|
QSqlQuery query;
|
2012-05-05 16:06:24 +02:00
|
|
|
query.prepare("SELECT id, url, title, icon FROM bookmarks WHERE title LIKE ? OR url LIKE ? LIMIT ?");
|
2012-04-15 15:41:48 +02:00
|
|
|
query.addBindValue(searchString);
|
|
|
|
query.addBindValue(searchString);
|
|
|
|
query.addBindValue(limit);
|
|
|
|
query.exec();
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
while (query.next()) {
|
2012-04-20 14:03:08 +02:00
|
|
|
QStandardItem* item = new QStandardItem();
|
2012-05-05 16:06:24 +02:00
|
|
|
const QUrl &url = query.value(1).toUrl();
|
2012-04-20 14:03:08 +02:00
|
|
|
|
2012-05-05 16:06:24 +02:00
|
|
|
item->setIcon(qIconProvider->iconFromImage(QImage::fromData(query.value(3).toByteArray())));
|
2012-04-20 14:03:08 +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);
|
|
|
|
item->setData(QVariant(true), BookmarkRole);
|
2012-04-22 17:09:43 +02:00
|
|
|
appendRow(item);
|
2012-04-20 14:03:08 +02:00
|
|
|
urlList.append(url);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-04-20 14:03:08 +02:00
|
|
|
limit -= query.size();
|
|
|
|
|
2012-05-05 16:06:24 +02:00
|
|
|
query.prepare("SELECT id, url, title FROM history WHERE title LIKE ? OR url LIKE ? ORDER BY count DESC LIMIT ?");
|
2012-04-15 15:41:48 +02:00
|
|
|
query.addBindValue(searchString);
|
|
|
|
query.addBindValue(searchString);
|
2012-04-20 14:03:08 +02:00
|
|
|
query.addBindValue(limit);
|
2012-04-15 15:41:48 +02:00
|
|
|
query.exec();
|
|
|
|
|
|
|
|
while (query.next()) {
|
2012-04-20 14:03:08 +02:00
|
|
|
QStandardItem* item = new QStandardItem();
|
2012-05-05 16:06:24 +02:00
|
|
|
const QUrl &url = query.value(1).toUrl();
|
2012-04-20 14:03:08 +02:00
|
|
|
|
|
|
|
if (urlList.contains(url)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-04-22 17:09:43 +02:00
|
|
|
appendRow(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2012-05-05 16:06:24 +02: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-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
|
|
|
}
|