• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

korgac

  • sources
  • kde-4.12
  • kdepim
  • korgac
koalarmclient.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the KDE reminder agent.
3 
4  Copyright (c) 2002,2003 Cornelius Schumacher <schumacher@kde.org>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License along
17  with this program; if not, write to the Free Software Foundation, Inc.,
18  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20  As a special exception, permission is given to link this program
21  with any edition of Qt, and distribute the resulting executable,
22  without including the source code for Qt in the source distribution.
23 */
24 //krazy:excludeall=kdebug because we use the korgac(check) debug area in here
25 
26 #include "koalarmclient.h"
27 #if !defined(KORGAC_AKONADI_AGENT)
28 #include "alarmdialog.h"
29 #include "alarmdockwindow.h"
30 #else
31 #include <KNotification>
32 #endif
33 #include "korgacadaptor.h"
34 
35 #include <calendarsupport/utils.h>
36 
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>
44 
45 #include <KCalCore/Calendar>
46 
47 #include <KApplication>
48 #include <KCheckableProxyModel>
49 #include <KConfig>
50 #include <KConfigGroup>
51 #include <KDebug>
52 #include <KStandardDirs>
53 #include <KSystemTimeZones>
54 
55 #ifdef Q_WS_MAEMO_5
56 #include <QtMaemo5/QMaemo5InformationBox>
57 #endif
58 
59 using namespace KCalCore;
60 
61 KOAlarmClient::KOAlarmClient( QObject *parent )
62  : QObject( parent ), mDocker( 0 ), mDialog( 0 )
63 {
64  new KOrgacAdaptor( this );
65  Akonadi::DBusConnectionPool::threadConnection().registerObject( QLatin1String("/ac"), this );
66  kDebug();
67 
68 #if !defined(KORGAC_AKONADI_AGENT)
69  if ( dockerEnabled() ) {
70  mDocker = new AlarmDockWindow;
71  connect( this, SIGNAL(reminderCount(int)), mDocker, SLOT(slotUpdate(int)) );
72  connect( mDocker, SIGNAL(quitSignal()), SLOT(slotQuit()) );
73  }
74 #endif
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();
80 
81  connect( &mCheckTimer, SIGNAL(timeout()), SLOT(checkAlarms()) );
82  connect( mETM, SIGNAL(collectionPopulated(Akonadi::Collection::Id)),
83  SLOT(deferredInit()) );
84  connect( mETM, SIGNAL(collectionTreeFetched(Akonadi::Collection::List)),
85  SLOT(deferredInit()) );
86 
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() );
91 
92  checkAlarms();
93  mCheckTimer.start( 1000 * interval ); // interval in seconds
94 }
95 
96 KOAlarmClient::~KOAlarmClient()
97 {
98 #if !defined(KORGAC_AKONADI_AGENT)
99  delete mDocker;
100  delete mDialog;
101 #endif
102 }
103 
104 void checkAllItems( KCheckableProxyModel *model, const QModelIndex &parent = QModelIndex() )
105 {
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 );
110 
111  if ( model->rowCount( index ) > 0 ) {
112  checkAllItems( model, index );
113  }
114  }
115 }
116 
117 void KOAlarmClient::deferredInit()
118 {
119  if ( !collectionsAvailable() ) {
120  return;
121  }
122 
123  kDebug(5891) << "Performing delayed initialization.";
124 
125  // load reminders that were active when quitting
126  KConfigGroup genGroup( KGlobal::config(), "General" );
127  const int numReminders = genGroup.readEntry( "Reminders", 0 );
128 
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 );
132 
133  const KUrl url = incGroup.readEntry( "AkonadiUrl" );
134  Akonadi::Item::Id akonadiItemId = -1;
135  if ( !url.isValid() ) {
136  // logic to migrate old KOrganizer incidence uid's to a Akonadi item.
137  const QString uid = incGroup.readEntry( "UID" );
138  if ( !uid.isEmpty() ) {
139  akonadiItemId = mCalendar->item( uid ).id();
140  }
141  } else {
142  akonadiItemId = Akonadi::Item::fromUrl( url ).id();
143  }
144 
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() );
150  }
151  }
152  }
153 
154  KCheckableProxyModel *checkableModel = mCalendar->checkableProxyModel();
155  checkAllItems( checkableModel );
156 
157  // Now that everything is set up, a first check for reminders can be performed.
158  checkAlarms();
159 }
160 
161 bool KOAlarmClient::dockerEnabled()
162 {
163  KConfig korgConfig( KStandardDirs::locate( "config", QLatin1String("korganizerrc") ) );
164  KConfigGroup generalGroup( &korgConfig, "System Tray" );
165  return generalGroup.readEntry( "ShowReminderDaemon", true );
166 }
167 
168 bool KOAlarmClient::collectionsAvailable() const
169 {
170  // The list of collections must be available.
171  if ( !mETM->isCollectionTreeFetched() ) {
172  return false;
173  }
174 
175  // All collections must be populated.
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 );
180  bool haveData =
181  mETM->data( index, Akonadi::EntityTreeModel::IsPopulatedRole ).toBool();
182  if ( !haveData ) {
183  return false;
184  }
185  }
186 
187  return true;
188 }
189 
190 void KOAlarmClient::checkAlarms()
191 {
192  KConfigGroup cfg( KGlobal::config(), "General" );
193 
194  if ( !cfg.readEntry( "Enabled", true ) ) {
195  return;
196  }
197 
198  // We do not want to miss any reminders, so don't perform check unless
199  // the collections are available and populated.
200  if ( !collectionsAvailable() ) {
201  kDebug(5891) << "Collections are not available; aborting check.";
202  return;
203  }
204 
205  QDateTime from = mLastChecked.addSecs( 1 );
206  mLastChecked = QDateTime::currentDateTime();
207 
208  kDebug(5891) << "Check:" << from.toString() << " -" << mLastChecked.toString();
209 
210  const Alarm::List alarms = mCalendar->alarms( KDateTime( from, KDateTime::LocalZone ),
211  KDateTime( mLastChecked, KDateTime::LocalZone ),
212  true /* exclude blocked alarms */);
213 
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 );
218 
219  createReminder( mCalendar, item, from, alarm->text() );
220  }
221 }
222 
223 void KOAlarmClient::createReminder( const Akonadi::ETMCalendar::Ptr &calendar,
224  const Akonadi::Item &aitem,
225  const QDateTime &remindAtDate,
226  const QString &displayText )
227 {
228  if ( !CalendarSupport::hasIncidence( aitem ) ) {
229  return;
230  }
231 
232 #if !defined(Q_WS_MAEMO_5) && !defined(Q_WS_WINCE) && !defined(KORGAC_AKONADI_AGENT)
233  if ( !mDialog ) {
234  mDialog = new AlarmDialog( calendar );
235  connect( this, SIGNAL(saveAllSignal()), mDialog, SLOT(slotSave()) );
236  if ( mDocker ) {
237  connect( mDialog, SIGNAL(reminderCount(int)),
238  mDocker, SLOT(slotUpdate(int)) );
239  connect( mDocker, SIGNAL(suspendAllSignal()),
240  mDialog, SLOT(suspendAll()) );
241  connect( mDocker, SIGNAL(dismissAllSignal()),
242  mDialog, SLOT(dismissAll()) );
243  }
244  }
245 
246  mDialog->addIncidence( aitem, remindAtDate, displayText );
247  mDialog->wakeUp();
248 #else
249  const Incidence::Ptr incidence = CalendarSupport::incidence( aitem );
250  Q_UNUSED( calendar );
251  Q_UNUSED( remindAtDate );
252  Q_UNUSED( displayText );
253 
254 #if defined(Q_WS_MAEMO_5)
255  QMaemo5InformationBox::information( 0, incidence->summary(), QMaemo5InformationBox::NoTimeout );
256 #else
257  KNotification *notify = new KNotification( "reminder", 0, KNotification::Persistent );
258  notify->setText( incidence->summary() );
259  notify->sendEvent();
260 #endif
261 
262 #endif
263  saveLastCheckTime();
264 }
265 
266 void KOAlarmClient::slotQuit()
267 {
268  emit saveAllSignal();
269  saveLastCheckTime();
270  quit();
271 }
272 
273 void KOAlarmClient::saveLastCheckTime()
274 {
275  KConfigGroup cg( KGlobal::config(), "Alarms" );
276  cg.writeEntry( "CalendarsLastChecked", mLastChecked );
277  KGlobal::config()->sync();
278 }
279 
280 void KOAlarmClient::quit()
281 {
282  kDebug();
283 #if !defined(KORGAC_AKONADI_AGENT)
284  kapp->quit();
285 #endif
286 }
287 
288 #if !defined(Q_WS_WINCE)
289 bool KOAlarmClient::commitData( QSessionManager & )
290 {
291  emit saveAllSignal();
292  saveLastCheckTime();
293  return true;
294 }
295 #endif
296 
297 void KOAlarmClient::forceAlarmCheck()
298 {
299  checkAlarms();
300  saveLastCheckTime();
301 }
302 
303 void KOAlarmClient::dumpDebug()
304 {
305  KConfigGroup cfg( KGlobal::config(), "Alarms" );
306  const QDateTime lastChecked = cfg.readEntry( "CalendarsLastChecked", QDateTime() );
307  kDebug() << "Last Check:" << lastChecked;
308 }
309 
310 QStringList KOAlarmClient::dumpAlarms()
311 {
312  const KDateTime start = KDateTime( QDateTime::currentDateTime().date(),
313  QTime( 0, 0 ), KDateTime::LocalZone );
314  const KDateTime end = start.addDays( 1 ).addSecs( -1 );
315 
316  QStringList lst;
317  // Don't translate, this is for debugging purposes.
318  lst << QLatin1String( "AlarmDeamon::dumpAlarms() from " ) + start.toString() + QLatin1String(" to ") +
319  end.toString();
320 
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(')');
325  }
326 
327  return lst;
328 }
329 
330 void KOAlarmClient::debugShowDialog()
331 {
332 // showAlarmDialog();
333 }
334 
335 void KOAlarmClient::hide()
336 {
337 #if !defined(KORGAC_AKONADI_AGENT)
338  delete mDocker;
339  mDocker = 0;
340 #endif
341 }
342 
343 void KOAlarmClient::show()
344 {
345 #if !defined(KORGAC_AKONADI_AGENT)
346  if ( !mDocker ) {
347  if ( dockerEnabled() ) {
348  mDocker = new AlarmDockWindow;
349  connect( this, SIGNAL(reminderCount(int)), mDocker, SLOT(slotUpdate(int)) );
350  connect( mDocker, SIGNAL(quitSignal()), SLOT(slotQuit()) );
351  }
352 
353  if ( mDialog ) {
354  connect( mDialog, SIGNAL(reminderCount(int)),
355  mDocker, SLOT(slotUpdate(int)) );
356  connect( mDocker, SIGNAL(suspendAllSignal()),
357  mDialog, SLOT(suspendAll()) );
358  connect( mDocker, SIGNAL(dismissAllSignal()),
359  mDialog, SLOT(dismissAll()) );
360  }
361  }
362 #endif
363 }
364 
365 #include "koalarmclient.moc"
KOAlarmClient::saveAllSignal
void saveAllSignal()
checkAllItems
void checkAllItems(KCheckableProxyModel *model, const QModelIndex &parent=QModelIndex())
Definition: koalarmclient.cpp:104
alarmdockwindow.h
alarmdialog.h
KOAlarmClient::debugShowDialog
void debugShowDialog()
Definition: koalarmclient.cpp:330
KOAlarmClient::commitData
bool commitData(QSessionManager &)
Definition: koalarmclient.cpp:289
KOAlarmClient::deferredInit
void deferredInit()
Definition: koalarmclient.cpp:117
KOAlarmClient::hide
void hide()
Definition: koalarmclient.cpp:335
KOAlarmClient::show
void show()
Definition: koalarmclient.cpp:343
QObject
KOAlarmClient::quit
void quit()
Definition: koalarmclient.cpp:280
KOAlarmClient::~KOAlarmClient
~KOAlarmClient()
Definition: koalarmclient.cpp:96
koalarmclient.h
AlarmDialog
Definition: alarmdialog.h:58
KOAlarmClient::forceAlarmCheck
void forceAlarmCheck()
Definition: koalarmclient.cpp:297
AlarmDialog::wakeUp
void wakeUp()
Definition: alarmdialog.cpp:688
AlarmDockWindow
Definition: alarmdockwindow.h:33
KOAlarmClient::checkAlarms
void checkAlarms()
Definition: koalarmclient.cpp:190
KOAlarmClient::reminderCount
void reminderCount(int)
KOAlarmClient::dumpDebug
void dumpDebug()
Definition: koalarmclient.cpp:303
KOAlarmClient::slotQuit
void slotQuit()
Definition: koalarmclient.cpp:266
KOAlarmClient::KOAlarmClient
KOAlarmClient(QObject *parent=0)
Definition: koalarmclient.cpp:61
AlarmDialog::addIncidence
void addIncidence(const Akonadi::Item &incidence, const QDateTime &reminderAt, const QString &displayText)
Definition: alarmdialog.cpp:318
KOAlarmClient::dumpAlarms
QStringList dumpAlarms()
Definition: koalarmclient.cpp:310
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:50 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

korgac

Skip menu "korgac"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal