alarmdialog.cpp 34.4 KB
Newer Older
1
/*
Allen Winter's avatar
Allen Winter committed
2
  This file is part of the KDE reminder agent.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

  Copyright (c) 2000,2003 Cornelius Schumacher <schumacher@kde.org>
  Copyright (c) 2008-2009 Allen Winter <winter@kde.org>
  Copyright (c) 2009-2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

  As a special exception, permission is given to link this program
  with any edition of Qt, and distribute the resulting executable,
  without including the source code for Qt in the source distribution.
*/

Laurent Montel's avatar
Laurent Montel committed
27
#include "config-korganizer.h"
28 29
#include "alarmdialog.h"
#include "korganizer_interface.h"
30
#include "mailclient.h"
Laurent Montel's avatar
Laurent Montel committed
31
#include "koalarmclient_debug.h"
32

Laurent Montel's avatar
Laurent Montel committed
33 34 35 36
#include <CalendarSupport/IncidenceViewer>
#include <CalendarSupport/KCalPrefs>
#include <CalendarSupport/IdentityManager>
#include <CalendarSupport/Utils>
37

38 39
#include <IncidenceEditor/IncidenceDialog>
#include <IncidenceEditor/IncidenceDialogFactory>
40

41 42
#include <KCalCore/Event>
#include <KCalCore/Todo>
Laurent Montel's avatar
Laurent Montel committed
43
#include <KCalUtils/IncidenceFormatter>
44

Laurent Montel's avatar
Laurent Montel committed
45 46
#include <KIdentityManagement/Identity>
#include <KIdentityManagement/IdentityManager>
47

Laurent Montel's avatar
Laurent Montel committed
48
#include <AkonadiCore/Item>
49

Laurent Montel's avatar
Laurent Montel committed
50
#include <MailTransport/TransportManager>
Laurent Montel's avatar
Laurent Montel committed
51
#include <QUrl>
52

Laurent Montel's avatar
Laurent Montel committed
53
#include <KLocale>
54
#include <KComboBox>
Laurent Montel's avatar
Laurent Montel committed
55
#include <QHBoxLayout>
Laurent Montel's avatar
Laurent Montel committed
56
#include <KLocalizedString>
57 58 59 60 61 62
#include <KMessageBox>
#include <KNotification>
#include <KSharedConfig>
#include <KSystemTimeZone>
#include <KToolInvocation>
#include <KWindowSystem>
Laurent Montel's avatar
Port++  
Laurent Montel committed
63
#include <KIconLoader>
Laurent Montel's avatar
Laurent Montel committed
64
#include <QIcon>
65
#include <phonon/mediaobject.h>
66
#include <QLabel>
67
#include <QKeyEvent>
68 69 70
#include <QSpinBox>
#include <QTreeWidget>
#include <QVBoxLayout>
Laurent Montel's avatar
Laurent Montel committed
71 72 73
#include <KConfigGroup>
#include <QDialogButtonBox>
#include <QPushButton>
74

Laurent Montel's avatar
Laurent Montel committed
75
using namespace KIdentityManagement;
76 77 78 79 80 81
using namespace KCalCore;
using namespace KCalUtils;

static int defSuspendVal = 5;
static int defSuspendUnit = 0; // 0=>minutes, 1=>hours, 2=>days, 3=>weeks

82
class ReminderTree : public QTreeWidget
83
{
Laurent Montel's avatar
Laurent Montel committed
84 85
public:
    ReminderTree(QWidget *parent) : QTreeWidget(parent)
86 87 88 89 90 91 92
    {
    }

};

class ReminderTreeItem : public QTreeWidgetItem
{
Laurent Montel's avatar
Laurent Montel committed
93 94 95
public:
    ReminderTreeItem(const Akonadi::Item &incidence, QTreeWidget *parent)
        : QTreeWidgetItem(parent), mIncidence(incidence), mNotified(false)
96 97
    {
    }
Sergio Martins's avatar
Sergio Martins committed
98
    bool operator<(const QTreeWidgetItem &other) const Q_DECL_OVERRIDE;
99 100 101 102 103 104 105 106 107 108

    QString mDisplayText;

    const Akonadi::Item mIncidence;
    QDateTime mRemindAt;
    KDateTime mTrigger;
    KDateTime mHappening;
    bool mNotified;
};

109
struct ConfItem {
Laurent Montel's avatar
Laurent Montel committed
110 111 112
    QString uid;
    QUrl akonadiUrl;
    QDateTime remindAt;
113 114
};

Laurent Montel's avatar
Laurent Montel committed
115
bool ReminderTreeItem::operator<(const QTreeWidgetItem &other) const
116
{
Laurent Montel's avatar
Laurent Montel committed
117 118 119
    switch (treeWidget()->sortColumn()) {
    case 1: { // happening datetime
        const ReminderTreeItem *item = static_cast<const ReminderTreeItem *>(&other);
120
        return item->mHappening < mHappening;
Laurent Montel's avatar
Laurent Montel committed
121 122 123
    }
    case 2: { // trigger datetime
        const ReminderTreeItem *item = static_cast<const ReminderTreeItem *>(&other);
124
        return item->mTrigger < mTrigger;
Laurent Montel's avatar
Laurent Montel committed
125 126 127 128
    }
    default:
        return QTreeWidgetItem::operator < (other);
    }
129 130
}

Laurent Montel's avatar
Laurent Montel committed
131 132 133
AlarmDialog::AlarmDialog(const Akonadi::ETMCalendar::Ptr &calendar, QWidget *parent)
    : QDialog(parent, Qt::WindowStaysOnTopHint),
      mCalendar(calendar), mSuspendTimer(this)
