Falkon Develop
Cross-platform Qt-based web browser
locationcompletermodel.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
19#include "mainapplication.h"
20#include "iconprovider.h"
21#include "bookmarkitem.h"
22#include "bookmarks.h"
23#include "qzsettings.h"
24#include "browserwindow.h"
25#include "tabwidget.h"
26#include "sqldatabase.h"
27
29 : QStandardItemModel(parent)
30{
31}
32
33void LocationCompleterModel::setCompletions(const QList<QStandardItem*> &items)
34{
35 clear();
36 addCompletions(items);
37}
38
39void LocationCompleterModel::addCompletions(const QList<QStandardItem*> &items)
40{
41 for (QStandardItem *item : items) {
42 item->setIcon(QPixmap::fromImage(item->data(ImageRole).value<QImage>()));
43 setTabPosition(item);
44 if (item->icon().isNull()) {
45 item->setIcon(IconProvider::emptyWebIcon());
46 }
47 appendRow(QList<QStandardItem*>{item});
48 }
49}
50
51QList<QStandardItem*> LocationCompleterModel::suggestionItems() const
52{
53 QList<QStandardItem*> items;
54 for (int i = 0; i < rowCount(); ++i) {
55 QStandardItem *it = item(i);
56 if (it->data(SearchSuggestionRole).toBool()) {
57 items.append(it);
58 }
59 }
60 return items;
61}
62
63QSqlQuery LocationCompleterModel::createDomainQuery(const QString &text)
64{
65 if (text.isEmpty() || text == QLatin1String("www.")) {
66 return QSqlQuery(SqlDatabase::instance()->database());
67 }
68
69 bool withoutWww = text.startsWith(QLatin1Char('w')) && !text.startsWith(QLatin1String("www."));
70 QString query = QSL("SELECT url FROM history WHERE ");
71
72 if (withoutWww) {
73 query.append(QLatin1String("url NOT LIKE ? AND url NOT LIKE ? AND "));
74 }
75 else {
76 query.append(QLatin1String("url LIKE ? OR url LIKE ? OR "));
77 }
78
79 query.append(QLatin1String("(url LIKE ? OR url LIKE ?) ORDER BY date DESC LIMIT 1"));
80
81 QSqlQuery sqlQuery(SqlDatabase::instance()->database());
82 sqlQuery.prepare(query);
83
84 if (withoutWww) {
85 sqlQuery.addBindValue(QSL("http://www.%"));
86 sqlQuery.addBindValue(QSL("https://www.%"));
87 sqlQuery.addBindValue(QSL("http://%1%").arg(text));
88 sqlQuery.addBindValue(QSL("https://%1%").arg(text));
89 }
90 else {
91 sqlQuery.addBindValue(QSL("http://%1%").arg(text));
92 sqlQuery.addBindValue(QSL("https://%1%").arg(text));
93 sqlQuery.addBindValue(QSL("http://www.%1%").arg(text));
94 sqlQuery.addBindValue(QSL("https://www.%1%").arg(text));
95 }
96
97 return sqlQuery;
98}
99
100QSqlQuery LocationCompleterModel::createHistoryQuery(const QString &searchString, int limit, bool exactMatch)
101{
102 QStringList searchList;
103 QString query = QLatin1String("SELECT id, url, title, count FROM history WHERE ");
104
105 if (exactMatch) {
106 query.append(QLatin1String("title LIKE ? OR url LIKE ? "));
107 }
108 else {
109 searchList = searchString.split(QLatin1Char(' '), Qt::SkipEmptyParts);
110 const int slSize = searchList.size();
111 for (int i = 0; i < slSize; ++i) {
112 query.append(QLatin1String("(title LIKE ? OR url LIKE ?) "));
113 if (i < slSize - 1) {
114 query.append(QLatin1String("AND "));
115 }
116 }
117 }
118
119 query.append(QLatin1String("ORDER BY date DESC LIMIT ?"));
120
121 QSqlQuery sqlQuery(SqlDatabase::instance()->database());
122 sqlQuery.prepare(query);
123
124 if (exactMatch) {
125 sqlQuery.addBindValue(QSL("%%1%").arg(searchString));
126 sqlQuery.addBindValue(QSL("%%1%").arg(searchString));
127 }
128 else {
129 for (const QString &str : std::as_const(searchList)) {
130 sqlQuery.addBindValue(QSL("%%1%").arg(str));
131 sqlQuery.addBindValue(QSL("%%1%").arg(str));
132 }
133 }
134
135 sqlQuery.addBindValue(limit);
136
137 return sqlQuery;
138}
139
140void LocationCompleterModel::setTabPosition(QStandardItem* item) const
141{
142 Q_ASSERT(item);
143
144 item->setData(-1, TabPositionTabRole);
145
146 if (!qzSettings->showSwitchTab || item->data(VisitSearchItemRole).toBool()) {
147 return;
148 }
149
150 const QUrl url = item->data(UrlRole).toUrl();
151 const QList<BrowserWindow*> windows = mApp->windows();
152
153 for (BrowserWindow* window : windows) {
154 QList<WebTab*> tabs = window->tabWidget()->allTabs();
155 for (int i = 0; i < tabs.count(); ++i) {
156 WebTab* tab = tabs.at(i);
157 if (tab->url() == url) {
158 item->setData(QVariant::fromValue<void*>(static_cast<void*>(window)), TabPositionWindowRole);
159 item->setData(i, TabPositionTabRole);
160 return;
161 }
162 }
163 }
164}
165
166void LocationCompleterModel::refreshTabPositions() const
167{
168 for (int row = 0; row < rowCount(); ++row) {
169 QStandardItem* itm = item(row);
170 if (itm) {
171 setTabPosition(itm);
172 }
173 }
174}
static QIcon emptyWebIcon()
LocationCompleterModel(QObject *parent=nullptr)
void setCompletions(const QList< QStandardItem * > &items)
void addCompletions(const QList< QStandardItem * > &items)
static QSqlQuery createHistoryQuery(const QString &searchString, int limit, bool exactMatch=false)
static QSqlQuery createDomainQuery(const QString &text)
QList< QStandardItem * > suggestionItems() const
static SqlDatabase * instance()
Definition: webtab.h:40
QUrl url() const
Definition: webtab.cpp:263
#define mApp
i
Definition: i18n.py:23
#define QSL(x)
Definition: qzcommon.h:40
#define qzSettings
Definition: qzsettings.h:69