2012-02-20 21:47:14 +00:00
|
|
|
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
|
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>
|
2010-07-05 18:36:03 +00:00
|
|
|
#include <QBuffer>
|
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,
|
2022-07-08 13:47:09 +00:00
|
|
|
std::vector< sptr< Dictionary::Class > > const & allDictionaries,
|
2018-03-28 14:21:32 +00:00
|
|
|
Config::Group const & inactiveGroup ):
|
2009-04-10 12:48:40 +00:00
|
|
|
id( cfgGroup.id ),
|
2009-01-28 20:55:45 +00:00
|
|
|
name( cfgGroup.name ),
|
2010-07-05 14:13:29 +00:00
|
|
|
icon( cfgGroup.icon ),
|
2017-05-05 14:39:51 +00:00
|
|
|
favoritesFolder( cfgGroup.favoritesFolder ),
|
2010-07-05 14:13:29 +00:00
|
|
|
shortcut( cfgGroup.shortcut )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2010-07-05 18:36:03 +00:00
|
|
|
if ( !cfgGroup.iconData.isEmpty() )
|
|
|
|
iconData = iconFromData( cfgGroup.iconData );
|
|
|
|
|
2022-03-13 16:17:34 +00:00
|
|
|
QMap<string, sptr< Dictionary::Class > > groupDicts;
|
2022-03-14 15:18:53 +00:00
|
|
|
QVector<string> dictOrderList;
|
2022-07-08 13:47:09 +00:00
|
|
|
auto dictMap = Dictionary::dictToMap( allDictionaries );
|
2018-03-28 14:21:32 +00:00
|
|
|
|
2023-05-13 00:44:17 +00:00
|
|
|
for( auto const & dict: cfgGroup.dictionaries )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2023-05-13 00:44:17 +00:00
|
|
|
std::string dictId = dict.id.toStdString();
|
2009-04-01 12:00:28 +00:00
|
|
|
|
2023-05-13 00:44:17 +00:00
|
|
|
if( dictMap.contains( dictId ) )
|
2022-07-08 13:47:09 +00:00
|
|
|
{
|
2023-05-13 00:44:17 +00:00
|
|
|
groupDicts.insert( dictId, dictMap[ dictId ] );
|
|
|
|
dictOrderList.push_back(dictId);
|
2009-04-01 12:00:28 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2018-03-28 14:21:32 +00:00
|
|
|
|
|
|
|
// Remove inactive dictionaries
|
2022-03-13 16:17:34 +00:00
|
|
|
if( !inactiveGroup.dictionaries.isEmpty() )
|
2018-03-28 14:21:32 +00:00
|
|
|
{
|
2023-05-13 00:44:17 +00:00
|
|
|
set< string, std::less<> > inactiveSet;
|
|
|
|
for(auto const & dict: inactiveGroup.dictionaries)
|
2022-03-13 16:17:34 +00:00
|
|
|
{
|
2023-05-13 00:44:17 +00:00
|
|
|
string dictId = dict.id.toStdString();
|
|
|
|
groupDicts.remove(dictId);
|
|
|
|
dictOrderList.removeOne(dictId);
|
2022-03-13 16:17:34 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-14 15:18:53 +00:00
|
|
|
for(const auto & dictId : dictOrderList)
|
2022-03-13 16:17:34 +00:00
|
|
|
{
|
2022-03-14 15:18:53 +00:00
|
|
|
dictionaries.push_back(groupDicts[dictId]);
|
2018-03-28 14:21:32 +00:00
|
|
|
}
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-05-12 02:57:57 +00:00
|
|
|
Group::Group( unsigned id_, QString const & name_ ):
|
|
|
|
id( id_ ),
|
|
|
|
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;
|
2010-07-05 14:13:29 +00:00
|
|
|
result.shortcut = shortcut;
|
2017-05-05 14:39:51 +00:00
|
|
|
result.favoritesFolder = favoritesFolder;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2010-07-05 18:36:03 +00:00
|
|
|
if ( !iconData.isNull() )
|
|
|
|
{
|
|
|
|
QDataStream stream( &result.iconData, QIODevice::WriteOnly );
|
|
|
|
|
|
|
|
stream << iconData;
|
|
|
|
}
|
|
|
|
|
2023-05-13 00:44:17 +00:00
|
|
|
for ( auto const & dict : dictionaries ) {
|
2009-04-01 12:00:28 +00:00
|
|
|
result.dictionaries.push_back(
|
2023-05-13 00:44:17 +00:00
|
|
|
Config::DictionaryRef( dict->getId().c_str(), QString::fromUtf8( dict->getName().c_str() ) ) );
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-10-21 19:28:09 +00:00
|
|
|
QIcon Group::makeIcon() const
|
|
|
|
{
|
2010-07-05 18:36:03 +00:00
|
|
|
if ( !iconData.isNull() )
|
|
|
|
return iconData;
|
|
|
|
|
2009-10-21 19:28:09 +00:00
|
|
|
QIcon i = icon.size() ?
|
|
|
|
QIcon( ":/flags/" + icon ) : QIcon();
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2012-09-26 13:13:47 +00:00
|
|
|
void Group::checkMutedDictionaries( Config::MutedDictionaries * mutedDictionaries ) const
|
|
|
|
{
|
|
|
|
Config::MutedDictionaries temp;
|
|
|
|
|
2023-05-13 00:44:17 +00:00
|
|
|
for ( auto const & dict : dictionaries )
|
2012-09-26 13:13:47 +00:00
|
|
|
{
|
2023-05-13 00:44:17 +00:00
|
|
|
QString id = QString::fromStdString( dict->getId() );
|
2012-09-26 13:13:47 +00:00
|
|
|
if( mutedDictionaries->contains( id ) )
|
|
|
|
temp.insert( id );
|
|
|
|
}
|
|
|
|
* mutedDictionaries = temp;
|
|
|
|
}
|
|
|
|
|
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 ) );
|
|
|
|
|
2023-05-13 00:44:17 +00:00
|
|
|
return nullptr;
|
2009-05-14 19:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Group const * Groups::findGroup( unsigned id ) const
|
|
|
|
{
|
|
|
|
for( unsigned x = 0; x < size(); ++x )
|
|
|
|
if ( operator [] ( x ).id == id )
|
|
|
|
return &( operator [] ( x ) );
|
|
|
|
|
2023-05-13 00:44:17 +00:00
|
|
|
return nullptr;
|
2009-05-14 19:27:19 +00:00
|
|
|
}
|
|
|
|
|
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 )
|
|
|
|
{
|
2023-05-13 00:44:17 +00:00
|
|
|
set< string, std::less<> > presentIds;
|
2009-05-18 18:01:50 +00:00
|
|
|
|
|
|
|
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 )
|
|
|
|
{
|
2012-12-10 12:49:45 +00:00
|
|
|
for( int x = 0; x < groups.size(); ++x )
|
2009-05-18 18:01:50 +00:00
|
|
|
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
|
|
|
|
2010-07-05 18:36:03 +00:00
|
|
|
QIcon iconFromData( QByteArray const & iconData )
|
|
|
|
{
|
|
|
|
QDataStream stream( iconData );
|
|
|
|
|
|
|
|
QIcon result;
|
|
|
|
|
|
|
|
stream >> result;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|