-rw-r--r-- | core/pim/todo/todotable.cpp | 148 |
1 files changed, 136 insertions, 12 deletions
diff --git a/core/pim/todo/todotable.cpp b/core/pim/todo/todotable.cpp index 2acd03c..401d2c8 100644 --- a/core/pim/todo/todotable.cpp +++ b/core/pim/todo/todotable.cpp | |||
@@ -23,4 +23,6 @@ | |||
23 | 23 | ||
24 | #include <opie/tododb.h> | 24 | #include <opie/tododb.h> |
25 | #include <opie/xmltree.h> | ||
26 | |||
25 | #include <qpe/categoryselect.h> | 27 | #include <qpe/categoryselect.h> |
26 | #include <qpe/xmlreader.h> | 28 | #include <qpe/xmlreader.h> |
@@ -33,4 +35,5 @@ | |||
33 | #include <qtimer.h> | 35 | #include <qtimer.h> |
34 | #include <qdatetime.h> | 36 | #include <qdatetime.h> |
37 | #include <qtextstream.h> | ||
35 | 38 | ||
36 | #include <qcursor.h> | 39 | #include <qcursor.h> |
@@ -41,11 +44,12 @@ | |||
41 | 44 | ||
42 | #include <stdio.h> | 45 | #include <stdio.h> |
46 | #include <iostream> | ||
43 | 47 | ||
48 | namespace { | ||
44 | 49 | ||
45 | 50 | static bool taskCompare( const ToDoEvent &task, const QRegExp &r, int category ); | |
46 | static bool taskCompare( const ToDoEvent &task, const QRegExp &r, int category ); | 51 | static QString journalFileName(); |
47 | 52 | static ToDoEvent xmlToEvent( XMLElement *ev ); | |
48 | static QString journalFileName(); | 53 | } |
49 | |||
50 | CheckItem::CheckItem( QTable *t, const QString &key ) | 54 | CheckItem::CheckItem( QTable *t, const QString &key ) |
51 | : QTableItem( t, Never, "" ), checked( FALSE ), sortKey( key ) | 55 | : QTableItem( t, Never, "" ), checked( FALSE ), sortKey( key ) |
@@ -410,5 +414,5 @@ void TodoTable::load( const QString &fn ) | |||
410 | loadFile( fn, false ); | 414 | loadFile( fn, false ); |
411 | if ( QFile::exists(journalFileName()) ) { | 415 | if ( QFile::exists(journalFileName()) ) { |
412 | loadFile( journalFileName(), true ); | 416 | applyJournal( ); |
413 | save( fn ); | 417 | save( fn ); |
414 | } | 418 | } |
@@ -419,5 +423,4 @@ void TodoTable::load( const QString &fn ) | |||
419 | setSorting(true ); | 423 | setSorting(true ); |
420 | } | 424 | } |
421 | |||
422 | void TodoTable::updateVisible() | 425 | void TodoTable::updateVisible() |
423 | { | 426 | { |
@@ -447,5 +450,5 @@ void TodoTable::updateVisible() | |||
447 | hide = true; | 450 | hide = true; |
448 | } else { | 451 | } else { |
449 | // do some comparing, we have to reverse our idea here... which idea - zecke | 452 | // do some comparing, we have to reverse our idea here... which idea? - zecke |
450 | if ( !hide ) { | 453 | if ( !hide ) { |
451 | hide = true; | 454 | hide = true; |
@@ -500,4 +503,5 @@ void TodoTable::clear() | |||
500 | it != todoList.end(); ++it ) { | 503 | it != todoList.end(); ++it ) { |
501 | ToDoEvent *todo = *it; | 504 | ToDoEvent *todo = *it; |
505 | updateJournal( todo, ACTION_REMOVE, 0 ); | ||
502 | delete todo; | 506 | delete todo; |
503 | } | 507 | } |
@@ -534,5 +538,5 @@ void TodoTable::slotCheckPriority(int row, int col ) | |||
534 | 538 | ||
535 | 539 | ||
536 | void TodoTable::updateJournal( const ToDoEvent &/*todo*/, journal_action action, int row ) | 540 | void TodoTable::updateJournal( const ToDoEvent &todo, journal_action action, int row ) |
537 | { | 541 | { |
538 | QFile f( journalFileName() ); | 542 | QFile f( journalFileName() ); |
@@ -544,5 +548,22 @@ void TodoTable::updateJournal( const ToDoEvent &/*todo*/, journal_action action, | |||
544 | // todo.save( buf ); | 548 | // todo.save( buf ); |
545 | buf += " Action=\"" + QString::number( int(action) ) + "\""; | 549 | buf += " Action=\"" + QString::number( int(action) ) + "\""; |
546 | buf += " Row=\"" + QString::number( row ) + "\""; // better write the id | 550 | buf += " Uid=\"" + QString::number( todo.uid() ) + "\""; // better write the id |
551 | buf += " Completed=\""+ QString::number((int)todo.isCompleted() ) + "\""; | ||
552 | buf += " HasDate=\""+ QString::number((int)todo.hasDate() ) +"\""; | ||
553 | buf += " Priority=\"" + QString::number( todo.priority() ) + "\""; | ||
554 | QArray<int> arrat = todo.categories(); | ||
555 | QString attr; | ||
556 | for(uint i=0; i < arrat.count(); i++ ){ | ||
557 | attr.append(QString::number(arrat[i])+";" ); | ||
558 | } | ||
559 | if(!attr.isEmpty() ) // remove the last ; | ||
560 | attr.remove(attr.length()-1, 1 ); | ||
561 | buf += " Categories=\"" + attr + "\""; | ||
562 | buf += " Description=\"" + todo.description() + "\""; | ||
563 | if(todo.hasDate() ) { | ||
564 | buf += " DateYear=\""+QString::number( todo.date().year() ) + "\""; | ||
565 | buf += " DateMonth=\"" + QString::number( todo.date().month() ) + "\""; | ||
566 | buf += " DateDay=\"" + QString::number( todo.date().day() ) + "\""; | ||
567 | } | ||
547 | buf += "/>\n"; | 568 | buf += "/>\n"; |
548 | str = buf.utf8(); | 569 | str = buf.utf8(); |
@@ -716,5 +737,53 @@ int TodoTable::showCategoryId() const | |||
716 | return id; | 737 | return id; |
717 | } | 738 | } |
718 | 739 | void TodoTable::applyJournal() | |
740 | { | ||
741 | // we need to hack | ||
742 | QFile file( journalFileName() ); | ||
743 | if( file.open(IO_ReadOnly ) ) { | ||
744 | QByteArray ar = file.readAll(); | ||
745 | file.close(); | ||
746 | QFile file2( journalFileName() + "_new" ); | ||
747 | if( file2.open(IO_WriteOnly ) ){ | ||
748 | QTextStream str(&file2 ); | ||
749 | str << QString::fromLatin1("<Tasks>") << endl; | ||
750 | str << ar.data(); | ||
751 | str << QString::fromLatin1("</Tasks>") << endl; | ||
752 | file2.close(); | ||
753 | } | ||
754 | XMLElement *root = XMLElement::load(journalFileName()+ "_new"); | ||
755 | XMLElement *el = root->firstChild(); | ||
756 | el = el->firstChild(); | ||
757 | while( el ){ | ||
758 | qWarning("journal: %s %s", el->attribute("Uid" ).latin1(), el->tagName().latin1() ); | ||
759 | doApply( el ); | ||
760 | el = el->nextChild(); | ||
761 | } | ||
762 | QFile::remove(journalFileName()+ "_new" ); | ||
763 | } | ||
764 | } | ||
765 | // check Action and decide | ||
766 | void TodoTable::doApply(XMLElement *el ) | ||
767 | { | ||
768 | QString dummy; | ||
769 | bool ok; | ||
770 | int action; | ||
771 | dummy = el->attribute("Action" ); | ||
772 | action = dummy.toInt(&ok ); | ||
773 | ToDoEvent ev = xmlToEvent( el ); | ||
774 | if( ok ){ | ||
775 | switch( action ){ | ||
776 | case ACTION_ADD: | ||
777 | addEntry( ev ); | ||
778 | break; | ||
779 | case ACTION_REMOVE:{ // find an entry with the same uid and remove it then | ||
780 | break; | ||
781 | } | ||
782 | case ACTION_REPLACE: | ||
783 | break; | ||
784 | } | ||
785 | } | ||
786 | } | ||
787 | namespace { | ||
719 | static bool taskCompare( const ToDoEvent &task, const QRegExp &r, int category ) | 788 | static bool taskCompare( const ToDoEvent &task, const QRegExp &r, int category ) |
720 | { | 789 | { |
@@ -742,8 +811,62 @@ static QString journalFileName() | |||
742 | QString str; | 811 | QString str; |
743 | str = getenv( "HOME" ); | 812 | str = getenv( "HOME" ); |
744 | str += "/.todojournal"; | 813 | str += "/.opie_todojournal"; |
745 | return str; | 814 | return str; |
746 | } | 815 | } |
816 | static ToDoEvent xmlToEvent( XMLElement *element ) | ||
817 | { | ||
818 | QString dummy; | ||
819 | ToDoEvent event; | ||
820 | bool ok; | ||
821 | int dumInt; | ||
822 | // completed | ||
823 | dummy = element->attribute("Completed" ); | ||
824 | dumInt = dummy.toInt(&ok ); | ||
825 | if(ok ) event.setCompleted( dumInt == 0 ? false : true ); | ||
826 | // hasDate | ||
827 | dummy = element->attribute("HasDate" ); | ||
828 | dumInt = dummy.toInt(&ok ); | ||
829 | if(ok ) event.setHasDate( dumInt == 0 ? false: true ); | ||
830 | // set the date | ||
831 | bool hasDa = dumInt; | ||
832 | if ( hasDa ) { //parse the date | ||
833 | int year, day, month = 0; | ||
834 | year = day = month; | ||
835 | // year | ||
836 | dummy = element->attribute("DateYear" ); | ||
837 | dumInt = dummy.toInt(&ok ); | ||
838 | if( ok ) year = dumInt; | ||
839 | // month | ||
840 | dummy = element->attribute("DateMonth" ); | ||
841 | dumInt = dummy.toInt(&ok ); | ||
842 | if(ok ) month = dumInt; | ||
843 | dummy = element->attribute("DateDay" ); | ||
844 | dumInt = dummy.toInt(&ok ); | ||
845 | if(ok ) day = dumInt; | ||
846 | // set the date | ||
847 | QDate date( year, month, day ); | ||
848 | event.setDate( date); | ||
849 | } | ||
850 | dummy = element->attribute("Priority" ); | ||
851 | dumInt = dummy.toInt(&ok ); | ||
852 | if(!ok ) dumInt = ToDoEvent::NORMAL; | ||
853 | event.setPriority( dumInt ); | ||
854 | //description | ||
855 | dummy = element->attribute("Description" ); | ||
856 | event.setDescription( dummy ); | ||
857 | // category | ||
858 | dummy = element->attribute("Categories" ); | ||
859 | QStringList ids = QStringList::split(";", dummy ); | ||
860 | event.setCategories( ids ); | ||
861 | |||
862 | //uid | ||
863 | dummy = element->attribute("Uid" ); | ||
864 | dumInt = dummy.toInt(&ok ); | ||
865 | if(ok ) event.setUid( dumInt ); | ||
866 | |||
867 | return event; | ||
868 | } | ||
747 | 869 | ||
870 | } | ||
748 | // int TodoTable::rowHeight( int ) const | 871 | // int TodoTable::rowHeight( int ) const |
749 | // { | 872 | // { |
@@ -760,2 +883,3 @@ static QString journalFileName() | |||
760 | // return QMIN( pos/18, numRows()-1 ); | 883 | // return QMIN( pos/18, numRows()-1 ); |
761 | // } | 884 | // } |
885 | |||