diff --git a/CHANGES.md b/CHANGES.md index b5aca1dc..6067cbe8 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,6 +3,14 @@ ## Until to now - **CLEANING OLD/USELESS CODE** + - remove Runnable Class in dsl, zim , epwing etc files. + +- add "send to anki" +- right context menu actions, remove nonsense character like `OBJ`,punctuation etc. +- epwing remove duplicate entries when index. + + +## Until 2022-5-21 - fix a zim about:blank#block [issue](https://github.com/goldendict/goldendict/issues/1472#issuecomment-1086776611) - add fallback font family configuration for dictionary through preference dialog. - fix mdx (compact html) display error on the last item. diff --git a/article_inspect.cpp b/article_inspect.cpp index ff296787..e194fec1 100644 --- a/article_inspect.cpp +++ b/article_inspect.cpp @@ -10,8 +10,8 @@ ArticleInspector::ArticleInspector( QWidget * parent ) : QWidget( parent, Qt::Wi QVBoxLayout * v = new QVBoxLayout( this ); v->setSpacing( 0 ); v->setContentsMargins( 0, 0, 0, 0 ); - inspectView = new QWebEngineView( this ); - v->addWidget( inspectView ); + viewContainer = new QWebEngineView( this ); + v->addWidget( viewContainer ); resize(800,600); } @@ -19,8 +19,7 @@ ArticleInspector::ArticleInspector( QWidget * parent ) : QWidget( parent, Qt::Wi void ArticleInspector::setInspectPage( QWebEngineView * view ) { auto page=view->page(); - this->inspectedPage = page; - page->setDevToolsPage( inspectView->page() ); + viewContainer->page()->setInspectedPage(page); #if( QT_VERSION > QT_VERSION_CHECK( 6, 0, 0 ) ) // without this line, application will crash on qt6.2 ,see https://bugreports.qt.io/browse/QTBUG-101724 if( view->lastContextMenuRequest() ) @@ -36,5 +35,5 @@ void ArticleInspector::setInspectPage( QWebEngineView * view ) void ArticleInspector::closeEvent( QCloseEvent * ) { - inspectedPage->setDevToolsPage( nullptr ); + viewContainer->page()->setInspectedPage(nullptr); } diff --git a/article_inspect.h b/article_inspect.h index a99d1ff2..8c7b50c5 100644 --- a/article_inspect.h +++ b/article_inspect.h @@ -9,8 +9,7 @@ class ArticleInspector : public QWidget { Q_OBJECT - QWebEngineView * inspectView = nullptr; - QWebEnginePage * inspectedPage = nullptr; + QWebEngineView * viewContainer = nullptr; public: ArticleInspector( QWidget * parent = nullptr ); diff --git a/article_maker.cc b/article_maker.cc index 5df14417..41f97422 100644 --- a/article_maker.cc +++ b/article_maker.cc @@ -465,7 +465,7 @@ void ArticleRequest::altSearchFinished() if ( altSearches.empty() ) { #ifdef QT_DEBUG - qDebug( "alts finished\n" ); + qDebug( "alts finished" ); #endif // They all've finished! Now we can look up bodies @@ -534,7 +534,7 @@ void ArticleRequest::bodyFinished() if ( bodyDone ) return; - GD_DPRINTF( "some body finished\n" ); + GD_DPRINTF( "some body finished" ); bool wasUpdated = false; @@ -546,7 +546,7 @@ void ArticleRequest::bodyFinished() { // Good - GD_DPRINTF( "one finished.\n" ); + GD_DPRINTF( "one finished." ); Dictionary::DataRequest & req = *bodyRequests.front(); @@ -672,13 +672,13 @@ void ArticleRequest::bodyFinished() foundAnyDefinitions = true; } - GD_DPRINTF( "erasing..\n" ); + GD_DPRINTF( "erasing.." ); bodyRequests.pop_front(); - GD_DPRINTF( "erase done..\n" ); + GD_DPRINTF( "erase done.." ); } else { - GD_DPRINTF( "one not finished.\n" ); + GD_DPRINTF( "one not finished." ); break; } } diff --git a/article_netmgr.cc b/article_netmgr.cc index db2aa0e9..7ca15408 100644 --- a/article_netmgr.cc +++ b/article_netmgr.cc @@ -202,7 +202,7 @@ QNetworkReply * ArticleNetworkAccessManager::getArticleReply( QNetworkRequest co // getHostBase( refererUrl ).toUtf8().data() ); if ( !url.host().endsWith( refererUrl.host() ) && - getHostBase( url ) != getHostBase( refererUrl ) && !url.scheme().startsWith("data") ) + getHostBaseFromUrl( url ) != getHostBaseFromUrl( refererUrl ) && !url.scheme().startsWith("data") ) { gdWarning( "Blocking element \"%s\" due to not same domain", url.toEncoded().data() ); @@ -254,9 +254,9 @@ QNetworkReply * ArticleNetworkAccessManager::getArticleReply( QNetworkRequest co sptr< Dictionary::DataRequest > ArticleNetworkAccessManager::getResource( QUrl const & url, QString & contentType ) { - GD_DPRINTF( "getResource: %ls\n", url.toString().toStdWString().c_str() ); - GD_DPRINTF( "scheme: %ls\n", url.scheme().toStdWString().c_str() ); - GD_DPRINTF( "host: %ls\n", url.host().toStdWString().c_str() ); + 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" ) { @@ -515,6 +515,17 @@ void LocalSchemeHandler::requestStarted(QWebEngineUrlRequestJob *requestJob) QNetworkRequest request; request.setUrl( url ); + //all the url reached here must be either gdlookup or bword scheme. + auto queryWord = Utils::Url::getQueryWord( url ); + auto word = queryWord.second; + // or the condition can be (!queryWord.first || word.isEmpty()) + // ( queryWord.first && word.isEmpty() ) is only part of the above condition. + if( queryWord.first && word.isEmpty() ) + { + // invalid gdlookup url. + return; + } + QNetworkReply * reply = this->mManager.getArticleReply( request ); connect( reply, &QNetworkReply::finished, requestJob, [ = ]() { requestJob->reply( "text/html", reply ); } ); connect( requestJob, &QObject::destroyed, reply, &QObject::deleteLater ); diff --git a/articleview.cc b/articleview.cc index 160d004b..4f997a8d 100644 --- a/articleview.cc +++ b/articleview.cc @@ -708,39 +708,14 @@ void ArticleView::tryMangleWebsiteClickedUrl( QUrl & url, Contexts & contexts ) { if( framed ) { - // QVariant result = runJavaScriptSync( ui.definition->page(), "gdLastUrlText" ); - QVariant result; - - if( result.type() == QVariant::String ) - { - // Looks this way - contexts[ dictionaryIdFromScrollTo( ca ) ] = QString::fromLatin1( url.toEncoded() ); - - QUrl target; - - QString queryWord = result.toString(); - - // Empty requests are treated as no request, so we work this around by - // adding a space. - if( queryWord.isEmpty() ) - queryWord = " "; - - target.setScheme( "gdlookup" ); - target.setHost( "localhost" ); - target.setPath( "/" + queryWord ); - - url = target; - } + // no need to translate website internal url to gd builtin url + // and lack the formulation to convert them. + qDebug() << "in the website with url:" << url; } } ); } } -void ArticleView::updateCurrentArticleFromCurrentFrame( QWebEnginePage * frame ,QPoint * point) -{ - -} - void ArticleView::saveHistoryUserData() { ui.definition->setProperty("sx", ui.definition->page()->scrollPosition().x()); @@ -1100,8 +1075,6 @@ void ArticleView::linkClicked( QUrl const & url_ ) if( kmod & Qt::AltModifier ) return; - updateCurrentArticleFromCurrentFrame(); - QUrl url( url_ ); Contexts contexts; @@ -1134,6 +1107,14 @@ void ArticleView::openLink( QUrl const & url, QUrl const & ref, audioPlayer->stop(); qDebug() << "open link url:" << url; + auto queryWord = Utils::Url::getQueryWord( url ); + auto word = queryWord.second; + if( queryWord.first && word.isEmpty() ) + { + // invalid gdlookup url. + return; + } + Contexts contexts( contexts_ ); if( url.scheme().compare( "gdpicture" ) == 0 ) @@ -1146,10 +1127,10 @@ void ArticleView::openLink( QUrl const & url, QUrl const & ref, QStringList dictsList = Utils::Url::queryItemValue( ref, "dictionaries" ) .split( ",", Qt::SkipEmptyParts ); - showDefinition( url.path(), dictsList, QRegExp(), getGroup( ref ), false ); + showDefinition( word, dictsList, QRegExp(), getGroup( ref ), false ); } else - showDefinition( url.path(), + showDefinition( word, getGroup( ref ), scrollTo, contexts ); } else @@ -1172,16 +1153,6 @@ void ArticleView::openLink( QUrl const & url, QUrl const & ref, return; } - QString word; - - if( Utils::Url::hasQueryItem( url, "word" ) ) - { - word=Utils::Url::queryItemValue (url,"word"); - } - else{ - word=url.path ().mid (1); - } - QString newScrollTo( scrollTo ); if( Utils::Url::hasQueryItem( url, "dict" ) ) { @@ -1725,8 +1696,6 @@ void ArticleView::contextMenuRequested( QPoint const & pos ) { // Is that a link? Is there a selection? QWebEnginePage* r=ui.definition->page(); - updateCurrentArticleFromCurrentFrame(ui.definition->page(), const_cast(& pos)); - QMenu menu( this ); QAction * followLink = 0; @@ -1801,7 +1770,7 @@ void ArticleView::contextMenuRequested( QPoint const & pos ) } QString selectedText = ui.definition->selectedText(); - QString text = selectedText.trimmed(); + QString text = Utils::trimNonChar( selectedText ); if ( text.size() && text.size() < 60 ) { diff --git a/articleview.hh b/articleview.hh index 658b2111..35aa99a1 100644 --- a/articleview.hh +++ b/articleview.hh @@ -380,10 +380,6 @@ private: /// url to the appropriate "contexts" entry. void tryMangleWebsiteClickedUrl( QUrl & url, Contexts & contexts ); - /// Use the known information about the current frame to update the current - /// article's value. - void updateCurrentArticleFromCurrentFrame( QWebEnginePage * frame = 0 ,QPoint * point=0); - /// Saves current article and scroll position for the current history item. /// Should be used when leaving the page. void saveHistoryUserData(); diff --git a/dsl.cc b/dsl.cc index 6302feee..6125342e 100644 --- a/dsl.cc +++ b/dsl.cc @@ -1556,32 +1556,8 @@ void DslDictionary::getArticleText( uint32_t articleAddress, QString & headword, /// DslDictionary::getArticle() -class DslArticleRequest; - -class DslArticleRequestRunnable: public QRunnable -{ - DslArticleRequest & r; - QSemaphore & hasExited; - -public: - - DslArticleRequestRunnable( DslArticleRequest & r_, - QSemaphore & hasExited_ ): r( r_ ), - hasExited( hasExited_ ) - {} - - ~DslArticleRequestRunnable() - { - hasExited.release(); - } - - virtual void run(); -}; - class DslArticleRequest: public Dictionary::DataRequest { - friend class DslArticleRequestRunnable; - wstring word; vector< wstring > alts; DslDictionary & dict; @@ -1597,11 +1573,10 @@ public: DslDictionary & dict_, bool ignoreDiacritics_ ): word( word_ ), alts( alts_ ), dict( dict_ ), ignoreDiacritics( ignoreDiacritics_ ) { - QThreadPool::globalInstance()->start( - new DslArticleRequestRunnable( *this, hasExited ) ); + QThreadPool::globalInstance()->start( [ this ]() { this->run(); } ); } - void run(); // Run from another thread by DslArticleRequestRunnable + void run(); virtual void cancel() { @@ -1611,15 +1586,10 @@ public: ~DslArticleRequest() { isCancelled.ref(); - hasExited.acquire(); + //hasExited.acquire(); } }; -void DslArticleRequestRunnable::run() -{ - r.run(); -} - void DslArticleRequest::run() { if ( Utils::AtomicInt::loadAcquire( isCancelled ) ) @@ -1759,32 +1729,8 @@ sptr< Dictionary::DataRequest > DslDictionary::getArticle( wstring const & word, //// DslDictionary::getResource() -class DslResourceRequest; - -class DslResourceRequestRunnable: public QRunnable -{ - DslResourceRequest & r; - QSemaphore & hasExited; - -public: - - DslResourceRequestRunnable( DslResourceRequest & r_, - QSemaphore & hasExited_ ): r( r_ ), - hasExited( hasExited_ ) - {} - - ~DslResourceRequestRunnable() - { - hasExited.release(); - } - - virtual void run(); -}; - class DslResourceRequest: public Dictionary::DataRequest { - friend class DslResourceRequestRunnable; - DslDictionary & dict; string resourceName; @@ -1799,11 +1745,10 @@ public: dict( dict_ ), resourceName( resourceName_ ) { - QThreadPool::globalInstance()->start( - new DslResourceRequestRunnable( *this, hasExited ) ); + QThreadPool::globalInstance()->start( [ this ]() { this->run(); } ); } - void run(); // Run from another thread by DslResourceRequestRunnable + void run(); virtual void cancel() { @@ -1813,15 +1758,10 @@ public: ~DslResourceRequest() { isCancelled.ref(); - hasExited.acquire(); + //hasExited.acquire(); } }; -void DslResourceRequestRunnable::run() -{ - r.run(); -} - void DslResourceRequest::run() { // Some runnables linger enough that they are cancelled before they start diff --git a/goldendict.pro b/goldendict.pro index 853ce5ab..f42b42c6 100644 --- a/goldendict.pro +++ b/goldendict.pro @@ -75,7 +75,8 @@ win32 { DEFINES += NOMINMAX __WIN64 } LIBS += -L$${PWD}/winlibs/lib/msvc - QMAKE_CXXFLAGS += /wd4290 /Zc:__cplusplus /std:c++17 # silence the warning C4290: C++ exception specification ignored + # silence the warning C4290: C++ exception specification ignored + QMAKE_CXXFLAGS += /wd4290 /Zc:__cplusplus /std:c++17 /permissive- # QMAKE_LFLAGS_RELEASE += /OPT:REF /OPT:ICF # QMAKE_LFLAGS_RELEASE = /INCREMENTAL:NO /DEBUG CONFIG+=force_debug_info @@ -124,7 +125,6 @@ unix:!mac { DEFINES += HAVE_X11 lessThan(QT_MAJOR_VERSION, 6): QT += x11extras - greaterThan(QT_MAJOR_VERSION, 5): QT += gui-private CONFIG += link_pkgconfig @@ -139,9 +139,7 @@ unix:!mac { libavcodec \ libswresample \ } - arm { - #LIBS += -liconv - } else { + !arm { LIBS += -lX11 -lXtst } diff --git a/hotkeywrapper.cc b/hotkeywrapper.cc index c942253c..1a26d9a4 100644 --- a/hotkeywrapper.cc +++ b/hotkeywrapper.cc @@ -468,10 +468,17 @@ void HotkeyWrapper::init() { keyToUngrab = grabbedKeys.end(); +#if QT_VERSION < 0x060000 + Display *displayID = QX11Info::display(); +#else + QNativeInterface::QX11Application *x11AppInfo = qApp->nativeInterface(); + Display *displayID = x11AppInfo->display(); +#endif + // We use RECORD extension instead of XGrabKey. That's because XGrabKey // prevents other clients from getting their input if it's grabbed. - Display * display = QX11Info::display(); + Display * display = displayID; lShiftCode = XKeysymToKeycode( display, XK_Shift_L ); rShiftCode = XKeysymToKeycode( display, XK_Shift_R ); @@ -678,13 +685,25 @@ public: ~X11GrabUngrabErrorHandler() { - XFlush( QX11Info::display() ); +#if QT_VERSION < 0x060000 + Display *displayID = QX11Info::display(); +#else + QNativeInterface::QX11Application *x11AppInfo = qApp->nativeInterface(); + Display *displayID = x11AppInfo->display(); +#endif + XFlush( displayID ); (void) XSetErrorHandler( previousErrorHandler_ ); } bool isError() const { - XFlush( QX11Info::display() ); +#if QT_VERSION < 0x060000 + Display *displayID = QX11Info::display(); +#else + QNativeInterface::QX11Application *x11AppInfo = qApp->nativeInterface(); + Display *displayID = x11AppInfo->display(); +#endif + XFlush( displayID ); return error; } @@ -706,8 +725,14 @@ HotkeyWrapper::GrabbedKeys::iterator HotkeyWrapper::grabKey( quint32 keyCode, if ( result.second ) { +#if QT_VERSION < 0x060000 + Display *displayID = QX11Info::display(); +#else + QNativeInterface::QX11Application *x11AppInfo = qApp->nativeInterface(); + Display *displayID = x11AppInfo->display(); +#endif X11GrabUngrabErrorHandler errorHandler; - XGrabKey( QX11Info::display(), keyCode, modifiers, QX11Info::appRootWindow(), + XGrabKey( displayID, keyCode, modifiers, DefaultRootWindow(displayID), True, GrabModeAsync, GrabModeAsync ); if ( errorHandler.isError() ) @@ -722,8 +747,14 @@ HotkeyWrapper::GrabbedKeys::iterator HotkeyWrapper::grabKey( quint32 keyCode, void HotkeyWrapper::ungrabKey( GrabbedKeys::iterator i ) { +#if QT_VERSION < 0x060000 + Display *displayID = QX11Info::display(); +#else + QNativeInterface::QX11Application *x11AppInfo = qApp->nativeInterface(); + Display *displayID = x11AppInfo->display(); +#endif X11GrabUngrabErrorHandler errorHandler; - XUngrabKey( QX11Info::display(), i->first, i->second, QX11Info::appRootWindow() ); + XUngrabKey( displayID, i->first, i->second, XDefaultRootWindow(displayID) ); grabbedKeys.erase( i ); @@ -746,14 +777,25 @@ quint32 HotkeyWrapper::nativeKey(int key) keySymName = QKeySequence( key ).toString(); break; } - - Display * display = QX11Info::display(); +#if QT_VERSION < 0x060000 + Display *displayID = QX11Info::display(); +#else + QNativeInterface::QX11Application *x11AppInfo = qApp->nativeInterface(); + Display *displayID = x11AppInfo->display(); +#endif + Display * display = displayID; return XKeysymToKeycode( display, XStringToKeysym( keySymName.toLatin1().data() ) ); } void HotkeyWrapper::unregister() { - Display * display = QX11Info::display(); +#if QT_VERSION < 0x060000 + Display *displayID = QX11Info::display(); +#else + QNativeInterface::QX11Application *x11AppInfo = qApp->nativeInterface(); + Display *displayID = x11AppInfo->display(); +#endif + Display * display = displayID; XRecordDisableContext( display, recordContext ); XSync( display, False ); diff --git a/hotkeywrapper.hh b/hotkeywrapper.hh index 844458b2..00ad0965 100644 --- a/hotkeywrapper.hh +++ b/hotkeywrapper.hh @@ -10,7 +10,7 @@ #include #include #if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) -#include +#include #else #include #endif diff --git a/keyboardstate.cc b/keyboardstate.cc index b6163f19..2d336ed0 100644 --- a/keyboardstate.cc +++ b/keyboardstate.cc @@ -8,7 +8,7 @@ #include #elif defined(HAVE_X11) #if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) -#include +#include #else #include #endif @@ -44,9 +44,17 @@ bool KeyboardState::checkModifiersPressed( int mask ) ( mask & Shift && !( keys & ( 1 << shiftKeyBit ) ) ) || ( mask & Win && !( keys & ( 1 << controlKeyBit ) ) ) ); #else + +#if QT_VERSION < 0x060000 + Display *displayID = QX11Info::display(); +#else + QNativeInterface::QX11Application *x11AppInfo = qApp->nativeInterface(); + Display *displayID = x11AppInfo->display(); +#endif + XkbStateRec state; - XkbGetState( QX11Info::display(), XkbUseCoreKbd, &state ); + XkbGetState( displayID, XkbUseCoreKbd, &state ); return !( ( mask & Alt && !( state.base_mods & Mod1Mask ) ) || diff --git a/mainwindow.cc b/mainwindow.cc index 9876c8b4..2ba00eb2 100644 --- a/mainwindow.cc +++ b/mainwindow.cc @@ -65,7 +65,7 @@ #ifdef HAVE_X11 #if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) -#include +#include #else #include #endif @@ -1246,7 +1246,7 @@ void MainWindow::quitApp() { if( inspector && inspector->isVisible() ) { - inspector->hide(); + inspector->close(); } commitData(); qApp->quit(); @@ -1635,9 +1635,10 @@ ArticleView * MainWindow::createNewTab( bool switchToIt, groupList ); connect( view, &ArticleView::inspectSignal,this,[this](QWebEngineView * view){ - if(inspector){ - inspector->setInspectPage(view); + if( !inspector ){ + inspector = new ArticleInspector( this ); } + inspector->setInspectPage( view ); }); connect( view, SIGNAL( titleChanged( ArticleView *, QString const & ) ), @@ -2903,9 +2904,15 @@ void MainWindow::toggleMainWindow( bool onlyShow ) focusTranslateLine(); #ifdef HAVE_X11 +#if QT_VERSION < 0x060000 + Display *displayID = QX11Info::display(); +#else + QNativeInterface::QX11Application *x11AppInfo = qApp->nativeInterface(); + Display *displayID = x11AppInfo->display(); +#endif Window wh = 0; int rev = 0; - XGetInputFocus( QX11Info::display(), &wh, &rev ); + XGetInputFocus( displayID, &wh, &rev ); if( wh != translateLine->internalWinId() && !byIconClick ) { QPoint p( 1, 1 ); @@ -2918,17 +2925,17 @@ void MainWindow::toggleMainWindow( bool onlyShow ) event.xbutton.x_root = p.x(); event.xbutton.y_root = p.y(); event.xbutton.window = internalWinId(); - event.xbutton.root = QX11Info::appRootWindow( QX11Info::appScreen() ); + event.xbutton.root = XDefaultRootWindow(displayID); event.xbutton.state = Button1Mask; event.xbutton.button = Button1; event.xbutton.same_screen = true; event.xbutton.time = CurrentTime; - XSendEvent( QX11Info::display(), internalWinId(), true, 0xfff, &event ); - XFlush( QX11Info::display() ); + XSendEvent( displayID, internalWinId(), true, 0xfff, &event ); + XFlush( displayID ); event.type = ButtonRelease; - XSendEvent( QX11Info::display(), internalWinId(), true, 0xfff, &event ); - XFlush( QX11Info::display() ); + XSendEvent( displayID, internalWinId(), true, 0xfff, &event ); + XFlush( displayID ); } #endif } diff --git a/utils.hh b/utils.hh index 36318167..bfd71a79 100644 --- a/utils.hh +++ b/utils.hh @@ -28,6 +28,39 @@ inline QString rstrip(const QString &str) { return ""; } +/** + * remove punctuation , space, symbol + * + * + * " abc, '" should be "abc" + */ +inline QString trimNonChar( const QString & str ) +{ + QString remain; + int n = str.size() - 1; + for( ; n >= 0; --n ) + { + auto c = str.at( n ); + if( !c.isSpace() && !c.isSymbol() && !c.isNonCharacter() && !c.isPunct()&& !c.isNull() ) + { + remain = str.left( n + 1 ); + break; + } + } + + n = 0; + for( ; n < remain.size(); n++ ) + { + auto c = remain.at( n ); + if( !c.isSpace() && !c.isSymbol() && !c.isNonCharacter() && !c.isPunct() ) + { + return remain.mid( n ); + } + } + + return ""; +} + /** * str="abc\r\n\u0000" should be returned as "abc" * @brief rstripnull @@ -37,7 +70,8 @@ inline QString rstrip(const QString &str) { inline QString rstripnull(const QString &str) { int n = str.size() - 1; for (; n >= 0; --n) { - if (!str.at(n).isSpace()&&!str.at(n).isNull()) { + auto c = str.at(n); + if (!c.isSpace()&&!c.isNull()) { return str.left(n + 1); } } @@ -96,6 +130,7 @@ inline int loadAcquire( QAtomicInt const & ref ) namespace Url { + // This wrapper is created due to behavior change of the setPath() method // See: https://bugreports.qt-project.org/browse/QTBUG-27728 // https://codereview.qt-project.org/#change,38257 @@ -158,16 +193,15 @@ inline QString fragment( const QUrl & url ) return url.fragment( QUrl::FullyDecoded ); } -// extract query word from url -inline QString getWordFromUrl( const QUrl & url ) +// get the query word of bword and gdlookup scheme. +// if the scheme is gdlookup or scheme ,the first value of pair is true,otherwise is false; +inline std::pair< bool, QString > getQueryWord( QUrl const & url ) { QString word; - if( url.scheme().compare( "bword" ) == 0 ) - { - word = url.path(); - } - else if( url.scheme() == "gdlookup" ) // Plain html links inherit gdlookup scheme + bool validScheme = false; + if( url.scheme().compare( "gdlookup" ) == 0 ) { + validScheme = true; if( hasQueryItem( url, "word" ) ) { word = queryItemValue( url, "word" ); @@ -177,8 +211,12 @@ inline QString getWordFromUrl( const QUrl & url ) word = url.path().mid( 1 ); } } - - return word; + if( url.scheme().compare( "bword" ) == 0 ) + { + validScheme = true; + word = url.path().mid( 1 ); + } + return std::make_pair( validScheme, word ); } } @@ -188,13 +226,6 @@ namespace { /// Uses some heuristics to chop off the first domain name from the host name, /// but only if it's not too base. Returns the resulting host name. -inline QString getHostBase( QUrl const & url ) -{ - QString host = url.host(); - - return getHostBase(host); -} - inline QString getHostBase( QString const & host ) { QStringList domains = host.split( '.' ); @@ -219,6 +250,13 @@ inline QString getHostBase( QString const & host ) else return host; } + +inline QString getHostBaseFromUrl( QUrl const & url ) +{ + QString host = url.host(); + + return getHostBase( host ); +} } #endif // UTILS_HH diff --git a/zim.cc b/zim.cc index 15e8a5be..5fcdad4f 100644 --- a/zim.cc +++ b/zim.cc @@ -1132,32 +1132,8 @@ sptr< Dictionary::DataRequest > ZimDictionary::getSearchResults( QString const & /// ZimDictionary::getArticle() -class ZimArticleRequest; - -class ZimArticleRequestRunnable: public QRunnable -{ - ZimArticleRequest & r; - QSemaphore & hasExited; - -public: - - ZimArticleRequestRunnable( ZimArticleRequest & r_, - QSemaphore & hasExited_ ): r( r_ ), - hasExited( hasExited_ ) - {} - - ~ZimArticleRequestRunnable() - { - hasExited.release(); - } - - virtual void run(); -}; - class ZimArticleRequest: public Dictionary::DataRequest { - friend class ZimArticleRequestRunnable; - wstring word; vector< wstring > alts; ZimDictionary & dict; @@ -1173,11 +1149,10 @@ public: ZimDictionary & dict_, bool ignoreDiacritics_ ): word( word_ ), alts( alts_ ), dict( dict_ ), ignoreDiacritics( ignoreDiacritics_ ) { - QThreadPool::globalInstance()->start( - new ZimArticleRequestRunnable( *this, hasExited ) ); + QThreadPool::globalInstance()->start( [ this ]() { this->run(); } ); } - void run(); // Run from another thread by ZimArticleRequestRunnable + void run(); virtual void cancel() { @@ -1191,11 +1166,6 @@ public: } }; -void ZimArticleRequestRunnable::run() -{ - r.run(); -} - void ZimArticleRequest::run() { if ( Utils::AtomicInt::loadAcquire( isCancelled ) ) @@ -1336,32 +1306,8 @@ sptr< Dictionary::DataRequest > ZimDictionary::getArticle( wstring const & word, //// ZimDictionary::getResource() -class ZimResourceRequest; - -class ZimResourceRequestRunnable: public QRunnable -{ - ZimResourceRequest & r; - QSemaphore & hasExited; - -public: - - ZimResourceRequestRunnable( ZimResourceRequest & r_, - QSemaphore & hasExited_ ): r( r_ ), - hasExited( hasExited_ ) - {} - - ~ZimResourceRequestRunnable() - { - //hasExited.release(); - } - - virtual void run(); -}; - class ZimResourceRequest: public Dictionary::DataRequest { - friend class ZimResourceRequestRunnable; - ZimDictionary & dict; string resourceName; @@ -1372,12 +1318,10 @@ class ZimResourceRequest: public Dictionary::DataRequest public: ZimResourceRequest(ZimDictionary &dict_, string const &resourceName_) : dict(dict_), resourceName(resourceName_) { - //(new ZimResourceRequestRunnable(*this, hasExited))->run(); - QThreadPool::globalInstance()->start( - new ZimResourceRequestRunnable( *this, hasExited ) ); + QThreadPool::globalInstance()->start( [ this ]() { this->run(); } ); } - void run(); // Run from another thread by ZimResourceRequestRunnable + void run(); virtual void cancel() { @@ -1391,11 +1335,6 @@ public: } }; -void ZimResourceRequestRunnable::run() -{ - r.run(); -} - void ZimResourceRequest::run() { // Some runnables linger enough that they are cancelled before they start