134
{
Laurent Montel's avatar
Laurent Montel committed
135 136 137 138 139
    // User1 => Edit...
    // User2 => Dismiss All
    // User3 => Dismiss Selected
    //    Ok => Suspend

140
    connect(calendar.data(), &Akonadi::ETMCalendar::calendarChanged, this, &AlarmDialog::slotCalendarChanged);
Laurent Montel's avatar
Laurent Montel committed
141

Laurent Montel's avatar
Laurent Montel committed
142
    KIconLoader::global()->addAppDir(QStringLiteral("korgac"));
Laurent Montel's avatar
Laurent Montel committed
143 144 145 146 147 148 149 150 151 152 153

    KSharedConfig::Ptr config = KSharedConfig::openConfig();
    KConfigGroup generalConfig(config, "General");
    QPoint pos = generalConfig.readEntry("Position", QPoint(0, 0));

    QWidget *topBox = new QWidget(this);
    if (!pos.isNull()) {
        mPos = pos;
        topBox->move(mPos);
    }
    setWindowTitle(i18nc("@title:window", "Reminders"));
Laurent Montel's avatar
Laurent Montel committed
154
    setWindowIcon(QIcon::fromTheme(QStringLiteral("korgac")));
Laurent Montel's avatar
Laurent Montel committed
155 156 157 158 159 160 161 162 163 164 165 166
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok);
    QVBoxLayout *mainLayout = new QVBoxLayout;
    setLayout(mainLayout);
    mainLayout->addWidget(topBox);
    mOkButton = buttonBox->button(QDialogButtonBox::Ok);
    mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
    mUser1Button = new QPushButton;
    buttonBox->addButton(mUser1Button, QDialogButtonBox::ActionRole);
    mUser2Button = new QPushButton;
    buttonBox->addButton(mUser2Button, QDialogButtonBox::ActionRole);
    mUser3Button = new QPushButton;
    buttonBox->addButton(mUser3Button, QDialogButtonBox::ActionRole);
Laurent Montel's avatar
Laurent Montel committed
167 168
    connect(buttonBox, &QDialogButtonBox::accepted, this, &AlarmDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &AlarmDialog::reject);
Laurent Montel's avatar
Laurent Montel committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
    mainLayout->addWidget(buttonBox);

    mUser3Button->setText(i18nc("@action:button", "Dismiss Reminder"));
    mUser3Button->setToolTip(i18nc("@info:tooltip",
                                   "Dismiss the reminders for the selected incidences"));
    mUser2Button->setText(i18nc("@action:button", "Dismiss All"));
    mUser2Button->setToolTip(i18nc("@info:tooltip",
                                   "Dismiss the reminders for all listed incidences"));
    mUser1Button->setText(i18nc("@action:button", "Edit..."));
    mUser1Button->setToolTip(i18nc("@info:tooltip",
                                   "Edit the selected incidence"));
    mOkButton->setText(i18nc("@action:button", "Suspend"));
    mOkButton->setToolTip(i18nc("@info:tooltip",
                                "Suspend the reminders for the selected incidences "
                                "by the specified interval"));

    // Try to keep the dialog small and non-obtrusive.
    setMinimumWidth(575);
    setMinimumHeight(300);

    QVBoxLayout *mTopLayout = new QVBoxLayout(topBox);

    QLabel *label = new QLabel(
        i18nc("@label",
              "Reminders: "
              "Click on a title to toggle the details viewer for that item"),
        topBox);
    mTopLayout->addWidget(label);

    mIncidenceTree = new ReminderTree(topBox);
    mIncidenceTree->setColumnCount(3);
    mIncidenceTree->setSortingEnabled(true);
    const QStringList headerLabels =
        (QStringList(i18nc("@title:column reminder title", "Title"))
         << i18nc("@title:column happens at date/time", "Date Time")
         << i18nc("@title:column trigger date/time", "Trigger Time"));
    mIncidenceTree->setHeaderLabels(headerLabels);
    mIncidenceTree->headerItem()->setToolTip(
        0,
        i18nc("@info:tooltip", "The event or to-do title"));
    mIncidenceTree->headerItem()->setToolTip(
        1,
        i18nc("@info:tooltip", "The reminder is set for this date/time"));
    mIncidenceTree->headerItem()->setToolTip(
        2,
        i18nc("@info:tooltip", "The date/time the reminder was triggered"));

    mIncidenceTree->setWordWrap(true);
    mIncidenceTree->setAllColumnsShowFocus(true);
    mIncidenceTree->setSelectionMode(QAbstractItemView::ExtendedSelection);
    mIncidenceTree->setRootIsDecorated(false);

    mTopLayout->addWidget(mIncidenceTree);

223
    connect(mIncidenceTree, &QTreeWidget::itemClicked, this, &AlarmDialog::update);
Laurent Montel's avatar
Laurent Montel committed
224
    connect(mIncidenceTree, &QTreeWidget::itemDoubleClicked, this, &AlarmDialog::edit);
225
    connect(mIncidenceTree, &ReminderTree::itemSelectionChanged, this, &AlarmDialog::updateButtons);
Laurent Montel's avatar
Laurent Montel committed
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

    mDetailView = new CalendarSupport::IncidenceViewer(mCalendar.data(), topBox);
    QString s;
    s = xi18nc("@info default incidence details string",
               "<emphasis>Select an event or to-do from the list above "
               "to view its details here.</emphasis>");
    mDetailView->setDefaultMessage(s);
    mTopLayout->addWidget(mDetailView);
    mDetailView->hide();
    mLastItem = 0;

    QWidget *suspendBox = new QWidget(topBox);
    QHBoxLayout *suspendBoxHBoxLayout = new QHBoxLayout(suspendBox);
    suspendBoxHBoxLayout->setMargin(0);
    mTopLayout->addWidget(suspendBox);

    QLabel *l = new QLabel(i18nc("@label:spinbox", "Suspend &duration:"), suspendBox);
    suspendBoxHBoxLayout->addWidget(l);

    mSuspendSpin = new QSpinBox(suspendBox);
    suspendBoxHBoxLayout->addWidget(mSuspendSpin);
    mSuspendSpin->setRange(1, 9999);
    mSuspendSpin->setValue(defSuspendVal);    // default suspend duration
    mSuspendSpin->setToolTip(
        i18nc("@info:tooltip",
              "Suspend the reminders by this amount of time"));
    mSuspendSpin->setWhatsThis(
        i18nc("@info:whatsthis",
              "Each reminder for the selected incidences will be suspended "
              "by this number of time units. You can choose the time units "
              "(typically minutes) in the adjacent selector."));

    l->setBuddy(mSuspendSpin);

    mSuspendUnit = new KComboBox(suspendBox);
    suspendBoxHBoxLayout->addWidget(mSuspendUnit);
    mSuspendUnit->addItem(i18nc("@item:inlistbox suspend in terms of minutes", "minute(s)"));
    mSuspendUnit->addItem(i18nc("@item:inlistbox suspend in terms of hours", "hour(s)"));
    mSuspendUnit->addItem(i18nc("@item:inlistbox suspend in terms of days", "day(s)"));
    mSuspendUnit->addItem(i18nc("@item:inlistbox suspend in terms of weeks", "week(s)"));
    mSuspendUnit->setToolTip(
        i18nc("@info:tooltip",
              "Suspend the reminders using this time unit"));
    mSuspendUnit->setWhatsThis(
        i18nc("@info:whatsthis",
              "Each reminder for the selected incidences will be suspended "
              "using this time unit. You can set the number of time units "
              "in the adjacent number entry input."));

    mSuspendUnit->setCurrentIndex(defSuspendUnit);

    connect(&mSuspendTimer, &QTimer::timeout, this, &AlarmDialog::wakeUp);

    connect(mOkButton, &QPushButton::clicked, this, &AlarmDialog::slotOk);
    connect(mUser1Button, &QPushButton::clicked, this, &AlarmDialog::slotUser1);
    connect(mUser2Button, &QPushButton::clicked, this, &AlarmDialog::slotUser2);
    connect(mUser3Button, &QPushButton::clicked, this, &AlarmDialog::slotUser3);

    mIdentityManager = new CalendarSupport::IdentityManager;
