kiten/lib
Entry.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
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
00079
00080 }
00081
00082
00083 const QString &Entry::getDictName() const { return sourceDict; }
00084
00085
00086 QString Entry::getWord() const { return Word; }
00087
00088 QString Entry::getMeanings() const {return Meanings.join(outputListDelimiter);}
00089
00090 QStringList Entry::getMeaningsList() const { return Meanings; }
00091
00092 QString Entry::getReadings() const {return Readings.join(outputListDelimiter);}
00093
00094 QStringList Entry::getReadingsList() const { return Readings; }
00095
00096 const QHash<QString,QString> &Entry::getExtendedInfo() const { return ExtendedInfo; }
00097
00098
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
00115
00116 inline QString Entry::makeLink(const QString &entryString) const
00117 {
00118 return "<a href=\"" + entryString + "\">" + entryString + "</a>";
00119 }
00120
00121
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;
00128 return true;
00129 }
00130
00131 inline QString Entry::toKVTML() const
00132 {
00133
00134
00135
00136
00137
00138
00139
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
00146 inline QString Entry::HTMLWord() const
00147 {
00148 return "<span class=\"Word\">" + Word + "</span>";
00149 }
00150
00151
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
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
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
00264
00265
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
00289 if(thisOne != thatOne) {
00290
00291 if(thatOne.isEmpty()) return true;
00292
00293 if(thisOne.isEmpty()) return false;
00294
00295 return this->sortByField(that,field);
00296 }
00297 }
00298 }
00299 }
00300 return false;
00301 }
00302
00303 bool Entry::sortByField(const Entry &that, const QString &field) const {
00304 return this->getExtendedInfoItem(field) < that.getExtendedInfoItem(field);
00305 }
00306