diff --git a/qml/main.qml b/qml/main.qml index 244844ceb..54ef8a9dc 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -41,6 +41,8 @@ ApplicationWindow { iconSource: ":/assets/img/icon.png" + isOnline: spectralController.isOnline + onShowWindow: window.showWindow() } diff --git a/src/controller.cpp b/src/controller.cpp index 0de2106a5..66ee53ace 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -41,6 +41,9 @@ Controller::Controller(QObject* parent) : QObject(parent) { Connection::setRoomType(); Connection::setUserType(); + connect(&m_ncm, &QNetworkConfigurationManager::onlineStateChanged, this, + &Controller::isOnlineChanged); + QTimer::singleShot(0, this, SLOT(invokeLogin())); } @@ -134,6 +137,14 @@ void Controller::addConnection(Connection* c) { c->saveState(); }); connect(c, &Connection::loggedOut, this, [=] { dropConnection(c); }); + connect(&m_ncm, &QNetworkConfigurationManager::onlineStateChanged, + [=](bool status) { + if (!status) + return; + + c->stopSync(); + c->sync(30000); + }); using namespace QMatrixClient; diff --git a/src/controller.h b/src/controller.h index dafd9c07b..ee9bee5ec 100644 --- a/src/controller.h +++ b/src/controller.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -23,6 +24,7 @@ class Controller : public QObject { setQuitOnLastWindowClosed NOTIFY quitOnLastWindowClosedChanged) Q_PROPERTY(Connection* connection READ connection WRITE setConnection NOTIFY connectionChanged) + Q_PROPERTY(bool isOnline READ isOnline NOTIFY isOnlineChanged) Q_PROPERTY(bool busy READ busy WRITE setBusy NOTIFY busyChanged) public: @@ -54,6 +56,8 @@ class Controller : public QObject { } } + bool isOnline() { return m_ncm.isOnline(); } + bool busy() { return m_busy; } void setBusy(bool busy) { if (m_busy == busy) { @@ -79,6 +83,7 @@ class Controller : public QObject { private: QVector m_connections; QPointer m_connection; + QNetworkConfigurationManager m_ncm; bool m_busy = false; QByteArray loadAccessTokenFromFile(const AccountSettings& account); @@ -105,6 +110,7 @@ class Controller : public QObject { void quitOnLastWindowClosedChanged(); void unreadCountChanged(); void connectionChanged(); + void isOnlineChanged(); public slots: void logout(Connection* conn); diff --git a/src/trayicon.cpp b/src/trayicon.cpp index 2b2635378..1c3b0d8cf 100644 --- a/src/trayicon.cpp +++ b/src/trayicon.cpp @@ -27,7 +27,7 @@ void MsgCountComposedIcon::paint(QPainter* painter, icon_.paint(painter, rect, Qt::AlignCenter, mode, state); - if (msgCount <= 0) + if (isOnline && msgCount <= 0) return; QColor backgroundColor("red"); @@ -46,10 +46,12 @@ void MsgCountComposedIcon::paint(QPainter* painter, painter->drawEllipse(bubble); painter->setPen(QPen(textColor)); painter->setBrush(Qt::NoBrush); - if (msgCount < 100) { - painter->drawText(bubble, Qt::AlignCenter, QString::number(msgCount)); - } else { + if (!isOnline) { + painter->drawText(bubble, Qt::AlignCenter, "x"); + } else if (msgCount >= 100) { painter->drawText(bubble, Qt::AlignCenter, "99+"); + } else { + painter->drawText(bubble, Qt::AlignCenter, QString::number(msgCount)); } } @@ -111,7 +113,7 @@ void TrayIcon::setNotificationCount(int count) { #elif defined(Q_OS_WIN) // FIXME: Find a way to use Windows apis for the badge counter (if any). #else - if (count == icon_->msgCount) + if (!icon_ || count == icon_->msgCount) return; // Custom drawing on Linux. @@ -126,6 +128,23 @@ void TrayIcon::setNotificationCount(int count) { emit notificationCountChanged(); } +void TrayIcon::setIsOnline(bool online) { + m_isOnline = online; + + if (!icon_ || online == icon_->isOnline) + return; + + // Custom drawing on Linux. + MsgCountComposedIcon* tmp = + static_cast(icon_->clone()); + tmp->isOnline = online; + + setIcon(QIcon(tmp)); + + icon_ = tmp; + emit isOnlineChanged(); +} + void TrayIcon::setIconSource(const QString& source) { m_iconSource = source; #if defined(Q_OS_MAC) || defined(Q_OS_WIN) @@ -133,6 +152,8 @@ void TrayIcon::setIconSource(const QString& source) { #else icon_ = new MsgCountComposedIcon(source); setIcon(QIcon(icon_)); + icon_->isOnline = m_isOnline; + icon_->msgCount = m_notificationCount; #endif emit iconSourceChanged(); } diff --git a/src/trayicon.h b/src/trayicon.h index 73763b219..5c3c9d130 100644 --- a/src/trayicon.h +++ b/src/trayicon.h @@ -26,6 +26,7 @@ class MsgCountComposedIcon : public QIconEngine { QIcon::State state); int msgCount = 0; + bool isOnline = true; // Default to false? private: const int BubbleDiameter = 14; @@ -39,6 +40,7 @@ class TrayIcon : public QSystemTrayIcon { iconSourceChanged) Q_PROPERTY(int notificationCount READ notificationCount WRITE setNotificationCount NOTIFY notificationCountChanged) + Q_PROPERTY(bool isOnline READ isOnline WRITE setIsOnline NOTIFY isOnlineChanged) public: TrayIcon(QObject* parent = nullptr); @@ -48,15 +50,20 @@ class TrayIcon : public QSystemTrayIcon { int notificationCount() { return m_notificationCount; } void setNotificationCount(int count); + bool isOnline() { return m_isOnline; } + void setIsOnline(bool online); + signals: void notificationCountChanged(); void iconSourceChanged(); + void isOnlineChanged(); void showWindow(); private: QString m_iconSource; int m_notificationCount = 0; + bool m_isOnline = true; QAction* viewAction_; QAction* quitAction_;