2012-04-22 17:09:43 +02:00
|
|
|
/* ============================================================
|
2017-08-25 17:11:29 +02:00
|
|
|
* Falkon - Qt web browser
|
2017-08-11 12:26:50 +02:00
|
|
|
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
|
2012-04-22 17:09:43 +02: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/>.
|
|
|
|
* ============================================================ */
|
|
|
|
#include "locationcompleter.h"
|
|
|
|
#include "locationcompletermodel.h"
|
|
|
|
#include "locationcompleterview.h"
|
2014-03-15 19:36:03 +01:00
|
|
|
#include "locationcompleterrefreshjob.h"
|
2012-04-22 17:09:43 +02:00
|
|
|
#include "locationbar.h"
|
2014-03-01 13:02:57 +01:00
|
|
|
#include "mainapplication.h"
|
|
|
|
#include "browserwindow.h"
|
|
|
|
#include "tabbedwebview.h"
|
|
|
|
#include "tabwidget.h"
|
|
|
|
#include "history.h"
|
|
|
|
#include "bookmarks.h"
|
|
|
|
#include "bookmarkitem.h"
|
2014-01-08 10:29:01 +01:00
|
|
|
#include "qzsettings.h"
|
2017-08-11 15:20:30 +02:00
|
|
|
#include "opensearchengine.h"
|
|
|
|
#include "networkmanager.h"
|
2012-04-22 17:09:43 +02:00
|
|
|
|
2017-06-07 16:01:06 +02:00
|
|
|
#include <QWindow>
|
|
|
|
|
2012-04-22 17:09:43 +02:00
|
|
|
LocationCompleterView* LocationCompleter::s_view = 0;
|
|
|
|
LocationCompleterModel* LocationCompleter::s_model = 0;
|
|
|
|
|
|
|
|
LocationCompleter::LocationCompleter(QObject* parent)
|
|
|
|
: QObject(parent)
|
2014-03-01 13:02:57 +01:00
|
|
|
, m_window(0)
|
2012-04-22 17:09:43 +02:00
|
|
|
, m_locationBar(0)
|
2014-03-15 19:36:03 +01:00
|
|
|
, m_lastRefreshTimestamp(0)
|
2014-03-15 23:22:35 +01:00
|
|
|
, m_popupClosed(false)
|
2012-04-22 17:09:43 +02:00
|
|
|
{
|
|
|
|
if (!s_view) {
|
|
|
|
s_model = new LocationCompleterModel;
|
|
|
|
s_view = new LocationCompleterView;
|
|
|
|
s_view->setModel(s_model);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-01 13:02:57 +01:00
|
|
|
void LocationCompleter::setMainWindow(BrowserWindow* window)
|
|
|
|
{
|
|
|
|
m_window = window;
|
|
|
|
}
|
|
|
|
|
2012-04-22 17:09:43 +02:00
|
|
|
void LocationCompleter::setLocationBar(LocationBar* locationBar)
|
|
|
|
{
|
|
|
|
m_locationBar = locationBar;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationCompleter::closePopup()
|
|
|
|
{
|
2014-03-15 23:22:35 +01:00
|
|
|
m_popupClosed = true;
|
2012-04-22 17:09:43 +02:00
|
|
|
s_view->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationCompleter::complete(const QString &string)
|
|
|
|
{
|
2014-03-15 23:22:35 +01:00
|
|
|
QString trimmedStr = string.trimmed();
|
2012-04-22 17:09:43 +02:00
|
|
|
|
2014-03-15 23:22:35 +01:00
|
|
|
// Indicates that new completion was requested by user
|
|
|
|
// Eg. popup was not closed yet this completion session
|
|
|
|
m_popupClosed = false;
|
2014-01-08 10:29:01 +01:00
|
|
|
|
2014-06-08 13:56:20 +02:00
|
|
|
emit cancelRefreshJob();
|
|
|
|
|
2014-03-15 23:22:35 +01:00
|
|
|
LocationCompleterRefreshJob* job = new LocationCompleterRefreshJob(trimmedStr);
|
|
|
|
connect(job, SIGNAL(finished()), this, SLOT(refreshJobFinished()));
|
2014-06-08 13:56:20 +02:00
|
|
|
connect(this, SIGNAL(cancelRefreshJob()), job, SLOT(jobCancelled()));
|
2017-08-11 15:20:30 +02:00
|
|
|
|
|
|
|
if (qzSettings->searchFromAddressBar && trimmedStr.length() > 2) {
|
|
|
|
if (!m_openSearchEngine) {
|
|
|
|
m_openSearchEngine = new OpenSearchEngine(this);
|
|
|
|
m_openSearchEngine->setNetworkAccessManager(mApp->networkManager());
|
|
|
|
connect(m_openSearchEngine, &OpenSearchEngine::suggestions, this, &LocationCompleter::addSuggestions);
|
|
|
|
}
|
|
|
|
m_openSearchEngine->setSuggestionsUrl(LocationBar::searchEngine().suggestionsUrl);
|
|
|
|
m_openSearchEngine->setSuggestionsParameters(LocationBar::searchEngine().suggestionsParameters);
|
|
|
|
m_suggestionsTerm = trimmedStr;
|
|
|
|
m_openSearchEngine->requestSuggestions(m_suggestionsTerm);
|
|
|
|
}
|
2012-04-22 17:09:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocationCompleter::showMostVisited()
|
|
|
|
{
|
2013-02-11 13:33:02 +01:00
|
|
|
complete(QString());
|
2012-04-22 17:09:43 +02:00
|
|
|
}
|
|
|
|
|
2014-03-15 23:22:35 +01:00
|
|
|
void LocationCompleter::refreshJobFinished()
|
2012-04-22 17:09:43 +02:00
|
|
|
{
|
2014-03-15 23:22:35 +01:00
|
|
|
LocationCompleterRefreshJob* job = qobject_cast<LocationCompleterRefreshJob*>(sender());
|
|
|
|
Q_ASSERT(job);
|
2012-09-02 15:19:12 +02:00
|
|
|
|
2014-06-26 15:42:38 +02:00
|
|
|
// Don't show results of older jobs
|
2014-03-15 23:22:35 +01:00
|
|
|
// Also don't open the popup again when it was already closed
|
|
|
|
if (job->timestamp() > m_lastRefreshTimestamp && !m_popupClosed) {
|
|
|
|
s_model->setCompletions(job->completions());
|
|
|
|
m_lastRefreshTimestamp = job->timestamp();
|
|
|
|
|
|
|
|
showPopup();
|
2017-08-11 15:20:30 +02:00
|
|
|
addSuggestions(m_oldSuggestions);
|
2014-03-15 23:22:35 +01:00
|
|
|
|
2017-08-11 12:26:50 +02:00
|
|
|
if (!s_view->currentIndex().isValid() && s_model->index(0, 0).data(LocationCompleterModel::VisitSearchItemRole).toBool()) {
|
|
|
|
m_ignoreCurrentChanged = true;
|
|
|
|
s_view->setCurrentIndex(s_model->index(0, 0));
|
|
|
|
m_ignoreCurrentChanged = false;
|
|
|
|
}
|
|
|
|
|
2014-03-15 23:22:35 +01:00
|
|
|
if (qzSettings->useInlineCompletion) {
|
|
|
|
emit showDomainCompletion(job->domainCompletion());
|
2017-08-12 15:09:05 +02:00
|
|
|
|
|
|
|
m_originalText = m_locationBar->text();
|
|
|
|
s_view->setOriginalText(m_originalText);
|
2014-03-15 23:22:35 +01:00
|
|
|
}
|
2012-04-22 17:09:43 +02:00
|
|
|
}
|
|
|
|
|
2014-03-15 23:22:35 +01:00
|
|
|
job->deleteLater();
|
2012-04-22 17:09:43 +02:00
|
|
|
}
|
|
|
|
|
2013-02-22 15:20:28 +01:00
|
|
|
void LocationCompleter::slotPopupClosed()
|
2012-04-22 17:09:43 +02:00
|
|
|
{
|
2017-08-11 15:20:30 +02:00
|
|
|
m_oldSuggestions.clear();
|
|
|
|
|
2013-02-22 15:20:28 +01:00
|
|
|
disconnect(s_view, SIGNAL(closed()), this, SLOT(slotPopupClosed()));
|
2014-03-01 13:02:57 +01:00
|
|
|
disconnect(s_view, SIGNAL(indexActivated(QModelIndex)), this, SLOT(indexActivated(QModelIndex)));
|
|
|
|
disconnect(s_view, SIGNAL(indexCtrlActivated(QModelIndex)), this, SLOT(indexCtrlActivated(QModelIndex)));
|
|
|
|
disconnect(s_view, SIGNAL(indexShiftActivated(QModelIndex)), this, SLOT(indexShiftActivated(QModelIndex)));
|
|
|
|
disconnect(s_view, SIGNAL(indexDeleteRequested(QModelIndex)), this, SLOT(indexDeleteRequested(QModelIndex)));
|
|
|
|
disconnect(s_view->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(currentChanged(QModelIndex)));
|
2013-02-22 15:20:28 +01:00
|
|
|
|
|
|
|
emit popupClosed();
|
2012-04-22 17:09:43 +02:00
|
|
|
}
|
|
|
|
|
2017-08-11 15:20:30 +02:00
|
|
|
void LocationCompleter::addSuggestions(const QStringList &suggestions)
|
|
|
|
{
|
|
|
|
const auto suggestionItems = s_model->suggestionItems();
|
|
|
|
|
|
|
|
// Delete existing suggestions
|
|
|
|
for (QStandardItem *item : suggestionItems) {
|
|
|
|
s_model->takeRow(item->row());
|
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add new suggestions
|
|
|
|
QList<QStandardItem*> items;
|
|
|
|
for (const QString &suggestion : suggestions) {
|
|
|
|
QStandardItem* item = new QStandardItem();
|
|
|
|
item->setText(suggestion);
|
|
|
|
item->setData(suggestion, LocationCompleterModel::TitleRole);
|
|
|
|
item->setData(suggestion, LocationCompleterModel::UrlRole);
|
|
|
|
item->setData(m_suggestionsTerm, LocationCompleterModel::SearchStringRole);
|
|
|
|
item->setData(true, LocationCompleterModel::SearchSuggestionRole);
|
|
|
|
items.append(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
s_model->addCompletions(items);
|
|
|
|
adjustPopupSize();
|
|
|
|
m_oldSuggestions = suggestions;
|
|
|
|
}
|
|
|
|
|
2014-03-15 23:22:35 +01:00
|
|
|
void LocationCompleter::currentChanged(const QModelIndex &index)
|
2014-03-15 19:36:03 +01:00
|
|
|
{
|
2017-08-11 12:26:50 +02:00
|
|
|
if (m_ignoreCurrentChanged) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-15 23:22:35 +01:00
|
|
|
QString completion = index.data().toString();
|
2014-03-15 19:36:03 +01:00
|
|
|
|
2017-08-11 12:26:50 +02:00
|
|
|
bool completeDomain = index.data(LocationCompleterModel::VisitSearchItemRole).toBool();
|
|
|
|
|
|
|
|
// Domain completion was dismissed
|
|
|
|
if (completeDomain && completion == m_originalText) {
|
|
|
|
completeDomain = false;
|
|
|
|
}
|
2016-12-29 12:11:40 +01:00
|
|
|
|
2014-03-15 23:22:35 +01:00
|
|
|
if (completion.isEmpty()) {
|
2017-08-11 12:26:50 +02:00
|
|
|
completeDomain = true;
|
2014-03-15 23:22:35 +01:00
|
|
|
completion = m_originalText;
|
2014-03-15 19:36:03 +01:00
|
|
|
}
|
|
|
|
|
2017-08-11 12:26:50 +02:00
|
|
|
emit showCompletion(completion, completeDomain);
|
2014-03-15 19:36:03 +01:00
|
|
|
}
|
|
|
|
|
2014-03-01 13:02:57 +01:00
|
|
|
void LocationCompleter::indexActivated(const QModelIndex &index)
|
|
|
|
{
|
|
|
|
Q_ASSERT(index.isValid());
|
|
|
|
|
2015-11-24 18:42:04 +01:00
|
|
|
bool ok;
|
|
|
|
const int tabPos = index.data(LocationCompleterModel::TabPositionTabRole).toInt(&ok);
|
2014-03-01 13:02:57 +01:00
|
|
|
|
|
|
|
// Switch to tab with simple index activation
|
2015-11-24 18:42:04 +01:00
|
|
|
if (ok && tabPos > -1) {
|
2014-03-01 13:02:57 +01:00
|
|
|
BrowserWindow* window = static_cast<BrowserWindow*>(index.data(LocationCompleterModel::TabPositionWindowRole).value<void*>());
|
|
|
|
Q_ASSERT(window);
|
|
|
|
switchToTab(window, tabPos);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index.data(LocationCompleterModel::BookmarkRole).toBool()) {
|
|
|
|
BookmarkItem* bookmark = static_cast<BookmarkItem*>(index.data(LocationCompleterModel::BookmarkItemRole).value<void*>());
|
|
|
|
bookmark->updateVisitCount();
|
|
|
|
}
|
|
|
|
|
2017-10-07 19:54:41 +02:00
|
|
|
QString urlString = index.data(LocationCompleterModel::UrlRole).toString();
|
|
|
|
|
2017-08-11 12:26:50 +02:00
|
|
|
if (index.data(LocationCompleterModel::VisitSearchItemRole).toBool()) {
|
2017-10-07 19:54:41 +02:00
|
|
|
urlString = m_originalText;
|
2017-08-11 12:26:50 +02:00
|
|
|
}
|
|
|
|
|
2017-10-07 19:54:41 +02:00
|
|
|
loadString(urlString);
|
2014-03-01 13:02:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocationCompleter::indexCtrlActivated(const QModelIndex &index)
|
|
|
|
{
|
|
|
|
Q_ASSERT(index.isValid());
|
|
|
|
Q_ASSERT(m_window);
|
|
|
|
|
|
|
|
if (index.data(LocationCompleterModel::BookmarkRole).toBool()) {
|
|
|
|
BookmarkItem* bookmark = static_cast<BookmarkItem*>(index.data(LocationCompleterModel::BookmarkItemRole).value<void*>());
|
|
|
|
bookmark->updateVisitCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
const QUrl url = index.data(LocationCompleterModel::UrlRole).toUrl();
|
|
|
|
const QString title = index.data(LocationCompleterModel::TitleRole).toString();
|
|
|
|
|
|
|
|
closePopup();
|
|
|
|
|
|
|
|
// Clear locationbar
|
|
|
|
emit clearCompletion();
|
|
|
|
|
|
|
|
// Open url in new tab
|
|
|
|
m_window->tabWidget()->addView(url, title, Qz::NT_CleanSelectedTab);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationCompleter::indexShiftActivated(const QModelIndex &index)
|
|
|
|
{
|
|
|
|
Q_ASSERT(index.isValid());
|
|
|
|
|
|
|
|
if (index.data(LocationCompleterModel::BookmarkRole).toBool()) {
|
|
|
|
BookmarkItem* bookmark = static_cast<BookmarkItem*>(index.data(LocationCompleterModel::BookmarkItemRole).value<void*>());
|
|
|
|
bookmark->updateVisitCount();
|
|
|
|
}
|
|
|
|
|
2017-10-07 19:54:41 +02:00
|
|
|
const QString urlString = index.data(LocationCompleterModel::UrlRole).toString();
|
2014-03-01 13:02:57 +01:00
|
|
|
const int tabPos = index.data(LocationCompleterModel::TabPositionTabRole).toInt();
|
|
|
|
|
|
|
|
// Load url (instead of switching to tab) with shift activation
|
|
|
|
if (tabPos > -1) {
|
2017-10-07 19:54:41 +02:00
|
|
|
loadString(urlString);
|
2014-03-01 13:02:57 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
closePopup();
|
|
|
|
|
|
|
|
// Clear locationbar
|
|
|
|
emit clearCompletion();
|
|
|
|
|
|
|
|
// Open new window
|
2017-10-07 19:54:41 +02:00
|
|
|
mApp->createWindow(Qz::BW_NewWindow, QUrl(urlString));
|
2014-03-01 13:02:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocationCompleter::indexDeleteRequested(const QModelIndex &index)
|
|
|
|
{
|
|
|
|
if (!index.isValid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index.data(LocationCompleterModel::BookmarkRole).toBool()) {
|
|
|
|
BookmarkItem* bookmark = static_cast<BookmarkItem*>(index.data(LocationCompleterModel::BookmarkItemRole).value<void*>());
|
|
|
|
mApp->bookmarks()->removeBookmark(bookmark);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int id = index.data(LocationCompleterModel::IdRole).toInt();
|
|
|
|
mApp->history()->deleteHistoryEntry(id);
|
|
|
|
}
|
|
|
|
|
2014-03-16 17:20:32 +01:00
|
|
|
s_view->setUpdatesEnabled(false);
|
2014-03-01 13:02:57 +01:00
|
|
|
s_model->removeRow(index.row(), index.parent());
|
2014-03-16 17:20:32 +01:00
|
|
|
s_view->setUpdatesEnabled(true);
|
|
|
|
|
|
|
|
// Close popup when removing last item
|
|
|
|
if (s_model->rowCount() == 0) {
|
|
|
|
closePopup();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
adjustPopupSize();
|
|
|
|
}
|
2014-03-01 13:02:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocationCompleter::switchToTab(BrowserWindow* window, int tab)
|
|
|
|
{
|
|
|
|
Q_ASSERT(window);
|
|
|
|
Q_ASSERT(tab >= 0);
|
|
|
|
|
|
|
|
closePopup();
|
|
|
|
|
|
|
|
// Clear locationbar
|
|
|
|
emit clearCompletion();
|
|
|
|
|
|
|
|
TabWidget* tabWidget = window->tabWidget();
|
|
|
|
|
|
|
|
if (window->isActiveWindow() || tabWidget->currentIndex() != tab) {
|
|
|
|
tabWidget->setCurrentIndex(tab);
|
|
|
|
window->show();
|
|
|
|
window->activateWindow();
|
|
|
|
window->raise();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
window->weView()->setFocus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 19:54:41 +02:00
|
|
|
void LocationCompleter::loadString(const QString &urlString)
|
2014-03-01 13:02:57 +01:00
|
|
|
{
|
|
|
|
closePopup();
|
|
|
|
|
|
|
|
// Show url in locationbar
|
2017-10-07 19:54:41 +02:00
|
|
|
emit showCompletion(urlString, false);
|
2014-03-01 13:02:57 +01:00
|
|
|
|
|
|
|
// Load url
|
|
|
|
emit loadCompletion();
|
|
|
|
}
|
|
|
|
|
2012-04-22 17:09:43 +02:00
|
|
|
void LocationCompleter::showPopup()
|
|
|
|
{
|
|
|
|
Q_ASSERT(m_locationBar);
|
|
|
|
|
|
|
|
if (s_model->rowCount() == 0) {
|
|
|
|
s_view->close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s_view->isVisible()) {
|
|
|
|
adjustPopupSize();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect popupRect(m_locationBar->mapToGlobal(m_locationBar->pos()), m_locationBar->size());
|
|
|
|
popupRect.setY(popupRect.bottom());
|
|
|
|
|
|
|
|
s_view->setGeometry(popupRect);
|
2014-03-01 13:02:57 +01:00
|
|
|
s_view->setFocusProxy(m_locationBar);
|
|
|
|
s_view->setCurrentIndex(QModelIndex());
|
2012-04-22 17:09:43 +02:00
|
|
|
|
2013-02-22 15:20:28 +01:00
|
|
|
connect(s_view, SIGNAL(closed()), this, SLOT(slotPopupClosed()));
|
2014-03-01 13:02:57 +01:00
|
|
|
connect(s_view, SIGNAL(indexActivated(QModelIndex)), this, SLOT(indexActivated(QModelIndex)));
|
|
|
|
connect(s_view, SIGNAL(indexCtrlActivated(QModelIndex)), this, SLOT(indexCtrlActivated(QModelIndex)));
|
|
|
|
connect(s_view, SIGNAL(indexShiftActivated(QModelIndex)), this, SLOT(indexShiftActivated(QModelIndex)));
|
|
|
|
connect(s_view, SIGNAL(indexDeleteRequested(QModelIndex)), this, SLOT(indexDeleteRequested(QModelIndex)));
|
|
|
|
connect(s_view->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(currentChanged(QModelIndex)));
|
2012-04-22 17:09:43 +02:00
|
|
|
|
2017-06-07 16:01:06 +02:00
|
|
|
if (m_locationBar->nativeParentWidget()) {
|
|
|
|
s_view->createWinId();
|
|
|
|
s_view->windowHandle()->setTransientParent(m_locationBar->nativeParentWidget()->windowHandle());
|
|
|
|
}
|
|
|
|
|
2012-04-22 17:09:43 +02:00
|
|
|
adjustPopupSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationCompleter::adjustPopupSize()
|
|
|
|
{
|
2017-08-12 15:02:00 +02:00
|
|
|
const int maxItemsCount = 12;
|
2014-03-01 13:02:57 +01:00
|
|
|
const int popupHeight = s_view->sizeHintForRow(0) * qMin(maxItemsCount, s_model->rowCount()) + 2 * s_view->frameWidth();
|
2012-04-22 17:09:43 +02:00
|
|
|
|
2017-08-11 12:26:50 +02:00
|
|
|
m_originalText = m_locationBar->text();
|
|
|
|
s_view->setOriginalText(m_originalText);
|
2017-08-12 15:09:05 +02:00
|
|
|
|
2012-04-22 17:09:43 +02:00
|
|
|
s_view->resize(s_view->width(), popupHeight);
|
|
|
|
s_view->show();
|
|
|
|
}
|