285 286 287 288
}

AlarmDialog::~AlarmDialog()
{
Laurent Montel's avatar
Laurent Montel committed
289 290
    mIncidenceTree->clear();
    delete mIdentityManager;
291 292
}

Laurent Montel's avatar
Laurent Montel committed
293
ReminderTreeItem *AlarmDialog::searchByItem(const Akonadi::Item &incidence)
294
{
Laurent Montel's avatar
Laurent Montel committed
295 296 297 298 299 300 301 302 303
    ReminderTreeItem *found = 0;
    QTreeWidgetItemIterator it(mIncidenceTree);
    while (*it) {
        ReminderTreeItem *item = static_cast<ReminderTreeItem *>(*it);
        if (item->mIncidence == incidence) {
            found = item;
            break;
        }
        ++it;
304
    }
Laurent Montel's avatar
Laurent Montel committed
305
    return found;
306 307
}

Laurent Montel's avatar
Laurent Montel committed
308
static QString cleanSummary(const QString &summary)
309
{
Laurent Montel's avatar
Laurent Montel committed
310 311 312 313 314 315 316 317 318 319
    static QString etc = i18nc("@label an elipsis", "...");
    int maxLen = 30;
    QString retStr = summary;
    retStr.replace(QLatin1Char('\n'), QLatin1Char(' '));
    if (retStr.length() > maxLen) {
        maxLen -= etc.length();
        retStr = retStr.left(maxLen);
        retStr += etc;
    }
    return retStr;
320 321
}

Laurent Montel's avatar
Laurent Montel committed
322 323 324
void AlarmDialog::addIncidence(const Akonadi::Item &incidenceitem,
                               const QDateTime &reminderAt,
                               const QString &displayText)
325
{
Laurent Montel's avatar
Laurent Montel committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    Incidence::Ptr incidence = CalendarSupport::incidence(incidenceitem);
    ReminderTreeItem *item = searchByItem(incidenceitem);
    if (!item) {
        item = new ReminderTreeItem(incidenceitem, mIncidenceTree);
    }
    item->mNotified = false;
    item->mHappening = KDateTime();
    item->mRemindAt = reminderAt;
    item->mTrigger = KDateTime::currentLocalDateTime();
    item->mDisplayText = displayText;
    item->setText(0, cleanSummary(incidence->summary()));

    QString displayStr;
    const KDateTime dateTime = triggerDateForIncidence(incidence, reminderAt,
                               displayStr);

    if (incidence->type() == Incidence::TypeEvent) {
Laurent Montel's avatar
Laurent Montel committed
343
        item->setIcon(0, QIcon::fromTheme(QStringLiteral("view-calendar-day")));
Laurent Montel's avatar
Laurent Montel committed
344
    } else if (incidence->type() == Incidence::TypeTodo) {
Laurent Montel's avatar
Laurent Montel committed
345
        item->setIcon(0, QIcon::fromTheme(QStringLiteral("view-calendar-tasks")));
Laurent Montel's avatar
Laurent Montel committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
    }

    item->mHappening = dateTime;
    item->setText(1, displayStr);

    item->setText(2, IncidenceFormatter::dateTimeToString(
                      item->mTrigger, false, true, KDateTime::Spec::LocalZone()));
    QString tip =
        IncidenceFormatter::toolTipStr(
            CalendarSupport::displayName(mCalendar.data(), incidenceitem.parentCollection()),
            incidence,
            item->mRemindAt.date(), true,
            KDateTime::Spec::LocalZone());
    if (!item->mDisplayText.isEmpty()) {
        tip += QLatin1String("<br>") + item->mDisplayText;
    }
    item->setToolTip(0, tip);
    item->setToolTip(1, tip);
    item->setToolTip(2, tip);
    item->setData(0, QTreeWidgetItem::UserType, false);

    mIncidenceTree->setCurrentItem(item);
    showDetails(item);
    slotSave();
370 371 372 373
}

void AlarmDialog::slotOk()
{
Laurent Montel's avatar
Laurent Montel committed
374
    suspend();
375 376 377 378
}

void AlarmDialog::slotUser1()
{
379
    const ReminderList selection = selectedItems();
Laurent Montel's avatar
Laurent Montel committed
380
    if (!selection.isEmpty()) {
381
        ReminderTreeItem  *item = selection.first();
Laurent Montel's avatar
Laurent Montel committed
382
        if (mCalendar->hasRight(item->mIncidence, Akonadi::Collection::CanChangeItem)) {
383 384 385
            edit();
        }
    }
386 387 388 389
}

void AlarmDialog::slotUser2()
{
Laurent Montel's avatar
Laurent Montel committed
390
    dismissAll();
391 392 393 394
}

void AlarmDialog::slotUser3()
{
Laurent Montel's avatar
Laurent Montel committed
395
    dismissCurrent();
396 397 398 399
}

