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