File indexing completed on 2024-07-14 04:55:02

0001 /*
0002     SPDX-FileCopyrightText: 2024 Ralf Habacker  ralf.habacker @freenet.de
0003 
0004     This file is part of libalkimia.
0005 
0006     SPDX-License-Identifier: LGPL-2.1-or-later
0007 */
0008 
0009 #include "alkonlinequoteuploaddialog.h"
0010 
0011 #include "ui_alkonlinequoteuploaddialog.h"
0012 #include "alkonlinequotesource.h"
0013 #include "alknewstuffstore.h"
0014 
0015 #include <QApplication>
0016 #include <QClipboard>
0017 #include <QDate>
0018 #include <QDesktopServices>
0019 #include <QDir>
0020 #include <QFileInfo>
0021 #include <QProcess>
0022 #include <QTimer>
0023 
0024 static const char *packager = "/usr/bin/7za";
0025 
0026 
0027 AlkOnlineQuoteUploadDialog::AlkOnlineQuoteUploadDialog(const AlkOnlineQuoteSource &source, bool autoGenerate, QWidget *parent)
0028     : QDialog(parent)
0029     , ui(new Ui::AlkOnlineQuoteUploadDialog)
0030     , m_source(source)
0031 {
0032     ui->setupUi(this);
0033     ui->m_editSourceName->setText(source.name());
0034     ui->m_editVersion->setText(QDate::currentDate().toString(Qt::ISODate));
0035     ui->m_packageLabel->setVisible(false);
0036     ui->m_failedLabel->setVisible(false);
0037     QUrl packageUrl;
0038     QString id = m_source.profile()->GHNSId(source.name());
0039     if (!id.isEmpty()) {
0040         m_storePackageEditUrl = QUrl(AlkNewStuffStore::packageBaseUrl + id + AlkNewStuffStore::packageEditSuffix);
0041         packageUrl = QUrl(AlkNewStuffStore::packageBaseUrl + id);
0042         if (autoGenerate) {
0043             slotCreatePackage();
0044             slotCopyToClipboard();
0045             slotEnterStoreAndClose();
0046             QTimer::singleShot(0, this, SLOT(close()));
0047         }
0048     } else {
0049         m_storePackageEditUrl = QUrl(AlkNewStuffStore::newPackageUrl);
0050     }
0051     ui->m_storePackageEditUrl->setText(QString("<a href=\"%1\">%1</a>").arg(m_storePackageEditUrl.toString()));
0052     ui->m_storePackageUrl->setText(QString("<a href=\"%1\">%1</a>").arg(packageUrl.toString()));
0053 
0054     connect(ui->m_createButton, SIGNAL(clicked()), this, SLOT(slotCreatePackage()));
0055     connect(ui->m_enterStoreButtonAndClose, SIGNAL(clicked()), this, SLOT(slotEnterStoreAndClose()));
0056     connect(ui->m_copyToClipboardButton, SIGNAL(clicked()), this, SLOT(slotCopyToClipboard()));
0057     connect(ui->m_closeButton, SIGNAL(clicked()), this, SLOT(accept()));
0058 }
0059 
0060 AlkOnlineQuoteUploadDialog::~AlkOnlineQuoteUploadDialog()
0061 {
0062     delete ui;
0063 }
0064 
0065 bool AlkOnlineQuoteUploadDialog::isSupported()
0066 {
0067     QFileInfo fi(packager);
0068     return fi.exists();
0069 }
0070 
0071 bool AlkOnlineQuoteUploadDialog::compressFile(const QString &outFile, const QString &inFile)
0072 {
0073     QProcess zip;
0074     QStringList args;
0075     args << "a"
0076          << outFile
0077          << inFile;
0078 
0079     zip.start(packager, args);
0080     if (!zip.waitForStarted())
0081         return false;
0082 
0083     if (!zip.waitForFinished())
0084         return false;
0085 
0086     return true;
0087 }
0088 
0089 void AlkOnlineQuoteUploadDialog::slotCreatePackage()
0090 {
0091     QString tempPath = QDir::tempPath();
0092     QFileInfo inFile(m_source.ghnsWriteFileName());
0093     QFileInfo outFile(tempPath + "/" + inFile.baseName() + "-" + ui->m_editVersion->text() + ".zip");
0094     ui->m_copyToClipboardButton->setEnabled(true);
0095     m_file = outFile.absoluteFilePath();
0096     ui->m_fileLabel->setText(QString("<a href=\"%1\">%1</a>").arg(m_file.toString()));
0097 
0098     if (!compressFile(outFile.absoluteFilePath(), inFile.absoluteFilePath())) {
0099         ui->m_failedLabel->setVisible(true);
0100         return;
0101     }
0102 
0103     ui->m_packageLabel->setVisible(true);
0104     ui->m_createButton->setEnabled(false);
0105     ui->m_closeButton->setEnabled(true);
0106 }
0107 
0108 void AlkOnlineQuoteUploadDialog::slotCopyToClipboard()
0109 {
0110     QClipboard *clipboard = QApplication::clipboard();
0111     clipboard->setText(m_file.toString());
0112 }
0113 
0114 void AlkOnlineQuoteUploadDialog::slotEnterStoreAndClose()
0115 {
0116     QDesktopServices::openUrl(m_storePackageEditUrl);
0117     accept();
0118 }