27 #if !defined(KORGAC_AKONADI_AGENT)
31 #include <KNotification>
33 #include "korgacadaptor.h"
35 #include <calendarsupport/utils.h>
37 #include <Akonadi/ChangeRecorder>
38 #include <Akonadi/Collection>
39 #include <Akonadi/DBusConnectionPool>
40 #include <Akonadi/EntityTreeModel>
41 #include <Akonadi/Item>
42 #include <Akonadi/ItemFetchScope>
43 #include <Akonadi/Session>
45 #include <KCalCore/Calendar>
47 #include <KApplication>
48 #include <KCheckableProxyModel>
50 #include <KConfigGroup>
52 #include <KStandardDirs>
53 #include <KSystemTimeZones>
56 #include <QtMaemo5/QMaemo5InformationBox>
59 using namespace KCalCore;
62 :
QObject( parent ), mDocker( 0 ), mDialog( 0 )
64 new KOrgacAdaptor(
this );
65 Akonadi::DBusConnectionPool::threadConnection().registerObject( QLatin1String(
"/ac"),
this );
68 #if !defined(KORGAC_AKONADI_AGENT)
69 if ( dockerEnabled() ) {
71 connect(
this, SIGNAL(
reminderCount(
int)), mDocker, SLOT(slotUpdate(
int)) );
72 connect( mDocker, SIGNAL(quitSignal()), SLOT(
slotQuit()) );
75 QStringList mimeTypes;
76 mimeTypes << Event::eventMimeType() << Todo::todoMimeType();
77 mCalendar = Akonadi::ETMCalendar::Ptr(
new Akonadi::ETMCalendar( mimeTypes ) );
78 mCalendar->setObjectName( QLatin1String(
"KOrgac's calendar") );
79 mETM = mCalendar->entityTreeModel();
81 connect( &mCheckTimer, SIGNAL(timeout()), SLOT(
checkAlarms()) );
82 connect( mETM, SIGNAL(collectionPopulated(Akonadi::Collection::Id)),
84 connect( mETM, SIGNAL(collectionTreeFetched(Akonadi::Collection::List)),
87 KConfigGroup alarmGroup( KGlobal::config(),
"Alarms" );
88 const int interval = alarmGroup.readEntry(
"Interval", 60 );
89 kDebug() <<
"KOAlarmClient check interval:" << interval <<
"seconds.";
90 mLastChecked = alarmGroup.readEntry(
"CalendarsLastChecked", QDateTime() );
93 mCheckTimer.start( 1000 * interval );
98 #if !defined(KORGAC_AKONADI_AGENT)
104 void checkAllItems( KCheckableProxyModel *model,
const QModelIndex &parent = QModelIndex() )
106 const int rowCount = model->rowCount( parent );
107 for (
int row = 0; row < rowCount; row++ ) {
108 QModelIndex index = model->index( row, 0, parent );
109 model->setData( index, Qt::Checked, Qt::CheckStateRole );
111 if ( model->rowCount( index ) > 0 ) {
119 if ( !collectionsAvailable() ) {
123 kDebug(5891) <<
"Performing delayed initialization.";
126 KConfigGroup genGroup( KGlobal::config(),
"General" );
127 const int numReminders = genGroup.readEntry(
"Reminders", 0 );
129 for (
int i=1; i<=numReminders; ++i ) {
130 const QString group( QString::fromLatin1(
"Incidence-%1" ).arg( i ) );
131 const KConfigGroup incGroup( KGlobal::config(), group );
133 const KUrl url = incGroup.readEntry(
"AkonadiUrl" );
134 Akonadi::Item::Id akonadiItemId = -1;
135 if ( !url.isValid() ) {
137 const QString uid = incGroup.readEntry(
"UID" );
138 if ( !uid.isEmpty() ) {
139 akonadiItemId = mCalendar->item( uid ).id();
142 akonadiItemId = Akonadi::Item::fromUrl( url ).id();
145 if ( akonadiItemId >= 0 ) {
146 const QDateTime dt = incGroup.readEntry(
"RemindAt", QDateTime() );
147 Akonadi::Item i = mCalendar->item( Akonadi::Item::fromUrl( url ).
id() );
148 if ( CalendarSupport::hasIncidence( i ) && !CalendarSupport::incidence( i )->alarms().isEmpty() ) {
149 createReminder( mCalendar, i, dt, QString() );
154 KCheckableProxyModel *checkableModel = mCalendar->checkableProxyModel();
161 bool KOAlarmClient::dockerEnabled()
163 KConfig korgConfig( KStandardDirs::locate(
"config", QLatin1String(
"korganizerrc") ) );
164 KConfigGroup generalGroup( &korgConfig,
"System Tray" );
165 return generalGroup.readEntry(
"ShowReminderDaemon",
true );
168 bool KOAlarmClient::collectionsAvailable()
const
171 if ( !mETM->isCollectionTreeFetched() ) {
176 const int rowCount = mETM->rowCount();
177 for (
int row = 0; row < rowCount; ++row ) {
178 static const int column = 0;
179 const QModelIndex index = mETM->index( row, column );
181 mETM->data( index, Akonadi::EntityTreeModel::IsPopulatedRole ).toBool();
192 KConfigGroup cfg( KGlobal::config(),
"General" );
194 if ( !cfg.readEntry(
"Enabled",
true ) ) {
200 if ( !collectionsAvailable() ) {
201 kDebug(5891) <<
"Collections are not available; aborting check.";
205 QDateTime from = mLastChecked.addSecs( 1 );
206 mLastChecked = QDateTime::currentDateTime();
208 kDebug(5891) <<
"Check:" << from.toString() <<
" -" << mLastChecked.toString();
210 const Alarm::List alarms = mCalendar->alarms( KDateTime( from, KDateTime::LocalZone ),
211 KDateTime( mLastChecked, KDateTime::LocalZone ),
214 foreach (
const Alarm::Ptr &alarm, alarms ) {
215 const QString uid = alarm->customProperty(
"ETMCalendar",
"parentUid" );
216 const Akonadi::Item::Id
id = mCalendar->item( uid ).id();
217 const Akonadi::Item item = mCalendar->item(
id );
219 createReminder( mCalendar, item, from, alarm->text() );
223 void KOAlarmClient::createReminder(
const Akonadi::ETMCalendar::Ptr &calendar,
224 const Akonadi::Item &aitem,
225 const QDateTime &remindAtDate,
226 const QString &displayText )
228 if ( !CalendarSupport::hasIncidence( aitem ) ) {
232 #if !defined(Q_WS_MAEMO_5) && !defined(Q_WS_WINCE) && !defined(KORGAC_AKONADI_AGENT)
235 connect(
this, SIGNAL(
saveAllSignal()), mDialog, SLOT(slotSave()) );
238 mDocker, SLOT(slotUpdate(
int)) );
239 connect( mDocker, SIGNAL(suspendAllSignal()),
240 mDialog, SLOT(suspendAll()) );
241 connect( mDocker, SIGNAL(dismissAllSignal()),
242 mDialog, SLOT(dismissAll()) );
246 mDialog->
addIncidence( aitem, remindAtDate, displayText );
249 const Incidence::Ptr incidence = CalendarSupport::incidence( aitem );
250 Q_UNUSED( calendar );
251 Q_UNUSED( remindAtDate );
252 Q_UNUSED( displayText );
254 #if defined(Q_WS_MAEMO_5)
255 QMaemo5InformationBox::information( 0, incidence->summary(), QMaemo5InformationBox::NoTimeout );
257 KNotification *notify =
new KNotification(
"reminder", 0, KNotification::Persistent );
258 notify->setText( incidence->summary() );
273 void KOAlarmClient::saveLastCheckTime()
275 KConfigGroup cg( KGlobal::config(),
"Alarms" );
276 cg.writeEntry(
"CalendarsLastChecked", mLastChecked );
277 KGlobal::config()->sync();
283 #if !defined(KORGAC_AKONADI_AGENT)
288 #if !defined(Q_WS_WINCE)
305 KConfigGroup cfg( KGlobal::config(),
"Alarms" );
306 const QDateTime lastChecked = cfg.readEntry(
"CalendarsLastChecked", QDateTime() );
307 kDebug() <<
"Last Check:" << lastChecked;
312 const KDateTime start = KDateTime( QDateTime::currentDateTime().date(),
313 QTime( 0, 0 ), KDateTime::LocalZone );
314 const KDateTime end = start.addDays( 1 ).addSecs( -1 );
318 lst << QLatin1String(
"AlarmDeamon::dumpAlarms() from " ) + start.toString() + QLatin1String(
" to ") +
321 Alarm::List alarms = mCalendar->alarms( start, end );
322 foreach( Alarm::Ptr a, alarms ) {
323 const Incidence::Ptr parentIncidence = mCalendar->incidence( a->parentUid() );
324 lst << QLatin1String(
" " ) + parentIncidence->summary() + QLatin1String(
" (") + a->time().toString() + QLatin1Char(
')');
337 #if !defined(KORGAC_AKONADI_AGENT)
345 #if !defined(KORGAC_AKONADI_AGENT)
347 if ( dockerEnabled() ) {
349 connect(
this, SIGNAL(
reminderCount(
int)), mDocker, SLOT(slotUpdate(
int)) );
350 connect( mDocker, SIGNAL(quitSignal()), SLOT(
slotQuit()) );
355 mDocker, SLOT(slotUpdate(
int)) );
356 connect( mDocker, SIGNAL(suspendAllSignal()),
357 mDialog, SLOT(suspendAll()) );
358 connect( mDocker, SIGNAL(dismissAllSignal()),
359 mDialog, SLOT(dismissAll()) );
365 #include "koalarmclient.moc"
void checkAllItems(KCheckableProxyModel *model, const QModelIndex &parent=QModelIndex())
bool commitData(QSessionManager &)
KOAlarmClient(QObject *parent=0)
void addIncidence(const Akonadi::Item &incidence, const QDateTime &reminderAt, const QString &displayText)