+ 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:
Konstantin Isakov 2009-04-01 12:00:28 +00:00
parent 4a99ce281f
commit c32a12afb9
6 changed files with 75 additions and 7 deletions

View file

@ -142,7 +142,8 @@ Class load() throw( exError )
QDomNodeList dicts = grp.elementsByTagName( "dictionary" ); QDomNodeList dicts = grp.elementsByTagName( "dictionary" );
for( unsigned y = 0; y < dicts.length(); ++y ) 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 ); c.groups.push_back( g );
} }
@ -258,15 +259,21 @@ void save( Class const & c ) throw( exError )
group.setAttributeNode( icon ); 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" ); QDomElement dictionary = dd.createElement( "dictionary" );
group.appendChild( dictionary ); group.appendChild( dictionary );
QDomText value = dd.createTextNode( *j ); QDomText value = dd.createTextNode( j->id );
dictionary.appendChild( value ); dictionary.appendChild( value );
QDomAttr name = dd.createAttribute( "name" );
name.setValue( j->name );
dictionary.setAttributeNode( name );
} }
} }
} }

View file

@ -28,11 +28,23 @@ struct Path
/// A list of paths where to search for the dictionaries /// A list of paths where to search for the dictionaries
typedef vector< Path > Paths; 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 /// A dictionary group
struct Group struct Group
{ {
QString name, icon; QString name, icon;
vector< QString > dictionaries; // consists of dictionary's ids vector< DictionaryRef > dictionaries;
}; };
/// All the groups /// All the groups

View file

@ -150,7 +150,7 @@ bool DictListModel::setData( QModelIndex const & index, const QVariant & value,
{ {
Config::Group g; Config::Group g;
g.dictionaries.push_back( value.toString() ); g.dictionaries.push_back( Config::DictionaryRef( value.toString(), QString() ) );
Instances::Group i( g, *allDicts ); Instances::Group i( g, *allDicts );

View file

@ -12,14 +12,31 @@ Group::Group( Config::Group const & cfgGroup,
{ {
for( unsigned x = 0; x < cfgGroup.dictionaries.size(); ++x ) 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--; ) for( unsigned y = allDictionaries.size(); y--; )
if ( allDictionaries[ y ]->getId() == id ) if ( allDictionaries[ y ]->getId() == id )
{ {
dictionaries.push_back( allDictionaries[ y ] ); dictionaries.push_back( allDictionaries[ y ] );
added = true;
break; 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; result.icon = icon;
for( unsigned x = 0; x < dictionaries.size(); ++x ) 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; 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;
}
}
}
} }

View file

@ -36,6 +36,12 @@ struct Group
typedef vector< Group > Groups; 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 #endif

View file

@ -392,8 +392,14 @@ void MainWindow::updateGroupList()
groupInstances.clear(); groupInstances.clear();
for( unsigned x = 0; x < cfg.groups.size(); ++x ) for( unsigned x = 0; x < cfg.groups.size(); ++x )
{
groupInstances.push_back( Instances::Group( cfg.groups[ x ], dictionaries ) ); 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->fill( groupInstances );
ui.groupList->setCurrentGroup( cfg.lastMainGroup ); ui.groupList->setCurrentGroup( cfg.lastMainGroup );