void AlarmDialog::dismissCurrent()
{
Laurent Montel's avatar
Laurent Montel committed
400
    dismiss(selectedItems());
401

Laurent Montel's avatar
Laurent Montel committed
402 403 404 405 406
    if (activeCount() == 0) {
        accept();
    } else {
        update();
    }
Laurent Montel's avatar
Laurent Montel committed
407
    Q_EMIT reminderCount(activeCount());
408 409 410 411
}

void AlarmDialog::dismissAll()
{
Laurent Montel's avatar
Laurent Montel committed
412
    ReminderList selections;
413

Laurent Montel's avatar
Laurent Montel committed
414 415 416 417 418 419
    QTreeWidgetItemIterator it(mIncidenceTree);
    while (*it) {
        if (!(*it)->isDisabled()) {   //do not disable suspended reminders
            selections.append(static_cast<ReminderTreeItem *>(*it));
        }
        ++it;
420
    }
Laurent Montel's avatar
Laurent Montel committed
421
    dismiss(selections);
422

Laurent Montel's avatar
Laurent Montel committed
423 424
    setTimer();
    accept();
Laurent Montel's avatar
Laurent Montel committed
425
    Q_EMIT reminderCount(activeCount());
426 427
}

Laurent Montel's avatar
Laurent Montel committed
428
void AlarmDialog::dismiss(ReminderList selections)
429
{
Laurent Montel's avatar
Laurent Montel committed
430
    QList<Akonadi::Item::Id> ids;
431
    ids.reserve(selections.count());
Laurent Montel's avatar
Laurent Montel committed
432
    for (ReminderList::Iterator it = selections.begin(); it != selections.end(); ++it) {
Laurent Montel's avatar
Laurent Montel committed
433
        qCDebug(KOALARMCLIENT_LOG) << "removing " << CalendarSupport::incidence((*it)->mIncidence)->summary();
Laurent Montel's avatar
Laurent Montel committed
434 435 436 437 438 439 440 441
        if (mIncidenceTree->itemBelow(*it)) {
            mIncidenceTree->setCurrentItem(mIncidenceTree->itemBelow(*it));
        } else if (mIncidenceTree->itemAbove(*it)) {
            mIncidenceTree->setCurrentItem(mIncidenceTree->itemAbove(*it));
        }
        mIncidenceTree->removeItemWidget(*it, 0);
        ids.append((*it)->mIncidence.id());
        delete *it;
442
    }
443

Laurent Montel's avatar
Laurent Montel committed
444
    removeFromConfig(ids);
445 446 447 448
}

void AlarmDialog::edit()
{
Laurent Montel's avatar
Laurent Montel committed
449 450 451 452 453 454 455 456 457 458 459 460 461
    ReminderList selection = selectedItems();
    if (selection.count() != 1) {
        return;
    }
    Incidence::Ptr incidence = CalendarSupport::incidence(selection.first()->mIncidence);
    if (!mCalendar->hasRight(selection.first()->mIncidence, Akonadi::Collection::CanChangeItem)) {
        KMessageBox::sorry(
            this,
            i18nc("@info",
                  "\"%1\" is a read-only item so modifications are not possible.",
                  cleanSummary(incidence->summary())));
        return;
    }
462

Laurent Montel's avatar
Laurent Montel committed
463
    openIncidenceEditorNG(selection.first()->mIncidence);
464 465 466 467
}

void AlarmDialog::suspend()
{
Laurent Montel's avatar
Laurent Montel committed
468 469 470
    if (!isVisible()) {   //do nothing if the dialog is hidden
        return;
    }
471

Laurent Montel's avatar
Laurent Montel committed
472 473
    int unit = 1;
    switch (mSuspendUnit->currentIndex()) {
474
    case 3: // weeks
Laurent Montel's avatar
Laurent Montel committed
475
        unit *=  7;
476
    case 2: // days
Laurent Montel's avatar
Laurent Montel committed
477
        unit *= 24;
478
    case 1: // hours
Laurent Montel's avatar
Laurent Montel committed
479
        unit *= 60;
480
    case 0: // minutes
Laurent Montel's avatar
Laurent Montel committed
481
        unit *= 60;
482
    default:
Laurent Montel's avatar
Laurent Montel committed
483
        break;
484
    }
Laurent Montel's avatar
Laurent Montel committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499

    ReminderTreeItem *selitem = 0;
    QTreeWidgetItemIterator it(mIncidenceTree);
    while (*it) {
        if ((*it)->isSelected() && !(*it)->isDisabled()) {   //suspend selected, non-suspended reminders
            (*it)->setSelected(false);
            (*it)->setDisabled(true);
            ReminderTreeItem *item = static_cast<ReminderTreeItem *>(*it);
            item->mRemindAt = QDateTime::currentDateTime().addSecs(unit * mSuspendSpin->value());
            item->mHappening = KDateTime(item->mRemindAt, KDateTime::Spec::LocalZone());
            item->mNotified = false;
            (*it)->setText(1, KLocale::global()->formatDateTime(item->mHappening));
            selitem = item;
        }
        ++it;
500 501
    }

Laurent Montel's avatar
Laurent Montel committed
502 503 504 505 506 507 508
    if (selitem) {
        if (mIncidenceTree->itemBelow(selitem)) {
            mIncidenceTree->setCurrentItem(mIncidenceTree->itemBelow(selitem));
        } else if (mIncidenceTree->itemAbove(selitem)) {
            mIncidenceTree->setCurrentItem(mIncidenceTree->itemAbove(selitem));
        }
    }
509

Laurent Montel's avatar
Laurent Montel committed
510 511 512 513 514 515 516 517 518 519
    // save suspended alarms too so they can be restored on restart
    // kolab/issue4108
    slotSave();

    setTimer();
    if (activeCount() == 0) {
        accept();
    } else {
        update();
    }
Laurent Montel's avatar
Laurent Montel committed
520
    Q_EMIT reminderCount(activeCount());
521 522 523 524
}

