diff --git a/snapd/devicepluginsnapd.cpp b/snapd/devicepluginsnapd.cpp index 53fdfd96..34c56dc0 100644 --- a/snapd/devicepluginsnapd.cpp +++ b/snapd/devicepluginsnapd.cpp @@ -1,6 +1,6 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * Copyright (C) 2017 Simon Stürz . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + + #include "snapdconnection.h" #include "extern-plugininfo.h" +#include #include #include @@ -15,9 +39,9 @@ SnapdConnection::SnapdConnection(QObject *parent) : connect(this, SIGNAL(error(QLocalSocket::LocalSocketError)), this, SLOT(onError(QLocalSocket::LocalSocketError))); } -SnapdReply *SnapdConnection::get(const QString &path) +SnapdReply *SnapdConnection::get(const QString &path, QObject *parent) { - SnapdReply *reply = new SnapdReply(this); + SnapdReply *reply = new SnapdReply(parent); reply->setRequestPath(path); reply->setRequestMethod("GET"); reply->setRequestRawMessage(createRequestHeader("GET", path)); @@ -30,9 +54,9 @@ SnapdReply *SnapdConnection::get(const QString &path) return reply; } -SnapdReply *SnapdConnection::post(const QString &path, const QByteArray &payload) +SnapdReply *SnapdConnection::post(const QString &path, const QByteArray &payload, QObject *parent) { - SnapdReply *reply = new SnapdReply(this); + SnapdReply *reply = new SnapdReply(parent); reply->setRequestPath(path); reply->setRequestMethod("POST"); QByteArray header = createRequestHeader("POST", path, payload); @@ -57,22 +81,28 @@ void SnapdConnection::setConnected(const bool &connected) return; m_connected = connected; + emit connectedChanged(m_connected); // Clean up replies of disconnected if (!m_connected) { - foreach (SnapdReply *reply, m_replyQueue) { - reply->setFinished(false); - } - if (m_currentReply) { m_currentReply->setFinished(false); m_currentReply = nullptr; } - m_replyQueue.clear(); - } + while (!m_replyQueue.isEmpty()) { + QPointer reply = m_replyQueue.dequeue(); + if (!reply.isNull()) { + reply->setFinished(false); + } + } - emit connectedChanged(m_connected); + } else { + // Start with a clean parsing + m_payload.clear(); + m_header.clear(); + m_chuncked = false; + } } QByteArray SnapdConnection::createRequestHeader(const QString &method, const QString &path, const QByteArray &payload) diff --git a/snapd/snapdconnection.h b/snapd/snapdconnection.h index d079839e..a90a97b3 100644 --- a/snapd/snapdconnection.h +++ b/snapd/snapdconnection.h @@ -1,3 +1,25 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2017-2018 Simon Stürz . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + #ifndef SNAPDCONNECTION_H #define SNAPDCONNECTION_H @@ -13,8 +35,8 @@ class SnapdConnection : public QLocalSocket public: explicit SnapdConnection(QObject *parent = nullptr); - SnapdReply *get(const QString &path); - SnapdReply *post(const QString &path, const QByteArray &payload); + SnapdReply *get(const QString &path, QObject *parent); + SnapdReply *post(const QString &path, const QByteArray &payload, QObject *parent); bool isConnected() const; @@ -39,6 +61,9 @@ private: void processData(); void sendNextRequest(); +signals: + void connectedChanged(const bool &connected); + private slots: void onConnected(); void onDisconnected(); @@ -46,9 +71,6 @@ private slots: void onStateChanged(const QLocalSocket::LocalSocketState &state); void onReadyRead(); -signals: - void connectedChanged(const bool &connected); - }; #endif // SNAPDCONNECTION_H diff --git a/snapd/snapdcontrol.cpp b/snapd/snapdcontrol.cpp index 751b810e..8662cf8d 100644 --- a/snapd/snapdcontrol.cpp +++ b/snapd/snapdcontrol.cpp @@ -1,6 +1,6 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * Copyright (C) 2017 Simon Stürz isConnected()) return; - SnapdReply *reply = m_snapConnection->get("/v2/system-info"); + SnapdReply *reply = m_snapConnection->get("/v2/system-info", this); connect(reply, &SnapdReply::finished, this, &SnapdControl::onLoadSystemInfoFinished); } @@ -93,7 +93,7 @@ void SnapdControl::loadSnapList() if (!m_snapConnection->isConnected()) return; - SnapdReply *reply = m_snapConnection->get("/v2/snaps"); + SnapdReply *reply = m_snapConnection->get("/v2/snaps", this); connect(reply, &SnapdReply::finished, this, &SnapdControl::onLoadSnapListFinished); } @@ -105,7 +105,7 @@ void SnapdControl::loadRunningChanges() if (!m_snapConnection->isConnected()) return; - SnapdReply *reply = m_snapConnection->get("/v2/changes"); + SnapdReply *reply = m_snapConnection->get("/v2/changes", this); connect(reply, &SnapdReply::finished, this, &SnapdControl::onLoadRunningChangesFinished); } @@ -117,7 +117,7 @@ void SnapdControl::loadChange(const int &change) if (!m_snapConnection->isConnected()) return; - SnapdReply *reply = m_snapConnection->get(QString("/v2/changes/%1").arg(QString::number(change))); + SnapdReply *reply = m_snapConnection->get(QString("/v2/changes/%1").arg(QString::number(change)), this); connect(reply, &SnapdReply::finished, this, &SnapdControl::onLoadChangeFinished); } @@ -381,7 +381,7 @@ void SnapdControl::snapRefresh() request.insert("action", "refresh"); qCDebug(dcSnapd()) << "Refresh all snaps"; - SnapdReply *reply = m_snapConnection->post("/v2/snaps", QJsonDocument::fromVariant(request).toJson(QJsonDocument::Compact)); + SnapdReply *reply = m_snapConnection->post("/v2/snaps", QJsonDocument::fromVariant(request).toJson(QJsonDocument::Compact), this); connect(reply, &SnapdReply::finished, this, &SnapdControl::onSnapRefreshFinished); } @@ -398,7 +398,7 @@ void SnapdControl::changeSnapChannel(const QString &snapName, const QString &cha request.insert("channel", channel); qCDebug(dcSnapd()) << "Refresh snap" << snapName << "to channel" << channel; - SnapdReply *reply = m_snapConnection->post(QString("/v2/snaps/%1").arg(snapName), QJsonDocument::fromVariant(request).toJson(QJsonDocument::Compact)); + SnapdReply *reply = m_snapConnection->post(QString("/v2/snaps/%1").arg(snapName), QJsonDocument::fromVariant(request).toJson(QJsonDocument::Compact), this); connect(reply, &SnapdReply::finished, this, &SnapdControl::onChangeSnapChannelFinished); } @@ -410,7 +410,7 @@ void SnapdControl::checkForUpdates() if (!m_snapConnection->isConnected()) return; - SnapdReply *reply = m_snapConnection->get("/v2/find?select=refresh"); + SnapdReply *reply = m_snapConnection->get("/v2/find?select=refresh", this); connect(reply, &SnapdReply::finished, this, &SnapdControl::onCheckForUpdatesFinished); } @@ -426,6 +426,6 @@ void SnapdControl::snapRevert(const QString &snapName) request.insert("action", "revert"); qCDebug(dcSnapd()) << "Revert snap" << snapName; - SnapdReply *reply = m_snapConnection->post(QString("/v2/snaps/%1").arg(snapName), QJsonDocument::fromVariant(request).toJson(QJsonDocument::Compact)); + SnapdReply *reply = m_snapConnection->post(QString("/v2/snaps/%1").arg(snapName), QJsonDocument::fromVariant(request).toJson(QJsonDocument::Compact), this); connect(reply, &SnapdReply::finished, this, &SnapdControl::onSnapRevertFinished); } diff --git a/snapd/snapdcontrol.h b/snapd/snapdcontrol.h index 4b5bfc9d..dd857fd4 100644 --- a/snapd/snapdcontrol.h +++ b/snapd/snapdcontrol.h @@ -1,6 +1,6 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * Copyright (C) 2017 Simon Stürz . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + #include "snapdreply.h" QString SnapdReply::requestPath() const diff --git a/snapd/snapdreply.h b/snapd/snapdreply.h index 0251335f..7399de8e 100644 --- a/snapd/snapdreply.h +++ b/snapd/snapdreply.h @@ -1,3 +1,25 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2017-2018 Simon Stürz . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + #ifndef SNAPDREPLY_H #define SNAPDREPLY_H