File indexing completed on 2024-12-15 03:45:00
0001 /* 0002 SPDX-FileCopyrightText: 2017 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: MIT 0005 */ 0006 0007 #include "productexportjob.h" 0008 0009 #include <rest/restapi.h> 0010 #include <core/sample.h> 0011 0012 #include <QDir> 0013 #include <QNetworkReply> 0014 0015 using namespace KUserFeedback::Console; 0016 0017 ProductExportJob::ProductExportJob(const QString& productId, const QString& destination, RESTClient* restClient, QObject* parent) 0018 : Job(parent) 0019 , m_dest(destination) 0020 , m_restClient(restClient) 0021 { 0022 Q_ASSERT(m_restClient); 0023 Q_ASSERT(m_restClient->isConnected()); 0024 auto reply = RESTApi::listProducts(restClient); 0025 connect(reply, &QNetworkReply::finished, this, [this, productId, reply]() { 0026 reply->deleteLater(); 0027 if (reply->error() != QNetworkReply::NoError) { 0028 deleteLater(); 0029 return; 0030 } 0031 const auto products = Product::fromJson(reply->readAll()); 0032 const auto it = std::find_if(products.begin(), products.end(), [productId](const auto &p) { 0033 return p.name() == productId; 0034 }); 0035 if (it == products.end()) { 0036 emitError(tr("Product not found.")); 0037 } else { 0038 m_product = *it; 0039 doExportSchema(); 0040 } 0041 }); 0042 } 0043 0044 ProductExportJob::ProductExportJob(const Product& product, const QString& destination, RESTClient* restClient, QObject* parent) 0045 : Job(parent) 0046 , m_product(product) 0047 , m_dest(destination) 0048 , m_restClient(restClient) 0049 { 0050 Q_ASSERT(m_restClient); 0051 Q_ASSERT(m_restClient->isConnected()); 0052 doExportSchema(); 0053 } 0054 0055 ProductExportJob::~ProductExportJob() = default; 0056 0057 void ProductExportJob::doExportSchema() 0058 { 0059 Q_ASSERT(m_product.isValid()); 0060 0061 QFile f(destination() + QLatin1Char('/') + m_product.name() + QLatin1String(".schema")); 0062 if (!f.open(QFile::WriteOnly)) { 0063 emitError(tr("Could not open file: %1").arg(f.errorString())); 0064 return; 0065 } 0066 f.write(m_product.toJson()); 0067 0068 doExportSurveys(); 0069 } 0070 0071 void ProductExportJob::doExportSurveys() 0072 { 0073 auto reply = RESTApi::listSurveys(m_restClient, m_product); 0074 connect(reply, &QNetworkReply::finished, this, [this, reply]() { 0075 reply->deleteLater(); 0076 if (reply->error() != QNetworkReply::NoError) { 0077 deleteLater(); 0078 return; 0079 } 0080 0081 QFile f(destination() + QLatin1Char('/') + m_product.name() + QLatin1String(".surveys")); 0082 if (!f.open(QFile::WriteOnly)) { 0083 emitError(tr("Could not open file: %1").arg(f.errorString())); 0084 return; 0085 } 0086 f.write(reply->readAll()); 0087 doExportData(); 0088 }); 0089 } 0090 0091 void ProductExportJob::doExportData() 0092 { 0093 auto reply = RESTApi::listSamples(m_restClient, m_product); 0094 connect(reply, &QNetworkReply::finished, this, [this, reply]() { 0095 reply->deleteLater(); 0096 if (reply->error() != QNetworkReply::NoError) { 0097 deleteLater(); 0098 return; 0099 } 0100 0101 QFile f(destination() + QLatin1Char('/') + m_product.name() + QLatin1String(".data")); 0102 if (!f.open(QFile::WriteOnly)) { 0103 emitError(tr("Could not open file: %1").arg(f.errorString())); 0104 return; 0105 } 0106 const auto samples = Sample::fromJson(reply->readAll(), m_product); 0107 f.write(Sample::toJson(samples, m_product)); 0108 emitFinished(); 0109 }); 0110 } 0111 0112 QString ProductExportJob::destination() const 0113 { 0114 QDir dest(m_dest); 0115 Q_ASSERT(dest.exists()); 0116 0117 dest.mkpath(m_product.name()); 0118 dest.cd(m_product.name()); 0119 return dest.absolutePath(); 0120 } 0121 0122 #include "moc_productexportjob.cpp"