File size: 4,851 Bytes
b6bd94d | 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 | /* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "instances.hh"
#include <set>
#include <QBuffer>
#include <utility>
namespace Instances {
using std::set;
using std::string;
Group::Group( const Config::Group & cfgGroup,
const std::vector< sptr< Dictionary::Class > > & allDictionaries,
const Config::Group & inactiveGroup ):
id( cfgGroup.id ),
name( cfgGroup.name ),
icon( cfgGroup.icon ),
favoritesFolder( cfgGroup.favoritesFolder ),
shortcut( cfgGroup.shortcut )
{
if ( !cfgGroup.iconData.isEmpty() ) {
iconData = iconFromData( cfgGroup.iconData );
}
QMap< string, sptr< Dictionary::Class > > groupDicts;
QList< string > dictOrderList;
auto dictMap = Dictionary::dictToMap( allDictionaries );
for ( const auto & dict : cfgGroup.dictionaries ) {
const std::string dictId = dict.id.toStdString();
//avoid duplicate dictionary in groups in config file.
if ( dictMap.contains( dictId ) && !dictOrderList.contains( dictId ) ) {
groupDicts.insert( dictId, dictMap[ dictId ] );
dictOrderList.push_back( dictId );
}
}
// Remove inactive dictionaries
if ( !inactiveGroup.dictionaries.isEmpty() ) {
for ( const auto & dict : inactiveGroup.dictionaries ) {
const string dictId = dict.id.toStdString();
groupDicts.remove( dictId );
dictOrderList.removeOne( dictId );
}
}
for ( const auto & dictId : dictOrderList ) {
if ( groupDicts.contains( dictId ) ) {
dictionaries.push_back( groupDicts[ dictId ] );
}
}
}
Group::Group():
id( 0 )
{
}
Group::Group( unsigned id_, QString name_ ):
id( id_ ),
name( std::move( name_ ) )
{
}
Config::Group Group::makeConfigGroup()
{
Config::Group result;
result.id = id;
result.name = name;
result.icon = icon;
result.shortcut = shortcut;
result.favoritesFolder = favoritesFolder;
if ( !iconData.isNull() ) {
QDataStream stream( &result.iconData, QIODevice::WriteOnly );
stream << iconData;
}
for ( const auto & dict : dictionaries ) {
result.dictionaries.push_back(
Config::DictionaryRef( dict->getId().c_str(), QString::fromUtf8( dict->getName().c_str() ) ) );
}
return result;
}
QIcon Group::makeIcon() const
{
if ( !iconData.isNull() ) {
return iconData;
}
QIcon i = icon.size() ? QIcon( ":/flags/" + icon ) : QIcon();
return i;
}
void Group::checkMutedDictionaries( Config::DictionarySets * mutedDictionaries ) const
{
Config::DictionarySets temp;
for ( const auto & dict : dictionaries ) {
auto dictId = QString::fromStdString( dict->getId() );
if ( mutedDictionaries->contains( dictId ) ) {
temp.insert( dictId );
}
}
*mutedDictionaries = temp;
}
const Group * Groups::findGroup( unsigned id ) const
{
for ( unsigned x = 0; x < size(); ++x ) {
if ( operator[]( x ).id == id ) {
return &( operator[]( x ) );
}
}
return nullptr;
}
void complementDictionaryOrder( Group & group,
const Group & inactiveDictionaries,
const vector< sptr< Dictionary::Class > > & dicts )
{
set< string, std::less<> > presentIds;
for ( unsigned x = group.dictionaries.size(); x--; ) {
presentIds.insert( group.dictionaries[ x ]->getId() );
}
for ( unsigned x = inactiveDictionaries.dictionaries.size(); x--; ) {
presentIds.insert( inactiveDictionaries.dictionaries[ x ]->getId() );
}
for ( const auto & dict : dicts ) {
if ( presentIds.find( dict->getId() ) == presentIds.end() ) {
group.dictionaries.push_back( dict );
}
}
}
void updateNames( Config::Group & group, const vector< sptr< Dictionary::Class > > & allDictionaries )
{
for ( unsigned x = group.dictionaries.size(); x--; ) {
const 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;
}
}
}
}
void updateNames( Config::Groups & groups, const vector< sptr< Dictionary::Class > > & allDictionaries )
{
for ( auto & group : groups ) {
updateNames( group, allDictionaries );
}
}
void updateNames( Config::Class & cfg, const vector< sptr< Dictionary::Class > > & allDictionaries )
{
updateNames( cfg.dictionaryOrder, allDictionaries );
updateNames( cfg.inactiveDictionaries, allDictionaries );
updateNames( cfg.groups, allDictionaries );
}
QIcon iconFromData( const QByteArray & iconData )
{
QDataStream stream( iconData );
QIcon result;
stream >> result;
return result;
}
} // namespace Instances
|