Compare commits

..

1 commit

Author SHA1 Message Date
xiaoyifang 1c842a06c2
Merge 15b918eb6a into bd13598cc2 2024-11-16 08:48:35 +02:00

View file

@ -486,7 +486,7 @@ void DictServerWordSearchRequest::run()
qDebug() << "receive match:" << reply;
auto code = reply.left( 3 );
if ( code != "152" ) {
if ( reply.left( 3 ) != "152" ) {
matchNext();
}
@ -574,11 +574,8 @@ class DictServerArticleRequest: public Dictionary::DataRequest
DictServerDictionary & dict;
string articleData;
QString articleText;
int currentDatabase = 0;
DictServerState state;
QTimer * timer;
bool contentInHtml = false;
@ -590,21 +587,14 @@ 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([^>]*)>",
@ -697,7 +687,10 @@ public:
defineNext();
} );
timer->start();
QTimer::singleShot( 5000, this, [ this ]() {
qDebug() << "Server takes too much time to response";
cancel();
} );
}
void run();
@ -730,9 +723,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;
@ -755,19 +748,34 @@ void DictServerArticleRequest::run()
if ( reply.left( 3 ) == "150" ) {
// Articles found
int countPos = reply.indexOf( ' ', 4 );
// Get articles count,
// todo ,how to use this count?
QString articleText;
// Get articles count
int count = reply.mid( 4, countPos > 4 ? countPos - 4 : -1 ).toInt();
// Read articles
readData( reply );
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 << QDateTime::currentDateTime();
readData( reply );
qDebug() << "receive define data:" << reply;
while ( true ) {
if ( reply.isEmpty() ) {
return;
}
readData( reply );
reply = dictImpl->socket.readLine();
}
}
} );
@ -806,8 +814,7 @@ void DictServerArticleRequest::readData( QByteArray reply )
pos = endPos + 1;
QString dbID;
QString dbName;
QString dbID, dbName;
// Retrieve database ID
endPos = reply.indexOf( ' ', pos );
@ -820,7 +827,8 @@ void DictServerArticleRequest::readData( QByteArray reply )
dbID = reply.mid( pos, endPos - pos );
// Retrieve database ID
pos = endPos + 1;
pos = endPos + 1;
endPos = reply.indexOf( ' ', pos );
if ( reply[ pos ] == '\"' ) {
endPos = reply.indexOf( '\"', pos + 1 ) + 1;
}
@ -844,30 +852,47 @@ void DictServerArticleRequest::readData( QByteArray reply )
articleData += string( "<div class=\"dictserver_from\">" ) + dbName.toUtf8().data() + "[" + dbID.toUtf8().data()
+ "]" + "</div>";
reply = dictImpl->socket.readAll();
// Retreive MIME headers if any
articleText += reply;
qDebug() << "reply data:" << reply << QDateTime::currentDateTime();
if ( articleText.contains( "\r\n.\r\n" ) ) {
//discard all left message.
emit finishedArticle( articleText );
return;
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" ) {
//discard all left message.
while ( !dictImpl->socket.readLine().isEmpty() ) {}
emit finishedArticle( articleText );
return;
}
articleText += reply;
}
}
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()