diff --git a/connector/syncthingnotifier.cpp b/connector/syncthingnotifier.cpp index c233048..0aeb574 100644 --- a/connector/syncthingnotifier.cpp +++ b/connector/syncthingnotifier.cpp @@ -39,6 +39,8 @@ SyncthingNotifier::SyncthingNotifier(const SyncthingConnection &connection, QObj { connect(&connection, &SyncthingConnection::statusChanged, this, &SyncthingNotifier::handleStatusChangedEvent); connect(&connection, &SyncthingConnection::dirCompleted, this, &SyncthingNotifier::emitSyncComplete); + connect(&connection, &SyncthingConnection::newDevAvailable, this, &SyncthingNotifier::handleNewDevEvent); + connect(&connection, &SyncthingConnection::newDirAvailable, this, &SyncthingNotifier::handleNewDirEvent); } void SyncthingNotifier::handleStatusChangedEvent(SyncthingStatus newStatus) @@ -57,6 +59,39 @@ void SyncthingNotifier::handleStatusChangedEvent(SyncthingStatus newStatus) m_previousStatus = newStatus; } +void SyncthingNotifier::handleNewDevEvent(DateTime when, const QString &devId, const QString &address) +{ + VAR_UNUSED(when) + + // ignore if not enabled + if (!(m_enabledNotifications & SyncthingHighLevelNotification::NewDevice)) { + return; + } + + emit newDevice(devId, tr("Device %1 (%2) wants to connect.").arg(devId, address)); +} + +void SyncthingNotifier::handleNewDirEvent(DateTime when, const QString &devId, const SyncthingDev *dev, const QString &dirId, const QString &dirLabel) +{ + VAR_UNUSED(when) + + // ignore if not enabled + if (!(m_enabledNotifications & SyncthingHighLevelNotification::NewDir)) { + return; + } + + // format message + const auto message([&devId, dev, &dirId, &dirLabel] { + const auto devPrefix(dev ? (tr("Device ") + dev->displayName()) : (tr("Unknown device ") + devId)); + if (dirLabel.isEmpty()) { + return devPrefix + tr(" wants to share directory %1.").arg(dirId); + } else { + return devPrefix + tr(" wants to share directory %1 (%2).").arg(dirLabel, dirId); + } + }()); + emit newDir(devId, dirId, message); +} + /*! * \brief Returns whether a "disconnected" notification should be shown. * \todo Unify with InternalError::isRelevant(). diff --git a/connector/syncthingnotifier.h b/connector/syncthingnotifier.h index 0654950..3fd55de 100644 --- a/connector/syncthingnotifier.h +++ b/connector/syncthingnotifier.h @@ -27,6 +27,8 @@ enum class SyncthingHighLevelNotification { ConnectedDisconnected = 0x1, LocalSyncComplete = 0x2, RemoteSyncComplete = 0x4, + NewDevice = 0x8, + NewDir = 0x10, }; /// \cond @@ -68,11 +70,19 @@ Q_SIGNALS: void connected(); ///! \brief Emitted when the connection to Syncthing has been interrupted. void disconnected(); - ///! \brief Emitted when the specified \a dirs have been completed synchronization. + ///! \brief Emitted when one or more directories have completed synchronization. + ///! \remarks Both, local and remote devices, are taken into account. void syncComplete(const QString &message); + ///! \brief Emitted when a new device talks to us. + void newDevice(const QString &devId, const QString &message); + ///! \brief Emitted when a new directory is shared with us. + void newDir(const QString &devId, const QString &dirId, const QString &message); private Q_SLOTS: void handleStatusChangedEvent(SyncthingStatus newStatus); + void handleNewDevEvent(ChronoUtilities::DateTime when, const QString &devId, const QString &address); + void handleNewDirEvent( + ChronoUtilities::DateTime when, const QString &devId, const SyncthingDev *dev, const QString &dirId, const QString &dirLabel); private: bool isDisconnectRelevant() const;