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

korgac

  • sources
  • kde-4.14
  • 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 
54 #ifdef Q_WS_MAEMO_5
55 #include <QtMaemo5/QMaemo5InformationBox>
56 #endif
57 
58 using namespace KCalCore;
59 
60 KOAlarmClient::KOAlarmClient( QObject *parent )
61  : QObject( parent ), mDocker( 0 ), mDialog( 0 )
62 {
63  new KOrgacAdaptor( this );
64  Akonadi::DBusConnectionPool::threadConnection().registerObject( QLatin1String("/ac"), this );
65  kDebug();
66 
67 #if !defined(KORGAC_AKONADI_AGENT)
68  if ( dockerEnabled() ) {
69  mDocker = new AlarmDockWindow;
70  connect( this, SIGNAL(reminderCount(int)), mDocker, SLOT(slotUpdate(int)) );
71  connect( mDocker, SIGNAL(quitSignal()), SLOT(slotQuit()) );
72  }
73 #endif
74  QStringList mimeTypes;
75  mimeTypes << Event::eventMimeType() << Todo::todoMimeType();
76  mCalendar = Akonadi::ETMCalendar::Ptr( new Akonadi::ETMCalendar( mimeTypes ) );
77  mCalendar->setObjectName( QLatin1String("KOrgac's calendar") );
78  mETM = mCalendar->entityTreeModel();
79 
80  connect( &mCheckTimer, SIGNAL(timeout()), SLOT(checkAlarms()) );
81  connect( mETM, SIGNAL(collectionPopulated(Akonadi::Collection::Id)),
82  SLOT(deferredInit()) );
83  connect( mETM, SIGNAL(collectionTreeFetched(Akonadi::Collection::List)),
84  SLOT(deferredInit()) );
85 
86  KConfigGroup alarmGroup( KGlobal::config(), "Alarms" );
87  const int interval = alarmGroup.readEntry( "Interval", 60 );
88  kDebug() << "KOAlarmClient check interval:" << interval << "seconds.";
89  mLastChecked = alarmGroup.readEntry( "CalendarsLastChecked", QDateTime() );
90 
91  checkAlarms();
92  mCheckTimer.start( 1000 * interval ); // interval in seconds
93 }
94 
95 KOAlarmClient::~KOAlarmClient()
96 {
97 #if !defined(KORGAC_AKONADI_AGENT)
98  delete mDocker;
99  delete mDialog;
100 #endif
101 }
102 
103 void checkAllItems( KCheckableProxyModel *model, const QModelIndex &parent = QModelIndex() )
104 {
105  const int rowCount = model->rowCount( parent );
106  for ( int row = 0; row < rowCount; ++row ) {
107  QModelIndex index = model->index( row, 0, parent );
108  model->setData( index, Qt::Checked, Qt::CheckStateRole );
109 
110  if ( model->rowCount( index ) > 0 ) {
111  checkAllItems( model, index );
112  }
113  }
114 }
115 
116 void KOAlarmClient::deferredInit()
117 {
118  if ( !collectionsAvailable() ) {
119  return;
120  }
121 
122  kDebug(5891) << "Performing delayed initialization.";
123 
124  // load reminders that were active when quitting
125  KConfigGroup genGroup( KGlobal::config(), "General" );
126  const int numReminders = genGroup.readEntry( "Reminders", 0 );
127 
128  for ( int i=1; i<=numReminders; ++i ) {
129  const QString group( QString::fromLatin1( "Incidence-%1" ).arg( i ) );
130  const KConfigGroup incGroup( KGlobal::config(), group );
131 
132  const KUrl url = incGroup.readEntry( "AkonadiUrl" );
133  Akonadi::Item::Id akonadiItemId = -1;
134  if ( !url.isValid() ) {
135  // logic to migrate old KOrganizer incidence uid's to a Akonadi item.
136  const QString uid = incGroup.readEntry( "UID" );
137  if ( !uid.isEmpty() ) {
138  akonadiItemId = mCalendar->item( uid ).id();
139  }
140  } else {
141  akonadiItemId = Akonadi::Item::fromUrl( url ).id();
142  }
143 
144  if ( akonadiItemId >= 0 ) {
145  const QDateTime dt = incGroup.readEntry( "RemindAt", QDateTime() );
146  Akonadi::Item i = mCalendar->item( Akonadi::Item::fromUrl( url ).id() );
147  if ( CalendarSupport::hasIncidence( i ) && !CalendarSupport::incidence( i )->alarms().isEmpty() ) {
148  createReminder( mCalendar, i, dt, QString() );
149  }
150  }
151  }
152 
153  KCheckableProxyModel *checkableModel = mCalendar->checkableProxyModel();
154  checkAllItems( checkableModel );
155 
156  // Now that everything is set up, a first check for reminders can be performed.
157  checkAlarms();
158 }
159 
160 bool KOAlarmClient::dockerEnabled()
161 {
162  KConfig korgConfig( KStandardDirs::locate( "config", QLatin1String("korganizerrc") ) );
163  KConfigGroup generalGroup( &korgConfig, "System Tray" );
164  return generalGroup.readEntry( "ShowReminderDaemon", true );
165 }
166 
167 bool KOAlarmClient::collectionsAvailable() const
168 {
169  // The list of collections must be available.
170  if ( !mETM->isCollectionTreeFetched() ) {
171  return false;
172  }
173 
174  // All collections must be populated.
175  const int rowCount = mETM->rowCount();
176  for ( int row = 0; row < rowCount; ++row ) {
177  static const int column = 0;
178  const QModelIndex index = mETM->index( row, column );
179  bool haveData =
180  mETM->data( index, Akonadi::EntityTreeModel::IsPopulatedRole ).toBool();
181  if ( !haveData ) {
182  return false;
183  }
184  }
185 
186  return true;
187 }
188 
189 void KOAlarmClient::checkAlarms()
190 {
191  KConfigGroup cfg( KGlobal::config(), "General" );
192 
193  if ( !cfg.readEntry( "Enabled", true ) ) {
194  return;
195  }
196 
197  // We do not want to miss any reminders, so don't perform check unless
198  // the collections are available and populated.
199  if ( !collectionsAvailable() ) {
200  kDebug(5891) << "Collections are not available; aborting check.";
201  return;
202  }
203 
204  QDateTime from = mLastChecked.addSecs( 1 );
205  mLastChecked = QDateTime::currentDateTime();
206 
207  kDebug(5891) << "Check:" << from.toString() << " -" << mLastChecked.toString();
208 
209  const Alarm::List alarms = mCalendar->alarms( KDateTime( from, KDateTime::LocalZone ),
210  KDateTime( mLastChecked, KDateTime::LocalZone ),
211  true /* exclude blocked alarms */);
212 
213  foreach ( const Alarm::Ptr &alarm, alarms ) {
214  const QString uid = alarm->customProperty( "ETMCalendar", "parentUid" );
215  const Akonadi::Item::Id id = mCalendar->item( uid ).id();
216  const Akonadi::Item item = mCalendar->item( id );
217 
218  createReminder( mCalendar, item, from, alarm->text() );
219  }
220 }
221 
222 void KOAlarmClient::createReminder( const Akonadi::ETMCalendar::Ptr &calendar,
223  const Akonadi::Item &aitem,
224  const QDateTime &remindAtDate,
225  const QString &displayText )
226 {
227  if ( !CalendarSupport::hasIncidence( aitem ) ) {
228  return;
229  }
230 
231 #if !defined(Q_WS_MAEMO_5) && !defined(KORGAC_AKONADI_AGENT)
232  if ( !mDialog ) {
233  mDialog = new AlarmDialog( calendar );
234  connect( this, SIGNAL(saveAllSignal()), mDialog, SLOT(slotSave()) );
235  if ( mDocker ) {
236  connect( mDialog, SIGNAL(reminderCount(int)),
237  mDocker, SLOT(slotUpdate(int)) );
238  connect( mDocker, SIGNAL(suspendAllSignal()),
239  mDialog, SLOT(suspendAll()) );
240  connect( mDocker, SIGNAL(dismissAllSignal()),
241  mDialog, SLOT(dismissAll()) );
242  }
243  }
244 
245  mDialog->addIncidence( aitem, remindAtDate, displayText );
246  mDialog->wakeUp();
247 #else
248  const Incidence::Ptr incidence = CalendarSupport::incidence( aitem );
249  Q_UNUSED( calendar );
250  Q_UNUSED( remindAtDate );
251  Q_UNUSED( displayText );
252 
253 #if defined(Q_WS_MAEMO_5)
254  QMaemo5InformationBox::information( 0, incidence->summary(), QMaemo5InformationBox::NoTimeout );
255 #else
256  KNotification *notify = new KNotification( "reminder", 0, KNotification::Persistent );
257  notify->setText( incidence->summary() );
258  notify->sendEvent();
259 #endif
260 
261 #endif
262  saveLastCheckTime();
263 }
264 
265 void KOAlarmClient::slotQuit()
266 {
267  emit saveAllSignal();
268  saveLastCheckTime();
269  quit();
270 }
271 
272 void KOAlarmClient::saveLastCheckTime()
273 {
274  KConfigGroup cg( KGlobal::config(), "Alarms" );
275  cg.writeEntry( "CalendarsLastChecked", mLastChecked );
276  KGlobal::config()->sync();
277 }
278 
279 void KOAlarmClient::quit()
280 {
281  kDebug();
282 #if !defined(KORGAC_AKONADI_AGENT)
283  kapp->quit();
284 #endif
285 }
286 
287 bool KOAlarmClient::commitData( QSessionManager & )
288 {
289  emit saveAllSignal();
290  saveLastCheckTime();
291  return true;
292 }
293 
294 void KOAlarmClient::forceAlarmCheck()
295 {
296  checkAlarms();
297  saveLastCheckTime();
298 }
299 
300 QString KOAlarmClient::dumpDebug()
301 {
302  KConfigGroup cfg( KGlobal::config(), "Alarms" );
303  const QDateTime lastChecked = cfg.readEntry( "CalendarsLastChecked", QDateTime() );
304  QString str = QString::fromLatin1("Last Check: %1").arg(lastChecked.toString());
305  return str;
306 }
307 
308 QStringList KOAlarmClient::dumpAlarms()
309 {
310  const KDateTime start = KDateTime( QDateTime::currentDateTime().date(),
311  QTime( 0, 0 ), KDateTime::LocalZone );
312  const KDateTime end = start.addDays( 1 ).addSecs( -1 );
313 
314  QStringList lst;
315  // Don't translate, this is for debugging purposes.
316  lst << QLatin1String( "AlarmDeamon::dumpAlarms() from " ) + start.toString() + QLatin1String(" to ") +
317  end.toString();
318 
319  Alarm::List alarms = mCalendar->alarms( start, end );
320  if (alarms.isEmpty() ) {
321  lst << QLatin1String("No alarm found.");
322  } else {
323  foreach( Alarm::Ptr a, alarms ) {
324  const Incidence::Ptr parentIncidence = mCalendar->incidence( a->parentUid() );
325  lst << QLatin1String( " " ) + parentIncidence->summary() + QLatin1String(" (") + a->time().toString() + QLatin1Char(')');
326  }
327  }
328 
329  return lst;
330 }
331 
332 void KOAlarmClient::hide()
333 {
334 #if !defined(KORGAC_AKONADI_AGENT)
335  delete mDocker;
336  mDocker = 0;
337 #endif
338 }
339 
340 void KOAlarmClient::show()
341 {
342 #if !defined(KORGAC_AKONADI_AGENT)
343  if ( !mDocker ) {
344  if ( dockerEnabled() ) {
345  mDocker = new AlarmDockWindow;
346  connect( this, SIGNAL(reminderCount(int)), mDocker, SLOT(slotUpdate(int)) );
347  connect( mDocker, SIGNAL(quitSignal()), SLOT(slotQuit()) );
348  }
349 
350  if ( mDialog ) {
351  connect( mDialog, SIGNAL(reminderCount(int)),
352  mDocker, SLOT(slotUpdate(int)) );
353  connect( mDocker, SIGNAL(suspendAllSignal()),
354  mDialog, SLOT(suspendAll()) );
355  connect( mDocker, SIGNAL(dismissAllSignal()),
356  mDialog, SLOT(dismissAll()) );
357  }
358  }
359 #endif
360 }
361 
KOAlarmClient::saveAllSignal
void saveAllSignal()
KOAlarmClient::dumpDebug
QString dumpDebug()
Definition: koalarmclient.cpp:300
QModelIndex
QDateTime::toString
QString toString(Qt::DateFormat format) const
checkAllItems
void checkAllItems(KCheckableProxyModel *model, const QModelIndex &parent=QModelIndex())
Definition: koalarmclient.cpp:103
alarmdockwindow.h
QSessionManager
alarmdialog.h
KOAlarmClient::commitData
bool commitData(QSessionManager &)
Definition: koalarmclient.cpp:287
KOAlarmClient::deferredInit
void deferredInit()
Definition: koalarmclient.cpp:116
KOAlarmClient::hide
void hide()
Definition: koalarmclient.cpp:332
KOAlarmClient::show
void show()
Definition: koalarmclient.cpp:340
QTime
KOAlarmClient::quit
void quit()
Definition: koalarmclient.cpp:279
KOAlarmClient::~KOAlarmClient
~KOAlarmClient()
Definition: koalarmclient.cpp:95
QObject
QString::isEmpty
bool isEmpty() const
koalarmclient.h
QString
AlarmDialog
Definition: alarmdialog.h:58
QStringList
KOAlarmClient::forceAlarmCheck
void forceAlarmCheck()
Definition: koalarmclient.cpp:294
QLatin1Char
AlarmDialog::wakeUp
void wakeUp()
Definition: alarmdialog.cpp:676
QDateTime::currentDateTime
QDateTime currentDateTime()
QLatin1String
AlarmDockWindow
Definition: alarmdockwindow.h:33
KOAlarmClient::checkAlarms
void checkAlarms()
Definition: koalarmclient.cpp:189
KOAlarmClient::reminderCount
void reminderCount(int)
QString::fromLatin1
QString fromLatin1(const char *str, int size)
QTimer::start
void start(int msec)
KOAlarmClient::slotQuit
void slotQuit()
Definition: koalarmclient.cpp:265
QDateTime::addSecs
QDateTime addSecs(int s) const
KOAlarmClient::KOAlarmClient
KOAlarmClient(QObject *parent=0)
Definition: koalarmclient.cpp:60
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
AlarmDialog::addIncidence
void addIncidence(const Akonadi::Item &incidence, const QDateTime &reminderAt, const QString &displayText)
Definition: alarmdialog.cpp:303
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QDateTime
KOAlarmClient::dumpAlarms
QStringList dumpAlarms()
Definition: koalarmclient.cpp:308
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:25 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
  • pimprint

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