author | kergoth <kergoth> | 2002-06-07 18:53:14 (UTC) |
---|---|---|
committer | kergoth <kergoth> | 2002-06-07 18:53:14 (UTC) |
commit | 640d964cfdc7467f6cacb513087cd3acda2c04f0 (patch) (unidiff) | |
tree | 9a784686c1795f8b1f81eb344598f3b549d43467 /library/backend/categories.cpp | |
parent | dfb9c76738bb68e235114c5ad43dbd26a59b98ab (diff) | |
download | opie-640d964cfdc7467f6cacb513087cd3acda2c04f0.zip opie-640d964cfdc7467f6cacb513087cd3acda2c04f0.tar.gz opie-640d964cfdc7467f6cacb513087cd3acda2c04f0.tar.bz2 |
Backing out unintentional merge from TT branch.
Diffstat (limited to 'library/backend/categories.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | library/backend/categories.cpp | 146 |
1 files changed, 61 insertions, 85 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 | |||
@@ -150,78 +150,48 @@ int CategoryGroup::id(const QString &label) const | |||
150 | 150 | ||
151 | QStringList CategoryGroup::labels() const | 151 | QStringList CategoryGroup::labels() const |
152 | { | 152 | { |
153 | QStringList labels; | 153 | QStringList labels; |
154 | for ( QMap<int, QString>::ConstIterator it = mIdLabelMap.begin(); | 154 | for ( QMap<int, QString>::ConstIterator it = mIdLabelMap.begin(); |
155 | it != mIdLabelMap.end(); ++it ) | 155 | it != mIdLabelMap.end(); ++it ) |
156 | labels += *it; | 156 | labels += *it; |
157 | // ### I don't think this is the place for this... | 157 | // ### I don't think this is the place for this... |
158 | // labels.sort(); | 158 | // labels.sort(); |
159 | return labels; | 159 | return labels; |
160 | } | 160 | } |
161 | 161 | ||
162 | QStringList CategoryGroup::labels(const QArray<int> &catids ) const | 162 | QStringList CategoryGroup::labels(const QArray<int> &catids ) const |
163 | { | 163 | { |
164 | QStringList labels; | 164 | QStringList labels; |
165 | if ( catids.count() == 0 ) | 165 | if ( catids.count() == 0 ) |
166 | return labels; | 166 | return labels; |
167 | for ( QMap<int, QString>::ConstIterator it = mIdLabelMap.begin(); | 167 | for ( QMap<int, QString>::ConstIterator it = mIdLabelMap.begin(); |
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 | ||
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 | /*********************************************************** |
205 | * | 175 | * |
206 | * Categories | 176 | * Categories |
207 | * | 177 | * |
208 | **********************************************************/ | 178 | **********************************************************/ |
209 | 179 | ||
210 | /** Add the category name as long as it doesn't already exist locally | 180 | /** Add the category name as long as it doesn't already exist locally |
211 | * or globally. Return TRUE if added, FALSE if conflicts. | 181 | * or globally. Return TRUE if added, FALSE if conflicts. |
212 | */ | 182 | */ |
213 | int Categories::addCategory( const QString &appname, | 183 | int Categories::addCategory( const QString &appname, |
214 | const QString &catname, | 184 | const QString &catname, |
215 | int uid ) | 185 | int uid ) |
216 | { | 186 | { |
217 | if ( mGlobalCats.contains(catname) ) | 187 | if ( mGlobalCats.contains(catname) ) |
218 | return 0; | 188 | return 0; |
219 | 189 | ||
220 | QMap< QString, CategoryGroup >::Iterator | 190 | QMap< QString, CategoryGroup >::Iterator |
221 | appIt = mAppCats.find( appname ); | 191 | appIt = mAppCats.find( appname ); |
222 | 192 | ||
223 | if ( appIt == mAppCats.end() ) { | 193 | if ( appIt == mAppCats.end() ) { |
224 | CategoryGroup newgroup; | 194 | CategoryGroup newgroup; |
225 | newgroup.add( uid, catname ); | 195 | newgroup.add( uid, catname ); |
226 | mAppCats.insert( appname, newgroup ); | 196 | mAppCats.insert( appname, newgroup ); |
227 | emit categoryAdded( *this, appname, uid ); | 197 | emit categoryAdded( *this, appname, uid ); |
@@ -322,149 +292,130 @@ bool Categories::removeGlobalCategory( const QString &catname ) | |||
322 | return FALSE; | 292 | return FALSE; |
323 | } | 293 | } |
324 | 294 | ||
325 | 295 | ||
326 | bool Categories::removeGlobalCategory( int uid ) | 296 | bool Categories::removeGlobalCategory( int uid ) |
327 | { | 297 | { |
328 | if ( mGlobalCats.remove( uid ) ) { | 298 | if ( mGlobalCats.remove( uid ) ) { |
329 | emit categoryRemoved( *this, QString::null, uid ); | 299 | emit categoryRemoved( *this, QString::null, uid ); |
330 | return TRUE; | 300 | return TRUE; |
331 | } | 301 | } |
332 | return FALSE; | 302 | return FALSE; |
333 | } | 303 | } |
334 | 304 | ||
335 | /** Returns the sorted list of all categories that are associated with | 305 | /** Returns the sorted list of all categories that are associated with |
336 | * the app. If includeGlobal parameter is TRUE then the returned | 306 | * the app. If includeGlobal parameter is TRUE then the returned |
337 | * categories will include the global category items. | 307 | * categories will include the global category items. |
338 | */ | 308 | */ |
339 | QStringList Categories::labels( const QString &app, | 309 | 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; |
352 | case AllLabel: | 330 | case AllLabel: |
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 | ||
370 | QString Categories::label( const QString &app, int id ) const | 341 | QString Categories::label( const QString &app, int id ) const |
371 | { | 342 | { |
372 | if ( mGlobalCats.contains( id ) ) | 343 | if ( mGlobalCats.contains( id ) ) |
373 | return mGlobalCats.label( id ); | 344 | return mGlobalCats.label( id ); |
374 | QMap< QString, CategoryGroup >::ConstIterator | 345 | QMap< QString, CategoryGroup >::ConstIterator |
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 | ||
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 |
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 | */ |
394 | QString Categories::displaySingle( const QString &app, | 357 | QString Categories::displaySingle( const QString &app, |
395 | const QArray<int> &catids, | 358 | const QArray<int> &catids, |
396 | DisplaySingle display ) const | 359 | DisplaySingle display ) const |
397 | { | 360 | { |
398 | QStringList strs = labels( app, catids ); | 361 | QStringList strs = labels( app, catids ); |
399 | if ( !strs.count() ) | 362 | if ( !strs.count() ) |
400 | return tr("Unfiled"); | 363 | return tr("Unfiled"); |
401 | strs.sort(); | 364 | strs.sort(); |
402 | QString r; | 365 | QString r; |
403 | if ( strs.count() > 1 ) { | 366 | if ( strs.count() > 1 ) { |
404 | switch ( display ) { | 367 | switch ( display ) { |
405 | case ShowFirst: | 368 | case ShowFirst: |
406 | r = strs.first(); | 369 | r = strs.first(); |
407 | break; | 370 | break; |
408 | case ShowMulti: | 371 | case ShowMulti: |
409 | r = strs.first() + tr(" (multi.)"); | 372 | r = strs.first() + tr(" (multi.)"); |
410 | break; | 373 | break; |
411 | case ShowAll: | 374 | case ShowAll: |
412 | r = strs.join(" "); | 375 | r = strs.join(" "); |
413 | break; | 376 | break; |
414 | } | 377 | } |
415 | } | 378 | } |
416 | else r = strs.first(); | 379 | else r = strs.first(); |
417 | return r; | 380 | return r; |
418 | } | 381 | } |
419 | 382 | ||
420 | QArray<int> Categories::ids( const QString &app ) const | 383 | QArray<int> Categories::ids( const QString &app, const QStringList &labels) const |
421 | { | ||
422 | QArray<int> allIds = mGlobalCats.ids(); | ||
423 | QArray<int> appIds = mAppCats[app].ids(); | ||
424 | |||
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 | |||
433 | return appIds; | ||
434 | } | ||
435 | |||
436 | QArray<int> Categories::ids( const QString &app, const QStringList &cats ) const | ||
437 | { | 384 | { |
438 | QArray<int> allIds = mGlobalCats.ids( cats ); | 385 | QArray<int> results; |
439 | QArray<int> appIds = mAppCats[app].ids( cats ); | 386 | QStringList::ConstIterator it; |
440 | 387 | int i; | |
441 | uint appSize = appIds.size(); | 388 | |
442 | appIds.resize( appSize + allIds.size() ); | 389 | for ( i=0, it=labels.begin(); it!=labels.end(); i++, ++it ) { |
443 | for ( uint i = appSize; i < appIds.size(); ++i ) | 390 | int value = id( app, *it ); |
444 | appIds[int(i)] = allIds[int(i - appSize)]; | 391 | if ( value != 0 ) { |
445 | 392 | int tmp = results.size(); | |
446 | return appIds; | 393 | results.resize( tmp + 1 ); |
394 | results[ tmp ] = value; | ||
395 | } | ||
396 | } | ||
397 | return results; | ||
447 | } | 398 | } |
448 | 399 | ||
449 | int Categories::id( const QString &app, const QString &cat ) const | 400 | int 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; |
453 | int uid = mGlobalCats.id( cat ); | 404 | int uid = mGlobalCats.id( cat ); |
454 | if ( uid != 0 ) | 405 | if ( uid != 0 ) |
455 | return uid; | 406 | return uid; |
456 | return mAppCats[app].id( cat ); | 407 | return mAppCats[app].id( cat ); |
457 | } | 408 | } |
458 | 409 | ||
459 | 410 | ||
460 | /** Return TRUE if renaming succeeded; FALSE if app name not found, | 411 | /** Return TRUE if renaming succeeded; FALSE if app name not found, |
461 | * or if there was a name conflict | 412 | * or if there was a name conflict |
462 | */ | 413 | */ |
463 | bool Categories::renameCategory( const QString &appname, | 414 | bool Categories::renameCategory( const QString &appname, |
464 | const QString &oldName, | 415 | const QString &oldName, |
465 | const QString &newName ) | 416 | const QString &newName ) |
466 | { | 417 | { |
467 | QMap< QString, CategoryGroup >::Iterator | 418 | QMap< QString, CategoryGroup >::Iterator |
468 | appIt = mAppCats.find( appname ); | 419 | appIt = mAppCats.find( appname ); |
469 | 420 | ||
470 | if ( appIt != mAppCats.end() ) { | 421 | if ( appIt != mAppCats.end() ) { |
@@ -518,87 +469,112 @@ bool Categories::isGlobal( const QString &catname ) const | |||
518 | bool Categories::exists( const QString &catname ) const | 469 | bool Categories::exists( const QString &catname ) const |
519 | { | 470 | { |
520 | if ( isGlobal(catname) ) | 471 | if ( isGlobal(catname) ) |
521 | return TRUE; | 472 | return TRUE; |
522 | 473 | ||
523 | for ( QMap<QString, CategoryGroup>::ConstIterator appsIt = mAppCats.begin(); appsIt != mAppCats.end(); ++appsIt ) | 474 | for ( QMap<QString, CategoryGroup>::ConstIterator appsIt = mAppCats.begin(); appsIt != mAppCats.end(); ++appsIt ) |
524 | if ( exists( appsIt.key(), catname ) ) | 475 | if ( exists( appsIt.key(), catname ) ) |
525 | return TRUE; | 476 | return TRUE; |
526 | 477 | ||
527 | return FALSE; | 478 | return FALSE; |
528 | } | 479 | } |
529 | 480 | ||
530 | bool Categories::exists( const QString &appname, | 481 | bool Categories::exists( const QString &appname, |
531 | const QString &catname) const | 482 | const QString &catname) const |
532 | { | 483 | { |
533 | QMap< QString, CategoryGroup >::ConstIterator | 484 | QMap< QString, CategoryGroup >::ConstIterator |
534 | appIt = mAppCats.find( appname ); | 485 | appIt = mAppCats.find( appname ); |
535 | 486 | ||
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 | |||
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()); |
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 | ||
576 | bool Categories::load( const QString &fname ) | 547 | bool 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() ); |
587 | QChar *uc = (QChar *)data.unicode(); | 563 | QChar *uc = (QChar *)data.unicode(); |
588 | int len = data.length(); | 564 | int len = data.length(); |
589 | 565 | ||
590 | // QTime t; | 566 | // QTime t; |
591 | // t.start(); | 567 | // t.start(); |
592 | QString name; | 568 | QString name; |
593 | QString id; | 569 | QString id; |
594 | QString app; | 570 | QString app; |
595 | int i = 0; | 571 | int i = 0; |
596 | while ( (i = data.find( "<Category ", i)) != -1 ) { | 572 | while ( (i = data.find( "<Category ", i)) != -1 ) { |
597 | 573 | ||
598 | i += 10; | 574 | i += 10; |
599 | name = QString::null; | 575 | name = QString::null; |
600 | app = QString::null; | 576 | app = QString::null; |
601 | while ( 1 ) { | 577 | while ( 1 ) { |
602 | // skip white space | 578 | // skip white space |
603 | while ( i < len && | 579 | while ( i < len && |
604 | (uc[i] == ' ' || uc[i] == '\n' || uc[i] == '\r') ) | 580 | (uc[i] == ' ' || uc[i] == '\n' || uc[i] == '\r') ) |