Skip to content

Commit 9cd5999

Browse files
committed
decoupling mergin api from activeProject and rebuilding connections
1 parent e0f23b2 commit 9cd5999

File tree

9 files changed

+59
-48
lines changed

9 files changed

+59
-48
lines changed

app/activeproject.cpp

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,13 @@ ActiveProject::ActiveProject( AppSettings &appSettings
3131
, ActiveLayer &activeLayer
3232
, LayersProxyModel &recordingLayerPM
3333
, LocalProjectsManager &localProjectsManager
34-
, MerginApi *merginApi
3534
, QObject *parent ) :
3635

3736
QObject( parent )
3837
, mAppSettings( appSettings )
3938
, mActiveLayer( activeLayer )
4039
, mRecordingLayerPM( recordingLayerPM )
4140
, mLocalProjectsManager( localProjectsManager )
42-
, mMerginApi( merginApi )
4341
, mProjectLoadingLog( "" )
4442
{
4543
// we used to have our own QgsProject instance, but unfortunately few pieces of qgis_core
@@ -76,24 +74,6 @@ ActiveProject::ActiveProject( AppSettings &appSettings
7674
setAutosyncEnabled( mAppSettings.autosyncAllowed() );
7775

7876
QObject::connect( &mAppSettings, &AppSettings::autosyncAllowedChanged, this, &ActiveProject::setAutosyncEnabled );
79-
80-
QObject::connect(
81-
mMerginApi,
82-
&MerginApi::projectMetadataRoleUpdated,
83-
this, [this]( const QString & projectFullName, const QString & role )
84-
{
85-
if ( projectFullName == this->projectFullName() )
86-
{
87-
setProjectRole( role );
88-
}
89-
} );
90-
91-
QObject::connect(
92-
mMerginApi,
93-
&MerginApi::authChanged,
94-
this,
95-
&ActiveProject::updateUserRoleInActiveProject
96-
);
9777
}
9878

9979
ActiveProject::~ActiveProject() = default;
@@ -204,11 +184,14 @@ bool ActiveProject::forceLoad( const QString &filePath, bool force )
204184
CoreUtils::log( QStringLiteral( "Project load" ), QStringLiteral( "Could not find project in local projects: " ) + filePath );
205185
}
206186

187+
QString role = MerginProjectMetadata::fromCachedJson( CoreUtils::getProjectMetadataPath( mLocalProject.projectDir ) ).role;
188+
qDebug() << "ROLE 1 : " << role;
189+
setProjectRole( role );
190+
207191
updateMapTheme();
208192
updateRecordingLayers();
209193
updateActiveLayer();
210194
updateMapSettingsLayers();
211-
updateUserRoleInActiveProject();
212195

213196
emit localProjectChanged( mLocalProject );
214197
emit projectReloaded( mQgsProject );
@@ -589,9 +572,3 @@ void ActiveProject::setProjectRole( const QString &role )
589572
emit projectRoleChanged();
590573
}
591574
}
592-
593-
void ActiveProject::updateUserRoleInActiveProject()
594-
{
595-
// update user's role each time a project is opened or auth changes, following #3174
596-
mMerginApi->updateProjectMetadataRole( projectFullName() );
597-
}

app/activeproject.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class ActiveProject: public QObject
4545
, ActiveLayer &activeLayer
4646
, LayersProxyModel &recordingLayerPM
4747
, LocalProjectsManager &localProjectsManager
48-
, MerginApi *mMerginApi
4948
, QObject *parent = nullptr );
5049

5150
virtual ~ActiveProject();
@@ -131,7 +130,12 @@ class ActiveProject: public QObject
131130
/**
132131
* Calls Mergin API to update current project’s role
133132
*/
134-
Q_INVOKABLE void updateUserRoleInActiveProject();
133+
void updateUserRoleInActiveProject();
134+
135+
/**
136+
* Update current project’s role
137+
*/
138+
void onProjectRoleUpdated( const QString &projectFullName, const QString &role );
135139

136140
signals:
137141
void qgsProjectChanged();

app/main.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,8 @@ int main( int argc, char *argv[] )
494494
LayersProxyModel recordingLpm( &lm, LayerModelTypes::ActiveLayerSelection );
495495

