add more spacing
[personal-kdebase.git] / workspace / plasma / dataengines / dict / dictengine.cpp
blob3cac83b0753c4fe383c675f2b525e53609882371
1 /*
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"
20 #include <iostream>
22 #include <QtNetwork/QTcpSocket>
23 #include <KDebug>
24 #include <KLocale>
26 #include <Plasma/DataContainer>
28 DictEngine::DictEngine(QObject* parent, const QVariantList& args)
29 : Plasma::DataEngine(parent, args)
30 , tcpSocket(0)
32 Q_UNUSED(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)
43 dictName = dict;
46 void DictEngine::setServer(const QString &server)
48 serverName = server;
51 static QString wnToHtml(const QString &word, QByteArray &text)
53 QList<QByteArray> splitText = text.split('\n');
54 QString def;
55 def += "<dl>\n";
56 QRegExp linkRx("\\{(.*)\\}");
57 linkRx.setMinimal(true);
59 bool isFirst=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)
64 //552 No match
65 QString currentLine = splitText.takeFirst();
66 if (currentLine.startsWith("151")) {
67 isFirst = true;
68 continue;
71 if (currentLine.startsWith('.')) {
72 def += "</dd>";
73 continue;
76 if (!(currentLine.startsWith("150") || currentLine.startsWith("151")
77 || currentLine.startsWith("250") || currentLine.startsWith("552"))) {
78 currentLine.replace(linkRx,"<a href=\"\\1\">\\1</a>");
80 if (isFirst) {
81 def += "<dt><b>" + currentLine + "</b></dt>\n<dd>";
82 isFirst = false;
83 continue;
84 } else {
85 if (currentLine.contains(QRegExp("([1-9]{1,2}:)"))) {
86 def += "\n<br>\n";
88 currentLine.replace(QRegExp("^([\\s\\S]*[1-9]{1,2}:)"), "<b>\\1</b>");
89 def += currentLine;
90 continue;
96 def += "</dl>";
97 return def;
100 void DictEngine::getDefinition()
102 tcpSocket->waitForReadyRead();
103 tcpSocket->readAll();
104 QByteArray ret;
106 tcpSocket->write(QByteArray("DEFINE "));
107 tcpSocket->write(dictName.toAscii());
108 tcpSocket->write(QByteArray(" \""));
109 tcpSocket->write(currentWord.toAscii());
110 tcpSocket->write(QByteArray("\"\n"));
111 tcpSocket->flush();
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);
121 // qWarning()<<ret;
122 setData(currentWord, "text", wnToHtml(currentWord,ret));
128 void DictEngine::getDicts()
130 QMap<QString, QString> theHash;
131 tcpSocket->waitForReadyRead();
132 tcpSocket->readAll();
133 QByteArray ret;
135 tcpSocket->write(QByteArray("SHOW DB\n"));;
136 tcpSocket->flush();
138 tcpSocket->waitForReadyRead();
139 while (!ret.contains("250")) {
140 tcpSocket->waitForReadyRead();
141 ret += tcpSocket->readAll();
144 QList<QByteArray> retLines = ret.split('\n');
145 QString tmp1, tmp2;
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...
153 break;
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();
175 tcpSocket = 0;
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();
184 tcpSocket = 0;
186 currentWord = word;
187 if (currentWord.simplified().count() != 0)
189 setData(currentWord, dictName, QString());
190 tcpSocket = new QTcpSocket(this);
191 tcpSocket->abort();
192 connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(socketClosed()));
194 if (currentWord == "list-dictionaries") {
195 connect(tcpSocket, SIGNAL(connected()), this, SLOT(getDicts()));
196 } else {
197 connect(tcpSocket, SIGNAL(connected()), this ,SLOT(getDefinition()));
199 tcpSocket->connectToHost(serverName, 2628);
200 } else {
201 setData(currentWord, dictName, QString());
203 return true;
206 #include "dictengine.moc"