2 * Copyright (C) 2007 Thomas Georgiou <TAGeorgiou@gmail.com> and Jeff Cooper <weirdsox11@gmail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License version 2 as
6 * published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details
13 * You should have received a copy of the GNU Library General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "dictengine.h"
22 #include <QtNetwork/QTcpSocket>
26 #include <Plasma/DataContainer>
28 DictEngine::DictEngine(QObject
* parent
, const QVariantList
& args
)
29 : Plasma::DataEngine(parent
, args
)
33 serverName
="dict.org"; //In case we need to switch it later
34 dictName
="wn"; //Default, good dictionary
37 DictEngine::~DictEngine()
41 void DictEngine::setDict(const QString
&dict
)
46 void DictEngine::setServer(const QString
&server
)
51 static QString
wnToHtml(const QString
&word
, QByteArray
&text
)
53 QList
<QByteArray
> splitText
= text
.split('\n');
56 QRegExp
linkRx("\\{(.*)\\}");
57 linkRx
.setMinimal(true);
60 while (!splitText
.empty()) {
61 //150 n definitions retrieved - definitions follow
62 //151 word database name - text follows
63 //250 ok (optional timing information here)
65 QString currentLine
= splitText
.takeFirst();
66 if (currentLine
.startsWith("151")) {
71 if (currentLine
.startsWith('.')) {
76 if (!(currentLine
.startsWith("150") || currentLine
.startsWith("151")
77 || currentLine
.startsWith("250") || currentLine
.startsWith("552"))) {
78 currentLine
.replace(linkRx
,"<a href=\"\\1\">\\1</a>");
81 def
+= "<dt><b>" + currentLine
+ "</b></dt>\n<dd>";
85 if (currentLine
.contains(QRegExp("([1-9]{1,2}:)"))) {
88 currentLine
.replace(QRegExp("^([\\s\\S]*[1-9]{1,2}:)"), "<b>\\1</b>");
100 void DictEngine::getDefinition()
102 tcpSocket
->waitForReadyRead();
103 tcpSocket
->readAll();
106 tcpSocket
->write(QByteArray("DEFINE "));
107 tcpSocket
->write(dictName
.toAscii());
108 tcpSocket
->write(QByteArray(" \""));
109 tcpSocket
->write(currentWord
.toAscii());
110 tcpSocket
->write(QByteArray("\"\n"));
113 while (!ret
.contains("250") && !ret
.contains("552")) {
114 tcpSocket
->waitForReadyRead();
115 ret
+= tcpSocket
->readAll();
118 connect(tcpSocket
, SIGNAL(disconnected()), this, SLOT(socketClosed()));
119 tcpSocket
->disconnectFromHost();
120 // setData(currentWord, dictName, ret);
122 setData(currentWord
, "text", wnToHtml(currentWord
,ret
));
128 void DictEngine::getDicts()
130 QMap
<QString
, QString
> theHash
;
131 tcpSocket
->waitForReadyRead();
132 tcpSocket
->readAll();
135 tcpSocket
->write(QByteArray("SHOW DB\n"));;
138 tcpSocket
->waitForReadyRead();
139 while (!ret
.contains("250")) {
140 tcpSocket
->waitForReadyRead();
141 ret
+= tcpSocket
->readAll();
144 QList
<QByteArray
> retLines
= ret
.split('\n');
147 while (!retLines
.empty()) {
148 QString curr
= QString(retLines
.takeFirst());
150 if (curr
.startsWith("554")) {
151 //TODO: What happens if no DB available?
152 //TODO: Eventually there will be functionality to change the server...
156 if (!curr
.startsWith('-')) {
157 curr
= curr
.trimmed();
158 tmp1
= curr
.section(' ', 0, 1);
159 tmp2
= curr
.section(' ', 1);
160 // theHash.insert(tmp1, tmp2);
161 //kDebug() << tmp1 + " " + tmp2;
162 setData("list-dictionaries", tmp1
, tmp2
);
166 tcpSocket
->disconnectFromHost();
167 // setData("list-dictionaries", "dictionaries", QByteArray(theHash);
172 void DictEngine::socketClosed()
174 tcpSocket
->deleteLater();
178 bool DictEngine::sourceRequestEvent(const QString
&word
)
180 if (tcpSocket
&& currentWord
!= word
)
182 tcpSocket
->abort(); //stop if lookup is in progress and new word is requested
183 tcpSocket
->deleteLater();
187 if (currentWord
.simplified().count() != 0)
189 setData(currentWord
, dictName
, QString());
190 tcpSocket
= new QTcpSocket(this);
192 connect(tcpSocket
, SIGNAL(disconnected()), this, SLOT(socketClosed()));
194 if (currentWord
== "list-dictionaries") {
195 connect(tcpSocket
, SIGNAL(connected()), this, SLOT(getDicts()));
197 connect(tcpSocket
, SIGNAL(connected()), this ,SLOT(getDefinition()));
199 tcpSocket
->connectToHost(serverName
, 2628);
201 setData(currentWord
, dictName
, QString());
206 #include "dictengine.moc"