feat: add an option to make debug dictionary easier (#1086)

* feat: add an option to make debug dictionary easier

* fix: code smells

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
xiaoyifang 2023-08-26 13:19:21 +08:00 committed by GitHub
parent d310f2c55c
commit 6ba8b0c16b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 45 additions and 8 deletions

View file

@ -8,7 +8,7 @@ GlobalBroadcaster::GlobalBroadcaster( QObject * parent ):
{ {
QStringList whiteUrlHosts = { "ajax.googleapis.com" }; QStringList whiteUrlHosts = { "ajax.googleapis.com" };
for ( auto host : whiteUrlHosts ) { for ( const auto host : whiteUrlHosts ) {
addWhitelist( host ); addWhitelist( host );
} }
} }
@ -17,11 +17,20 @@ GlobalBroadcaster * GlobalBroadcaster::instance()
{ {
return bdcaster; return bdcaster;
} }
void GlobalBroadcaster::insertCache( const QString & key, QByteArray * object )
{
//do not cache the item when debug dictionary.
if ( preference->dictionaryDebug )
return;
cache.insert( key, object );
}
void GlobalBroadcaster::setPreference( Config::Preferences * p ) void GlobalBroadcaster::setPreference( Config::Preferences * p )
{ {
preference = p; preference = p;
} }
Config::Preferences * GlobalBroadcaster::getPreference() Config::Preferences * GlobalBroadcaster::getPreference() const
{ {
return preference; return preference;
} }
@ -29,11 +38,11 @@ Config::Preferences * GlobalBroadcaster::getPreference()
void GlobalBroadcaster::addWhitelist( QString url ) void GlobalBroadcaster::addWhitelist( QString url )
{ {
whitelist.insert( url ); whitelist.insert( url );
auto baseUrl = Utils::Url::getHostBase( url ); const auto baseUrl = Utils::Url::getHostBase( url );
whitelist.insert( baseUrl ); whitelist.insert( baseUrl );
} }
bool GlobalBroadcaster::existedInWhitelist( QString url ) bool GlobalBroadcaster::existedInWhitelist( QString url ) const
{ {
return whitelist.contains( url ); return whitelist.contains( url );
} }

View file

@ -29,10 +29,10 @@ class GlobalBroadcaster: public QObject
public: public:
void setPreference( Config::Preferences * _pre ); void setPreference( Config::Preferences * _pre );
Config::Preferences * getPreference(); Config::Preferences * getPreference() const;
GlobalBroadcaster( QObject * parent = nullptr ); GlobalBroadcaster( QObject * parent = nullptr );
void addWhitelist( QString host ); void addWhitelist( QString host );
bool existedInWhitelist( QString host ); bool existedInWhitelist( QString host ) const;
static GlobalBroadcaster * instance(); static GlobalBroadcaster * instance();
unsigned currentGroupId; unsigned currentGroupId;
QString translateLineText{}; QString translateLineText{};
@ -43,6 +43,8 @@ public:
PronounceEngine pronounce_engine; PronounceEngine pronounce_engine;
QCache< QString, QByteArray > cache; QCache< QString, QByteArray > cache;
void insertCache( const QString &, QByteArray * );
signals: signals:
void dictionaryChanges( ActiveDictIds ad ); void dictionaryChanges( ActiveDictIds ad );
void dictionaryClear( ActiveDictIds ad ); void dictionaryClear( ActiveDictIds ad );

View file

@ -494,6 +494,13 @@ void saveMutedDictionaries( QDomDocument & dd, QDomElement & muted, MutedDiction
} // namespace } // namespace
bool fromConfig2Preference( const QDomNode & node, const QString & expectedValue, bool defaultValue = false )
{
if ( !node.isNull() )
return ( node.toElement().text() == expectedValue );
return defaultValue;
}
Class load() Class load()
{ {
QString configName = getConfigFileName(); QString configName = getConfigFileName();
@ -1005,6 +1012,8 @@ Class load()
c.preferences.removeInvalidIndexOnExit = c.preferences.removeInvalidIndexOnExit =
( preferences.namedItem( "removeInvalidIndexOnExit" ).toElement().text() == "1" ); ( preferences.namedItem( "removeInvalidIndexOnExit" ).toElement().text() == "1" );
c.preferences.dictionaryDebug = fromConfig2Preference( preferences.namedItem( "dictionaryDebug" ), "1" );
if ( !preferences.namedItem( "maxStringsInHistory" ).isNull() ) if ( !preferences.namedItem( "maxStringsInHistory" ).isNull() )
c.preferences.maxStringsInHistory = preferences.namedItem( "maxStringsInHistory" ).toElement().text().toUInt(); c.preferences.maxStringsInHistory = preferences.namedItem( "maxStringsInHistory" ).toElement().text().toUInt();
@ -1989,6 +1998,10 @@ void save( Class const & c )
opt.appendChild( dd.createTextNode( c.preferences.removeInvalidIndexOnExit ? "1" : "0" ) ); opt.appendChild( dd.createTextNode( c.preferences.removeInvalidIndexOnExit ? "1" : "0" ) );
preferences.appendChild( opt ); preferences.appendChild( opt );
opt = dd.createElement( "dictionaryDebug" );
opt.appendChild( dd.createTextNode( c.preferences.dictionaryDebug ? "1" : "0" ) );
preferences.appendChild( opt );
opt = dd.createElement( "maxStringsInHistory" ); opt = dd.createElement( "maxStringsInHistory" );
opt.appendChild( dd.createTextNode( QString::number( c.preferences.maxStringsInHistory ) ) ); opt.appendChild( dd.createTextNode( QString::number( c.preferences.maxStringsInHistory ) ) );
preferences.appendChild( opt ); preferences.appendChild( opt );

View file

@ -403,6 +403,7 @@ struct Preferences
int maxNetworkCacheSize; int maxNetworkCacheSize;
bool clearNetworkCacheOnExit; bool clearNetworkCacheOnExit;
bool removeInvalidIndexOnExit = false; bool removeInvalidIndexOnExit = false;
bool dictionaryDebug = false;
qreal zoomFactor; qreal zoomFactor;
qreal helpZoomFactor; qreal helpZoomFactor;

View file

@ -756,7 +756,7 @@ void MddResourceRequest::run()
hasAnyData = true; hasAnyData = true;
data.resize( bytes->size() ); data.resize( bytes->size() );
memcpy( &data.front(), bytes->constData(), bytes->size() ); memcpy( &data.front(), bytes->constData(), bytes->size() );
GlobalBroadcaster::instance()->cache.insert( unique_key, bytes ); GlobalBroadcaster::instance()->insertCache( unique_key, bytes );
break; break;
} }
} }
@ -790,7 +790,7 @@ void MddResourceRequest::run()
data.resize( bytes.size() ); data.resize( bytes.size() );
memcpy( &data.front(), bytes.constData(), bytes.size() ); memcpy( &data.front(), bytes.constData(), bytes.size() );
//cache the processed css result to avoid process again. //cache the processed css result to avoid process again.
GlobalBroadcaster::instance()->cache.insert( unique_key, new QByteArray( bytes ) ); GlobalBroadcaster::instance()->insertCache( unique_key, new QByteArray( bytes ) );
} }
if ( Filetype::isNameOfTiff( u8ResourceName ) ) { if ( Filetype::isNameOfTiff( u8ResourceName ) ) {
// Convert it // Convert it

View file

@ -347,6 +347,7 @@ Preferences::Preferences( QWidget * parent, Config::Class & cfg_ ):
//Misc //Misc
ui.removeInvalidIndexOnExit->setChecked( p.removeInvalidIndexOnExit ); ui.removeInvalidIndexOnExit->setChecked( p.removeInvalidIndexOnExit );
ui.dictionaryDebug->setChecked( p.dictionaryDebug );
// Add-on styles // Add-on styles
ui.addonStylesLabel->setVisible( ui.addonStyles->count() > 1 ); ui.addonStylesLabel->setVisible( ui.addonStyles->count() > 1 );
@ -509,6 +510,7 @@ Config::Preferences Preferences::getPreferences()
p.clearNetworkCacheOnExit = ui.clearNetworkCacheOnExit->isChecked(); p.clearNetworkCacheOnExit = ui.clearNetworkCacheOnExit->isChecked();
p.removeInvalidIndexOnExit = ui.removeInvalidIndexOnExit->isChecked(); p.removeInvalidIndexOnExit = ui.removeInvalidIndexOnExit->isChecked();
p.dictionaryDebug = ui.dictionaryDebug->isChecked();
p.addonStyle = ui.addonStyles->getCurrentStyle(); p.addonStyle = ui.addonStyles->getCurrentStyle();

View file

@ -1960,6 +1960,16 @@ from Stardict, Babylon and GLS dictionaries</string>
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QCheckBox" name="dictionaryDebug">
<property name="toolTip">
<string>When debugging with dictionary css/js, disable certain cache to make it easier.</string>
</property>
<property name="text">
<string>Dictionary debug</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
</item> </item>