1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 10:46:35 +01:00

[LocationCompleter] Speedup searching in Bookmarks & History

See #1135
This commit is contained in:
nowrep 2014-02-18 16:17:22 +01:00
parent 7323fb1cad
commit 65a47fd450
4 changed files with 33 additions and 118 deletions

View File

@ -63,87 +63,6 @@ bool Bookmarks::showOnlyIconsInToolbar() const
return m_showOnlyIconsInToolbar;
}
void Bookmarks::exportToHtml(const QString &fileName)
{
Q_UNUSED(fileName)
#if 0
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
qWarning() << "Bookmarks::exportHtml Cannot open file for writing!" << file.errorString();
}
QTextStream out(&file);
out << "<!DOCTYPE NETSCAPE-Bookmark-file-1>" << endl;
out << "<!-- This is an automatically generated file." << endl;
out << " It will be read and overwritten." << endl;
out << " DO NOT EDIT! -->" << endl;
out << "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\">" << endl;
out << "<TITLE>Bookmarks</TITLE>" << endl;
out << "<H1>Bookmarks</H1>" << endl;
out << "<DL><p>" << endl;
QString indent = " ";
QList<QPair<QString, bool> > allFolders;
QPair<QString, bool> menu;
menu.first = "bookmarksMenu";
menu.second = false;
QPair<QString, bool> toolbar;
toolbar.first = "bookmarksToolbar";
toolbar.second = false;
allFolders.append(menu);
allFolders.append(toolbar);
QSqlQuery query;
query.exec("SELECT name, subfolder FROM folders");
while (query.next()) {
QPair<QString, bool> pair;
pair.first = query.value(0).toString();
pair.second = query.value(1).toString() == QLatin1String("yes");
allFolders.append(pair);
}
for (int i = 0; i < allFolders.size(); ++i) {
QPair<QString, bool> pair = allFolders.at(i);
out << indent << "<DT><H3 TOOLBAR_SUBFOLDER=\"" << (pair.second ? "yes" : "no") << "\">" << pair.first << "</H3>" << endl;
out << indent << "<DL><p>" << endl;
QSqlQuery q;
q.prepare("SELECT title, url FROM bookmarks WHERE folder = ?");
q.addBindValue(pair.first);
q.exec();
while (q.next()) {
QString title = q.value(0).toString();
QString url = q.value(1).toString();
out << indent << indent << "<DT><A HREF=\"" << url << "\">" << title << "</A>" << endl;
}
out << indent << "</DL><p>" << endl;
}
query.exec("SELECT title, url FROM bookmarks WHERE folder='' OR folder='unsorted'");
while (query.next()) {
QString title = query.value(0).toString();
QString url = query.value(1).toString();
out << indent << "<DT><A HREF=\"" << url << "\">" << title << "</A>" << endl;
}
out << "</DL><p>" << endl;
#endif
}
BookmarkItem* Bookmarks::rootItem() const
{
return m_root;
@ -196,10 +115,10 @@ QList<BookmarkItem*> Bookmarks::searchBookmarks(const QUrl &url) const
return items;
}
QList<BookmarkItem*> Bookmarks::searchBookmarks(const QString &string, Qt::CaseSensitivity sensitive) const
QList<BookmarkItem*> Bookmarks::searchBookmarks(const QString &string, int limit, Qt::CaseSensitivity sensitive) const
{
QList<BookmarkItem*> items;
search(&items, m_root, string, sensitive);
search(&items, m_root, string, limit, sensitive);
return items;
}
@ -467,16 +386,20 @@ void Bookmarks::search(QList<BookmarkItem*>* items, BookmarkItem* parent, const
}
}
void Bookmarks::search(QList<BookmarkItem*>* items, BookmarkItem* parent, const QString &string, Qt::CaseSensitivity sensitive) const
void Bookmarks::search(QList<BookmarkItem*>* items, BookmarkItem* parent, const QString &string, int limit, Qt::CaseSensitivity sensitive) const
{
Q_ASSERT(items);
Q_ASSERT(parent);
if (limit == items->count()) {
return;
}
switch (parent->type()) {
case BookmarkItem::Root:
case BookmarkItem::Folder:
foreach (BookmarkItem* child, parent->children()) {
search(items, child, string, sensitive);
search(items, child, string, limit, sensitive);
}
break;

View File

@ -39,7 +39,6 @@ public:
void saveSettings();
bool showOnlyIconsInToolbar() const;
void exportToHtml(const QString &fileName);
BookmarkItem* rootItem() const;
BookmarkItem* toolbarFolder() const;
@ -54,8 +53,8 @@ public:
// Search bookmarks (urls only) for exact url match
QList<BookmarkItem*> searchBookmarks(const QUrl &url) const;
// Search bookmarks (urls only) for contains match through all properties
QList<BookmarkItem*> searchBookmarks(const QString &string, Qt::CaseSensitivity sensitive = Qt::CaseInsensitive) const;
// Search bookmarks for contains match through all properties
QList<BookmarkItem*> searchBookmarks(const QString &string, int limit = -1, Qt::CaseSensitivity sensitive = Qt::CaseInsensitive) const;
void addBookmark(BookmarkItem* parent, BookmarkItem* item);
void insertBookmark(BookmarkItem* parent, int row, BookmarkItem* item);
@ -86,7 +85,7 @@ private:
QVariantList writeBookmarks(BookmarkItem* parent);
void search(QList<BookmarkItem*>* items, BookmarkItem* parent, const QUrl &url) const;
void search(QList<BookmarkItem*>* items, BookmarkItem* parent, const QString &string, Qt::CaseSensitivity sensitive) const;
void search(QList<BookmarkItem*>* items, BookmarkItem* parent, const QString &string, int limit, Qt::CaseSensitivity sensitive) const;
BookmarkItem* m_root;
BookmarkItem* m_folderToolbar;

View File

@ -34,8 +34,15 @@ LocationCompleterModel::LocationCompleterModel(QObject* parent)
static bool countBiggerThan(const QStandardItem* i1, const QStandardItem* i2)
{
return i1->data(LocationCompleterModel::CountRole).toInt() >
i2->data(LocationCompleterModel::CountRole).toInt();
// 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;
}
void LocationCompleterModel::refreshCompletions(const QString &string)
@ -54,20 +61,19 @@ void LocationCompleterModel::refreshCompletions(const QString &string)
clear();
Type showType = (Type) qzSettings->showLocationSuggestions;
int limit = string.size() < 3 ? 25 : 15;
const int bookmarksLimit = 10;
const int historyLimit = 20;
QList<QUrl> urlList;
QList<QStandardItem*> itemList;
Type showType = (Type) qzSettings->showLocationSuggestions;
if (showType == HistoryAndBookmarks || showType == Bookmarks) {
QList<BookmarkItem*> bookmarks = mApp->bookmarks()->searchBookmarks(string);
QList<BookmarkItem*> bookmarks = mApp->bookmarks()->searchBookmarks(string, bookmarksLimit);
foreach (BookmarkItem* bookmark, bookmarks) {
Q_ASSERT(bookmark->isUrl());
QStandardItem* item = new QStandardItem();
item->setIcon(bookmark->icon());
item->setText(bookmark->url().toEncoded());
item->setData(-1, IdRole);
@ -83,18 +89,20 @@ void LocationCompleterModel::refreshCompletions(const QString &string)
urlList.append(bookmark->url());
itemList.append(item);
}
limit -= itemList.count();
}
if (showType == HistoryAndBookmarks || showType == History) {
QSqlQuery query = createQuery(string, "count DESC", urlList, limit);
QSqlQuery query = createQuery(string, historyLimit);
query.exec();
while (query.next()) {
QStandardItem* item = new QStandardItem();
const QUrl url = query.value(1).toUrl();
if (urlList.contains(url)) {
continue;
}
QStandardItem* item = new QStandardItem();
item->setIcon(_iconForUrl(url));
item->setText(url.toEncoded());
item->setData(query.value(0), IdRole);
@ -183,8 +191,7 @@ QString LocationCompleterModel::completeDomain(const QString &text)
return sqlQuery.value(0).toUrl().host();
}
QSqlQuery LocationCompleterModel::createQuery(const QString &searchString, const QString &orderBy,
const QList<QUrl> &alreadyFound, int limit, bool exactMatch)
QSqlQuery LocationCompleterModel::createQuery(const QString &searchString, int limit, bool exactMatch)
{
QStringList searchList;
QString query = QLatin1String("SELECT id, url, title, count FROM history WHERE ");
@ -203,17 +210,7 @@ QSqlQuery LocationCompleterModel::createQuery(const QString &searchString, const
}
}
for (int i = 0; i < alreadyFound.count(); i++) {
query.append(QLatin1String("AND (NOT url=?) "));
}
query.append(QLatin1String("GROUP BY url "));
if (!orderBy.isEmpty()) {
query.append(QLatin1String("ORDER BY ") + orderBy);
}
query.append(QLatin1String(" LIMIT ?"));
query.append(QLatin1String("LIMIT ?"));
QSqlQuery sqlQuery;
sqlQuery.prepare(query);
@ -229,10 +226,6 @@ QSqlQuery LocationCompleterModel::createQuery(const QString &searchString, const
}
}
foreach (const QUrl &url, alreadyFound) {
sqlQuery.addBindValue(url);
}
sqlQuery.addBindValue(limit);
return sqlQuery;

View File

@ -61,7 +61,7 @@ private:
Nothing = 4
};
QSqlQuery createQuery(const QString &searchString, const QString &orderBy, const QList<QUrl> &alreadyFound,
QSqlQuery createQuery(const QString &searchString,
int limit, bool exactMatch = false);
TabPosition tabPositionForUrl(const QUrl &url) const;