Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 60 additions & 1 deletion src/core/qfieldcloud/qfieldcloudprojectsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkInformation>
#include <QNetworkReply>
#include <QSettings>
#include <QTemporaryFile>
Expand All @@ -41,6 +42,24 @@ QFieldCloudProjectsModel::QFieldCloudProjectsModel()
{
// TODO all of these connects are a bit too much, and I guess not very precise, should be refactored!

QNetworkInformation::loadBackendByFeatures( QNetworkInformation::Feature::Reachability );
if ( QNetworkInformation *info = QNetworkInformation::instance() )
{
connect( info, &QNetworkInformation::reachabilityChanged, this, [this]( QNetworkInformation::Reachability ) {
if ( !networkLooksActive() || mPendingPushes.isEmpty() )
return;

// Copying so we dont fight with new entries
const auto pending = mPendingPushes;
mPendingPushes.clear();

for ( auto it = pending.cbegin(); it != pending.cend(); ++it )
{
projectPush( it.key(), it.value() );
}
} );
}

connect( this, &QFieldCloudProjectsModel::dataChanged, this, [this]( const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles ) {
Q_UNUSED( bottomRight )
Q_UNUSED( roles )
Expand Down Expand Up @@ -315,6 +334,33 @@ void QFieldCloudProjectsModel::projectPackageAndDownload( const QString &project
emit dataChanged( projectIndex, projectIndex );
}

bool QFieldCloudProjectsModel::networkLooksActive() const
{
QNetworkInformation *info = QNetworkInformation::instance();
if ( !info->supports( QNetworkInformation::Feature::Reachability ) )
{
// No backend or no reachability support, dont change behaviour
return true;
}

switch ( info->reachability() )
{
case QNetworkInformation::Reachability::Online:
return true;

case QNetworkInformation::Reachability::Unknown:
// treat as active to avoid blocking pushes if OS cant tell
return true;

case QNetworkInformation::Reachability::Disconnected:
case QNetworkInformation::Reachability::Local:
case QNetworkInformation::Reachability::Site:
return false;
}

return true;
}

void QFieldCloudProjectsModel::projectPush( const QString &projectId, const bool shouldDownloadUpdates )
{
const QModelIndex projectIndex = findProjectIndex( projectId );
Expand All @@ -323,8 +369,21 @@ void QFieldCloudProjectsModel::projectPush( const QString &projectId, const bool
return;

QFieldCloudProject *project = mProjects[projectIndex.row()];
if ( !project )
return;

//if not active, queue + warn and return
if ( !networkLooksActive() )
{
const bool mergedFlag = mPendingPushes.value( projectId, false ) || shouldDownloadUpdates;
mPendingPushes.insert( projectId, mergedFlag );

if ( !( project->status() == QFieldCloudProject::ProjectStatus::Idle ) )
emit warning( tr( "Network is not currently active. "
"We will push the changes automatically once you are back online." ) );
return;
}

if ( project->status() != QFieldCloudProject::ProjectStatus::Idle )
return;

project->push( shouldDownloadUpdates );
Expand Down
4 changes: 4 additions & 0 deletions src/core/qfieldcloud/qfieldcloudprojectsmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "qgsgpkgflusher.h"

#include <QAbstractListModel>
#include <QHash>
#include <QJsonArray>
#include <QNetworkReply>
#include <QPointer>
Expand Down Expand Up @@ -252,6 +253,9 @@ class QFieldCloudProjectsModel : public QAbstractListModel
QString mUrl;

const int mProjectsPerFetch = 250;

bool networkLooksActive() const;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have one minor naming suggestion regarding the networkLooksActive() method.
Since the method is actually checking QNetworkInformation::reachability(), a name that reflects the reachability aspect might make the intent clearer to future readers.
Something like isReachableToCloud()

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely, thankyou for the input @mohsenD98 😄

QHash<QString, bool> mPendingPushes;
};

/**
Expand Down
Loading