diff options
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/accountstate.cpp | 4 | ||||
-rw-r--r-- | src/gui/socketapi/socketapi.cpp | 59 | ||||
-rw-r--r-- | src/gui/socketapi/socketapi.h | 3 |
3 files changed, 50 insertions, 16 deletions
diff --git a/src/gui/accountstate.cpp b/src/gui/accountstate.cpp index 8fd3128bd..86b0e5b70 100644 --- a/src/gui/accountstate.cpp +++ b/src/gui/accountstate.cpp @@ -23,6 +23,7 @@ #include "gui/tlserrordialog.h" #include "logger.h" #include "settingsdialog.h" +#include "socketapi/socketapi.h" #include "theme.h" #include <QFontMetrics> @@ -103,10 +104,13 @@ AccountState::AccountState(AccountPtr account) connect(account->credentials(), &AbstractCredentials::requestLogout, this, [this] { _state = State::SignedOut; }); + + FolderMan::instance()->socketApi()->registerAccount(account); } AccountState::~AccountState() { + FolderMan::instance()->socketApi()->unregisterAccount(account()); } AccountStatePtr AccountState::loadFromSettings(AccountPtr account, const QSettings &settings) diff --git a/src/gui/socketapi/socketapi.cpp b/src/gui/socketapi/socketapi.cpp index fcec8cb88..94de46739 100644 --- a/src/gui/socketapi/socketapi.cpp +++ b/src/gui/socketapi/socketapi.cpp @@ -77,6 +77,11 @@ namespace { +const QString unregisterpathMessageC() +{ + return QStringLiteral("UNREGISTER_PATH"); +} + const QLatin1Char RecordSeparator() { return QLatin1Char('\x1e'); @@ -159,25 +164,16 @@ QObject *findWidget(const QString &queryString, const QList<QWidget *> &widgets } #endif -static inline QString removeTrailingSlash(QString path) -{ - Q_ASSERT(path.endsWith(QLatin1Char('/'))); - path.truncate(path.length() - 1); - return path; -} - static QString buildMessage(const QString &verb, const QString &path, const QString &status = QString()) { QString msg(verb); if (!status.isEmpty()) { - msg.append(QLatin1Char(':')); - msg.append(status); + msg += QLatin1Char(':') + status; } if (!path.isEmpty()) { - msg.append(QLatin1Char(':')); - QFileInfo fi(path); - msg.append(QDir::toNativeSeparators(fi.absoluteFilePath())); + const QFileInfo fi(path); + msg += QLatin1Char(':') + QDir::toNativeSeparators(fi.absoluteFilePath()); } return msg; } @@ -288,9 +284,14 @@ void SocketApi::slotNewConnection() auto listener = QSharedPointer<SocketListener>::create(socket); _listeners.insert(socket, listener); + for (auto a : _registeredAccounts) { + if (a->hasDefaultSyncRoot()) { + broadcastMessage(buildRegisterPathMessage(Utility::stripTrailingSlash(a->defaultSyncRoot()))); + } + } for (Folder *f : FolderMan::instance()->folders()) { if (f->canSync()) { - QString message = buildRegisterPathMessage(removeTrailingSlash(f->path())); + QString message = buildRegisterPathMessage(Utility::stripTrailingSlash(f->path())); listener->sendMessage(message); } } @@ -390,13 +391,39 @@ void SocketApi::slotReadSocket() } } + +void SocketApi::registerAccount(const AccountPtr &a) +{ + // Make sure not to register twice to each connected client + if (_registeredAccounts.contains(a)) { + return; + } + + if (a->hasDefaultSyncRoot()) { + broadcastMessage(buildRegisterPathMessage(Utility::stripTrailingSlash(a->defaultSyncRoot()))); + } + _registeredAccounts.insert(a); +} + +void SocketApi::unregisterAccount(const AccountPtr &a) +{ + if (!_registeredAccounts.contains(a)) { + return; + } + + if (a->hasDefaultSyncRoot()) { + broadcastMessage(buildMessage(unregisterpathMessageC(), Utility::stripTrailingSlash(a->defaultSyncRoot()))); + } + _registeredAccounts.remove(a); +} + void SocketApi::slotRegisterPath(Folder *folder) { // Make sure not to register twice to each connected client if (_registeredFolders.contains(folder)) return; - broadcastMessage(buildRegisterPathMessage(removeTrailingSlash(folder->path()))); + broadcastMessage(buildRegisterPathMessage(Utility::stripTrailingSlash(folder->path()))); _registeredFolders.insert(folder); } @@ -405,7 +432,7 @@ void SocketApi::slotUnregisterPath(Folder *folder) if (!_registeredFolders.contains(folder)) return; - broadcastMessage(buildMessage(QLatin1String("UNREGISTER_PATH"), removeTrailingSlash(folder->path()), QString()), true); + broadcastMessage(buildMessage(unregisterpathMessageC(), Utility::stripTrailingSlash(folder->path()), QString()), true); _registeredFolders.remove(folder); } @@ -429,7 +456,7 @@ void SocketApi::slotUpdateFolderView(Folder *f) case SyncResult::Error: Q_FALLTHROUGH(); case SyncResult::SetupError: { - const QString rootPath = removeTrailingSlash(f->path()); + const QString rootPath = Utility::stripTrailingSlash(f->path()); broadcastStatusPushMessage(rootPath, f->syncEngine().syncFileStatusTracker().fileStatus("")); broadcastMessage(buildMessage(QStringLiteral("UPDATE_VIEW"), rootPath)); diff --git a/src/gui/socketapi/socketapi.h b/src/gui/socketapi/socketapi.h index 43c71cafe..3db8b79ce 100644 --- a/src/gui/socketapi/socketapi.h +++ b/src/gui/socketapi/socketapi.h @@ -57,6 +57,8 @@ public: ~SocketApi() override; public slots: + void registerAccount(const AccountPtr &a); + void unregisterAccount(const AccountPtr &a); void slotUpdateFolderView(Folder *f); void slotUnregisterPath(Folder *f); void slotRegisterPath(Folder *f); @@ -165,6 +167,7 @@ private: QString buildRegisterPathMessage(const QString &path); QSet<Folder *> _registeredFolders; + QSet<AccountPtr> _registeredAccounts; QMap<SocketApiSocket *, QSharedPointer<SocketListener>> _listeners; SocketApiServer _localServer; }; |