void AlarmDialog::setTimer()
{
Laurent Montel's avatar
Laurent Montel committed
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
    int nextReminderAt = -1;

    QTreeWidgetItemIterator it(mIncidenceTree);
    while (*it) {
        ReminderTreeItem *item = static_cast<ReminderTreeItem *>(*it);
        if (item->mRemindAt > QDateTime::currentDateTime()) {
            const int secs = QDateTime::currentDateTime().secsTo(item->mRemindAt);
            nextReminderAt = nextReminderAt <= 0 ? secs : qMin(nextReminderAt, secs);
        }
        ++it;
    }

    if (nextReminderAt >= 0) {
        mSuspendTimer.stop();
        mSuspendTimer.start(1000 * (nextReminderAt + 1));
        mSuspendTimer.setSingleShot(true);
541 542 543 544 545
    }
}

void AlarmDialog::show()
{
Laurent Montel's avatar
Laurent Montel committed
546 547 548 549 550 551 552 553 554 555 556 557 558 559
    mIncidenceTree->resizeColumnToContents(0);
    mIncidenceTree->resizeColumnToContents(1);
    mIncidenceTree->resizeColumnToContents(2);
    mIncidenceTree->sortItems(1, Qt::AscendingOrder);

    // select the first item that hasn't already been notified
    QTreeWidgetItemIterator it(mIncidenceTree);
    while (*it) {
        ReminderTreeItem *item = static_cast<ReminderTreeItem *>(*it);
        if (!item->mNotified) {
            (*it)->setSelected(true);
            break;
        }
        ++it;
560 561
    }

Laurent Montel's avatar
Laurent Montel committed
562
    // reset the default suspend time
563 564 565
// Allen: commented-out the following lines on 17 Sept 2013
//  mSuspendSpin->setValue( defSuspendVal );
//  mSuspendUnit->setCurrentIndex( defSuspendUnit );
566

Laurent Montel's avatar
Laurent Montel committed
567 568 569 570 571 572 573 574 575 576 577
    QDialog::show();
    if (!mPos.isNull()) {
        QDialog::move(mPos);
    }
    KWindowSystem::unminimizeWindow(winId(), false);
    KWindowSystem::setState(winId(), NET::KeepAbove | NET::DemandsAttention);
    KWindowSystem::setOnAllDesktops(winId(), true);
    KWindowSystem::activateWindow(winId());

    // Audio, Procedure, and EMail alarms
    eventNotification();
578 579 580 581
}

void AlarmDialog::suspendAll()
{
Laurent Montel's avatar
Laurent Montel committed
582 583
    mIncidenceTree->clearSelection();
    QTreeWidgetItemIterator it(mIncidenceTree);
584

Laurent Montel's avatar
Laurent Montel committed
585 586 587 588 589 590
    // first, select all non-suspended reminders
    while (*it) {
        if (!(*it)->isDisabled()) {   //do not suspend suspended reminders
            (*it)->setSelected(true);
        }
        ++it;
591 592
    }

Laurent Montel's avatar
Laurent Montel committed
593 594
    //suspend all selected reminders
    suspend();
595 596 597 598
}

void AlarmDialog::eventNotification()
{
Laurent Montel's avatar
Laurent Montel committed
599 600 601 602 603 604 605 606 607 608 609 610
    bool beeped = false;
    bool found = false;

    ReminderList list;

    QTreeWidgetItemIterator it(mIncidenceTree);
    while (*it) {
        ReminderTreeItem *item = static_cast<ReminderTreeItem *>(*it);
        ++it;
        if (item->isDisabled() || item->mNotified) {
            //skip suspended reminders or reminders that have been notified
            continue;
611
        }
Laurent Montel's avatar
Laurent Montel committed
612 613 614 615 616 617 618 619 620 621
        found = true;
        item->mNotified = true;
        Incidence::Ptr incidence = CalendarSupport::incidence(item->mIncidence);
        Alarm::List alarms = incidence->alarms();
        Alarm::List::ConstIterator ait;
        for (ait = alarms.constBegin(); ait != alarms.constEnd(); ++ait) {
            Alarm::Ptr alarm = *ait;
            // FIXME: Check whether this should be done for all multiple alarms
            if (alarm->type() == Alarm::Procedure) {
                // FIXME: Add a message box asking whether the procedure should really be executed
Laurent Montel's avatar
Laurent Montel committed
622
                qCDebug(KOALARMCLIENT_LOG) << "Starting program: '" << alarm->programFile() << "'";
Laurent Montel's avatar
Laurent Montel committed
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650

                QString program = alarm->programFile();

                // if the program name contains spaces escape it
                if (program.contains(QLatin1Char(' '))   &&
                        !(program.startsWith(QLatin1Char('\"')) &&
                          program.endsWith(QLatin1Char('\"')))) {
                    program = QLatin1Char('\"') + program + QLatin1Char('\"');
                }

                QProcess::startDetached(program + QLatin1Char(' ') + alarm->programArguments());
            } else if (alarm->type() == Alarm::Audio) {
                beeped = true;
                Phonon::MediaObject *player =
                    Phonon::createPlayer(Phonon::NotificationCategory,
                                         QUrl::fromLocalFile(alarm->audioFile()));
                player->setParent(this);
                connect(player, &Phonon::MediaObject::finished, player, &Phonon::MediaObject::deleteLater);
                player->play();
            } else if (alarm->type() == Alarm::Email) {
                QString from = CalendarSupport::KCalPrefs::instance()->email();
                Identity id = mIdentityManager->identityForAddress(from);
                QString to;
                if (alarm->mailAddresses().isEmpty()) {
                    to = from;
                } else {
                    const Person::List addresses = alarm->mailAddresses();
                    QStringList add;
651
                    add.reserve(addresses.count());
Laurent Montel's avatar
Laurent Montel committed
652 653 654 655
                    for (Person::List::ConstIterator it = addresses.constBegin();
                            it != addresses.constEnd(); ++it) {
                        add << (*it)->fullName();
                    }
Laurent Montel's avatar
Laurent Montel committed
656
                    to = add.join(QStringLiteral(", "));
Laurent Montel's avatar
Laurent Montel committed
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
                }

                QString subject;

                Akonadi::Item parentItem = mCalendar->item(alarm->parentUid());
                Incidence::Ptr parent = CalendarSupport::incidence(parentItem);

                if (alarm->mailSubject().isEmpty()) {
                    if (parent->summary().isEmpty()) {
                        subject = i18nc("@title", "Reminder");
                    } else {
                        subject = i18nc("@title", "Reminder: %1", cleanSummary(parent->summary()));
                    }
                } else {
                    subject = i18nc("@title", "Reminder: %1", alarm->mailSubject());
                }

                QString body =
                    IncidenceFormatter::mailBodyStr(
                        parent.staticCast<IncidenceBase>(), KSystemTimeZones::local());
                if (!alarm->mailText().isEmpty()) {
                    body += QLatin1Char('\n') + alarm->mailText();
                }
                //TODO: support attachments
                KOrg::MailClient mailer;
                mailer.send(id, from, to, QString(), subject, body, true, false, QString(),
                            MailTransport::TransportManager::self()->defaultTransportName());
            }
685 686 687
        }
    }

Laurent Montel's avatar
Laurent Montel committed
688 689 690
    if (!beeped && found) {
        KNotification::beep();
    }
691 692 693 694
}

