diff --git a/app/activeproject.cpp b/app/activeproject.cpp index fb976aadd..098278a1d 100644 --- a/app/activeproject.cpp +++ b/app/activeproject.cpp @@ -31,7 +31,6 @@ ActiveProject::ActiveProject( AppSettings &appSettings , ActiveLayer &activeLayer , LayersProxyModel &recordingLayerPM , LocalProjectsManager &localProjectsManager - , MerginApi *merginApi , QObject *parent ) : QObject( parent ) @@ -39,7 +38,6 @@ ActiveProject::ActiveProject( AppSettings &appSettings , mActiveLayer( activeLayer ) , mRecordingLayerPM( recordingLayerPM ) , mLocalProjectsManager( localProjectsManager ) - , mMerginApi( merginApi ) , mProjectLoadingLog( "" ) { // we used to have our own QgsProject instance, but unfortunately few pieces of qgis_core @@ -76,24 +74,6 @@ ActiveProject::ActiveProject( AppSettings &appSettings setAutosyncEnabled( mAppSettings.autosyncAllowed() ); QObject::connect( &mAppSettings, &AppSettings::autosyncAllowedChanged, this, &ActiveProject::setAutosyncEnabled ); - - QObject::connect( - mMerginApi, - &MerginApi::projectMetadataRoleUpdated, - this, [this]( const QString & projectFullName, const QString & role ) - { - if ( projectFullName == this->projectFullName() ) - { - setProjectRole( role ); - } - } ); - - QObject::connect( - mMerginApi, - &MerginApi::authChanged, - this, - &ActiveProject::updateUserRoleInActiveProject - ); } ActiveProject::~ActiveProject() = default; @@ -204,11 +184,14 @@ bool ActiveProject::forceLoad( const QString &filePath, bool force ) CoreUtils::log( QStringLiteral( "Project load" ), QStringLiteral( "Could not find project in local projects: " ) + filePath ); } + QString role = MerginProjectMetadata::fromCachedJson( CoreUtils::getProjectMetadataPath( mLocalProject.projectDir ) ).role; + qDebug() << "ROLE 1 : " << role; + setProjectRole( role ); + updateMapTheme(); updateRecordingLayers(); updateActiveLayer(); updateMapSettingsLayers(); - updateUserRoleInActiveProject(); emit localProjectChanged( mLocalProject ); emit projectReloaded( mQgsProject ); @@ -589,9 +572,3 @@ void ActiveProject::setProjectRole( const QString &role ) emit projectRoleChanged(); } } - -void ActiveProject::updateUserRoleInActiveProject() -{ - // update user's role each time a project is opened or auth changes, following #3174 - mMerginApi->updateProjectMetadataRole( projectFullName() ); -} diff --git a/app/activeproject.h b/app/activeproject.h index 115c274c6..fb6084242 100644 --- a/app/activeproject.h +++ b/app/activeproject.h @@ -45,7 +45,6 @@ class ActiveProject: public QObject , ActiveLayer &activeLayer , LayersProxyModel &recordingLayerPM , LocalProjectsManager &localProjectsManager - , MerginApi *mMerginApi , QObject *parent = nullptr ); virtual ~ActiveProject(); @@ -131,7 +130,12 @@ class ActiveProject: public QObject /** * Calls Mergin API to update current project’s role */ - Q_INVOKABLE void updateUserRoleInActiveProject(); + void updateUserRoleInActiveProject(); + + /** + * Update current project’s role + */ + void onProjectRoleUpdated( const QString &projectFullName, const QString &role ); signals: void qgsProjectChanged(); diff --git a/app/main.cpp b/app/main.cpp index 7e87609b0..6f741af8f 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -494,7 +494,8 @@ int main( int argc, char *argv[] ) LayersProxyModel recordingLpm( &lm, LayerModelTypes::ActiveLayerSelection ); ActiveLayer al; - ActiveProject activeProject( as, al, recordingLpm, localProjectsManager, ma.get() ); + ActiveProject activeProject( as, al, recordingLpm, localProjectsManager ); + std::unique_ptr vm( new VariablesManager( ma.get() ) ); vm->registerInputExpressionFunctions(); @@ -569,6 +570,27 @@ int main( int argc, char *argv[] ) syncManager.syncProject( project, SyncOptions::Authorized, SyncOptions::Retry ); } ); + QObject::connect( &activeProject, &ActiveProject::projectReloaded, &lambdaContext, [merginApi = ma.get(), &activeProject]() + { + merginApi->reloadProjectRole( activeProject.projectFullName() ); + } ); + + QObject::connect( ma.get(), &MerginApi::authChanged, &lambdaContext, [merginApi = ma.get(), &activeProject]() + { + if ( activeProject.isProjectLoaded() ) + { + merginApi->reloadProjectRole( activeProject.projectFullName() ); + } + } ); + + QObject::connect( ma.get(), &MerginApi::projectRoleUpdated, &activeProject, [&activeProject]( const QString & projectFullName, const QString & role ) + { + if ( projectFullName == activeProject.projectFullName() ) + { + activeProject.setProjectRole( role ); + } + } ); + QObject::connect( ma.get(), &MerginApi::notifyInfo, &lambdaContext, [¬ificationModel]( const QString & message ) { notificationModel.addInfo( message ); diff --git a/app/test/testactiveproject.cpp b/app/test/testactiveproject.cpp index 4e6edf70c..8b14abd24 100644 --- a/app/test/testactiveproject.cpp +++ b/app/test/testactiveproject.cpp @@ -39,7 +39,7 @@ void TestActiveProject::testProjectValidations() ActiveLayer al; LayersModel lm; LayersProxyModel lpm( &lm, LayerModelTypes::ActiveLayerSelection ); - ActiveProject activeProject( as, al, lpm, mApi->localProjectsManager(), mApi ); + ActiveProject activeProject( as, al, lpm, mApi->localProjectsManager() ); QSignalSpy spyReportIssues( &activeProject, &ActiveProject::reportIssue ); QSignalSpy spyErrorsFound( &activeProject, &ActiveProject::loadingErrorFound ); @@ -66,7 +66,7 @@ void TestActiveProject::testProjectLoadFailure() ActiveLayer al; LayersModel lm; LayersProxyModel lpm( &lm, LayerModelTypes::ActiveLayerSelection ); - ActiveProject activeProject( as, al, lpm, mApi->localProjectsManager(), mApi ); + ActiveProject activeProject( as, al, lpm, mApi->localProjectsManager() ); mApi->localProjectsManager().addLocalProject( projectdir, projectname ); @@ -88,7 +88,7 @@ void TestActiveProject::testPositionTrackingFlag() ActiveLayer al; LayersModel lm; LayersProxyModel lpm( &lm, LayerModelTypes::ActiveLayerSelection ); - ActiveProject activeProject( as, al, lpm, mApi->localProjectsManager(), mApi ); + ActiveProject activeProject( as, al, lpm, mApi->localProjectsManager() ); // project "planes" - tracking not enabled QString projectDir = TestUtils::testDataDir() + "/planes/"; diff --git a/app/test/testmerginapi.cpp b/app/test/testmerginapi.cpp index 149a2f5e2..b0f89230d 100644 --- a/app/test/testmerginapi.cpp +++ b/app/test/testmerginapi.cpp @@ -2369,7 +2369,7 @@ void TestMerginApi::testAutosync() MapThemesModel mtm; AppSettings as; ActiveLayer al; LayersModel lm; LayersProxyModel lpm( &lm, LayerModelTypes::ActiveLayerSelection ); - ActiveProject activeProject( as, al, lpm, mApi->localProjectsManager(), mApi ); + ActiveProject activeProject( as, al, lpm, mApi->localProjectsManager() ); mApi->localProjectsManager().addLocalProject( projectdir, projectname ); diff --git a/core/coreutils.cpp b/core/coreutils.cpp index 834e24c12..a52a5d796 100644 --- a/core/coreutils.cpp +++ b/core/coreutils.cpp @@ -335,3 +335,11 @@ QString CoreUtils::bytesToHumanSize( double bytes ) return QString::number( bytes / 1024.0 / 1024.0 / 1024.0 / 1024.0, 'f', precision ) + " TB"; } } + +QString CoreUtils::getProjectMetadataPath( QString projectDir ) +{ + if ( projectDir.isEmpty() ) + return QString(); + + return projectDir + "/.mergin/mergin.json"; +} diff --git a/core/coreutils.h b/core/coreutils.h index f39754737..c1e82d4ad 100644 --- a/core/coreutils.h +++ b/core/coreutils.h @@ -125,6 +125,11 @@ class CoreUtils */ static QString bytesToHumanSize( double bytes ); + /** + * Returns path to project metadata file for a given project directory + */ + static QString getProjectMetadataPath( QString projectDir ); + private: static QString sLogFile; static int CHECKSUM_CHUNK_SIZE; diff --git a/core/merginapi.cpp b/core/merginapi.cpp index dc5f641dc..7b8bd7977 100644 --- a/core/merginapi.cpp +++ b/core/merginapi.cpp @@ -3984,27 +3984,22 @@ DownloadQueueItem::DownloadQueueItem( const QString &fp, qint64 s, int v, qint64 tempFileName = CoreUtils::uuidWithoutBraces( QUuid::createUuid() ); } -void MerginApi::updateProjectMetadataRole( const QString &projectFullName ) +void MerginApi::reloadProjectRole( const QString &projectFullName ) { if ( projectFullName.isEmpty() ) { return; } - QString cachedRole = MerginApi::getCachedProjectRole( projectFullName ); - QNetworkReply *reply = getProjectInfo( projectFullName ); if ( !reply ) - { - emit projectMetadataRoleUpdated( projectFullName, cachedRole ); return; - } reply->request().setAttribute( static_cast( AttrProjectFullName ), projectFullName ); - connect( reply, &QNetworkReply::finished, this, &MerginApi::updateProjectMetadataRoleReplyFinished ); + connect( reply, &QNetworkReply::finished, this, &MerginApi::reloadProjectRoleReplyFinished ); } -void MerginApi::updateProjectMetadataRoleReplyFinished() +void MerginApi::reloadProjectRoleReplyFinished() { QNetworkReply *r = qobject_cast( sender() ); Q_ASSERT( r ); @@ -4022,7 +4017,7 @@ void MerginApi::updateProjectMetadataRoleReplyFinished() { if ( updateCachedProjectRole( projectFullName, role ) ) { - emit projectMetadataRoleUpdated( projectFullName, role ); + emit projectRoleUpdated( projectFullName, role ); } else { @@ -4032,7 +4027,7 @@ void MerginApi::updateProjectMetadataRoleReplyFinished() } else { - emit projectMetadataRoleUpdated( projectFullName, cachedRole ); + emit projectRoleUpdated( projectFullName, cachedRole ); } r->deleteLater(); diff --git a/core/merginapi.h b/core/merginapi.h index c0cbaaa1b..8609cf51d 100644 --- a/core/merginapi.h +++ b/core/merginapi.h @@ -574,9 +574,9 @@ class MerginApi: public QObject bool apiSupportsWorkspaces(); /** - * Updates project metadata role by fetching latest information from server. + * Reloads project metadata role by fetching latest information from server. */ - Q_INVOKABLE void updateProjectMetadataRole( const QString &projectFullName ); + Q_INVOKABLE void reloadProjectRole( const QString &projectFullName ); signals: void apiSupportsSubscriptionsChanged(); @@ -655,7 +655,7 @@ class MerginApi: public QObject void apiSupportsWorkspacesChanged(); void serverWasUpgraded(); - void projectMetadataRoleUpdated( const QString &projectFullName, const QString &role ); + void projectRoleUpdated( const QString &projectFullName, const QString &role ); private slots: void listProjectsReplyFinished( QString requestId ); @@ -795,7 +795,7 @@ class MerginApi: public QObject bool projectFileHasBeenUpdated( const ProjectDiff &diff ); - void updateProjectMetadataRoleReplyFinished(); + void reloadProjectRoleReplyFinished(); bool updateCachedProjectRole( const QString &projectFullName, const QString &newRole );