Verified Commit 04773128 authored by Fushan Wen's avatar Fushan Wen 💬
Browse files

dict: support multiple dicts in the engine

The dict protocol supports "Command Pipelining", so the engine can
concatenate multiple DEFINE commands into one command.

See also: https://datatracker.ietf.org/doc/html/rfc2229#section-4

CCBUG: 453878
parent ed4131f9
......@@ -19,7 +19,7 @@ using namespace std::chrono_literals;
DictEngine::DictEngine(QObject *parent)
: QObject(parent)
, m_dictName(QStringLiteral("wn")) // In case we need to switch it later
, m_dictNames{QByteArrayLiteral("wn")} // In case we need to switch it later
, m_serverName(QStringLiteral("dict.org")) // Default, good dictionary
, m_definitionResponses{
QByteArrayLiteral("250"), /**< ok (optional timing information here) */
......@@ -38,9 +38,9 @@ DictEngine::~DictEngine()
{
}
void DictEngine::setDict(const QString &dict)
void DictEngine::setDict(const QByteArray &dict)
{
m_dictName = dict;
m_dictNames = dict.split(',');
}
void DictEngine::setServer(const QString &server)
......@@ -123,8 +123,12 @@ void DictEngine::getDefinition()
m_tcpSocket->readAll();
const QByteArray command = QByteArray("DEFINE ") + m_dictName.toLatin1() + " \"" + m_currentWord.toUtf8() + "\"\n";
// qDebug() << command;
// Command Pipelining: https://datatracker.ietf.org/doc/html/rfc2229#section-4
QByteArray command;
for (const QByteArray &dictName : std::as_const(m_dictNames)) {
command += QByteArrayLiteral("DEFINE ") + dictName + QByteArrayLiteral(" \"") + m_currentWord.toUtf8() + QByteArrayLiteral("\"\n");
}
m_tcpSocket->write(command);
m_tcpSocket->flush();
......@@ -223,10 +227,10 @@ void DictEngine::requestDefinition(const QString &query)
// asked for a dictionary?
if (queryParts.count() > 1) {
setDict(queryParts[queryParts.count() - 2]);
setDict(queryParts[queryParts.count() - 2].toLatin1());
// default to wordnet
} else {
setDict(QStringLiteral("wn"));
setDict(QByteArrayLiteral("wn"));
}
// asked for a server?
......
......@@ -67,14 +67,14 @@ private Q_SLOTS:
private:
void getDefinition();
void getDicts();
void setDict(const QString &dict);
void setDict(const QByteArray &dict);
void setServer(const QString &server);
QHash<QString, QString> m_dictNameToDictCode;
QTcpSocket *m_tcpSocket = nullptr;
QString m_currentWord;
QString m_currentQuery;
QString m_dictName;
QByteArrayList m_dictNames;
QString m_serverName;
QMap<QString, QMap<QString, QString>> m_availableDictsCache;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment