korganizer Library API Documentation

birthdays.cpp

00001 /*
00002     This file is part of KOrganizer.
00003     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 #include <qfile.h>
00021 #include <qstring.h>
00022 #include <qdatetime.h>
00023 
00024 #include <kapplication.h>
00025 #include <kconfig.h>
00026 #include <kstandarddirs.h>
00027 #include <klocale.h>
00028 #include <kdebug.h>
00029 #include <kaction.h>
00030 #include <kmessagebox.h>
00031 
00032 #include <kabc/addressbook.h>
00033 #include <kabc/stdaddressbook.h>
00034 
00035 #include <libkcal/calendar.h>
00036 #include <libkcal/event.h>
00037 #include <libkcal/alarm.h>
00038 
00039 #include "calendarview.h"
00040 #include "koprefs.h"
00041 
00042 #include "birthdaysdialog.h"
00043 
00044 #include "birthdays.h"
00045 #include "birthdays.moc"
00046 
00047 
00048 class BirthdaysFactory : public KOrg::PartFactory {
00049   public:
00050     KOrg::Part *create(KOrg::MainWindow *parent, const char *name)
00051     {
00052       return new Birthdays(parent,name);
00053     }
00054 };
00055 
00056 extern "C" {
00057   void *init_libkorg_birthdays()
00058   {
00059     return (new BirthdaysFactory);
00060   }
00061 }
00062 
00063 
00064 Birthdays::Birthdays(KOrg::MainWindow *parent, const char *name) :
00065   KOrg::Part(parent,name)
00066 {
00067   setXMLFile("plugins/birthdaysui.rc");
00068 
00069   new KAction(i18n("Import Birthdays..."), 0, this, SLOT(importBirthdays()),
00070               actionCollection(), "import_birthdays");
00071   mParent = parent;
00072 }
00073 
00074 Birthdays::~Birthdays()
00075 {
00076 }
00077 
00078 QString Birthdays::info()
00079 {
00080   return i18n("This plugin inserts birthdays imported from the KDE addressbook for the next one year.");
00081 }
00082 
00083 void Birthdays::importBirthdays()
00084 {
00085 //  kdDebug() << "import the birthdays from the addressbook" << endl;
00086 
00087 #ifndef KORG_NOKABC
00088   Calendar *cal = mainWindow()->view()->calendar();
00089   QDateTime birthdate;
00090   QString summary;
00091   int inserted_birthdays = 0;
00092 
00093   BirthdaysDialog *bd = new BirthdaysDialog();
00094   if (bd->exec()!=QDialog::Accepted) return;
00095 
00096   KABC::AddressBook *add_book = KABC::StdAddressBook::self();
00097   KABC::AddressBook::Iterator it;
00098   for ( it = add_book->begin(); it != add_book->end(); ++it ) {
00099     if ( (*it).birthday().date().isValid() ) {
00100       kdDebug() << "found a birthday " << (*it).birthday().toString() << endl;
00101 
00102       QString name = (*it).nickName();
00103       if (name.isEmpty()) name = (*it).realName();
00104       summary = i18n("%1's birthday").arg( name );
00105       birthdate = (*it).birthday();
00106 
00107       Event *ev = 0;
00108       Event *e;
00109       // look if not already imported
00110       bool insert = true;
00111       QPtrList<Event> events = cal->events(birthdate);
00112       for ( e = events.first(); e; e = events.next() ) {
00113         kdDebug() << summary << " | " << e->summary() << endl;
00114         if ( e->summary()==summary ) {
00115           kdDebug() << " inserted " << e->summary() << endl;
00116           insert = false;
00117           ev = e;
00118           e = events.last();
00119         }
00120       }
00121       if (!ev) ev = new Event();
00122 
00123 
00124       ev->setDtStart(birthdate);
00125       ev->setDtEnd(birthdate);
00126       ev->setHasEndDate(true);
00127 
00128       ev->setSummary(summary);
00129 
00130       // Set the recurrence
00131       Recurrence *vRecurrence = ev->recurrence();
00132       vRecurrence->setRecurStart(birthdate);
00133       vRecurrence->setYearly(Recurrence::rYearlyMonth,1,-1);
00134       vRecurrence->addYearlyNum(birthdate.date().month());
00135 
00136       ev->clearAlarms();
00137       if (bd->mAlarm->isChecked()) {
00138         // Set the alarm
00139         Alarm* vAlarm = ev->newAlarm();
00140         vAlarm->setText(summary);
00141         vAlarm->setTime(birthdate);
00142         vAlarm->setOffset(-1440 * bd->mAlarmTimeEdit->text().toInt());
00143         vAlarm->setEnabled(true);
00144       }
00145 
00146       // insert category
00147       QStringList::Iterator itc;
00148       for (itc = KOPrefs::instance()->mCustomCategories.begin();
00149           itc != KOPrefs::instance()->mCustomCategories.end(); ++itc ) {
00150         if ((*itc)==i18n("Birthday"))
00151           ev->setCategories(i18n("Birthday"));
00152       }
00153 
00154       if (insert) {
00155         cal->addEvent(ev);
00156         inserted_birthdays++;
00157         kdDebug() << "imported " << birthdate.toString() << endl;
00158       }
00159     }
00160   }
00161   summary = i18n("Imported 1 birthday.", "Imported %n birthdays.", inserted_birthdays);
00162   KMessageBox::information(mParent,summary);
00163 #endif
00164 
00165 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 15 11:41:08 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001