File: main.cpp

package info (click to toggle)
charmtimetracker 1.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,340 kB
  • sloc: cpp: 19,176; xml: 284; python: 120; makefile: 14
file content (156 lines) | stat: -rw-r--r-- 6,717 bytes parent folder | download | duplicates (2)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
  main.cpp

  This file is part of Charm, a task-based time tracking application.

  Copyright (C) 2009-2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com

  Author: Mirko Boehm <mirko.boehm@kdab.com>

  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, see <http://www.gnu.org/licenses/>.
*/

#include <iostream>

#include <QDomDocument>
#include <QDomElement>
#include <QObject>
#include <QFile>

#include "Core/Event.h"
#include "Core/CharmExceptions.h"
#include <Core/XmlSerialization.h>

#include "Exceptions.h"
#include "Options.h"

int main(int argc, char **argv)
{
    using namespace std;

    try {
        using namespace TimesheetGenerator;
        cout << "Timesheet Generator, (C) 2009 Mirko Boehm, KDAB" << endl;
        Options options(argc, argv);

        // create a QDomDocument from the file content:
        QDomDocument doc(QString::fromLatin1("charm_template"));
        QFile file(options.file());
        if (!file.open(QIODevice::ReadOnly))
            throw Exception(QObject::tr("Cannot open specified file for reading."));
        QString errorMessage;
        int errorLine, errorColumn;
        if (!doc.setContent(&file, false, &errorMessage, &errorLine, &errorColumn)) {
            throw Exception(QObject::tr("Cannot read specified file: ") + errorMessage
                            + QObject::tr(" (%1:%2)").arg(errorLine).arg(errorColumn));
        }

        // find the time sheet elements, loop to create the time sheets:
        QDomElement docElem = doc.documentElement();
        if (docElem.tagName() != QString::fromLatin1("charm_template"))
            throw Exception(QObject::tr("Wrong document element found in the specified file."));
        const QString timesheetTagName = QString::fromLatin1("timesheet");
        for (QDomElement child = docElem.firstChildElement(timesheetTagName);
             !child.isNull();
             child = child.nextSiblingElement(timesheetTagName)) {
            const QString userIdString = child.attributes().namedItem(QStringLiteral("userid")).nodeValue();
            bool ok;
            const int userId = userIdString.toInt(&ok);
            if (!ok)
                throw Exception(QObject::tr("Mis-spelled or missing user id in timesheet entry."));

            // read event entries:
            EventList events;
            // FIXME readEfforts?
            const QString eventTagName = QString::fromLatin1("event");
            for (QDomElement eventElem = child.firstChildElement(eventTagName);
                 !eventElem.isNull();
                 eventElem = eventElem.nextSiblingElement(eventTagName)) {
                try {
                    Event event = Event::fromXml(eventElem);
                    // event.dump();
                    events << event;
                } catch (XmlSerializationException &e) {
                    throw Exception(QObject::tr("Error reading event: ") + e.what());
                }
            }
            // now create the time sheet:
            QDomDocument document = XmlSerialization::createXmlTemplate(QStringLiteral("weekly-timesheet"));

            // find metadata and report element:
            QDomElement root = document.documentElement();
            QDomElement metadata = XmlSerialization::metadataElement(document);
            QDomElement report = XmlSerialization::reportElement(document);
            Q_ASSERT(!root.isNull() && !metadata.isNull() && !report.isNull());
            // extend metadata tag: add year, and serial (week) number:
            // temp:
            // the start date of the week, specified on the command line:
            const QDateTime start(options.date(), QTime(), Qt::UTC);
            int year;
            const int week = start.date().weekNumber(&year);

            {
                QDomElement yearElement = document.createElement(QStringLiteral("year"));
                metadata.appendChild(yearElement);
                QDomText text = document.createTextNode(QString::number(year));
                yearElement.appendChild(text);
                QDomElement weekElement = document.createElement(QStringLiteral("serial-number"));
                weekElement.setAttribute(QStringLiteral("semantics"), QStringLiteral("week-number"));
                metadata.appendChild(weekElement);
                QDomText weektext = document.createTextNode(QString::number(week));
                weekElement.appendChild(weektext);
            }

            {   // effort
                // make effort element:
                QDomElement effort = document.createElement(QStringLiteral("effort"));
                report.appendChild(effort);
                // create elements:
                Q_FOREACH (Event event, events) {
                    const QDateTime end = start.addSecs(event.duration());
                    event.setStartDateTime(start);
                    event.setEndDateTime(end);
                    effort.appendChild(event.toXml(document));
                }
            }

            // save the file:
            // temp:
            const QString filename = QString::fromLatin1(
                "WeeklyTimesheet-generated-%1-%2-%3.charmreport")
                                     .arg(userId).arg(year).arg(week);
            QFile file(filename);
            if (file.open(QIODevice::WriteOnly)) {
                QTextStream stream(&file);
                document.save(stream, 4);
                cout << "Generated Time Sheet: " << qPrintable(filename) << endl;
            } else {
                throw Exception(QObject::tr("Error writing file ") + filename);
            }
        }
        return 0;
    } catch (TimesheetGenerator::UsageException &e) {
        cerr << e.what() << endl;
        cout << "Usage: " << endl
             << "   * TimesheetGenerator -h                              <-- get help"
             << endl
             <<
            "   * TimesheetGenerator -f template-filename -d date    <-- generate timesheets from template for that date"
             << endl;
        return 1;
    } catch (TimesheetGenerator::Exception &e) {
        cerr << e.what() << endl;
        return 1;
    }
}