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

kiten/lib

dictFileEdict.cpp

Go to the documentation of this file.
00001 /* This file is part of Kiten, a KDE Japanese Reference Tool...
00002     Copyright (C) 2001 by Jason Katz-Brown
00003               (C) 2006 by Joseph Kerian  <jkerian@gmail.com>
00004               (C) 2006 by 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 "dictFileEdict.h"
00023 
00024 #include <kconfig.h>
00025 #include <kconfigskeleton.h>
00026 #include <kglobal.h>
00027 #include <kdebug.h>
00028 #include <kstandarddirs.h>
00029 #include <kprocess.h>
00030 #include <kapplication.h>
00031 
00032 #include <QtCore/QByteArray>
00033 #include <QtCore/QVector>
00034 #include <QtCore/QString>
00035 
00036 #include <QtCore/QTextStream>
00037 #include <QtCore/QTextCodec>
00038 #include <QtCore/QFile>
00039 
00040 #include "dictFilePreferenceDialog.h"
00041 #include "DictQuery.h"  //DictQuery classs
00042 #include "Entry.h"      //Entry and EntryList classes
00043 #include "EntryList.h"
00044 
00045 /* Per instructions in the super-class, this constructor basically sets the
00046   dictionaryType member variable to identify this as an edict-type database handler */
00047 dictFileEdict::dictFileEdict() : dictFile("edict")
00048 {
00049     m_searchableAttributes.insert("common","common");
00050 }
00051 
00052 QStringList *dictFileEdict::displayFields = NULL;
00053 
00054 /* The destructor... ditch our memory maps and close our files here
00055   (if they were open) */
00056 dictFileEdict::~dictFileEdict() {
00057 }
00058 
00059 /* Scan a potential file for the correct format, remembering to skip comment
00060   characters. This is not a foolproof scan, but it should be checked before adding
00061   a new dictionary.
00062   Valid EDICT format is considered:
00063   <kanji or kana>+ [<kana>] /latin characters & symbols/separated with slashes/
00064   Comment lines start with... something... not remembering now.
00065  */
00066 bool dictFileEdict::validDictionaryFile(const QString &filename) {
00067     QFile file(filename);
00068     int totalLineCounter = 0;
00069     bool returnFlag = true;
00070 
00071     if(!file.exists())  //The easy test... does it exist?
00072         return false;
00073     if(!file.open(QIODevice::ReadOnly)) //And can we read it?
00074         return false;
00075 
00076     //Now we can actually check the file
00077     QTextStream fileStream(&file);
00078     fileStream.setCodec(QTextCodec::codecForName("eucJP"));
00079     QString commentMarker("????"); //Note: Don't touch this! vim seems to have
00080                                                         //An odd text codec error here too :(
00081     QRegExp formattedLine("^\\S+\\s+(\\[\\S+\\]\\s+)?/.*/$");
00082     while( !fileStream.atEnd() ) {
00083         QString line = fileStream.readLine();
00084         totalLineCounter++;
00085 
00086         if(line.left(4)==commentMarker)
00087             continue;
00088         if(line.contains(formattedLine)) //If it matches our regex
00089             continue;
00090 
00091         returnFlag = false;
00092         break;
00093     }
00094 
00095     file.close();
00096     return returnFlag;
00097 }
00098 
00099 /* Reject queries that specify anything we don't understand */
00100 //TODO: Actually write this method (validQuery)
00101 bool dictFileEdict::validQuery(const DictQuery &query) {
00102     return true;
00103 }
00104 
00105 /* Load up the dictionary... */
00106 bool dictFileEdict::loadDictionary(const QString &fileName, const QString &dictName) {
00107     if(m_file.valid()) return false; //Already loaded
00108 
00109     if(m_file.loadFile(fileName)) {
00110         m_dictionaryName = dictName;
00111         m_dictionaryFile = fileName;
00112         return true;
00113     }
00114     return false;
00115 }
00116 
00117 /* Do a search, respond with a list of entries.
00118  The general strategy will be to take the first word of the query, and do a
00119  binary search on the dictionary for that item. Take all results and filter
00120  them using the rest of the query with the validate method.
00121  */
00122 EntryList *dictFileEdict::doSearch(const DictQuery &i_query) {
00123     if(i_query.isEmpty() || !m_file.valid())    //No query or dict, no results.
00124         return new EntryList();
00125 
00126     DictQuery query(i_query);
00127     kDebug()<< "Search from : " << getName();
00128 
00129     QString firstChoice = query.getWord();
00130     if(firstChoice.length() == 0) {
00131         firstChoice = query.getPronunciation();
00132         if(firstChoice.length() == 0) {
00133             firstChoice = query.getMeaning().split(' ').first().toLower();
00134             if(firstChoice.length() == 0) {
00135                 //The nastiest situation... we have to assemble a search string
00136                 //from the first property
00137                 QList<QString> keys = query.listPropertyKeys();
00138                 if(keys.size() == 0) //Shouldn't happen... but maybe in the future
00139                     return new EntryList();
00140                 firstChoice = keys[0];
00141                 firstChoice = firstChoice + query.getProperty(firstChoice);
00142                 //TODO: doSearch: some accomodation for searching for ranges and such of properties
00143             }
00144         }
00145     } else
00146         firstChoice = firstChoice.at(0); //Only search for one kanji or the
00147                                          //binary lookup mechanism breaks
00148 
00149     QVector<QString> preliminaryResults = m_file.findMatches(firstChoice);
00150 
00151     if(preliminaryResults.size() == 0)  //If there were no matches... return an empty list
00152         return new EntryList();
00153 
00154     EntryList *results = new EntryList();
00155     foreach(const QString &it, preliminaryResults) {
00156 //      kDebug() << "result: " << it << endl;
00157             Entry *result = makeEntry(it);
00158             if(result->matchesQuery(query))
00159                 results->append(result);
00160     }
00161     return results;
00162 }
00163 
00164 /* Make a list of all the extra fields in our db.. Entry uses this to decide
00165   what goes in the interpretations it gives. */
00166 QStringList dictFileEdict::listDictDisplayOptions(QStringList x) const {
00167     x += displayOptions().keys();
00168     return x;
00169 }
00170 
00171 QMap<QString,QString> dictFileEdict::displayOptions() const {
00172     QMap<QString,QString> list;
00173     list["Common(C)"]="C";
00174     list["Part of speech(type)"]="type";
00175     return list;
00176 }
00177 
00178 DictionaryPreferenceDialog *dictFileEdict::preferencesWidget(KConfigSkeleton *config, QWidget *parent) {
00179     dictFileFieldSelector *dialog = new dictFileFieldSelector(config, getType(),parent);
00180     dialog->addAvailable(listDictDisplayOptions(QStringList()));
00181     return dialog;
00182 }
00183 
00184 void
00185 dictFileEdict::loadSettings(KConfigSkeleton *config) {
00186     QMap<QString,QString> long2short = displayOptions();
00187     long2short["Word/Kanji"]="Word/Kanji";
00188     long2short["Reading"]="Reading";
00189     long2short["Meaning"]="Meaning";
00190     long2short["--Newline--"]="--Newline--";
00191 
00192     KConfigSkeletonItem *item = config->findItem(getType()+"__displayFields");
00193     this->displayFields = loadListType(item,this->displayFields,long2short);
00194 }
00195 
00196 QStringList *
00197 dictFileEdict::loadListType(KConfigSkeletonItem *item, QStringList *list,
00198         const QMap<QString,QString> &long2short) {
00199     QStringList listFromItem;
00200 
00201     if(item != NULL)
00202         listFromItem = item->property().toStringList();
00203     if(!listFromItem.isEmpty()) {
00204         if(list != NULL)
00205             delete list;
00206         list = new QStringList();
00207         foreach(const QString &it, listFromItem)
00208             if(long2short.contains(it))
00209                 list->append(long2short[it]);
00210     }
00211     return list;
00212 }

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