From 547f2a4cd286c4711a69b62e3c6bdc89b1fd7da2 Mon Sep 17 00:00:00 2001 From: Xiao YiFang Date: Wed, 8 Jun 2022 08:19:23 +0800 Subject: [PATCH] remember last link's groupId and muted dictionary --- article_netmgr.cc | 21 ++++----------------- articlewebpage.cpp | 25 ++++++++++++++++++++++++- articlewebpage.h | 7 +++++++ globalbroadcaster.cpp | 22 ++++++++-------------- globalbroadcaster.h | 4 ---- mainwindow.cc | 1 + 6 files changed, 44 insertions(+), 36 deletions(-) diff --git a/article_netmgr.cc b/article_netmgr.cc index 32c65ea9..e0e23650 100644 --- a/article_netmgr.cc +++ b/article_netmgr.cc @@ -250,22 +250,11 @@ QNetworkReply * ArticleNetworkAccessManager::getArticleReply( QNetworkRequest co } sptr< Dictionary::DataRequest > ArticleNetworkAccessManager::getResource( - QUrl const & resUrl, QString & contentType ) + QUrl const & url, QString & contentType ) { - GD_DPRINTF( "getResource: %ls", resUrl.toString().toStdWString().c_str() ); - GD_DPRINTF( "scheme: %ls", resUrl.scheme().toStdWString().c_str() ); - GD_DPRINTF( "host: %ls", resUrl.host().toStdWString().c_str() ); - - QUrl url = resUrl; - if( url.scheme() == "bword" || url.scheme() == "entry" ) - { - url.setScheme( "gdlookup" ); - url.setHost( "localhost" ); - url.setPath( "" ); - auto [ valid, word ] = Utils::Url::getQueryWord( resUrl ); - Utils::Url::addQueryItem( url, "word", word ); - Utils::Url::addQueryItem( url, "group", QString( "%1" ).arg( GlobalBroadcaster::instance()->getGroupId() ) ); - } + GD_DPRINTF( "getResource: %ls", url.toString().toStdWString().c_str() ); + GD_DPRINTF( "scheme: %ls", url.scheme().toStdWString().c_str() ); + GD_DPRINTF( "host: %ls", url.host().toStdWString().c_str() ); if ( url.scheme() == "gdlookup" ) { @@ -285,8 +274,6 @@ sptr< Dictionary::DataRequest > ArticleNetworkAccessManager::getResource( bool groupIsValid = false; unsigned group = Utils::Url::queryItemValue( url, "group" ).toUInt( &groupIsValid ); - - GlobalBroadcaster::instance()->setGroupId(group); QString dictIDs = Utils::Url::queryItemValue( url, "dictionaries" ); if( !dictIDs.isEmpty() ) diff --git a/articlewebpage.cpp b/articlewebpage.cpp index 9b782f8d..bf686114 100644 --- a/articlewebpage.cpp +++ b/articlewebpage.cpp @@ -1,15 +1,38 @@ #include "articlewebpage.h" +#include "utils.hh" ArticleWebPage::ArticleWebPage(QObject *parent) : QWebEnginePage{parent} { } -bool ArticleWebPage::acceptNavigationRequest( const QUrl & url, NavigationType type, bool isMainFrame ) +bool ArticleWebPage::acceptNavigationRequest( const QUrl & resUrl, NavigationType type, bool isMainFrame ) { + QUrl url = resUrl; + if( url.scheme() == "bword" || url.scheme() == "entry" ) + { + url.setScheme( "gdlookup" ); + url.setHost( "localhost" ); + url.setPath( "" ); + auto [ valid, word ] = Utils::Url::getQueryWord( resUrl ); + Utils::Url::addQueryItem( url, "word", word ); + Utils::Url::addQueryItem( url, "group", lastReq.group ); + Utils::Url::addQueryItem( url, "muted", lastReq.mutedDicts ); + setUrl( url ); + return false; + } + + //save current gdlookup's values. + if( url.scheme() == "gdlookup" ) + { + lastReq.group = Utils::Url::queryItemValue( url, "group" ); + lastReq.mutedDicts = Utils::Url::queryItemValue( url, "muted" ); + } + if( type == QWebEnginePage::NavigationTypeLinkClicked ) { emit linkClicked( url ); return true; } + return QWebEnginePage::acceptNavigationRequest( url, type, isMainFrame ); } diff --git a/articlewebpage.h b/articlewebpage.h index 3e1d32d6..19027148 100644 --- a/articlewebpage.h +++ b/articlewebpage.h @@ -3,6 +3,11 @@ #include +struct LastReqInfo{ + QString group; + QString mutedDicts; +}; + class ArticleWebPage : public QWebEnginePage { Q_OBJECT @@ -12,6 +17,8 @@ signals: void linkClicked( const QUrl & url ); protected: virtual bool acceptNavigationRequest( const QUrl & url, NavigationType type, bool isMainFrame ); +private: + LastReqInfo lastReq; }; #endif // ARTICLEWEBPAGE_H diff --git a/globalbroadcaster.cpp b/globalbroadcaster.cpp index 7d2468d7..bcac205f 100644 --- a/globalbroadcaster.cpp +++ b/globalbroadcaster.cpp @@ -20,21 +20,15 @@ Config::Preferences * GlobalBroadcaster::getPreference() return preference; } -void GlobalBroadcaster::addWhitelist(QString url){ - whitelist.push_back(url); - auto baseUrl=::getHostBase(url); - whitelist.push_back(baseUrl); +void GlobalBroadcaster::addWhitelist( QString url ) +{ + whitelist.push_back( url ); + auto baseUrl = ::getHostBase( url ); + whitelist.push_back( baseUrl ); } -bool GlobalBroadcaster::existedInWhitelist(QString url){ - return std::find(whitelist.begin(), whitelist.end(), url) != whitelist.end(); -} - -void GlobalBroadcaster::setGroupId(int groupId){ - this->groupId = groupId; -} - -int GlobalBroadcaster::getGroupId(){ - return groupId; +bool GlobalBroadcaster::existedInWhitelist( QString url ) +{ + return std::find( whitelist.begin(), whitelist.end(), url ) != whitelist.end(); } // namespace global diff --git a/globalbroadcaster.h b/globalbroadcaster.h index c53bc50f..6e6992c2 100644 --- a/globalbroadcaster.h +++ b/globalbroadcaster.h @@ -17,7 +17,6 @@ class GlobalBroadcaster : public QObject private: Config::Preferences * preference; std::vector whitelist; - int groupId; public: void setPreference( Config::Preferences * _pre ); @@ -27,9 +26,6 @@ public: bool existedInWhitelist(QString host); static GlobalBroadcaster * instance(); - //store the latest groupId; - void setGroupId(int groupId); - int getGroupId(); signals: void dictionaryChanges( ActiveDictIds ad ); }; diff --git a/mainwindow.cc b/mainwindow.cc index 29f2bee3..fb4ae931 100644 --- a/mainwindow.cc +++ b/mainwindow.cc @@ -1417,6 +1417,7 @@ void MainWindow::updateGroupList() groupList->fill( groupInstances ); groupList->setCurrentGroup( cfg.lastMainGroupId ); + updateCurrentGroupProperty(); updateDictionaryBar();