void AlarmDialog::wakeUp()
{
Laurent Montel's avatar
Laurent Montel committed
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
    bool activeReminders = false;
    QTreeWidgetItemIterator it(mIncidenceTree);
    QTreeWidgetItem *firstItem = 0;
    while (*it) {
        if (!firstItem) {
            firstItem = *it;
        }
        ReminderTreeItem *item = static_cast<ReminderTreeItem *>(*it);
        Incidence::Ptr incidence = CalendarSupport::incidence(item->mIncidence);

        if (item->mRemindAt <= QDateTime::currentDateTime()) {
            if (item->isDisabled()) {   //do not wakeup non-suspended reminders
                item->setDisabled(false);
                item->setSelected(false);
            }
            activeReminders = true;
        } else {
            item->setDisabled(true);
        }
714

Laurent Montel's avatar
Laurent Montel committed
715 716
        ++it;
    }
717

Laurent Montel's avatar
Laurent Montel committed
718 719 720 721 722
    if (activeReminders) {
        show();
    }
    setTimer();
    showDetails(firstItem);
Laurent Montel's avatar
Laurent Montel committed
723
    Q_EMIT reminderCount(activeCount());
724 725 726 727
}

void AlarmDialog::slotSave()
{
Laurent Montel's avatar
Laurent Montel committed
728 729 730 731 732 733 734 735
    KSharedConfig::Ptr config = KSharedConfig::openConfig();
    KConfigGroup generalConfig(config, "General");
    int numReminders = 0;

    QTreeWidgetItemIterator it(mIncidenceTree);
    while (*it) {
        ReminderTreeItem *item = static_cast<ReminderTreeItem *>(*it);
        KConfigGroup incidenceConfig(config,
Laurent Montel's avatar
Laurent Montel committed
736
                                     QStringLiteral("Incidence-%1").arg(numReminders + 1));
Laurent Montel's avatar
Laurent Montel committed
737 738

        Incidence::Ptr incidence = CalendarSupport::incidence(item->mIncidence);
Laurent Montel's avatar
Laurent Montel committed
739
        incidenceConfig.writeEntry("AkonadiUrl", item->mIncidence.url());
Laurent Montel's avatar
Laurent Montel committed
740 741 742 743 744 745 746 747
        incidenceConfig.writeEntry("RemindAt", item->mRemindAt);
        ++numReminders;
        ++it;
    }

    generalConfig.writeEntry("Reminders", numReminders);
    generalConfig.writeEntry("Position", pos());
    config->sync();
748 749
}

Laurent Montel's avatar
Minor  
Laurent Montel committed
750
AlarmDialog::ReminderList AlarmDialog::selectedItems() const
751
{
Laurent Montel's avatar
Laurent Montel committed
752
    ReminderList list;
753

Laurent Montel's avatar
Laurent Montel committed
754 755 756 757 758 759
    QTreeWidgetItemIterator it(mIncidenceTree);
    while (*it) {
        if ((*it)->isSelected()) {
            list.append(static_cast<ReminderTreeItem *>(*it));
        }
        ++it;
760
    }
Laurent Montel's avatar
Laurent Montel committed
761
    return list;
762 763 764 765
}

int AlarmDialog::activeCount()
{
Laurent Montel's avatar
Laurent Montel committed
766 767 768 769 770 771 772
    int count = 0;
    QTreeWidgetItemIterator it(mIncidenceTree);
    while (*it) {
        if (!(*it)->isDisabled()) {   //suspended reminders are non-active
            ++count;
        }
        ++it;
773
    }
Laurent Montel's avatar
Laurent Montel committed
774
    qCDebug(KOALARMCLIENT_LOG) << "computed " << count << " active reminders";
Laurent Montel's avatar
Laurent Montel committed
775
    return count;
776 777
}

Laurent Montel's avatar
Laurent Montel committed
778
void AlarmDialog::closeEvent(QCloseEvent *)
779
{
Laurent Montel's avatar
Laurent Montel committed
780 781
    slotSave();
    accept();
782 783 784 785
}

void AlarmDialog::updateButtons()
{
Laurent Montel's avatar
Laurent Montel committed
786 787 788
    const ReminderList selection = selectedItems();
    const int count = selection.count();
    const bool enabled = (count > 0);
Laurent Montel's avatar
Laurent Montel committed
789
    qCDebug(KOALARMCLIENT_LOG) << "selected items=" << count;
Laurent Montel's avatar
Laurent Montel committed
790 791 792 793 794 795 796 797
    mUser3Button->setEnabled(enabled);
    mOkButton->setEnabled(enabled);
    if (count == 1) {
        ReminderTreeItem  *item = selection.first();
        mUser1Button->setEnabled(mCalendar->hasRight(item->mIncidence, Akonadi::Collection::CanChangeItem));
    } else {
        mUser1Button->setEnabled(false);
    }
798 799
}

