1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
/* ============================================================
*
* This file is a part of the rekonq project
*
* Copyright (C) 2009 by Andrea Diamantini <adjam7 at gmail dot com>
*
*
* 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 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* 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/>.
*
* ============================================================ */
// Self Includes
#include "urlresolver.h"
#include "urlresolver.moc"
// Local Includes
#include "historymanager.h"
#include "bookmarkprovider.h"
#include "searchengine.h"
// KDE Includes
#include <KBookmark>
#include <KUriFilter>
#include <KCompletion>
#include <KService>
#include <KConfig>
#include <KConfigGroup>
#include <KProtocolInfo>
// Qt Includes
#include <QByteArray>
// defines
#define MAX_ELEMENTS 8
#define MIN_SUGGESTIONS 3
// NOTE
// default kurifilter plugin list (at least in my box):
// 1. "kshorturifilter"
// 2. "kurisearchfilter"
// 3. "localdomainurifilter"
// 4 ."kuriikwsfilter"
// 5. "fixhosturifilter"
// ------------------------------------------------------------------------
KService::Ptr UrlResolver::_searchEngine;
QRegExp UrlResolver::_browseRegexp;
QRegExp UrlResolver::_searchEnginesRegexp;
UrlResolver::UrlResolver(const QString &typedUrl)
: QObject()
, _typedString(typedUrl.trimmed())
{
if (!_searchEngine )
setSearchEngine(SearchEngine::defaultEngine());
if ( _browseRegexp.isEmpty() )
{
kDebug() << "browse regexp empty. Setting value..";
QString protocol = QString("^(%1)").arg(KProtocolInfo::protocols().join("|"));
QString localhost = "^localhost";
QString local = "^/";
QString ipv4 = "^0*([1-9]?\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\.0*([1-9]?\\d|1\\d\\d|2[0-4]\\d|25[0-5])"\
"\\.0*([1-9]?\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\.0*([1-9]?\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
QString ipv6 = "^([0-9a-fA-F]{4}|0)(\\:([0-9a-fA-F]{4}|0)){7}";
QString address = "[\\d\\w-.]+\\.(a[cdefgilmnoqrstuwz]|b[abdefghijmnorstvwyz]|"\
"c[acdfghiklmnoruvxyz]|d[ejkmnoz]|e[ceghrstu]|f[ijkmnor]|g[abdefghilmnpqrstuwy]|"\
"h[kmnrtu]|i[delmnoqrst]|j[emop]|k[eghimnprwyz]|l[abcikrstuvy]|"\
"m[acdghklmnopqrstuvwxyz]|n[acefgilopruz]|om|p[aefghklmnrstwy]|qa|r[eouw]|"\
"s[abcdeghijklmnortuvyz]|t[cdfghjkmnoprtvwz]|u[augkmsyz]|v[aceginu]|w[fs]|"\
"y[etu]|z[amw]|aero|arpa|biz|com|coop|edu|info|int|gov|local|mil|museum|name|net|org|"\
"pro)";
_browseRegexp = QRegExp('(' + protocol + ")|(" + localhost + ")|(" + local + ")|(" + address + ")|(" + ipv6 + ")|(" + ipv4 +')');
}
if ( _searchEnginesRegexp.isEmpty() )
{
QString reg;
QString engineUrl;
Q_FOREACH(KService::Ptr s, SearchEngine::favorites())
{
engineUrl = QRegExp::escape(s->property("Query").toString()).replace("\\\\\\{@\\}","[\\d\\w-.]+");
if (reg.isEmpty())
reg = '(' + engineUrl + ')';
else
reg = reg + "|(" + engineUrl + ')';
}
_searchEnginesRegexp = QRegExp(reg);
}
}
UrlSearchList UrlResolver::orderedSearchItems()
{
if( _typedString.startsWith( QL1S("about:") ) )
{
UrlSearchList list;
UrlSearchItem home(UrlSearchItem::Browse, QString("about:home"), QL1S("home") );
list << home;
UrlSearchItem favs(UrlSearchItem::Browse, QString("about:favorites"), QL1S("favorites") );
list << favs;
UrlSearchItem clos(UrlSearchItem::Browse, QString("about:closedTabs"), QL1S("closed tabs") );
list << clos;
UrlSearchItem book(UrlSearchItem::Browse, QString("about:bookmarks"), QL1S("bookmarks") );
list << book;
UrlSearchItem hist(UrlSearchItem::Browse, QString("about:history"), QL1S("history") );
list << hist;
UrlSearchItem down(UrlSearchItem::Browse, QString("about:downloads"), QL1S("downloads") );
list << down;
return list;
}
//compute lists
computeHistory();
computeQurlFromUserInput();
computeWebSearches();
computeBookmarks();
return orderLists();
}
UrlSearchList UrlResolver::orderLists()
{
// NOTE
// the logic here is : "we wanna suggest (at least) 10 elements"
// so we have (more or less) 2 from first results (1 from QUrl Resolutions, 1 from
// search engines).
// There are 8 remaining: if bookmarkResults + historyResults <= 8, catch all, else
// catch first 4 results from the two resulting lists :)
QTime myTime;
myTime.start();
UrlSearchList list;
if(_browseRegexp.indexIn(_typedString) != -1)
{
list << _qurlFromUserInput;
list << _webSearches;
}
else
{
list << _webSearches;
list << _qurlFromUserInput;
}
//find the history items that match the typed string
UrlSearchItem privileged = privilegedItem(&_history);
int historyCount = _history.count();
//find the bookmarks items that match the typed string
if (privileged.type == UrlSearchItem::Undefined)
{
privileged = privilegedItem(&_bookmarks);
}
else if(privileged.type == UrlSearchItem::History && _bookmarks.removeOne(privileged))
{
privileged.type |= UrlSearchItem::Bookmark;
}
int bookmarksCount = _bookmarks.count();
if (privileged.type != UrlSearchItem::Undefined)
{
list.prepend(privileged);
}
int availableEntries = MAX_ELEMENTS - list.count() - MIN_SUGGESTIONS;
UrlSearchList common;
int commonCount = 0;
// prefer items which are history items as well bookmarks item
// if there are more than 1000 bookmark results, the performance impact is noticeable
if(bookmarksCount < 1000)
{
//add as many items to the common list as there are available entries in the dropdown list
UrlSearchItem urlSearchItem;
for(int i = 0; i < _history.count(); i++)
{
if (_bookmarks.removeOne(_history.at(i)))
{
urlSearchItem = _history.takeAt(i);
urlSearchItem.type |= UrlSearchItem::Bookmark;
common << urlSearchItem;
commonCount++;
if(commonCount >= availableEntries)
{
break;
}
}
}
commonCount = common.count();
if(commonCount >= availableEntries)
{
common = common.mid(0, availableEntries);
_history = UrlSearchList();
_bookmarks = UrlSearchList();
availableEntries = 0;
}
else //remove all items from the history and bookmarks list up to the remaining entries in the dropdown list
{
availableEntries -= commonCount;
if(historyCount >= availableEntries)
{
_history = _history.mid(0, availableEntries);
}
if(bookmarksCount >= availableEntries)
{
_bookmarks = _bookmarks.mid(0, availableEntries);
}
}
}
else //if there are too many bookmarks items, remove all items up to the remaining entries in the dropdown list
{
if(historyCount >= availableEntries)
{
_history = _history.mid(0, availableEntries);
}
if(bookmarksCount >= availableEntries)
{
_bookmarks = _bookmarks.mid(0, availableEntries);
}
UrlSearchItem urlSearchItem;
for(int i = 0; i < _history.count(); i++)
{
if (_bookmarks.removeOne(_history.at(i)))
{
urlSearchItem = _history.takeAt(i);
urlSearchItem.type |= UrlSearchItem::Bookmark;
common << urlSearchItem;
}
}
availableEntries -= common.count();
}
historyCount = _history.count();
bookmarksCount = _bookmarks.count();
commonCount = common.count();
kDebug() << "HISTORY COUNT: " << historyCount;
//now fill the list to MAX_ELEMENTS
if(availableEntries > 0)
{
int historyEntries = ((int) (availableEntries / 2)) + availableEntries % 2;
int bookmarksEntries = availableEntries - historyEntries;
if (historyCount >= historyEntries && bookmarksCount >= bookmarksEntries)
{
_history = _history.mid(0, historyEntries);
_bookmarks = _bookmarks.mid(0, bookmarksEntries);
}
else if (historyCount < historyEntries && bookmarksCount >= bookmarksEntries)
{
if(historyCount + bookmarksCount > availableEntries)
{
_bookmarks = _bookmarks.mid(0, availableEntries - historyCount);
}
}
else if (historyCount >= historyEntries && bookmarksCount < bookmarksEntries)
{
if(historyCount + bookmarksCount > availableEntries)
{
_history = _history.mid(0, availableEntries - bookmarksCount);
}
}
}
availableEntries -= _history.count();
availableEntries -= _bookmarks.count();
if (_suggestions.count() > availableEntries + MIN_SUGGESTIONS)
{
_suggestions = _suggestions.mid(0, availableEntries + MIN_SUGGESTIONS);
}
list = list + _history + common + _bookmarks + _suggestions;
qWarning() << "orderedSearchItems leave: " << " elapsed: " << myTime.elapsed();
return list;
}
//////////////////////////////////////////////////////////////////////////
// PRIVATE ENGINES
// QUrl from User Input (easily the best solution... )
void UrlResolver::computeQurlFromUserInput()
{
QString url = _typedString;
QUrl urlFromUserInput = QUrl::fromUserInput(url);
if (urlFromUserInput.isValid())
{
QString gTitle = i18nc("Browse a website", "Browse");
UrlSearchItem gItem(UrlSearchItem::Browse, urlFromUserInput.toString(), gTitle);
_qurlFromUserInput << gItem;
}
}
// webSearches
void UrlResolver::computeWebSearches()
{
QString query = _typedString;
KService::Ptr engine = SearchEngine::fromString(_typedString);
if (engine)
{
query = query.remove(0, _typedString.indexOf(SearchEngine::delimiter()) + 1);
_searchEngine = engine;
}
if(_searchEngine)
{
UrlSearchItem item = UrlSearchItem(UrlSearchItem::Search, SearchEngine::buildQuery(_searchEngine, query), _searchEngine->name());
UrlSearchList list;
list << item;
_webSearches = list;
}
}
// history
void UrlResolver::computeHistory()
{
QList<HistoryItem> found = Application::historyManager()->find(_typedString);
qSort(found);
Q_FOREACH(const HistoryItem &i, found)
{
if (_searchEnginesRegexp.indexIn(i.url) == -1) //filter all urls that are search engine results
{
UrlSearchItem gItem(UrlSearchItem::History, i.url, i.title);
_history << gItem;
}
}
}
// bookmarks
void UrlResolver::computeBookmarks()
{
QList<KBookmark> found = Application::bookmarkProvider()->find(_typedString);
kDebug() << "FOUND: " << found.count();
Q_FOREACH(const KBookmark &b, found)
{
UrlSearchItem gItem(UrlSearchItem::Bookmark, b.url().url(), b.fullText());
_bookmarks << gItem;
}
}
// opensearch suggestion
void UrlResolver::computeSuggestions()
{
// if a string startsWith /, it is probably a local path
// so, no need for suggestions...
if(_typedString.startsWith('/') || !Application::opensearchManager()->isSuggestionAvailable())
{
UrlSearchList list;
emit suggestionsReady(list, _typedString);
return;
}
connect(Application::opensearchManager(),
SIGNAL(suggestionReceived(const QString &, const ResponseList &)),
this,
SLOT(suggestionsReceived(const QString &, const ResponseList &)));
Application::opensearchManager()->requestSuggestion(_typedString);
}
void UrlResolver::suggestionsReceived(const QString &text, const ResponseList &suggestions)
{
if(text != _typedString)
return;
UrlSearchList sugList;
Q_FOREACH(const Response &i, suggestions)
{
QString url = i.url;
if (url.isEmpty())
{
url = SearchEngine::buildQuery(searchEngine(), i.title);
}
UrlSearchItem gItem(UrlSearchItem::Suggestion, url, i.title, i.description, i.image, i.image_width, i.image_height);
sugList << gItem;
}
emit suggestionsReady(sugList, _typedString);
this->deleteLater();
}
UrlSearchItem UrlResolver::privilegedItem(UrlSearchList* list)
{
UrlSearchItem item;
QString dot = QString(QL1C('.'));
QString test1 = QString(QL1C('/')) + _typedString + dot;
QString test2 = dot + _typedString + dot;
for(int i = 0; i<list->count(); i++)
{
item = list->at(i);
//TODO: move this to AwesomeUrlCompletion::substringCompletion and add a priviledged flag to UrlSearchItem
if (item.url.contains(test1) || item.url.contains(test2))
{
list->removeAt(i);
return item;
}
}
return UrlSearchItem();
}
|