File indexing completed on 2024-11-17 04:49:33

0001 /*
0002  * Copyright (c) 2019 Alexander Potashev <aspotashev@gmail.com>
0003  *
0004  * This program is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU General Public License as
0006  * published by the Free Software Foundation; either version 2 of
0007  * the License or (at your option) version 3 or any later version
0008  * accepted by the membership of KDE e.V. (or its successor approved
0009  * by the membership of KDE e.V.), which shall act as a proxy
0010  * defined in Section 14 of version 3 of the license.
0011  *
0012  * This program is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015  * GNU General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU General Public License
0018  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0019  */
0020 
0021 #ifndef KTIMETRACKER_EVENTSMODEL_H
0022 #define KTIMETRACKER_EVENTSMODEL_H
0023 
0024 #include <KCalendarCore/Calendar>
0025 
0026 #include "event.h"
0027 
0028 class Task;
0029 
0030 class EventsModel : public QObject
0031 {
0032     Q_OBJECT
0033 
0034 public:
0035     EventsModel() = default;
0036     ~EventsModel() override;
0037 
0038     void load(const KCalendarCore::Event::List &events);
0039     QList<Event *> events() const;
0040     QList<Event *> eventsForTask(const Task *task) const;
0041     Event *eventByUID(const QString &uid) const;
0042 
0043     // Delete all events
0044     void clear();
0045 
0046     void removeAllForTask(const Task *task);
0047     void removeByUID(const QString &uid);
0048 
0049     void addEvent(Event *event);
0050 
0051     /**
0052      * Log the change in a task's time.
0053      *
0054      * This is also called when a timer is stopped.
0055      * We create an iCalendar event to store each change. The event start
0056      * date is set to the current datetime. If time is added to the task, the
0057      * task end date is set to start time + delta. If the time is negative,
0058      * the end date is set to the start time.
0059      *
0060      * In both cases (positive or negative delta), we create a custom iCalendar
0061      * property that stores the delta (in seconds). This property is called
0062      * X-KDE-ktimetracker-duration.
0063      *
0064      * Note that the ktimetracker UI allows the user to change both the session and
0065      * the total task time, and this routine does not account for all possible
0066      * cases. For example, it is possible for the user to do something crazy
0067      * like add 10 minutes to the session time and subtract 50 minutes from
0068      * the total time. Although this change violates a basic law of physics,
0069      * it is allowed.
0070      *
0071      * For now, you should pass in the change to the total task time.
0072      *
0073      * @param task   The task the change is for.
0074      * @param delta  Change in task time, in seconds.  Can be negative.
0075      */
0076     void changeTime(const Task *task, int64_t deltaSeconds);
0077 
0078     /**
0079      * Book time to a task.
0080      *
0081      * Creates an iCalendar event and adds it to the calendar. Does not write
0082      * calendar to disk, just adds event to calendar in memory. However, the
0083      * resource framework does try to get a lock on the file. After a
0084      * successful lock, the calendar marks this incidence as modified and then
0085      * releases the lock.
0086      *
0087      * @param task Task
0088      * @param startDateTime Date and time the booking starts.
0089      * @param durationInSeconds Duration of time to book, in seconds.
0090      *
0091      * @return true if event was added, false if not (if, for example, the
0092      * attempted file lock failed).
0093      */
0094     bool bookTime(const Task *task, const QDateTime &startDateTime, int64_t durationInSeconds);
0095 
0096     void startTask(const Task *task);
0097     void stopTask(const Task *task, const QDateTime &when);
0098 
0099 Q_SIGNALS:
0100     void timesChanged();
0101 
0102 private:
0103     QList<Event *> m_events;
0104 };
0105 
0106 #endif // KTIMETRACKER_EVENTSMODEL_H