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>
|
2024-01-22 09:04:30 +00:00
|
|
|
#include <utility>
|
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 );
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2010-07-05 18:36:03 +00:00
|
|
|
|
2022-03-13 16:17:34 +00:00
|
|
|
QMap< string, sptr< Dictionary::Class > > groupDicts;
|
2022-03-14 15:18:53 +00:00
|
|
|
QList< 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 ) {
|
2024-01-22 09:04:30 +00:00
|
|
|
std::string const dictId = dict.id.toStdString();
|
2009-04-01 12:00:28 +00:00
|
|
|
|
2024-11-01 02:25:29 +00:00
|
|
|
//avoid duplicate dictionary in groups in config file.
|
|
|
|
if ( dictMap.contains( dictId ) && !dictOrderList.contains( dictId ) ) {
|
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() ) {
|
2023-05-13 00:44:17 +00:00
|
|
|
for ( auto const & dict : inactiveGroup.dictionaries ) {
|
2024-01-22 09:04:30 +00:00
|
|
|
string const dictId = dict.id.toStdString();
|
2023-05-13 00:44:17 +00:00
|
|
|
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 ) {
|
2024-01-04 12:23:05 +00:00
|
|
|
if ( groupDicts.contains( dictId ) ) {
|
|
|
|
dictionaries.push_back( groupDicts[ dictId ] );
|
|
|
|
}
|
2018-03-28 14:21:32 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2024-01-22 09:04:30 +00:00
|
|
|
Group::Group( QString name_ ):
|
2009-05-18 18:01:50 +00:00
|
|
|
id( 0 ),
|
2024-01-22 09:04:30 +00:00
|
|
|
name( std::move( name_ ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-01-22 09:04:30 +00:00
|
|
|
Group::Group( unsigned id_, QString name_ ):
|
2023-05-12 02:57:57 +00:00
|
|
|
id( id_ ),
|
2024-01-22 09:04:30 +00:00
|
|
|
name( std::move( name_ ) )
|
2023-05-12 02:57:57 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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;
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2010-07-05 18:36:03 +00:00
|
|
|
|
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 ) {
|
2024-01-22 09:04:30 +00:00
|
|
|
auto dictId = QString::fromStdString( dict->getId() );
|
|
|
|
if ( mutedDictionaries->contains( dictId ) ) {
|
|
|
|
temp.insert( dictId );
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2012-09-26 13:13:47 +00:00
|
|
|
}
|
|
|
|
*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 ) );
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
|
|
|
}
|
2009-05-14 19:27:19 +00:00
|
|
|
|
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 ) );
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
|
|
|
}
|
2009-05-14 19:27:19 +00:00
|
|
|
|
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() );
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
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() );
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2009-05-18 18:01:50 +00:00
|
|
|
|
2024-01-22 09:04:30 +00:00
|
|
|
for ( const auto & dict : dicts ) {
|
|
|
|
if ( presentIds.find( dict->getId() ) == presentIds.end() ) {
|
|
|
|
group.dictionaries.push_back( dict );
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
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--; ) {
|
2024-01-22 09:04:30 +00:00
|
|
|
std::string const id = group.dictionaries[ x ].id.toStdString();
|
2009-04-01 12:00:28 +00:00
|
|
|
|
|
|
|
for ( unsigned y = allDictionaries.size(); y--; ) {
|
|
|
|
if ( allDictionaries[ y ]->getId() == id ) {
|
|
|
|
group.dictionaries[ x ].name = QString::fromUtf8( allDictionaries[ y ]->getName().c_str() );
|
|
|
|
break;
|
|
|
|
}
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2009-04-01 12:00:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-18 18:01:50 +00:00
|
|
|
void updateNames( Config::Groups & groups, vector< sptr< Dictionary::Class > > const & allDictionaries )
|
|
|
|
{
|
2024-01-22 09:04:30 +00:00
|
|
|
for ( auto & group : groups ) {
|
|
|
|
updateNames( group, allDictionaries );
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2009-05-18 18:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
} // namespace Instances
|