2009-02-05 14:21:47 +00:00
|
|
|
/* This file is (c) 2008-2009 Konstantin Isakov <ikm@users.berlios.de>
|
2009-01-28 20:55:45 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include "instances.hh"
|
2009-05-18 18:01:50 +00:00
|
|
|
#include <set>
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
namespace Instances {
|
|
|
|
|
2009-05-18 18:01:50 +00:00
|
|
|
using std::set;
|
|
|
|
using std::string;
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
Group::Group( Config::Group const & cfgGroup,
|
|
|
|
vector< sptr< Dictionary::Class > > const & allDictionaries ):
|
2009-04-10 12:48:40 +00:00
|
|
|
id( cfgGroup.id ),
|
2009-01-28 20:55:45 +00:00
|
|
|
name( cfgGroup.name ),
|
|
|
|
icon( cfgGroup.icon )
|
|
|
|
{
|
|
|
|
for( unsigned x = 0; x < cfgGroup.dictionaries.size(); ++x )
|
|
|
|
{
|
2009-04-01 12:00:28 +00:00
|
|
|
std::string id = cfgGroup.dictionaries[ x ].id.toStdString();
|
|
|
|
|
|
|
|
bool added = false;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
for( unsigned y = allDictionaries.size(); y--; )
|
|
|
|
if ( allDictionaries[ y ]->getId() == id )
|
|
|
|
{
|
|
|
|
dictionaries.push_back( allDictionaries[ y ] );
|
2009-04-01 12:00:28 +00:00
|
|
|
added = true;
|
2009-01-28 20:55:45 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-04-01 12:00:28 +00:00
|
|
|
|
|
|
|
if ( !added )
|
|
|
|
{
|
|
|
|
// Try matching by name instead
|
|
|
|
std::string name = cfgGroup.dictionaries[ x ].name.toUtf8().data();
|
|
|
|
|
|
|
|
if ( name.size() )
|
|
|
|
for( unsigned y = 0; y < allDictionaries.size(); ++y )
|
|
|
|
if ( allDictionaries[ y ]->getName() == name )
|
|
|
|
{
|
|
|
|
dictionaries.push_back( allDictionaries[ y ] );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-18 18:01:50 +00:00
|
|
|
Group::Group( QString const & name_ ): id( 0 ), name( name_ )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Config::Group Group::makeConfigGroup()
|
|
|
|
{
|
|
|
|
Config::Group result;
|
|
|
|
|
2009-04-10 12:48:40 +00:00
|
|
|
result.id = id;
|
2009-01-28 20:55:45 +00:00
|
|
|
result.name = name;
|
|
|
|
result.icon = icon;
|
|
|
|
|
|
|
|
for( unsigned x = 0; x < dictionaries.size(); ++x )
|
2009-04-01 12:00:28 +00:00
|
|
|
result.dictionaries.push_back(
|
|
|
|
Config::DictionaryRef( dictionaries[ x ]->getId().c_str(),
|
|
|
|
QString::fromUtf8( dictionaries[ x ]->getName().c_str() ) ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-05-14 19:27:19 +00:00
|
|
|
Group * Groups::findGroup( unsigned id )
|
|
|
|
{
|
|
|
|
for( unsigned x = 0; x < size(); ++x )
|
|
|
|
if ( operator [] ( x ).id == id )
|
|
|
|
return &( operator [] ( x ) );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Group const * Groups::findGroup( unsigned id ) const
|
|
|
|
{
|
|
|
|
for( unsigned x = 0; x < size(); ++x )
|
|
|
|
if ( operator [] ( x ).id == id )
|
|
|
|
return &( operator [] ( x ) );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-23 14:19:57 +00:00
|
|
|
void complementDictionaryOrder( Group & group,
|
|
|
|
Group const & inactiveDictionaries,
|
2009-05-18 18:01:50 +00:00
|
|
|
vector< sptr< Dictionary::Class > > const & dicts )
|
|
|
|
{
|
|
|
|
set< string > presentIds;
|
|
|
|
|
|
|
|
for( unsigned x = group.dictionaries.size(); x--; )
|
2009-05-23 14:19:57 +00:00
|
|
|
presentIds.insert( group.dictionaries[ x ]->getId());
|
2009-05-18 18:01:50 +00:00
|
|
|
|
|
|
|
for( unsigned x = inactiveDictionaries.dictionaries.size(); x--; )
|
2009-05-23 14:19:57 +00:00
|
|
|
presentIds.insert( inactiveDictionaries.dictionaries[ x ]->getId() );
|
2009-05-18 18:01:50 +00:00
|
|
|
|
|
|
|
for( unsigned x = 0; x < dicts.size(); ++x )
|
|
|
|
{
|
|
|
|
if ( presentIds.find( dicts[ x ]->getId() ) == presentIds.end() )
|
2009-05-23 14:19:57 +00:00
|
|
|
group.dictionaries.push_back( dicts[ x ] );
|
2009-05-18 18:01:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-01 12:00:28 +00:00
|
|
|
void updateNames( Config::Group & group,
|
|
|
|
vector< sptr< Dictionary::Class > > const & allDictionaries )
|
|
|
|
{
|
|
|
|
|
|
|
|
for( unsigned x = group.dictionaries.size(); x--; )
|
|
|
|
{
|
|
|
|
std::string id = group.dictionaries[ x ].id.toStdString();
|
|
|
|
|
|
|
|
for( unsigned y = allDictionaries.size(); y--; )
|
|
|
|
if ( allDictionaries[ y ]->getId() == id )
|
|
|
|
{
|
|
|
|
group.dictionaries[ x ].name = QString::fromUtf8( allDictionaries[ y ]->getName().c_str() );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-18 18:01:50 +00:00
|
|
|
void updateNames( Config::Groups & groups,
|
|
|
|
vector< sptr< Dictionary::Class > > const & allDictionaries )
|
|
|
|
{
|
|
|
|
for( unsigned x = 0; x < groups.size(); ++x )
|
|
|
|
updateNames( groups[ x ], allDictionaries );
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateNames( Config::Class & cfg,
|
|
|
|
vector< sptr< Dictionary::Class > > const & allDictionaries )
|
|
|
|
{
|
|
|
|
updateNames( cfg.dictionaryOrder, allDictionaries );
|
|
|
|
updateNames( cfg.inactiveDictionaries, allDictionaries );
|
|
|
|
updateNames( cfg.groups, allDictionaries );
|
|
|
|
}
|
2009-04-01 12:00:28 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|