File indexing completed on 2024-11-24 04:48:39

0001 /*
0002  * Copyright (C) 2021 David Stächele <david@daiwai.de>
0003  *
0004  *   This program is free software; you can redistribute it and/or modify
0005  *   it under the terms of the GNU General Public License as published by
0006  *   the Free Software Foundation; either version 2 of the License, or
0007  *   (at your option) any later version.
0008  *
0009  *   This program is distributed in the hope that it will be useful,
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *   GNU General Public License for more details.
0013  *
0014  *   You should have received a copy of the GNU General Public License along
0015  *   with this program; if not, write to the
0016  *      Free Software Foundation, Inc.
0017  *      51 Franklin Street, Fifth Floor
0018  *      Boston, MA  02110-1301  USA.
0019  *
0020  */
0021 
0022 #include "csveventlog.h"
0023 
0024 #include "ktt_debug.h"
0025 #include "model/eventsmodel.h"
0026 #include "model/projectmodel.h"
0027 #include "model/task.h"
0028 #include "model/tasksmodel.h"
0029 
0030 QString exportCSVEventLogToString(ProjectModel *projectModel, const ReportCriteria &rc)
0031 {
0032     const QDate &from = rc.from;
0033     const QDate &to = rc.to;
0034     QString delim = rc.delimiter;
0035     const QString cr = QStringLiteral("\n");
0036     const QString dateTimeFormat = QStringLiteral("yyyy-MM-dd HH:mm");
0037     QStringList events;
0038     QString row;
0039     QString header;
0040 
0041     // Write header columns
0042     header.append(QStringLiteral("Start"));
0043     header.append(delim);
0044     header.append(QStringLiteral("End"));
0045     header.append(delim);
0046     header.append(QStringLiteral("Task name"));
0047     header.append(delim);
0048     header.append(QStringLiteral("Comment"));
0049     header.append(delim);
0050     header.append(QStringLiteral("UID"));
0051     header.append(cr);
0052 
0053     // Export all events
0054     for (const auto *event : projectModel->eventsModel()->events()) {
0055         row.clear();
0056 
0057         QDateTime start = event->dtStart();
0058         QDateTime end = event->dtEnd();
0059 
0060         if (start.date() < from || end.date() > to) {
0061             continue;
0062         }
0063 
0064         // Write CSV row
0065         row.append(start.toString(dateTimeFormat));
0066         row.append(delim);
0067         row.append(end.toString(dateTimeFormat));
0068         row.append(delim);
0069         row.append(getFullEventName(event, projectModel));
0070         row.append(delim);
0071 
0072         qDebug() << "event->comments.count() =" << event->comments().count();
0073         if (event->comments().count() > 0) {
0074             row.append(event->comments().last());
0075         }
0076         row.append(delim);
0077 
0078         row.append(event->uid());
0079         row.append(cr);
0080 
0081         events.append(row);
0082     }
0083 
0084     events.sort();
0085     // prepend header after sorting
0086     events.prepend(header);
0087 
0088     return events.join(QStringLiteral(""));
0089 }
0090 
0091 QString getFullEventName(const Event *event, ProjectModel *projectModel)
0092 {
0093     QString fullName;
0094 
0095     // maybe the file is corrupt and (*i)->relatedTo is NULL
0096     if (event->relatedTo().isEmpty()) {
0097         qCDebug(KTT_LOG) << "There is no 'relatedTo' entry for " << event->summary();
0098         return fullName;
0099     }
0100 
0101     Task *parent = dynamic_cast<Task *>(projectModel->tasksModel()->taskByUID(event->relatedTo()));
0102     if (!parent) {
0103         qCDebug(KTT_LOG) << "Skipping orphaned (no related parent task) entry " << event->summary();
0104         return fullName;
0105     }
0106 
0107     Task *parentTask;
0108     parentTask = parent;
0109     fullName += parentTask->name();
0110     parentTask = parentTask->parentTask();
0111     while (parentTask) {
0112         fullName = parentTask->name() + QStringLiteral("->") + fullName;
0113         qCDebug(KTT_LOG) << "Fullname(inside): " << fullName;
0114         parentTask = parentTask->parentTask();
0115         qCDebug(KTT_LOG) << "Parent task: " << parentTask;
0116     }
0117 
0118     return fullName;
0119 }