31 #include "gui/formuploadbundle.h"
33 #include "gui/lineeditwithstatus.h"
34 #include "miscellaneous/application.h"
35 #include "miscellaneous/iconfactory.h"
36 #include "network-web/downloader.h"
37 #include "network-web/networkfactory.h"
38 #include "core/templatefactory.h"
39 #include "core/templatecore.h"
40 #include "core/templateeditor.h"
41 #include "definitions/definitions.h"
43 #include <QPushButton>
44 #include "QFileDialog"
47 FormUploadBundle::FormUploadBundle(QWidget *parent)
51 setWindowFlags(Qt::MSWindowsFixedSizeDialogHint | Qt::Dialog | Qt::WindowSystemMenuHint | Qt::WindowTitleHint);
55 m_btnClose = m_ui->m_buttonBox->button(QDialogButtonBox::Close);
56 m_btnUpload = m_ui->m_buttonBox->addButton(tr(
"&Upload application"),
57 QDialogButtonBox::ActionRole);
59 m_ui->m_txtApplicationName->lineEdit()->setPlaceholderText(tr(
"Name of your application"));
60 m_ui->m_txtAuthorEmail->lineEdit()->setPlaceholderText(tr(
"Your e-mail"));
61 m_ui->m_txtAuthorName->lineEdit()->setPlaceholderText(tr(
"Your name"));
64 m_ui->m_cmbCategory->addItem(tr(
"Science"),
"Science");
65 m_ui->m_cmbCategory->addItem(tr(
"Math"),
"Math");
66 m_ui->m_cmbCategory->addItem(tr(
"Physics"),
"Physics");
67 m_ui->m_cmbCategory->addItem(tr(
"Literature"),
"Literature");
68 m_ui->m_cmbCategory->addItem(tr(
"English"),
"English");
69 m_ui->m_cmbCategory->addItem(tr(
"Geography"),
"Geography");
70 m_ui->m_cmbCategory->addItem(tr(
"Social Studies"),
"Social Studies");
71 m_ui->m_cmbCategory->addItem(tr(
"Language"),
"Language");
72 m_ui->m_cmbCategory->addItem(tr(
"History"),
"History");
73 m_ui->m_cmbCategory->addItem(tr(
"Chemistry"),
"Chemistry");
75 connect(m_ui->m_txtApplicationName->lineEdit(), SIGNAL(textChanged(QString)),
this, SLOT(checkApplicationName(QString)));
76 connect(m_ui->m_txtAuthorEmail->lineEdit(), SIGNAL(textChanged(QString)),
this, SLOT(checkAuthorEmail(QString)));
77 connect(m_ui->m_txtAuthorName->lineEdit(), SIGNAL(textChanged(QString)),
this, SLOT(checkAuthorName(QString)));
78 connect(
this, SIGNAL(metadataChanged()),
this, SLOT(checkMetadata()));
79 connect(m_btnUpload, SIGNAL(clicked()),
this, SLOT(startUpload()));
80 connect(m_ui->m_btnSelectIcon, SIGNAL(clicked()),
this, SLOT(selectApplicationIcon()));
82 setTabOrder(m_ui->m_txtApplicationName->lineEdit(), m_ui->m_txtApplicationDescription);
83 setTabOrder(m_ui->m_txtApplicationDescription, m_ui->m_txtAuthorName->lineEdit());
84 setTabOrder(m_ui->m_txtAuthorName->lineEdit(), m_ui->m_txtAuthorEmail->lineEdit());
85 setTabOrder(m_ui->m_txtAuthorEmail->lineEdit(), m_ui->m_cmbCategory);
89 checkApplicationName(QString());
90 checkAuthorName(QString());
91 checkAuthorEmail(QString());
92 checkApplicationIcon(QString());
95 FormUploadBundle::~FormUploadBundle() {
99 void FormUploadBundle::checkAuthorName(
const QString &author_name) {
100 bool input_ok = !author_name.simplified().isEmpty();
101 m_ui->m_txtAuthorName->setStatus(input_ok ?
102 WidgetWithStatus::Ok :
103 WidgetWithStatus::Error,
105 tr(
"Your name is okay.") :
106 tr(
"Enter valid name."));
108 emit metadataChanged();
111 void FormUploadBundle::checkAuthorEmail(
const QString &author_email) {
112 bool input_ok = QRegExp(
"^\\S+@\\S+\\.\\S{2,3}$").exactMatch(author_email);
113 m_ui->m_txtAuthorEmail->setStatus(input_ok ?
114 WidgetWithStatus::Ok :
115 WidgetWithStatus::Error,
117 tr(
"Your e-mail is okay.") :
118 tr(
"Enter valid e-mail in the form \"john@doe.com\"."));
120 emit metadataChanged();
123 void FormUploadBundle::checkApplicationName(
const QString &application_name) {
124 bool input_ok = !application_name.simplified().isEmpty();
125 m_ui->m_txtApplicationName->setStatus(input_ok ?
126 WidgetWithStatus::Ok :
127 WidgetWithStatus::Error,
129 tr(
"Your application name is okay.") :
130 tr(
"Enter valid application name."));
132 emit metadataChanged();
135 void FormUploadBundle::checkMetadata() {
136 m_btnUpload->setEnabled(m_ui->m_txtApplicationName->status() == WidgetWithStatus::Ok &&
137 m_ui->m_txtAuthorEmail->status() == WidgetWithStatus::Ok &&
138 m_ui->m_txtAuthorName->status() == WidgetWithStatus::Ok &&
139 m_ui->m_lblIcon->status() == WidgetWithStatus::Ok);
141 if (m_btnUpload->isEnabled()) {
142 m_ui->m_lblProgress->setStatus(WidgetWithStatus::Ok,
143 tr(
"Ready to upload."),
144 tr(
"Ready to upload."));
147 m_ui->m_lblProgress->setStatus(WidgetWithStatus::Error,
148 tr(
"Fill-in missing metadata."),
149 tr(
"In order to continue, fill-in missing metadata."));
153 void FormUploadBundle::selectApplicationIcon() {
154 QString selected_picture = QFileDialog::getOpenFileName(
this, tr(
"Select icon for application"),
155 m_ui->m_lblIcon->label()->toolTip().isEmpty() ?
157 m_ui->m_lblIcon->label()->toolTip(),
158 tr(
"Icons (*.jpg *.png)"));
160 checkApplicationIcon(selected_picture);
163 void FormUploadBundle::checkApplicationIcon(
const QString &icon_path) {
164 if (!icon_path.isEmpty()) {
165 m_ui->m_lblIcon->setStatus(WidgetWithStatus::Ok,
166 tr(
"Correct icon is selected."),
167 QDir::toNativeSeparators(icon_path));
170 m_ui->m_lblIcon->setStatus(WidgetWithStatus::Error,
171 tr(
"No valid icon is selected."),
172 QDir::toNativeSeparators(icon_path));
175 emit metadataChanged();
178 void FormUploadBundle::startUpload() {
180 QString xml_bundle_data = qApp->templateManager()->activeCore()->editor()->generateBundleData();
182 if (xml_bundle_data.isEmpty()) {
183 m_ui->m_lblProgress->setStatus(WidgetWithStatus::Error,
184 tr(
"Cannot upload application."),
185 tr(
"Application cannot be uploaded because template return error.\nContact application developers to fix this issue."));
189 if (m_uploader == NULL) {
192 connect(m_uploader, SIGNAL(progress(qint64,qint64)),
this, SLOT(uploadProgress(qint64,qint64)));
193 connect(m_uploader, SIGNAL(completed(QNetworkReply::NetworkError,QByteArray)),
this,
194 SLOT(uploadCompleted(QNetworkReply::NetworkError,QByteArray)));
198 m_btnClose->setEnabled(
false);
199 m_btnUpload->setEnabled(
false);
207 if (endpoint_status != QNetworkReply::NoError) {
209 m_btnClose->setEnabled(
true);
210 m_btnUpload->setEnabled(
true);
211 m_ui->m_lblProgress->setStatus(WidgetWithStatus::Error,
212 tr(
"Endpoint was not obtained."),
213 tr(
"Endpoint was not obtained."));
218 m_uploader->
uploadBundleFile(QString(endpoint), xml_bundle_data, STORE_API_KEY,
219 m_ui->m_txtAuthorName->lineEdit()->text(),
220 m_ui->m_txtAuthorEmail->lineEdit()->text(),
221 m_ui->m_txtApplicationName->lineEdit()->text(),
222 m_ui->m_lblIcon->label()->toolTip());
223 m_ui->m_lblProgress->setStatus(WidgetWithStatus::Information,
224 tr(
"Uploading application..."),
225 tr(
"Uploading application..."));
228 void FormUploadBundle::uploadProgress(qint64 bytes_sent, qint64 bytes_total) {
229 m_ui->m_progressUpload->setValue(bytes_sent * 100.0 / bytes_total);
230 qApp->processEvents();
233 void FormUploadBundle::uploadCompleted(QNetworkReply::NetworkError error, QByteArray output) {
234 qDebug(qPrintable(QString(output)));
239 if (m_uploadStatus == StoreFactory::Success) {
243 m_ui->m_lblProgress->setStatus(WidgetWithStatus::Error,
248 m_btnClose->setEnabled(
true);
249 m_btnUpload->setEnabled(
true);
253 return m_uploadStatus;
Simple file downloader with progress reporting.
UploadStatus
Possible states of application upload process.
Main BuildmLearn Store functionality.
static QNetworkReply::NetworkError downloadFile(const QString &url, int timeout, QByteArray &output, bool protected_contents=false, const QString &username=QString(), const QString &password=QString())
Performs SYNCHRONOUS download of file with given URL and given timeout.
static UploadStatus parseResponseXml(QNetworkReply::NetworkError error_status, const QByteArray &response)
Parses received XML from BuildmLearn Store server.
void uploadBundleFile(QString url, const QString &bundle_data, const QString &key, const QString &author_name, const QString &author_email, const QString &application_name, const QString &application_icon)
Uploads given bundle_data to store server via HTTP POST.
static QString uploadStatusToString(UploadStatus status)
Converts UploadStatus enumeration to textual representation.
static IconFactory * instance()
Singleton getter.