Laurent Montel's avatar
Laurent Montel committed
800
void AlarmDialog::toggleDetails(QTreeWidgetItem *item)
801
{
Laurent Montel's avatar
Laurent Montel committed
802 803 804 805 806 807 808 809 810 811 812
    if (!item) {
        return;
    }

    if (!mDetailView->isHidden()) {
        if (mLastItem == item) {
            resize(size().width(), size().height() - mDetailView->height() - 50);
            mDetailView->hide();
        } else {
            showDetails(item);
        }
813
    } else {
Laurent Montel's avatar
Laurent Montel committed
814 815 816
        resize(size().width(), size().height() + mDetailView->height() + 50);
        showDetails(item);
        mDetailView->show();
817
    }
Laurent Montel's avatar
Laurent Montel committed
818
    mLastItem = item;
819 820
}

Laurent Montel's avatar
Laurent Montel committed
821
void AlarmDialog::showDetails(QTreeWidgetItem *item)
822
{
Laurent Montel's avatar
Laurent Montel committed
823 824 825 826 827 828 829 830
    if (!item) {
        return;
    }

    ReminderTreeItem *reminderItem = dynamic_cast<ReminderTreeItem *>(item);

    if (!reminderItem) {
        mDetailView->setIncidence(Akonadi::Item());
831
    } else {
Laurent Montel's avatar
Laurent Montel committed
832 833 834 835 836 837 838 839
        if (!reminderItem->mDisplayText.isEmpty()) {
            QString txt = QLatin1String("<qt><p><b>") + reminderItem->mDisplayText + QLatin1String("</b></p></qt>");
            mDetailView->setHeaderText(txt);
        } else {
            mDetailView->setHeaderText(QString());
        }
        Incidence::Ptr incidence = CalendarSupport::incidence(reminderItem->mIncidence);
        mDetailView->setIncidence(reminderItem->mIncidence, reminderItem->mRemindAt.date());
840 841 842 843 844
    }
}

void AlarmDialog::update()
{
Laurent Montel's avatar
Laurent Montel committed
845 846 847 848 849 850 851 852
    updateButtons();

    const ReminderList selection = selectedItems();
    if (!selection.isEmpty()) {
        ReminderTreeItem  *item = selection.first();
        mUser1Button->setEnabled((mCalendar->hasRight(item->mIncidence, Akonadi::Collection::CanChangeItem)) && (selection.count() == 1));
        toggleDetails(item);
    }
853 854 855 856
}

void AlarmDialog::accept()
{
Laurent Montel's avatar
Laurent Montel committed
857 858 859 860
    if (activeCount() == 0) {
        mPos = pos();
        hide();
    }
861 862
}

863
/** static */
Laurent Montel's avatar
Laurent Montel committed
864 865 866
KDateTime AlarmDialog::triggerDateForIncidence(const Incidence::Ptr &incidence,
        const QDateTime &reminderAt,
        QString &displayStr)
867
{
Laurent Montel's avatar
Laurent Montel committed
868
    KDateTime result;
869

Laurent Montel's avatar
Laurent Montel committed
870 871 872
    if (incidence->alarms().isEmpty()) {
        return result;
    }
873

874
    Alarm::Ptr alarm = incidence->alarms().at(0);
875

Laurent Montel's avatar
Laurent Montel committed
876 877 878
    if (incidence->recurs()) {
        result = incidence->recurrence()->getNextDateTime(
                     KDateTime(reminderAt, KDateTime::Spec::LocalZone()));
879

Laurent Montel's avatar
Laurent Montel committed
880 881
        displayStr = KLocale::global()->formatDateTime(result.toLocalZone());
    }
882

Laurent Montel's avatar
Laurent Montel committed
883 884 885 886 887 888
    if (!result.isValid()) {
        result = incidence->dateTime(Incidence::RoleAlarm);
        displayStr = IncidenceFormatter::dateTimeToString(result, false,
                     true,
                     KDateTime::Spec::LocalZone());
    }
889

Laurent Montel's avatar
Laurent Montel committed
890
    return result;
891 892
}

893 894
void AlarmDialog::slotCalendarChanged()
{
Laurent Montel's avatar
Laurent Montel committed
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
    KCalCore::Incidence::List incidences = mCalendar->incidences();
    Akonadi::Item::List items = mCalendar->itemList(incidences);
    for (Akonadi::Item::List::ConstIterator it = items.constBegin();
            it != items.constEnd(); ++it) {
        ReminderTreeItem *item = searchByItem(*it);

        if (item) {
            Incidence::Ptr incidence = CalendarSupport::incidence(*it);
            QString displayStr;

            // Yes, alarms can be empty, if someone edited the incidence and removed all alarms
            if (!incidence->alarms().isEmpty()) {
                const KDateTime dateTime = triggerDateForIncidence(incidence,
                                           item->mRemindAt,
                                           displayStr);

                const QString summary = cleanSummary(incidence->summary());

                if (displayStr != item->text(1) || summary != item->text(0)) {
                    item->setText(1, displayStr);
                    item->setText(0, summary);
                }
            }
918
        }
919 920
    }
}
921

Laurent Montel's avatar
Laurent Montel committed
922
void AlarmDialog::keyPressEvent(QKeyEvent *e)
923
{
Laurent Montel's avatar
Laurent Montel committed
924
    const int key = e->key() | e->modifiers();
925

Laurent Montel's avatar
Laurent Montel committed
926 927 928 929
    if (key == Qt::Key_Enter || key == Qt::Key_Return) {
        e->ignore();
        return;
    }
930

Laurent Montel's avatar
Laurent Montel committed
931
    QDialog::keyPressEvent(e);
932 933
}

Laurent Montel's avatar
Laurent Montel committed
934
bool AlarmDialog::openIncidenceEditorThroughKOrganizer(const Incidence::Ptr &incidence)
935
{
Laurent Montel's avatar
Laurent Montel committed
936 937
    if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(QStringLiteral("org.kde.korganizer"))) {
        if (KToolInvocation::startServiceByDesktopName(QStringLiteral("korganizer"), QString())) {
Laurent Montel's avatar
Laurent Montel committed
938 939 940 941 942 943 944 945
            KMessageBox::error(
                this,
                i18nc("@info",
                      "Could not start KOrganizer so editing is not possible."));
            return false;
        }
    }
    org::kde::korganizer::Korganizer korganizer(
Laurent Montel's avatar
Laurent Montel committed
946
        QStringLiteral("org.kde.korganizer"), QStringLiteral("/Korganizer"), QDBusConnection::sessionBus());
Laurent Montel's avatar
Laurent Montel committed
947

