libkcal Library API Documentation

resourcekabc.cpp

00001 /*
00002     This file is part of libkcal.
00003 
00004     Copyright (c) 2003 Cornelius Schumacher <schumacher@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 <typeinfo>
00023 #include <stdlib.h>
00024 
00025 #include <qdatetime.h>
00026 #include <qstring.h>
00027 #include <qptrlist.h>
00028 
00029 #include <klocale.h>
00030 #include <kdebug.h>
00031 #include <kurl.h>
00032 #include <kio/job.h>
00033 #include <kstandarddirs.h>
00034 
00035 #include <kabc/stdaddressbook.h>
00036 #include <kabc/locknull.h>
00037 
00038 #include "vcaldrag.h"
00039 #include "vcalformat.h"
00040 #include "icalformat.h"
00041 #include "exceptions.h"
00042 #include "incidence.h"
00043 #include "event.h"
00044 #include "todo.h"
00045 #include "journal.h"
00046 #include "filestorage.h"
00047 #include "libkcal/alarm.h"
00048 
00049 #include <kresources/configwidget.h>
00050 
00051 #include "resourcekabcconfig.h"
00052 
00053 #include "resourcekabc.h"
00054 
00055 using namespace KCal;
00056 
00057 extern "C"
00058 {
00059   void *init_kcal_kabc()
00060   {
00061     return new KRES::PluginFactory<ResourceKABC,ResourceKABCConfig>();
00062   }
00063 }
00064 
00065 
00066 ResourceKABC::ResourceKABC( const KConfig* config )
00067   : ResourceCalendar( config ), mAlarmDays( 1 ), mAlarm( false )
00068 {
00069   if ( config ) {
00070     readConfig( config );
00071   }
00072 
00073   init();
00074 }
00075 
00076 ResourceKABC::ResourceKABC( )
00077   : ResourceCalendar( 0 ), mAlarmDays( 1 ), mAlarm( false )
00078 {
00079   init();
00080 }
00081 
00082 ResourceKABC::~ResourceKABC()
00083 {
00084   delete mLock;
00085 }
00086 
00087 void ResourceKABC::init()
00088 {
00089   setType( "birthdays" );
00090 
00091   mOpen = false;
00092   setReadOnly( true );
00093 
00094   mLock = new KABC::LockNull( false );
00095 
00096   mAddressbook = 0;
00097 }
00098 
00099 void ResourceKABC::readConfig( const KConfig *config )
00100 {
00101   mAlarmDays = config->readNumEntry( "AlarmDays", 1 );
00102   mAlarm = config->readBoolEntry( "Alarm", false );
00103 }
00104 
00105 void ResourceKABC::writeConfig( KConfig *config )
00106 {
00107   ResourceCalendar::writeConfig( config );
00108   config->writeEntry( "AlarmDays", mAlarmDays );
00109   config->writeEntry( "Alarm", mAlarm );
00110   load();
00111 }
00112 
00113 
00114 bool ResourceKABC::doOpen()
00115 {
00116   kdDebug(5800) << "ResourceKABC::doOpen()" << endl;
00117 
00118   mAddressbook = KABC::StdAddressBook::self();
00119   connect( mAddressbook, SIGNAL(addressBookChanged(AddressBook*)), SLOT( reload() ) );
00120 
00121   mOpen = true;
00122 
00123   return true;
00124 }
00125 
00126 bool ResourceKABC::doLoad()
00127 {
00128   kdDebug(5800) << "ResourceKABC::load()" << endl;
00129 
00130   if ( !mOpen ) return true;
00131 
00132   mCalendar.close();
00133 
00134   // import from kabc
00135   QString summary;
00136 
00137   KABC::Addressee::List anniversaries;
00138   KABC::Addressee::List::Iterator addrIt;
00139 
00140   KABC::AddressBook::Iterator it;
00141   for ( it = mAddressbook->begin(); it != mAddressbook->end(); ++it ) {
00142 
00143     QDateTime birthdate = (*it).birthday().date();
00144     if ( birthdate.isValid() ) {
00145       kdDebug(5800) << "found a birthday " << birthdate.toString() << endl;
00146 
00147       QString name = (*it).nickName();
00148       if (name.isEmpty()) name = (*it).realName();
00149       summary = i18n("%1's birthday").arg( name );
00150 
00151       Event *ev = new Event();
00152 
00153       ev->setDtStart(birthdate);
00154       ev->setDtEnd(birthdate);
00155       ev->setHasEndDate(true);
00156       ev->setFloats(true);
00157       ev->setTransparency( Event::Transparent );
00158 
00159       ev->setSummary(summary);
00160 
00161       // Set the recurrence
00162       Recurrence *vRecurrence = ev->recurrence();
00163       vRecurrence->setRecurStart(birthdate);
00164       vRecurrence->setYearly(Recurrence::rYearlyMonth,1,-1);
00165       vRecurrence->addYearlyNum(birthdate.date().month());
00166 
00167       ev->clearAlarms();
00168 
00169       if ( mAlarm ) {
00170         // Set the alarm
00171         Alarm* vAlarm = ev->newAlarm();
00172         vAlarm->setText(summary);
00173         vAlarm->setTime(birthdate);
00174         // 24 hours before
00175         vAlarm->setStartOffset( -1440 * mAlarmDays );
00176         vAlarm->setEnabled(true);
00177       }
00178 
00179       // insert category
00180       ev->setCategories(i18n("Birthday"));
00181 
00182       ev->setReadOnly( true );
00183       mCalendar.addEvent(ev);
00184       kdDebug(5800) << "imported " << birthdate.toString() << endl;
00185     }
00186 
00187         QString anniversary_string = (*it).custom( "KADDRESSBOOK", "X-Anniversary" );
00188         if (anniversary_string.isEmpty() )
00189             continue;
00190     QDateTime anniversary = QDate::fromString( anniversary_string, Qt::ISODate );
00191     if ( !anniversary.isValid() )
00192       continue;
00193 
00194     QString name = (*it).custom( "KADDRESSBOOK", "X-SpousesName" );
00195     if ( name.isEmpty() )
00196       anniversaries.append( *it );
00197     else {
00198       bool found = false;
00199       for ( addrIt = anniversaries.begin(); addrIt != anniversaries.end(); ++addrIt ) {
00200         if ( name == (*addrIt).realName() ) {
00201           QDateTime spouseAnniversary = QDate::fromString( (*addrIt).custom( "KADDRESSBOOK", "X-Anniversary" ), Qt::ISODate );
00202           if ( anniversary == spouseAnniversary ) {
00203             found = true;
00204             break;
00205 
00206           }
00207         }
00208       }
00209 
00210       if ( !found )
00211         anniversaries.append( *it );
00212     }
00213   }
00214 
00215   for ( addrIt = anniversaries.begin(); addrIt != anniversaries.end(); ++addrIt ) {
00216     QDateTime anniversary = QDate::fromString( (*addrIt).custom( "KADDRESSBOOK", "X-Anniversary" ), Qt::ISODate );
00217     kdDebug(5800) << "found a anniversary " << anniversary.toString() << endl;
00218 
00219     QString name = (*addrIt).nickName();
00220     QString spouseName = (*addrIt).custom( "KADDRESSBOOK", "X-SpousesName" );
00221     if ( name.isEmpty() )
00222       name = (*addrIt).givenName();
00223     if ( !spouseName.isEmpty() ) {
00224       KABC::Addressee spouse;
00225       spouse.setNameFromString( spouseName );
00226       name += " & " + spouse.givenName();
00227     }
00228     summary = i18n("%1's anniversary").arg( name );
00229 
00230     Event *ev = new Event();
00231 
00232     ev->setDtStart(anniversary);
00233     ev->setDtEnd(anniversary);
00234     ev->setHasEndDate(true);
00235     ev->setFloats(true);
00236 
00237     ev->setSummary(summary);
00238 
00239     // Set the recurrence
00240     Recurrence *vRecurrence = ev->recurrence();
00241     vRecurrence->setRecurStart(anniversary);
00242     vRecurrence->setYearly(Recurrence::rYearlyMonth,1,-1);
00243     vRecurrence->addYearlyNum(anniversary.date().month());
00244 
00245     ev->clearAlarms();
00246 
00247     if ( mAlarm ) {
00248       // Set the alarm
00249       Alarm* vAlarm = ev->newAlarm();
00250       vAlarm->setText(summary);
00251       vAlarm->setTime(anniversary);
00252       // 24 hours before
00253       vAlarm->setStartOffset( -1440 * mAlarmDays );
00254       vAlarm->setEnabled(true);
00255     }
00256 
00257     // insert category
00258     ev->setCategories(i18n("Anniversary"));
00259 
00260     ev->setReadOnly( true );
00261     mCalendar.addEvent(ev);
00262     kdDebug(5800) << "imported " << anniversary.toString() << endl;
00263   }
00264 
00265   return true;
00266 }
00267 
00268 void ResourceKABC::setAlarm( bool a )
00269 {
00270   mAlarm = a;
00271 }
00272 
00273 bool ResourceKABC::alarm()
00274 {
00275   return mAlarm;
00276 }
00277 
00278 void ResourceKABC::setAlarmDays( int ad )
00279 {
00280   mAlarmDays = ad;
00281 }
00282 
00283 int ResourceKABC::alarmDays()
00284 {
00285   return mAlarmDays;
00286 }
00287 
00288 bool ResourceKABC::doSave()
00289 {
00290   // is always read only!
00291   return true;
00292 }
00293 
00294 bool ResourceKABC::isSaving()
00295 {
00296   return false;
00297 }
00298 
00299 KABC::Lock *ResourceKABC::lock()
00300 {
00301   return mLock;
00302 }
00303 
00304 void ResourceKABC::doClose()
00305 {
00306   if ( !mOpen ) return;
00307 
00308   mCalendar.close();
00309   mOpen = false;
00310 }
00311 
00312 
00313 bool ResourceKABC::addEvent(Event*)
00314 {
00315   return false;
00316 }
00317 
00318 void ResourceKABC::deleteEvent(Event*)
00319 {
00320 }
00321 
00322 
00323 Event *ResourceKABC::event( const QString &uid )
00324 {
00325   return mCalendar.event( uid );
00326 }
00327 
00328 Event::List ResourceKABC::rawEventsForDate(const QDate &qd, bool sorted)
00329 {
00330   return mCalendar.rawEventsForDate( qd, sorted );
00331 }
00332 
00333 
00334 Event::List ResourceKABC::rawEvents( const QDate &start, const QDate &end,
00335                                           bool inclusive )
00336 {
00337   return mCalendar.rawEvents( start, end, inclusive );
00338 }
00339 
00340 Event::List ResourceKABC::rawEventsForDate(const QDateTime &qdt)
00341 {
00342   return mCalendar.rawEventsForDate( qdt.date() );
00343 }
00344 
00345 Event::List ResourceKABC::rawEvents()
00346 {
00347   return mCalendar.rawEvents();
00348 }
00349 
00350 bool ResourceKABC::addTodo(Todo*)
00351 {
00352   return false;
00353 }
00354 
00355 void ResourceKABC::deleteTodo(Todo*)
00356 {
00357 }
00358 
00359 
00360 Todo::List ResourceKABC::rawTodos()
00361 {
00362   return mCalendar.rawTodos();
00363 }
00364 
00365 Todo *ResourceKABC::todo( const QString &uid )
00366 {
00367   return mCalendar.todo( uid );
00368 }
00369 
00370 Todo::List ResourceKABC::rawTodosForDate( const QDate &date )
00371 {
00372   return mCalendar.rawTodosForDate( date );
00373 }
00374 
00375 
00376 bool ResourceKABC::addJournal(Journal*)
00377 {
00378   return false;
00379 }
00380 
00381 void ResourceKABC::deleteJournal(Journal*)
00382 {
00383 }
00384 
00385 Journal *ResourceKABC::journal(const QDate &date)
00386 {
00387 //  kdDebug(5800) << "ResourceKABC::journal() " << date.toString() << endl;
00388 
00389   return mCalendar.journal( date );
00390 }
00391 
00392 Journal *ResourceKABC::journal(const QString &uid)
00393 {
00394   return mCalendar.journal( uid );
00395 }
00396 
00397 Journal::List ResourceKABC::journals()
00398 {
00399   return mCalendar.journals();
00400 }
00401 
00402 
00403 Alarm::List ResourceKABC::alarmsTo( const QDateTime &to )
00404 {
00405   return mCalendar.alarmsTo( to );
00406 }
00407 
00408 Alarm::List ResourceKABC::alarms( const QDateTime &from, const QDateTime &to )
00409 {
00410 //  kdDebug(5800) << "ResourceKABC::alarms(" << from.toString() << " - " << to.toString() << ")\n";
00411 
00412   return mCalendar.alarms( from, to );
00413 }
00414 
00415 void ResourceKABC::dump() const
00416 {
00417   ResourceCalendar::dump();
00418 }
00419 
00420 void ResourceKABC::reload()
00421 {
00422   load();
00423 }
00424 
00425 void ResourceKABC::setTimeZoneId( const QString& tzid )
00426 {
00427   mCalendar.setTimeZoneId( tzid );
00428 }
00429 
00430 #include "resourcekabc.moc"
KDE Logo
This file is part of the documentation for libkcal Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Apr 4 04:45:02 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003