496496
ActiveLayer al;
497-
ActiveProject activeProject( as, al, recordingLpm, localProjectsManager, ma.get() );
497+
ActiveProject activeProject( as, al, recordingLpm, localProjectsManager );
498+
498499
std::unique_ptr<VariablesManager> vm( new VariablesManager( ma.get() ) );
499500
vm->registerInputExpressionFunctions();
500501

@@ -569,6 +570,27 @@ int main( int argc, char *argv[] )
569570
syncManager.syncProject( project, SyncOptions::Authorized, SyncOptions::Retry );
570571
} );
571572

573+
QObject::connect( &activeProject, &ActiveProject::projectReloaded, &lambdaContext, [merginApi = ma.get(), &activeProject]()
574+
{
575+
merginApi->reloadProjectRole( activeProject.projectFullName() );
576+
} );
577+
578+
QObject::connect( ma.get(), &MerginApi::authChanged, &lambdaContext, [merginApi = ma.get(), &activeProject]()
579+
{
580+
if ( activeProject.isProjectLoaded() )
581+
{
582+
merginApi->reloadProjectRole( activeProject.projectFullName() );
583+
}
584+
} );
585+
586+
QObject::connect( ma.get(), &MerginApi::projectRoleUpdated, &activeProject, [&activeProject]( const QString & projectFullName, const QString & role )
587+
{
588+
if ( projectFullName == activeProject.projectFullName() )
589+
{
590+
activeProject.setProjectRole( role );
591+
}
592+
} );
593+
572594
QObject::connect( ma.get(), &MerginApi::notifyInfo, &lambdaContext, [&notificationModel]( const QString & message )
573595
{
574596
notificationModel.addInfo( message );

app/test/testactiveproject.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void TestActiveProject::testProjectValidations()
3939
ActiveLayer al;
4040
LayersModel lm;
4141
LayersProxyModel lpm( &lm, LayerModelTypes::ActiveLayerSelection );
42-
ActiveProject activeProject( as, al, lpm, mApi->localProjectsManager(), mApi );
42+
ActiveProject activeProject( as, al, lpm, mApi->localProjectsManager() );
4343

4444
QSignalSpy spyReportIssues( &activeProject, &ActiveProject::reportIssue );
4545
QSignalSpy spyErrorsFound( &activeProject, &ActiveProject::loadingErrorFound );
@@ -66,7 +66,7 @@ void TestActiveProject::testProjectLoadFailure()
6666
ActiveLayer al;
6767
LayersModel lm;
6868
LayersProxyModel lpm( &lm, LayerModelTypes::ActiveLayerSelection );
69-
ActiveProject activeProject( as, al, lpm, mApi->localProjectsManager(), mApi );
69+
ActiveProject activeProject( as, al, lpm, mApi->localProjectsManager() );
7070

7171
mApi->localProjectsManager().addLocalProject( projectdir, projectname );
7272

@@ -88,7 +88,7 @@ void TestActiveProject::testPositionTrackingFlag()
8888
ActiveLayer al;
8989
LayersModel lm;
9090
LayersProxyModel lpm( &lm, LayerModelTypes::ActiveLayerSelection );
91-
ActiveProject activeProject( as, al, lpm, mApi->localProjectsManager(), mApi );
91+
ActiveProject activeProject( as, al, lpm, mApi->localProjectsManager() );
9292

9393
// project "planes" - tracking not enabled
9494
QString projectDir = TestUtils::testDataDir() + "/planes/";

app/test/testmerginapi.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2369,7 +2369,7 @@ void TestMerginApi::testAutosync()
23692369

23702370
MapThemesModel mtm; AppSettings as; ActiveLayer al;
23712371
LayersModel lm; LayersProxyModel lpm( &lm, LayerModelTypes::ActiveLayerSelection );
2372-
ActiveProject activeProject( as, al, lpm, mApi->localProjectsManager(), mApi );
2372+
ActiveProject activeProject( as, al, lpm, mApi->localProjectsManager() );
23732373

23742374
mApi->localProjectsManager().addLocalProject( projectdir, projectname );
23752375

core/coreutils.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,11 @@ QString CoreUtils::bytesToHumanSize( double bytes )
335335
return QString::number( bytes / 1024.0 / 1024.0 / 1024.0 / 1024.0, 'f', precision ) + " TB";
336336
}
337337
}
338+
339+
QString CoreUtils::getProjectMetadataPath( QString projectDir )
340+
{
341+
if ( projectDir.isEmpty() )
342+
return QString();
343+
344+
return projectDir + "/.mergin/mergin.json";
345+
}

