File indexing completed on 2024-04-28 16:30:35

0001 /***************************************************************************
0002  * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr
0003  * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  ***************************************************************************/
0006 /** @file
0007 * This file defines the main of skroogeconvert.
0008 *
0009 * @author Stephane MANKOWSKI / Guillaume DE BURE
0010  */
0011 #include "skgdocumentbank.h"
0012 #include "skgimportexportmanager.h"
0013 #include "skgtraces.h"
0014 #include "skgtransactionmng.h"
0015 
0016 #include <kaboutdata.h>
0017 
0018 #include <qapplication.h>
0019 #include <qcommandlineoption.h>
0020 #include <qcommandlineparser.h>
0021 #include <qcoreapplication.h>
0022 #include <qfileinfo.h>
0023 #include <qurl.h>
0024 
0025 /**
0026  * To compute the version
0027  */
0028 #define VER1_(x) #x
0029 /**
0030  * To compute the version
0031  */
0032 #define VER_(x) VER1_(x)
0033 /**
0034  * To compute the version
0035  */
0036 #define VER VER_(SKGVERSION)
0037 
0038 
0039 /**
0040  * The main of the application
0041  * @param argc number of arguments
0042  * @param argv arguments
0043  * @return return code
0044  */
0045 int main(int argc, char** argv)
0046 {
0047     KLocalizedString::setApplicationDomain("skrooge");
0048 
0049     KAboutData about(QStringLiteral("skroogeconvert"),
0050                      i18nc("The name of the application", "Skrooge Convert"),
0051                      QStringLiteral(VER),
0052                      i18nc("The description of the application", "A conversion tool for financial files (KMyMoney, GnuCash, Skrooge, …)"),
0053                      KAboutLicense::GPL_V3,
0054                      i18nc("Fullname", "(c) 2007-%1 Stephane MANKOWSKI & Guillaume DE BURE", QDate::currentDate().toString(QStringLiteral("yyyy"))),
0055                      QString(),
0056                      QStringLiteral("https://skrooge.org"));
0057 
0058     about.addAuthor(i18nc("Fullname", "Stephane MANKOWSKI"), i18nc("A job description", "Architect & Developer"), QStringLiteral("stephane@mankowski.fr"), QString()
0059                     , QStringLiteral("miraks")
0060                    );
0061     about.addAuthor(i18nc("Fullname", "Guillaume DE BURE"), i18nc("A job description", "Developer"), QStringLiteral("guillaume.debure@gmail.com"), QString()
0062                     , QStringLiteral("willy9")
0063                    );
0064     about.setOtherText(i18nc("The description of the application", "The application name is inspired by Charles Dicken's tale <i>A Christmas Carol</i>, where the main character, Ebenezer Scrooge, a grumpy old narrow man, gets visited by three ghosts who change the way he sees the world, in a good way."));
0065     about.setTranslator(i18nc("NAME OF TRANSLATORS", "Your names"), i18nc("EMAIL OF TRANSLATORS", "Your emails"));
0066     KAboutData::setApplicationData(about);
0067 
0068     QCoreApplication app(argc, argv);
0069     QCoreApplication::setApplicationName(about.componentName());
0070     QCoreApplication::setOrganizationDomain(about.organizationDomain());
0071     QCoreApplication::setApplicationVersion(about.version());
0072 
0073     QCommandLineParser parser;
0074     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("in"), i18nc("Application argument", "Input file. Supported formats:\n%1", SKGImportExportManager().getImportMimeTypeFilter(false)), QStringLiteral("file")));
0075     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("out"), i18nc("Application argument", "Output file. Supported formats:\n%1", SKGImportExportManager().getExportMimeTypeFilter(false)), QStringLiteral("file")));
0076     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("param"), i18nc("Application argument", "Name of a parameter"), QStringLiteral("name")));
0077     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("value"), i18nc("Application argument", "Value of a parameter"), QStringLiteral("value")));
0078     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("param_export"), i18nc("Application argument", "Name of a parameter for export"), QStringLiteral("name_export")));
0079     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("value_export"), i18nc("Application argument", "Value of a parameter for export"), QStringLiteral("value_export")));
0080 
0081     about.setupCommandLine(&parser);
0082     parser.process(app);
0083     about.processCommandLine(&parser);
0084 
0085 
0086     // Build list of arguments
0087     SKGError err;
0088     if (!parser.isSet(QStringLiteral("in"))) {
0089         err = SKGError(ERR_INVALIDARG, i18nc("Error message", "Missing -in option"));
0090     } else if (!parser.isSet(QStringLiteral("out"))) {
0091         err = SKGError(ERR_INVALIDARG, i18nc("Error message", "Missing -out option"));
0092     }
0093 
0094     // Initialisation of a bank document
0095     SKGDocumentBank document;
0096     document.setComputeBalances(false);
0097     IFOKDO(err, document.initialize())
0098     IFOK(err) {
0099         // Import
0100         QString file = parser.value(QStringLiteral("in"));
0101         QFileInfo fi(file);
0102         if (fi.exists()) {
0103             file = fi.absoluteFilePath();
0104         }
0105         SKGImportExportManager imp(&document, QUrl::fromLocalFile(file));
0106         QMap<QString, QString>  parameters = imp.getImportParameters();
0107         QStringList params = parser.values(QStringLiteral("param"));
0108         QStringList values = parser.values(QStringLiteral("value"));
0109         int nb = qMin(params.count(), values.count());
0110         for (int i = 0; i < nb; ++i) {
0111             parameters[params.at(i)] = values.at(i);
0112         }
0113         imp.setImportParameters(parameters);
0114 
0115         SKGTRACESEPARATOR;
0116         SKGTRACE << i18nc("Title of a console trace section", " Import parameters") << SKGENDL;
0117         QMapIterator<QString, QString> i(imp.getImportParameters());
0118         while (i.hasNext()) {
0119             i.next();
0120             SKGTRACE << " " << i.key() << "=" << i.value() << SKGENDL;
0121         }
0122         SKGTRACESEPARATOR;
0123         SKGTRACE << i18nc("Title of a console trace section", " Imported file:") << imp.getFileName().toDisplayString()  << SKGENDL;
0124         if (fi.suffix().toUpper() == QStringLiteral("SKG") || fi.suffix().toUpper() == QStringLiteral("SQLITE") || fi.suffix().toUpper() == QStringLiteral("SQLCIPHER")) {
0125             err = document.load(file, parameters[QStringLiteral("password")]);
0126         } else {
0127             SKGBEGINTRANSACTION(document, QStringLiteral("IMPORT"), err)
0128             IFOKDO(err, imp.importFile())
0129         }
0130     }
0131     IFOK(err) {
0132         // Export
0133         QString file = parser.value(QStringLiteral("out"));
0134         QFileInfo fi(file);
0135         if (fi.exists()) {
0136             file = fi.absoluteFilePath();
0137         }
0138         SKGImportExportManager exp(&document, QUrl::fromLocalFile(file));
0139         QMap<QString, QString>  parameters = exp.getExportParameters();
0140         QStringList params = parser.values(QStringLiteral("param_export"));
0141         QStringList values = parser.values(QStringLiteral("value_export"));
0142         int nb = qMin(params.count(), values.count());
0143         for (int i = 0; i < nb; ++i) {
0144             parameters[params.at(i)] = values.at(i);
0145         }
0146         exp.setExportParameters(parameters);
0147 
0148         SKGTRACESEPARATOR;
0149         SKGTRACE << i18nc("Title of a console trace section", " Export parameters") << SKGENDL;
0150         QMapIterator<QString, QString> i(exp.getExportParameters());
0151         while (i.hasNext()) {
0152             i.next();
0153             SKGTRACE << " " << i.key() << "=" << i.value() << SKGENDL;
0154         }
0155         SKGTRACESEPARATOR;
0156         SKGTRACE << i18nc("Title of a console trace section", " Exported file:") << exp.getFileName().toDisplayString()  << SKGENDL;
0157         if (fi.suffix().toUpper() == QStringLiteral("SKG")) {
0158             err = document.saveAs(file, true);
0159         } else {
0160             err = exp.exportFile();
0161         }
0162     }
0163 
0164     // Dump getMessages
0165     SKGDocument::SKGMessageList oMessages;
0166     IFOKDO(err, document.getMessages(document.getCurrentTransaction(), oMessages))
0167     int nb = oMessages.count();
0168     if (nb > 0) {
0169         SKGTRACESEPARATOR;
0170         for (int i = 0; i < nb; ++i) {
0171             SKGTRACE << oMessages.at(i).Text << SKGENDL;
0172         }
0173     }
0174 
0175     IFKO(err) {
0176         SKGTRACESUITE << err.getFullMessageWithHistorical() << SKGENDL;
0177         SKGTRACESEPARATOR;
0178         SKGTRACE << i18nc("Title of a console trace section", " FAILED") << SKGENDL;
0179         SKGTRACESEPARATOR;
0180     } else {
0181         SKGTRACESEPARATOR;
0182         SKGTRACE << i18nc("Title of a console trace section", " SUCCESSFUL") << SKGENDL;
0183         SKGTRACESEPARATOR;
0184     }
0185 
0186     SKGTraces::dumpProfilingStatistics();
0187     return err.getReturnCode();
0188 }