| 
									
										
										
										
											2017-04-27 20:26:05 +02:00
										 |  |  | // SPDX-License-Identifier: GPL-2.0
 | 
					
						
							| 
									
										
										
										
											2016-04-04 22:02:03 -07:00
										 |  |  | #include "desktop-widgets/diveshareexportdialog.h"
 | 
					
						
							| 
									
										
										
										
											2014-09-21 16:11:58 +02:00
										 |  |  | #include "ui_diveshareexportdialog.h"
 | 
					
						
							| 
									
										
										
										
											2016-04-04 22:02:03 -07:00
										 |  |  | #include "desktop-widgets/mainwindow.h"
 | 
					
						
							|  |  |  | #include "core/save-html.h"
 | 
					
						
							|  |  |  | #include "desktop-widgets/subsurfacewebservices.h"
 | 
					
						
							| 
									
										
										
										
											2018-06-03 22:15:19 +02:00
										 |  |  | #include "core/qthelper.h"
 | 
					
						
							| 
									
										
										
										
											2016-04-04 22:02:03 -07:00
										 |  |  | #include "core/cloudstorage.h"
 | 
					
						
							| 
									
										
										
										
											2019-12-10 14:39:53 +01:00
										 |  |  | #include "core/uploadDiveShare.h"
 | 
					
						
							| 
									
										
										
										
											2019-12-08 16:47:23 +01:00
										 |  |  | #include "core/settings/qPrefCloudStorage.h"
 | 
					
						
							| 
									
										
										
										
											2014-09-21 16:11:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <QDesktopServices>
 | 
					
						
							| 
									
										
										
										
											2019-12-08 16:47:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-21 16:11:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | DiveShareExportDialog::DiveShareExportDialog(QWidget *parent) : | 
					
						
							|  |  |  | 	QDialog(parent), | 
					
						
							|  |  |  | 	ui(new Ui::DiveShareExportDialog), | 
					
						
							| 
									
										
										
										
											2019-12-10 15:36:31 +01:00
										 |  |  | 	exportSelected(false) | 
					
						
							| 
									
										
										
										
											2014-09-21 16:11:58 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	ui->setupUi(this); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DiveShareExportDialog::~DiveShareExportDialog() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	delete ui; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DiveShareExportDialog::UIDFromBrowser() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QDesktopServices::openUrl(QUrl(DIVESHARE_BASE_URI "/secret")); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DiveShareExportDialog *DiveShareExportDialog::instance() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	static DiveShareExportDialog *self = new DiveShareExportDialog(MainWindow::instance()); | 
					
						
							|  |  |  | 	self->ui->txtResult->setHtml(""); | 
					
						
							|  |  |  | 	self->ui->buttonBox->setStandardButtons(QDialogButtonBox::Cancel); | 
					
						
							|  |  |  | 	return self; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DiveShareExportDialog::prepareDivesForUpload(bool selected) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	exportSelected = selected; | 
					
						
							|  |  |  | 	ui->frameConfigure->setVisible(true); | 
					
						
							|  |  |  | 	ui->frameResults->setVisible(false); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-08 16:47:23 +01:00
										 |  |  | 	ui->txtUID->setText(qPrefCloudStorage::diveshare_uid()); | 
					
						
							|  |  |  | 	ui->chkPrivate->setChecked(qPrefCloudStorage::diveshare_private()); | 
					
						
							| 
									
										
										
										
											2014-09-21 16:11:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	show(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static QByteArray generate_html_list(const QByteArray &data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QList<QByteArray> dives = data.split('\n'); | 
					
						
							|  |  |  | 	QByteArray html; | 
					
						
							|  |  |  | 	html.append("<html><body><table>"); | 
					
						
							|  |  |  | 	for (int i = 0; i < dives.length(); i++ ) { | 
					
						
							|  |  |  | 		html.append("<tr>"); | 
					
						
							|  |  |  | 		QList<QByteArray> dive_details = dives[i].split(','); | 
					
						
							|  |  |  | 		if (dive_details.length() < 3) | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		QByteArray dive_id = dive_details[0]; | 
					
						
							|  |  |  | 		QByteArray dive_delete = dive_details[1]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		html.append("<td>"); | 
					
						
							|  |  |  | 		html.append("<a href=\"" DIVESHARE_BASE_URI "/dive/" + dive_id + "\">"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		//Title gets separated too, this puts it back together
 | 
					
						
							|  |  |  | 		const char *sep = ""; | 
					
						
							|  |  |  | 		for (int t = 2; t < dive_details.length(); t++) { | 
					
						
							|  |  |  | 			html.append(sep); | 
					
						
							|  |  |  | 			html.append(dive_details[t]); | 
					
						
							|  |  |  | 			sep = ","; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		html.append("</a>"); | 
					
						
							|  |  |  | 		html.append("</td>"); | 
					
						
							|  |  |  | 		html.append("<td>"); | 
					
						
							|  |  |  | 		html.append("<a href=\"" DIVESHARE_BASE_URI "/delete/dive/" + dive_delete + "\">Delete dive</a>"); | 
					
						
							|  |  |  | 		html.append("</td>"  ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		html.append("</tr>"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	html.append("</table></body></html>"); | 
					
						
							|  |  |  | 	return html; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-10 15:36:31 +01:00
										 |  |  | void DiveShareExportDialog::finishedSlot(bool isOk, const QString &text, const QByteArray &html) | 
					
						
							| 
									
										
										
										
											2014-09-21 16:11:58 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	ui->progressBar->setVisible(false); | 
					
						
							| 
									
										
										
										
											2019-12-10 15:36:31 +01:00
										 |  |  | 	if (!isOk) { | 
					
						
							| 
									
										
										
										
											2014-09-21 16:11:58 +02:00
										 |  |  | 		ui->buttonBox->setStandardButtons(QDialogButtonBox::Cancel); | 
					
						
							| 
									
										
										
										
											2019-12-10 15:36:31 +01:00
										 |  |  | 		ui->txtResult->setText(text); | 
					
						
							| 
									
										
										
										
											2014-09-21 16:11:58 +02:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		ui->buttonBox->setStandardButtons(QDialogButtonBox::Ok); | 
					
						
							| 
									
										
										
										
											2019-12-10 15:36:31 +01:00
										 |  |  | 		ui->txtResult->setHtml(generate_html_list(html)); | 
					
						
							| 
									
										
										
										
											2014-09-21 16:11:58 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DiveShareExportDialog::doUpload() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	//Store current settings
 | 
					
						
							| 
									
										
										
										
											2019-12-10 14:39:53 +01:00
										 |  |  | 	QString uid = ui->txtUID->text(); | 
					
						
							|  |  |  | 	bool noPublic = ui->chkPrivate->isChecked(); | 
					
						
							|  |  |  | 	qPrefCloudStorage::set_diveshare_uid(uid); | 
					
						
							|  |  |  | 	qPrefCloudStorage::set_diveshare_private(noPublic); | 
					
						
							| 
									
										
										
										
											2014-09-21 16:11:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	//Change UI into results mode
 | 
					
						
							|  |  |  | 	ui->frameConfigure->setVisible(false); | 
					
						
							|  |  |  | 	ui->frameResults->setVisible(true); | 
					
						
							|  |  |  | 	ui->progressBar->setVisible(true); | 
					
						
							|  |  |  | 	ui->progressBar->setRange(0, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-10 14:39:53 +01:00
										 |  |  | 	uploadDiveShare::instance()->doUpload(exportSelected, uid, noPublic); | 
					
						
							| 
									
										
										
										
											2019-12-10 15:36:31 +01:00
										 |  |  | 	connect(uploadDiveShare::instance(), SIGNAL(uploadFinish(bool, const QString &, const QByteArray &)), | 
					
						
							|  |  |  | 			this, SLOT(finishedSlot(bool, const QString &, const QByteArray &))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Not implemented in the UI, but would be nice to have
 | 
					
						
							|  |  |  | 	//connect(uploadDiveLogsDE::instance(), SIGNAL(uploadProgress(qreal, qreal)),
 | 
					
						
							|  |  |  | 	//		this, SLOT(updateProgress(qreal, qreal)));
 | 
					
						
							|  |  |  | 	//connect(uploadDiveLogsDE::instance(), SIGNAL(uploadStatus(const QString &)),
 | 
					
						
							|  |  |  | 	//		this, SLOT(uploadStatus(const QString &)));
 | 
					
						
							| 
									
										
										
										
											2014-09-21 16:11:58 +02:00
										 |  |  | } |