Diffstat (limited to 'library/backend/categories.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | library/backend/categories.cpp | 142 |
1 files changed, 59 insertions, 83 deletions
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 | |||
@@ -173,32 +173,2 @@ QStringList CategoryGroup::labels(const QArray<int> &catids ) const | |||
173 | 173 | ||
174 | QArray<int> CategoryGroup::ids( const QStringList &cats ) const | ||
175 | { | ||
176 | QArray<int> results; | ||
177 | |||
178 | for ( QStringList::ConstIterator catIt = cats.begin(); | ||
179 | catIt != cats.end(); ++catIt ) { | ||
180 | if ( *catIt == QObject::tr("All") || *catIt == QObject::tr("Unfiled") ) | ||
181 | continue; | ||
182 | int value = id( *catIt ); | ||
183 | if ( value != 0 ) { | ||
184 | int tmp = results.size(); | ||
185 | results.resize( tmp + 1 ); | ||
186 | results[ tmp ] = value; | ||
187 | } | ||
188 | } | ||
189 | |||
190 | return results; | ||
191 | } | ||
192 | |||
193 | QArray<int> CategoryGroup::ids() const | ||
194 | { | ||
195 | QArray<int> results( mIdLabelMap.count() ); | ||
196 | int i = 0; | ||
197 | for ( QMap<int, QString>::ConstIterator it = mIdLabelMap.begin(); | ||
198 | it != mIdLabelMap.end(); ++it ) | ||
199 | results[i++] = it.key(); | ||
200 | |||
201 | return results; | ||
202 | } | ||
203 | |||
204 | /*********************************************************** | 174 | /*********************************************************** |
@@ -345,2 +315,10 @@ QStringList Categories::labels( const QString &app, | |||
345 | QStringList cats; | 315 | QStringList cats; |
316 | |||
317 | if ( appIt != mAppCats.end() ) | ||
318 | cats += (*appIt).labels(); | ||
319 | else qDebug("Categories::labels didn't find app %s", app.latin1() ); | ||
320 | if ( includeGlobal ) | ||
321 | cats += mGlobalCats.labels(); | ||
322 | |||
323 | cats.sort(); | ||
346 | switch ( extra ) { | 324 | switch ( extra ) { |
@@ -358,10 +336,3 @@ QStringList Categories::labels( const QString &app, | |||
358 | } | 336 | } |
359 | if ( appIt != mAppCats.end() ) | 337 | |
360 | cats += (*appIt).labels(); | ||
361 | else qDebug("Categories::labels didn't find app %s", app.latin1() ); | ||
362 | if ( includeGlobal ) | ||
363 | cats += mGlobalCats.labels(); | ||
364 | // I don't think a sorted list is useful, the user might find prefer | ||
365 | // it in the original order. | ||
366 | // cats.sort(); | ||
367 | return cats; | 338 | return cats; |
@@ -380,10 +351,2 @@ QString Categories::label( const QString &app, int id ) const | |||
380 | 351 | ||
381 | QStringList Categories::labels( const QString & app, | ||
382 | const QArray<int> &catids ) const | ||
383 | { | ||
384 | QStringList strs = mGlobalCats.labels( catids ); | ||
385 | strs += mAppCats[app].labels( catids ); | ||
386 | return strs; | ||
387 | } | ||
388 | |||
389 | /** Returns a single string associated with the cat ids for display in | 352 | /** Returns a single string associated with the cat ids for display in |
@@ -419,29 +382,17 @@ QString Categories::displaySingle( const QString &app, | |||
419 | 382 | ||
420 | QArray<int> Categories::ids( const QString &app ) const | 383 | QArray<int> Categories::ids( const QString &app, const QStringList &labels) const |
421 | { | 384 | { |
422 | QArray<int> allIds = mGlobalCats.ids(); | 385 | QArray<int> results; |
423 | QArray<int> appIds = mAppCats[app].ids(); | 386 | QStringList::ConstIterator it; |
424 | 387 | int i; | |
425 | // we should make the guarentee that the ids are in the | ||
426 | // same order as the labels, (i.e. app cats then global) | ||
427 | // otherwise there is no point in having these two separate functions. | ||
428 | uint appSize = appIds.size(); | ||
429 | appIds.resize( appSize + allIds.size() ); | ||
430 | for ( uint i = appSize; i < appIds.size(); ++i ) | ||
431 | appIds[int(i)] = allIds[int(i - appSize)]; | ||
432 | 388 | ||
433 | return appIds; | 389 | for ( i=0, it=labels.begin(); it!=labels.end(); i++, ++it ) { |
390 | int value = id( app, *it ); | ||
391 | if ( value != 0 ) { | ||
392 | int tmp = results.size(); | ||
393 | results.resize( tmp + 1 ); | ||
394 | results[ tmp ] = value; | ||
434 | } | 395 | } |
435 | 396 | } | |
436 | QArray<int> Categories::ids( const QString &app, const QStringList &cats ) const | 397 | return results; |
437 | { | ||
438 | QArray<int> allIds = mGlobalCats.ids( cats ); | ||
439 | QArray<int> appIds = mAppCats[app].ids( cats ); | ||
440 | |||
441 | uint appSize = appIds.size(); | ||
442 | appIds.resize( appSize + allIds.size() ); | ||
443 | for ( uint i = appSize; i < appIds.size(); ++i ) | ||
444 | appIds[int(i)] = allIds[int(i - appSize)]; | ||
445 | |||
446 | return appIds; | ||
447 | } | 398 | } |
@@ -541,6 +492,11 @@ bool Categories::exists( const QString &appname, | |||
541 | 492 | ||
493 | |||
542 | bool Categories::save( const QString &fname ) const | 494 | bool Categories::save( const QString &fname ) const |
543 | { | 495 | { |
544 | QFile file( fname ); | 496 | QString strNewFile = fname + ".new"; |
545 | if ( !file.open( IO_WriteOnly ) ) { | 497 | QFile f( strNewFile ); |
498 | QString out; | ||
499 | int total_written; | ||
500 | |||
501 | if ( !f.open( IO_WriteOnly|IO_Raw ) ) { | ||
546 | qWarning("Unable to write to %s", fname.latin1()); | 502 | qWarning("Unable to write to %s", fname.latin1()); |
@@ -549,11 +505,11 @@ bool Categories::save( const QString &fname ) const | |||
549 | 505 | ||
550 | QTextStream ts( &file ); | 506 | out = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
551 | ts << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; | 507 | out += "<!DOCTYPE CategoryList>\n"; |
552 | ts << "<!DOCTYPE CategoryList>" << endl; | 508 | |
509 | out += "<Categories>\n"; | ||
553 | 510 | ||
554 | ts << "<Categories>" << endl; | ||
555 | for ( QMap<int, QString>::ConstIterator git = mGlobalCats.idMap().begin(); | 511 | for ( QMap<int, QString>::ConstIterator git = mGlobalCats.idMap().begin(); |
556 | git != mGlobalCats.idMap().end(); ++git ) | 512 | git != mGlobalCats.idMap().end(); ++git ) |
557 | ts << "<Category id=\"" << git.key() << "\"" | 513 | out += "<Category id=\"" + QString::number(git.key()) + "\"" + |
558 | << " name=\"" << escapeString(*git) << "\" />" << endl; | 514 | " name=\"" + escapeString(*git) + "\" />\n"; |
559 | 515 | ||
@@ -565,9 +521,24 @@ bool Categories::save( const QString &fname ) const | |||
565 | appcatit != appcats.end(); ++appcatit ) | 521 | appcatit != appcats.end(); ++appcatit ) |
566 | ts << "<Category id=\"" << appcatit.key() << "\"" | 522 | out += "<Category id=\"" + QString::number(appcatit.key()) + "\"" + |
567 | << " app=\"" << escapeString(app) << "\"" | 523 | " app=\"" + escapeString(app) + "\"" + |
568 | << " name=\"" << escapeString(*appcatit) << "\" />" << endl; | 524 | " name=\"" + escapeString(*appcatit) + "\" />\n"; |
525 | } | ||
526 | out += "</Categories>\n"; | ||
527 | |||
528 | QCString cstr = out.utf8(); | ||
529 | total_written = f.writeBlock( cstr.data(), cstr.length() ); | ||
530 | if ( total_written != int(cstr.length()) ) { | ||
531 | f.close(); | ||
532 | QFile::remove( strNewFile ); | ||
533 | return FALSE; | ||
534 | } | ||
535 | f.close(); | ||
536 | |||
537 | if ( ::rename( strNewFile.latin1(), fname.latin1() ) < 0 ) { | ||
538 | qWarning( "problem renaming file %s to %s", | ||
539 | strNewFile.latin1(), fname.latin1()); | ||
540 | // remove the tmp file... | ||
541 | QFile::remove( strNewFile ); | ||
569 | } | 542 | } |
570 | ts << "</Categories>" << endl; | ||
571 | 543 | ||
572 | file.close(); | ||
573 | return TRUE; | 544 | return TRUE; |
@@ -580,2 +551,7 @@ bool Categories::load( const QString &fname ) | |||
580 | qWarning("Unable to open %s", fname.latin1()); | 551 | qWarning("Unable to open %s", fname.latin1()); |
552 | |||
553 | addGlobalCategory(tr("Business")); | ||
554 | addGlobalCategory(tr("Personal")); | ||
555 | save(fname); | ||
556 | |||
581 | return FALSE; | 557 | return FALSE; |