~ubuntu-branches/ubuntu/vivid/kdepim/vivid

« back to all changes in this revision

Viewing changes to kalarm/kalarmapp.cpp

  • Committer: Package Import Robot
  • Author(s): Scott Kitterman, Jonathan Riddell, Rohan Garg, Scott Kitterman
  • Date: 2012-11-21 13:12:36 UTC
  • mfrom: (0.2.33)
  • Revision ID: package-import@ubuntu.com-20121121131236-32ijw9a2txrar80k
Tags: 4:4.9.80-0ubuntu1
[ Jonathan Riddell ]
* New upstream beta release

[ Rohan Garg ]
* Add nepomuk-core-dev to build-deps

[ Scott Kitterman ]
* Add new package, libpimcommon4
  - Add libpimcommon4.install
  - Add to debian/control, including kdepim-dbg and kdepim-dev depends
  - Add to kdepim-dev.install
* Remove usr/bin/backupmail and related files from kmail.install as they are
  not provided by upstream anymore
* Add usr/bin/pimsettingexporter and related files to kmail.install
* Add libnepomukwidgets-dev to build-depends

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
#include "alarmcalendar.h"
25
25
#include "alarmlistview.h"
 
26
#include "alarmtime.h"
26
27
#include "autoqpointer.h"
27
28
#include "commandoptions.h"
28
29
#include "dbushandler.h"
81
82
static const char* KTTSD_DBUS_SERVICE  = "org.kde.kttsd";
82
83
static const char* KTTDS_DBUS_PATH     = "/KSpeech";
83
84
 
 
85
#ifdef USE_AKONADI
 
86
static const int AKONADI_TIMEOUT = 30;   // timeout (seconds) for Akonadi collections to be populated
 
87
#endif
 
88
 
84
89
static void setEventCommandError(const KAEvent&, KAEvent::CmdErrType);
85
90
static void clearEventCommandError(const KAEvent&, KAEvent::CmdErrType);
86
91
 
327
332
            {
328
333
                // Display or delete the event with the specified event ID
329
334
                EventFunc function = (command == CommandOptions::TRIGGER_EVENT) ? EVENT_TRIGGER : EVENT_CANCEL;
330
 
                if (!initCheck(true))   // open the calendar, don't start processing execution queue yet
 
335
                // Open the calendar, don't start processing execution queue yet,
 
336
                // and wait for the Akonadi collection to be populated.
 
337
#ifdef USE_AKONADI
 
338
                if (!initCheck(true, true, options.eventId().collectionId()))
 
339
#else
 
340
                if (!initCheck(true))
 
341
#endif
331
342
                    exitCode = 1;
332
343
                else
333
344
                {
334
 
                    startProcessQueue();        // start processing the execution queue
 
345
                    startProcessQueue();      // start processing the execution queue
 
346
                    dontRedisplay = true;
 
347
#ifdef USE_AKONADI
 
348
                    if (!handleEvent(options.eventId(), function, true))
 
349
#else
335
350
                    if (!handleEvent(options.eventId(), function))
 
351
#endif
 
352
                    {
 
353
#ifdef USE_AKONADI
 
354
                        CommandOptions::printError(i18nc("@info:shell", "%1: Event <resource>%2</resource> not found, or not unique", "--" + options.commandName(), options.eventId().eventId()));
 
355
#else
 
356
                        CommandOptions::printError(i18nc("@info:shell", "%1: Event <resource>%2</resource> not found", "--" + options.commandName(), options.eventId()));
 
357
#endif
336
358
                        exitCode = 1;
 
359
                    }
337
360
                }
338
361
                break;
339
362
            }
 
363
            case CommandOptions::LIST:
 
364
                // Output a list of scheduled alarms to stdout.
 
365
                // Open the calendar, don't start processing execution queue yet,
 
366
                // and wait for all Akonadi collections to be populated.
 
367
#ifdef USE_AKONADI
 
368
                if (!initCheck(true, true))
 
369
#else
 
370
                if (!initCheck(true))
 
371
#endif
 
372
                    exitCode = 1;
 
373
                else
 
374
                {
 
375
                    dontRedisplay = true;
 
376
                    QStringList alarms = scheduledAlarmList();
 
377
                    for (int i = 0, count = alarms.count();  i < count;  ++i)
 
378
                        std::cout << alarms[i].toUtf8().constData() << std::endl;
 
379
                }
 
380
                break;
340
381
            case CommandOptions::EDIT:
341
 
                // Edit a specified existing alarm
 
382
                // Edit a specified existing alarm.
 
383
                // Open the calendar and wait for the Akonadi collection to be populated.
 
384
#ifdef USE_AKONADI
 
385
                if (!initCheck(false, true, options.eventId().collectionId()))
 
386
#else
342
387
                if (!initCheck())
 
388
#endif
343
389
                    exitCode = 1;
344
390
                else if (!KAlarm::editAlarmById(options.eventId()))
