whatsnextview.cpp 12.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
/*
  This file is part of KOrganizer.

  Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>

  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.
*/

#include "whatsnextview.h"

#include <Akonadi/Calendar/ETMCalendar>
#include <CalendarSupport/KCalPrefs>
#include <CalendarSupport/Utils>

#include <KCalUtils/IncidenceFormatter>

#include <KIconLoader>

#include <QBoxLayout>
#include <KLocale>
using namespace EventViews;

void WhatsNextTextBrowser::setSource(const QUrl &name)
{
    QString uri = name.toString();
    if (uri.startsWith(QStringLiteral("event:"))) {
        Q_EMIT showIncidence(uri);
    } else if (uri.startsWith(QStringLiteral("todo:"))) {
        Q_EMIT showIncidence(uri);
    } else {
        QTextBrowser::setSource(QUrl(uri));
    }
}

WhatsNextView::WhatsNextView(QWidget *parent)
    : EventView(parent)
{
    mView = new WhatsNextTextBrowser(this);
    connect(mView, &WhatsNextTextBrowser::showIncidence, this, &WhatsNextView::showIncidence);

    QBoxLayout *topLayout = new QVBoxLayout(this);
Laurent Montel's avatar
Laurent Montel committed
58
    topLayout->setMargin(0);
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    topLayout->addWidget(mView);
}

WhatsNextView::~WhatsNextView()
{
}

int WhatsNextView::currentDateCount() const
{
    return mStartDate.daysTo(mEndDate);
}

void WhatsNextView::createTaskRow(KIconLoader *kil)
{
    QString ipath;
    kil->loadIcon(QStringLiteral("view-calendar-tasks"), KIconLoader::NoGroup, 22,
                  KIconLoader::DefaultState, QStringList(), &ipath);
    mText += QLatin1String("<h2><img src=\"");
    mText += ipath;
78
    mText += QLatin1String("\" width=\"22\" height=\"22\">");
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    mText += i18n("To-dos:") + QLatin1String("</h2>\n");
    mText += QLatin1String("<ul>\n");
}