core/coreutils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ class CoreUtils
125125
*/
126126
static QString bytesToHumanSize( double bytes );
127127

128+
/**
129+
* Returns path to project metadata file for a given project directory
130+
*/
131+
static QString getProjectMetadataPath( QString projectDir );
132+
128133
private:
129134
static QString sLogFile;
130135
static int CHECKSUM_CHUNK_SIZE;

core/merginapi.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3984,27 +3984,22 @@ DownloadQueueItem::DownloadQueueItem( const QString &fp, qint64 s, int v, qint64
39843984
tempFileName = CoreUtils::uuidWithoutBraces( QUuid::createUuid() );
39853985
}
39863986

3987-
void MerginApi::updateProjectMetadataRole( const QString &projectFullName )
3987+
void MerginApi::reloadProjectRole( const QString &projectFullName )
39883988
{
39893989
if ( projectFullName.isEmpty() )
39903990
{
39913991
return;
39923992
}
39933993

3994-
QString cachedRole = MerginApi::getCachedProjectRole( projectFullName );
3995-
39963994
QNetworkReply *reply = getProjectInfo( projectFullName );
39973995
if ( !reply )
3998-
{
3999-
emit projectMetadataRoleUpdated( projectFullName, cachedRole );
40003996
return;
4001-
}
40023997

40033998
reply->request().setAttribute( static_cast<QNetworkRequest::Attribute>( AttrProjectFullName ), projectFullName );
4004-
connect( reply, &QNetworkReply::finished, this, &MerginApi::updateProjectMetadataRoleReplyFinished );
3999+
connect( reply, &QNetworkReply::finished, this, &MerginApi::reloadProjectRoleReplyFinished );
40054000
}
40064001

4007-
void MerginApi::updateProjectMetadataRoleReplyFinished()
4002+
void MerginApi::reloadProjectRoleReplyFinished()
40084003
{
40094004
QNetworkReply *r = qobject_cast<QNetworkReply *>( sender() );
40104005
Q_ASSERT( r );
@@ -4022,7 +4017,7 @@ void MerginApi::updateProjectMetadataRoleReplyFinished()
40224017
{
40234018
if ( updateCachedProjectRole( projectFullName, role ) )
40244019
{
4025-
emit projectMetadataRoleUpdated( projectFullName, role );
4020+
emit projectRoleUpdated( projectFullName, role );
40264021
}
40274022
else
40284023
{
@@ -4032,7 +4027,7 @@ void MerginApi::updateProjectMetadataRoleReplyFinished()
40324027
}
40334028
else
40344029
{
4035-
emit projectMetadataRoleUpdated( projectFullName, cachedRole );
4030+
emit projectRoleUpdated( projectFullName, cachedRole );
40364031
}
40374032

40384033
r->deleteLater();

core/merginapi.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,9 @@ class MerginApi: public QObject
574574
bool apiSupportsWorkspaces();
575575

576576
/**
577-
* Updates project metadata role by fetching latest information from server.
577+
* Reloads project metadata role by fetching latest information from server.
578578
*/
579-
Q_INVOKABLE void updateProjectMetadataRole( const QString &projectFullName );
579+
Q_INVOKABLE void reloadProjectRole( const QString &projectFullName );
580580

581581
signals:
582582
void apiSupportsSubscriptionsChanged();
@@ -655,7 +655,7 @@ class MerginApi: public QObject
655655
void apiSupportsWorkspacesChanged();
656656

657657
void serverWasUpgraded();
658-
void projectMetadataRoleUpdated( const QString &projectFullName, const QString &role );
658+
void projectRoleUpdated( const QString &projectFullName, const QString &role );
659659

660660
private slots:
661661
void listProjectsReplyFinished( QString requestId );
@@ -795,7 +795,7 @@ class MerginApi: public QObject
795795

796796
bool projectFileHasBeenUpdated( const ProjectDiff &diff );
797797

798-
void updateProjectMetadataRoleReplyFinished();
798+
void reloadProjectRoleReplyFinished();
799799

800800
bool updateCachedProjectRole( const QString &projectFullName, const QString &newRole );
801801

0 commit comments

Comments
 (0)