kpilot

vcal-conduit.cc

Go to the documentation of this file.
00001 /* KPilot
00002 **
00003 ** Copyright (C) 2002-2003 Reinhold Kainhofer
00004 ** Copyright (C) 2001 by Dan Pilone
00005 **
00006 ** This file defines the vcal-conduit plugin.
00007 */
00008 
00009 /*
00010 ** This program is free software; you can redistribute it and/or modify
00011 ** it under the terms of the GNU General Public License as published by
00012 ** the Free Software Foundation; either version 2 of the License, or
00013 ** (at your option) any later version.
00014 **
00015 ** This program is distributed in the hope that it will be useful,
00016 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00018 ** GNU General Public License for more details.
00019 **
00020 ** You should have received a copy of the GNU General Public License
00021 ** along with this program in a file called COPYING; if not, write to
00022 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00023 ** MA 02110-1301, USA.
00024 */
00025 
00026 /*
00027 ** Bug reports and questions can be sent to kde-pim@kde.org
00028 */
00029 
00030 #include "options.h"
00031 
00032 #include <libkcal/calendar.h>
00033 #include <libkcal/calendarlocal.h>
00034 #include <libkcal/recurrence.h>
00035 #include <libkcal/vcalformat.h>
00036 
00037 #include "pilotDateEntry.h"
00038 #include "pilotDatabase.h"
00039 
00040 #include "vcal-conduit.moc"
00041 #include "vcalconduitSettings.h"
00042 
00043 #include "kcalRecord.h"
00044 #include "vcalRecord.h"
00045 
00046 
00047 extern "C"
00048 {
00049 
00050 unsigned long version_conduit_vcal = Pilot::PLUGIN_API;
00051 
00052 }
00053 
00054 
00055 
00056 
00057 VCalConduitPrivate::VCalConduitPrivate(KCal::Calendar *b) :
00058     VCalConduitPrivateBase(b)
00059 {
00060     fAllEvents.setAutoDelete(false);
00061 }
00062 
00063 void VCalConduitPrivate::addIncidence(KCal::Incidence*e)
00064 {
00065     fAllEvents.append(dynamic_cast<KCal::Event*>(e));
00066     fCalendar->addEvent(dynamic_cast<KCal::Event*>(e));
00067 }
00068 
00069 int VCalConduitPrivate::updateIncidences()
00070 {
00071     FUNCTIONSETUP;
00072     if (!fCalendar) return 0;
00073     fAllEvents = fCalendar->events();
00074     fAllEvents.setAutoDelete(false);
00075     return fAllEvents.count();
00076 }
00077 
00078 
00079 void VCalConduitPrivate::removeIncidence(KCal::Incidence *e)
00080 {
00081     // use dynamic_cast which returns a null pointer if the class does not match...
00082     fAllEvents.remove(dynamic_cast<KCal::Event*>(e));
00083     if (!fCalendar) return;
00084     fCalendar->deleteEvent(dynamic_cast<KCal::Event*>(e));
00085     // now just in case we're in the middle of reading through our list
00086     // and we delete something, set reading to false so we start at the
00087     // top again next time and don't have problems with our iterator
00088     reading = false;
00089 }
00090 
00091 
00092 KCal::Incidence *VCalConduitPrivate::findIncidence(recordid_t id)
00093 {
00094     KCal::Event::List::ConstIterator it;
00095     for( it = fAllEvents.begin(); it != fAllEvents.end(); ++it ) {
00096         KCal::Event *event = *it;
00097         if ((recordid_t)event->pilotId() == id) return event;
00098     }
00099     return 0L;
00100 }
00101 
00102 KCal::Incidence *VCalConduitPrivate::findIncidence(PilotRecordBase *tosearch)
00103 {
00104     PilotDateEntry*entry=dynamic_cast<PilotDateEntry*>(tosearch);
00105     if (!entry) return 0L;
00106 
00107     QString title=entry->getDescription();
00108     QDateTime dt=readTm( entry->getEventStart() );
00109 
00110     KCal::Event::List::ConstIterator it;
00111     for( it = fAllEvents.begin(); it != fAllEvents.end(); ++it ) {
00112         KCal::Event *event = *it;
00113         if ( (event->dtStart() == dt) && (event->summary() == title) ) return event;
00114     }
00115     return 0L;
00116 }
00117 
00118 
00119 
00120 KCal::Incidence *VCalConduitPrivate::getNextIncidence()
00121 {
00122     FUNCTIONSETUP;
00123 
00124     if (reading) {
00125         ++fAllEventsIterator;
00126     } else {
00127         reading=true;
00128         fAllEventsIterator = fAllEvents.begin();
00129     }
00130     // At end of list, or empty list.
00131     return (fAllEventsIterator == fAllEvents.end()) ? 0L : *fAllEventsIterator;
00132 }
00133 
00138 KCal::Incidence *VCalConduitPrivate::getNextModifiedIncidence()
00139 {
00140     FUNCTIONSETUP;
00141     KCal::Event*e=0L;
00142     if (!reading)
00143     {
00144         // Start from the top
00145         reading=true;
00146         fAllEventsIterator = fAllEvents.begin();
00147     }
00148     else
00149     {
00150         // Move on from current position
00151         ++fAllEventsIterator;
00152     }
00153 
00154     // Fetch (new) current if possible.
00155     if ( fAllEventsIterator != fAllEvents.end() ) e = *fAllEventsIterator;
00156     // Then walk the list until we find an unsynced entry
00157     while ( fAllEventsIterator != fAllEvents.end() &&
00158         e && e->syncStatus()!=KCal::Incidence::SYNCMOD && e->pilotId() > 0)
00159     {
00160         e = (++fAllEventsIterator != fAllEvents.end()) ? *fAllEventsIterator : 0L;
00161     }
00162     return (fAllEventsIterator == fAllEvents.end()) ? 0L :  *fAllEventsIterator;
00163 }
00164 
00165 
00166 
00167 /****************************************************************************
00168  *                          VCalConduit class                               *
00169  ****************************************************************************/
00170 
00171 VCalConduit::VCalConduit(KPilotLink *d,
00172     const char *n,
00173     const QStringList &a) :
00174     VCalConduitBase(d,n,a),
00175     fAppointmentAppInfo( 0L )
00176 {
00177     FUNCTIONSETUP;
00178     fConduitName=i18n("Calendar");
00179 }
00180 
00181 
00182 VCalConduit::~VCalConduit()
00183 {
00184 //  FUNCTIONSETUP;
00185 }
00186 
00187 VCalConduitPrivateBase *VCalConduit::createPrivateCalendarData(KCal::Calendar *fCalendar) {
00188     return new VCalConduitPrivate(fCalendar);
00189 }
00190 
00191 void VCalConduit::_getAppInfo()
00192 {
00193     FUNCTIONSETUP;
00194     // get the address application header information
00195     KPILOT_DELETE(fAppointmentAppInfo);
00196     fAppointmentAppInfo = new PilotDateInfo( fDatabase );
00197 }
00198 
00199 const QString VCalConduit::getTitle(PilotRecordBase *de)
00200 {
00201     PilotDateEntry*d=dynamic_cast<PilotDateEntry*>(de);
00202     if (d) return QString(d->getDescription());
00203     return QString::null;
00204 }
00205 
00206 
00207 
00208 PilotRecord *VCalConduit::recordFromIncidence(PilotRecordBase *de, const KCal::Incidence*e)
00209 {
00210     FUNCTIONSETUP;
00211     if (!de || !e)
00212     {
00213         DEBUGKPILOT << fname
00214             << ": got NULL entry or NULL incidence." << endl;
00215         return 0L;
00216     }
00217 
00218     if ( (e->recurrenceType() == KCal::Recurrence::rYearlyDay) ||
00219         (e->recurrenceType() ==  KCal::Recurrence::rYearlyPos) )
00220     {
00221         // Warn ahead of time
00222         emit logMessage(i18n("Event \"%1\" has a yearly recurrence other than by month, will change this to recurrence by month on handheld.").arg(e->summary()));
00223     }
00224 
00225     PilotDateEntry *dateEntry = dynamic_cast<PilotDateEntry*>(de);
00226     if (!dateEntry)
00227     {
00228         // Secretly wasn't a date entry after all
00229         return 0L;
00230     }
00231 
00232     const KCal::Event *event = dynamic_cast<const KCal::Event *>(e);
00233     if (!event)
00234     {
00235         DEBUGKPILOT << fname << ": Incidence is not an event." << endl;
00236         return 0L;
00237     }
00238 
00239     if (KCalSync::setDateEntry(dateEntry, event,*fAppointmentAppInfo->categoryInfo()))
00240     {
00241         return dateEntry->pack();
00242     }
00243     else
00244     {
00245         return 0L;
00246     }
00247 }
00248 
00249 KCal::Incidence *VCalConduit::incidenceFromRecord(KCal::Incidence *e, const PilotRecordBase  *de)
00250 {
00251     FUNCTIONSETUP;
00252 
00253     if (!de || !e)
00254     {
00255         DEBUGKPILOT << fname
00256             << ": Got NULL entry or NULL incidence." << endl;
00257         return 0L;
00258     }
00259 
00260     const PilotDateEntry *dateEntry = dynamic_cast<const PilotDateEntry *>(de);
00261     if (!dateEntry)
00262     {
00263         DEBUGKPILOT << fname << ": HH record not a date entry." << endl;
00264         return 0L;
00265     }
00266 
00267     KCal::Event *event = dynamic_cast<KCal::Event *>(e);
00268     if (!event)
00269     {
00270         DEBUGKPILOT << fname << ": Incidence is not an event." << endl;
00271         return 0L;
00272     }
00273 
00274     KCalSync::setEvent(event, dateEntry,*fAppointmentAppInfo->categoryInfo());
00275     return e;
00276 }
00277 
00278 
00279 
00280 PilotRecordBase * VCalConduit::newPilotEntry(PilotRecord*r)
00281 {
00282     return new PilotDateEntry(r);
00283 }
00284 
00285 KCal::Incidence* VCalConduit::newIncidence()
00286 {
00287   return new KCal::Event;
00288 }
00289 
00290 static VCalConduitSettings *config_vcal = 0L;
00291 
00292 VCalConduitSettings *VCalConduit::theConfig()
00293 {
00294     if (!config_vcal)
00295     {
00296         config_vcal = new VCalConduitSettings(CSL1("Calendar"));
00297     }
00298 
00299     return config_vcal;
00300 }
00301 
00302 VCalConduitSettings *VCalConduit::config() {
00303     return theConfig();
00304 }
00305 
00306 
00307 
00308 // vim: ts=4:sw=4:noexpandtab:
00309