Dict Engine: cache available dicts

This makes the config dialog more responsive when opening it multiple
times, and minimizes network traffic.

The equality check at the beginning of sourceRequestEvent was
completely wrong, if the source (DataContainer) was deleted-because-unused then
we need to proceed with the request even if it's the same as the
previous request. And if the request is ongoing then DataEngine
doesn't call us again, it connects to the existing source...
wilder-5.14
David Faure 8 years ago
parent a9b08ac604
commit 1abcb79b50
  1. 26
      dataengines/dict/dictengine.cpp
  2. 3
      dataengines/dict/dictengine.h

@ -152,6 +152,7 @@ void DictEngine::getDicts()
ret += m_tcpSocket->readAll();
}
QVariantMap *availableDicts = new QVariantMap;
const QList<QByteArray> retLines = ret.split('\n');
for (const QByteArray &curr : retLines) {
if (curr.startsWith("554")) {
@ -175,14 +176,15 @@ void DictEngine::getDicts()
description.chop(1);
}
setData(QStringLiteral("list-dictionaries"), id, description); // this is additive
availableDicts->insert(id, description);
}
}
m_availableDictsCache.insert(m_serverName, availableDicts);
m_tcpSocket->disconnectFromHost();
}
void DictEngine::socketClosed()
{
if (m_tcpSocket) {
@ -195,10 +197,6 @@ bool DictEngine::sourceRequestEvent(const QString &query)
{
// FIXME: this is COMPLETELY broken .. it can only look up one query at a time!
// a DataContainer subclass that does the look up should probably be made
if (m_currentQuery == query) {
return false;
}
if (m_tcpSocket) {
m_tcpSocket->abort(); //stop if lookup is in progress and new query is requested
m_tcpSocket->deleteLater();
@ -232,7 +230,23 @@ bool DictEngine::sourceRequestEvent(const QString &query)
if (m_currentWord.simplified().isEmpty()) {
setData(m_currentQuery, m_dictName, QString());
} else {
setData(m_currentQuery, m_dictName, QString());
if (m_currentWord == QLatin1String("list-dictionaries")) {
// Use cache if available
QVariantMap *dicts = m_availableDictsCache.object(m_serverName);
if (dicts) {
for (auto it = dicts->constBegin(); it != dicts->constEnd(); ++it) {
setData(m_currentQuery, it.key(), it.value());
}
return true;
}
}
// We need to do this in order to create the DataContainer immediately in DataEngine
// so it can connect to updates. Not sure why DataEnginePrivate::requestSource
// doesn't create the DataContainer when sourceRequestEvent returns true, by doing
// source(sourceName) instead of source(sourceName, false), but well, I'm too scared to change that.
setData(m_currentQuery, QVariant());
m_tcpSocket = new QTcpSocket(this);
connect(m_tcpSocket, &QTcpSocket::disconnected, this, &DictEngine::socketClosed);

@ -20,6 +20,8 @@
#define DICTENGINE_H
#include <Plasma/DataEngine>
#include <QMap>
#include <QCache>
#include <QVariantMap>
class QTcpSocket;
/**
@ -53,6 +55,7 @@ class DictEngine: public Plasma::DataEngine
QString m_currentQuery;
QString m_dictName;
QString m_serverName;
QCache<QString, QVariantMap> m_availableDictsCache;
};

Loading…
Cancel
Save