PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/desktop-widgets/diveshareexportdialog.cpp

https://github.com/dirkhh/subsurface
C++ | 128 lines | 97 code | 21 blank | 10 comment | 5 complexity | 654f801a1d8cbe925a7f427de79dc461 MD5 | raw file
  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 "core/uploadDiveShare.h"
  10. #include "core/settings/qPrefCloudStorage.h"
  11. #include <QDesktopServices>
  12. DiveShareExportDialog::DiveShareExportDialog(QWidget *parent) :
  13. QDialog(parent),
  14. ui(new Ui::DiveShareExportDialog),
  15. exportSelected(false)
  16. {
  17. ui->setupUi(this);
  18. // creating this connection in the .ui file appears to fail with Qt6
  19. connect(ui->getUIDbutton, &QPushButton::clicked, this, &DiveShareExportDialog::UIDFromBrowser);
  20. }
  21. DiveShareExportDialog::~DiveShareExportDialog()
  22. {
  23. delete ui;
  24. }
  25. void DiveShareExportDialog::UIDFromBrowser()
  26. {
  27. QDesktopServices::openUrl(QUrl(DIVESHARE_BASE_URI "/secret"));
  28. }
  29. DiveShareExportDialog *DiveShareExportDialog::instance()
  30. {
  31. static DiveShareExportDialog *self = new DiveShareExportDialog(MainWindow::instance());
  32. self->ui->txtResult->setHtml("");
  33. self->ui->buttonBox->setStandardButtons(QDialogButtonBox::Cancel);
  34. return self;
  35. }
  36. void DiveShareExportDialog::prepareDivesForUpload(bool selected)
  37. {
  38. exportSelected = selected;
  39. ui->frameConfigure->setVisible(true);
  40. ui->frameResults->setVisible(false);
  41. ui->txtUID->setText(qPrefCloudStorage::diveshare_uid());
  42. ui->chkPrivate->setChecked(qPrefCloudStorage::diveshare_private());
  43. show();
  44. }
  45. static QByteArray generate_html_list(const QByteArray &data)
  46. {
  47. QList<QByteArray> dives = data.split('\n');
  48. QByteArray html;
  49. html.append("<html><body><table>");
  50. for (int i = 0; i < dives.length(); i++ ) {
  51. html.append("<tr>");
  52. QList<QByteArray> dive_details = dives[i].split(',');
  53. if (dive_details.length() < 3)
  54. continue;
  55. QByteArray dive_id = dive_details[0];
  56. QByteArray dive_delete = dive_details[1];
  57. html.append("<td>");
  58. html.append("<a href=\"" DIVESHARE_BASE_URI "/dive/" + dive_id + "\">");
  59. //Title gets separated too, this puts it back together
  60. const char *sep = "";
  61. for (int t = 2; t < dive_details.length(); t++) {
  62. html.append(sep);
  63. html.append(dive_details[t]);
  64. sep = ",";
  65. }
  66. html.append("</a>");
  67. html.append("</td>");
  68. html.append("<td>");
  69. html.append("<a href=\"" DIVESHARE_BASE_URI "/delete/dive/" + dive_delete + "\">Delete dive</a>");
  70. html.append("</td>" );
  71. html.append("</tr>");
  72. }
  73. html.append("</table></body></html>");
  74. return html;
  75. }
  76. void DiveShareExportDialog::finishedSlot(bool isOk, const QString &text, const QByteArray &html)
  77. {
  78. ui->progressBar->setVisible(false);
  79. if (!isOk) {
  80. ui->buttonBox->setStandardButtons(QDialogButtonBox::Cancel);
  81. ui->txtResult->setText(text);
  82. } else {
  83. ui->buttonBox->setStandardButtons(QDialogButtonBox::Ok);
  84. ui->txtResult->setHtml(generate_html_list(html));
  85. }
  86. }
  87. void DiveShareExportDialog::doUpload()
  88. {
  89. //Store current settings
  90. QString uid = ui->txtUID->text();
  91. bool noPublic = ui->chkPrivate->isChecked();
  92. qPrefCloudStorage::set_diveshare_uid(uid);
  93. qPrefCloudStorage::set_diveshare_private(noPublic);
  94. //Change UI into results mode
  95. ui->frameConfigure->setVisible(false);
  96. ui->frameResults->setVisible(true);
  97. ui->progressBar->setVisible(true);
  98. ui->progressBar->setRange(0, 0);
  99. uploadDiveShare::instance()->doUpload(exportSelected, uid, noPublic);
  100. connect(uploadDiveShare::instance(), SIGNAL(uploadFinish(bool, const QString &, const QByteArray &)),
  101. this, SLOT(finishedSlot(bool, const QString &, const QByteArray &)));
  102. // Not implemented in the UI, but would be nice to have
  103. //connect(uploadDiveLogsDE::instance(), SIGNAL(uploadProgress(qreal, qreal)),
  104. // this, SLOT(updateProgress(qreal, qreal)));
  105. //connect(uploadDiveLogsDE::instance(), SIGNAL(uploadStatus(const QString &)),
  106. // this, SLOT(uploadStatus(const QString &)));
  107. }