void WhatsNextView::updateView()
{
    KIconLoader *kil = KIconLoader::global();
    QString ipath;
    kil->loadIcon(QStringLiteral("office-calendar"), KIconLoader::NoGroup, 32,
                  KIconLoader::DefaultState, QStringList(), &ipath);

    mText = QStringLiteral("<table width=\"100%\">\n");
    mText += QLatin1String("<tr bgcolor=\"#3679AD\"><td><h1>");
    mText += QLatin1String("<img src=\"");
    mText += ipath;
94
    mText += QLatin1String("\" width=\"32\" height=\"32\">");
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    mText += QLatin1String("<font color=\"white\"> ");
    mText += i18n("What's Next?") + QLatin1String("</font></h1>");
    mText += QLatin1String("</td></tr>\n<tr><td>");

    mText += QLatin1String("<h2>");
    if (mStartDate.daysTo(mEndDate) < 1) {
        mText += QLocale::system().toString(mStartDate);
    } else {
        mText += i18nc(
                     "date from - to", "%1 - %2",
                     QLocale::system().toString(mStartDate),
                     QLocale::system().toString(mEndDate));
    }
    mText += QLatin1String("</h2>\n");

    KCalCore::Event::List events;
    KDateTime::Spec timeSpec = CalendarSupport::KCalPrefs::instance()->timeSpec();

    events = calendar()->events(mStartDate, mEndDate, timeSpec, false);
    events = calendar()->sortEvents(events, KCalCore::EventSortStartDate,
                                    KCalCore::SortDirectionAscending);

    if (events.count() > 0) {
        mText += QLatin1String("<p></p>");
        kil->loadIcon(QStringLiteral("view-calendar-day"), KIconLoader::NoGroup, 22,
                      KIconLoader::DefaultState, QStringList(), &ipath);
        mText += QLatin1String("<h2><img src=\"");
        mText += ipath;
Laurent Montel's avatar
Laurent Montel committed
123
        mText += QLatin1String("\" width=\"22\" height=\"22\">");
124 125 126 127 128 129 130 131 132 133 134 135
        mText += i18n("Events:") + QLatin1String("</h2>\n");
        mText += QLatin1String("<table>\n");
        Q_FOREACH (const KCalCore::Event::Ptr &ev, events) {
            if (!ev->recurs()) {
                appendEvent(ev);
            } else {
                KCalCore::Recurrence *recur = ev->recurrence();
                int duration = ev->dtStart().secsTo(ev->dtEnd());
                KDateTime start = recur->getPreviousDateTime(KDateTime(mStartDate, QTime(), timeSpec));
                KDateTime end = start.addSecs(duration);
                KDateTime endDate(mEndDate, QTime(23, 59, 59), timeSpec);
                if (end.date() >= mStartDate) {
136
                    appendEvent(ev, start.toLocalZone().dateTime(), end.toLocalZone().dateTime());
137 138 139 140 141 142 143 144 145 146 147 148
                }
                KCalCore::DateTimeList times = recur->timesInInterval(start, endDate);
                int count = times.count();
                if (count > 0) {
                    int i = 0;
                    if (times[0] == start) {
                        ++i;  // start has already been appended
                    }
                    if (!times[count - 1].isValid()) {
                        --count;  // list overflow
                    }
                    for (;  i < count && times[i].date() <= mEndDate;  ++i) {
149
                        appendEvent(ev, times[i].toLocalZone().dateTime());
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 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
                    }
                }
            }
        }
        mText += QLatin1String("</table>\n");
    }

    mTodos.clear();
    KCalCore::Todo::List todos = calendar()->todos(KCalCore::TodoSortDueDate,
                                 KCalCore::SortDirectionAscending);
    if (todos.count() > 0) {
        bool taskHeaderWasCreated = false;
        Q_FOREACH (const KCalCore::Todo::Ptr &todo, todos) {
            if (!todo->isCompleted() && todo->hasDueDate() && todo->dtDue().date() <= mEndDate) {
                if (!taskHeaderWasCreated) {
                    createTaskRow(kil);
                    taskHeaderWasCreated = true;
                }
                appendTodo(todo);
            }
        }
        bool gotone = false;
        int priority = 1;
        while (!gotone && priority <= 9) {
            Q_FOREACH (const KCalCore::Todo::Ptr &todo, todos) {
                if (!todo->isCompleted() && (todo->priority() == priority)) {
                    if (!taskHeaderWasCreated) {
                        createTaskRow(kil);
                        taskHeaderWasCreated = true;
                    }
                    appendTodo(todo);
                    gotone = true;
                }
            }
            priority++;
        }
        if (taskHeaderWasCreated) {
            mText += QLatin1String("</ul>\n");
        }
    }

    QStringList myEmails(CalendarSupport::KCalPrefs::instance()->allEmails());
    int replies = 0;
    events = calendar()->events(QDate::currentDate(), QDate(2975, 12, 6), timeSpec);
    Q_FOREACH (const KCalCore::Event::Ptr &ev, events) {
        KCalCore::Attendee::Ptr me = ev->attendeeByMails(myEmails);
Laurent Montel's avatar
Laurent Montel committed
196
        if (me != nullptr) {
197 198 199 200 201 202 203
            if (me->status() == KCalCore::Attendee::NeedsAction && me->RSVP()) {
                if (replies == 0) {
                    mText += QLatin1String("<p></p>");
                    kil->loadIcon(QStringLiteral("mail-reply-sender"), KIconLoader::NoGroup, 22,
                                  KIconLoader::DefaultState, QStringList(), &ipath);
                    mText += QLatin1String("<h2><img src=\"");
                    mText += ipath;
Laurent Montel's avatar
Laurent Montel committed
204
                    mText += QLatin1String("\" width=\"22\" height=\"22\">");
205 206 207 208 209 210 211 212 213 214 215
                    mText += i18n("Events and to-dos that need a reply:") + QLatin1String("</h2>\n");
                    mText += QLatin1String("<table>\n");
                }
                replies++;
                appendEvent(ev);
            }
        }
    }
    todos = calendar()->todos();
    Q_FOREACH (const KCalCore::Todo::Ptr &to, todos) {
        KCalCore::Attendee::Ptr me = to->attendeeByMails(myEmails);
Laurent Montel's avatar
Laurent Montel committed
216
        if (me != nullptr) {
217 218 219 220 221 222 223
            if (me->status() == KCalCore::Attendee::NeedsAction && me->RSVP()) {
                if (replies == 0) {
                    mText += QLatin1String("<p></p>");
                    kil->loadIcon(QStringLiteral("mail-reply-sender"), KIconLoader::NoGroup, 22,
                                  KIconLoader::DefaultState, QStringList(), &ipath);
                    mText += QLatin1String("<h2><img src=\"");
                    mText += ipath;
Laurent Montel's avatar
Laurent Montel committed
224
                    mText += QLatin1String("\" width=\"22\" height=\"22\">");
225 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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
                    mText += i18n("Events and to-dos that need a reply:") + QLatin1String("</h2>\n");
                    mText += QLatin1String("<table>\n");
                }
                replies++;
                appendEvent(to);
            }
        }
    }
    if (replies > 0) {
        mText += QLatin1String("</table>\n");
    }

    mText += QLatin1String("</td></tr>\n</table>\n");

    mView->setText(mText);

}

