Compare commits

...

4 commits

Author SHA1 Message Date
xiaoyifang ecb90c86ce
Merge 3c5233f2a1 into 720f66c781 2024-11-11 01:04:15 -05:00
shenleban tongying 720f66c781 clean: make toggleMainWindow reasonable
Some checks are pending
SonarCloud / Build and analyze (push) Waiting to run
2024-11-11 00:18:02 -05:00
shenleban tongying acbfef0870 opt: don't focus on the main window if word comes from headword dialog 2024-11-11 00:18:02 -05:00
shenleban tongying d4db51f278 opt: mdx -> avoid duplicated Adler-32 checksum in zlib decompression 2024-11-10 22:32:22 -05:00
6 changed files with 35 additions and 48 deletions

View file

@ -26,6 +26,7 @@ Checks: >
-google-default-arguments, -google-default-arguments,
-google-readability-casting, -google-readability-casting,
-hicpp-deprecated-headers, -hicpp-deprecated-headers,
-hicpp-no-array-decay,
-misc-const-correctness, -misc-const-correctness,
-misc-include-cleaner, -misc-include-cleaner,
-misc-non-private-member-variables-in-classes, -misc-non-private-member-variables-in-classes,

View file

@ -264,14 +264,12 @@ bool MdictParser::parseCompressedBlock( qint64 compressedBlockSize,
case 0x02000000: case 0x02000000:
// zlib compression // zlib compression
decompressedBlock = zlibDecompress( buf, size ); decompressedBlock = zlibDecompress( buf, size, checksum );
if ( decompressedBlock.isEmpty() ) {
if ( !checkAdler32( decompressedBlock.constData(), decompressedBlock.size(), checksum ) ) { gdWarning( "MDict: parseCompressedBlock: zlib: failed to decompress or checksum does not match" );
gdWarning( "MDict: parseCompressedBlock: zlib: checksum does not match" );
return false; return false;
} }
break; break;
default: default:
gdWarning( "MDict: parseCompressedBlock: unknown type" ); gdWarning( "MDict: parseCompressedBlock: unknown type" );
return false; return false;

View file

@ -3,20 +3,21 @@
#include <bzlib.h> #include <bzlib.h>
#include <lzma.h> #include <lzma.h>
#define CHUNK_SIZE 2048 using std::string;
QByteArray zlibDecompress( const char * bufptr, unsigned length ) static constexpr qsizetype CHUNK_SIZE = 2048;
QByteArray zlibDecompress( const char * bufptr, unsigned length, uLong adler32_checksum )
{ {
z_stream zs; z_stream zs{};
char buf[ CHUNK_SIZE ];
QByteArray str; QByteArray str;
int res; int res = Z_OK;
memset( &zs, 0, sizeof( zs ) );
zs.next_in = (Bytef *)bufptr; zs.next_in = (Bytef *)bufptr;
zs.avail_in = length; zs.avail_in = length;
res = inflateInit( &zs ); res = inflateInit( &zs );
if ( res == Z_OK ) { if ( res == Z_OK ) {
char buf[ CHUNK_SIZE ];
while ( res != Z_STREAM_END ) { while ( res != Z_STREAM_END ) {
zs.next_out = (Bytef *)buf; zs.next_out = (Bytef *)buf;
zs.avail_out = CHUNK_SIZE; zs.avail_out = CHUNK_SIZE;
@ -27,9 +28,7 @@ QByteArray zlibDecompress( const char * bufptr, unsigned length )
} }
} }
} }
if ( inflateEnd( &zs ) != Z_OK || res != Z_STREAM_END || ( adler32_checksum != 0 && zs.adler != adler32_checksum ) ) {
inflateEnd( &zs );
if ( res != Z_STREAM_END ) {
str.clear(); str.clear();
} }
return str; return str;
@ -37,7 +36,7 @@ QByteArray zlibDecompress( const char * bufptr, unsigned length )
string decompressZlib( const char * bufptr, unsigned length ) string decompressZlib( const char * bufptr, unsigned length )
{ {
QByteArray b = zlibDecompress( bufptr, length ); QByteArray b = zlibDecompress( bufptr, length, 0 );
return string( b.constData(), b.size() ); return string( b.constData(), b.size() );
} }

View file

@ -3,12 +3,11 @@
#include <QByteArray> #include <QByteArray>
#include <string> #include <string>
using std::string; /// @param adler32_checksum 0 to skip checksum
QByteArray zlibDecompress( const char * bufptr, unsigned length, unsigned long adler32_checksum );
QByteArray zlibDecompress( const char * bufptr, unsigned length ); std::string decompressZlib( const char * bufptr, unsigned length );
string decompressZlib( const char * bufptr, unsigned length ); std::string decompressBzip2( const char * bufptr, unsigned length );
string decompressBzip2( const char * bufptr, unsigned length ); std::string decompressLzma2( const char * bufptr, unsigned length, bool raw_decoder = false );
string decompressLzma2( const char * bufptr, unsigned length, bool raw_decoder = false );

View file

@ -402,10 +402,9 @@ MainWindow::MainWindow( Config::Class & cfg_ ):
connect( wordsZoomBase, &QAction::triggered, this, &MainWindow::doWordsZoomBase ); connect( wordsZoomBase, &QAction::triggered, this, &MainWindow::doWordsZoomBase );
// tray icon // tray icon
connect( trayIconMenu.addAction( tr( "Show &Main Window" ) ), connect( trayIconMenu.addAction( tr( "Show &Main Window" ) ), &QAction::triggered, this, [ this ] {
&QAction::triggered, this->toggleMainWindow( true );
this, } );
&MainWindow::showMainWindow );
trayIconMenu.addAction( enableScanningAction ); trayIconMenu.addAction( enableScanningAction );
trayIconMenu.addSeparator(); trayIconMenu.addSeparator();
@ -2532,7 +2531,7 @@ void MainWindow::handleEsc()
} }
if ( cfg.preferences.escKeyHidesMainWindow ) { if ( cfg.preferences.escKeyHidesMainWindow ) {
toggleMainWindow(); toggleMainWindow( false );
} }
else { else {
focusTranslateLine(); focusTranslateLine();
@ -2873,7 +2872,7 @@ void MainWindow::showTranslationForDicts( QString const & inWord,
ignoreDiacritics ); ignoreDiacritics );
} }
void MainWindow::toggleMainWindow( bool onlyShow ) void MainWindow::toggleMainWindow( bool ensureShow )
{ {
bool shown = false; bool shown = false;
@ -2906,7 +2905,7 @@ void MainWindow::toggleMainWindow( bool onlyShow )
} }
shown = true; shown = true;
} }
else if ( !onlyShow ) { else if ( !ensureShow ) {
// On Windows and Linux, a hidden window won't show a task bar icon // On Windows and Linux, a hidden window won't show a task bar icon
// When trayicon is enabled, the duplication is unneeded // When trayicon is enabled, the duplication is unneeded
@ -2990,7 +2989,7 @@ void MainWindow::installHotKeys()
void MainWindow::hotKeyActivated( int hk ) void MainWindow::hotKeyActivated( int hk )
{ {
if ( !hk ) { if ( !hk ) {
toggleMainWindow(); toggleMainWindow( false );
} }
else if ( scanPopup ) { else if ( scanPopup ) {
#ifdef HAVE_X11 #ifdef HAVE_X11
@ -3075,7 +3074,7 @@ void MainWindow::trayIconActivated( QSystemTrayIcon::ActivationReason r )
switch ( r ) { switch ( r ) {
case QSystemTrayIcon::Trigger: case QSystemTrayIcon::Trigger:
// Left click toggles the visibility of main window // Left click toggles the visibility of main window
toggleMainWindow(); toggleMainWindow( false );
break; break;
case QSystemTrayIcon::MiddleClick: case QSystemTrayIcon::MiddleClick:
@ -3088,10 +3087,6 @@ void MainWindow::trayIconActivated( QSystemTrayIcon::ActivationReason r )
} }
} }
void MainWindow::showMainWindow()
{
toggleMainWindow( true );
}
void MainWindow::visitHomepage() void MainWindow::visitHomepage()
{ {
@ -3730,13 +3725,6 @@ void MainWindow::wordReceived( const QString & word )
respondToTranslationRequest( word, false ); respondToTranslationRequest( word, false );
} }
void MainWindow::headwordReceived( const QString & word, const QString & ID )
{
toggleMainWindow( true );
setInputLineText( word, WildcardPolicy::EscapeWildcards, NoPopupChange );
respondToTranslationRequest( word, false, ArticleView::scrollToFromDictionaryId( ID ), false );
}
void MainWindow::updateFavoritesMenu() void MainWindow::updateFavoritesMenu()
{ {
if ( ui.favoritesPane->isVisible() ) { if ( ui.favoritesPane->isVisible() ) {
@ -4141,7 +4129,13 @@ void MainWindow::showDictionaryHeadwords( Dictionary::Class * dict )
headwordsDlg = new DictHeadwords( this, cfg, dict ); headwordsDlg = new DictHeadwords( this, cfg, dict );
addGlobalActionsToDialog( headwordsDlg ); addGlobalActionsToDialog( headwordsDlg );
addGroupComboBoxActionsToDialog( headwordsDlg, groupList ); addGroupComboBoxActionsToDialog( headwordsDlg, groupList );
connect( headwordsDlg, &DictHeadwords::headwordSelected, this, &MainWindow::headwordReceived ); connect( headwordsDlg,
&DictHeadwords::headwordSelected,
this,
[ this ]( QString const & headword, QString const & dictID ) {
setInputLineText( headword, WildcardPolicy::EscapeWildcards, NoPopupChange );
respondToTranslationRequest( headword, false, ArticleView::scrollToFromDictionaryId( dictID ), false );
} );
connect( headwordsDlg, connect( headwordsDlg,
&DictHeadwords::closeDialog, &DictHeadwords::closeDialog,
this, this,

View file

@ -67,7 +67,6 @@ public slots:
void messageFromAnotherInstanceReceived( QString const & ); void messageFromAnotherInstanceReceived( QString const & );
void showStatusBarMessage( QString const &, int, QPixmap const & ); void showStatusBarMessage( QString const &, int, QPixmap const & );
void wordReceived( QString const & ); void wordReceived( QString const & );
void headwordReceived( QString const &, QString const & );
void headwordFromFavorites( QString const &, QString const & ); void headwordFromFavorites( QString const &, QString const & );
void quitApp(); void quitApp();
@ -226,9 +225,8 @@ private:
/// group, or to all dictionaries if there are no groups. /// group, or to all dictionaries if there are no groups.
vector< sptr< Dictionary::Class > > const & getActiveDicts(); vector< sptr< Dictionary::Class > > const & getActiveDicts();
/// Brings the main window to front if it's not currently, or hides it /// @param ensureShow only ensure the window will be shown and no "toggling"
/// otherwise. The hiding part is omitted if onlyShow is true. void toggleMainWindow( bool ensureShow );
void toggleMainWindow( bool onlyShow = false );
/// Creates hotkeyWrapper and hooks the currently set keys for it /// Creates hotkeyWrapper and hooks the currently set keys for it
void installHotKeys(); void installHotKeys();
@ -398,8 +396,6 @@ private slots:
void setAutostart( bool ); void setAutostart( bool );
void showMainWindow();
void visitHomepage(); void visitHomepage();
void visitForum(); void visitForum();
void openConfigFolder(); void openConfigFolder();