From 09ee6007e90171c3e4bbb73cc7a4c91872373242 Mon Sep 17 00:00:00 2001 From: David Rosca Date: Thu, 25 Jan 2018 16:29:48 +0100 Subject: [PATCH] LocationCompleter: Handle creating LoadRequest Instead of just passing string that LocationBar should somehow parse and decide what to do (which may not correspond to what was indicated in completion entry), make the LocationCompleter create it with all needed info and pass it to LocationBar to load. This fixes issue where search completions may have been loaded with different search engine when suggestion starts with search engine shortcut (which would also get cut in the search). This also removes the possibility to open new window from completer with activating entry with Shift key. --- .../completer/locationcompleter.cpp | 93 ++++++++++--------- .../navigation/completer/locationcompleter.h | 4 +- src/lib/navigation/locationbar.cpp | 1 - 3 files changed, 53 insertions(+), 45 deletions(-) diff --git a/src/lib/navigation/completer/locationcompleter.cpp b/src/lib/navigation/completer/locationcompleter.cpp index 91c142dbc..a47e436ad 100644 --- a/src/lib/navigation/completer/locationcompleter.cpp +++ b/src/lib/navigation/completer/locationcompleter.cpp @@ -213,18 +213,7 @@ void LocationCompleter::indexActivated(const QModelIndex &index) return; } - if (index.data(LocationCompleterModel::BookmarkRole).toBool()) { - BookmarkItem* bookmark = static_cast(index.data(LocationCompleterModel::BookmarkItemRole).value()); - bookmark->updateVisitCount(); - } - - QString urlString = index.data(LocationCompleterModel::UrlRole).toString(); - - if (index.data(LocationCompleterModel::VisitSearchItemRole).toBool()) { - urlString = m_originalText; - } - - loadString(urlString); + loadRequest(createLoadRequest(index)); } void LocationCompleter::indexCtrlActivated(const QModelIndex &index) @@ -232,48 +221,26 @@ void LocationCompleter::indexCtrlActivated(const QModelIndex &index) Q_ASSERT(index.isValid()); Q_ASSERT(m_window); - if (index.data(LocationCompleterModel::BookmarkRole).toBool()) { - BookmarkItem* bookmark = static_cast(index.data(LocationCompleterModel::BookmarkItemRole).value()); - 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); + // Load request in new tab + m_window->tabWidget()->addView(createLoadRequest(index), Qz::NT_CleanSelectedTab); } void LocationCompleter::indexShiftActivated(const QModelIndex &index) { Q_ASSERT(index.isValid()); - if (index.data(LocationCompleterModel::BookmarkRole).toBool()) { - BookmarkItem* bookmark = static_cast(index.data(LocationCompleterModel::BookmarkItemRole).value()); - bookmark->updateVisitCount(); - } - - const QString urlString = index.data(LocationCompleterModel::UrlRole).toString(); - const int tabPos = index.data(LocationCompleterModel::TabPositionTabRole).toInt(); - - // Load url (instead of switching to tab) with shift activation - if (tabPos > -1) { - loadString(urlString); - return; - } - closePopup(); // Clear locationbar emit clearCompletion(); - // Open new window - mApp->createWindow(Qz::BW_NewWindow, QUrl(urlString)); + // Load request + loadRequest(createLoadRequest(index)); } void LocationCompleter::indexDeleteRequested(const QModelIndex &index) @@ -305,6 +272,48 @@ void LocationCompleter::indexDeleteRequested(const QModelIndex &index) } } +LoadRequest LocationCompleter::createLoadRequest(const QModelIndex &index) +{ + LoadRequest request; + QString searchString; + SearchEngine searchEngine; + BookmarkItem *bookmark = nullptr; + + if (index.data(LocationCompleterModel::HistoryRole).toBool()) { + request = index.data(LocationCompleterModel::UrlRole).toUrl(); + } else if (index.data(LocationCompleterModel::BookmarkRole).toBool()) { + bookmark = static_cast(index.data(LocationCompleterModel::BookmarkItemRole).value()); + } else if (index.data(LocationCompleterModel::SearchSuggestionRole).toBool()) { + searchEngine = LocationBar::searchEngine(); + searchString = index.data(LocationCompleterModel::TitleRole).toString(); + } else if (index.data(LocationCompleterModel::VisitSearchItemRole).toBool()) { + const auto action = LocationBar::loadAction(m_originalText); + switch (action.type) { + case LocationBar::LoadAction::Url: + request = action.loadRequest; + break; + case LocationBar::LoadAction::Bookmark: + bookmark = action.bookmark; + break; + case LocationBar::LoadAction::Search: + searchEngine = action.searchEngine; + searchString = m_originalText; + break; + default: + break; + } + } + + if (bookmark) { + bookmark->updateVisitCount(); + request = bookmark->url(); + } else if (searchEngine.isValid()) { + request = mApp->searchEnginesManager()->searchResult(searchEngine, searchString); + } + + return request; +} + void LocationCompleter::switchToTab(BrowserWindow* window, int tab) { Q_ASSERT(window); @@ -328,15 +337,15 @@ void LocationCompleter::switchToTab(BrowserWindow* window, int tab) } } -void LocationCompleter::loadString(const QString &urlString) +void LocationCompleter::loadRequest(const LoadRequest &request) { closePopup(); // Show url in locationbar - emit showCompletion(urlString, false); + emit showCompletion(request.url().toString(), false); - // Load url - emit loadCompletion(); + // Load request + emit loadRequested(request); } void LocationCompleter::showPopup() diff --git a/src/lib/navigation/completer/locationcompleter.h b/src/lib/navigation/completer/locationcompleter.h index c54c92ab9..bea9f5812 100644 --- a/src/lib/navigation/completer/locationcompleter.h +++ b/src/lib/navigation/completer/locationcompleter.h @@ -50,7 +50,6 @@ public slots: signals: void showCompletion(const QString &completion, bool completeDomain); void showDomainCompletion(const QString &completion); - void loadCompletion(); void clearCompletion(); void popupClosed(); void cancelRefreshJob(); @@ -68,8 +67,9 @@ private slots: void indexDeleteRequested(const QModelIndex &index); private: + LoadRequest createLoadRequest(const QModelIndex &index); void switchToTab(BrowserWindow* window, int tab); - void loadString(const QString &url); + void loadRequest(const LoadRequest &reqeust); void showPopup(); void adjustPopupSize(); diff --git a/src/lib/navigation/locationbar.cpp b/src/lib/navigation/locationbar.cpp index 8d1303d64..ea0240a11 100644 --- a/src/lib/navigation/locationbar.cpp +++ b/src/lib/navigation/locationbar.cpp @@ -75,7 +75,6 @@ LocationBar::LocationBar(BrowserWindow* window) m_completer->setLocationBar(this); connect(m_completer, SIGNAL(showCompletion(QString,bool)), this, SLOT(showCompletion(QString,bool))); connect(m_completer, SIGNAL(showDomainCompletion(QString)), this, SLOT(showDomainCompletion(QString))); - connect(m_completer, SIGNAL(loadCompletion()), this, SLOT(requestLoadUrl())); connect(m_completer, SIGNAL(clearCompletion()), this, SLOT(clearCompletion())); connect(m_completer, &LocationCompleter::loadRequested, this, &LocationBar::loadRequest);