Merge branch 'staged' into dev

This commit is contained in:
Xiao YiFang 2022-07-15 19:51:23 +08:00
commit a8b57564d8
13 changed files with 39 additions and 86 deletions

View file

@ -11,19 +11,17 @@ using std::vector;
EditDictionaries::EditDictionaries( QWidget * parent, Config::Class & cfg_,
vector< sptr< Dictionary::Class > > & dictionaries_,
QMap<std::string, sptr< Dictionary::Class > > & dictMap_,
Instances::Groups & groupInstances_,
QNetworkAccessManager & dictNetMgr_ ):
QDialog( parent, Qt::WindowSystemMenuHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint ),
cfg( cfg_ ),
dictionaries( dictionaries_ ),
dictMap(dictMap_),
groupInstances( groupInstances_ ),
dictNetMgr( dictNetMgr_ ),
origCfg( cfg ),
sources( this, cfg ),
orderAndProps( new OrderAndProps( this, cfg.dictionaryOrder, cfg.inactiveDictionaries,
dictionaries, dictMap ) ),
dictionaries ) ),
groups( new Groups( this, dictionaries, cfg.groups, orderAndProps->getCurrentDictionaryOrder() ) ),
dictionariesChanged( false ),
groupsChanged( false ),
@ -216,7 +214,7 @@ void EditDictionaries::acceptChangedSources( bool rebuildGroups )
orderAndProps.clear();
loadDictionaries( this, true, cfg, dictionaries, dictNetMgr );
dictMap = Dictionary::dictToMap(dictionaries);
// If no changes to groups were made, update the original data
bool noGroupEdits = ( origCfg.groups == savedGroups );
@ -241,7 +239,7 @@ void EditDictionaries::acceptChangedSources( bool rebuildGroups )
if ( rebuildGroups )
{
orderAndProps = new OrderAndProps( this, savedOrder, savedInactive, dictionaries, dictMap );
orderAndProps = new OrderAndProps( this, savedOrder, savedInactive, dictionaries );
groups = new Groups( this, dictionaries, savedGroups, orderAndProps->getCurrentDictionaryOrder() );
ui.tabs->removeTab( 1 );

View file

@ -23,7 +23,6 @@ public:
EditDictionaries( QWidget * parent, Config::Class & cfg,
std::vector< sptr< Dictionary::Class > > & dictionaries,
QMap<std::string, sptr< Dictionary::Class > > & dictMap_,
Instances::Groups & groupInstances, // We only clear those on rescan
QNetworkAccessManager & dictNetMgr );
@ -78,7 +77,6 @@ private:
Config::Class & cfg;
std::vector< sptr< Dictionary::Class > > & dictionaries;
QMap<std::string, sptr< Dictionary::Class > > & dictMap;
Instances::Groups & groupInstances;
QNetworkAccessManager & dictNetMgr;

View file

@ -20,7 +20,7 @@ Groups::Groups( QWidget * parent,
// Populate the dictionaries' list
ui.dictionaries->setAsSource();
ui.dictionaries->populate( Instances::Group( order, Dictionary::dictToMap(dicts), Config::Group() ).dictionaries,
ui.dictionaries->populate( Instances::Group( order, dicts, Config::Group() ).dictionaries,
dicts );
ui.searchLine->applyTo( ui.dictionaries );
@ -87,13 +87,12 @@ void Groups::editGroup( unsigned id )
void Groups::updateDictionaryOrder( Config::Group const & order )
{
// Make sure it differs from what we have
auto dictMap = Dictionary::dictToMap( dicts );
Instances::Group newOrder( order, dictMap, Config::Group() );
Instances::Group newOrder( order, dicts, Config::Group() );
if( ui.dictionaries->getCurrentDictionaries() != newOrder.dictionaries )
{
// Repopulate
ui.dictionaries->populate( Instances::Group( order, dictMap, Config::Group() ).dictionaries, dicts );
ui.dictionaries->populate( Instances::Group( order, dicts, Config::Group() ).dictionaries, dicts );
}
}

View file

@ -31,8 +31,7 @@ DictGroupWidget::DictGroupWidget( QWidget * parent,
groupId( group.id )
{
ui.setupUi( this );
auto dictMap = Dictionary::dictToMap(dicts);
ui.dictionaries->populate( Instances::Group( group, dictMap, Config::Group() ).dictionaries, dicts );
ui.dictionaries->populate( Instances::Group( group, dicts, Config::Group() ).dictionaries, dicts );
// Populate icons' list
@ -329,7 +328,7 @@ bool DictListModel::setData( QModelIndex const & index, const QVariant & value,
g.dictionaries.push_back( Config::DictionaryRef( value.toString(), QString() ) );
Instances::Group i( g, Dictionary::dictToMap(*allDicts), Config::Group() );
Instances::Group i( g, *allDicts, Config::Group() );
if ( i.dictionaries.size() == 1 )
{

View file

@ -46,57 +46,16 @@ void IframeSchemeHandler::requestStarted(QWebEngineUrlRequestJob *requestJob)
while( !base.isEmpty() && !base.endsWith( "/" ) )
base.chop( 1 );
QRegularExpression tags( "<\\s*(a|link|img|script)\\s+[^>]*(src|href)\\s*=\\s*['\"][^>]+>",
QRegularExpression::CaseInsensitiveOption );
QRegularExpression links( "\\b(src|href)\\s*=\\s*(['\"])([^'\"]+['\"])",
QRegularExpression::CaseInsensitiveOption );
int pos = 0;
QString articleNewString;
QRegularExpressionMatchIterator it = tags.globalMatch( articleString );
while( it.hasNext() )
{
QRegularExpressionMatch match = it.next();
articleNewString += articleString.mid( pos, match.capturedStart() - pos );
pos = match.capturedEnd();
QString tag = match.captured();
QRegularExpressionMatch match_links = links.match( tag );
if( !match_links.hasMatch() )
{
articleNewString += tag;
continue;
}
QString url = match_links.captured( 3 );
if( url.indexOf( ":/" ) >= 0 || url.indexOf( "data:" ) >= 0 || url.indexOf( "mailto:" ) >= 0 ||
url.startsWith( "#" ) || url.startsWith( "javascript:" ) )
{
// External link, anchor or base64-encoded data
articleNewString += tag;
continue;
}
QString newUrl = match_links.captured( 1 ) + "=" + match_links.captured( 2 );
if( url.startsWith( "//" ) )
newUrl += reply->url().scheme() + ":";
else if( url.startsWith( "/" ) )
newUrl += root;
else
newUrl += base;
newUrl += match_links.captured( 3 );
tag.replace( match_links.capturedStart(), match_links.capturedLength(), newUrl );
articleNewString += tag;
}
if( pos )
{
articleNewString += articleString.mid( pos );
articleString = articleNewString;
articleNewString.clear();
}
QRegularExpression tags( "<base\\s+.*?>",
QRegularExpression::CaseInsensitiveOption | QRegularExpression::DotMatchesEverythingOption );
QString baseTagHtml = "<base href=\"" + base + "\">";
// remove existed base tag
articleString.remove( tags ) ;
qsizetype pos = articleString.indexOf( "<head>" );
if( pos > -1 )
articleString.insert( pos + 6, baseTagHtml );
buffer->setData(codec->fromUnicode(articleString));

View file

@ -11,7 +11,7 @@ using std::set;
using std::string;
Group::Group( Config::Group const & cfgGroup,
QMap<std::string, sptr< Dictionary::Class > > const & allDictionaries,
std::vector< sptr< Dictionary::Class > > const & allDictionaries,
Config::Group const & inactiveGroup ):
id( cfgGroup.id ),
name( cfgGroup.name ),
@ -24,13 +24,15 @@ Group::Group( Config::Group const & cfgGroup,
QMap<string, sptr< Dictionary::Class > > groupDicts;
QVector<string> dictOrderList;
auto dictMap = Dictionary::dictToMap( allDictionaries );
for( unsigned x = 0; x < (unsigned)cfgGroup.dictionaries.size(); ++x )
{
std::string id = cfgGroup.dictionaries[ x ].id.toStdString();
if(allDictionaries.contains(id)){
groupDicts.insert(id, allDictionaries[ id ] );
if( dictMap.contains( id ) )
{
groupDicts.insert( id, dictMap[ id ] );
dictOrderList.push_back(id);
}
}

View file

@ -29,7 +29,7 @@ struct Group
/// Instantiates the given group from its configuration. If some dictionary
/// wasn't found, it just skips it.
Group( Config::Group const & cfgGroup,
QMap<std::string, sptr< Dictionary::Class > > const & allDictionaries,
std::vector< sptr< Dictionary::Class > > const & allDictionaries,
Config::Group const & inactiveGroup );
/// Creates an empty group.

View file

@ -1398,11 +1398,11 @@ void MainWindow::updateGroupList()
// Add dictionaryOrder first, as the 'All' group.
{
Instances::Group g( cfg.dictionaryOrder, dictMap, Config::Group() );
Instances::Group g( cfg.dictionaryOrder, dictionaries, Config::Group() );
// Add any missing entries to dictionary order
Instances::complementDictionaryOrder( g,
Instances::Group( cfg.inactiveDictionaries, dictMap, Config::Group() ),
Instances::Group( cfg.inactiveDictionaries, dictionaries, Config::Group() ),
dictionaries );
g.name = tr( "All" );
@ -1413,7 +1413,7 @@ void MainWindow::updateGroupList()
}
for( int x = 0; x < cfg.groups.size(); ++x )
groupInstances.push_back( Instances::Group( cfg.groups[ x ], dictMap, cfg.inactiveDictionaries ) );
groupInstances.push_back( Instances::Group( cfg.groups[ x ], dictionaries, cfg.inactiveDictionaries ) );
// Update names for dictionaries that are present, so that they could be
// found in case they got moved.
@ -2072,7 +2072,7 @@ void MainWindow::editDictionaries( unsigned editDictionaryGroup )
{ // Limit existence of newCfg
Config::Class newCfg = cfg;
EditDictionaries dicts( this, newCfg, dictionaries, dictMap, groupInstances, dictNetMgr );
EditDictionaries dicts( this, newCfg, dictionaries, groupInstances, dictNetMgr );
connect( &dicts, SIGNAL( showDictionaryInfo( QString const & ) ),
this, SLOT( showDictionaryInfo( QString const & ) ) );

View file

@ -15,6 +15,7 @@
#include "utils.hh"
#include <QRegularExpression>
#include "globalbroadcaster.h"
namespace MediaWiki {
@ -125,6 +126,8 @@ MediaWikiWordSearchRequest::MediaWikiWordSearchRequest( wstring const & str,
GD_DPRINTF( "request begin\n" );
QUrl reqUrl( url + "/api.php?action=query&list=allpages&aplimit=40&format=xml" );
GlobalBroadcaster::instance()->addWhitelist( reqUrl.host() );
Utils::Url::addQueryItem( reqUrl, "apfrom", gd::toQString( str ).replace( '+', "%2B" ) );
netReply = mgr.get( QNetworkRequest( reqUrl ) );

View file

@ -85,15 +85,12 @@ bool dictLessThan( sptr< Dictionary::Class > const & dict1,
OrderAndProps::OrderAndProps( QWidget * parent,
Config::Group const & dictionaryOrder,
Config::Group const & inactiveDictionaries,
std::vector< sptr< Dictionary::Class > > const &
allDictionaries ,
QMap<std::string, sptr< Dictionary::Class > > const & dictMap):
std::vector< sptr< Dictionary::Class > > const & allDictionaries):
QWidget( parent )
{
ui.setupUi( this );
Instances::Group order( dictionaryOrder, dictMap, Config::Group() );
Instances::Group inactive( inactiveDictionaries, dictMap, Config::Group() );
Instances::Group order( dictionaryOrder, allDictionaries, Config::Group() );
Instances::Group inactive( inactiveDictionaries, allDictionaries, Config::Group() );
Instances::complementDictionaryOrder( order, inactive, allDictionaries );

View file

@ -16,8 +16,7 @@ public:
OrderAndProps( QWidget * parent, Config::Group const & dictionaryOrder,
Config::Group const & inactiveDictionaries,
std::vector< sptr< Dictionary::Class > > const & allDictionaries ,
QMap<std::string, sptr< Dictionary::Class > > const & dictMap);
std::vector< sptr< Dictionary::Class > > const & allDictionaries);
Config::Group getCurrentDictionaryOrder() const;
Config::Group getCurrentInactiveDictionaries() const;
@ -35,7 +34,6 @@ private slots:
private:
Ui::OrderAndProps ui;
void disableDictionaryDescription();
void describeDictionary( DictListWidget *, QModelIndex const & );

View file

@ -84,11 +84,11 @@ inline bool isExternalLink(QUrl const &url) {
url.scheme() == "file" || url.toString().startsWith( "//" );
}
inline bool isCssFontImage(QUrl const &url) {
inline bool isHtmlResources(QUrl const &url) {
auto fileName = url.fileName();
auto ext=fileName.mid(fileName.lastIndexOf("."));
QStringList extensions{".css",".woff",".woff2","ttf",".bmp" ,".jpg", ".png", ".tif",".wav", ".ogg", ".oga", ".mp3", ".mp4", ".aac", ".flac",".mid", ".wv ",".ape"} ;
return extensions.indexOf(ext)>-1;
QStringList extensions{".css",".woff",".woff2","ttf",".bmp" ,".jpg", ".png",".gif", ".tif",".wav", ".ogg", ".oga", ".mp3", ".mp4", ".aac", ".flac",".mid", ".wv",".ape"} ;
return extensions.contains( ext, Qt::CaseInsensitive );
}
inline QString escape( QString const & plain )

View file

@ -21,7 +21,7 @@ void WebUrlRequestInterceptor::interceptRequest( QWebEngineUrlRequestInfo &info)
//whitelist url does not block
return;
}
if(Utils::isCssFontImage(info.requestUrl())){
if(Utils::isHtmlResources(info.requestUrl())){
//let throuth the resources file.
return;
}