From 88b265765f3dec5365a82e15f3f0b4257576b564 Mon Sep 17 00:00:00 2001 From: Xiao YiFang Date: Tue, 7 Jun 2022 21:22:37 +0800 Subject: [PATCH 1/2] add entry:// link support --- article_netmgr.cc | 22 ++++++++++++++++++---- globalbroadcaster.cpp | 8 ++++++++ globalbroadcaster.h | 6 ++++++ resources/gd-custom.js | 5 +++++ utils.hh | 4 +++- 5 files changed, 40 insertions(+), 5 deletions(-) diff --git a/article_netmgr.cc b/article_netmgr.cc index 5a1722899..32c65ea9b 100644 --- a/article_netmgr.cc +++ b/article_netmgr.cc @@ -8,6 +8,7 @@ #include "gddebug.hh" #include "utils.hh" #include +#include "globalbroadcaster.h" using std::string; @@ -249,11 +250,22 @@ QNetworkReply * ArticleNetworkAccessManager::getArticleReply( QNetworkRequest co } sptr< Dictionary::DataRequest > ArticleNetworkAccessManager::getResource( - QUrl const & url, QString & contentType ) + QUrl const & resUrl, QString & contentType ) { - 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() ); + 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() ) ); + } if ( url.scheme() == "gdlookup" ) { @@ -273,6 +285,8 @@ 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/globalbroadcaster.cpp b/globalbroadcaster.cpp index 885bf89a7..7d2468d74 100644 --- a/globalbroadcaster.cpp +++ b/globalbroadcaster.cpp @@ -29,4 +29,12 @@ void GlobalBroadcaster::addWhitelist(QString url){ 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; +} // namespace global diff --git a/globalbroadcaster.h b/globalbroadcaster.h index 3014a46ec..c53bc50f5 100644 --- a/globalbroadcaster.h +++ b/globalbroadcaster.h @@ -17,6 +17,8 @@ class GlobalBroadcaster : public QObject private: Config::Preferences * preference; std::vector whitelist; + int groupId; + public: void setPreference( Config::Preferences * _pre ); Config::Preferences * getPreference(); @@ -24,6 +26,10 @@ class GlobalBroadcaster : public QObject void addWhitelist(QString host); 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/resources/gd-custom.js b/resources/gd-custom.js index ae07cf319..d7557151a 100644 --- a/resources/gd-custom.js +++ b/resources/gd-custom.js @@ -6,6 +6,11 @@ $(function() { if ('string' != typeof(link)) { return; } + + if(link.indexOf("javascript:")>=0){ + return; + } + if(link.indexOf(":")>=0){ emitClickedEvent(link); return false; diff --git a/utils.hh b/utils.hh index eb4fee08d..6f4b0b1ba 100644 --- a/utils.hh +++ b/utils.hh @@ -231,7 +231,9 @@ inline std::pair< bool, QString > getQueryWord( QUrl const & url ) { //url,bword://localhost/word if( path.startsWith( "/" ) ) - word = url.path().mid( 1 ); + word = path.mid( 1 ); + else + word = path; } else { From 0e4e7afb9aacec603c95309935966762f90fc90d Mon Sep 17 00:00:00 2001 From: Xiao YiFang Date: Wed, 8 Jun 2022 08:19:23 +0800 Subject: [PATCH 2/2] remember last link's groupId and muted dictionary this whole logic need reconsidering and refactor. --- article_netmgr.cc | 5 ++--- articleview.cc | 2 ++ globalbroadcaster.cpp | 4 ++-- globalbroadcaster.h | 9 ++++++--- mainwindow.cc | 6 ++++++ 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/article_netmgr.cc b/article_netmgr.cc index 32c65ea9b..39d461fea 100644 --- a/article_netmgr.cc +++ b/article_netmgr.cc @@ -264,7 +264,8 @@ sptr< Dictionary::DataRequest > ArticleNetworkAccessManager::getResource( 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() ) ); + Utils::Url::addQueryItem( url, "group", QString::number( GlobalBroadcaster::instance()->getGroupId() ) ); + Utils::Url::addQueryItem( url, "muted", GlobalBroadcaster::instance()->mutedDicts ); } if ( url.scheme() == "gdlookup" ) @@ -285,8 +286,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/articleview.cc b/articleview.cc index 7e7120bb9..08b641c59 100644 --- a/articleview.cc +++ b/articleview.cc @@ -435,6 +435,8 @@ void ArticleView::showDefinition( Config::InputPhrase const & phrase, unsigned g if ( mutedDicts.size() ) Utils::Url::addQueryItem( req, "muted", mutedDicts ); + GlobalBroadcaster::instance()->mutedDicts = mutedDicts; + // Update both histories (pages history and headwords history) saveHistoryUserData(); emit sendWordToHistory( phrase.phrase ); diff --git a/globalbroadcaster.cpp b/globalbroadcaster.cpp index 7d2468d74..dc4c0d373 100644 --- a/globalbroadcaster.cpp +++ b/globalbroadcaster.cpp @@ -30,11 +30,11 @@ bool GlobalBroadcaster::existedInWhitelist(QString url){ return std::find(whitelist.begin(), whitelist.end(), url) != whitelist.end(); } -void GlobalBroadcaster::setGroupId(int groupId){ +void GlobalBroadcaster::setGroupId(unsigned groupId){ this->groupId = groupId; } -int GlobalBroadcaster::getGroupId(){ +unsigned GlobalBroadcaster::getGroupId(){ return groupId; } // namespace global diff --git a/globalbroadcaster.h b/globalbroadcaster.h index c53bc50f5..997939cd3 100644 --- a/globalbroadcaster.h +++ b/globalbroadcaster.h @@ -17,7 +17,8 @@ class GlobalBroadcaster : public QObject private: Config::Preferences * preference; std::vector whitelist; - int groupId; + unsigned groupId; + public: void setPreference( Config::Preferences * _pre ); @@ -28,8 +29,10 @@ class GlobalBroadcaster : public QObject static GlobalBroadcaster * instance(); //store the latest groupId; - void setGroupId(int groupId); - int getGroupId(); + void setGroupId(unsigned groupId); + unsigned getGroupId(); + + QString mutedDicts; signals: void dictionaryChanges( ActiveDictIds ad ); }; diff --git a/mainwindow.cc b/mainwindow.cc index 29f2bee31..8b4709e01 100644 --- a/mainwindow.cc +++ b/mainwindow.cc @@ -1417,6 +1417,9 @@ void MainWindow::updateGroupList() groupList->fill( groupInstances ); groupList->setCurrentGroup( cfg.lastMainGroupId ); + + GlobalBroadcaster::instance()->setGroupId( cfg.lastMainGroupId); + updateCurrentGroupProperty(); updateDictionaryBar(); @@ -2305,6 +2308,9 @@ void MainWindow::updateCurrentGroupProperty() Instances::Group * grp = groupInstances.findGroup( groupList->getCurrentGroup() ); + //record the latest groupid + GlobalBroadcaster::instance()->setGroupId(groupList->getCurrentGroup()); + if ( grp && translateLine->property( "currentGroup" ).toString() != grp->name ) {