qTbot 0.89.ee6949a
qTbot is base back end library for your c++ Qt projects.
ibot.cpp
Go to the documentation of this file.
1//#
2//# Copyright (C) 2023-2024 QuasarApp.
3//# Distributed under the GPLv3 software license, see the accompanying
4//# Everyone is permitted to copy and distribute verbatim copies
5//# of this license document, but changing it is not allowed.
6//#
7
8#include "ibot.h"
9#include "qstandardpaths.h"
10
11#include <QNetworkReply>
12
13namespace qTbot {
14
16 _manager = new QNetworkAccessManager();
17 _manager->setAutoDeleteReplies(false);
18}
19
21 delete _manager;
22}
23
25 setToken({});
26}
27
28const QByteArray &IBot::token() const {
29 return _token;
30}
31
32void IBot::setToken(const QByteArray &newToken) {
33 _token = newToken;
34}
35
36void IBot::incomeNewUpdate(const QSharedPointer<iUpdate> &message) {
37 if (!message->isValid())
38 return;
39
40 auto id = message->updateId();
41
42 if (!_processed.contains(id)) {
43
44 _processed.insert(id);
45 _notProcessedUpdates[id] = message;
46
47 handleIncomeNewUpdate(message);
48 }
49}
50
51QSharedPointer<QNetworkReply>
52IBot::sendRequest(const QSharedPointer<iRequest> &rquest) {
53 if (!rquest)
54 return nullptr;
55
56 doRemoveFinishedRequests();
57
58 auto && url = makeUrl(rquest);
59
60#ifdef QTBOT_PRINT_RQUESTS
61 qDebug() << url;
62#endif
63
64 QSharedPointer<QNetworkReply> networkReplay;
65 QSharedPointer<QHttpMultiPart> httpData;
66
67 switch (rquest->method()) {
68 case iRequest::Get: {
69 auto reply = _manager->get(QNetworkRequest(url));
70
71 // we control replay object wia shared pointers.
72 reply->setParent(nullptr);
73
74 networkReplay.reset(reply);
75 break;
76 }
77
78 case iRequest::Post:
79// req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
80// reply = m_nam.post(req, params.toByteArray());
81
82// break;
84 QNetworkRequest netRequest(url);
85
86 httpData = rquest->argsToMultipartFormData();
87 if (httpData) {
88 auto reply = _manager->post(netRequest, httpData.data());
89
90 // we control replay object wia shared pointers.
91 reply->setParent(nullptr);
92
93 networkReplay.reset(reply);
94 } else {
95 return nullptr;
96 }
97
98 break;
99 }
100
101 size_t address = reinterpret_cast<size_t>(networkReplay.get());
102 _replayStorage[address] = networkReplay;
103
104 connect(networkReplay.get(), &QNetworkReply::finished, this,
105 [this, address, httpData]() {
106 _toRemove.push_back(address);
107 });
108
109 connect(networkReplay.get(), &QNetworkReply::errorOccurred, this,
110 [this, address](QNetworkReply::NetworkError err){
111 qWarning() << "The reqeust " << address << " finished with error code : " << err;
112 if (auto&& replay = _replayStorage.value(address)) {
113 qWarning() << replay->errorString();
114 }
115
116 _toRemove.push_back(address);
117 });
118
119 return networkReplay;
120}
121
122void IBot::markUpdateAsProcessed(const QSharedPointer<iUpdate> &message) {
123 _notProcessedUpdates.remove(message->updateId());
124}
125
126void IBot::markUpdateAsUnprocessed(const QSharedPointer<iUpdate> &message) {
127 return markUpdateAsUnprocessed(message->updateId());
128}
129
130void IBot::markUpdateAsUnprocessed(unsigned long long messageID) {
131 _processed.remove(messageID);
132}
133
134QString IBot::defaultFileStorageLocation() const {
135 return QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
136}
137
138void IBot::handleIncomeNewUpdate(const QSharedPointer<iUpdate> & message) {
139 emit sigReceiveUpdate(message);
140}
141
142void IBot::doRemoveFinishedRequests() {
143 for (auto address: std::as_const(_toRemove)) {
144 _replayStorage.remove(address);
145 }
146
147 _toRemove.clear();
148}
149
150QSet<unsigned long long> IBot::processed() const {
151 return _processed;
152}
153
154void IBot::setProcessed(const QSet<unsigned long long> &newProcessed) {
155 _processed = newProcessed;
156}
157
158const QString &IBot::name() const {
159 return _name;
160}
161
162void IBot::setName(const QString &newName) {
163 _name = newName;
164}
165
166QSharedPointer<iUpdate> IBot::takeNextUnreadUpdate() {
167 if (_notProcessedUpdates.size()) {
168 auto toRemove = std::move(*_notProcessedUpdates.begin());
169 _notProcessedUpdates.erase(_notProcessedUpdates.cbegin());
170 return toRemove;
171 }
172
173 return nullptr;
174}
175
176
177}
virtual QString makeUrl(const QSharedPointer< iRequest > &request) const =0
makeUrl This method prepare a prefix url for http requests.
const QByteArray & token() const
token This is token value for authication on the remote server (bot)
Definition ibot.cpp:28
virtual void handleIncomeNewUpdate(const QSharedPointer< iUpdate > &)
handleIncomeNewUpdate This method just emit the sigReceiveUpdate signal.
Definition ibot.cpp:138
void incomeNewUpdate(const QSharedPointer< iUpdate > &message)
incomeNewUpdate This method save incomed messages into store.
Definition ibot.cpp:36
void setToken(const QByteArray &newToken)
setToken This is setter of the IBot::token value.
Definition ibot.cpp:32
virtual void logout()
login This method remove login token of bot.
Definition ibot.cpp:24
QSharedPointer< QNetworkReply > sendRequest(const QSharedPointer< iRequest > &rquest)
sendRequest This method sent custom requests to the server.
Definition ibot.cpp:52
@ Post
general post request
Definition irequest.h:44
@ Upload
this is post request to upload a big data to telegram
Definition irequest.h:46
@ Get
general ger request, all request data sent as a url line
Definition irequest.h:42