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> 
   55 #include <QtMaemo5/QMaemo5InformationBox> 
   58 using namespace KCalCore;
 
   61   : 
QObject( parent ), mDocker( 0 ), mDialog( 0 )
 
   63   new KOrgacAdaptor( 
this );
 
   64   Akonadi::DBusConnectionPool::threadConnection().registerObject( 
QLatin1String(
"/ac"), 
this );
 
   67 #if !defined(KORGAC_AKONADI_AGENT) 
   68   if ( dockerEnabled() ) {
 
   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();
 
   81   connect( mETM, SIGNAL(collectionPopulated(Akonadi::Collection::Id)),
 
   83   connect( mETM, SIGNAL(collectionTreeFetched(Akonadi::Collection::List)),
 
   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() );
 
   92   mCheckTimer.
start( 1000 * interval );  
 
   97 #if !defined(KORGAC_AKONADI_AGENT) 
  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 );
 
  110     if ( model->rowCount( index ) > 0 ) {
 
  118   if ( !collectionsAvailable() ) {
 
  122   kDebug(5891) << 
"Performing delayed initialization.";
 
  125   KConfigGroup genGroup( KGlobal::config(), 
"General" );
 
  126   const int numReminders = genGroup.readEntry( 
"Reminders", 0 );
 
  128   for ( 
int i=1; i<=numReminders; ++i ) {
 
  130     const KConfigGroup incGroup( KGlobal::config(), group );
 
  132     const KUrl url = incGroup.readEntry( 
"AkonadiUrl" );
 
  133     Akonadi::Item::Id akonadiItemId = -1;
 
  134     if ( !url.isValid() ) {
 
  136       const QString uid = incGroup.readEntry( 
"UID" );
 
  138         akonadiItemId = mCalendar->item( uid ).id();
 
  141       akonadiItemId = Akonadi::Item::fromUrl( url ).id();
 
  144     if ( akonadiItemId >= 0 ) {
 
  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() );
 
  153   KCheckableProxyModel *checkableModel = mCalendar->checkableProxyModel();
 
  160 bool KOAlarmClient::dockerEnabled()
 
  162   KConfig korgConfig( KStandardDirs::locate( 
"config", 
QLatin1String(
"korganizerrc") ) );
 
  163   KConfigGroup generalGroup( &korgConfig, 
"System Tray" );
 
  164   return generalGroup.readEntry( 
"ShowReminderDaemon", 
true );
 
  167 bool KOAlarmClient::collectionsAvailable()
 const 
  170   if ( !mETM->isCollectionTreeFetched() ) {
 
  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 );
 
  180       mETM->data( index, Akonadi::EntityTreeModel::IsPopulatedRole ).toBool();
 
  191   KConfigGroup cfg( KGlobal::config(), 
"General" );
 
  193   if ( !cfg.readEntry( 
"Enabled", 
true ) ) {
 
  199   if ( !collectionsAvailable() ) {
 
  200     kDebug(5891) << 
"Collections are not available; aborting check.";
 
  207   kDebug(5891) << 
"Check:" << from.
toString() << 
" -" << mLastChecked.
toString();
 
  209   const Alarm::List alarms = mCalendar->alarms( KDateTime( from, KDateTime::LocalZone ),
 
  210                                                 KDateTime( mLastChecked, KDateTime::LocalZone ),
 
  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 );
 
  218     createReminder( mCalendar, item, from, alarm->text() );
 
  222 void KOAlarmClient::createReminder( 
const Akonadi::ETMCalendar::Ptr &calendar,
 
  223                                     const Akonadi::Item &aitem,
 
  227   if ( !CalendarSupport::hasIncidence( aitem ) ) {
 
  231 #if !defined(Q_WS_MAEMO_5) && !defined(KORGAC_AKONADI_AGENT) 
  237                mDocker, SLOT(slotUpdate(
int)) );
 
  238       connect( mDocker, SIGNAL(suspendAllSignal()),
 
  239                mDialog, SLOT(suspendAll()) );
 
  240       connect( mDocker, SIGNAL(dismissAllSignal()),
 
  241                mDialog, SLOT(dismissAll()) );
 
  245   mDialog->
addIncidence( aitem, remindAtDate, displayText );
 
  248   const Incidence::Ptr incidence = CalendarSupport::incidence( aitem );
 
  249   Q_UNUSED( calendar );
 
  250   Q_UNUSED( remindAtDate );
 
  251   Q_UNUSED( displayText );
 
  253 #if defined(Q_WS_MAEMO_5) 
  254   QMaemo5InformationBox::information( 0, incidence->summary(), QMaemo5InformationBox::NoTimeout );
 
  256   KNotification *notify = 
new KNotification( 
"reminder", 0, KNotification::Persistent );
 
  257   notify->setText( incidence->summary() );
 
  272 void KOAlarmClient::saveLastCheckTime()
 
  274   KConfigGroup cg( KGlobal::config(), 
"Alarms" );
 
  275   cg.writeEntry( 
"CalendarsLastChecked", mLastChecked );
 
  276   KGlobal::config()->sync();
 
  282 #if !defined(KORGAC_AKONADI_AGENT) 
  302   KConfigGroup cfg( KGlobal::config(), 
"Alarms" );
 
  311                                      QTime( 0, 0 ), KDateTime::LocalZone );
 
  312   const KDateTime end = start.addDays( 1 ).addSecs( -1 );
 
  319   Alarm::List alarms = mCalendar->alarms( start, end );
 
  320   if (alarms.isEmpty() ) {
 
  323       foreach( Alarm::Ptr a, alarms ) {
 
  324           const Incidence::Ptr parentIncidence = mCalendar->incidence( a->parentUid() );
 
  334 #if !defined(KORGAC_AKONADI_AGENT) 
  342 #if !defined(KORGAC_AKONADI_AGENT) 
  344     if ( dockerEnabled() ) {
 
  352                mDocker, SLOT(slotUpdate(
int)) );
 
  353       connect( mDocker, SIGNAL(suspendAllSignal()),
 
  354                mDialog, SLOT(suspendAll()) );
 
  355       connect( mDocker, SIGNAL(dismissAllSignal()),
 
  356                mDialog, SLOT(dismissAll()) );
 
QString toString(Qt::DateFormat format) const
void checkAllItems(KCheckableProxyModel *model, const QModelIndex &parent=QModelIndex())
bool commitData(QSessionManager &)
QDateTime currentDateTime()
QString fromLatin1(const char *str, int size)
QDateTime addSecs(int s) const
KOAlarmClient(QObject *parent=0)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void addIncidence(const Akonadi::Item &incidence, const QDateTime &reminderAt, const QString &displayText)
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const