Alkimia API

alkonlinequoteuploaddialog.cpp
1/*
2 SPDX-FileCopyrightText: 2024 Ralf Habacker ralf.habacker @freenet.de
3
4 This file is part of libalkimia.
5
6 SPDX-License-Identifier: LGPL-2.1-or-later
7*/
8
9#include "alkonlinequoteuploaddialog.h"
10
11#include "alknewstuffstore.h"
12#include "alkonlinequotesource.h"
13#include "ui_alkonlinequoteuploaddialog.h"
14
15#include <QApplication>
16#include <QClipboard>
17#include <QDate>
18#include <QDesktopServices>
19#include <QDir>
20#include <QFileInfo>
21#include <QProcess>
22#include <QTimer>
23
24static const char *packager = "/usr/bin/7za";
25
26
27AlkOnlineQuoteUploadDialog::AlkOnlineQuoteUploadDialog(const AlkOnlineQuoteSource &source, bool autoGenerate, QWidget *parent)
28 : QDialog(parent)
29 , ui(new Ui::AlkOnlineQuoteUploadDialog)
30 , m_source(source)
31{
32 ui->setupUi(this);
33 ui->m_editSourceName->setText(source.name());
34 ui->m_editVersion->setText(QDate::currentDate().toString(Qt::ISODate));
35 ui->m_packageLabel->setVisible(false);
36 ui->m_failedLabel->setVisible(false);
37 QUrl packageUrl;
38 QString id = m_source.profile()->GHNSId(source.name());
39 if (!id.isEmpty()) {
40 m_storePackageEditUrl = QUrl(AlkNewStuffStore::packageBaseUrl + id + AlkNewStuffStore::packageEditSuffix);
41 packageUrl = QUrl(AlkNewStuffStore::packageBaseUrl + id);
42 if (autoGenerate) {
43 slotCreatePackage();
44 slotCopyToClipboard();
45 slotEnterStoreAndClose();
46 QTimer::singleShot(0, this, SLOT(close()));
47 }
48 } else {
49 m_storePackageEditUrl = QUrl(AlkNewStuffStore::newPackageUrl);
50 }
51 ui->m_storePackageEditUrl->setText(QString("<a href=\"%1\">%1</a>").arg(m_storePackageEditUrl.toString()));
52 ui->m_storePackageUrl->setText(QString("<a href=\"%1\">%1</a>").arg(packageUrl.toString()));
53
54 connect(ui->m_createButton, SIGNAL(clicked()), this, SLOT(slotCreatePackage()));
55 connect(ui->m_enterStoreButtonAndClose, SIGNAL(clicked()), this, SLOT(slotEnterStoreAndClose()));
56 connect(ui->m_copyToClipboardButton, SIGNAL(clicked()), this, SLOT(slotCopyToClipboard()));
57 connect(ui->m_closeButton, SIGNAL(clicked()), this, SLOT(accept()));
58}
59
60AlkOnlineQuoteUploadDialog::~AlkOnlineQuoteUploadDialog()
61{
62 delete ui;
63}
64
65bool AlkOnlineQuoteUploadDialog::isSupported()
66{
67 QFileInfo fi(packager);
68 return fi.exists();
69}
70
71bool AlkOnlineQuoteUploadDialog::compressFile(const QString &outFile, const QString &inFile)
72{
73 QProcess zip;
74 QStringList args;
75 args << "a"
76 << outFile
77 << inFile;
78
79 zip.start(packager, args);
80 if (!zip.waitForStarted())
81 return false;
82
83 if (!zip.waitForFinished())
84 return false;
85
86 return true;
87}
88
89void AlkOnlineQuoteUploadDialog::slotCreatePackage()
90{
91 QString tempPath = QDir::tempPath();
92 QFileInfo inFile(m_source.ghnsWriteFileName());
93 QFileInfo outFile(tempPath + "/" + inFile.baseName() + "-" + ui->m_editVersion->text() + ".zip");
94 ui->m_copyToClipboardButton->setEnabled(true);
95 m_file = outFile.absoluteFilePath();
96 ui->m_fileLabel->setText(QString("<a href=\"%1\">%1</a>").arg(m_file.toString()));
97
98 if (!compressFile(outFile.absoluteFilePath(), inFile.absoluteFilePath())) {
99 ui->m_failedLabel->setVisible(true);
100 return;
101 }
102
103 ui->m_packageLabel->setVisible(true);
104 ui->m_createButton->setEnabled(false);
105 ui->m_closeButton->setEnabled(true);
106}
107
108void AlkOnlineQuoteUploadDialog::slotCopyToClipboard()
109{
110 QClipboard *clipboard = QApplication::clipboard();
111 clipboard->setText(m_file.toString());
112}
113
114void AlkOnlineQuoteUploadDialog::slotEnterStoreAndClose()
115{
116 QDesktopServices::openUrl(m_storePackageEditUrl);
117 accept();
118}
const QList< QKeySequence > & close()
void setText(const QString &text, Mode mode)
QDate currentDate()
QString toString(QStringView format, QCalendar cal) const const
bool openUrl(const QUrl &url)
virtual void accept()
QString tempPath()
QClipboard * clipboard()
void start(OpenMode mode)
bool waitForFinished(int msecs)
bool waitForStarted(int msecs)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString(FormattingOptions options) const const
void setupUi(QWidget *widget)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.