libkpimexchange Library API Documentation

exchangecalendar.cpp

00001 /*
00002     This file is part of libkpimexchange.
00003     Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org>
00004 
00005     This library is free software; you can redistribute it and/or modify it
00006     under the terms of the GNU Library General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or (at your
00008     option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful, but WITHOUT
00011     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013     License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to the
00017     Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00018     02111-1307, USA.
00019 */
00020 
00021 #include <stdlib.h>
00022 
00023 #include <qdatetime.h>
00024 #include <qstring.h>
00025 #include <qptrlist.h>
00026 
00027 #include <kdebug.h>
00028 
00029 #include <libkcal/calendarlocal.h>
00030 #include <libkcal/calendar.h>
00031 #include <libkcal/journal.h>
00032 
00033 #include "dateset.h"
00034 #include "exchangeaccount.h"
00035 #include "exchangeclient.h"
00036 
00037 #include "exchangecalendar.h"
00038 
00039 using namespace KCal;
00040 using namespace KPIM;
00041 
00042 ExchangeCalendar::ExchangeCalendar( KPIM::ExchangeAccount* account )
00043   : Calendar()
00044 {
00045   init( account );
00046   mCache = new CalendarLocal();
00047 }
00048 
00049 ExchangeCalendar::ExchangeCalendar( KPIM::ExchangeAccount* account, const QString &timeZoneId)
00050   : Calendar(timeZoneId)
00051 {
00052   init( account );
00053   mCache = new CalendarLocal( timeZoneId );
00054 }
00055 
00056 void ExchangeCalendar::init( KPIM::ExchangeAccount* account )
00057 {
00058   kdDebug() << "ExchangeCalendar::init()" << endl;
00059   mAccount = account;
00060   mClient = new ExchangeClient( account );
00061   mDates = new DateSet();
00062 
00063   mEventDates = new QMap<Event,QDateTime>();
00064   mCacheDates = new QMap<QDate, QDateTime>();
00065 
00066   mCachedSeconds = 600; // After 5 minutes, reread from server
00067   // mOldestDate = 0L;
00068   // mNewestDate = 0L;
00069 }
00070 
00071 
00072 ExchangeCalendar::~ExchangeCalendar()
00073 {
00074   kdDebug() << "Destructing ExchangeCalendar" << endl;
00075   close();
00076   // delete mNewestDate;
00077   // delete mOldestDate;
00078   delete mDates;
00079   delete mClient;
00080   delete mEventDates;
00081   delete mCacheDates;
00082   delete mCache;
00083 }
00084  
00085 
00086 bool ExchangeCalendar::load( const QString &fileName )
00087 {
00088   // return mCache->load( fileName );
00089   return true;
00090 }
00091 
00092 bool ExchangeCalendar::save( const QString &fileName, CalFormat *format )
00093 {
00094   return mCache->save( fileName, format );
00095 }
00096 
00097 void ExchangeCalendar::close()
00098 {
00099   mCache->close();
00100   setModified( false );
00101 }
00102 
00103 
00104 void ExchangeCalendar::addEvent(Event *anEvent)
00105 {
00106   kdDebug() << "ExchangeCalendar::addEvent" << endl;
00107   mCache->addEvent( anEvent );
00108   insertEvent(anEvent);
00109  
00110   anEvent->registerObserver( this );
00111 
00112   setModified( true );
00113 }
00114 
00115 // probably not really efficient, but...it works for now.
00116 void ExchangeCalendar::deleteEvent(Event *event)
00117 {
00118   kdDebug(5800) << "ExchangeCalendar::deleteEvent" << endl;
00119   mCache->deleteEvent( event );
00120   setModified( true );
00121 }
00122 
00123 
00124 Event *ExchangeCalendar::event( const QString &uid )
00125 {
00126   kdDebug(5800) << "ExchangeCalendar::event(): " << uid << endl;
00127 
00128   return mCache->event( uid );
00129 }
00130 
00131 void ExchangeCalendar::addTodo(Todo *todo)
00132 {
00133   mCache->addTodo( todo );
00134 
00135   todo->registerObserver( this );
00136 
00137   setModified( true );
00138 }
00139 
00140 void ExchangeCalendar::deleteTodo(Todo *todo)
00141 {
00142   mCache->deleteTodo( todo );
00143 
00144   setModified( true );
00145 }
00146 
00147 QPtrList<Todo> ExchangeCalendar::rawTodos() const
00148 {
00149   return mCache->rawTodos();
00150 }
00151 
00152 Todo *ExchangeCalendar::todo( const QString &uid )
00153 {
00154   return mCache->todo( uid );
00155 }
00156 
00157 QPtrList<Todo> ExchangeCalendar::todos( const QDate &date )
00158 {
00159   return mCache->todos( date );
00160 }
00161 
00162 int ExchangeCalendar::numEvents(const QDate &qd)
00163 {
00164    kdDebug() << "ExchangeCalendar::numEvents" << endl;
00165   return mCache->numEvents( qd );
00166 }
00167 
00168 
00169 Alarm::List ExchangeCalendar::alarmsTo( const QDateTime &to )
00170 {
00171   return mCache->alarmsTo( to );
00172 }
00173 
00174 Alarm::List ExchangeCalendar::alarms( const QDateTime &from, const QDateTime &to )
00175 {
00176   kdDebug(5800) << "ExchangeCalendar::alarms(" << from.toString() << " - " << to.toString() << ")\n";
00177   return mCache->alarms( from, to );
00178 }
00179 
00180 /****************************** PROTECTED METHODS ****************************/
00181 
00182 // after changes are made to an event, this should be called.
00183 void ExchangeCalendar::update(IncidenceBase *incidence)
00184 {
00185   setModified( true );
00186 }
00187 
00188 // this function will take a VEvent and insert it into the event
00189 // dictionary for the ExchangeCalendar.  If there is no list of events for that
00190 // particular location in the dictionary, a new one will be created.
00191 void ExchangeCalendar::insertEvent(const Event *anEvent)
00192 {
00193   kdDebug() << "ExchangeCalendar::insertEvent" << endl;
00194  
00195 }
00196 
00197 // taking a QDate, this function will look for an eventlist in the dict
00198 // with that date attached -
00199 QPtrList<Event> ExchangeCalendar::rawEventsForDate(const QDate &qd, bool sorted)
00200 {
00201   kdDebug() << "ExchangeCalendar::rawEventsForDate(" << qd.toString() << "," << sorted << ")" << endl;
00202  
00203   // If the events for this date are not in the cache, or if they are old,
00204   // get them again
00205   QDateTime now = QDateTime::currentDateTime();
00206   // kdDebug() << "Now is " << now.toString() << endl;
00207   // kdDebug() << "mDates: " << mDates << endl;
00208   // kdDebug() << "mDates->contains(qd) is " << mDates->contains( qd ) << endl;
00209   QDate start = QDate( qd.year(), qd.month(), 1 ); // First day of month
00210   if ( !mDates->contains( start ) || (*mCacheDates)[start].secsTo( now ) > mCachedSeconds ) {
00211     kdDebug() << "Reading events for month of " << start.toString() << endl;
00212     QDate end = start.addMonths( 1 ).addDays( -1 ); // Last day of month
00213     mClient->downloadSynchronous( mCache, start, end, true ); // Show progress dialog
00214     mDates->add( start );
00215     mCacheDates->insert( start, now );
00216   }
00217 
00218   // Events are safely in the cache now, return them from cache
00219   QPtrList<Event> events = mCache->rawEventsForDate( qd, sorted );
00220   kdDebug() << "Found " << events.count() << " events." << endl;
00221   return events;
00222 
00223 /*
00224   if (!sorted) {
00225     return eventList;
00226   }
00227 
00228   //  kdDebug(5800) << "Sorting events for date\n" << endl;
00229   // now, we have to sort it based on getDtStart.time()
00230   QPtrList<Event> eventListSorted;
00231   for (anEvent = eventList.first(); anEvent; anEvent = eventList.next()) {
00232     if (!eventListSorted.isEmpty() &&
00233         anEvent->dtStart().time() < eventListSorted.at(0)->dtStart().time()) {
00234       eventListSorted.insert(0,anEvent);
00235       goto nextToInsert;
00236     }
00237     for (i = 0; (uint) i+1 < eventListSorted.count(); i++) {
00238       if (anEvent->dtStart().time() > eventListSorted.at(i)->dtStart().time() &&
00239           anEvent->dtStart().time() <= eventListSorted.at(i+1)->dtStart().time()) {
00240         eventListSorted.insert(i+1,anEvent);
00241         goto nextToInsert;
00242       }
00243     }
00244     eventListSorted.append(anEvent);
00245   nextToInsert:
00246     continue;
00247   }
00248   return eventListSorted;
00249 */
00250 }
00251 
00252 
00253 QPtrList<Event> ExchangeCalendar::rawEvents( const QDate &start, const QDate &end,
00254                                           bool inclusive )
00255 {
00256    kdDebug() << "ExchangeCalendar::rawEvents(start,end,inclusive)" << endl;
00257  return mCache->rawEvents( start, end, inclusive );
00258 }
00259 
00260 QPtrList<Event> ExchangeCalendar::rawEventsForDate(const QDateTime &qdt)
00261 {
00262    kdDebug() << "ExchangeCalendar::rawEventsForDate(qdt)" << endl;
00263  return rawEventsForDate( qdt.date() );
00264 }
00265 
00266 QPtrList<Event> ExchangeCalendar::rawEvents()
00267 {
00268    kdDebug() << "ExchangeCalendar::rawEvents()" << endl;
00269  return mCache->rawEvents();
00270 }
00271 
00272 void ExchangeCalendar::addJournal(Journal *journal)
00273 {
00274   kdDebug(5800) << "Adding Journal on " << journal->dtStart().toString() << endl;
00275   mCache->addJournal( journal );
00276 
00277   journal->registerObserver( this );
00278 
00279   setModified( true );
00280 }
00281 
00282 Journal *ExchangeCalendar::journal(const QDate &date)
00283 {
00284 //  kdDebug(5800) << "ExchangeCalendar::journal() " << date.toString() << endl;
00285   return mCache->journal( date );
00286 }
00287 
00288 Journal *ExchangeCalendar::journal(const QString &uid)
00289 {
00290   return mCache->journal( uid );
00291 }
00292 
00293 QPtrList<Journal> ExchangeCalendar::journals()
00294 {
00295   return mCache->journals();
00296 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sat Oct 18 02:47:24 2003 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001