void WhatsNextView::showDates(const QDate &start, const QDate &end, const QDate &)
{
    mStartDate = start;
    mEndDate = end;
    updateView();
}

void WhatsNextView::showIncidences(const Akonadi::Item::List &incidenceList, const QDate &date)
{
    Q_UNUSED(incidenceList);
    Q_UNUSED(date);
}

void WhatsNextView::changeIncidenceDisplay(const Akonadi::Item &,
        Akonadi::IncidenceChanger::ChangeType)
{
    updateView();
}

void WhatsNextView::appendEvent(const KCalCore::Incidence::Ptr &incidence, const QDateTime &start,
                                const QDateTime &end)
{
    mText += QLatin1String("<tr><td><b>");
    if (const KCalCore::Event::Ptr event = incidence.dynamicCast<KCalCore::Event>()) {
        KDateTime::Spec timeSpec = CalendarSupport::KCalPrefs::instance()->timeSpec();
        KDateTime starttime(start, timeSpec);
        if (!starttime.isValid()) {
            starttime = event->dtStart();
        }
        KDateTime endtime(end, timeSpec);
        if (!endtime.isValid()) {
            endtime = starttime.addSecs(event->dtStart().secsTo(event->dtEnd()));
        }

        if (starttime.date().daysTo(endtime.date()) >= 1) {
            mText += i18nc(
                         "date from - to", "%1 - %2",
                         KLocale::global()->formatDateTime(
                             starttime.toTimeSpec(CalendarSupport::KCalPrefs::instance()->timeSpec())),
                         KLocale::global()->formatDateTime(
                             endtime.toTimeSpec(CalendarSupport::KCalPrefs::instance()->timeSpec())));
        } else {
            mText += i18nc(
                         "date, from - to", "%1, %2 - %3",
                         QLocale::system().toString(
                             starttime.toTimeSpec(CalendarSupport::KCalPrefs::instance()->timeSpec()).date(),
                             QLocale::ShortFormat),
                         QLocale::system().toString(
                             starttime.toTimeSpec(CalendarSupport::KCalPrefs::instance()->timeSpec()).time(), QLocale::ShortFormat),
                         QLocale::system().toString(
                             endtime.toTimeSpec(CalendarSupport::KCalPrefs::instance()->timeSpec()).time(), QLocale::ShortFormat));
        }
    }
    mText += QLatin1String("</b></td><td><a ");
    if (incidence->type() == KCalCore::Incidence::TypeEvent) {
        mText += QLatin1String("href=\"event:");
    }
    if (incidence->type() == KCalCore::Incidence::TypeTodo) {
        mText += QLatin1String("href=\"todo:");
    }
    mText += incidence->uid() + QLatin1String("\">");
    mText += incidence->summary();
    mText += QLatin1String("</a></td></tr>\n");
}

void WhatsNextView::appendTodo(const KCalCore::Incidence::Ptr &incidence)
{
    Akonadi::Item aitem = calendar()->item(incidence);
    if (mTodos.contains(aitem)) {
        return;
    }
    mTodos.append(aitem);
    mText += QLatin1String("<li><a href=\"todo:") + incidence->uid() + QLatin1String("\">");
    mText += incidence->summary();
    mText += QLatin1String("</a>");

    if (const KCalCore::Todo::Ptr todo = CalendarSupport::todo(aitem)) {
        if (todo->hasDueDate()) {
            mText += i18nc("to-do due date", "  (Due: %1)",
                           KCalUtils::IncidenceFormatter::dateTimeToString(
                               todo->dtDue(), todo->allDay()));
        }
        mText += QLatin1String("</li>\n");
    }
}

void WhatsNextView::showIncidence(const QString &uid)
{
    Akonadi::Item item;

    Akonadi::ETMCalendar::Ptr cal = calendar();
    if (!cal) {
        return;
    }

    if (uid.startsWith(QStringLiteral("event:"))) {
        item = cal->item(uid.mid(6));
    } else if (uid.startsWith(QStringLiteral("todo:"))) {
        item = cal->item(uid.mid(5));
    }

    if (item.isValid()) {
        Q_EMIT showIncidenceSignal(item);
    }
}