From b7a707d7c00c6204c5140384392afa3fded3064f Mon Sep 17 00:00:00 2001 From: Abs62 Date: Fri, 17 Jun 2016 17:27:48 +0300 Subject: [PATCH] Websites: Add alternative loading method --- config.cc | 15 ++- config.hh | 9 +- loaddictionaries.cc | 2 +- sources.cc | 48 +++++++-- website.cc | 244 +++++++++++++++++++++++++++++++++++++++++--- website.hh | 16 ++- 6 files changed, 299 insertions(+), 35 deletions(-) diff --git a/config.cc b/config.cc index 9b27b315..2e4c11a2 100644 --- a/config.cc +++ b/config.cc @@ -203,11 +203,11 @@ WebSites makeDefaultWebSites() { WebSites ws; - ws.push_back( WebSite( "b88cb2898e634c6638df618528284c2d", "Google En-En (Oxford)", "https://www.google.com/dictionary?aq=f&langpair=en|en&q=%GDWORD%&hl=en", false, "" ) ); - ws.push_back( WebSite( "f376365a0de651fd7505e7e5e683aa45", "Urban Dictionary", "https://www.urbandictionary.com/define.php?term=%GDWORD%", false, "" ) ); - ws.push_back( WebSite( "324ca0306187df7511b26d3847f4b07c", "Multitran (En)", "https://multitran.ru/c/m.exe?CL=1&l1=1&s=%GD1251%", false, "" ) ); - ws.push_back( WebSite( "924db471b105299c82892067c0f10787", "Lingvo (En-Ru)", "http://lingvopro.abbyyonline.com/en/Search/en-ru/%GDWORD%", false, "" ) ); - ws.push_back( WebSite( "087a6d65615fb047f4c80eef0a9465db", "Michaelis (Pt-En)", "http://michaelis.uol.com.br/moderno/ingles/index.php?lingua=portugues-ingles&palavra=%GDISO1%", false, "" ) ); + ws.push_back( WebSite( "b88cb2898e634c6638df618528284c2d", "Google En-En (Oxford)", "https://www.google.com/dictionary?aq=f&langpair=en|en&q=%GDWORD%&hl=en", false, "", true ) ); + ws.push_back( WebSite( "f376365a0de651fd7505e7e5e683aa45", "Urban Dictionary", "https://www.urbandictionary.com/define.php?term=%GDWORD%", false, "", true ) ); + ws.push_back( WebSite( "324ca0306187df7511b26d3847f4b07c", "Multitran (En)", "https://multitran.ru/c/m.exe?CL=1&l1=1&s=%GD1251%", false, "", true ) ); + ws.push_back( WebSite( "924db471b105299c82892067c0f10787", "Lingvo (En-Ru)", "http://lingvopro.abbyyonline.com/en/Search/en-ru/%GDWORD%", false, "", true ) ); + ws.push_back( WebSite( "087a6d65615fb047f4c80eef0a9465db", "Michaelis (Pt-En)", "http://michaelis.uol.com.br/moderno/ingles/index.php?lingua=portugues-ingles&palavra=%GDISO1%", false, "", true ) ); return ws; } @@ -634,6 +634,7 @@ Class load() throw( exError ) w.url = ws.attribute( "url" ); w.enabled = ( ws.attribute( "enabled" ) == "1" ); w.iconFilename = ws.attribute( "icon" ); + w.inside_iframe = ( ws.attribute( "inside_iframe", "1" ) == "1" ); c.webSites.push_back( w ); } @@ -1353,6 +1354,10 @@ void save( Class const & c ) throw( exError ) QDomAttr icon = dd.createAttribute( "icon" ); icon.setValue( i->iconFilename ); ws.setAttributeNode( icon ); + + QDomAttr inside_iframe = dd.createAttribute( "inside_iframe" ); + inside_iframe.setValue( i->inside_iframe ? "1" : "0" ); + ws.setAttributeNode( inside_iframe ); } } diff --git a/config.hh b/config.hh index b4cf5ffb..f266edac 100644 --- a/config.hh +++ b/config.hh @@ -276,17 +276,20 @@ struct WebSite QString id, name, url; bool enabled; QString iconFilename; + bool inside_iframe; WebSite(): enabled( false ) {} WebSite( QString const & id_, QString const & name_, QString const & url_, - bool enabled_, QString const & iconFilename_ ): - id( id_ ), name( name_ ), url( url_ ), enabled( enabled_ ), iconFilename( iconFilename_ ) {} + bool enabled_, QString const & iconFilename_, bool inside_iframe_ ): + id( id_ ), name( name_ ), url( url_ ), enabled( enabled_ ), iconFilename( iconFilename_ ), + inside_iframe( inside_iframe_ ) {} bool operator == ( WebSite const & other ) const { return id == other.id && name == other.name && url == other.url && - enabled == other.enabled && iconFilename == other.iconFilename; } + enabled == other.enabled && iconFilename == other.iconFilename && + inside_iframe == other.inside_iframe; } }; /// All the WebSites diff --git a/loaddictionaries.cc b/loaddictionaries.cc index cf31e632..3eaebf24 100644 --- a/loaddictionaries.cc +++ b/loaddictionaries.cc @@ -328,7 +328,7 @@ void loadDictionaries( QWidget * parent, bool showInitially, ///// WebSites are very simple, no need to create them asyncronously { vector< sptr< Dictionary::Class > > dicts = - WebSite::makeDictionaries( cfg.webSites ); + WebSite::makeDictionaries( cfg.webSites, dictNetMgr ); dictionaries.insert( dictionaries.end(), dicts.begin(), dicts.end() ); } diff --git a/sources.cc b/sources.cc index 48925aa4..49166698 100644 --- a/sources.cc +++ b/sources.cc @@ -58,6 +58,7 @@ Sources::Sources( QWidget * parent, Config::Class const & cfg): ui.webSites->resizeColumnToContents( 1 ); ui.webSites->resizeColumnToContents( 2 ); ui.webSites->resizeColumnToContents( 3 ); + ui.webSites->resizeColumnToContents( 4 ); ui.dictServers->setTabKeyNavigation( true ); ui.dictServers->setModel( &dictServersModel ); @@ -588,7 +589,7 @@ Qt::ItemFlags WebSitesModel::flags( QModelIndex const & index ) const if ( index.isValid() ) { - if ( !index.column() ) + if ( index.column() <= 1 ) result |= Qt::ItemIsUserCheckable; else result |= Qt::ItemIsEditable; @@ -615,16 +616,26 @@ int WebSitesModel::columnCount( QModelIndex const & parent ) const QVariant WebSitesModel::headerData( int section, Qt::Orientation /*orientation*/, int role ) const { + if( role == Qt::ToolTipRole ) + { + if( section == 1 ) + return tr( "Insert article as link inside "; + string result = "
"; - dr->getData().resize( result.size() ); + result += string( ""; - memcpy( &( dr->getData().front() ), result.data(), result.size() ); + dr->getData().resize( result.size() ); - return dr; + memcpy( &( dr->getData().front() ), result.data(), result.size() ); + + return dr; + } + + // To load data from site + + return new WebSiteArticleRequest( urlString, netMgr, this ); } void WebSiteDictionary::loadIcon() throw() @@ -158,7 +366,8 @@ void WebSiteDictionary::loadIcon() throw() } -vector< sptr< Dictionary::Class > > makeDictionaries( Config::WebSites const & ws ) +vector< sptr< Dictionary::Class > > makeDictionaries( Config::WebSites const & ws, + QNetworkAccessManager & mgr ) throw( std::exception ) { vector< sptr< Dictionary::Class > > result; @@ -169,7 +378,10 @@ vector< sptr< Dictionary::Class > > makeDictionaries( Config::WebSites const & w result.push_back( new WebSiteDictionary( ws[ x ].id.toUtf8().data(), ws[ x ].name.toUtf8().data(), ws[ x ].url, - ws[ x ].iconFilename ) ); + ws[ x ].iconFilename, + ws[ x ].inside_iframe, + mgr ) + ); } return result; diff --git a/website.hh b/website.hh index dd5bf379..899641fb 100644 --- a/website.hh +++ b/website.hh @@ -6,6 +6,8 @@ #include "dictionary.hh" #include "config.hh" +#include +#include /// Support for any web sites via a templated url. namespace WebSite { @@ -13,9 +15,21 @@ namespace WebSite { using std::vector; using std::string; -vector< sptr< Dictionary::Class > > makeDictionaries( Config::WebSites const & ) +vector< sptr< Dictionary::Class > > makeDictionaries( Config::WebSites const &, + QNetworkAccessManager & ) throw( std::exception ); +/// Exposed here for moc +class WebSiteDataRequestSlots: public Dictionary::DataRequest +{ + Q_OBJECT + +protected slots: + + virtual void requestFinished( QNetworkReply * ) + {} +}; + } #endif