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

kiten/lib

  • sources
  • kde-4.14
  • kdeedu
  • kiten
  • lib
dictionarymanager.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  * This file is part of Kiten, a KDE Japanese Reference Tool *
3  * Copyright (C) 2006 Joseph Kerian <jkerian@gmail.com> *
4  * Copyright (C) 2011 Daniel E. Moctezuma <democtezuma@gmail.com> *
5  * *
6  * This library is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Library General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Library General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Library General Public License *
17  * along with this library; see the file COPYING.LIB. If not, write to *
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
19  * Boston, MA 02110-1301, USA. *
20  *****************************************************************************/
21 
22 #include "dictionarymanager.h"
23 
24 #include "dictfile.h"
25 #include "dictionarypreferencedialog.h"
26 #include "dictquery.h"
27 #include "entry.h"
28 #include "entrylist.h"
29 #include "kitenmacros.h"
30 
31 #include <KDebug>
32 #include <KGlobal>
33 #include <KConfig>
34 #include <KConfigSkeleton>
35 
36 #include <QFile>
37 #include <QString>
38 
39 /* Includes to handle various types of dictionaries
40 IMPORTANT: To add a dictionary type, add the header file here and add it to the
41  if statement under addDictionary() */
42 #include "DictEdict/dictfileedict.h"
43 #include "DictKanjidic/dictfilekanjidic.h"
44 
45 class DictionaryManager::Private
46 {
47  public:
51  QHash<QString,DictFile*> dictManagers;
52 };
53 
54 #if 0
55 class debug_entry : public Entry
56 {
57  public:
58  debug_entry(QString word) : Entry( QString( "libkiten" ), word
59  , QStringList(), QStringList() ), count( counter++ )
60  { }
61  virtual Entry * clone() const { return new debug_entry( *this ); }
62  virtual bool loadEntry( const QString& ) { return false; }
63  virtual QString dumpEntry() const { return ""; }
64  virtual bool sort( const debug_entry &that, const QStringList &dicts,
65  const QStringList &fields )
66  { return this->count < that.count; }
67 
68  int count;
69  static int counter;
70 };
71 int debug_entry::counter = 0;
72 #endif
73 
77 DictionaryManager::DictionaryManager()
78 : d( new Private )
79 {
80 }
81 
85 DictionaryManager::~DictionaryManager()
86 {
87  {
88  QMutableHashIterator<QString, DictFile*> it( d->dictManagers );
89  while( it.hasNext() )
90  {
91  it.next();
92  delete it.value();
93  it.remove();
94  }
95  }
96 
97  delete d;
98 }
99 
104 bool DictionaryManager::addDictionary( const QString &file
105  , const QString &name
106  , const QString &type )
107 {
108  if( d->dictManagers.contains( name ) ) //This name already exists in the list!
109  {
110  return false;
111  }
112 
113  DictFile *newDict = makeDictFile( type );
114  if( newDict == NULL )
115  {
116  return false;
117  }
118 
119  if( ! newDict->loadDictionary( file, name ) )
120  {
121  kDebug() << "Dictionary load FAILED: " << newDict->getName();
122  delete newDict;
123  return false;
124  }
125 
126  kDebug() << "Dictionary Loaded : " << newDict->getName();
127  d->dictManagers.insert( name, newDict );
128  return true;
129 }
130 
138 EntryList *DictionaryManager::doSearch( const DictQuery &query ) const
139 {
140  EntryList *ret = new EntryList();
141  #if 0
142  if( query.getMeaning() == "(libkiten)" )
143  {
144  ret->append( new debug_entry( "Summary of libkiten data" ) );
145  foreach( const QString &dict, listDictionaries() )
146  {
147  ret->append( new debug_entry( dict ) );
148  }
149  return ret;
150  }
151  #endif
152 
153  // There are two basic modes.... one in which the query
154  // specifies the dictionary list, one in which it does not
155  QStringList dictsFromQuery = query.getDictionaries();
156  if( dictsFromQuery.isEmpty() )
157  {
158  // None specified, search all
159  foreach( DictFile *it, d->dictManagers )
160  {
161  kDebug() << "Searching in " << it->getName() << "dictionary." << endl;
162  EntryList *temp = it->doSearch( query );
163  if( temp )
164  {
165  ret->appendList( temp );
166  }
167  delete temp;
168  }
169  }
170  else
171  {
172  foreach( const QString &target, dictsFromQuery )
173  {
174  DictFile *newestFound = d->dictManagers.find( target ).value();
175  if( newestFound != 0 )
176  {
177  EntryList *temp = newestFound->doSearch( query );
178  if( temp )
179  {
180  ret->appendList( temp );
181  }
182  delete temp;
183  }
184  }
185  }
186 
187  ret->setQuery( query ); //Store the query for later use.
188  kDebug() << "From query: '" << query.toString() << "' Found " << ret->count() << " results";
189  kDebug() << "Incoming match type: " << query.getMatchType() << " Outgoing: " << ret->getQuery().getMatchType();
190  return ret;
191 }
192 
198 EntryList *DictionaryManager::doSearchInList( const DictQuery &query, const EntryList *list ) const
199 {
200  EntryList *ret = new EntryList();
201 
202  foreach( Entry* it, *list )
203  {
204  if( it->matchesQuery( query ) )
205  {
206  Entry *x = it->clone();
207  ret->append( x );
208  }
209  }
210 
211  ret->setQuery( query + list->getQuery() );
212  return ret;
213 }
214 
215 QMap<QString, QString> DictionaryManager::generateExtendedFieldsList()
216 {
217  QMap<QString,QString> result;
218  QStringList dictTypes = listDictFileTypes();
219  foreach( const QString &dictType, dictTypes )
220  {
221  DictFile *tempDictFile = makeDictFile( dictType );
222  QMap<QString,QString> tempList = tempDictFile->getSearchableAttributes();
223  QMap<QString,QString>::const_iterator it = tempList.constBegin();
224  while( it != tempList.constEnd() )
225  {
226  if( ! result.contains( it.key() ) )
227  {
228  result.insert( it.key(), it.value() );
229  }
230  ++it;
231  }
232  delete tempDictFile;
233  }
234 
235  return result;
236 }
237 
238 QMap<QString,DictionaryPreferenceDialog*>
239 DictionaryManager::generatePreferenceDialogs( KConfigSkeleton *config, QWidget *parent )
240 {
241  QMap<QString,DictionaryPreferenceDialog*> result;
242  QStringList dictTypes = listDictFileTypes();
243  foreach( const QString &dictType, dictTypes )
244  {
245  DictFile *tempDictFile = makeDictFile( dictType );
246  DictionaryPreferenceDialog *newDialog = tempDictFile->preferencesWidget( config, parent );
247 
248  if( newDialog == NULL ) continue;
249  result.insert( dictType, newDialog );
250  delete tempDictFile;
251  }
252 
253  return result;
254 }
255 
261 QStringList DictionaryManager::listDictionaries() const
262 {
263  QStringList ret;
264  foreach( DictFile *it, d->dictManagers )
265  {
266  ret.append( it->getName() );
267  }
268 
269  return ret;
270 }
271 
277 QStringList DictionaryManager::listDictFileTypes()
278 {
279  QStringList list;
280  list.append( EDICT );
281  list.append( KANJIDIC );
282 
283  //Add your dictionary type here!
284 
285  return list;
286 }
287 
294 QPair<QString, QString> DictionaryManager::listDictionaryInfo( const QString &name ) const
295 {
296  if( ! d->dictManagers.contains( name ) ) //This name not in list!
297  {
298  return qMakePair( QString(), QString() );
299  }
300 
301  return qMakePair( d->dictManagers[ name ]->getName(), d->dictManagers[ name ]->getFile() );
302 }
303 
309 QStringList DictionaryManager::listDictionariesOfType( const QString &type ) const
310 {
311  QStringList ret;
312  QHash<QString, DictFile*>::const_iterator it = d->dictManagers.constBegin();
313  while( it != d->dictManagers.constEnd() )
314  {
315  if( it.value()->getType() == type )
316  {
317  ret.append( it.key() );
318  }
319 
320  ++it;
321  }
322 
323  return ret;
324 }
325 
329 void DictionaryManager::loadDictSettings( const QString &dictName, KConfigSkeleton *config )
330 {
331  DictFile *dict = this->makeDictFile( dictName );
332  if( dict != NULL )
333  {
334  config->setCurrentGroup( "dicts_" + dictName.toLower() );
335  dict->loadSettings( config );
336  }
337 }
338 
339 void DictionaryManager::loadSettings( const KConfig &config )
340 {
341  //TODO
342 }
343 
349 DictFile *DictionaryManager::makeDictFile( const QString &type )
350 {
351  if( type == EDICT )
352  {
353  return new DictFileEdict();
354  }
355  else if( type == KANJIDIC )
356  {
357  return new DictFileKanjidic();
358  }
359 
360  //Add new dictionary types here!!!
361 
362  return NULL;
363 }
364 
365 void DictionaryManager::removeAllDictionaries()
366 {
367  d->dictManagers.clear();
368 }
369 
376 bool DictionaryManager::removeDictionary( const QString &name )
377 {
378  DictFile *file = d->dictManagers.take( name );
379  delete file;
380  return true;
381 }
EntryList::setQuery
void setQuery(const DictQuery &newQuery)
Set the query for this list.
Definition: entrylist.cpp:331
QMutableHashIterator::hasNext
bool hasNext() const
QWidget
DictionaryManager::doSearchInList
EntryList * doSearchInList(const DictQuery &query, const EntryList *list) const
A simple method for searching inside of a given set of results.
Definition: dictionarymanager.cpp:198
DictionaryManager::DictionaryManager
DictionaryManager()
Basic constructor.
Definition: dictionarymanager.cpp:77
QMap::contains
bool contains(const Key &key) const
dictionarymanager.h
QHash::key
const Key key(const T &value) const
DictionaryPreferenceDialog
This abstract base class specifies the interface for dictionary preference dialogs in user applicatio...
Definition: dictionarypreferencedialog.h:42
DictionaryManager::generatePreferenceDialogs
static QMap< QString, DictionaryPreferenceDialog * > generatePreferenceDialogs(KConfigSkeleton *config, QWidget *parent=NULL)
Given a config and parent widget, return a mapping from dictionary types to preference dialogs...
Definition: dictionarymanager.cpp:239
QMutableHashIterator
DictFileKanjidic
Definition: dictfilekanjidic.h:39
Entry
The Entry class is a generic base class for each particular entry in a given dictionary.
Definition: entry.h:44
dictfilekanjidic.h
QMap::constBegin
const_iterator constBegin() const
QMap< QString, QString >
EDICT
#define EDICT
Definition: kitenmacros.h:24
dictfile.h
DictionaryManager::listDictFileTypes
static QStringList listDictFileTypes()
Get a list of all supported dictionary types.
Definition: dictionarymanager.cpp:277
DictQuery::getDictionaries
QStringList getDictionaries() const
Returns a list of the dictionaries that this particular query will target.
Definition: dictquery.cpp:583
DictFile::getSearchableAttributes
virtual const QMap< QString, QString > & getSearchableAttributes() const
Fetch a list of searchable attributes and their codes.
Definition: dictfile.h:144
DictFile::getName
virtual QString getName() const
Returns the name of the dictionary.
Definition: dictfile.h:132
Entry::loadEntry
virtual bool loadEntry(const QString &)=0
An entry should be able to parse an in-file representation of an entry as a QString and put it back...
DictionaryManager::addDictionary
bool addDictionary(const QString &file, const QString &name, const QString &type)
Open a specified dictionary, and load it under this manager's control.
Definition: dictionarymanager.cpp:104
Entry::matchesQuery
virtual bool matchesQuery(const DictQuery &) const
Fairly important method, this tests if this particular entry matches a query.
Definition: entry.cpp:294
KANJIDIC
#define KANJIDIC
Definition: kitenmacros.h:25
QList::count
int count(const T &value) const
QList::append
void append(const T &value)
QHash< QString, DictFile * >
entry.h
DictQuery::getMeaning
QString getMeaning() const
Accessor for the non-japanese meaning field.
Definition: dictquery.cpp:502
QList::isEmpty
bool isEmpty() const
QMap::constEnd
const_iterator constEnd() const
DictionaryManager::loadSettings
void loadSettings(const KConfig &config)
Load general settings.
Definition: dictionarymanager.cpp:339
QMutableHashIterator::next
Item next()
DictFile::loadSettings
virtual void loadSettings(KConfigSkeleton *)
Load information from the KConfigSkeleton that you've setup in the above preferences widget...
Definition: dictfile.h:127
DictFile::loadDictionary
virtual bool loadDictionary(const QString &file, const QString &name)=0
Load a dictionary as at system startup.
QString
EntryList::appendList
void appendList(const EntryList *other)
Append another EntryList onto this one.
Definition: entrylist.cpp:306
DictionaryManager::listDictionaryInfo
QPair< QString, QString > listDictionaryInfo(const QString &name) const
Returns type and file for an open dictionary of a given.
Definition: dictionarymanager.cpp:294
DictionaryManager::loadDictSettings
void loadDictSettings(const QString &dict, KConfigSkeleton *config)
Trigger loading preferences from a given KConfigSkeleton config object for a dictionary of type dict...
Definition: dictionarymanager.cpp:329
QStringList
QPair
Entry::clone
virtual Entry * clone() const =0
A clone method, this should just implement "return new EntrySubClass(*this)".
QString::toLower
QString toLower() const
QMutableHashIterator::remove
void remove()
QHash::value
const T value(const Key &key) const
QMutableHashIterator::value
T & value()
QMap::key
const Key key(const T &value) const
DictFile::doSearch
virtual EntryList * doSearch(const DictQuery &query)=0
This actually conducts the search on the given query.
DictQuery::toString
const QString toString() const
This returns a QString that represents the query.
Definition: dictquery.cpp:279
DictionaryManager::removeAllDictionaries
void removeAllDictionaries()
Removes all previously loaded dictionaries (if any).
Definition: dictionarymanager.cpp:365
DictionaryManager::removeDictionary
bool removeDictionary(const QString &name)
Close a dictionary by name.
Definition: dictionarymanager.cpp:376
DictionaryManager::~DictionaryManager
virtual ~DictionaryManager()
Basic destructor.
Definition: dictionarymanager.cpp:85
DictFile::preferencesWidget
virtual DictionaryPreferenceDialog * preferencesWidget(KConfigSkeleton *config, QWidget *parent=NULL)
If you want your own dialog to pick preferences for your dict, then override this.
Definition: dictfile.h:121
Entry::sort
virtual bool sort(const Entry &that, const QStringList &dictionaryList, const QStringList &fieldList) const
An overrideable sorting function, similer to operator< in most contexts The default version will sort...
Definition: entry.cpp:425
DictQuery
A class to allow users of libkiten to properly setup a database query.
Definition: dictquery.h:89
kitenmacros.h
DictionaryManager::doSearch
EntryList * doSearch(const DictQuery &query) const
This is the main search routine that most of kiten should use.
Definition: dictionarymanager.cpp:138
DictQuery::getMatchType
MatchType getMatchType() const
Get which match type is currently set on the DictQuery.
Definition: dictquery.cpp:606
Entry::dumpEntry
virtual QString dumpEntry() const =0
Return a QString of an entry, as if it were dumped back into it's source file.
DictFileEdict
Definition: dictfileedict.h:49
DictionaryManager::listDictionariesOfType
QStringList listDictionariesOfType(const QString &type) const
Lists all dictionaries of a given type (Convenient for preference dialogs)
Definition: dictionarymanager.cpp:309
dictquery.h
QMap::insert
iterator insert(const Key &key, const T &value)
DictFile
Abstract base class, used internally by the library for handling different types of dictionaries This...
Definition: dictfile.h:47
dictfileedict.h
DictionaryManager::listDictionaries
QStringList listDictionaries() const
List names of each open dictionary.
Definition: dictionarymanager.cpp:261
DictionaryManager::generateExtendedFieldsList
static QMap< QString, QString > generateExtendedFieldsList()
Compiles a list of all fields beyond the basic three (word/pronunciation/meaning) that all dictionary...
Definition: dictionarymanager.cpp:215
dictionarypreferencedialog.h
EntryList
EntryList is a simple container for Entry objects, and is-a QList A few simple overrides allo...
Definition: entrylist.h:38
entrylist.h
EntryList::getQuery
DictQuery getQuery() const
Get the query that generated this list, note that if you have appended EntryLists from two different ...
Definition: entrylist.cpp:323
QMap::value
const T value(const Key &key) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:16:38 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kiten/lib

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

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal