Skip to content

Commit

Permalink
QX register event
Browse files Browse the repository at this point in the history
  • Loading branch information
Fanda Vacek committed Nov 18, 2024
1 parent 0b39229 commit 03c17e2
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Event::services::qx {
//===============================================
// QxClientServiceSettings
//===============================================
QString QxClientServiceSettings::xchgKey() const
QString QxClientServiceSettings::eventKey() const
{
auto *event_plugin = getPlugin<EventPlugin>();
auto *cfg = event_plugin->eventConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class QxClientServiceSettings : public ServiceSettings
{
using Super = ServiceSettings;

QF_VARIANTMAP_FIELD(QString, e, setE, xchangeServerUrl)
//QF_VARIANTMAP_FIELD(QString, e, setE, ventPath)
//QF_VARIANTMAP_FIELD(QString, a, setA, piKey)
QF_VARIANTMAP_FIELD2(QString, e, setE, xchangeServerUrl, "http://localhost:8000")
// QF_VARIANTMAP_FIELD(QString, a, setA, dminPassword)
QF_VARIANTMAP_FIELD(QString, e, setE, ventId)
public:
QxClientServiceSettings(const QVariantMap &o = QVariantMap()) : Super(o) {}

QString xchgKey() const;
QString eventKey() const;
};

class QxClientService : public Service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
#include "ui_qxclientservicewidget.h"
#include "qxclientservice.h"

#include <plugins/Event/src/eventplugin.h>

#include <qf/qmlwidgets/framework/mainwindow.h>
#include <qf/qmlwidgets/dialogs/messagebox.h>
#include <qf/core/assert.h>

#include <QFileDialog>
#include <QUrlQuery>
#include <QClipboard>

#include <plugins/Event/src/eventplugin.h>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QJsonDocument>

namespace Event::services::qx {

Expand All @@ -24,19 +27,63 @@ QxClientServiceWidget::QxClientServiceWidget(QWidget *parent)

auto *svc = service();
if(svc) {
auto ss = svc->settings();
ui->edExchangeKey->setText(ss.xchgKey());
ui->edServerUrl->setText(ss.exchangeServerUrl());
auto settings = svc->settings();
ui->edEventKey->setText(settings.eventKey());
ui->edServerUrl->setText(settings.exchangeServerUrl());
connect(ui->btRegisterEvent, &QAbstractButton::clicked, this, [this, settings]() {
auto *manager = new QNetworkAccessManager(this);
connect(manager, &QNetworkAccessManager::finished, this, [this, manager](QNetworkReply *reply) {
if (reply->error() == QNetworkReply::NoError) {
auto code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (code == 200 || code == 201) {
// exists or created
auto event_id = QString::fromUtf8(reply->readAll());
ui->edEventId->setText(event_id);
setMessage();
}
}
else {
qfError() << reply->errorString();
setMessage(reply->errorString(), true);
}
reply->deleteLater();
// One QNetworkAccessManager instance should be enough for the whole Qt application.
manager->deleteLater();
});
auto url = QUrl(settings.exchangeServerUrl() + "/api/event");
QNetworkRequest rq(url);
rq.setRawHeader("eventKey", settings.eventKey().toUtf8());
QVariantMap data = {
{"name", "Zavody"},
{"date", QDateTime::currentDateTime().toString(Qt::ISODate)},
{"place", "Praha"},
};
manager->post(rq, QJsonDocument::fromVariant(data).toJson());
});
}
connect(ui->btRegisterEvent, &QAbstractButton::clicked, this, []() {
});
}

QxClientServiceWidget::~QxClientServiceWidget()
{
delete ui;
}

void QxClientServiceWidget::setMessage(const QString &msg, bool is_error)
{
if (msg.isEmpty()) {
ui->lblStatus->setStyleSheet({});
}
else {
if (is_error) {
ui->lblStatus->setStyleSheet("background: salmon");
}
else {
ui->lblStatus->setStyleSheet("background: lemon");
}
}
ui->lblStatus->setText(msg);
}

bool QxClientServiceWidget::acceptDialogDone(int result)
{
if(result == QDialog::Accepted) {
Expand All @@ -60,6 +107,7 @@ bool QxClientServiceWidget::saveSettings()
if(svc) {
auto ss = svc->settings();
ss.setExchangeServerUrl(ui->edServerUrl->text());
// ss.setAdminPassword(ui->edAdminPassword->text());
svc->setSettings(ss);
}
return true;
Expand All @@ -69,7 +117,7 @@ void QxClientServiceWidget::updateOCheckListPostUrl()
{
auto url = QStringLiteral("%1/api/event/%2/ochecklist")
.arg(ui->edServerUrl->text())
.arg(ui->edExchangeKey->text());
.arg(ui->edEventId->text());
ui->edOChecklistUrl->setText(url);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ class QxClientServiceWidget : public qf::qmlwidgets::framework::DialogWidget
using Super = qf::qmlwidgets::framework::DialogWidget;
public:
explicit QxClientServiceWidget(QWidget *parent = nullptr);
~QxClientServiceWidget();
~QxClientServiceWidget() override;
private:
void setMessage(const QString &msg = {}, bool is_error = false);
QxClientService* service();
bool saveSettings();
void updateOCheckListPostUrl();
private:
Ui::QxClientServiceWidget *ui;
bool acceptDialogDone(int result);
bool acceptDialogDone(int result) override;
};

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>391</width>
<height>195</height>
<height>255</height>
</rect>
</property>
<property name="windowTitle">
Expand All @@ -16,17 +16,7 @@
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="toolTip">
<string>Fill this url into HTTP POST synchonization method in O-CheckList</string>
</property>
<property name="text">
<string>OCheckList Url</string>
</property>
</widget>
</item>
<item row="2" column="1">
<item row="3" column="1">
<widget class="QLineEdit" name="edOChecklistUrl">
<property name="readOnly">
<bool>true</bool>
Expand All @@ -40,21 +30,48 @@
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="edEventId">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="1" colspan="2">
<widget class="QLineEdit" name="edServerUrl"/>
</item>
<item row="1" column="0">
<item row="3" column="0">
<widget class="QLabel" name="label_3">
<property name="toolTip">
<string>Fill this url into HTTP POST synchonization method in O-CheckList</string>
</property>
<property name="text">
<string>OCheckList Url</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_4">
<property name="toolTip">
<string>Fill this url into HTTP POST synchonization method in O-CheckList</string>
</property>
<property name="text">
<string>Exchange key</string>
<string>Event ID</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_5">
<property name="toolTip">
<string>Fill this url into HTTP POST synchonization method in O-CheckList</string>
</property>
<property name="text">
<string>Event key</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="edExchangeKey">
<widget class="QLineEdit" name="edEventKey">
<property name="readOnly">
<bool>true</bool>
</property>
Expand Down Expand Up @@ -110,7 +127,7 @@
</widget>
<tabstops>
<tabstop>edServerUrl</tabstop>
<tabstop>edExchangeKey</tabstop>
<tabstop>edEventId</tabstop>
<tabstop>edOChecklistUrl</tabstop>
<tabstop>btRegisterEvent</tabstop>
</tabstops>
Expand Down

0 comments on commit 03c17e2

Please sign in to comment.