Avoid redundant code to check whether connection config is sufficient

This commit is contained in:
Martchus 2024-06-21 00:11:21 +02:00
parent 94fc0fe432
commit cf8408f188
2 changed files with 16 additions and 11 deletions

View File

@ -345,19 +345,27 @@ void SyncthingConnection::connect()
// reset status // reset status
m_connectionAborted = m_abortingToConnect = m_abortingToReconnect = m_hasConfig = m_hasStatus = m_hasEvents = m_hasDiskEvents = false; m_connectionAborted = m_abortingToConnect = m_abortingToReconnect = m_hasConfig = m_hasStatus = m_hasEvents = m_hasDiskEvents = false;
// check configuration if (!checkConnectionConfiguration()) {
if (m_apiKey.isEmpty() || m_syncthingUrl.isEmpty()) {
emit error(tr("Connection configuration is insufficient."), SyncthingErrorCategory::OverallConnection, QNetworkReply::NoError);
setStatus(SyncthingStatus::Disconnected);
return; return;
} }
// start by requesting config and status; if both are available request further info and events
requestConfig(); requestConfig();
requestStatus(); requestStatus();
m_keepPolling = true; m_keepPolling = true;
} }
/*!
* \brief Returns whether the connection configuration is sufficient and sets the connection into the disconnected state if not.
*/
bool SyncthingConnection::checkConnectionConfiguration()
{
if (!m_apiKey.isEmpty() && !m_syncthingUrl.isEmpty()) {
return true;
}
emit error(tr("Connection configuration is insufficient."), SyncthingErrorCategory::OverallConnection, QNetworkReply::NoError);
setStatus(SyncthingStatus::Disconnected);
return false;
}
/*! /*!
* \brief Applies the specified configuration and tries to reconnect via reconnect() if properties requiring reconnect * \brief Applies the specified configuration and tries to reconnect via reconnect() if properties requiring reconnect
* to take effect have changed. * to take effect have changed.
@ -536,15 +544,11 @@ void SyncthingConnection::continueReconnecting()
emit newConfigApplied(); emit newConfigApplied();
} }
if (m_apiKey.isEmpty() || m_syncthingUrl.isEmpty()) { if (!checkConnectionConfiguration()) {
emit error(tr("Connection configuration is insufficient."), SyncthingErrorCategory::OverallConnection, QNetworkReply::NoError);
setStatus(SyncthingStatus::Disconnected);
return; return;
} }
requestConfig(); requestConfig();
requestStatus(); requestStatus();
setStatus(SyncthingStatus::Reconnecting); setStatus(SyncthingStatus::Reconnecting);
} }

View File

@ -465,6 +465,7 @@ private:
QString configPath() const; QString configPath() const;
QByteArray changeConfigVerb() const; QByteArray changeConfigVerb() const;
QString folderErrorsPath() const; QString folderErrorsPath() const;
bool checkConnectionConfiguration();
QString m_syncthingUrl; QString m_syncthingUrl;
QByteArray m_apiKey; QByteArray m_apiKey;