summaryrefslogtreecommitdiffstats
path: root/desktop-widgets/diveshareexportdialog.cpp
blob: 7d4c156aad08b4bc3fe26314a3295a94661b7a38 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// SPDX-License-Identifier: GPL-2.0
#include "desktop-widgets/diveshareexportdialog.h"
#include "ui_diveshareexportdialog.h"
#include "desktop-widgets/mainwindow.h"
#include "core/save-html.h"
#include "desktop-widgets/subsurfacewebservices.h"
#include "core/helpers.h"
#include "core/cloudstorage.h"

#include <QDesktopServices>
#include <QSettings>

DiveShareExportDialog::DiveShareExportDialog(QWidget *parent) :
	QDialog(parent),
	ui(new Ui::DiveShareExportDialog),
	exportSelected(false),
	reply(NULL)
{
	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->setAttribute(Qt::WA_QuitOnClose, false);
	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);

	QSettings settings;
	if (settings.contains("diveshareExport/uid"))
		ui->txtUID->setText(settings.value("diveshareExport/uid").toString());

	if (settings.contains("diveshareExport/private"))
		ui->chkPrivate->setChecked(settings.value("diveshareExport/private").toBool());

	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;
}

void DiveShareExportDialog::finishedSlot()
{
	ui->progressBar->setVisible(false);
	if (reply->error() != 0) {
		ui->buttonBox->setStandardButtons(QDialogButtonBox::Cancel);
		ui->txtResult->setText(reply->errorString());
	} else {
		ui->buttonBox->setStandardButtons(QDialogButtonBox::Ok);
		ui->txtResult->setHtml(generate_html_list(reply->readAll()));
	}

	reply->deleteLater();
}

void DiveShareExportDialog::doUpload()
{
	//Store current settings
	QSettings settings;
	settings.setValue("diveshareExport/uid", ui->txtUID->text());
	settings.setValue("diveshareExport/private", ui->chkPrivate->isChecked());

	//Change UI into results mode
	ui->frameConfigure->setVisible(false);
	ui->frameResults->setVisible(true);
	ui->progressBar->setVisible(true);
	ui->progressBar->setRange(0, 0);

	//generate json
	struct membuffer buf = {};
	export_list(&buf, NULL, exportSelected, false);
	QByteArray json_data(buf.buffer, buf.len);
	free_buffer(&buf);

	//Request to server
	QNetworkRequest request;

	if (ui->chkPrivate->isChecked())
		request.setUrl(QUrl(DIVESHARE_BASE_URI "/upload?private=true"));
	else
		request.setUrl(QUrl(DIVESHARE_BASE_URI "/upload"));

	request.setRawHeader("User-Agent", getUserAgent().toUtf8());
	if (ui->txtUID->text().length() != 0)
		request.setRawHeader("X-UID", ui->txtUID->text().toUtf8());

	reply = manager()->put(request, json_data);

	QObject::connect(reply, SIGNAL(finished()), this, SLOT(finishedSlot()));
}