File: kmmreport-export.cpp

package info (click to toggle)
kmymoney 5.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 75,568 kB
  • sloc: cpp: 162,743; xml: 2,691; sh: 846; python: 656; perl: 289; javascript: 28; makefile: 10
file content (208 lines) | stat: -rw-r--r-- 7,743 bytes parent folder | download
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
    SPDX-FileCopyrightText: 2024 Ralf Habacker <ralf.habacker@freenet.de>
    SPDX-License-Identifier: GPL-2.0-or-later
*/

// KDE includes

#include <KLocalizedString>

// Qt includes

#include <QCommandLineParser>
#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QtDebug>

// Project includes

#include "config-kmymoney-version.h"
#include "mymoneyenums.h"
#include "mymoneyfile.h"
#include "mymoneyreport.h"
#include "mymoneyxmlreader.h"
#include "objectinfotable.h"
#include "pivottable.h"
#include "querytable.h"

using namespace reports;

class Options
{
public:
    QStringList fileUrls;
    bool hasCSVOption = false;
    bool hasCreateReferenceOption = false;
    bool hasHTMLOption = false;
    bool hasListOption = false;
    bool hasObjectInfoTableOption = false;
    bool hasPivotTableOption = false;
    bool hasQueryTableOption = true;
    bool hasXMLOption = false;
    QString outFileName;
    QString useReportName;

    QString extension() const
    {
        if (hasCSVOption)
            return "csv";
        else
            return "html";
    }
};

QTextStream& qStdOut()
{
    static QTextStream ts(stdout);
    return ts;
}

template<class T>
void exportTable(QTextStream& o, const Options& options, const MyMoneyReport& report)
{
    T table(report);
    if (options.hasCSVOption) {
        o << table.renderCSV();
    } else if (options.hasHTMLOption) {
        o << table.renderHTML();
    } else if (options.hasXMLOption) {
        o << table.toXml();
    }
}

template void exportTable<PivotTable>(QTextStream& o, const Options& options, const MyMoneyReport& report);
template void exportTable<QueryTable>(QTextStream& o, const Options& options, const MyMoneyReport& report);
template void exportTable<ObjectInfoTable>(QTextStream& o, const Options& options, const MyMoneyReport& report);

void exportTable(QTextStream& o, const Options& options, const MyMoneyReport& report)
{
    if (options.hasObjectInfoTableOption && report.reportType() == eMyMoney::Report::ReportType::InfoTable)
        exportTable<ObjectInfoTable>(o, options, report);
    if (options.hasPivotTableOption && report.reportType() == eMyMoney::Report::ReportType::PivotTable)
        exportTable<PivotTable>(o, options, report);
    if (options.hasQueryTableOption && report.reportType() == eMyMoney::Report::ReportType::QueryTable)
        exportTable<QueryTable>(o, options, report);
}

void showReportName(QTextStream& o, const Options& options, const MyMoneyReport& report)
{
    if (options.hasObjectInfoTableOption && report.reportType() == eMyMoney::Report::ReportType::InfoTable)
        o << report.name() << "\n";
    if (options.hasPivotTableOption && report.reportType() == eMyMoney::Report::ReportType::PivotTable)
        o << report.name() << "\n";
    if (options.hasQueryTableOption && report.reportType() == eMyMoney::Report::ReportType::QueryTable)
        o << report.name() << "\n";
    if (!options.hasObjectInfoTableOption && !options.hasPivotTableOption && !options.hasQueryTableOption)
        o << report.name() << "\n";
}

