diff --git a/src/model/eventsmodel.cpp b/src/model/eventsmodel.cpp index 3605c8a..0d08864 100644 --- a/src/model/eventsmodel.cpp +++ b/src/model/eventsmodel.cpp @@ -1,172 +1,172 @@ /* * Copyright (c) 2019 Alexander Potashev * * 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) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * 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, see . */ #include "eventsmodel.h" #include #include "task.h" #include "ktt_debug.h" EventsModel::EventsModel() : m_events() { } void EventsModel::load(const KCalendarCore::Event::List &events) { clear(); for (const auto& event : events) { m_events.append(new Event(event)); } } EventsModel::~EventsModel() { clear(); } QList EventsModel::events() const { return m_events; } -QList EventsModel::eventsForTask(Task *task) const +QList EventsModel::eventsForTask(const Task *task) const { QList res; for (auto *event : events()) { if (event->relatedTo() == task->uid()) { res.append(event); } } return res; } Event *EventsModel::eventByUID(const QString &uid) const { for (auto *event : events()) { if (event->uid() == uid) { return event; } } return nullptr; } void EventsModel::clear() { for (auto *event : m_events) { delete event; } m_events.clear(); } void EventsModel::removeAllForTask(const Task *task) { for (auto it = m_events.begin(); it != m_events.end();) { Event *event = *it; if (event->relatedTo() == task->uid()) { delete event; it = m_events.erase(it); } else { ++it; } } } void EventsModel::removeByUID(const QString &uid) { for (auto it = m_events.begin(); it != m_events.end(); ++it) { Event *event = *it; if (event->uid() == uid) { delete event; m_events.erase(it); return; } } } void EventsModel::addEvent(Event *event) { m_events.append(event); } static KCalendarCore::Event::Ptr baseEvent(const Task *task) { qCDebug(KTT_LOG) << "Entering function"; KCalendarCore::Event::Ptr e(new KCalendarCore::Event()); QStringList categories; e->setSummary(task->name()); // Can't use setRelatedToUid()--no error, but no RelatedTo written to disk e->setRelatedTo( task->uid() ); // Debugging: some events where not getting a related-to field written. Q_ASSERT(e->relatedTo() == task->uid()); // Have to turn this off to get datetimes in date fields. e->setAllDay(false); // e->setDtStart(KDateTime(task->startTime(), KDateTime::Spec::LocalZone())); e->setDtStart(task->startTime()); // So someone can filter this mess out of their calendar display categories.append(i18n("KTimeTracker")); e->setCategories(categories); return e; } void EventsModel::changeTime(const Task* task, long deltaSeconds) { qCDebug(KTT_LOG) << "Entering function; deltaSeconds=" << deltaSeconds; QDateTime end; auto kcalEvent = baseEvent(task); auto *e = new Event(kcalEvent); // Don't use duration, as ICalFormatImpl::writeIncidence never writes a // duration, even though it looks like it's used in event.cpp. end = task->startTime(); if ( deltaSeconds > 0 ) end = task->startTime().addSecs(deltaSeconds); e->setDtEnd(end); // Use a custom property to keep a record of negative durations e->setDuration(deltaSeconds); addEvent(e); } bool EventsModel::bookTime(const Task* task, const QDateTime& startDateTime, long durationInSeconds) { qCDebug(KTT_LOG) << "Entering function"; auto kcalEvent = baseEvent(task); auto *e = new Event(kcalEvent); e->setDtStart(startDateTime); e->setDtEnd(startDateTime.addSecs( durationInSeconds)); // Use a custom property to keep a record of negative durations e->setDuration(durationInSeconds); addEvent(e); return true; } diff --git a/src/model/eventsmodel.h b/src/model/eventsmodel.h index bbd1739..0ee84d7 100644 --- a/src/model/eventsmodel.h +++ b/src/model/eventsmodel.h @@ -1,103 +1,103 @@ /* * Copyright (c) 2019 Alexander Potashev * * 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) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * 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, see . */ #ifndef KTIMETRACKER_EVENTSMODEL_H #define KTIMETRACKER_EVENTSMODEL_H #include #include "event.h" class Task; class EventsModel : public QObject { Q_OBJECT public: EventsModel(); ~EventsModel() override; void load(const KCalendarCore::Event::List &events); QList events() const; - QList eventsForTask(Task *task) const; + QList eventsForTask(const Task *task) const; Event *eventByUID(const QString &uid) const; // Delete all events void clear(); void removeAllForTask(const Task *task); void removeByUID(const QString &uid); void addEvent(Event *event); /** * Log the change in a task's time. * * This is also called when a timer is stopped. * We create an iCalendar event to store each change. The event start * date is set to the current datetime. If time is added to the task, the * task end date is set to start time + delta. If the time is negative, * the end date is set to the start time. * * In both cases (postive or negative delta), we create a custom iCalendar * property that stores the delta (in seconds). This property is called * X-KDE-ktimetracker-duration. * * Note that the ktimetracker UI allows the user to change both the session and * the total task time, and this routine does not account for all posibile * cases. For example, it is possible for the user to do something crazy * like add 10 minutes to the session time and subtract 50 minutes from * the total time. Although this change violates a basic law of physics, * it is allowed. * * For now, you should pass in the change to the total task time. * * @param task The task the change is for. * @param delta Change in task time, in seconds. Can be negative. */ void changeTime(const Task* task, long deltaSeconds); /** * Book time to a task. * * Creates an iCalendar event and adds it to the calendar. Does not write * calendar to disk, just adds event to calendar in memory. However, the * resource framework does try to get a lock on the file. After a * successful lock, the calendar marks this incidence as modified and then * releases the lock. * * @param task Task * @param startDateTime Date and time the booking starts. * @param durationInSeconds Duration of time to book, in seconds. * * @return true if event was added, false if not (if, for example, the * attempted file lock failed). */ bool bookTime(const Task *task, const QDateTime &startDateTime, long durationInSeconds); Q_SIGNALS: void timesChanged(); private: QList m_events; }; #endif // KTIMETRACKER_EVENTSMODEL_H