summaryrefslogtreecommitdiff
path: root/library/backend/categories.cpp
Unidiff
Diffstat (limited to 'library/backend/categories.cpp') (more/less context) (show whitespace changes)
-rw-r--r--library/backend/categories.cpp142
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
@@ -168,42 +168,12 @@ QStringList CategoryGroup::labels(const QArray<int> &catids ) const
168 it != mIdLabelMap.end(); ++it ) 168 it != mIdLabelMap.end(); ++it )
169 if ( catids.find( it.key() ) != -1 ) 169 if ( catids.find( it.key() ) != -1 )
170 labels += *it; 170 labels += *it;
171 return labels; 171 return labels;
172} 172}
173 173
174QArray<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
193QArray<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/***********************************************************
205 * 175 *
206 * Categories 176 * Categories
207 * 177 *
208 **********************************************************/ 178 **********************************************************/
209 179
@@ -340,12 +310,20 @@ QStringList Categories::labels( const QString &app,
340 bool includeGlobal, 310 bool includeGlobal,
341 ExtraLabels extra ) const 311 ExtraLabels extra ) const
342{ 312{
343 QMap< QString, CategoryGroup >::ConstIterator 313 QMap< QString, CategoryGroup >::ConstIterator
344 appIt = mAppCats.find( app ); 314 appIt = mAppCats.find( 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 ) {
347 case NoExtra: break; 325 case NoExtra: break;
348 case AllUnfiled: 326 case AllUnfiled:
349 cats.append( tr("All") ); 327 cats.append( tr("All") );
350 cats.append( tr("Unfiled") ); 328 cats.append( tr("Unfiled") );
351 break; 329 break;
@@ -353,20 +331,13 @@ QStringList Categories::labels( const QString &app,
353 cats.append( tr("All") ); 331 cats.append( tr("All") );
354 break; 332 break;
355 case UnfiledLabel: 333 case UnfiledLabel:
356 cats.append( tr("Unfiled") ); 334 cats.append( tr("Unfiled") );
357 break; 335 break;
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;
368} 339}
369 340
370QString Categories::label( const QString &app, int id ) const 341QString Categories::label( const QString &app, int id ) const
371{ 342{
372 if ( mGlobalCats.contains( id ) ) 343 if ( mGlobalCats.contains( id ) )
@@ -375,20 +346,12 @@ QString Categories::label( const QString &app, int id ) const
375 appIt = mAppCats.find( app ); 346 appIt = mAppCats.find( app );
376 if ( appIt == mAppCats.end() ) 347 if ( appIt == mAppCats.end() )
377 return QString::null; 348 return QString::null;
378 return (*appIt).label( id ); 349 return (*appIt).label( id );
379} 350}
380 351
381QStringList 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
390 * a combobox or any area that requires one string. If catids are empty 353 * a combobox or any area that requires one string. If catids are empty
391 * then "Unfiled" will be returned. If multiple categories are assigned 354 * then "Unfiled" will be returned. If multiple categories are assigned
392 * the first cat id is shown with " (multi)" appended to the string. 355 * the first cat id is shown with " (multi)" appended to the string.
393 */ 356 */
394QString Categories::displaySingle( const QString &app, 357QString Categories::displaySingle( const QString &app,
@@ -414,39 +377,27 @@ QString Categories::displaySingle( const QString &app,
414 } 377 }
415 } 378 }
416 else r = strs.first(); 379 else r = strs.first();
417 return r; 380 return r;
418} 381}
419 382
420QArray<int> Categories::ids( const QString &app ) const 383QArray<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 }
436QArray<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}
448 399
449int Categories::id( const QString &app, const QString &cat ) const 400int Categories::id( const QString &app, const QString &cat ) const
450{ 401{
451 if ( cat == tr("Unfiled") || cat.contains( tr(" (multi.)") ) ) 402 if ( cat == tr("Unfiled") || cat.contains( tr(" (multi.)") ) )
452 return 0; 403 return 0;
@@ -536,51 +487,76 @@ bool Categories::exists( const QString &appname,
536 if ( appIt == mAppCats.end() ) 487 if ( appIt == mAppCats.end() )
537 return FALSE; 488 return FALSE;
538 489
539 return (*appIt).contains( catname ); 490 return (*appIt).contains( catname );
540} 491}
541 492
493
542bool Categories::save( const QString &fname ) const 494bool 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());
547 return FALSE; 503 return FALSE;
548 } 504 }
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
560 for ( QMap<QString, CategoryGroup>::ConstIterator appsIt=mAppCats.begin(); 516 for ( QMap<QString, CategoryGroup>::ConstIterator appsIt=mAppCats.begin();
561 appsIt != mAppCats.end(); ++appsIt ) { 517 appsIt != mAppCats.end(); ++appsIt ) {
562 const QString &app = appsIt.key(); 518 const QString &app = appsIt.key();
563 const QMap<int, QString> &appcats = (*appsIt).idMap(); 519 const QMap<int, QString> &appcats = (*appsIt).idMap();
564 for ( QMap<int, QString>::ConstIterator appcatit = appcats.begin(); 520 for ( QMap<int, QString>::ConstIterator appcatit = appcats.begin();
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;
574} 545}
575 546
576bool Categories::load( const QString &fname ) 547bool Categories::load( const QString &fname )
577{ 548{
578 QFile file( fname ); 549 QFile file( fname );
579 if ( !file.open( IO_ReadOnly ) ) { 550 if ( !file.open( IO_ReadOnly ) ) {
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;
582 } 558 }
583 559
584 clear(); 560 clear();
585 QByteArray ba = file.readAll(); 561 QByteArray ba = file.readAll();
586 QString data = QString::fromUtf8( ba.data(), ba.size() ); 562 QString data = QString::fromUtf8( ba.data(), ba.size() );