From 71399d5eb21ea380cc5b591dbe01c6c53bf922c9 Mon Sep 17 00:00:00 2001 From: vtliem Date: Thu, 21 Jun 2012 18:43:28 +0900 Subject: [PATCH 1/2] Wiki images fix @https://github.com/goldendict/goldendict/issues/102 1/Fix wiki relative image url 2/Add Custom icon for wiki dic (relative file path : configdir/iconpath --- config.cc | 21 +++++++++++++-------- config.hh | 8 ++++---- mediawiki.cc | 17 +++++++++++++++-- sources.cc | 11 ++++++++++- 4 files changed, 42 insertions(+), 15 deletions(-) diff --git a/config.cc b/config.cc index dd1c8b89..6bcc787a 100644 --- a/config.cc +++ b/config.cc @@ -148,14 +148,14 @@ MediaWikis makeDefaultMediaWikis( bool enable ) { MediaWikis mw; - mw.push_back( MediaWiki( "ae6f89aac7151829681b85f035d54e48", "English Wikipedia", "http://en.wikipedia.org/w", enable ) ); - mw.push_back( MediaWiki( "affcf9678e7bfe701c9b071f97eccba3", "English Wiktionary", "http://en.wiktionary.org/w", false ) ); - mw.push_back( MediaWiki( "8e0c1c2b6821dab8bdba8eb869ca7176", "Russian Wikipedia", "http://ru.wikipedia.org/w", false ) ); - mw.push_back( MediaWiki( "b09947600ae3902654f8ad4567ae8567", "Russian Wiktionary", "http://ru.wiktionary.org/w", false ) ); - mw.push_back( MediaWiki( "a8a66331a1242ca2aeb0b4aed361c41d", "German Wikipedia", "http://de.wikipedia.org/w", false ) ); - mw.push_back( MediaWiki( "21c64bca5ec10ba17ff19f3066bc962a", "German Wiktionary", "http://de.wiktionary.org/w", false ) ); - mw.push_back( MediaWiki( "96957cb2ad73a20c7a1d561fc83c253a", "Portuguese Wikipedia", "http://pt.wikipedia.org/w", false ) ); - mw.push_back( MediaWiki( "ed4c3929196afdd93cc08b9a903aad6a", "Portuguese Wiktionary", "http://pt.wiktionary.org/w", false ) ); + mw.push_back( MediaWiki( "ae6f89aac7151829681b85f035d54e48", "English Wikipedia", "http://en.wikipedia.org/w", enable, "" ) ); + mw.push_back( MediaWiki( "affcf9678e7bfe701c9b071f97eccba3", "English Wiktionary", "http://en.wiktionary.org/w", false, "" ) ); + mw.push_back( MediaWiki( "8e0c1c2b6821dab8bdba8eb869ca7176", "Russian Wikipedia", "http://ru.wikipedia.org/w", false, "" ) ); + mw.push_back( MediaWiki( "b09947600ae3902654f8ad4567ae8567", "Russian Wiktionary", "http://ru.wiktionary.org/w", false, "" ) ); + mw.push_back( MediaWiki( "a8a66331a1242ca2aeb0b4aed361c41d", "German Wikipedia", "http://de.wikipedia.org/w", false, "" ) ); + mw.push_back( MediaWiki( "21c64bca5ec10ba17ff19f3066bc962a", "German Wiktionary", "http://de.wiktionary.org/w", false, "" ) ); + mw.push_back( MediaWiki( "96957cb2ad73a20c7a1d561fc83c253a", "Portuguese Wikipedia", "http://pt.wikipedia.org/w", false, "" ) ); + mw.push_back( MediaWiki( "ed4c3929196afdd93cc08b9a903aad6a", "Portuguese Wiktionary", "http://pt.wiktionary.org/w", false, "" ) ); return mw; } @@ -524,6 +524,7 @@ Class load() throw( exError ) w.name = mw.attribute( "name" ); w.url = mw.attribute( "url" ); w.enabled = ( mw.attribute( "enabled" ) == "1" ); + w.icon = mw.attribute( "icon" ); c.mediawikis.push_back( w ); } @@ -948,6 +949,10 @@ void save( Class const & c ) throw( exError ) QDomAttr enabled = dd.createAttribute( "enabled" ); enabled.setValue( i->enabled ? "1" : "0" ); mw.setAttributeNode( enabled ); + + QDomAttr icon = dd.createAttribute( "icon" ); + icon.setValue( i->icon ); + mw.setAttributeNode( icon ); } } diff --git a/config.hh b/config.hh index de967dda..8f8cf5f4 100644 --- a/config.hh +++ b/config.hh @@ -188,19 +188,19 @@ struct Preferences /// A MediaWiki network dictionary definition struct MediaWiki { - QString id, name, url; + QString id, name, url, icon; bool enabled; MediaWiki(): enabled( false ) {} MediaWiki( QString const & id_, QString const & name_, QString const & url_, - bool enabled_ ): - id( id_ ), name( name_ ), url( url_ ), enabled( enabled_ ) {} + bool enabled_, QString icon_ ): + id( id_ ), name( name_ ), url( url_ ), enabled( enabled_ ), icon( icon_ ) {} bool operator == ( MediaWiki const & other ) const { return id == other.id && name == other.name && url == other.url && - enabled == other.enabled; } + enabled == other.enabled && icon == other.icon ; } }; /// Any website which can be queried though a simple template substitution diff --git a/mediawiki.cc b/mediawiki.cc index cbf7467c..3d0c750c 100644 --- a/mediawiki.cc +++ b/mediawiki.cc @@ -20,17 +20,19 @@ namespace { class MediaWikiDictionary: public Dictionary::Class { string name; - QString url; + QString url, icon; QNetworkAccessManager & netMgr; public: MediaWikiDictionary( string const & id, string const & name_, QString const & url_, + QString const & icon_, QNetworkAccessManager & netMgr_ ): Dictionary::Class( id, vector< string >() ), name( name_ ), url( url_ ), + icon( icon_ ), netMgr( netMgr_ ) { } @@ -48,7 +50,15 @@ public: { return 0; } virtual QIcon getIcon() throw() - { return QIcon(":/icons/icon32_wiki.png"); } + { + if( !icon.isNull() && !icon.isEmpty() ) + { + QFileInfo fInfo( QDir( Config::getConfigDir() ), icon ); + if( fInfo.exists() ) + return QIcon( fInfo.absoluteFilePath() ); + } + return QIcon(":/icons/icon32_wiki.png"); + } virtual sptr< WordSearchRequest > prefixMatch( wstring const &, unsigned long maxResults ) throw( std::exception ); @@ -301,6 +311,8 @@ void MediaWikiArticleRequest::requestFinished( QNetworkReply * r ) // Add "http:" to image source urls articleString.replace( " src=\"//", " src=\"http://" ); + //fix src="/foo/bar/Baz.png" + articleString.replace( "src=\"/", "src=\"" + url +"/" ); // Replace the href="/foo/bar/Baz" to just href="Baz". articleString.replace( QRegExp( " > makeDictionaries( result.push_back( new MediaWikiDictionary( wikis[ x ].id.toStdString(), wikis[ x ].name.toUtf8().data(), wikis[ x ].url, + wikis[ x ].icon, mgr ) ); } diff --git a/sources.cc b/sources.cc index ddbccbf5..62328d38 100644 --- a/sources.cc +++ b/sources.cc @@ -40,6 +40,7 @@ Sources::Sources( QWidget * parent, Config::Paths const & paths, ui.mediaWikis->resizeColumnToContents( 0 ); ui.mediaWikis->resizeColumnToContents( 1 ); ui.mediaWikis->resizeColumnToContents( 2 ); + ui.mediaWikis->resizeColumnToContents( 3 ); ui.webSites->setTabKeyNavigation( true ); ui.webSites->setModel( &webSitesModel ); @@ -378,7 +379,7 @@ int MediaWikisModel::columnCount( QModelIndex const & parent ) const if ( parent.isValid() ) return 0; else - return 3; + return 4; } QVariant MediaWikisModel::headerData( int section, Qt::Orientation /*orientation*/, int role ) const @@ -392,6 +393,8 @@ QVariant MediaWikisModel::headerData( int section, Qt::Orientation /*orientation return tr( "Name" ); case 2: return tr( "Address" ); + case 3: + return tr( "Icon" ); default: return QVariant(); } @@ -412,6 +415,8 @@ QVariant MediaWikisModel::data( QModelIndex const & index, int role ) const return mediawikis[ index.row() ].name; case 2: return mediawikis[ index.row() ].url; + case 3: + return mediawikis[ index.row() ].icon; default: return QVariant(); } @@ -452,6 +457,10 @@ bool MediaWikisModel::setData( QModelIndex const & index, const QVariant & value mediawikis[ index.row() ].url = value.toString(); dataChanged( index, index ); return true; + case 3: + mediawikis[ index.row() ].icon = value.toString(); + dataChanged( index, index ); + return true; default: return false; } From adfd993f6bf408062059446e45e238554fe6fcf3 Mon Sep 17 00:00:00 2001 From: vtliem Date: Wed, 27 Jun 2012 17:49:38 +0900 Subject: [PATCH 2/2] Check for valid icon file --- mediawiki.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mediawiki.cc b/mediawiki.cc index 3d0c750c..5d2b897a 100644 --- a/mediawiki.cc +++ b/mediawiki.cc @@ -54,7 +54,7 @@ public: if( !icon.isNull() && !icon.isEmpty() ) { QFileInfo fInfo( QDir( Config::getConfigDir() ), icon ); - if( fInfo.exists() ) + if( fInfo.isFile() ) return QIcon( fInfo.absoluteFilePath() ); } return QIcon(":/icons/icon32_wiki.png");