Plasma
dictengine.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "dictengine.h"
00020 #include <iostream>
00021
00022 #include <QtNetwork/QTcpSocket>
00023 #include <KDebug>
00024 #include <KLocale>
00025
00026 #include <plasma/datacontainer.h>
00027
00028 DictEngine::DictEngine(QObject* parent, const QVariantList& args)
00029 : Plasma::DataEngine(parent, args),
00030 dictHash(0),
00031 tcpSocket(0)
00032 {
00033 Q_UNUSED(args)
00034 serverName="dict.org";
00035 dictName="wn";
00036 }
00037
00038 DictEngine::~DictEngine()
00039 {
00040 }
00041
00042 void DictEngine::setDict(const QString &dict)
00043 {
00044 dictName=dict;
00045 }
00046
00047 void DictEngine::setServer(const QString &server)
00048 {
00049 serverName=server;
00050 }
00051
00052 void DictEngine::getDefinition()
00053 {
00054
00055
00056
00057
00058
00059
00060 tcpSocket->waitForReadyRead();
00061 tcpSocket->readAll();
00062 QByteArray ret;
00063
00064 tcpSocket->write(QByteArray("DEFINE "));
00065 tcpSocket->write(dictName.toAscii());
00066 tcpSocket->write(QByteArray(" \""));
00067 tcpSocket->write(currentWord.toAscii());
00068 tcpSocket->write(QByteArray("\"\n"));
00069 tcpSocket->flush();
00070
00071 while (!ret.contains("250") && !ret.contains("552"))
00072 {
00073 tcpSocket->waitForReadyRead();
00074 ret += tcpSocket->readAll();
00075 }
00076
00077 connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(socketClosed()));
00078 tcpSocket->disconnectFromHost();
00079 setData(currentWord, dictName, ret);
00080 }
00081
00082
00083 QString DictEngine::parseToHtml(QByteArray &text)
00084 {
00085 QList<QByteArray> retLines = text.split('\n');
00086 QString def;
00087 if(currentWord == QLatin1String("plasma"))
00088 {
00089 def += "<dl><!--PAGE START--><!--DEFINITION START--><dt><b>Plasma</b> \\Plas\"ma\\, a.(for awesome)</dt><!--PAGE START--><dd>OOH! I know that one! Plasma is that awesome new desktop thing for KDE4! Oh wait, you want an actual definition? Here, No fun...</dd></dl><br />";
00090 }
00091 def += "<dl>\n";
00092
00093 bool isFirst=true;
00094 QString wordRegex;
00095 for(int i=0;i<currentWord.size();i++)
00096 {
00097 wordRegex += ('['+QString(currentWord[i].toUpper())+QString(currentWord[i].toLower())+']');
00098 }
00099
00100 while (!retLines.empty())
00101 {
00102 QString currentLine = QString(retLines.takeFirst());
00103 if (currentLine.startsWith("552"))
00104 {
00105 def += "<dt>";
00106 def += i18n("<b>No match found for %1 in database %2.</b>", currentWord, dictName);
00107 def += "</dt>";
00108 break;
00109 }
00110 if (currentLine.startsWith("151"))
00111 {
00112 isFirst = true;
00113 continue;
00114 }
00115 if (currentLine.startsWith('.'))
00116 {
00117 def += "</dd><!--PERIOD-->";
00118 continue;
00119 }
00120 if (!(currentLine.startsWith("150") || currentLine.startsWith("151")
00121 || currentLine.startsWith("250") || currentLine.startsWith("552")))
00122 {
00123 currentLine = currentLine.trimmed();
00124 if (currentLine.startsWith("1."))
00125 def += "<br />";
00126 if (currentLine.contains(QRegExp("^([1-9]{1,2}\\.)")))
00127 def += "<br />";
00128 currentLine.replace(QRegExp("\\{([A-Za-z ]+)\\}"), "<a href=\"\\1\" style=\"color: #0000FF\" >\\1</a>");
00129 currentLine.replace(QRegExp("^([1-9]{1,2}\\.)"), "<!--PAGE START--><b>\\1</b>");
00130 currentLine.replace(QRegExp("((^| |\\.)"+wordRegex+"( |\\.|(i?e)?s|$))"), "<b>\\1</b>");
00131 currentLine.replace(QRegExp("(^| |\\.)(\\[[^]]+\\])( |\\.|$)"), "<i>\\2</i>");
00132
00133
00134
00135 if(isFirst)
00136 {
00137 def += "<!--PAGE START--><!--DEFINITION START--><dt>" + currentLine.toAscii() + "</dt>\n<dd>\n";
00138 isFirst = false;
00139 continue;
00140 }
00141
00142 if(currentLine == "." || currentLine.isEmpty())
00143 def += "\n<br />\n";
00144 else
00145 def += currentLine.toAscii() + '\n';
00146 }
00147
00148 }
00149 def+="</dl>";
00150 return def;
00151 }
00152
00153 void DictEngine::getDicts()
00154 {
00155 QMap<QString, QString> theHash;
00156 tcpSocket->waitForReadyRead();
00157 tcpSocket->readAll();
00158 QByteArray ret;
00159
00160 tcpSocket->write(QByteArray("SHOW DB\n"));;
00161 tcpSocket->flush();
00162
00163 tcpSocket->waitForReadyRead();
00164 while (!ret.contains("250"))
00165 {
00166 tcpSocket->waitForReadyRead();
00167 ret += tcpSocket->readAll();
00168 }
00169
00170 QList<QByteArray> retLines = ret.split('\n');
00171
00172 QString tmp1, tmp2;
00173
00174
00175 while (!retLines.empty())
00176 {
00177 QString curr = QString(retLines.takeFirst());
00178 if (curr.startsWith("554"))
00179 {
00180
00181
00182 break;
00183 }
00184 if (!curr.startsWith('-'))
00185 {
00186 curr = curr.trimmed();
00187 tmp1=curr.section(' ',0,1);
00188 tmp2=curr.section(' ',1);
00189
00190 kDebug() << tmp1 + " " + tmp2;
00191 setData("showDictionaries",tmp1,tmp2);
00192 }
00193 }
00194 connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(socketClosed()));
00195 tcpSocket->disconnectFromHost();
00196
00197 }
00198
00199
00200
00201 void DictEngine::socketClosed()
00202 {
00203 tcpSocket->deleteLater();
00204 tcpSocket = 0;
00205 }
00206
00207 bool DictEngine::sourceRequestEvent(const QString &word)
00208 {
00209 if (tcpSocket && currentWord != word)
00210 {
00211 tcpSocket->abort();
00212 tcpSocket->deleteLater();
00213 tcpSocket = 0;
00214 }
00215 currentWord = word;
00216 if (currentWord.simplified().count() != 0)
00217 {
00218 setData(currentWord, dictName, QString());
00219 tcpSocket = new QTcpSocket(this);
00220 tcpSocket->abort();
00221 if (currentWord == "showDictionaries")
00222 {
00223 connect(tcpSocket, SIGNAL(connected()), this, SLOT(getDicts()));
00224 } else {
00225 connect(tcpSocket, SIGNAL(connected()), this ,SLOT(getDefinition()));
00226 }
00227 tcpSocket->connectToHost(serverName, 2628);
00228 } else {
00229 setData(currentWord, dictName, QString());
00230 }
00231 return true;
00232 }
00233
00234 #include "dictengine.moc"