mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-12-17 23:04:06 +00:00
+ Make groups resistant to losing dictionaries when they got moved or
otherwise change their ids by saving their names, too, and falling back to matching each group dictionary against name instead of id when no ids match.
This commit is contained in:
parent
4a99ce281f
commit
c32a12afb9
|
@ -142,7 +142,8 @@ Class load() throw( exError )
|
|||
QDomNodeList dicts = grp.elementsByTagName( "dictionary" );
|
||||
|
||||
for( unsigned y = 0; y < dicts.length(); ++y )
|
||||
g.dictionaries.push_back( dicts.item( y ).toElement().text() );
|
||||
g.dictionaries.push_back( DictionaryRef( dicts.item( y ).toElement().text(),
|
||||
dicts.item( y ).toElement().attribute( "name" ) ) );
|
||||
|
||||
c.groups.push_back( g );
|
||||
}
|
||||
|
@ -258,15 +259,21 @@ void save( Class const & c ) throw( exError )
|
|||
group.setAttributeNode( icon );
|
||||
}
|
||||
|
||||
for( vector< QString >::const_iterator j = i->dictionaries.begin(); j != i->dictionaries.end(); ++j )
|
||||
for( vector< DictionaryRef >::const_iterator j = i->dictionaries.begin(); j != i->dictionaries.end(); ++j )
|
||||
{
|
||||
QDomElement dictionary = dd.createElement( "dictionary" );
|
||||
|
||||
group.appendChild( dictionary );
|
||||
|
||||
QDomText value = dd.createTextNode( *j );
|
||||
QDomText value = dd.createTextNode( j->id );
|
||||
|
||||
dictionary.appendChild( value );
|
||||
|
||||
QDomAttr name = dd.createAttribute( "name" );
|
||||
|
||||
name.setValue( j->name );
|
||||
|
||||
dictionary.setAttributeNode( name );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,11 +28,23 @@ struct Path
|
|||
/// A list of paths where to search for the dictionaries
|
||||
typedef vector< Path > Paths;
|
||||
|
||||
struct DictionaryRef
|
||||
{
|
||||
QString id; // Dictionrary id, which is usually an md5 hash
|
||||
QString name; // Dictionary name, used to recover when its id changes
|
||||
|
||||
DictionaryRef()
|
||||
{}
|
||||
|
||||
DictionaryRef( QString const & id_, QString const & name_ ):
|
||||
id( id_ ), name( name_ ) {}
|
||||
};
|
||||
|
||||
/// A dictionary group
|
||||
struct Group
|
||||
{
|
||||
QString name, icon;
|
||||
vector< QString > dictionaries; // consists of dictionary's ids
|
||||
vector< DictionaryRef > dictionaries;
|
||||
};
|
||||
|
||||
/// All the groups
|
||||
|
|
|
@ -150,7 +150,7 @@ bool DictListModel::setData( QModelIndex const & index, const QVariant & value,
|
|||
{
|
||||
Config::Group g;
|
||||
|
||||
g.dictionaries.push_back( value.toString() );
|
||||
g.dictionaries.push_back( Config::DictionaryRef( value.toString(), QString() ) );
|
||||
|
||||
Instances::Group i( g, *allDicts );
|
||||
|
||||
|
|
|
@ -12,14 +12,31 @@ Group::Group( Config::Group const & cfgGroup,
|
|||
{
|
||||
for( unsigned x = 0; x < cfgGroup.dictionaries.size(); ++x )
|
||||
{
|
||||
std::string id = cfgGroup.dictionaries[ x ].toStdString();
|
||||
std::string id = cfgGroup.dictionaries[ x ].id.toStdString();
|
||||
|
||||
bool added = false;
|
||||
|
||||
for( unsigned y = allDictionaries.size(); y--; )
|
||||
if ( allDictionaries[ y ]->getId() == id )
|
||||
{
|
||||
dictionaries.push_back( allDictionaries[ y ] );
|
||||
added = true;
|
||||
break;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -35,9 +52,29 @@ Config::Group Group::makeConfigGroup()
|
|||
result.icon = icon;
|
||||
|
||||
for( unsigned x = 0; x < dictionaries.size(); ++x )
|
||||
result.dictionaries.push_back( dictionaries[ x ]->getId().c_str() );
|
||||
result.dictionaries.push_back(
|
||||
Config::DictionaryRef( dictionaries[ x ]->getId().c_str(),
|
||||
QString::fromUtf8( dictionaries[ x ]->getName().c_str() ) ) );
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -36,6 +36,12 @@ struct Group
|
|||
|
||||
typedef vector< Group > Groups;
|
||||
|
||||
/// For any dictionaries present in the group, updates their names to match
|
||||
/// the dictionaries they refer to in their current form, if they exist.
|
||||
/// If the dictionary instance can't be located, the name is left untouched.
|
||||
void updateNames( Config::Group &,
|
||||
vector< sptr< Dictionary::Class > > const & allDictionaries );
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -392,8 +392,14 @@ void MainWindow::updateGroupList()
|
|||
groupInstances.clear();
|
||||
|
||||
for( unsigned x = 0; x < cfg.groups.size(); ++x )
|
||||
{
|
||||
groupInstances.push_back( Instances::Group( cfg.groups[ x ], dictionaries ) );
|
||||
|
||||
// Update names for dictionaries that are present, so that they could be
|
||||
// found in case they got moved.
|
||||
Instances::updateNames( cfg.groups[ x ], dictionaries );
|
||||
}
|
||||
|
||||
ui.groupList->fill( groupInstances );
|
||||
ui.groupList->setCurrentGroup( cfg.lastMainGroup );
|
||||
|
||||
|
|
Loading…
Reference in a new issue