Compare commits

...

13 commits

Author SHA1 Message Date
xiaoyifang 9a41f99fab
Merge 3c5233f2a1 into 808d857602 2024-11-17 14:17:45 +08:00
xiaoyifang 808d857602
fix: [dictserver] some word does not got displayed (#1958)
Some checks are pending
SonarCloud / Build and analyze (push) Waiting to run
* fix: [dictserver] some word does not got displayed


---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2024-11-17 01:19:07 +00:00
xiaoyifang 3c5233f2a1 1 2024-11-11 09:19:29 +08:00
YiFang Xiao c5ca1b7d63 1 2024-11-11 09:16:14 +08:00
YiFang Xiao 5092ebe2ee 1 2024-11-11 09:16:14 +08:00
YiFang Xiao 5bef4cef22 1 2024-11-11 09:16:14 +08:00
xiaoyifang 59d01868da 1: do not update name 2024-11-11 09:16:14 +08:00
xiaoyifang 10b0496cce 1: do not update name 2024-11-11 09:16:14 +08:00
xiaoyifang 1cf495e7dd Revert "opt: update name should reflect the latest info"
This reverts commit 1272ea2d20a7908b9a69d6b551511916f97cc024.
2024-11-11 09:16:14 +08:00
xiaoyifang c2fc90801b opt: update name should reflect the latest info 2024-11-11 09:16:14 +08:00
autofix-ci[bot] 2de2141758 [autofix.ci] apply automated fixes 2024-11-11 09:16:14 +08:00
xiaoyifang db4c352d6c opt: erase the need to removeTabs 2024-11-11 09:16:14 +08:00
xiaoyifang 7c32cad65a opt: erase the need to removeTabs 2024-11-11 09:16:14 +08:00
7 changed files with 91 additions and 90 deletions

View file

@ -486,7 +486,7 @@ void DictServerWordSearchRequest::run()
qDebug() << "receive match:" << reply;
auto code = reply.left( 3 );
if ( reply.left( 3 ) != "152" ) {
if ( code != "152" ) {
matchNext();
}
@ -574,8 +574,11 @@ class DictServerArticleRequest: public Dictionary::DataRequest
DictServerDictionary & dict;
string articleData;
QString articleText;
int currentDatabase = 0;
DictServerState state;
QTimer * timer;
bool contentInHtml = false;
@ -587,14 +590,21 @@ public:
dict( dict_ ),
dictImpl( new DictServerImpl( this, dict_.url, "GoldenDict-t" ) )
{
timer = new QTimer( this );
timer->setInterval( 5000 );
timer->setSingleShot( true );
qDebug() << "receive data:" << QDateTime::currentDateTime();
connect( timer, &QTimer::timeout, this, [ this ]() {
qDebug() << "Server takes too much time to response" << QDateTime::currentDateTime();
cancel();
} );
connect( this, &DictServerArticleRequest::finishedArticle, this, [ this ]( QString articleText ) {
if ( Utils::AtomicInt::loadAcquire( isCancelled ) ) {
cancel();
return;
}
qDebug() << articleText;
static QRegularExpression phonetic( R"(\\([^\\]+)\\)",
QRegularExpression::CaseInsensitiveOption ); // phonetics: \stuff\ ...
static QRegularExpression divs_inside_phonetic( "</div([^>]*)><div([^>]*)>",
@ -687,10 +697,7 @@ public:
defineNext();
} );
QTimer::singleShot( 5000, this, [ this ]() {
qDebug() << "Server takes too much time to response";
cancel();
} );
timer->start();
}
void run();
@ -723,9 +730,9 @@ void DictServerArticleRequest::run()
return;
}
connect( &dictImpl->socket, &QTcpSocket::readyRead, this, [ this ]() {
QMutexLocker const _( &dictImpl->mutex );
timer->start();
if ( state == DictServerState::DEFINE ) {
QByteArray reply = dictImpl->socket.readLine();
qDebug() << "receive define:" << reply;
@ -748,34 +755,19 @@ void DictServerArticleRequest::run()
if ( reply.left( 3 ) == "150" ) {
// Articles found
int countPos = reply.indexOf( ' ', 4 );
QString articleText;
// Get articles count
// Get articles count,
// todo ,how to use this count?
int count = reply.mid( 4, countPos > 4 ? countPos - 4 : -1 ).toInt();
// Read articles
for ( int x = 0; x < count; x++ ) {
reply = dictImpl->socket.readLine();
if ( reply.isEmpty() ) {
state = DictServerState::DEFINE_DATA;
return;
}
readData( reply );
}
state = DictServerState::DEFINE_DATA;
}
}
else if ( state == DictServerState::DEFINE_DATA ) {
QByteArray reply = dictImpl->socket.readLine();
qDebug() << "receive define data:" << reply;
while ( true ) {
if ( reply.isEmpty() ) {
return;
}
qDebug() << "receive define data:" << reply << QDateTime::currentDateTime();
readData( reply );
reply = dictImpl->socket.readLine();
}
}
} );
@ -814,7 +806,8 @@ void DictServerArticleRequest::readData( QByteArray reply )
pos = endPos + 1;
QString dbID, dbName;
QString dbID;
QString dbName;
// Retrieve database ID
endPos = reply.indexOf( ' ', pos );
@ -828,7 +821,6 @@ void DictServerArticleRequest::readData( QByteArray reply )
// Retrieve database ID
pos = endPos + 1;
endPos = reply.indexOf( ' ', pos );
if ( reply[ pos ] == '\"' ) {
endPos = reply.indexOf( '\"', pos + 1 ) + 1;
}
@ -852,47 +844,30 @@ void DictServerArticleRequest::readData( QByteArray reply )
articleData += string( "<div class=\"dictserver_from\">" ) + dbName.toUtf8().data() + "[" + dbID.toUtf8().data()
+ "]" + "</div>";
// Retreive MIME headers if any
reply = dictImpl->socket.readAll();
static QRegularExpression contentTypeExpr( "Content-Type\\s*:\\s*text/html",
QRegularExpression::CaseInsensitiveOption );
for ( ;; ) {
reply = dictImpl->socket.readLine();
if ( reply.isEmpty() ) {
return;
}
if ( reply == "\r\n" ) {
break;
}
QRegularExpressionMatch match = contentTypeExpr.match( reply );
if ( match.hasMatch() ) {
contentInHtml = true;
}
}
QString articleText;
// Retrieve article text
articleText.clear();
for ( ;; ) {
reply = dictImpl->socket.readLine();
if ( reply.isEmpty() ) {
return;
}
qDebug() << "reply data:" << reply;
if ( reply == ".\r\n" ) {
articleText += reply;
qDebug() << "reply data:" << reply << QDateTime::currentDateTime();
if ( articleText.contains( "\r\n.\r\n" ) ) {
//discard all left message.
while ( !dictImpl->socket.readLine().isEmpty() ) {}
emit finishedArticle( articleText );
return;
}
}
else {
articleText += reply;
reply = dictImpl->socket.readAll();
qDebug() << "reply data:" << reply << QDateTime::currentDateTime();
articleText += reply;
if ( reply.contains( "\r\n.\r\n" ) ) {
//discard all left message. maybe delete all the remaining data after `.\r\n`
emit finishedArticle( articleText );
return;
}
}
//restart.
timer->start();
}
void DictServerArticleRequest::cancel()

View file

@ -224,7 +224,7 @@ void loadDictionaries( QWidget * parent,
loadDicts.wait();
if ( loadDicts.getExceptionText().size() ) {
if ( !loadDicts.getExceptionText().empty() ) {
QMessageBox::critical( parent,
QCoreApplication::translate( "LoadDictionaries", "Error loading dictionaries" ),
QString::fromUtf8( loadDicts.getExceptionText().c_str() ) );

View file

@ -204,26 +204,11 @@ void EditDictionaries::acceptChangedSources( bool rebuildGroups )
// Those hold pointers to dictionaries, we need to free them.
groupInstances.clear();
groups.clear();
orderAndProps.clear();
loadDictionaries( this, cfg, dictionaries, dictNetMgr );
Instances::updateNames( savedGroups, dictionaries );
Instances::updateNames( savedOrder, dictionaries );
Instances::updateNames( savedInactive, dictionaries );
if ( rebuildGroups ) {
ui.tabs->removeTab( 1 );
ui.tabs->removeTab( 1 );
orderAndProps = new OrderAndProps( this, savedOrder, savedInactive, dictionaries );
groups = new Groups( this, dictionaries, savedGroups, orderAndProps->getCurrentDictionaryOrder() );
ui.tabs->insertTab( 1, orderAndProps, QIcon( ":/icons/book.svg" ), tr( "&Dictionaries" ) );
ui.tabs->insertTab( 2, groups, QIcon( ":/icons/bookcase.svg" ), tr( "&Groups" ) );
connect( groups, &Groups::showDictionaryInfo, this, &EditDictionaries::showDictionaryInfo );
connect( orderAndProps, &OrderAndProps::showDictionaryHeadwords, this, &EditDictionaries::showDictionaryHeadwords );
orderAndProps->rebuild( savedOrder, savedInactive, dictionaries );
groups->rebuild( dictionaries, savedGroups, orderAndProps->getCurrentDictionaryOrder() );
}
setUpdatesEnabled( true );
}

View file

@ -64,6 +64,24 @@ Groups::Groups( QWidget * parent,
countChanged();
}
void Groups::rebuild( vector< sptr< Dictionary::Class > > const & dicts_,
Config::Groups const & groups_,
Config::Group const & order )
{
this->setUpdatesEnabled( false );
dicts = dicts_;
groups = groups_;
ui.dictionaries->setAsSource();
ui.dictionaries->populate( Instances::Group( order, dicts, Config::Group() ).dictionaries, dicts );
// Populate groups' widget
ui.groups->populate( groups, dicts, ui.dictionaries->getCurrentDictionaries() );
countChanged();
this->setUpdatesEnabled( true );
}
void Groups::editGroup( unsigned id )
{
for ( int x = 0; x < groups.size(); ++x ) {

View file

@ -18,7 +18,9 @@ public:
std::vector< sptr< Dictionary::Class > > const &,
Config::Groups const &,
Config::Group const & order );
void rebuild( std::vector< sptr< Dictionary::Class > > const & dicts_,
Config::Groups const & groups_,
Config::Group const & order );
/// Instructs the dialog to position itself on editing the given group.
void editGroup( unsigned id );
@ -31,7 +33,7 @@ public:
private:
Ui::Groups ui;
std::vector< sptr< Dictionary::Class > > const & dicts;
std::vector< sptr< Dictionary::Class > > dicts;
Config::Groups groups;
QToolButton * groupsListButton;

View file

@ -133,6 +133,25 @@ OrderAndProps::OrderAndProps( QWidget * parent,
showDictNumbers();
}
void OrderAndProps::rebuild( Config::Group const & dictionaryOrder,
Config::Group const & inactiveDictionaries,
std::vector< sptr< Dictionary::Class > > const & allDictionaries )
{
Instances::Group order( dictionaryOrder, allDictionaries, Config::Group() );
Instances::Group inactive( inactiveDictionaries, allDictionaries, Config::Group() );
Instances::complementDictionaryOrder( order, inactive, allDictionaries );
setUpdatesEnabled( false );
ui.dictionaryOrder->populate( order.dictionaries, allDictionaries );
ui.inactiveDictionaries->populate( inactive.dictionaries, allDictionaries );
disableDictionaryDescription();
showDictNumbers();
setUpdatesEnabled( true );
}
Config::Group OrderAndProps::getCurrentDictionaryOrder() const
{
Instances::Group g;

View file

@ -17,7 +17,9 @@ public:
Config::Group const & dictionaryOrder,
Config::Group const & inactiveDictionaries,
std::vector< sptr< Dictionary::Class > > const & allDictionaries );
void rebuild( Config::Group const & dictionaryOrder,
Config::Group const & inactiveDictionaries,
std::vector< sptr< Dictionary::Class > > const & allDictionaries );
Config::Group getCurrentDictionaryOrder() const;
Config::Group getCurrentInactiveDictionaries() const;