diff --git a/ScreenPlay/src/screenplaymanager.cpp b/ScreenPlay/src/screenplaymanager.cpp index c15af494..362a9761 100644 --- a/ScreenPlay/src/screenplaymanager.cpp +++ b/ScreenPlay/src/screenplaymanager.cpp @@ -382,7 +382,7 @@ ScreenPlayWallpaper* ScreenPlayManager::getWallpaperByAppID(const QString& appID */ void ScreenPlayManager::newConnection() { - qInfo() << "[1/3] SDKConnection incomming"; + qInfo() << "[1/4] SDKConnection incomming"; auto connection = std::make_unique(m_server->nextPendingConnection()); QObject::connect(connection.get(), &SDKConnection::requestRaise, this, &ScreenPlayManager::requestRaise); @@ -409,7 +409,7 @@ void ScreenPlayManager::newConnection() for (int i = 0; i < m_screenPlayWallpapers.size(); ++i) { if (m_screenPlayWallpapers.at(i)->appID() == matchingConnection->appID()) { - qInfo() << "Matching Wallpaper found!"; + qInfo() << "[3/4] Matching Wallpaper found!"; m_screenPlayWallpapers.at(i)->setSDKConnection(std::move(matchingConnection)); return; } @@ -417,7 +417,7 @@ void ScreenPlayManager::newConnection() for (int i = 0; i < m_screenPlayWidgets.size(); ++i) { if (m_screenPlayWidgets.at(i)->appID() == matchingConnection->appID()) { - qInfo() << "Matching Widget found!"; + qInfo() << "[3/4] Matching Widget found!"; m_screenPlayWidgets.at(i)->setSDKConnection(std::move(matchingConnection)); return; } @@ -444,7 +444,7 @@ bool ScreenPlayManager::removeWallpaper(const QString& appID) if (wallpaper->appID() != appID) { return false; } - wallpaper->messageQuit(); + qInfo() << "Remove wallpaper " << wallpaper->file() << "at monitor " << wallpaper->screenNumber(); @@ -452,6 +452,8 @@ bool ScreenPlayManager::removeWallpaper(const QString& appID) // for shared_ptr to release the object. m_monitorListModel->setWallpaperMonitor({}, wallpaper->screenNumber()); + wallpaper->close(); + decreaseActiveWallpaperCounter(); return true; @@ -481,7 +483,7 @@ bool ScreenPlayManager::removeWidget(const QString& appID) return false; } - widget->messageQuit(); + widget->close(); qInfo() << "Remove widget " << appID; diff --git a/ScreenPlay/src/screenplaywallpaper.cpp b/ScreenPlay/src/screenplaywallpaper.cpp index 481cb4cc..ab27b5e1 100644 --- a/ScreenPlay/src/screenplaywallpaper.cpp +++ b/ScreenPlay/src/screenplaywallpaper.cpp @@ -137,7 +137,7 @@ QJsonObject ScreenPlayWallpaper::getActiveSettingsJson() /*! \brief Sends command quit to the wallpaper. */ -void ScreenPlayWallpaper::messageQuit() +void ScreenPlayWallpaper::close() { // When the wallpaper never connected, this is invalid if (!m_connection) { @@ -145,9 +145,10 @@ void ScreenPlayWallpaper::messageQuit() return; } - QJsonObject obj; - obj.insert("command", "quit"); - m_connection->sendMessage(QJsonDocument(obj).toJson(QJsonDocument::Compact)); + if(m_connection->close()){ + m_isExiting = true; + } + } /*! \brief Prints the exit code if != 0. @@ -174,6 +175,9 @@ void ScreenPlayWallpaper::processError(QProcess::ProcessError error) */ bool ScreenPlayWallpaper::setWallpaperValue(const QString& key, const QString& value, const bool save) { + if(m_isExiting) + return false; + if (!m_connection) { qWarning() << "Cannot set value for unconnected wallpaper!"; return false; @@ -207,13 +211,12 @@ bool ScreenPlayWallpaper::setWallpaperValue(const QString& key, const QString& v void ScreenPlayWallpaper::setSDKConnection(std::unique_ptr connection) { m_connection = std::move(connection); - qInfo() << "[3/3] SDKConnection (Wallpaper) saved!"; + qInfo() << "[4/4] SDKConnection (Wallpaper) saved!"; setIsConnected(true); QObject::connect(m_connection.get(), &SDKConnection::disconnected, this, [this]() { setIsConnected(false); qInfo() << "disconnecetd;"; - }); QTimer::singleShot(1000, this, [this]() { if (playbackRate() != 1.0) { @@ -245,6 +248,10 @@ void ScreenPlayWallpaper::replace( const InstalledType::InstalledType type, const bool checkWallpaperVisible) { + + if(m_isExiting) + return; + if (!m_connection) { qWarning() << "Cannot replace for unconnected wallpaper!"; return; diff --git a/ScreenPlay/src/screenplaywallpaper.h b/ScreenPlay/src/screenplaywallpaper.h index fd15b06d..7019c0c6 100644 --- a/ScreenPlay/src/screenplaywallpaper.h +++ b/ScreenPlay/src/screenplaywallpaper.h @@ -132,7 +132,7 @@ signals: void isConnectedChanged(bool isConnected); public slots: - void messageQuit(); + void close(); void processExit(int exitCode, QProcess::ExitStatus exitStatus); void processError(QProcess::ProcessError error); bool setWallpaperValue(const QString& key, const QString& value, const bool save = false); @@ -256,6 +256,9 @@ private: float m_playbackRate { 1.0f }; QTimer m_pingAliveTimer; QStringList m_appArgumentsList; - bool m_isConnected; + bool m_isConnected { false }; + // There are still cases where we can access the current item + // while exiting. This flag is to ignore all setWallpaperValue calls + bool m_isExiting { false }; }; } diff --git a/ScreenPlay/src/screenplaywidget.cpp b/ScreenPlay/src/screenplaywidget.cpp index b08c0c1b..dee57ac5 100644 --- a/ScreenPlay/src/screenplaywidget.cpp +++ b/ScreenPlay/src/screenplaywidget.cpp @@ -73,11 +73,15 @@ bool ScreenPlayWidget::start() /*! \brief Sends command quit to the widget. */ -void ScreenPlayWidget::messageQuit() +void ScreenPlayWidget::close() { - QJsonObject obj; - obj.insert("command", "quit"); - m_connection->sendMessage(QJsonDocument(obj).toJson(QJsonDocument::Compact)); + // When the wallpaper never connected, this is invalid + if (!m_connection) { + qCritical() << "Cannot request quit, widget never connected!"; + return; + } + + m_connection->close(); } /*! diff --git a/ScreenPlay/src/screenplaywidget.h b/ScreenPlay/src/screenplaywidget.h index 7a10340c..b657273c 100644 --- a/ScreenPlay/src/screenplaywidget.h +++ b/ScreenPlay/src/screenplaywidget.h @@ -85,7 +85,7 @@ public: ProjectSettingsListModel* getProjectSettingsListModel() { return &m_projectSettingsListModel; } public slots: - void messageQuit(); + void close(); QJsonObject getActiveSettingsJson(); void setPreviewImage(QString previewImage) diff --git a/ScreenPlay/src/sdkconnection.cpp b/ScreenPlay/src/sdkconnection.cpp index 9e7d92a6..cc4e3ad0 100644 --- a/ScreenPlay/src/sdkconnection.cpp +++ b/ScreenPlay/src/sdkconnection.cpp @@ -64,7 +64,7 @@ void ScreenPlay::SDKConnection::readyRead() qCritical() << "Wallpaper type not found. Expected: " << ScreenPlayUtil::getAvailableTypes() << " got: " << msg; } - qInfo() << "[2/3] SDKConnection parsed with type: " << m_type << " connected with AppID:" << m_appID; + qInfo() << "[2/4] SDKConnection parsed with type: " << m_type << " connected with AppID:" << m_appID; emit appConnected(this); @@ -108,28 +108,15 @@ bool ScreenPlay::SDKConnection::sendMessage(const QByteArray& message) */ bool ScreenPlay::SDKConnection::close() { + if (!m_socket){ + qWarning() << "Cannot close invalid socket."; + return false; + } qInfo() << "Close " << m_type << m_appID << m_socket->state(); + m_socket->disconnectFromServer(); + m_socket->close(); - QJsonObject obj; - obj.insert("command", QJsonValue("quit")); - QByteArray command = QJsonDocument(obj).toJson(); - - m_socket->write(command); - if (!m_socket->waitForBytesWritten()) { - qWarning("Faild to send quit command to app"); - return false; - } - - if (m_socket->state() == QLocalSocket::ConnectedState) { - m_socket->disconnectFromServer(); - m_socket->close(); - - qInfo() << "### Destroy APPID:\t " << m_appID << " State: " << m_socket->state(); - } else { - qWarning() << "Cannot disconnect app " << m_appID << " with the state:" << m_socket->state(); - return false; - } - return true; + return m_socket->state() == QLocalSocket::UnconnectedState; } }