• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Engines

dictengine.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright (C) 2007 Thomas Georgiou <TAGeorgiou@gmail.com> and Jeff Cooper <weirdsox11@gmail.com>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library General Public License version 2 as
00006  *   published by the Free Software Foundation
00007  *
00008  *   This program is distributed in the hope that it will be useful,
00009  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *   GNU General Public License for more details
00012  *
00013  *   You should have received a copy of the GNU Library General Public
00014  *   License along with this program; if not, write to the
00015  *   Free Software Foundation, Inc.,
00016  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
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>
00027 
00028 DictEngine::DictEngine(QObject* parent, const QVariantList& args)
00029     : Plasma::DataEngine(parent, args)
00030     , tcpSocket(0)
00031 {
00032     Q_UNUSED(args)
00033     serverName="dict.org"; //In case we need to switch it later
00034     dictName="wn"; //Default, good dictionary
00035 }
00036 
00037 DictEngine::~DictEngine()
00038 {
00039 }
00040 
00041 void DictEngine::setDict(const QString &dict)
00042 {
00043     dictName = dict;
00044 }
00045 
00046 void DictEngine::setServer(const QString &server)
00047 {
00048     serverName = server;
00049 }
00050 
00051 static QString wnToHtml(const QString &word, QByteArray &text)
00052 {
00053     QList<QByteArray> splitText = text.split('\n');
00054     QString def;
00055     def += "<dl>\n";
00056     QRegExp linkRx("\\{(.*)\\}");
00057     linkRx.setMinimal(true);
00058 
00059     bool isFirst=true;
00060     while (!splitText.empty()) {
00061         //150 n definitions retrieved - definitions follow
00062         //151 word database name - text follows
00063         //250 ok (optional timing information here)
00064         //552 No match
00065         QString currentLine = splitText.takeFirst();
00066         if (currentLine.startsWith("151")) {
00067             isFirst = true;
00068             continue;
00069         }
00070 
00071         if (currentLine.startsWith('.')) {
00072             def += "</dd>";
00073             continue;
00074         }
00075 
00076         if (!(currentLine.startsWith("150") || currentLine.startsWith("151")
00077            || currentLine.startsWith("250") || currentLine.startsWith("552"))) {
00078             currentLine.replace(linkRx,"<a href=\"\\1\">\\1</a>");
00079 
00080             if (isFirst) {
00081                 def += "<dt><b>" + currentLine + "</b></dt>\n<dd>";
00082                 isFirst = false;
00083                 continue;
00084             } else {
00085                 if (currentLine.contains(QRegExp("([1-9]{1,2}:)"))) {
00086                     def += "\n<br>\n";
00087                 }
00088                 currentLine.replace(QRegExp("^([\\s\\S]*[1-9]{1,2}:)"), "<b>\\1</b>");
00089                 def += currentLine;
00090                 continue;
00091             }
00092         }
00093 
00094     }
00095 
00096     def += "</dl>";
00097     return def;
00098 }
00099 
00100 void DictEngine::getDefinition()
00101 {
00102     tcpSocket->waitForReadyRead();
00103     tcpSocket->readAll();
00104     QByteArray ret;
00105 
00106     tcpSocket->write(QByteArray("DEFINE "));
00107     tcpSocket->write(dictName.toAscii());
00108     tcpSocket->write(QByteArray(" \""));
00109     tcpSocket->write(currentWord.toAscii());
00110     tcpSocket->write(QByteArray("\"\n"));
00111     tcpSocket->flush();
00112 
00113     while (!ret.contains("250") && !ret.contains("552")) {
00114         tcpSocket->waitForReadyRead();
00115         ret += tcpSocket->readAll();
00116     }
00117 
00118     connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(socketClosed()));
00119     tcpSocket->disconnectFromHost();
00120     //       setData(currentWord, dictName, ret);
00121     //       qWarning()<<ret;
00122     setData(currentWord, "text", wnToHtml(currentWord,ret));
00123 }
00124 
00125 
00126 
00127 
00128 void DictEngine::getDicts()
00129 {
00130     QMap<QString, QString> theHash;
00131     tcpSocket->waitForReadyRead();
00132     tcpSocket->readAll();
00133     QByteArray ret;
00134 
00135     tcpSocket->write(QByteArray("SHOW DB\n"));;
00136     tcpSocket->flush();
00137 
00138     tcpSocket->waitForReadyRead();
00139     while (!ret.contains("250")) {
00140         tcpSocket->waitForReadyRead();
00141         ret += tcpSocket->readAll();
00142     }
00143 
00144     QList<QByteArray> retLines = ret.split('\n');
00145     QString tmp1, tmp2;
00146 
00147     while (!retLines.empty()) {
00148         QString curr = QString(retLines.takeFirst());
00149 
00150         if (curr.startsWith("554")) {
00151             //TODO: What happens if no DB available?
00152             //TODO: Eventually there will be functionality to change the server...
00153             break;
00154         }
00155 
00156         if (!curr.startsWith('-')) {
00157             curr = curr.trimmed();
00158             tmp1 = curr.section(' ', 0, 1);
00159             tmp2 = curr.section(' ', 1);
00160   //          theHash.insert(tmp1, tmp2);
00161             //kDebug() << tmp1 + "  " + tmp2;
00162             setData("list-dictionaries", tmp1, tmp2);
00163         }
00164     }
00165 
00166     tcpSocket->disconnectFromHost();
00167 //    setData("list-dictionaries", "dictionaries", QByteArray(theHash);
00168 }
00169 
00170 
00171 
00172 void DictEngine::socketClosed()
00173 {
00174     tcpSocket->deleteLater();
00175     tcpSocket = 0;
00176 }
00177 
00178 bool DictEngine::sourceRequestEvent(const QString &word)
00179 {
00180       if (tcpSocket && currentWord != word)
00181       {
00182           tcpSocket->abort(); //stop if lookup is in progress and new word is requested
00183           tcpSocket->deleteLater();
00184           tcpSocket = 0;
00185       }
00186       currentWord = word;
00187       if (currentWord.simplified().count() != 0)
00188       {
00189           setData(currentWord, dictName, QString());
00190           tcpSocket = new QTcpSocket(this);
00191           tcpSocket->abort();
00192           connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(socketClosed()));
00193 
00194           if (currentWord == "list-dictionaries") {
00195               connect(tcpSocket, SIGNAL(connected()), this, SLOT(getDicts()));
00196           } else {
00197               connect(tcpSocket, SIGNAL(connected()), this ,SLOT(getDefinition()));
00198           }
00199           tcpSocket->connectToHost(serverName, 2628);
00200       } else {
00201           setData(currentWord, dictName, QString());
00202       }
00203       return true;
00204 }
00205 
00206 #include "dictengine.moc"

Engines

Skip menu "Engines"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal