mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-11-27 15:24:05 +00:00
Compare commits
5 commits
1c842a06c2
...
9f8b449c3e
Author | SHA1 | Date | |
---|---|---|---|
9f8b449c3e | |||
808d857602 | |||
15b918eb6a | |||
27cbb7351b | |||
c787a08d2f |
|
@ -150,6 +150,7 @@ Preferences::Preferences():
|
||||||
doubleClickTranslates( true ),
|
doubleClickTranslates( true ),
|
||||||
selectWordBySingleClick( false ),
|
selectWordBySingleClick( false ),
|
||||||
autoScrollToTargetArticle( true ),
|
autoScrollToTargetArticle( true ),
|
||||||
|
targetArticleAtFirst( false ),
|
||||||
escKeyHidesMainWindow( false ),
|
escKeyHidesMainWindow( false ),
|
||||||
alwaysOnTop( false ),
|
alwaysOnTop( false ),
|
||||||
searchInDock( false ),
|
searchInDock( false ),
|
||||||
|
@ -878,6 +879,11 @@ Class load()
|
||||||
( preferences.namedItem( "autoScrollToTargetArticle" ).toElement().text() == "1" );
|
( preferences.namedItem( "autoScrollToTargetArticle" ).toElement().text() == "1" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( !preferences.namedItem( "targetArticleAtFirst" ).isNull() ) {
|
||||||
|
c.preferences.targetArticleAtFirst =
|
||||||
|
( preferences.namedItem( "targetArticleAtFirst" ).toElement().text() == "1" );
|
||||||
|
}
|
||||||
|
|
||||||
if ( !preferences.namedItem( "escKeyHidesMainWindow" ).isNull() ) {
|
if ( !preferences.namedItem( "escKeyHidesMainWindow" ).isNull() ) {
|
||||||
c.preferences.escKeyHidesMainWindow =
|
c.preferences.escKeyHidesMainWindow =
|
||||||
( preferences.namedItem( "escKeyHidesMainWindow" ).toElement().text() == "1" );
|
( preferences.namedItem( "escKeyHidesMainWindow" ).toElement().text() == "1" );
|
||||||
|
@ -1815,6 +1821,10 @@ void save( Class const & c )
|
||||||
opt.appendChild( dd.createTextNode( c.preferences.autoScrollToTargetArticle ? "1" : "0" ) );
|
opt.appendChild( dd.createTextNode( c.preferences.autoScrollToTargetArticle ? "1" : "0" ) );
|
||||||
preferences.appendChild( opt );
|
preferences.appendChild( opt );
|
||||||
|
|
||||||
|
opt = dd.createElement( "targetArticleAtFirst" );
|
||||||
|
opt.appendChild( dd.createTextNode( c.preferences.targetArticleAtFirst ? "1" : "0" ) );
|
||||||
|
preferences.appendChild( opt );
|
||||||
|
|
||||||
opt = dd.createElement( "escKeyHidesMainWindow" );
|
opt = dd.createElement( "escKeyHidesMainWindow" );
|
||||||
opt.appendChild( dd.createTextNode( c.preferences.escKeyHidesMainWindow ? "1" : "0" ) );
|
opt.appendChild( dd.createTextNode( c.preferences.escKeyHidesMainWindow ? "1" : "0" ) );
|
||||||
preferences.appendChild( opt );
|
preferences.appendChild( opt );
|
||||||
|
|
|
@ -297,6 +297,7 @@ struct Preferences
|
||||||
bool doubleClickTranslates;
|
bool doubleClickTranslates;
|
||||||
bool selectWordBySingleClick;
|
bool selectWordBySingleClick;
|
||||||
bool autoScrollToTargetArticle;
|
bool autoScrollToTargetArticle;
|
||||||
|
bool targetArticleAtFirst;
|
||||||
bool escKeyHidesMainWindow;
|
bool escKeyHidesMainWindow;
|
||||||
bool alwaysOnTop;
|
bool alwaysOnTop;
|
||||||
|
|
||||||
|
|
|
@ -486,7 +486,7 @@ void DictServerWordSearchRequest::run()
|
||||||
qDebug() << "receive match:" << reply;
|
qDebug() << "receive match:" << reply;
|
||||||
auto code = reply.left( 3 );
|
auto code = reply.left( 3 );
|
||||||
|
|
||||||
if ( reply.left( 3 ) != "152" ) {
|
if ( code != "152" ) {
|
||||||
|
|
||||||
matchNext();
|
matchNext();
|
||||||
}
|
}
|
||||||
|
@ -574,8 +574,11 @@ class DictServerArticleRequest: public Dictionary::DataRequest
|
||||||
DictServerDictionary & dict;
|
DictServerDictionary & dict;
|
||||||
string articleData;
|
string articleData;
|
||||||
|
|
||||||
|
QString articleText;
|
||||||
|
|
||||||
int currentDatabase = 0;
|
int currentDatabase = 0;
|
||||||
DictServerState state;
|
DictServerState state;
|
||||||
|
QTimer * timer;
|
||||||
bool contentInHtml = false;
|
bool contentInHtml = false;
|
||||||
|
|
||||||
|
|
||||||
|
@ -587,14 +590,21 @@ public:
|
||||||
dict( dict_ ),
|
dict( dict_ ),
|
||||||
dictImpl( new DictServerImpl( this, dict_.url, "GoldenDict-t" ) )
|
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 ) {
|
connect( this, &DictServerArticleRequest::finishedArticle, this, [ this ]( QString articleText ) {
|
||||||
if ( Utils::AtomicInt::loadAcquire( isCancelled ) ) {
|
if ( Utils::AtomicInt::loadAcquire( isCancelled ) ) {
|
||||||
cancel();
|
cancel();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
qDebug() << articleText;
|
|
||||||
|
|
||||||
static QRegularExpression phonetic( R"(\\([^\\]+)\\)",
|
static QRegularExpression phonetic( R"(\\([^\\]+)\\)",
|
||||||
QRegularExpression::CaseInsensitiveOption ); // phonetics: \stuff\ ...
|
QRegularExpression::CaseInsensitiveOption ); // phonetics: \stuff\ ...
|
||||||
static QRegularExpression divs_inside_phonetic( "</div([^>]*)><div([^>]*)>",
|
static QRegularExpression divs_inside_phonetic( "</div([^>]*)><div([^>]*)>",
|
||||||
|
@ -687,10 +697,7 @@ public:
|
||||||
defineNext();
|
defineNext();
|
||||||
} );
|
} );
|
||||||
|
|
||||||
QTimer::singleShot( 5000, this, [ this ]() {
|
timer->start();
|
||||||
qDebug() << "Server takes too much time to response";
|
|
||||||
cancel();
|
|
||||||
} );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void run();
|
void run();
|
||||||
|
@ -723,9 +730,9 @@ void DictServerArticleRequest::run()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
connect( &dictImpl->socket, &QTcpSocket::readyRead, this, [ this ]() {
|
connect( &dictImpl->socket, &QTcpSocket::readyRead, this, [ this ]() {
|
||||||
QMutexLocker const _( &dictImpl->mutex );
|
QMutexLocker const _( &dictImpl->mutex );
|
||||||
|
timer->start();
|
||||||
if ( state == DictServerState::DEFINE ) {
|
if ( state == DictServerState::DEFINE ) {
|
||||||
QByteArray reply = dictImpl->socket.readLine();
|
QByteArray reply = dictImpl->socket.readLine();
|
||||||
qDebug() << "receive define:" << reply;
|
qDebug() << "receive define:" << reply;
|
||||||
|
@ -748,34 +755,19 @@ void DictServerArticleRequest::run()
|
||||||
if ( reply.left( 3 ) == "150" ) {
|
if ( reply.left( 3 ) == "150" ) {
|
||||||
// Articles found
|
// Articles found
|
||||||
int countPos = reply.indexOf( ' ', 4 );
|
int countPos = reply.indexOf( ' ', 4 );
|
||||||
|
// Get articles count,
|
||||||
QString articleText;
|
// todo ,how to use this count?
|
||||||
|
|
||||||
// Get articles count
|
|
||||||
int count = reply.mid( 4, countPos > 4 ? countPos - 4 : -1 ).toInt();
|
int count = reply.mid( 4, countPos > 4 ? countPos - 4 : -1 ).toInt();
|
||||||
|
|
||||||
// Read articles
|
// Read articles
|
||||||
for ( int x = 0; x < count; x++ ) {
|
|
||||||
reply = dictImpl->socket.readLine();
|
|
||||||
if ( reply.isEmpty() ) {
|
|
||||||
state = DictServerState::DEFINE_DATA;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
readData( reply );
|
readData( reply );
|
||||||
}
|
|
||||||
state = DictServerState::DEFINE_DATA;
|
state = DictServerState::DEFINE_DATA;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if ( state == DictServerState::DEFINE_DATA ) {
|
else if ( state == DictServerState::DEFINE_DATA ) {
|
||||||
QByteArray reply = dictImpl->socket.readLine();
|
QByteArray reply = dictImpl->socket.readLine();
|
||||||
qDebug() << "receive define data:" << reply;
|
qDebug() << "receive define data:" << reply << QDateTime::currentDateTime();
|
||||||
while ( true ) {
|
|
||||||
if ( reply.isEmpty() ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
readData( reply );
|
readData( reply );
|
||||||
reply = dictImpl->socket.readLine();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
@ -814,7 +806,8 @@ void DictServerArticleRequest::readData( QByteArray reply )
|
||||||
|
|
||||||
pos = endPos + 1;
|
pos = endPos + 1;
|
||||||
|
|
||||||
QString dbID, dbName;
|
QString dbID;
|
||||||
|
QString dbName;
|
||||||
|
|
||||||
// Retrieve database ID
|
// Retrieve database ID
|
||||||
endPos = reply.indexOf( ' ', pos );
|
endPos = reply.indexOf( ' ', pos );
|
||||||
|
@ -828,7 +821,6 @@ void DictServerArticleRequest::readData( QByteArray reply )
|
||||||
|
|
||||||
// Retrieve database ID
|
// Retrieve database ID
|
||||||
pos = endPos + 1;
|
pos = endPos + 1;
|
||||||
endPos = reply.indexOf( ' ', pos );
|
|
||||||
if ( reply[ pos ] == '\"' ) {
|
if ( reply[ pos ] == '\"' ) {
|
||||||
endPos = reply.indexOf( '\"', pos + 1 ) + 1;
|
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()
|
articleData += string( "<div class=\"dictserver_from\">" ) + dbName.toUtf8().data() + "[" + dbID.toUtf8().data()
|
||||||
+ "]" + "</div>";
|
+ "]" + "</div>";
|
||||||
|
|
||||||
// Retreive MIME headers if any
|
reply = dictImpl->socket.readAll();
|
||||||
|
|
||||||
static QRegularExpression contentTypeExpr( "Content-Type\\s*:\\s*text/html",
|
articleText += reply;
|
||||||
QRegularExpression::CaseInsensitiveOption );
|
qDebug() << "reply data:" << reply << QDateTime::currentDateTime();
|
||||||
|
if ( articleText.contains( "\r\n.\r\n" ) ) {
|
||||||
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.
|
//discard all left message.
|
||||||
while ( !dictImpl->socket.readLine().isEmpty() ) {}
|
|
||||||
emit finishedArticle( articleText );
|
emit finishedArticle( articleText );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
articleText += reply;
|
||||||
|
reply = dictImpl->socket.readAll();
|
||||||
|
qDebug() << "reply data:" << reply << QDateTime::currentDateTime();
|
||||||
|
|
||||||
articleText += reply;
|
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()
|
void DictServerArticleRequest::cancel()
|
||||||
|
|
|
@ -185,6 +185,7 @@ Preferences::Preferences( QWidget * parent, Config::Class & cfg_ ):
|
||||||
ui.doubleClickTranslates->setChecked( p.doubleClickTranslates );
|
ui.doubleClickTranslates->setChecked( p.doubleClickTranslates );
|
||||||
ui.selectBySingleClick->setChecked( p.selectWordBySingleClick );
|
ui.selectBySingleClick->setChecked( p.selectWordBySingleClick );
|
||||||
ui.autoScrollToTargetArticle->setChecked( p.autoScrollToTargetArticle );
|
ui.autoScrollToTargetArticle->setChecked( p.autoScrollToTargetArticle );
|
||||||
|
ui.targetArticleAtFirst->setChecked( p.targetArticleAtFirst );
|
||||||
ui.escKeyHidesMainWindow->setChecked( p.escKeyHidesMainWindow );
|
ui.escKeyHidesMainWindow->setChecked( p.escKeyHidesMainWindow );
|
||||||
|
|
||||||
ui.darkMode->addItem( tr( "On" ), QVariant::fromValue( Config::Dark::On ) );
|
ui.darkMode->addItem( tr( "On" ), QVariant::fromValue( Config::Dark::On ) );
|
||||||
|
@ -441,6 +442,7 @@ Config::Preferences Preferences::getPreferences()
|
||||||
p.doubleClickTranslates = ui.doubleClickTranslates->isChecked();
|
p.doubleClickTranslates = ui.doubleClickTranslates->isChecked();
|
||||||
p.selectWordBySingleClick = ui.selectBySingleClick->isChecked();
|
p.selectWordBySingleClick = ui.selectBySingleClick->isChecked();
|
||||||
p.autoScrollToTargetArticle = ui.autoScrollToTargetArticle->isChecked();
|
p.autoScrollToTargetArticle = ui.autoScrollToTargetArticle->isChecked();
|
||||||
|
p.targetArticleAtFirst = ui.targetArticleAtFirst->isChecked();
|
||||||
p.escKeyHidesMainWindow = ui.escKeyHidesMainWindow->isChecked();
|
p.escKeyHidesMainWindow = ui.escKeyHidesMainWindow->isChecked();
|
||||||
|
|
||||||
p.darkMode = ui.darkMode->currentData().value< Config::Dark >();
|
p.darkMode = ui.darkMode->currentData().value< Config::Dark >();
|
||||||
|
|
|
@ -169,6 +169,16 @@ however, the article from the topmost dictionary is shown.</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
<item row="4" column="1">
|
||||||
|
<widget class="QCheckBox" name="targetArticleAtFirst">
|
||||||
|
<property name="text">
|
||||||
|
<string>Place the target article at the first place.</string>
|
||||||
|
</property>
|
||||||
|
<property name="checked">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
<item row="1" column="0">
|
<item row="1" column="0">
|
||||||
<widget class="QGroupBox" name="enableTrayIcon">
|
<widget class="QGroupBox" name="enableTrayIcon">
|
||||||
<property name="toolTip">
|
<property name="toolTip">
|
||||||
|
|
Loading…
Reference in a new issue