/
mainwindow.cpp
85 lines (81 loc) · 2.72 KB
/
mainwindow.cpp
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
// local
#include "mainwindow.h"
#include "ui_mainwindow.h"
// qt
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
namespace constants
{
const QString BASE_URL = "http://127.0.0.1:3000";
int STATUS_BAR_TIMEOUT = 5000;
int MAX_CONTENT_LENGTH = 1000;
namespace json_keys
{
const QString SUMMARY_KEY = "summary";
} // json_keys namespace
} // constants namespace
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, m_apiHandler(std::make_unique<APIHandler>(constants::BASE_URL))
{
ui->setupUi(this);
// Get Files
auto reply = m_apiHandler->GetFilesList();
connect(reply, &QNetworkReply::finished, this, [reply, this]
{
if(reply->error() == QNetworkReply::NoError)
{
auto data = reply->readAll();
QJsonDocument doc = QJsonDocument::fromJson(data);
QJsonObject obj = doc.object();
for(auto &&key: obj.keys()) {
auto summary = obj.value(key).toObject()[constants::json_keys::SUMMARY_KEY].toString().toStdString();
ui->filesComboBox->addItem(key);
m_summaries.push_back(summary);
}
connect(ui->filesComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), [=](int index)
{
ui->fileSummaryLabel->setText(QString::fromStdString(m_summaries[index]));
});
}
else
{
ui->statusbar->showMessage(reply->errorString(), constants::STATUS_BAR_TIMEOUT);
}
if(!m_summaries.empty())
{
ui->fileSummaryLabel->setText(QString::fromStdString(m_summaries[0]));
}
reply->deleteLater();
});
connect(ui->downloadButton, &QPushButton::clicked, this, [this]()
{
auto fileName = ui->filesComboBox->currentText();
auto reply = m_apiHandler->DownloadFile(fileName);
connect(reply, &QNetworkReply::finished, this, [this, reply]()
{
if(reply->error() == QNetworkReply::NoError)
{
auto data = reply->readAll();
// Do What ever you want with data now.
auto dataToDisplay = QString(data).left(std::min(constants::MAX_CONTENT_LENGTH, data.length()));
if(data.length() > constants::MAX_CONTENT_LENGTH)
{
dataToDisplay += "...";
}
ui->fileContent->setText(dataToDisplay);
}
else
{
ui->statusbar->showMessage(reply->errorString(), constants::STATUS_BAR_TIMEOUT);
}
reply->deleteLater();
});
});
}
MainWindow::~MainWindow()
{
delete ui;
}