00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
#include <qcheckbox.h>
00032
#include <krestrictedline.h>
00033
00034
#include <kabc/addressbook.h>
00035
#include <kabc/stdaddressbook.h>
00036
00037
#include <libkcal/calendar.h>
00038
#include <libkcal/event.h>
00039
#include <libkcal/alarm.h>
00040
00041
#include "calendarview.h"
00042
#include "koprefs.h"
00043
00044
#include "birthdaysdialog.h"
00045
00046
#include "birthdays.h"
00047
#include "birthdays.moc"
00048
00049
00050
class BirthdaysFactory :
public KOrg::PartFactory {
00051
public:
00052 KOrg::Part *create(
KOrg::MainWindow *parent,
const char *name)
00053 {
00054
return new Birthdays(parent,name);
00055 }
00056 };
00057
00058
extern "C" {
00059
void *init_libkorg_birthdays()
00060 {
00061
return (
new BirthdaysFactory);
00062 }
00063 }
00064
00065
00066 Birthdays::Birthdays(
KOrg::MainWindow *parent,
const char *name) :
00067 KOrg::Part(parent,name)
00068 {
00069 setInstance(
new KInstance(
"korganizer" ) );
00070
00071 setXMLFile(
"plugins/birthdaysui.rc");
00072
00073
new KAction( i18n(
"Import Birthdays..."), 0,
this, SLOT(importBirthdays()),
00074 actionCollection(),
"import_birthdays");
00075
00076 mParent = parent->
topLevelWidget();
00077 }
00078
00079 Birthdays::~Birthdays()
00080 {
00081 }
00082
00083 QString Birthdays::info()
00084 {
00085
return i18n(
"This plugin inserts birthdays imported from the KDE addressbook for the next one year.");
00086 }
00087
00088
void Birthdays::importBirthdays()
00089 {
00090
00091
00092
#ifndef KORG_NOKABC
00093
Calendar *cal = mainWindow()->view()->calendar();
00094 QDateTime birthdate;
00095 QString summary;
00096
int inserted_birthdays = 0;
00097
00098 BirthdaysDialog *bd =
new BirthdaysDialog();
00099
if (bd->exec()!=QDialog::Accepted)
return;
00100
00101 KABC::AddressBook *add_book = KABC::StdAddressBook::self();
00102 KABC::AddressBook::Iterator it;
00103
for ( it = add_book->begin(); it != add_book->end(); ++it ) {
00104
if ( (*it).birthday().date().isValid() ) {
00105 kdDebug(5850) <<
"found a birthday " << (*it).birthday().toString() << endl;
00106
00107 QString name = (*it).nickName();
00108
if (name.isEmpty()) name = (*it).realName();
00109 summary = i18n(
"%1's birthday").arg( name );
00110 birthdate = (*it).birthday();
00111
00112 Event *ev = 0;
00113 Event *e;
00114
00115
bool insert =
true;
00116 Event::List events = cal->events(birthdate);
00117 Event::List::ConstIterator it;
00118
for( it = events.begin(); it != events.end(); ++it ) {
00119 e = *it;
00120 kdDebug(5850) << summary <<
" | " << e->summary() << endl;
00121
if ( e->summary()==summary ) {
00122 kdDebug(5850) <<
" inserted " << e->summary() << endl;
00123 insert =
false;
00124 ev = e;
00125 e = events.last();
00126 }
00127 }
00128
if (!ev) ev =
new Event();
00129
00130
00131 ev->setDtStart(birthdate);
00132 ev->setDtEnd(birthdate);
00133 ev->setHasEndDate(
true);
00134
00135 ev->setSummary(summary);
00136
00137
00138 Recurrence *vRecurrence = ev->recurrence();
00139 vRecurrence->setRecurStart(birthdate);
00140 vRecurrence->setYearly(Recurrence::rYearlyMonth,1,-1);
00141 vRecurrence->addYearlyNum(birthdate.date().month());
00142
00143 ev->clearAlarms();
00144
if (bd->mAlarm->isChecked()) {
00145
00146 Alarm* vAlarm = ev->newAlarm();
00147 vAlarm->setText(summary);
00148 vAlarm->setTime(birthdate);
00149 vAlarm->setStartOffset(-1440 * bd->mAlarmTimeEdit->text().toInt());
00150 vAlarm->setEnabled(
true);
00151 }
00152
00153
00154 QStringList::Iterator itc;
00155
for (itc = KOPrefs::instance()->mCustomCategories.begin();
00156 itc != KOPrefs::instance()->mCustomCategories.end(); ++itc ) {
00157
if ((*itc)==i18n(
"Birthday"))
00158 ev->setCategories(i18n(
"Birthday"));
00159 }
00160
00161
if (insert) {
00162 cal->addEvent(ev);
00163 inserted_birthdays++;
00164 kdDebug(5850) <<
"imported " << birthdate.toString() << endl;
00165 }
00166 }
00167 }
00168 summary = i18n(
"Imported 1 birthday.",
"Imported %n birthdays.", inserted_birthdays);
00169 KMessageBox::information(mParent,summary);
00170
#endif
00171
00172 }