File indexing completed on 2024-11-17 04:49:33

0001 /*
0002  * Copyright (c) 2019 Alexander Potashev <aspotashev@gmail.com>
0003  *
0004  * This program is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU General Public License as
0006  * published by the Free Software Foundation; either version 2 of
0007  * the License or (at your option) version 3 or any later version
0008  * accepted by the membership of KDE e.V. (or its successor approved
0009  * by the membership of KDE e.V.), which shall act as a proxy
0010  * defined in Section 14 of version 3 of the license.
0011  *
0012  * This program is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015  * GNU General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU General Public License
0018  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0019  */
0020 
0021 #include "export.h"
0022 
0023 #include <KIO/StoredTransferJob>
0024 #include <KLocalizedString>
0025 
0026 #include "csveventlog.h"
0027 #include "csvhistory.h"
0028 #include "csvtotals.h"
0029 #include "ktt_debug.h"
0030 #include "model/projectmodel.h"
0031 #include "totalsastext.h"
0032 
0033 QString exportToString(ProjectModel *model, Task *currentTask, const ReportCriteria &rc)
0034 {
0035     switch (rc.reportType) {
0036     case ReportCriteria::CSVTotalsExport:
0037         return exportCSVToString(model->tasksModel(), rc);
0038     case ReportCriteria::CSVHistoryExport:
0039         return exportCSVHistoryToString(model, rc);
0040     case ReportCriteria::CSVEventLogExport:
0041         return exportCSVEventLogToString(model, rc);
0042     case ReportCriteria::TextTotalsExport:
0043         return totalsAsText(model->tasksModel(), currentTask, rc);
0044     default:
0045         return QString();
0046     }
0047 }
0048 
0049 QString writeExport(const QString &data, const QUrl &url)
0050 {
0051     QString err;
0052 
0053     // store the file locally or remote
0054     if (url.isLocalFile()) {
0055         qCDebug(KTT_LOG) << "storing a local file";
0056         QString filename = url.toLocalFile();
0057         if (filename.isEmpty()) {
0058             filename = url.url();
0059         }
0060 
0061         QFile f(filename);
0062         if (!f.open(QIODevice::WriteOnly)) {
0063             err = i18n("Could not open \"%1\".", filename);
0064             qCDebug(KTT_LOG) << "Could not open file";
0065         }
0066         qDebug() << "Err is " << err;
0067         if (err.length() == 0) {
0068             QTextStream stream(&f);
0069             qCDebug(KTT_LOG) << "Writing to file: " << data;
0070             // Export to file
0071             stream << data;
0072             f.close();
0073         }
0074     } else {
0075         // use remote file
0076         auto* const job = KIO::storedPut(data.toUtf8(), url, -1);
0077         //KJobWidgets::setWindow(job, &dialog); // TODO: add progress dialog
0078         if (!job->exec()) {
0079             err = QString::fromLatin1("Could not upload");
0080         }
0081     }
0082 
0083     return err;
0084 }