1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
/*****************************************************************************
* This file is part of Kiten, a KDE Japanese Reference Tool *
* Copyright (C) 2006 Joseph Kerian <jkerian@gmail.com> *
* Copyright (C) 2011 Daniel E. Moctezuma <democtezuma@gmail.com> *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public License *
* along with this library; see the file COPYING.LIB. If not, write to *
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
* Boston, MA 02110-1301, USA. *
*****************************************************************************/
#include "dictionarymanager.h"
#include "dictfile.h"
#include "dictionarypreferencedialog.h"
#include "dictquery.h"
#include "entry.h"
#include "entrylist.h"
#include "kitenmacros.h"
#include <KDebug>
#include <KGlobal>
#include <KConfig>
#include <KConfigSkeleton>
#include <QFile>
#include <QString>
/* Includes to handle various types of dictionaries
IMPORTANT: To add a dictionary type, add the header file here and add it to the
if statement under addDictionary() */
#include "DictEdict/dictfileedict.h"
#include "DictKanjidic/dictfilekanjidic.h"
class DictionaryManager::Private
{
public:
/**
* List of dictionaries, indexed by name
*/
QHash<QString,DictFile*> dictManagers;
};
#if 0
class debug_entry : public Entry
{
public:
debug_entry(QString word) : Entry( QString( "libkiten" ), word
, QStringList(), QStringList() ), count( counter++ )
{ }
virtual Entry * clone() const { return new debug_entry( *this ); }
virtual bool loadEntry( const QString& ) { return false; }
virtual QString dumpEntry() const { return ""; }
virtual bool sort( const debug_entry &that, const QStringList &dicts,
const QStringList &fields )
{ return this->count < that.count; }
int count;
static int counter;
};
int debug_entry::counter = 0;
#endif
/**
* The constructor. Set autodelete on our dictionary list
*/
DictionaryManager::DictionaryManager()
: d( new Private )
{
}
/**
* Delete everything in our hash
*/
DictionaryManager::~DictionaryManager()
{
{
QMutableHashIterator<QString, DictFile*> it( d->dictManagers );
while( it.hasNext() )
{
it.next();
delete it.value();
it.remove();
}
}
delete d;
}
/**
* Given a named Dict file/name/type... create and add the object if it
* seems to work properly on creation.
*/
bool DictionaryManager::addDictionary( const QString &file
, const QString &name
, const QString &type )
{
if( d->dictManagers.contains( name ) ) //This name already exists in the list!
{
return false;
}
DictFile *newDict = makeDictFile( type );
if( newDict == NULL )
{
return false;
}
if( ! newDict->loadDictionary( file, name ) )
{
kDebug() << "Dictionary load FAILED: " << newDict->getName();
delete newDict;
return false;
}
kDebug() << "Dictionary Loaded : " << newDict->getName();
d->dictManagers.insert( name, newDict );
return true;
}
/**
* Examine the DictQuery and farm out the search to the specialized dict
* managers. Note that a global search limit will probably be implemented
* either here or in the DictFile implementations... probably both
*
* @param query the query, see DictQuery documentation
*/
EntryList *DictionaryManager::doSearch( const DictQuery &query ) const
{
EntryList *ret = new EntryList();
#if 0
if( query.getMeaning() == "(libkiten)" )
{
ret->append( new debug_entry( "Summary of libkiten data" ) );
foreach( const QString &dict, listDictionaries() )
{
ret->append( new debug_entry( dict ) );
}
return ret;
}
#endif
// There are two basic modes.... one in which the query
// specifies the dictionary list, one in which it does not
QStringList dictsFromQuery = query.getDictionaries();
if( dictsFromQuery.isEmpty() )
{
// None specified, search all
foreach( DictFile *it, d->dictManagers )
{
kDebug() << "Searching in " << it->getName() << "dictionary." << endl;
EntryList *temp = it->doSearch( query );
if( temp )
{
ret->appendList( temp );
}
delete temp;
}
}
else
{
foreach( const QString &target, dictsFromQuery )
{
DictFile *newestFound = d->dictManagers.find( target ).value();
if( newestFound != 0 )
{
EntryList *temp = newestFound->doSearch( query );
if( temp )
{
ret->appendList( temp );
}
delete temp;
}
}
}
ret->setQuery( query ); //Store the query for later use.
kDebug() << "From query: '" << query.toString() << "' Found " << ret->count() << " results";
kDebug() << "Incoming match type: " << query.getMatchType() << " Outgoing: " << ret->getQuery().getMatchType();
return ret;
}
/**
* For this case, we let polymorphism do most of the work. We assume that the user wants
* to pare down the results, so we let the individual entry metching methods run over the
* new query and accept (and copy) any of those that pass.
*/
EntryList *DictionaryManager::doSearchInList( const DictQuery &query, const EntryList *list ) const
{
EntryList *ret = new EntryList();
foreach( Entry* it, *list )
{
if( it->matchesQuery( query ) )
{
Entry *x = it->clone();
ret->append( x );
}
}
ret->setQuery( query + list->getQuery() );
return ret;
}
QMap<QString, QString> DictionaryManager::generateExtendedFieldsList()
{
QMap<QString,QString> result;
QStringList dictTypes = listDictFileTypes();
foreach( const QString &dictType, dictTypes )
{
DictFile *tempDictFile = makeDictFile( dictType );
QMap<QString,QString> tempList = tempDictFile->getSearchableAttributes();
QMap<QString,QString>::const_iterator it = tempList.constBegin();
while( it != tempList.constEnd() )
{
if( ! result.contains( it.key() ) )
{
result.insert( it.key(), it.value() );
}
++it;
}
delete tempDictFile;
}
return result;
}
QMap<QString,DictionaryPreferenceDialog*>
DictionaryManager::generatePreferenceDialogs( KConfigSkeleton *config, QWidget *parent )
{
QMap<QString,DictionaryPreferenceDialog*> result;
QStringList dictTypes = listDictFileTypes();
foreach( const QString &dictType, dictTypes )
{
DictFile *tempDictFile = makeDictFile( dictType );
DictionaryPreferenceDialog *newDialog = tempDictFile->preferencesWidget( config, parent );
if( newDialog == NULL ) continue;
result.insert( dictType, newDialog );
delete tempDictFile;
}
return result;
}
/**
* Return a list of the dictionaries by their name (our key)
* Note that this dictionary name does not necessarily have to have anything
* to do with the actual dictionary name...
*/
QStringList DictionaryManager::listDictionaries() const
{
QStringList ret;
foreach( DictFile *it, d->dictManagers )
{
ret.append( it->getName() );
}
return ret;
}
/**
* IMPORTANT: To add a dictionary type, you have to manually add the creation
* step here, the prev method, and \#include your header file above. If you have
* fully implemented the interface in DictionaryManager.h, It should simply work.
*/
QStringList DictionaryManager::listDictFileTypes()
{
QStringList list;
list.append( EDICT );
list.append( KANJIDIC );
//Add your dictionary type here!
return list;
}
/**
* Return the dictionary type and file used by a named dictionary.
* returns a pair of empty QStrings if you specify an invalid name
*
* @param name the name of the dictionary, as given in the addDictionary method
*/
QPair<QString, QString> DictionaryManager::listDictionaryInfo( const QString &name ) const
{
if( ! d->dictManagers.contains( name ) ) //This name not in list!
{
return qMakePair( QString(), QString() );
}
return qMakePair( d->dictManagers[ name ]->getName(), d->dictManagers[ name ]->getFile() );
}
/**
* Return a list of the names of each dictionary of a given type.
*
* @param type the type of the dictionary we want a list of
*/
QStringList DictionaryManager::listDictionariesOfType( const QString &type ) const
{
QStringList ret;
QHash<QString, DictFile*>::const_iterator it = d->dictManagers.constBegin();
while( it != d->dictManagers.constEnd() )
{
if( it.value()->getType() == type )
{
ret.append( it.key() );
}
++it;
}
return ret;
}
/**
* Load preference settings for a particular dictionary
*/
void DictionaryManager::loadDictSettings( const QString &dictName, KConfigSkeleton *config )
{
DictFile *dict = this->makeDictFile( dictName );
if( dict != NULL )
{
config->setCurrentGroup( "dicts_" + dictName.toLower() );
dict->loadSettings( config );
}
}
void DictionaryManager::loadSettings( const KConfig &config )
{
//TODO
}
/**
* IMPORTANT: To add a dictionary type, you have to manually add the creation
* step here, the next method, and \#include your header file above. If you have
* fully implemented the interface in dictionarymanager.h, It should simply work.
*/
DictFile *DictionaryManager::makeDictFile( const QString &type )
{
if( type == EDICT )
{
return new DictFileEdict();
}
else if( type == KANJIDIC )
{
return new DictFileKanjidic();
}
//Add new dictionary types here!!!
return NULL;
}
void DictionaryManager::removeAllDictionaries()
{
d->dictManagers.clear();
}
/**
* Remove a dictionary from the list, and delete the dictionary object
* (it should close files, deallocate memory, etc).
*
* @param name the name of the dictionary, as given in the addDictionary method
*/
bool DictionaryManager::removeDictionary( const QString &name )
{
DictFile *file = d->dictManagers.take( name );
delete file;
return true;
}
|