From a5a2b4d67e6d6a66361a80aca75bbc1d7040d35b Mon Sep 17 00:00:00 2001 From: Konstantin Isakov Date: Tue, 23 Nov 2010 02:50:30 +0300 Subject: [PATCH] Make Forvo support translatable. We achieve this by making ForvoArticleRequest a proper Q_OBJECT. --- forvo.cc | 111 +++++++++++++++++++------------------------------------ forvo.hh | 39 +++++++++++++++++-- 2 files changed, 73 insertions(+), 77 deletions(-) diff --git a/forvo.cc b/forvo.cc index a91e17d7..c8113699 100644 --- a/forvo.cc +++ b/forvo.cc @@ -70,40 +70,53 @@ public: throw( std::exception ); }; -class ForvoArticleRequest: public ForvoDataRequestSlots +sptr< DataRequest > ForvoDictionary::getArticle( wstring const & word, + vector< wstring > const & alts, + wstring const & ) + throw( std::exception ) { - struct NetReply + if ( word.size() > 80 ) { - sptr< QNetworkReply > reply; - string word; - bool finished; + // Don't make excessively large queries -- they're fruitless anyway - NetReply( sptr< QNetworkReply > const & reply_, string const & word_ ): - reply( reply_ ), word( word_ ), finished( false ) - {} - }; + return new DataRequestInstant( false ); + } + else + return new ForvoArticleRequest( word, alts, apiKey, languageCode, getId(), + netMgr ); +} - typedef std::list< NetReply > NetReplies; - NetReplies netReplies; - QString apiKey, languageCode; - string dictionaryId; +QIcon ForvoDictionary::getIcon() throw() +{ +// Experimental code to generate icon -- but the flags clutter the interface too +// much and we're better with a single icon. +#if 0 + if ( languageCode.size() == 2 ) + { + QString countryCode = Language::countryCodeForId( LangCoder::code2toInt( languageCode.toAscii().data() ) ); -public: + if ( countryCode.size() ) + { + QImage flag( QString( ":/flags/%1.png" ).arg( countryCode.toLower() ) ); - ForvoArticleRequest( wstring const & word, vector< wstring > const & alts, - QString const & apiKey_, - QString const & languageCode_, - string const & dictionaryId_, - QNetworkAccessManager & mgr ); + if ( !flag.isNull() ) + { + QImage img( ":/icons/forvo_icon_base.png" ); - virtual void cancel(); + { + QPainter painter( &img ); + painter.drawImage( QPoint( 5, 7 ), flag ); + } -private: + return QIcon( QPixmap::fromImage( img ) ); + } + } + } +#endif + return QIcon( ":/icons/forvo.png" ); +} - void addQuery( QNetworkAccessManager & mgr, wstring const & word ); - - virtual void requestFinished( QNetworkReply * ); -}; +} void ForvoArticleRequest::cancel() { @@ -342,54 +355,6 @@ void ForvoArticleRequest::requestFinished( QNetworkReply * r ) update(); } -sptr< DataRequest > ForvoDictionary::getArticle( wstring const & word, - vector< wstring > const & alts, - wstring const & ) - throw( std::exception ) -{ - if ( word.size() > 80 ) - { - // Don't make excessively large queries -- they're fruitless anyway - - return new DataRequestInstant( false ); - } - else - return new ForvoArticleRequest( word, alts, apiKey, languageCode, getId(), - netMgr ); -} - -QIcon ForvoDictionary::getIcon() throw() -{ -// Experimental code to generate icon -- but the flags clutter the interface too -// much and we're better with a single icon. -#if 0 - if ( languageCode.size() == 2 ) - { - QString countryCode = Language::countryCodeForId( LangCoder::code2toInt( languageCode.toAscii().data() ) ); - - if ( countryCode.size() ) - { - QImage flag( QString( ":/flags/%1.png" ).arg( countryCode.toLower() ) ); - - if ( !flag.isNull() ) - { - QImage img( ":/icons/forvo_icon_base.png" ); - - { - QPainter painter( &img ); - painter.drawImage( QPoint( 5, 7 ), flag ); - } - - return QIcon( QPixmap::fromImage( img ) ); - } - } - } -#endif - return QIcon( ":/icons/forvo.png" ); -} - -} - vector< sptr< Dictionary::Class > > makeDictionaries( Dictionary::Initializing &, Config::Forvo const & forvo, diff --git a/forvo.hh b/forvo.hh index a2e9a10d..f181b0e7 100644 --- a/forvo.hh +++ b/forvo.hh @@ -7,12 +7,15 @@ #include "dictionary.hh" #include "config.hh" #include +#include "wstring.hh" +#include /// Support for Forvo pronunciations, based on its API. namespace Forvo { using std::vector; using std::string; +using gd::wstring; vector< sptr< Dictionary::Class > > makeDictionaries( Dictionary::Initializing &, @@ -21,14 +24,42 @@ vector< sptr< Dictionary::Class > > makeDictionaries( throw( std::exception ); /// Exposed here for moc -class ForvoDataRequestSlots: public Dictionary::DataRequest +class ForvoArticleRequest: public Dictionary::DataRequest { Q_OBJECT -protected slots: + struct NetReply + { + sptr< QNetworkReply > reply; + string word; + bool finished; - virtual void requestFinished( QNetworkReply * ) - {} + NetReply( sptr< QNetworkReply > const & reply_, string const & word_ ): + reply( reply_ ), word( word_ ), finished( false ) + {} + }; + + typedef std::list< NetReply > NetReplies; + NetReplies netReplies; + QString apiKey, languageCode; + string dictionaryId; + +public: + + ForvoArticleRequest( wstring const & word, vector< wstring > const & alts, + QString const & apiKey_, + QString const & languageCode_, + string const & dictionaryId_, + QNetworkAccessManager & mgr ); + + virtual void cancel(); + +private: + + void addQuery( QNetworkAccessManager & mgr, wstring const & word ); + +private slots: + virtual void requestFinished( QNetworkReply * ); }; }