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

kiten/lib

Entry.cpp

Go to the documentation of this file.
00001 /* This file is part of Kiten, a KDE Japanese Reference Tool...
00002  Copyright (C) 2001  Jason Katz-Brown <jason@katzbrown.com>
00003            (C) 2006  Joseph Kerian <jkerian@gmail.com>
00004               (C) 2006  Eric Kjeldergaard <kjelderg@gmail.com>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019    Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "Entry.h"
00023 #include "DictQuery.h"
00024 
00025 #include <qfileinfo.h>
00026 #include <qregexp.h>
00027 #include <qtextcodec.h>
00028 
00029 #include <kdebug.h>
00030 #include <klocale.h>
00031 #include <kmessagebox.h>
00032 #include <kstandarddirs.h>
00033 
00034 #include <iostream>
00035 #include <cassert>
00036 #include <sys/mman.h>
00037 #include <stdio.h>
00038 
00039 
00044 Entry::Entry() {
00045     init();
00046 }
00047 Entry::Entry(const QString &sourceDictionary)
00048     : sourceDict(sourceDictionary)
00049 {
00050     init();
00051 }
00052 
00053 Entry::Entry(const QString &sourceDictionary, const QString &word,
00054                 const QStringList &reading, const QStringList &meanings)
00055     : Word(word)
00056     , Meanings(meanings)
00057     , Readings(reading)
00058     , sourceDict(sourceDictionary)
00059 {
00060     init();
00061 }
00062 
00063 Entry::Entry(const Entry &src)
00064     : Word(src.Word)
00065     , Meanings(src.Meanings)
00066     , Readings(src.Readings)
00067     , ExtendedInfo(src.ExtendedInfo)
00068     , sourceDict(src.sourceDict)
00069 {
00070     outputListDelimiter=src.outputListDelimiter;
00071 }
00072 
00073 void Entry::init() {
00074     outputListDelimiter=i18n("; ");
00075 }
00076 
00077 Entry::~Entry() {
00078 //  kdDebug() << "nuking : " << Word << endl;
00079 
00080 }
00081 
00082 /* Get the dictionary name that generated this Entry. I can't think of a reason to be changing this.  */
00083 const QString &Entry::getDictName() const { return sourceDict; }
00084 /* Get the word from this Entry. If the entry is of type kanji/kana/meaning/etc, this will return
00085  * the kanji. If it is of kana/meaning/etc, it will return kana  */
00086 QString Entry::getWord() const { return Word; }
00087 /* Get a QString containing all of the meanings known, connected by the outputListDelimiter  */
00088 QString Entry::getMeanings() const {return Meanings.join(outputListDelimiter);}
00089 /* Simple accessor */
00090 QStringList Entry::getMeaningsList() const { return Meanings; }
00091 /* Simple accessor */
00092 QString Entry::getReadings() const {return Readings.join(outputListDelimiter);}
00093 /* Simple accessor */
00094 QStringList Entry::getReadingsList() const { return Readings; }
00095 /* Simple accessor */
00096 const QHash<QString,QString> &Entry::getExtendedInfo() const { return ExtendedInfo; }
00097 /* Simple accessor
00098  * @param x the key for the extended info item to get */
00099 QString Entry::getExtendedInfoItem(const QString &x) const { return ExtendedInfo[x]; }
00100 
00102 QString Entry::toHTML() const {
00103     return "<div class=\"Entry\">" + HTMLWord() + HTMLReadings() + HTMLMeanings() + "</div>";
00104 }
00105 
00110 QString Entry::toString() const {
00111     return Word + " (" + getReadings() + ") " + getMeanings();
00112 }
00113 
00114 /* New functions for Entry doing direct display */
00115 /* Creates a link for the given @p entryString. */
00116 inline QString Entry::makeLink(const QString &entryString) const
00117 {
00118     return "<a href=\"" + entryString + "\">" + entryString + "</a>";
00119 }
00120 
00121 /* Determines whether @p characeter is a kanji character. */
00122 bool Entry::isKanji(const QChar &character) const
00123 {
00124     ushort value = character.unicode();
00125     if(value < 255) return false;
00126     if(0x3040 <= value && value <= 0x30FF)
00127         return false; //Kana
00128     return true; //Note our folly here... we assuming any non-ascii/kana is kanji
00129 }
00130 
00131 inline QString Entry::toKVTML() const
00132 {
00133     /*
00134    <e m="1" s="1">
00135        <o width="414" l="en" q="t">(eh,) excuse me</o>
00136        <t width="417" l="jp" q="o">(あのう、) すみません </t>
00137    </e>
00138    */
00139     //TODO: en should not necessarily be the language here.
00140     return "<e>\n<o l=\"en\">" + getMeanings() + "</o>\n"
00141         "<t l=\"jp-kanji\">" + getWord() + "</t>\n" +
00142         "<t l=\"jp-kana\">" + getReadings() + "</t></e>\n\n";
00143 }
00144 
00145 /* Prepares Word for output as HTML */
00146 inline QString Entry::HTMLWord() const
00147 {
00148     return "<span class=\"Word\">" + Word + "</span>";
00149 }
00150 
00151 /* Prepares Readings for output as HTML */
00152 inline QString Entry::HTMLReadings() const
00153 {
00154     QStringList list;
00155     foreach(const QString &it, Readings) {
00156         list += makeLink(it);
00157     }
00158     return "<span class=\"Readings\">"+list.join(outputListDelimiter)+"</span>";
00159 }
00160 
00161 /* Prepares Meanings for output as HTML */
00162 inline QString Entry::HTMLMeanings() const
00163 {
00164     return "<span class=\"Meanings\">" + Meanings.join(outputListDelimiter)
00165         + "</span>";
00166 }
00167 
00168 bool Entry::matchesQuery(const DictQuery &query) const {
00169 
00170     if(!query.getWord().isEmpty()) {
00171         if(query.getMatchType() == DictQuery::matchExact &&
00172                 this->getWord() != query.getWord())
00173                 return false;
00174         if(query.getMatchType() == DictQuery::matchBeginning &&
00175                 !this->getWord().startsWith(query.getWord()))
00176                 return false;
00177         if(query.getMatchType() == DictQuery::matchAnywhere &&
00178                 !this->getWord().contains(query.getWord()))
00179                 return false;
00180     }
00181 
00182     if(!query.getPronunciation().isEmpty() && !getReadings().isEmpty())
00183         if(!listMatch(Readings, query.getPronunciation().split(DictQuery::mainDelimiter),
00184                     query.getMatchType() ) )
00185             return false;
00186 
00187     if(!query.getPronunciation().isEmpty() && getReadings().isEmpty() && !getWord().isEmpty())
00188     {
00189         switch (query.getMatchType())
00190         {
00191             case DictQuery::matchExact:
00192                 if (getWord() != query.getPronunciation())
00193                     return false;
00194                 break;
00195             case DictQuery::matchBeginning:
00196                 if (!getWord().startsWith(query.getPronunciation()))
00197                     return false;
00198                 break;
00199             case DictQuery::matchAnywhere:
00200                 if (!getWord().contains(query.getPronunciation()))
00201                     return false;
00202                 break;
00203         }
00204     }
00205 
00206     if(!query.getMeaning().isEmpty())
00207         if(!listMatch(Meanings.join(" ").toLower().split(' '),
00208                     query.getMeaning().toLower().split(DictQuery::mainDelimiter),
00209                         query.getMatchType()) )
00210             return false;
00211 
00212 
00213     QList<QString> propList = query.listPropertyKeys();
00214     foreach(const QString &key, propList) {
00215         if( !extendedItemCheck(key, query.getProperty(key)))
00216             return false;
00217     }
00218 
00219     return true;
00220 }
00221 
00222 
00223 bool Entry::extendedItemCheck(const QString& key, const QString &value) const
00224 {
00225     return getExtendedInfoItem(key) == value;
00226 }
00227 
00228 
00229 //Returns true if all members of test are in list
00230 bool Entry::listMatch(const QStringList &list, const QStringList &test, DictQuery::MatchType type) const
00231 {
00232     if(type == DictQuery::matchExact) {
00233         foreach(const QString &it, test)
00234             if(!list.contains(it))
00235                 return false;
00236     } else if(type == DictQuery::matchBeginning) {
00237         foreach(const QString &it, test) {
00238             bool found = false;
00239             foreach(const QString &it2, list)
00240                 if(it2.startsWith(it)) {
00241                     found = true;
00242                     break;
00243                 }
00244             if(!found)
00245                 return false;
00246         }
00247     } else {
00248         foreach(const QString &it, test) {
00249             bool found = false;
00250             foreach(const QString &it2, list)
00251                 if(it2.contains(it)) {
00252                     found = true;
00253                     break;
00254                 }
00255             if(!found)
00256                 return false;
00257         }
00258     }
00259     return true;
00260 }
00261 
00262 
00263 /* This version of sort only sorts dictionaries...
00264     This is a replacement for a operator< function... so we return true if
00265     "this" should show up first on the list. */
00266 bool Entry::sort(const Entry &that, const QStringList &dictOrder,
00267         const QStringList &fields) const {
00268     if(this->sourceDict != that.sourceDict) {
00269         foreach(const QString &dict, dictOrder) {
00270             if(dict == that.sourceDict)
00271                 return false;
00272             if(dict == this->sourceDict)
00273                 return true;
00274         }
00275     } else {
00276         foreach(const QString &field, fields) {
00277             if(field == QString("Word/Kanji"))
00278                 return this->getWord() < that.getWord();
00279             else if(field == QString("Meaning"))
00280                 return listMatch(that.getMeaningsList(),this->getMeaningsList(),DictQuery::matchExact) &&
00281                     (that.getMeaningsList().count() != this->getMeaningsList().count());
00282             else if(field == QString("Reading"))
00283                 return listMatch(that.getReadingsList(),this->getReadingsList(),DictQuery::matchExact) &&
00284                     (that.getReadingsList().count() != this->getReadingsList().count());
00285             else {
00286                 const QString thisOne = this->getExtendedInfoItem(field);
00287                 const QString thatOne = that.getExtendedInfoItem(field);
00288                 //Only sort by this field if the values differ, otherwise move to the next field
00289                 if(thisOne != thatOne) {
00290                     //If the second item does not have this field, sort this one first
00291                     if(thatOne.isEmpty()) return true;
00292                     //If we don't have this field, sort "this" to second
00293                     if(thisOne.isEmpty()) return false;
00294                     //Otherwise, send it to a virtual function (to allow dictionaries to override sorting)
00295                     return this->sortByField(that,field);
00296                 }
00297             }
00298         }
00299     }
00300     return false; //If we reach here, they match as much as possible
00301 }
00302 
00303 bool Entry::sortByField(const Entry &that, const QString &field) const {
00304     return  this->getExtendedInfoItem(field) < that.getExtendedInfoItem(field);
00305 }
00306 

kiten/lib

Skip menu "kiten/lib"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kdeedu

Skip menu "kdeedu"
  • kalzium
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  •   docs
  •   src
  • parley
  •   stepcore
Generated for kdeedu 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