345
391
                {
346
 
                    CommandOptions::printError(i18nc("@info:shell", "<icode>%1</icode>: Event <resource>%2</resource> not found, or not editable", QString::fromLatin1("--edit"), options.eventId()));
 
392
#ifdef USE_AKONADI
 
393
                    CommandOptions::printError(i18nc("@info:shell", "%1: Event <resource>%2</resource> not found, or not editable", "--" + options.commandName(), options.eventId().eventId()));
 
394
#else
 
395
                    CommandOptions::printError(i18nc("@info:shell", "%1: Event <resource>%2</resource> not found, or not editable", "--" + options.commandName(), options.eventId()));
 
396
#endif
347
397
                    exitCode = 1;
348
398
                }
349
399
                break;
518
568
    done = true;
519
569
#if KDE_IS_VERSION(4,5,70)
520
570
    if (!KSystemTimeZones::isTimeZoneDaemonAvailable())
521
 
    {    
 
571
    {
522
572
        kDebug() << "ktimezoned not running: using UTC only";
523
573
        KAMessageBox::information(MainWindow::mainMainWindow(),
524
574
                                  i18nc("@info", "Time zones are not accessible:<nl/>KAlarm will use the UTC time zone.<nl/><nl/>(The KDE time zone service is not available:<nl/>check that <application>ktimezoned</application> is installed.)"),
709
759
    if (interval <= 0)
710
760
    {
711
761
        // Queue the alarm
712
 
        queueAlarmId(nextEvent->id());
 
762
        queueAlarmId(*nextEvent);
713
763
        kDebug() << nextEvent->id() << ": due now";
714
764
        QTimer::singleShot(0, this, SLOT(processQueue()));
715
765
    }
742
792
* Also called when the execution queue has finished processing to check for the
743
793
* next alarm.
744
794
*/
745
 
void KAlarmApp::queueAlarmId(const QString& id)
 
795
void KAlarmApp::queueAlarmId(const KAEvent& event)
746
796
{
 
797
#ifdef USE_AKONADI
 
798
    EventId id(event);
 
799
#else
 
800
    const QString id(event.id());
 
801
#endif
747
802
    for (int i = 0, end = mActionQueue.count();  i < end;  ++i)
748
803
    {
749
804
        if (mActionQueue[i].function == EVENT_HANDLE  &&  mActionQueue[i].eventId == id)
798
853
                if (!cancelReminderAndDeferral(event))
799
854
                {
800
855
                    if (mAlarmsEnabled)
801
 
                        queueAlarmId(event.id());
 
856
                        queueAlarmId(event);
802
857
                }
803
858
            }
804
859
            mLoginAlarmsDone = true;
863
918
    {
864
919
        if (mAlarmsEnabled)
865
920
        {
866
 
            mActionQueue.enqueue(ActionQEntry(EVENT_HANDLE, ev.id()));
 
921
            mActionQueue.enqueue(ActionQEntry(EVENT_HANDLE, EventId(ev)));
867
922
            if (mInitialised)
868
923
                QTimer::singleShot(0, this, SLOT(processQueue()));
869
924
        }
1127
1182
 
1128
1183
 
1129
1184
/******************************************************************************
 
1185
* Output a list of pending alarms, with their next scheduled occurrence.
 
1186
*/
 
1187
QStringList KAlarmApp::scheduledAlarmList()
 
1188
{
 
1189
#ifdef USE_AKONADI
 
1190
    QVector<KAEvent> events = KAlarm::getSortedActiveEvents(this);
 
1191
#else
 
1192
    KAEvent::List events = KAlarm::getSortedActiveEvents();
 
1193
#endif
 
1194
    QStringList alarms;
 
1195
    for (int i = 0, count = events.count();  i < count;  ++i)
 
1196
    {
 
1197
#ifdef USE_AKONADI
 
1198
        KAEvent* event = &events[i];
 
1199
#else
 
1200
        KAEvent* event = events[i];
 
1201
#endif
 
1202
        KDateTime dateTime = event->nextTrigger(KAEvent::DISPLAY_TRIGGER).effectiveKDateTime().toLocalZone();
 
1203
#ifdef USE_AKONADI
 
1204
        Akonadi::Collection c(event->collectionId());
 
1205
        AkonadiModel::instance()->refresh(c);
 
1206
        QString text(c.resource() + ":");
 
1207
#else
 
1208
        QString text;
 
1209
#endif
 
1210
        text += event->id() + ' '
 
1211
             +  dateTime.toString("%Y%m%dT%H%M ")
 
1212
             +  AlarmText::summary(events[i], 1);
 
1213
        alarms << text;
 
1214
    }
 
1215
    return alarms;
 
1216
}
 
1217
 
 
1218
/******************************************************************************
1130
1219
* Enable or disable alarm monitoring.
1131
1220
*/
1132
1221
void KAlarmApp::setAlarmsEnabled(bool enabled)
1228
1317
* Optionally display the event. Delete the event from the calendar file and
1229
1318
* from every main window instance.
1230
1319
*/
 
1320
#ifdef USE_AKONADI
 
1321
bool KAlarmApp::dbusHandleEvent(const EventId& eventID, EventFunc function)
 
1322
#else
1231
1323
bool KAlarmApp::dbusHandleEvent(const QString& eventID, EventFunc function)
 
1324
#endif
1232
1325
{
1233
1326
    kDebug() << eventID;
1234
1327
    mActionQueue.append(ActionQEntry(function, eventID));
1238
1331
}
1239
1332
 
1240
1333
/******************************************************************************
 
1334
* Called in response to a D-Bus request to list all pending alarms.
 
1335
*/
 
1336
QString KAlarmApp::dbusList()
 
1337
{
 
1338
    kDebug();
 
1339
    return scheduledAlarmList().join("\n") + '\n';
 
1340
}
 
1341
 
 
1342
/******************************************************************************
1241
1343
* Either:
1242
1344
* a) Display the event and then delete it if it has no outstanding repetitions.
1243
1345
* b) Delete the event.
1247
1349
* Reply = false if event ID not found, or if more than one event with the same
1248
1350
*         ID is found.
1249
1351
*/
 
1352
#ifdef USE_AKONADI
 
1353
bool KAlarmApp::handleEvent(const EventId& id, EventFunc function, bool checkDuplicates)
 
1354
#else
1250
1355
bool KAlarmApp::handleEvent(const QString& eventID, EventFunc function)
 
1356
#endif
1251
1357
{
1252
1358
    // Delete any expired wake-on-suspend config data
1253
1359
    KAlarm::checkRtcWakeConfig();
1254
1360
 
1255
1361
#ifdef USE_AKONADI
1256
 
    KAEvent::List events = AlarmCalendar::resources()->events(eventID);
1257
 
    if (events.count() > 1)
 
1362
    const QString eventID(id.eventId());
 
1363
    KAEvent* event = AlarmCalendar::resources()->event(id, checkDuplicates);
 
1364
    if (!event)
1258
1365
    {
1259
 
        kWarning() << "Multiple events found with ID" << eventID;
 
1366
        if (id.collectionId() != -1)
 
1367
            kWarning() << "Event ID not found, or duplicated:" << eventID;
 
1368
        else
 
1369
            kWarning() << "Event ID not found:" << eventID;
1260
1370
        return false;
1261
1371
    }
1262
 
    if (events.isEmpty())
1263
1372
#else
1264
1373
    KAEvent* event = AlarmCalendar::resources()->event(eventID);
1265
1374
    if (!event)
1266
 
#endif
1267
1375
    {
1268
1376
        kWarning() << "Event ID not found:" << eventID;
1269
1377
        return false;
1270
1378
    }
1271
 
#ifdef USE_AKONADI
1272
 
    KAEvent* event = events[0];
1273
1379
#endif
1274
1380
    switch (function)
1275
1381
    {
1511
1617
    event.startChanges();
1512
1618
    if (alarm.repeatAtLogin())
1513
1619
    {
1514
 
        // Leave an alarm which repeats at every login until its main alarm triggers 
 
1620
        // Leave an alarm which repeats at every login until its main alarm triggers
1515
1621
        if (!event.reminderActive()  &&  event.reminderMinutes() < 0)
1516
1622
        {
1517
1623
            // Executing an at-login alarm: first schedule the reminder
1551
1657
                        // Note that at-login reminders are scheduled in execAlarm().
1552
1658
                        event.activateReminderAfter(last);
1553
1659
                        updateCalAndDisplay = true;
1554
 
                    }         
 
1660
                    }
1555
1661
                    if (cancelAlarm(event, alarm.type(), updateCalAndDisplay))
1556
1662
                        return -1;
1557
1663
                    break;
1601
1707
            // Set the reminder which is now due after the last main alarm trigger.
1602
1708
            // Note that at-login reminders are scheduled in execAlarm().
1603
1709
            event.activateReminderAfter(last);
1604
 
        }         
 
1710
        }
1605
1711
    }
1606
1712
    event.endChanges();
1607
1713
    if (update)
2216
2322
* If this is the first time through, open the calendar file, and start
2217
2323
* processing the execution queue.
2218
2324
*/
 
2325
#ifdef USE_AKONADI
 
2326
bool KAlarmApp::initCheck(bool calendarOnly, bool waitForCollection, Akonadi::Collection::Id collectionId)
 
2327
#else
2219
2328
bool KAlarmApp::initCheck(bool calendarOnly)
 
2329
#endif
2220
2330
{
2221
2331
    static bool firstTime = true;
2222
2332
    if (firstTime)
2239
2349
 
2240
2350
    if (!calendarOnly)
2241
2351
        startProcessQueue();      // start processing the execution queue
 
2352
 
 
2353
#ifdef USE_AKONADI
 
2354
    if (waitForCollection)
 
2355
    {
 
2356
#if KDE_IS_VERSION(4,9,80)
 
2357
        // Wait for one or all Akonadi collections to be populated
 
2358
        if (!CollectionControlModel::instance()->waitUntilPopulated(collectionId, AKONADI_TIMEOUT))
 
2359
            return false;
 
2360
#endif
 
2361
    }
 
2362
#endif
2242
2363
    return true;
2243
2364
}
2244
2365