Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/common/globalbroadcaster.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ GlobalBroadcaster::GlobalBroadcaster( QObject * parent ):
{
QStringList whiteUrlHosts = { "ajax.googleapis.com" };

for ( auto host : whiteUrlHosts ) {
for ( const auto host : whiteUrlHosts ) {
addWhitelist( host );
}
}
Expand All @@ -17,23 +17,32 @@ GlobalBroadcaster * GlobalBroadcaster::instance()
{
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 )
{
preference = p;
}
Config::Preferences * GlobalBroadcaster::getPreference()
Config::Preferences * GlobalBroadcaster::getPreference() const
{
return preference;
}

void GlobalBroadcaster::addWhitelist( QString url )
{
whitelist.insert( url );
auto baseUrl = Utils::Url::getHostBase( url );
const auto baseUrl = Utils::Url::getHostBase( url );
whitelist.insert( baseUrl );
}

bool GlobalBroadcaster::existedInWhitelist( QString url )
bool GlobalBroadcaster::existedInWhitelist( QString url ) const
{
return whitelist.contains( url );
}
Expand Down
6 changes: 4 additions & 2 deletions src/common/globalbroadcaster.hh
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ class GlobalBroadcaster: public QObject

public:
void setPreference( Config::Preferences * _pre );
Config::Preferences * getPreference();
Config::Preferences * getPreference() const;
GlobalBroadcaster( QObject * parent = nullptr );
void addWhitelist( QString host );
bool existedInWhitelist( QString host );
bool existedInWhitelist( QString host ) const;
static GlobalBroadcaster * instance();
unsigned currentGroupId;
QString translateLineText{};
Expand All @@ -43,6 +43,8 @@ public:
PronounceEngine pronounce_engine;
QCache< QString, QByteArray > cache;

void insertCache( const QString &, QByteArray * );

signals:
void dictionaryChanges( ActiveDictIds ad );
void dictionaryClear( ActiveDictIds ad );
Expand Down
13 changes: 13 additions & 0 deletions src/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,13 @@ void saveMutedDictionaries( QDomDocument & dd, QDomElement & muted, MutedDiction

} // namespace

bool fromConfig2Preference( const QDomNode & node, const QString & expectedValue, bool defaultValue = false )
{
if ( !node.isNull() )
return ( node.toElement().text() == expectedValue );
return defaultValue;
}

Class load()
{
QString configName = getConfigFileName();
Expand Down Expand Up @@ -1005,6 +1012,8 @@ Class load()
c.preferences.removeInvalidIndexOnExit =
( preferences.namedItem( "removeInvalidIndexOnExit" ).toElement().text() == "1" );

c.preferences.dictionaryDebug = fromConfig2Preference( preferences.namedItem( "dictionaryDebug" ), "1" );

if ( !preferences.namedItem( "maxStringsInHistory" ).isNull() )
c.preferences.maxStringsInHistory = preferences.namedItem( "maxStringsInHistory" ).toElement().text().toUInt();

Expand Down Expand Up @@ -1989,6 +1998,10 @@ void save( Class const & c )
opt.appendChild( dd.createTextNode( c.preferences.removeInvalidIndexOnExit ? "1" : "0" ) );
preferences.appendChild( opt );

opt = dd.createElement( "dictionaryDebug" );
opt.appendChild( dd.createTextNode( c.preferences.dictionaryDebug ? "1" : "0" ) );
preferences.appendChild( opt );

opt = dd.createElement( "maxStringsInHistory" );
opt.appendChild( dd.createTextNode( QString::number( c.preferences.maxStringsInHistory ) ) );
preferences.appendChild( opt );
Expand Down
1 change: 1 addition & 0 deletions src/config.hh
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ struct Preferences
int maxNetworkCacheSize;
bool clearNetworkCacheOnExit;
bool removeInvalidIndexOnExit = false;
bool dictionaryDebug = false;

qreal zoomFactor;
qreal helpZoomFactor;
Expand Down
4 changes: 2 additions & 2 deletions src/dict/mdx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ void MddResourceRequest::run()
hasAnyData = true;
data.resize( bytes->size() );
memcpy( &data.front(), bytes->constData(), bytes->size() );
GlobalBroadcaster::instance()->cache.insert( unique_key, bytes );
GlobalBroadcaster::instance()->insertCache( unique_key, bytes );
break;
}
}
Expand Down Expand Up @@ -790,7 +790,7 @@ void MddResourceRequest::run()
data.resize( bytes.size() );
memcpy( &data.front(), bytes.constData(), bytes.size() );
//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 ) ) {
// Convert it
Expand Down
2 changes: 2 additions & 0 deletions src/ui/preferences.cc
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ Preferences::Preferences( QWidget * parent, Config::Class & cfg_ ):

//Misc
ui.removeInvalidIndexOnExit->setChecked( p.removeInvalidIndexOnExit );
ui.dictionaryDebug->setChecked( p.dictionaryDebug );

// Add-on styles
ui.addonStylesLabel->setVisible( ui.addonStyles->count() > 1 );
Expand Down Expand Up @@ -509,6 +510,7 @@ Config::Preferences Preferences::getPreferences()
p.clearNetworkCacheOnExit = ui.clearNetworkCacheOnExit->isChecked();

p.removeInvalidIndexOnExit = ui.removeInvalidIndexOnExit->isChecked();
p.dictionaryDebug = ui.dictionaryDebug->isChecked();

p.addonStyle = ui.addonStyles->getCurrentStyle();

Expand Down
10 changes: 10 additions & 0 deletions src/ui/preferences.ui
Original file line number Diff line number Diff line change
Expand Up @@ -1960,6 +1960,16 @@ from Stardict, Babylon and GLS dictionaries</string>
</property>
</widget>
</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>
</widget>
</item>
Expand Down