File indexing completed on 2024-05-12 05:21:40

0001 /*
0002  * Copyright (C) 2003 by Mark Bucciarelli <mark@hubcapconsutling.com>
0003  * Copyright (C) 2019  Alexander Potashev <aspotashev@gmail.com>
0004  *
0005  *   This program is free software; you can redistribute it and/or modify
0006  *   it under the terms of the GNU General Public License as published by
0007  *   the Free Software Foundation; either version 2 of the License, or
0008  *   (at your option) any later version.
0009  *
0010  *   This program is distributed in the hope that it will be useful,
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  *   GNU General Public License for more details.
0014  *
0015  *   You should have received a copy of the GNU General Public License along
0016  *   with this program; if not, write to the
0017  *      Free Software Foundation, Inc.
0018  *      51 Franklin Street, Fifth Floor
0019  *      Boston, MA  02110-1301  USA.
0020  *
0021  */
0022 
0023 #ifndef KTIMETRACKER_STORAGE_H
0024 #define KTIMETRACKER_STORAGE_H
0025 
0026 #include <KCalendarCore/Event>
0027 #include <KCalendarCore/Todo>
0028 
0029 #include "desktoplist.h"
0030 #include "file/filecalendar.h"
0031 #include "reportcriteria.h"
0032 
0033 QT_BEGIN_NAMESPACE
0034 class QDateTime;
0035 class QLockFile;
0036 QT_END_NAMESPACE
0037 
0038 class ProjectModel;
0039 class Task;
0040 class TaskView;
0041 class TasksModel;
0042 class EventsModel;
0043 
0044 /**
0045  * Class to store/retrieve KTimeTracker data to/from persistent storage.
0046  *
0047  * The storage is an iCalendar file. Its name is contained in this class
0048  * in the variable _icalfile and can be read using the function icalfile().
0049  * The name gets set by the load() operation.
0050  *
0051  * All logic that deals with getting and saving data should go here.
0052  *
0053  * This program uses iCalendar to store its data. There are tasks and
0054  * events. Events have a start and a end date and an associated task.
0055  *
0056  * @short Logic that gets and stores KTimeTracker data to disk.
0057  * @author Mark Bucciarelli <mark@hubcapconsulting.com>
0058  */
0059 
0060 class TimeTrackerStorage : public QObject
0061 {
0062     Q_OBJECT
0063 
0064 public:
0065     TimeTrackerStorage();
0066 
0067     ~TimeTrackerStorage() override = default;
0068 
0069     /**
0070       Load the list view with tasks read from iCalendar file.
0071 
0072       Parses iCalendar file, builds list view items in the proper
0073       hierarchy, and loads them into the list view widget.
0074 
0075       If the file name passed in is the same as the last file name that was
0076       loaded, this method does nothing.
0077 
0078       This method considers any of the following conditions errors:
0079 
0080          @li the iCalendar file does not exist
0081          @li the iCalendar file is not readable
0082          @li the list group currently has list items
0083          @li an iCalendar todo has no related to attribute
0084          @li a todo is related to another todo which does not exist
0085 
0086       @param taskview The list group used in the TaskView. Must not be nullptr.
0087       @param url      Override preferences' filename
0088 
0089       @return empty string if success, error message if error.
0090      */
0091     QString load(TaskView *taskview, const QUrl &url);
0092 
0093     /**
0094      * Return the name of the iCal file
0095      */
0096     QUrl fileUrl();
0097 
0098     /**
0099      * Load tasks from calendar's todos.
0100      *
0101      * This is needed if the iCal file has been modified.
0102      */
0103     QString loadTasksFromCalendar(const KCalendarCore::Todo::List &todos);
0104 
0105     /** Close calendar and clear view.  Release lock if holding one. */
0106     void closeStorage();
0107 
0108     bool isLoaded() const { return m_model; }
0109 
0110     /** list of all events */
0111     EventsModel *eventsModel();
0112 
0113     TasksModel *tasksModel();
0114 
0115     ProjectModel *projectModel();
0116 
0117     /**
0118      * Deliver if all events of a task have an endtime
0119      *
0120      * If ktimetracker has been quit with one task running, it needs to resumeRunning().
0121      * This function delivers if an enddate of an event has not yet been stored.
0122      *
0123      * @param task        The task to be examined
0124      */
0125     bool allEventsHaveEndTime(Task *task);
0126 
0127     /**
0128      * Save all tasks and their totals to an iCalendar file.
0129      *
0130      * All tasks must have an associated VTODO object already created in the
0131      * calendar file; that is, the task->uid() must refer to a valid VTODO in
0132      * the calendar.
0133      * Delivers empty string if successful, else error msg.
0134      *
0135      * @return Null string on success. On failure, returns human-readable error message to display in a KMessageBox.
0136      */
0137     QString save();
0138 
0139     bool bookTime(const Task *task, const QDateTime &startDateTime, int64_t durationInSeconds);
0140 
0141     static QString createLockFileName(const QUrl &url);
0142 
0143 private Q_SLOTS:
0144     void onFileModified();
0145 
0146 private:
0147     ProjectModel *m_model;
0148     QUrl m_url;
0149     TaskView *m_taskView;
0150 };
0151 
0152 #endif // KTIMETRACKER_STORAGE_H