swift
autopublishcomponent.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2019 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "autopublishcomponent.h"
5 
6 #include <QDateTime>
7 #include <QDialog>
8 #include <QPointer>
9 #include <QPushButton>
10 #include <QTimer>
11 
12 #include "ui_autopublishcomponent.h"
13 
14 #include "core/db/databasewriter.h"
15 #include "core/webdataservices.h"
16 #include "gui/guiapplication.h"
17 #include "gui/guiutility.h"
19 
20 using namespace swift::misc;
21 using namespace swift::misc::simulation;
22 using namespace swift::core::db;
23 
24 namespace swift::gui::components
25 {
26  CAutoPublishComponent::CAutoPublishComponent(QWidget *parent)
27  : COverlayMessagesFrame(parent), ui(new Ui::CAutoPublishComponent)
28  {
29  ui->setupUi(this);
30  connect(ui->pb_Analyze, &QPushButton::released, this, &CAutoPublishComponent::analyzeAgainstDBData,
31  Qt::QueuedConnection);
32  connect(ui->pb_SendToDB, &QPushButton::released, this, &CAutoPublishComponent::sendToDb, Qt::QueuedConnection);
33  connect(ui->pb_DeleteFiles, &QPushButton::released, this, &CAutoPublishComponent::deleteAllFiles,
34  Qt::QueuedConnection);
35 
37  {
39  connect(w, &CDatabaseWriter::autoPublished, this, &CAutoPublishComponent::onAutoPublished,
40  Qt::QueuedConnection);
41  }
42  }
43 
45 
47  {
48  const int r = m_data.readFromJsonFiles();
49  this->displayData();
50  return r;
51  }
52 
53  bool CAutoPublishComponent::isEmpty() const { return m_data.isEmpty(); }
54 
55  void CAutoPublishComponent::analyzeAgainstDBData()
56  {
57  if (!sGui || sGui->isShuttingDown()) { return; }
58  const CAircraftModelList dbModels = sGui->getWebDataServices()->getModels();
59  const CStatusMessageList msgs = m_data.analyzeAgainstDBData(dbModels);
60  this->showOverlayMessages(msgs);
61  }
62 
63  void CAutoPublishComponent::sendToDb()
64  {
65  using namespace std::chrono_literals;
66 
67  if (!sGui || sGui->isShuttingDown()) { return; }
68  if (m_data.isEmpty())
69  {
70  this->showOverlayHTMLMessage("No data!", 5s);
71  return;
72  }
73 
74  if (!sGui->hasWebDataServices())
75  {
76  this->showOverlayHTMLMessage("No publishing web service!", 5s);
77  return;
78  }
79 
80  const CAircraftModelList dbModels = sGui->getWebDataServices()->getModels();
81  CStatusMessageList msgs = m_data.analyzeAgainstDBData(dbModels);
82  if (!msgs.hasErrorMessages())
83  {
84  const CStatusMessageList publishMsgs = sGui->getWebDataServices()->asyncAutoPublish(m_data);
85  msgs.push_back(publishMsgs);
86  }
87  this->showOverlayMessages(msgs);
88  }
89 
90  void CAutoPublishComponent::displayData()
91  {
92  ui->pte_Json->setPlainText(m_data.toDatabaseJson());
93  ui->le_Summary->setText(m_data.getSummary());
94  }
95 
96  void CAutoPublishComponent::deleteAllFiles()
97  {
98  const int c = CAutoPublishData::deleteAutoPublishFiles();
99  if (c > 0) { this->showOverlayHTMLMessage(QStringLiteral("Deleted %1 file(s)").arg(c)); }
100  this->readFiles();
101  }
102 
103  void CAutoPublishComponent::onAutoPublished(bool success, const QString &url, const CStatusMessageList &msgs)
104  {
105  using namespace std::chrono_literals;
106 
107  Q_UNUSED(url)
108  Q_UNUSED(success)
109 
110  if (success)
111  {
112  QPointer<CAutoPublishComponent> myself(this);
113  this->showOverlayMessagesWithConfirmation(msgs, true, "Clean up auto publish files?", [=] {
114  if (!myself) { return; }
115  const auto timeout = 5000ms;
116  myself->deleteAllFiles();
117  myself->showOverlayHTMLMessage("Cleaned auto publish files after uploading them to DB", timeout);
118 
119  QTimer::singleShot(timeout * 2, this, [=] {
120  if (!myself) { return; }
121  myself->closeParentDialog();
122  });
123  });
124  m_lastAutoPublish.set(QDateTime::currentMSecsSinceEpoch());
125  }
126  else { this->showOverlayMessages(msgs, true); }
127  }
128 
129  void CAutoPublishComponent::closeParentDialog()
130  {
131  QDialog *d = CGuiUtility::findParentDialog(this);
132  if (d) { d->close(); }
133  }
134 
135 } // namespace swift::gui::components
bool hasWebDataServices() const
Web data services available?
bool isShuttingDown() const
Is application shutting down?
CWebDataServices * getWebDataServices() const
Get the web data services.
db::CDatabaseWriter * getDatabaseWriter() const
DB writer class.
swift::misc::CStatusMessageList asyncAutoPublish(const swift::misc::simulation::CAutoPublishData &data) const
Auto publish to database.
swift::misc::simulation::CAircraftModelList getModels() const
Models.
Write to the swift DB.
static QDialog * findParentDialog(QWidget *widget)
Find parent dialog if there is any, otherwise null.
Definition: guiutility.cpp:868
bool showOverlayHTMLMessage(const QString &htmlMessage, std::chrono::milliseconds timeout=std::chrono::milliseconds(0))
HTML message.
void showOverlayMessages(const swift::misc::CStatusMessageList &messages, bool appendOldMessages=false, std::chrono::milliseconds timeout=std::chrono::milliseconds(0))
Show multiple messages.
void showOverlayMessagesWithConfirmation(const swift::misc::CStatusMessageList &messages, bool appendOldMessages, const QString &confirmationMessage, std::function< void()> okLambda, QMessageBox::StandardButton defaultButton=QMessageBox::Cancel, std::chrono::milliseconds timeout=std::chrono::milliseconds(0))
Show multiple messages with confirmation bar.
Using this class provides a QFrame with the overlay functionality already integrated.
Data automatically collected and be be sent to backend.
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition: sequence.h:305
Status messages, e.g. from Core -> GUI.
bool hasErrorMessages() const
Error messages.
Value object encapsulating a list of aircraft models.
CStatusMessageList analyzeAgainstDBData(const CAircraftModelList &dbModels)
Analyze against DB data.
int readFromJsonFiles(const QString &dirPath=CSwiftDirectories::logDirectory())
Read all JSON files matching the base name.
QString toDatabaseJson() const
Simple database JSON.
SWIFT_GUI_EXPORT swift::gui::CGuiApplication * sGui
Single instance of GUI application object.
Classes interacting with the swift database (aka "datastore").
High level reusable GUI components.
Definition: aboutdialog.cpp:13
Free functions in swift::misc.
auto singleShot(int msec, QObject *target, F &&task)
Starts a single-shot timer which will call a task in the thread of the given object when it times out...
Definition: threadutils.h:30