• Skip to content
  • Skip to link menu
KDE 4.1 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.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"; //In case we need to switch it later
00035     dictName="wn"; //Default, good dictionary
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 /*      if(currentWord == QLatin1String("about"))
00055       {
00056           setData(currentWord, "gcide", "<!--PAGE START--><!--DEFINITION START--><dl><dt><b>Developers</b></dt><!--PAGE START--><dd>KDE4 Dictionary Applet for Plasma was written by <i>Thomas Georgiou</i> and <i>Jeff Cooper</i></dd></dl>");
00057             return;
00058       }*/// NOT GOOD!!!
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")) //EASTER EGG!
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; //case insensitive regex of the word
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()) //iterate through all the lines
00101       {
00102           QString currentLine = QString(retLines.takeFirst());
00103           if (currentLine.startsWith("552")) //if no match was found
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")) //begin definition
00111           {
00112               isFirst = true;
00113               continue;
00114           }
00115           if (currentLine.startsWith('.')) //end definition
00116           {
00117               def += "</dd><!--PERIOD-->";
00118               continue;
00119           }
00120           if (!(currentLine.startsWith("150") || currentLine.startsWith("151")// if it is a definition line
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>"); //the i?e?s is for most plurals... i'll fix it soon
00131               currentLine.replace(QRegExp("(^| |\\.)(\\[[^]]+\\])( |\\.|$)"), "<i>\\2</i>");
00132 
00133               //currentLine.replace(currentWord, "<b>"+currentWord+"</b>", Qt::CaseInsensitive);
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             //TODO: What happens if no DB available?
00181             //TODO: Eventually there will be functionality to change the server...
00182             break;
00183         }
00184         if (!curr.startsWith('-'))
00185         {
00186             curr = curr.trimmed();
00187             tmp1=curr.section(' ',0,1);
00188             tmp2=curr.section(' ',1);
00189   //          theHash.insert(tmp1, tmp2);
00190             kDebug() << tmp1 + "  " + tmp2;
00191             setData("showDictionaries",tmp1,tmp2);
00192         }
00193     }
00194     connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(socketClosed()));
00195     tcpSocket->disconnectFromHost();
00196 //    setData("showDictionaries", "dictionaries", QByteArray(theHash);
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(); //stop if lookup is in progress and new word is requested
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"

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
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
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