Laurent Montel's avatar
Laurent Montel committed
948
    qCDebug(KOALARMCLIENT_LOG) << "editing incidence " << incidence->summary();
Laurent Montel's avatar
Laurent Montel committed
949 950 951 952 953 954
    if (!korganizer.editIncidence(incidence->uid())) {
        KMessageBox::error(
            this,
            i18nc("@info",
                  "An internal KOrganizer error occurred attempting to modify \"%1\"",
                  cleanSummary(incidence->summary())));
955
    }
Laurent Montel's avatar
Laurent Montel committed
956 957 958

    // get desktop # where korganizer (or kontact) runs
    QString object =
Laurent Montel's avatar
Laurent Montel committed
959 960 961
        QDBusConnection::sessionBus().interface()->isServiceRegistered(QStringLiteral("org.kde.kontact")) ?
        QStringLiteral("kontact/MainWindow_1") : QStringLiteral("korganizer/MainWindow_1");
    QDBusInterface korganizerObj(QStringLiteral("org.kde.korganizer"), QLatin1Char('/') + object);
Laurent Montel's avatar
Laurent Montel committed
962
#if KDEPIM_HAVE_X11
Laurent Montel's avatar
Laurent Montel committed
963
    QDBusReply<int> reply = korganizerObj.call(QStringLiteral("winId"));
Laurent Montel's avatar
Laurent Montel committed
964 965
    if (reply.isValid()) {
        int window = reply;
966 967
        auto winInfo = KWindowInfo(window, NET::WMDesktop);
        int desktop = winInfo.desktop();
Laurent Montel's avatar
Laurent Montel committed
968 969 970 971 972
        if (KWindowSystem::currentDesktop() == desktop) {
            KWindowSystem::minimizeWindow(winId(), false);
        } else {
            KWindowSystem::setCurrentDesktop(desktop);
        }
973
        KWindowSystem::activateWindow(winInfo.transientFor());
974
    }
Laurent Montel's avatar
Laurent Montel committed
975
#elif defined(Q_OS_WIN)
Laurent Montel's avatar
Laurent Montel committed
976
    // WId is a typedef to a void* on windows
Laurent Montel's avatar
Laurent Montel committed
977
    QDBusReply<qlonglong> reply = korganizerObj.call(QStringLiteral("winId"));
Laurent Montel's avatar
Laurent Montel committed
978 979 980 981 982 983
    if (reply.isValid()) {
        qlonglong window = reply;
        KWindowSystem::minimizeWindow(winId(), false);
        KWindowSystem::allowExternalProcessWindowActivation();
        KWindowSystem::activateWindow(reinterpret_cast<WId>(window));
    }
984
#else
Laurent Montel's avatar
Laurent Montel committed
985
    // TODO (mac)
986
#endif
Laurent Montel's avatar
Laurent Montel committed
987
    return true;
988 989
}

Laurent Montel's avatar
Laurent Montel committed
990
bool AlarmDialog::openIncidenceEditorNG(const Akonadi::Item &item)
991
{
Laurent Montel's avatar
Laurent Montel committed
992 993 994 995 996 997 998
    Incidence::Ptr incidence = CalendarSupport::incidence(item);
    IncidenceEditorNG::IncidenceDialog *dialog =
        IncidenceEditorNG::IncidenceDialogFactory::create(
            false, /*doesn't need initial saving*/
            incidence->type(), 0, this);
    dialog->load(item);
    return true;
999 1000
}

Laurent Montel's avatar
Laurent Montel committed
1001
void AlarmDialog::removeFromConfig(const QList<Akonadi::Item::Id> &ids)
1002
{
Laurent Montel's avatar
Laurent Montel committed
1003 1004 1005 1006 1007
    KSharedConfig::Ptr config = KSharedConfig::openConfig();
    KConfigGroup genGroup(config, "General");

    const int oldNumReminders = genGroup.readEntry("Reminders", 0);

Laurent Montel's avatar
Laurent Montel committed
1008
    QVector<ConfItem> newReminders;
Laurent Montel's avatar
Laurent Montel committed
1009 1010
    // Delete everything
    for (int i = 1; i <= oldNumReminders; ++i) {
Laurent Montel's avatar
Laurent Montel committed
1011
        const QString group(QStringLiteral("Incidence-%1").arg(i));
Laurent Montel's avatar
Laurent Montel committed
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
        KConfigGroup incGroup(config, group);
        const QString uid = incGroup.readEntry("UID");
        const QDateTime remindAtDate = incGroup.readEntry("RemindAt", QDateTime());
        const QUrl akonadiUrl = incGroup.readEntry("AkonadiUrl");
        const Akonadi::Item::Id id = Akonadi::Item::fromUrl(akonadiUrl).id();
        if (!ids.contains(id)) {
            ConfItem ci;
            ci.akonadiUrl = akonadiUrl;
            ci.remindAt = remindAtDate;
            ci.uid = uid;
            newReminders.append(ci);
        }
        config->deleteGroup(group);
    }

Laurent Montel's avatar
Laurent Montel committed
1027 1028
    const int newRemindersCount(newReminders.count());
    genGroup.writeEntry("Reminders", newRemindersCount);
Laurent Montel's avatar
Laurent Montel committed
1029 1030

    //Write everything except those which have an uid we don't want
Laurent Montel's avatar
Laurent Montel committed
1031
    for (int i = 0; i < newRemindersCount; ++i) {
Laurent Montel's avatar
Laurent Montel committed
1032
        const QString group(QStringLiteral("Incidence-%1").arg(i + 1));
Laurent Montel's avatar
Laurent Montel committed
1033
        KConfigGroup incGroup(config, group);
Laurent Montel's avatar
Laurent Montel committed
1034 1035 1036 1037
        const ConfItem conf = newReminders.at(i);
        incGroup.writeEntry("UID", conf.uid);
        incGroup.writeEntry("RemindAt", conf.remindAt);
        incGroup.writeEntry("AkonadiUrl", conf.akonadiUrl);
Laurent Montel's avatar
Laurent Montel committed
1038
        incGroup.sync();
1039
    }
Laurent Montel's avatar
Laurent Montel committed
1040
    genGroup.sync();
1041 1042
}