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 = KABC::StdAddressBook::self();
00100   connect( mAddressbook, SIGNAL(addressBookChanged(AddressBook*)), SLOT( reload() ) );
00101 }
00102 
00103 void ResourceKABC::readConfig( const KConfig *config )
00104 {
00105   mAlarmDays = config->readNumEntry( "AlarmDays", 1 );
00106   mAlarm = config->readBoolEntry( "Alarm", false );
00107 }
00108 
00109 void ResourceKABC::writeConfig( KConfig *config )
00110 {
00111   ResourceCalendar::writeConfig( config );
00112   config->writeEntry( "AlarmDays", mAlarmDays );
00113   config->writeEntry( "Alarm", mAlarm );
00114   load();
00115 }
00116 
00117 
00118 bool ResourceKABC::doOpen()
00119 {
00120   kdDebug(5800) << "ResourceKABC::doOpen()" << endl;
00121 
00122   mOpen = true;
00123 
00124   return true;
00125 }
00126 
00127 bool ResourceKABC::load()
00128 {
00129   kdDebug() << "ResourceKABC::load()" << endl;
00130 
00131   if ( !mOpen ) return true;
00132 
00133   mCalendar.close();
00134 
00135   // import from kabc
00136   QString summary;
00137 
00138   KABC::AddressBook::Iterator it;
00139   for ( it = mAddressbook->begin(); it != mAddressbook->end(); ++it ) {
00140 
00141     QDateTime birthdate = (*it).birthday().date();
00142     if ( birthdate.isValid() ) {
00143       kdDebug() << "found a birthday " << birthdate.toString() << endl;
00144 
00145       QString name = (*it).nickName();
00146       if (name.isEmpty()) name = (*it).realName();
00147       summary = i18n("%1's birthday").arg( name );
00148 
00149       Event *ev = new Event();
00150 
00151       ev->setDtStart(birthdate);
00152       ev->setDtEnd(birthdate);
00153       ev->setHasEndDate(true);
00154       ev->setFloats(true);
00155 
00156       ev->setSummary(summary);
00157 
00158       // Set the recurrence
00159       Recurrence *vRecurrence = ev->recurrence();
00160       vRecurrence->setRecurStart(birthdate);
00161       vRecurrence->setYearly(Recurrence::rYearlyMonth,1,-1);
00162       vRecurrence->addYearlyNum(birthdate.date().month());
00163 
00164       ev->clearAlarms();
00165 
00166       if ( mAlarm ) {
00167         // Set the alarm
00168         Alarm* vAlarm = ev->newAlarm();
00169         vAlarm->setText(summary);
00170         vAlarm->setTime(birthdate);
00171         // 24 hours before
00172         vAlarm->setStartOffset( -1440 * mAlarmDays );
00173         vAlarm->setEnabled(true);
00174       }
00175 
00176       // insert category
00177       ev->setCategories(i18n("Birthday"));
00178 
00179       ev->setReadOnly( true );
00180       mCalendar.addEvent(ev);
00181       kdDebug() << "imported " << birthdate.toString() << endl;
00182     }
00183 
00184     QDateTime anniversary = QDate::fromString( (*it).custom( "KADDRESSBOOK", "X-Anniversary" ), Qt::ISODate );
00185     if ( anniversary.isValid() ) {
00186       kdDebug() << "found a anniversary " << anniversary.toString() << endl;
00187 
00188       QString name = (*it).nickName();
00189       if (name.isEmpty()) name = (*it).realName();
00190       summary = i18n("%1's anniversary").arg( name );
00191 
00192       Event *ev = new Event();
00193 
00194       ev->setDtStart(anniversary);
00195       ev->setDtEnd(anniversary);
00196       ev->setHasEndDate(true);
00197       ev->setFloats(true);
00198 
00199       ev->setSummary(summary);
00200 
00201       // Set the recurrence
00202       Recurrence *vRecurrence = ev->recurrence();
00203       vRecurrence->setRecurStart(anniversary);
00204       vRecurrence->setYearly(Recurrence::rYearlyMonth,1,-1);
00205       vRecurrence->addYearlyNum(anniversary.date().month());
00206 
00207       ev->clearAlarms();
00208 
00209       if ( mAlarm ) {
00210         // Set the alarm
00211         Alarm* vAlarm = ev->newAlarm();
00212         vAlarm->setText(summary);
00213         vAlarm->setTime(anniversary);
00214         // 24 hours before
00215         vAlarm->setStartOffset( -1440 * mAlarmDays );
00216         vAlarm->setEnabled(true);
00217       }
00218 
00219       // insert category
00220       ev->setCategories(i18n("Anniversary"));
00221 
00222       ev->setReadOnly( true );
00223       mCalendar.addEvent(ev);
00224       kdDebug() << "imported " << anniversary.toString() << endl;
00225     }
00226   }
00227 
00228   emit resourceChanged( this );
00229 
00230   return true;
00231 }
00232 
00233 void ResourceKABC::setAlarm( bool a )
00234 {
00235   mAlarm = a;
00236 }
00237 
00238 bool ResourceKABC::alarm()
00239 {
00240   return mAlarm;
00241 }
00242 
00243 void ResourceKABC::setAlarmDays( int ad )
00244 {
00245   mAlarmDays = ad;
00246 }
00247 
00248 int ResourceKABC::alarmDays()
00249 {
00250   return mAlarmDays;
00251 }
00252 
00253 bool ResourceKABC::save()
00254 {
00255   // is always read only!
00256   return true;
00257 }
00258 
00259 bool ResourceKABC::isSaving()
00260 {
00261   return false;
00262 }
00263 
00264 KABC::Lock *ResourceKABC::lock()
00265 {
00266   return mLock;
00267 }
00268 
00269 void ResourceKABC::doClose()
00270 {
00271   if ( !mOpen ) return;
00272 
00273   mCalendar.close();
00274   mOpen = false;
00275 }
00276 
00277 
00278 bool ResourceKABC::addEvent(Event*)
00279 {
00280   return false;
00281 }
00282 
00283 void ResourceKABC::deleteEvent(Event*)
00284 {
00285 }
00286 
00287 
00288 Event *ResourceKABC::event( const QString &uid )
00289 {
00290   return mCalendar.event( uid );
00291 }
00292 
00293 Event::List ResourceKABC::rawEventsForDate(const QDate &qd, bool sorted)
00294 {
00295   return mCalendar.rawEventsForDate( qd, sorted );
00296 }
00297 
00298 
00299 Event::List ResourceKABC::rawEvents( const QDate &start, const QDate &end,
00300                                           bool inclusive )
00301 {
00302   return mCalendar.rawEvents( start, end, inclusive );
00303 }
00304 
00305 Event::List ResourceKABC::rawEventsForDate(const QDateTime &qdt)
00306 {
00307   return mCalendar.rawEventsForDate( qdt.date() );
00308 }
00309 
00310 Event::List ResourceKABC::rawEvents()
00311 {
00312   return mCalendar.rawEvents();
00313 }
00314 
00315 bool ResourceKABC::addTodo(Todo*)
00316 {
00317   return false;
00318 }
00319 
00320 void ResourceKABC::deleteTodo(Todo*)
00321 {
00322 }
00323 
00324 
00325 Todo::List ResourceKABC::rawTodos()
00326 {
00327   return mCalendar.rawTodos();
00328 }
00329 
00330 Todo *ResourceKABC::todo( const QString &uid )
00331 {
00332   return mCalendar.todo( uid );
00333 }
00334 
00335 Todo::List ResourceKABC::todos( const QDate &date )
00336 {
00337   return mCalendar.todos( date );
00338 }
00339 
00340 
00341 bool ResourceKABC::addJournal(Journal*)
00342 {
00343   return false;
00344 }
00345 
00346 void ResourceKABC::deleteJournal(Journal*)
00347 {
00348 }
00349 
00350 Journal *ResourceKABC::journal(const QDate &date)
00351 {
00352 //  kdDebug(5800) << "ResourceKABC::journal() " << date.toString() << endl;
00353 
00354   return mCalendar.journal( date );
00355 }
00356 
00357 Journal *ResourceKABC::journal(const QString &uid)
00358 {
00359   return mCalendar.journal( uid );
00360 }
00361 
00362 Journal::List ResourceKABC::journals()
00363 {
00364   return mCalendar.journals();
00365 }
00366 
00367 
00368 Alarm::List ResourceKABC::alarmsTo( const QDateTime &to )
00369 {
00370   return mCalendar.alarmsTo( to );
00371 }
00372 
00373 Alarm::List ResourceKABC::alarms( const QDateTime &from, const QDateTime &to )
00374 {
00375 //  kdDebug(5800) << "ResourceKABC::alarms(" << from.toString() << " - " << to.toString() << ")\n";
00376 
00377   return mCalendar.alarms( from, to );
00378 }
00379 
00380 void ResourceKABC::update(IncidenceBase *)
00381 {
00382 }
00383 
00384 void ResourceKABC::dump() const
00385 {
00386   ResourceCalendar::dump();
00387 }
00388 
00389 void ResourceKABC::reload()
00390 {
00391   load();
00392 }
00393 
00394 void ResourceKABC::setTimeZoneId( const QString& tzid )
00395 {
00396   mCalendar.setTimeZoneId( tzid );
00397 }
00398 
00399 #include "resourcekabc.moc"
KDE Logo
This file is part of the documentation for libkcal Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:36:23 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003