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

kiten/lib

DictionaryManager.cpp

Go to the documentation of this file.
00001 /* This file is part of Kiten, a KDE Japanese Reference Tool...
00002    Copyright (C) 2006 Joseph Kerian <jkerian@gmail.com>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "DictionaryManager.h"
00021 #include "DictionaryPreferenceDialog.h"
00022 #include "DictQuery.h"
00023 #include "Entry.h"
00024 #include "EntryList.h"
00025 
00026 #include "dictFile.h"
00027 
00028 #include <kdebug.h>
00029 #include <kglobal.h>
00030 #include <kconfig.h>
00031 #include <kconfigskeleton.h>
00032 #include <QtCore/QString>
00033 #include <QtCore/QFile>
00034 
00035 /* Includes to handle various types of dictionaries
00036 IMPORTANT: To add a dictionary type, add the header file here and add it to the
00037  if statement under addDictionary() */
00038 #include "dictEdict/dictFileEdict.h"
00039 #include "dictKanjidic/dictFileKanjidic.h"
00040 #include "dictDeinflect/dictFileDeinflect.h"
00041 
00042 #if 0
00043 class debug_entry : public Entry {
00044     public:
00045     debug_entry(QString word) :
00046         Entry(QString("libkiten"),word,QStringList(),QStringList()),
00047         count(counter++)    {
00048         }
00049     virtual Entry * clone() const { return new debug_entry(*this); }
00050     virtual bool loadEntry(const QString &) { return false; }
00051     virtual QString dumpEntry() const { return ""; }
00052     virtual bool sort(const debug_entry &that, const QStringList &dicts,
00053             const QStringList &fields) {
00054         return this->count < that.count;
00055     }
00056     int count;
00057     static int counter;
00058 };
00059 int debug_entry::counter = 0;
00060 #endif
00061 
00065 dictFile *DictionaryManager::makeDictFile(const QString &type) {
00066     if(type == "edict")
00067         return new dictFileEdict();
00068     if(type == "kanjidic")
00069         return new dictFileKanjidic();
00070     if(type == "deinflect")
00071         return new dictFileDeinflect();
00072     //Add new dictionary types here!!!
00073 
00074     return NULL;
00075 }
00079 QStringList DictionaryManager::listDictFileTypes() {
00080     QStringList list;
00081     list.append("edict");
00082     list.append("kanjidic");
00083     list.append("deinflect");
00084     //Add your dictionary type here!
00085     return list;
00086 }
00087 
00088 class DictionaryManager::Private {
00089 public:
00091     QHash<QString,dictFile*> dictManagers;  //List is indexed by dictionary names.
00092 };
00093 
00094 /* Given a named Dict file/name/type... create and add the object if it
00095   seems to work properly on creation. */
00096 bool DictionaryManager::addDictionary(const QString &file, const QString &name,
00097         const QString &type) {
00098 
00099     if(d->dictManagers.contains(name)) //This name already exists in the list!
00100         return false;
00101 
00102     dictFile *newDict = makeDictFile(type);
00103     if(newDict == NULL)
00104         return false;
00105 
00106     if(!newDict->loadDictionary(file,name)) {
00107         kDebug() << "Dictionary load FAILED: " << newDict->getName();
00108         delete newDict;
00109         return false;
00110     }
00111 
00112     kDebug() << "Dictionary Loaded : " << newDict->getName();
00113     d->dictManagers.insert(name,newDict);
00114     return true;
00115 }
00116 
00117 /* The constructor. Set autodelete on our dictionary list */
00118 DictionaryManager::DictionaryManager() : d(new Private) {
00119 }
00120 
00121 /* Delete everything in our hash */
00122 DictionaryManager::~DictionaryManager() {
00123     {
00124         QMutableHashIterator<QString, dictFile*> it(d->dictManagers);
00125         while(it.hasNext()) {
00126             it.next();
00127             delete it.value();
00128             it.remove();
00129         }
00130     }
00131    delete d;
00132 }
00133 
00134 /* Remove a dictionary from the list, and delete the dictionary object
00135   (it should close files, deallocate memory, etc).
00136   @param name the name of the dictionary, as given in the addDictionary method */
00137 bool DictionaryManager::removeDictionary(const QString &name) {
00138     dictFile *file = d->dictManagers.take(name);
00139     delete file;
00140     return true;
00141 }
00142 
00143 /* Return a list of the dictionaries by their name (our key)
00144   Note that this dictionary name does not necessarily have to have anything
00145   to do with the actual dictionary name... */
00146 QStringList DictionaryManager::listDictionaries() const {
00147     QStringList ret;
00148     foreach(dictFile *it, d->dictManagers)
00149         ret.append(it->getName());
00150     return ret;
00151 }
00152 
00153 /* Return the dictionary type and file used by a named dictionary.
00154   returns a pair of empty QStrings if you specify an invalid name
00155   @param name the name of the dictionary, as given in the addDictionary method */
00156 QPair<QString, QString> DictionaryManager::listDictionaryInfo(const QString &name) const {
00157     if(!d->dictManagers.contains(name)) //This name not in list!
00158         return qMakePair(QString(),QString());
00159     return qMakePair(d->dictManagers[name]->getName(),d->dictManagers[name]->getFile());
00160 }
00161 
00162 /* Return a list of the names of each dictionary of a given type.
00163  * @param type the type of the dictionary we want a list of */
00164 QStringList DictionaryManager::listDictionariesOfType(const QString &type) const {
00165     QStringList ret;
00166     QHash<QString, dictFile*>::const_iterator it = d->dictManagers.begin();
00167     while(it != d->dictManagers.end()) {
00168         if(it.value()->getType() == type)
00169             ret.append(it.key());
00170         ++it;
00171     }
00172     return ret;
00173 }
00174 
00175 /* Examine the DictQuery and farm out the search to the specialized dict
00176   managers. Note that a global search limit will probably be implemented
00177   either here or in the dictFile implementations... probably both
00178   @param query the query, see DictQuery documentation */
00179 EntryList *DictionaryManager::doSearch(const DictQuery &query) const {
00180     EntryList *ret=new EntryList();
00181 #if 0
00182     if(query.getMeaning() == "(libkiten)") {
00183             ret->append(new debug_entry("Summary of libkiten data"));
00184             foreach(const QString &dict, listDictionaries()) {
00185                 ret->append(new debug_entry(dict));
00186             }
00187             return ret;
00188     }
00189 #endif
00190 
00191     //There are two basic modes.... one in which the query
00192     //Specifies the dictionary list, one in which it does not
00193     QStringList dictsFromQuery = query.getDictionaries();
00194     if(dictsFromQuery.isEmpty()) { //None specified, search all
00195         foreach( dictFile *it, d->dictManagers) {
00196             EntryList *temp=it->doSearch(query);
00197             if(temp)
00198                ret->appendList(temp);
00199             delete temp;
00200         }
00201     } else {
00202         foreach( const QString &target, dictsFromQuery) {
00203             dictFile *newestFound = d->dictManagers.find(target).value();
00204             if(newestFound != 0) {
00205                 EntryList *temp = newestFound->doSearch(query);
00206                 if(temp)
00207                    ret->appendList(temp);
00208                 delete temp;
00209             }
00210         }
00211     }
00212 
00213     ret->setQuery(query); //Store the query for later use.
00214     kDebug()<<"From query: '"<<query.toString()<<"' Found " << ret->count() << " results";
00215     kDebug()<<"Incoming match type: "<<query.getMatchType()<<" Outgoing: "<<ret->getQuery().getMatchType();
00216     return ret;
00217 }
00218 
00219 /* For this case, we let polymorphism do most of the work. We assume that the user wants
00220   to pare down the results, so we let the individual entry metching methods run over the
00221   new query and accept (and copy) any of those that pass */
00222 EntryList *DictionaryManager::doSearchInList(const DictQuery &query, const EntryList *list) const {
00223     EntryList *ret = new EntryList();
00224 
00225     foreach( Entry* it, *list) {
00226         if(it->matchesQuery(query)) {
00227             Entry *x = it->clone();
00228             ret->append(x);
00229         }
00230     }
00231     ret->setQuery(query + list->getQuery());
00232     return ret;
00233 }
00234 
00235 /* Load preference settings for a particular dictionary */
00236 void DictionaryManager::loadDictSettings(const QString &dictName, KConfigSkeleton *config) {
00237     dictFile *dict = this->makeDictFile(dictName);
00238     if(dict != NULL) {
00239         config->setCurrentGroup("dicts_"+dictName.toLower());
00240         dict->loadSettings(config);
00241     }
00242 }
00243 
00244 void DictionaryManager::loadSettings(const KConfig &config) { //TODO
00245 }
00246 
00247 QMap<QString,DictionaryPreferenceDialog*>
00248 DictionaryManager::generatePreferenceDialogs(KConfigSkeleton *config, QWidget *parent) {
00249     QMap<QString,DictionaryPreferenceDialog*> result;
00250     QStringList dictTypes = listDictFileTypes();
00251     foreach(const QString &dictType, dictTypes) {
00252         dictFile *tempDictFile = makeDictFile(dictType);
00253         DictionaryPreferenceDialog *newDialog =
00254                 tempDictFile->preferencesWidget(config,parent);
00255         if(newDialog==NULL)
00256             continue;
00257         result.insert(dictType,newDialog);
00258         delete tempDictFile;
00259     }
00260     return result;
00261 }
00262 
00263 QMap<QString, QString>
00264 DictionaryManager::generateExtendedFieldsList() {
00265     QMap<QString,QString> result;
00266     QStringList dictTypes = listDictFileTypes();
00267     foreach(const QString &dictType, dictTypes) {
00268         dictFile *tempDictFile = makeDictFile(dictType);
00269         QMap<QString,QString> tempList = tempDictFile->getSearchableAttributes();
00270         QMap<QString,QString>::const_iterator it = tempList.constBegin();
00271         while( it != tempList.constEnd() ) {
00272             if(!result.contains(it.key()))
00273                 result.insert(it.key(),it.value());
00274             ++it;
00275         }
00276         delete tempDictFile;
00277     }
00278     return result;
00279 }
00280 

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