From 640d964cfdc7467f6cacb513087cd3acda2c04f0 Mon Sep 17 00:00:00 2001 From: kergoth Date: Fri, 07 Jun 2002 18:53:14 +0000 Subject: Backing out unintentional merge from TT branch. --- (limited to 'library/backend/categories.cpp') diff --git a/library/backend/categories.cpp b/library/backend/categories.cpp index 91331db..6e011c4 100644 --- a/library/backend/categories.cpp +++ b/library/backend/categories.cpp @@ -171,36 +171,6 @@ QStringList CategoryGroup::labels(const QArray &catids ) const return labels; } -QArray CategoryGroup::ids( const QStringList &cats ) const -{ - QArray results; - - for ( QStringList::ConstIterator catIt = cats.begin(); - catIt != cats.end(); ++catIt ) { - if ( *catIt == QObject::tr("All") || *catIt == QObject::tr("Unfiled") ) - continue; - int value = id( *catIt ); - if ( value != 0 ) { - int tmp = results.size(); - results.resize( tmp + 1 ); - results[ tmp ] = value; - } - } - - return results; -} - -QArray CategoryGroup::ids() const -{ - QArray results( mIdLabelMap.count() ); - int i = 0; - for ( QMap::ConstIterator it = mIdLabelMap.begin(); - it != mIdLabelMap.end(); ++it ) - results[i++] = it.key(); - - return results; -} - /*********************************************************** * * Categories @@ -343,6 +313,14 @@ QStringList Categories::labels( const QString &app, QMap< QString, CategoryGroup >::ConstIterator appIt = mAppCats.find( app ); QStringList cats; + + if ( appIt != mAppCats.end() ) + cats += (*appIt).labels(); + else qDebug("Categories::labels didn't find app %s", app.latin1() ); + if ( includeGlobal ) + cats += mGlobalCats.labels(); + + cats.sort(); switch ( extra ) { case NoExtra: break; case AllUnfiled: @@ -356,14 +334,7 @@ QStringList Categories::labels( const QString &app, cats.append( tr("Unfiled") ); break; } - if ( appIt != mAppCats.end() ) - cats += (*appIt).labels(); - else qDebug("Categories::labels didn't find app %s", app.latin1() ); - if ( includeGlobal ) - cats += mGlobalCats.labels(); - // I don't think a sorted list is useful, the user might find prefer - // it in the original order. -// cats.sort(); + return cats; } @@ -378,14 +349,6 @@ QString Categories::label( const QString &app, int id ) const return (*appIt).label( id ); } -QStringList Categories::labels( const QString & app, - const QArray &catids ) const -{ - QStringList strs = mGlobalCats.labels( catids ); - strs += mAppCats[app].labels( catids ); - return strs; -} - /** Returns a single string associated with the cat ids for display in * a combobox or any area that requires one string. If catids are empty * then "Unfiled" will be returned. If multiple categories are assigned @@ -417,33 +380,21 @@ QString Categories::displaySingle( const QString &app, return r; } -QArray Categories::ids( const QString &app ) const -{ - QArray allIds = mGlobalCats.ids(); - QArray appIds = mAppCats[app].ids(); - - // we should make the guarentee that the ids are in the - // same order as the labels, (i.e. app cats then global) - // otherwise there is no point in having these two separate functions. - uint appSize = appIds.size(); - appIds.resize( appSize + allIds.size() ); - for ( uint i = appSize; i < appIds.size(); ++i ) - appIds[int(i)] = allIds[int(i - appSize)]; - - return appIds; -} - -QArray Categories::ids( const QString &app, const QStringList &cats ) const +QArray Categories::ids( const QString &app, const QStringList &labels) const { - QArray allIds = mGlobalCats.ids( cats ); - QArray appIds = mAppCats[app].ids( cats ); - - uint appSize = appIds.size(); - appIds.resize( appSize + allIds.size() ); - for ( uint i = appSize; i < appIds.size(); ++i ) - appIds[int(i)] = allIds[int(i - appSize)]; - - return appIds; + QArray results; + QStringList::ConstIterator it; + int i; + + for ( i=0, it=labels.begin(); it!=labels.end(); i++, ++it ) { + int value = id( app, *it ); + if ( value != 0 ) { + int tmp = results.size(); + results.resize( tmp + 1 ); + results[ tmp ] = value; + } + } + return results; } int Categories::id( const QString &app, const QString &cat ) const @@ -539,23 +490,28 @@ bool Categories::exists( const QString &appname, return (*appIt).contains( catname ); } + bool Categories::save( const QString &fname ) const { - QFile file( fname ); - if ( !file.open( IO_WriteOnly ) ) { + QString strNewFile = fname + ".new"; + QFile f( strNewFile ); + QString out; + int total_written; + + if ( !f.open( IO_WriteOnly|IO_Raw ) ) { qWarning("Unable to write to %s", fname.latin1()); return FALSE; } - QTextStream ts( &file ); - ts << "\n"; - ts << "" << endl; + out = "\n"; + out += "\n"; + + out += "\n"; - ts << "" << endl; for ( QMap::ConstIterator git = mGlobalCats.idMap().begin(); git != mGlobalCats.idMap().end(); ++git ) - ts << "" << endl; + out += "\n"; for ( QMap::ConstIterator appsIt=mAppCats.begin(); appsIt != mAppCats.end(); ++appsIt ) { @@ -563,13 +519,28 @@ bool Categories::save( const QString &fname ) const const QMap &appcats = (*appsIt).idMap(); for ( QMap::ConstIterator appcatit = appcats.begin(); appcatit != appcats.end(); ++appcatit ) - ts << "" << endl; + out += "\n"; + } + out += "\n"; + + QCString cstr = out.utf8(); + total_written = f.writeBlock( cstr.data(), cstr.length() ); + if ( total_written != int(cstr.length()) ) { + f.close(); + QFile::remove( strNewFile ); + return FALSE; + } + f.close(); + + if ( ::rename( strNewFile.latin1(), fname.latin1() ) < 0 ) { + qWarning( "problem renaming file %s to %s", + strNewFile.latin1(), fname.latin1()); + // remove the tmp file... + QFile::remove( strNewFile ); } - ts << "" << endl; - file.close(); return TRUE; } @@ -578,6 +549,11 @@ bool Categories::load( const QString &fname ) QFile file( fname ); if ( !file.open( IO_ReadOnly ) ) { qWarning("Unable to open %s", fname.latin1()); + + addGlobalCategory(tr("Business")); + addGlobalCategory(tr("Personal")); + save(fname); + return FALSE; } -- cgit v0.9.0.2