File indexing completed on 2024-06-23 05:02:20

0001 /*
0002     SPDX-FileCopyrightText: 2007-2018 Thomas Baumgart <tbaumgart@kde.org>
0003     SPDX-FileCopyrightText: 2021 Dawid Wróbel <me@dawidwrobel.com>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "reporttable.h"
0008 
0009 // ----------------------------------------------------------------------------
0010 // QT Includes
0011 
0012 #include <QFile>
0013 #include <QLocale>
0014 
0015 // ----------------------------------------------------------------------------
0016 // KDE Includes
0017 
0018 #include <KLocalizedString>
0019 
0020 // ----------------------------------------------------------------------------
0021 // Project Includes
0022 
0023 #include "kmmurl.h"
0024 #include "kmymoneysettings.h"
0025 #include "kmymoneyutils.h"
0026 #include "mymoneyexception.h"
0027 #include "mymoneyfile.h"
0028 #include "mymoneysecurity.h"
0029 #include "mymoneyutils.h"
0030 
0031 reports::ReportTable::ReportTable(const MyMoneyReport& _report):
0032     m_reportStyleSheet("reportstylesheet"),
0033     m_config(_report),
0034     m_containsNonBaseCurrency(false)
0035 {
0036 }
0037 
0038 QString reports::ReportTable::cssFileNameGet()
0039 {
0040     QString cssfilename;
0041 
0042     if (!MyMoneyFile::instance()->value(m_reportStyleSheet).isEmpty()) {
0043         // try to find the stylesheet specific for this report
0044         cssfilename = QStandardPaths::locate(QStandardPaths::AppConfigLocation, "html/" + MyMoneyFile::instance()->value(m_reportStyleSheet));
0045     }
0046 
0047     if (cssfilename.isEmpty() || !QFile::exists(cssfilename)) {
0048         // if no report specific stylesheet was found, try to use the configured one
0049         cssfilename = KMyMoneySettings::cssFileDefault();
0050     }
0051 
0052     return cssfilename;
0053 }
0054 
0055 QString reports::ReportTable::renderHeader(const QString& title, const QByteArray& encoding)
0056 {
0057     QString header = QString(
0058                          "<!DOCTYPE HTML PUBLIC"
0059                          " \"-//W3C//DTD HTML 4.01 //EN\""
0060                          " \"http://www.w3.org/TR/html4/strict.dtd\">"
0061                          "\n<html>\n<head>"
0062                          "\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=%1\" />"
0063                          "\n<title>%2</title>")
0064                          .arg(encoding, title);
0065 
0066     // inline the CSS
0067     header += "<style type=\"text/css\">\n";
0068     header += KMyMoneyUtils::getStylesheet(cssFileNameGet());
0069     header += "</style>\n";
0070 
0071     header += QLatin1String("</head>\n<body>\n");
0072 
0073     return header;
0074 }
0075 
0076 QString reports::ReportTable::renderFooter()
0077 {
0078     return "</body>\n</html>\n";
0079 }
0080 
0081 QString reports::ReportTable::renderReport(const QString& type, const QByteArray& encoding, const QString& title)
0082 {
0083     MyMoneyFile* file = MyMoneyFile::instance();
0084     QString result;
0085 
0086     // convert a possible infinite report period to valid dates
0087     QDate fromDate, toDate;
0088     m_config.validDateRange(fromDate, toDate);
0089 
0090     if (type == QLatin1String("html")) {
0091         //this renders the HEAD tag and sets the correct css file
0092         result = renderHeader(title, encoding);
0093 
0094         try {
0095             // report's name
0096             result.append(QString::fromLatin1("<h2 class=\"report\">%1</h2>\n").arg(m_config.name()));
0097 
0098             // report's date range
0099             result.append(QString::fromLatin1("<div class=\"subtitle\">%1</div>\n"
0100                                               "<div class=\"gap\">&nbsp;</div>\n")
0101                               .arg(i18nc("Report date range", "%1 through %2", MyMoneyUtils::formatDate(fromDate), MyMoneyUtils::formatDate(toDate))));
0102             // report's currency information
0103             if (m_containsNonBaseCurrency) {
0104                 result.append(QString::fromLatin1("<div class=\"subtitle\">%1</div>\n"
0105                                                   "<div class=\"gap\">&nbsp;</div>\n").arg(m_config.isConvertCurrency() ?
0106                                                           i18n("All currencies converted to %1", file->baseCurrency().name()) :
0107                                                           i18n("All values shown in %1 unless otherwise noted", file->baseCurrency().name())));
0108             } else {
0109                 result.append(QString::fromLatin1("<div class=\"subtitle\">%1</div>\n"
0110                                                   "<div class=\"gap\">&nbsp;</div>\n").arg(
0111                                   i18n("All values shown in %1", file->baseCurrency().name())));
0112             }
0113 
0114             //this method is implemented by each concrete class
0115             result.append(renderHTML());
0116         } catch (const MyMoneyException &e) {
0117             result.append(QString::fromLatin1("<h1>%1</h1><p>%2</p>").arg(i18n("Unable to generate report"),
0118                           i18n("There was an error creating your report: \"%1\".\nPlease report this error to the developer's list: kmymoney-devel@kde.org", e.what())));
0119         }
0120 
0121         //this renders a common footer
0122         result.append(QLatin1String("</body>\n</html>\n"));
0123     } else if (type == QLatin1String("csv")) {
0124         result.append(QString::fromLatin1("\"Report: %1\"\n").arg(m_config.name()));
0125         result.append(
0126             QString::fromLatin1("%1\n").arg(i18nc("Report date range", "%1 through %2", MyMoneyUtils::formatDate(fromDate), MyMoneyUtils::formatDate(toDate))));
0127         if (m_containsNonBaseCurrency)
0128             result.append(QString::fromLatin1("%1\n").arg(m_config.isConvertCurrency() ?
0129                           i18n("All currencies converted to %1", file->baseCurrency().name()) :
0130                           i18n("All values shown in %1 unless otherwise noted", file->baseCurrency().name())));
0131         result.append(renderCSV());
0132     }
0133 
0134     return result;
0135 }