kitchensync Library API Documentation

todo.cpp

00001 /* 00002 This file is part of KitchenSync. 00003 00004 Copyright (c) 2002,2003 Holger Freyther <freyther@kde.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00019 Boston, MA 02111-1307, USA. 00020 */ 00021 00022 #include <qfile.h> 00023 00024 #include <kdebug.h> 00025 00026 #include <calendarsyncee.h> 00027 #include <idhelper.h> 00028 #include <libkcal/calendarlocal.h> 00029 00030 #include "device.h" 00031 #include "todo.h" 00032 00033 using namespace OpieHelper; 00034 00035 00036 ToDo::ToDo( CategoryEdit* edit, 00037 KSync::KonnectorUIDHelper* helper, 00038 const QString &tz, 00039 bool meta, Device* dev) 00040 : Base( edit, helper, tz, meta, dev ) 00041 { 00042 } 00043 ToDo::~ToDo(){ 00044 } 00045 KCal::Todo* ToDo::dom2todo( QDomElement e, ExtraMap& extra,const QStringList& lst ) { 00046 QString dummy; 00047 int Int; 00048 KCal::Todo* todo = new KCal::Todo(); 00049 QStringList list = QStringList::split(";", e.attribute("Categories") ); 00050 QStringList categories; 00051 00052 QString cat; 00053 for ( uint i = 0; i < list.count(); i++ ) { 00054 cat = m_edit->categoryById( list[i], "Todo List"); 00055 /* only if cat is not empty and not already added */ 00056 if (!cat.isEmpty() && !categories.contains( cat) ) 00057 categories.append(cat ); 00058 } 00059 if (!categories.isEmpty() ) { 00060 kdDebug(5226) << "List " << list.join(";") << endl; 00061 kdDebug(5226) << "TransLated " << categories.join(";") << endl; 00062 todo->setCategories( categories ); 00063 } 00064 00065 todo->setDescription(e.attribute("Description" ) ); 00066 todo->setSummary( e.attribute("Summary") ); //opie only 00067 if ( ( device() && device()->distribution() == Device::Zaurus ) || todo->summary().isEmpty() ) 00068 todo->setSummary( e.attribute("Description").stripWhiteSpace().left(20).simplifyWhiteSpace() ); 00069 00070 setUid(todo, e.attribute("Uid") ); 00071 00072 dummy = e.attribute("Completed"); 00073 00074 /* 00075 * if setCompleted is called 00076 * libkcal decides to put 00077 * percentage done to 100% 00078 * but if I put percentage 00079 * to say 50% it is not uncompleting the item 00080 * and if setCompleted( false ) is called 00081 * likcal sets percent completed to 0 00082 */ 00083 Int = dummy.toInt(); 00084 kdDebug(5227) << " Completed " << dummy << " " << Int << endl; 00085 00086 /* !0 */ 00087 if ( Int == 0) { 00088 kdDebug(5227) << "Calling not completed " << endl; 00089 todo->setCompleted( false ); 00090 /* 00091 * libkcal wants to be too smart again 00092 * 100% percent done but not completed 00093 * will be marked as completed... 00094 */ 00095 todo->setPercentComplete( e.attribute("Progress").toInt() ); 00096 }else{ 00097 kdDebug(5227) << "Todo is completed " << endl; 00098 todo->setCompleted(true ); 00099 } 00100 00101 00102 00103 kdDebug(5227) << "dummy completed " << todo->isCompleted() << endl; 00104 00105 dummy = e.attribute("Priority" ); 00106 todo->setPriority(dummy.toInt( ) ); 00107 dummy = e.attribute("HasDate" ); 00108 bool status = dummy.toInt( ); 00109 if(status){ 00110 kdDebug(5227) << "Has Due Date " << endl; 00111 todo->setHasDueDate(true ); 00112 QDateTime time = QDateTime::currentDateTime(); 00113 QDate date; 00114 dummy = e.attribute("DateDay" ); 00115 int day= dummy.toInt( ); 00116 int month = e.attribute("DateMonth").toInt( ); 00117 int year = e.attribute("DateYear").toInt( ); 00118 date.setYMD(year, month, day); 00119 time.setDate( date ); 00120 todo->setDtDue( time ); 00121 /* 00122 * libkcal does not set HasDueDate TRUE 00123 * if we supply a due date 00124 */ 00125 todo->setHasDueDate( true ); 00126 }else{ 00127 todo->setHasDueDate( false ); 00128 } 00129 00130 // time to add extra attributes 00131 extra.add("todo", e.attribute("Uid"), e.attributes(), lst ); 00132 00133 return todo; 00134 } 00135 00136 bool ToDo::toKDE( const QString &fileName, ExtraMap& map, KSync::CalendarSyncee *syncee ) 00137 { 00138 syncee->setSource( "OpieTodo" ); 00139 syncee->setIdentifier( "Opie" ); 00140 00141 if ( device() ) 00142 syncee->setSupports( device()->supports( Device::Todolist ) ); 00143 00144 QFile file( fileName ); 00145 if ( !file.open( IO_ReadOnly ) ) { 00146 return false; 00147 } 00148 00149 QDomDocument doc( "mydocument" ); 00150 if ( !doc.setContent( &file ) ) { 00151 return false; 00152 } 00153 00154 QStringList attr = attributes(); 00155 QDomElement docElem = doc.documentElement(); 00156 KCal::Todo *todo; 00157 QDomNode n = docElem.firstChild(); 00158 while ( !n.isNull() ) { 00159 QDomElement e = n.toElement(); 00160 if ( !e.isNull() ) { 00161 if ( e.tagName() == "Task" ) { 00162 todo = dom2todo( e, map,attr ); 00163 KSync::CalendarSyncEntry* entry; 00164 entry = new KSync::CalendarSyncEntry( todo, syncee ); 00165 syncee->addEntry( entry ); 00166 } 00167 } 00168 00169 n = n.nextSibling(); 00170 } 00171 00172 return true; 00173 } 00174 00175 KTempFile* ToDo::fromKDE( KSync::CalendarSyncee* syncee, ExtraMap& map ) 00176 { 00177 // KDE ID clear bit first 00178 m_kde2opie.clear(); 00179 Kontainer::ValueList newIds = syncee->ids( "TodoSyncEntry"); 00180 for ( Kontainer::ValueList::ConstIterator idIt = newIds.begin(); idIt != newIds.end(); ++idIt ) { 00181 m_helper->addId("TodoSyncEntry", (*idIt).first(), (*idIt).second() ); 00182 } 00183 // update m_helper first; 00184 KTempFile* tmpFile = file(); 00185 if (tmpFile->textStream() ) { 00186 // clear list 00187 KSync::CalendarSyncEntry* entry; 00188 QTextStream *stream = tmpFile->textStream(); 00189 stream->setEncoding( QTextStream::UnicodeUTF8 ); 00190 *stream << "<!DOCTYPE Tasks>" << endl; 00191 *stream << "<Tasks>" << endl; 00192 for ( entry = (KSync::CalendarSyncEntry*)syncee->firstEntry(); 00193 entry != 0l; 00194 entry = (KSync::CalendarSyncEntry*)syncee->nextEntry() ) 00195 { 00196 if ( entry->state() == KSync::SyncEntry::Removed ) 00197 continue; 00198 00199 KCal::Todo *todo = dynamic_cast<KCal::Todo*>( entry->incidence() ); 00200 if ( !todo ) 00201 continue; 00202 00203 *stream << todo2String( todo, map ) << endl; 00204 } 00205 *stream << "</Tasks>" << endl; 00206 } 00207 if (m_helper) 00208 m_helper->replaceIds( "TodoSyncEntry", m_kde2opie ); 00209 00210 tmpFile->close(); 00211 00212 return tmpFile; 00213 } 00214 void ToDo::setUid( KCal::Todo* todo, const QString &uid ) 00215 { 00216 todo->setUid( kdeId( "TodoSyncEntry", uid ) ); 00217 } 00218 00219 QString ToDo::todo2String( KCal::Todo* todo, ExtraMap& map ) 00220 { 00221 QString text; 00222 text.append("<Task "); 00223 QStringList list = todo->categories(); 00224 text.append( "Categories=\"" + categoriesToNumber( list ) + "\" " ); 00225 kdDebug(5227) << " todo->isCompleted " << todo->isCompleted() << endl; 00226 text.append( "Completed=\""+QString::number( todo->isCompleted()) + "\" " ); 00227 text.append( "Progress=\"" + QString::number( todo->percentComplete() ) + "\" "); 00228 00229 /* if it is not a Stock Zaurus we will right the summary */ 00230 if ( device() && device()->distribution() != Device::Zaurus ) 00231 text.append( "Summary=\"" + escape( todo->summary() ) + "\" "); 00232 00233 if ( todo->hasDueDate() ) { 00234 text.append( "HasDate=\"1\" "); 00235 QDateTime time = todo->dtDue(); 00236 text.append( "DateDay=\"" +QString::number( time.date().day() ) + "\" "); 00237 text.append( "DateMonth=\"" + QString::number( time.date().month() ) + "\" " ); 00238 text.append( "DateYear=\"" + QString::number( time.date().year() )+ "\" " ); 00239 }else{ 00240 text.append( "HasDate=\"0\" "); 00241 } 00242 text.append( "Priority=\"" + QString::number( todo->priority() ) +"\" " ); 00243 00244 /* if Opie let's write the description right away 00245 * else we need to find out if description is empty and then 00246 * fallback to the summary if both are empty you're lost! 00247 **/ 00248 if ( device() && device()->distribution() != Device::Zaurus ) 00249 text.append( "Description=\"" +escape( todo->description() ) + "\" " ); 00250 else{ 00251 QString desc = todo->description().isEmpty() ? todo->summary() : todo->description(); 00252 text.append( "Description=\"" +escape( desc ) ); 00253 } 00254 00255 // id hacking We don't want to have the ids growing and growing 00256 // when an id is used again it will be put to the used list and after done 00257 // with syncing we will replace the former 00258 QString uid = konnectorId("TodoSyncEntry", todo->uid() ); 00259 text.append("Uid=\"" +uid + "\" " ); 00260 00261 /* add custom entries */ 00262 text.append( map.toString("todo", uid ) ); 00263 00264 text.append(" />"); 00265 return text; 00266 } 00267 00268 QStringList ToDo::attributes()const { 00269 QStringList lst; 00270 lst << "Categories"; 00271 lst << "Completed"; 00272 lst << "Progress"; 00273 lst << "Summary"; 00274 lst << "HasDate"; 00275 lst << "DateDay"; 00276 lst << "DateMonth"; 00277 lst << "DateYear"; 00278 lst << "Priority"; 00279 lst << "Description"; 00280 lst << "Uid"; 00281 00282 return lst; 00283 }
KDE Logo
This file is part of the documentation for kitchensync Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Oct 1 15:19:00 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003