2017-09-08 16:54:59 +02:00
# include "./internalerror.h"
2018-04-11 23:15:15 +02:00
# include "./syncthinglauncher.h"
2017-09-08 16:54:59 +02:00
# include "../settings/settings.h"
2021-01-25 19:48:11 +01:00
# include <syncthingconnector/syncthingconnection.h>
# include <syncthingconnector/syncthingservice.h>
2017-09-08 16:54:59 +02:00
# include <QNetworkReply>
using namespace Data ;
namespace QtGui {
2023-06-30 12:43:06 +02:00
/*!
* \ brief Returns whether to ignore inavailability after start or standby - wakeup .
*/
static bool ignoreInavailabilityAfterStart (
const Settings : : Settings & settings , const SyncthingLauncher * launcher , const SyncthingService * service , const QString & message , int networkError )
{
if ( ! settings . ignoreInavailabilityAfterStart ) {
return false ;
}
// ignore only certain types of errors
// note: Not sure how to check for "Forbidden" except for checking the error message.
switch ( networkError ) {
case QNetworkReply : : ConnectionRefusedError :
case QNetworkReply : : HostNotFoundError :
case QNetworkReply : : TemporaryNetworkFailureError :
case QNetworkReply : : NetworkSessionFailedError :
case QNetworkReply : : ProxyConnectionRefusedError :
case QNetworkReply : : ProxyNotFoundError :
break ;
default :
if ( message . contains ( QLatin1String ( " Forbidden " ) ) ) {
break ;
}
return false ;
} ;
// ignore inavailable shorty after the start
if ( ( launcher & & launcher - > isRunning ( ) )
# ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD
& & ( ( service & & service - > isSystemdAvailable ( )
& & ! service - > isActiveWithoutSleepFor ( launcher - > activeSince ( ) , settings . ignoreInavailabilityAfterStart ) )
| | ! launcher - > isActiveFor ( settings . ignoreInavailabilityAfterStart ) )
# else
& & ! launcher - > isActiveFor ( settings . ignoreInavailabilityAfterStart )
# endif
) {
return true ;
}
# ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD
if ( service & & ! service - > isActiveWithoutSleepFor ( settings . ignoreInavailabilityAfterStart ) ) {
return true ;
}
# endif
return false ;
}
2018-04-02 20:26:00 +02:00
/*!
* \ brief Returns whether the error is relevant . Only in this case a notification for the error should be shown .
2018-04-02 20:50:59 +02:00
* \ todo Unify with SyncthingNotifier : : isDisconnectRelevant ( ) .
2018-04-02 20:26:00 +02:00
*/
2023-06-30 12:43:06 +02:00
bool InternalError : : isRelevant ( const SyncthingConnection & connection , SyncthingErrorCategory category , const QString & message , int networkError )
2017-09-08 16:54:59 +02:00
{
2018-04-02 20:26:00 +02:00
// ignore overall connection errors when auto reconnect tries >= 1
2021-10-07 18:21:48 +02:00
if ( category ! = SyncthingErrorCategory : : OverallConnection & & category ! = SyncthingErrorCategory : : TLS & & connection . autoReconnectTries ( ) > = 1 ) {
2018-04-02 20:26:00 +02:00
return false ;
}
// skip further considerations if connection is remote
2022-10-02 19:45:22 +02:00
if ( ! connection . syncthingUrl ( ) . isEmpty ( ) & & ! connection . isLocal ( ) ) {
2018-04-02 20:26:00 +02:00
return true ;
}
2022-10-02 19:45:22 +02:00
// ignore configuration errors on first launch (to avoid greeting people with an error message)
const auto & settings = Settings : : values ( ) ;
if ( ( settings . firstLaunch | | settings . fakeFirstLaunch )
& & ( category = = SyncthingErrorCategory : : OverallConnection & & networkError = = QNetworkReply : : NoError ) ) {
return false ;
}
2018-04-02 20:26:00 +02:00
// consider process/launcher or systemd unit status
2023-04-04 17:13:27 +02:00
const auto remoteHostClosed = networkError = = QNetworkReply : : ConnectionRefusedError | | networkError = = QNetworkReply : : RemoteHostClosedError
| | networkError = = QNetworkReply : : ProxyConnectionClosedError ;
// ignore "remote host closed" error if we've just stopped Syncthing ourselves (or "connection refused" which can also be the result of stopping Syncthing ourselves)
2023-04-04 17:14:16 +02:00
const auto * launcher = SyncthingLauncher : : mainInstance ( ) ;
2018-04-11 23:15:15 +02:00
if ( settings . launcher . considerForReconnect & & remoteHostClosed & & launcher & & launcher - > isManuallyStopped ( ) ) {
2018-04-02 20:26:00 +02:00
return false ;
}
2017-09-08 16:54:59 +02:00
# ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD
2023-04-04 17:14:16 +02:00
const auto * const service = SyncthingService : : mainInstance ( ) ;
2018-04-11 23:15:15 +02:00
if ( settings . systemd . considerForReconnect & & remoteHostClosed & & service & & service - > isManuallyStopped ( ) ) {
2018-04-02 20:26:00 +02:00
return false ;
}
2018-04-02 20:50:59 +02:00
# endif
2018-04-02 20:26:00 +02:00
2023-06-30 12:43:06 +02:00
return ! ignoreInavailabilityAfterStart ( settings , launcher , service , message , networkError ) ;
2017-09-08 16:54:59 +02:00
}
2017-09-17 21:48:15 +02:00
} // namespace QtGui