int main(int argc, char** argv)
{
    QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));
    KLocalizedString::setApplicationDomain(QByteArrayLiteral("kmymoney"));
    QCoreApplication app(argc, argv);
    QCoreApplication::setApplicationName("kmmreport-export");
    QCoreApplication::setApplicationVersion(VERSION);

    Options options;
    options.hasCSVOption = true;

    if (argc != 0) {
        QCommandLineParser parser;
        parser.setApplicationDescription(i18n("Export KMyMoney reports"));
        parser.addHelpOption();
        parser.addVersionOption();

        const QCommandLineOption csvOption(QStringLiteral("csv"), i18n("Export in csv format (default)"));
        parser.addOption(csvOption);

        const QCommandLineOption htmlOption(QStringLiteral("html"), i18n("Export in HTML format"));
        parser.addOption(htmlOption);

        const QCommandLineOption xmlOption(QStringLiteral("xml"), i18n("Export in xml format"));
        parser.addOption(xmlOption);

        const QCommandLineOption createReferenceOption(QStringLiteral("reference"), i18n("Create reference file from output"));
        parser.addOption(createReferenceOption);

        const QCommandLineOption infoTableOption(QStringLiteral("infotable"), i18n("Use info table for exporting"));
        parser.addOption(infoTableOption);

        const QCommandLineOption pivotTableOption(QStringLiteral("pivottable"), i18n("Use pivot table for exporting"));
        parser.addOption(pivotTableOption);

        const QCommandLineOption queryTableOption(QStringLiteral("querytable"), i18n("Use query table for exporting (default)"));
        parser.addOption(queryTableOption);

        const QCommandLineOption listOption(QStringLiteral("list"), i18n("List all custom reports"));
        parser.addOption(listOption);

        const QCommandLineOption outFileOption(QStringLiteral("output"), i18n("Filename for generated output"), "output");
        parser.addOption(outFileOption);

        const QCommandLineOption reportOption(QStringLiteral("report"), i18n("Process a specific report"), "report");
        parser.addOption(reportOption);

        parser.addPositionalArgument(QStringLiteral("url"), i18n("file to open"));

        parser.process(QCoreApplication::arguments());

        options.fileUrls = parser.positionalArguments();
        options.hasCreateReferenceOption = parser.isSet(createReferenceOption);
        options.hasCSVOption = parser.isSet(csvOption);
        options.hasHTMLOption = parser.isSet(htmlOption);
        options.hasXMLOption = parser.isSet(xmlOption);
        if (options.hasCSVOption && options.hasHTMLOption) {
            qWarning() << i18n("Only one of --csv and --html is supported");
            return 1;
        }
        options.hasListOption = parser.isSet(listOption);
        options.hasObjectInfoTableOption = parser.isSet(infoTableOption);
        options.hasPivotTableOption = parser.isSet(pivotTableOption);
        options.hasQueryTableOption = parser.isSet(queryTableOption);
        if (parser.isSet(outFileOption))
            options.outFileName = parser.value(outFileOption);
        if (parser.isSet(reportOption))
            options.useReportName = parser.value(reportOption);
    }

    for (const auto& fileUrl : options.fileUrls) {
        QFile f(fileUrl);
        if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
            return 2;

        MyMoneyXmlReader reader;
        MyMoneyFile::instance()->unload();
        reader.setFile(MyMoneyFile::instance());
        reader.read(&f);
        f.close();
        MyMoneyFile::instance()->applyFileFixes(false);

        for (const auto& report : MyMoneyFile::instance()->reportList()) {
            if (!options.useReportName.isEmpty() && options.useReportName != report.name())
                continue;
            if (options.hasListOption) {
                showReportName(qStdOut(), options, report);
                continue;
            }
            if (!options.outFileName.isEmpty()) {
                QFile of(options.outFileName);
                if (!of.open(QIODevice::WriteOnly))
                    return 3;
                qDebug() << QLatin1String("saving report '%1' into '%2'").arg(report.name(), of.fileName());
                qStdOut() << of.fileName() << "\n";
                QTextStream o(&of);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
                o.setCodec("UTF-8");
#endif
                exportTable(o, options, report);
                of.close();
            } else {
                exportTable(qStdOut(), options, report);
            }
        }
    }
    return 0;
}