PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/desktop-widgets/diveshareexportdialog.cpp

http://github.com/torvalds/subsurface
C++ | 142 lines | 110 code | 26 blank | 6 comment | 11 complexity | cd12b65e4c4f75ad5054bddec8973d3b MD5 | raw file
Possible License(s): GPL-2.0
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "desktop-widgets/diveshareexportdialog.h"
  3. #include "ui_diveshareexportdialog.h"
  4. #include "desktop-widgets/mainwindow.h"
  5. #include "core/save-html.h"
  6. #include "desktop-widgets/subsurfacewebservices.h"
  7. #include "core/qthelper.h"
  8. #include "core/cloudstorage.h"
  9. #include <QDesktopServices>
  10. #include <QSettings>
  11. DiveShareExportDialog::DiveShareExportDialog(QWidget *parent) :
  12. QDialog(parent),
  13. ui(new Ui::DiveShareExportDialog),
  14. exportSelected(false),
  15. reply(NULL)
  16. {
  17. ui->setupUi(this);
  18. }
  19. DiveShareExportDialog::~DiveShareExportDialog()
  20. {
  21. delete ui;
  22. }
  23. void DiveShareExportDialog::UIDFromBrowser()
  24. {
  25. QDesktopServices::openUrl(QUrl(DIVESHARE_BASE_URI "/secret"));
  26. }
  27. DiveShareExportDialog *DiveShareExportDialog::instance()
  28. {
  29. static DiveShareExportDialog *self = new DiveShareExportDialog(MainWindow::instance());
  30. self->ui->txtResult->setHtml("");
  31. self->ui->buttonBox->setStandardButtons(QDialogButtonBox::Cancel);
  32. return self;
  33. }
  34. void DiveShareExportDialog::prepareDivesForUpload(bool selected)
  35. {
  36. exportSelected = selected;
  37. ui->frameConfigure->setVisible(true);
  38. ui->frameResults->setVisible(false);
  39. QSettings settings;
  40. if (settings.contains("diveshareExport/uid"))
  41. ui->txtUID->setText(settings.value("diveshareExport/uid").toString());
  42. if (settings.contains("diveshareExport/private"))
  43. ui->chkPrivate->setChecked(settings.value("diveshareExport/private").toBool());
  44. show();
  45. }
  46. static QByteArray generate_html_list(const QByteArray &data)
  47. {
  48. QList<QByteArray> dives = data.split('\n');
  49. QByteArray html;
  50. html.append("<html><body><table>");
  51. for (int i = 0; i < dives.length(); i++ ) {
  52. html.append("<tr>");
  53. QList<QByteArray> dive_details = dives[i].split(',');
  54. if (dive_details.length() < 3)
  55. continue;
  56. QByteArray dive_id = dive_details[0];
  57. QByteArray dive_delete = dive_details[1];
  58. html.append("<td>");
  59. html.append("<a href=\"" DIVESHARE_BASE_URI "/dive/" + dive_id + "\">");
  60. //Title gets separated too, this puts it back together
  61. const char *sep = "";
  62. for (int t = 2; t < dive_details.length(); t++) {
  63. html.append(sep);
  64. html.append(dive_details[t]);
  65. sep = ",";
  66. }
  67. html.append("</a>");
  68. html.append("</td>");
  69. html.append("<td>");
  70. html.append("<a href=\"" DIVESHARE_BASE_URI "/delete/dive/" + dive_delete + "\">Delete dive</a>");
  71. html.append("</td>" );
  72. html.append("</tr>");
  73. }
  74. html.append("</table></body></html>");
  75. return html;
  76. }
  77. void DiveShareExportDialog::finishedSlot()
  78. {
  79. ui->progressBar->setVisible(false);
  80. if (reply->error() != 0) {
  81. ui->buttonBox->setStandardButtons(QDialogButtonBox::Cancel);
  82. ui->txtResult->setText(reply->errorString());
  83. } else {
  84. ui->buttonBox->setStandardButtons(QDialogButtonBox::Ok);
  85. ui->txtResult->setHtml(generate_html_list(reply->readAll()));
  86. }
  87. reply->deleteLater();
  88. }
  89. void DiveShareExportDialog::doUpload()
  90. {
  91. //Store current settings
  92. QSettings settings;
  93. settings.setValue("diveshareExport/uid", ui->txtUID->text());
  94. settings.setValue("diveshareExport/private", ui->chkPrivate->isChecked());
  95. //Change UI into results mode
  96. ui->frameConfigure->setVisible(false);
  97. ui->frameResults->setVisible(true);
  98. ui->progressBar->setVisible(true);
  99. ui->progressBar->setRange(0, 0);
  100. //generate json
  101. struct membuffer buf = {};
  102. export_list(&buf, NULL, exportSelected, false);
  103. QByteArray json_data(buf.buffer, buf.len);
  104. free_buffer(&buf);
  105. //Request to server
  106. QNetworkRequest request;
  107. if (ui->chkPrivate->isChecked())
  108. request.setUrl(QUrl(DIVESHARE_BASE_URI "/upload?private=true"));
  109. else
  110. request.setUrl(QUrl(DIVESHARE_BASE_URI "/upload"));
  111. request.setRawHeader("User-Agent", getUserAgent().toUtf8());
  112. if (ui->txtUID->text().length() != 0)
  113. request.setRawHeader("X-UID", ui->txtUID->text().toUtf8());
  114. reply = manager()->put(request, json_data);
  115. QObject::connect(reply, SIGNAL(finished()), this, SLOT(finishedSlot()));
  116. }