diff --git a/qml/cover/CoverPage.qml b/qml/cover/CoverPage.qml index b6c9961..34bd0eb 100644 --- a/qml/cover/CoverPage.qml +++ b/qml/cover/CoverPage.qml @@ -20,11 +20,32 @@ CoverBackground { Column { width: parent.width - Label { - color: Theme.highlightColor + Row { width: parent.width - text: model.client.config.teamName - truncationMode: TruncationMode.Fade + + Label { + color: Theme.highlightColor + width: parent.width - statusIndicator.width + text: model.client.config.teamName + truncationMode: TruncationMode.Fade + } + Rectangle { + id: statusIndicator + width: 15 + anchors.verticalCenter: parent.verticalCenter + height: width + radius: width / 2 + color: switch (model.client.connectionStatus) { + case Slack.Client.Connected: return "green" + case Slack.Client.Connecting: return "yellow" + case Slack.Client.Disconnected: return "red" + } + Behavior on color { + ColorAnimation { + duration: 200 + } + } + } } Row { diff --git a/qml/pages/ConnectionPanel.qml b/qml/pages/ConnectionPanel.qml index 72129f8..d08aea7 100644 --- a/qml/pages/ConnectionPanel.qml +++ b/qml/pages/ConnectionPanel.qml @@ -12,6 +12,8 @@ DockedPanel { dock: Dock.Bottom + visible: slackClient.connectionStatus !== Client.Connected || !slackClient.networkAccessible + Column { id: content width: parent.width - Theme.paddingLarge * (Screen.sizeCategory >= Screen.Large ? 4 : 2) @@ -30,18 +32,21 @@ DockedPanel { } Label { + visible: slackClient.connectionStatus === Client.Connecting text: qsTr("Reconnecting") } } Label { id: disconnectedMessage + visible: slackClient.connectionStatus === Client.Disconnected || !slackClient.networkAccessible anchors.horizontalCenter: parent.horizontalCenter - text: qsTr("Disconnected") + text: slackClient.networkAccessible ? qsTr("Disconnected") : qsTr("No network connection") } Button { id: reconnectButton + visible: slackClient.connectionStatus === Client.Disconnected && slackClient.networkAccessible anchors.horizontalCenter: parent.horizontalCenter text: qsTr("Reconnect") onClicked: { @@ -49,39 +54,4 @@ DockedPanel { } } } - - Component.onCompleted: { - slackClient.onConnected.connect(hideConnectionPanel) - slackClient.onReconnecting.connect(showReconnectingMessage) - slackClient.onDisconnected.connect(showDisconnectedMessage) - slackClient.onNetworkOff.connect(showNoNetworkMessage) - slackClient.onNetworkOn.connect(hideConnectionPanel) - } - - function hideConnectionPanel() { - connectionPanel.hide() - } - - function showReconnectingMessage() { - disconnectedMessage.visible = false - reconnectButton.visible = false - reconnectingMessage.visible = true - connectionPanel.show() - } - - function showDisconnectedMessage() { - disconnectedMessage.text = qsTr("Disconnected") - disconnectedMessage.visible = true - reconnectButton.visible = true - reconnectingMessage.visible = false - connectionPanel.show() - } - - function showNoNetworkMessage() { - disconnectedMessage.text = qsTr("No network connection") - disconnectedMessage.visible = true - reconnectButton.visible = false - reconnectingMessage.visible = false - connectionPanel.show() - } } diff --git a/qml/pages/TeamList.qml b/qml/pages/TeamList.qml index 546aa96..9ff7600 100644 --- a/qml/pages/TeamList.qml +++ b/qml/pages/TeamList.qml @@ -68,7 +68,7 @@ Page { } Label { - width: parent.width - icon.width - Theme.paddingMedium + width: parent.width - icon.width - Theme.paddingMedium - statusIcon.width wrapMode: Text.Wrap anchors.verticalCenter: parent.verticalCenter font.pixelSize: Theme.fontSizeMedium @@ -76,6 +76,24 @@ Page { text: model.client.config.teamName color: currentColor } + + Rectangle { + id: statusIcon + width: 20 + height: width + anchors.verticalCenter: parent.verticalCenter + radius: width / 2 + color: switch (model.client.connectionStatus) { + case Slack.Client.Connected: return "green" + case Slack.Client.Connecting: return "yellow" + case Slack.Client.Disconnected: return "red" + } + Behavior on color { + ColorAnimation { + duration: 200 + } + } + } } onClicked: { console.log("Opening team " + model.client.config.teamName) diff --git a/src/slackclient.cpp b/src/slackclient.cpp index f528f50..2009d21 100644 --- a/src/slackclient.cpp +++ b/src/slackclient.cpp @@ -88,22 +88,29 @@ void SlackClient::handleNetworkAccessibleChanged(QNetworkAccessManager::NetworkA networkAccessible = accessible; if (networkAccessible == QNetworkAccessManager::Accessible) { - emit networkOn(); + Q_EMIT networkAccessibleChanged(true); + } else { + Q_EMIT networkAccessibleChanged(false); } - else { - emit networkOff(); +} + +void SlackClient::setConnectionStatus(ConnectionStatus status) { + if (connectionStatus != status) { + connectionStatus = status; + Q_EMIT connectionStatusChanged(connectionStatus); } } void SlackClient::reconnect() { qDebug() << config->getTeamName() << ": Reconnecting"; - emit reconnecting(); + setConnectionStatus(Connecting); + start(); } void SlackClient::handleStreamStart() { qDebug() << config->getTeamName() << ": Stream started"; - emit connected(); + setConnectionStatus(Connected); updatePresenceSubscription(); } @@ -145,9 +152,11 @@ void SlackClient::handleStreamEnd() { if (!config->getAccessToken().isEmpty()) { qDebug() << config->getTeamName() << ": Stream reconnect scheduled"; - emit reconnecting(); + setConnectionStatus(Connecting); reconnectTimer->setSingleShot(true); reconnectTimer->start(1000); + } else { + setConnectionStatus(Disconnected); } } @@ -442,8 +451,9 @@ void SlackClient::handleTestLoginReply() { } void SlackClient::init() { - qDebug() << config->getTeamName() << ": Start init"; - loadUsers(); + setConnectionStatus(Connecting); + qDebug() << config->getTeamName() << ": Start init"; + loadUsers(); } void SlackClient::loadUsers(const QString &cursor) { @@ -485,7 +495,7 @@ void SlackClient::start() { if (Request::isError(data)) { qDebug() << config->getTeamName() << ": Connect result error"; - emit disconnected(); + setConnectionStatus(Disconnected); emit initFail(); } else { diff --git a/src/slackclient.h b/src/slackclient.h index d7fa617..a704334 100644 --- a/src/slackclient.h +++ b/src/slackclient.h @@ -23,10 +23,18 @@ class SlackClient : public QObject Q_PROPERTY(SlackClientConfig* config READ getConfig CONSTANT) Q_PROPERTY(QString team READ getTeam CONSTANT) Q_PROPERTY(bool initialized READ getInitialized NOTIFY initializedChanged) - + Q_PROPERTY(ConnectionStatus connectionStatus READ getConnectionStatus NOTIFY connectionStatusChanged) + Q_PROPERTY(bool networkAccessible READ getNetworkAccessible NOTIFY networkAccessibleChanged) Q_PROPERTY(int unreadCount READ getUnreadCount NOTIFY unreadCountChanged) public: + enum ConnectionStatus { + Disconnected, + Connecting, + Connected + }; + Q_ENUM(ConnectionStatus) + explicit SlackClient(QObject *parent = 0); // only to make qmlRegisterType happy, don't use explicit SlackClient(const QString &team, QObject *parent = 0); ~SlackClient() override; @@ -44,6 +52,10 @@ class SlackClient : public QObject QString getTeam() const; int getUnreadCount() const { return unreadCount; } + + ConnectionStatus getConnectionStatus() const { return connectionStatus; } + bool getNetworkAccessible() const { return networkAccessible == QNetworkAccessManager::Accessible; } + signals: void teamChanged(); @@ -78,12 +90,8 @@ class SlackClient : public QObject void postImageSuccess(); void postImageFail(); - void networkOff(); - void networkOn(); - - void reconnecting(); - void disconnected(); - void connected(); + void connectionStatusChanged(SlackClient::ConnectionStatus status); + void networkAccessibleChanged(bool hasNetwork); void unreadCountChanged(int count); @@ -125,6 +133,8 @@ public slots: bool appActive; QString activeWindow; + void setConnectionStatus(ConnectionStatus status); + QNetworkReply* executePost(QString method, const QMap &data); QNetworkReply *executePostWithFile(QString method, const QMap&, QFile *file); @@ -178,9 +188,10 @@ public slots: Storage storage; MessageFormatter messageFormatter; - QNetworkAccessManager::NetworkAccessibility networkAccessible; + QNetworkAccessManager::NetworkAccessibility networkAccessible = QNetworkAccessManager::UnknownAccessibility; bool initialized = false; + ConnectionStatus connectionStatus = Disconnected; int unreadCount = 0; };