Skip to content

Commit fa9862b

Browse files
elpasodakcarto
authored andcommitted
[bugfix] offline editing converting offline twice
Fixes #10537: Converting offline twice within the same instance causing unusable offline state. The problem was due to spatialite connection not being invalidated. When the new offline layer is re-created the provider connection doesn't pick the latest commits. Funded by Boundless (cherry-picked from f045492)
1 parent 21a2f41 commit fa9862b

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

src/core/qgsofflineediting.cpp

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,14 @@ void QgsOfflineEditing::synchronize()
287287
showWarning( remoteLayer->commitErrors().join( "\n" ) );
288288
}
289289
}
290-
290+
// Invalidate the connection to force a reload if the project is put offline
291+
// again with the same path
292+
offlineLayer->dataProvider()->invalidateConnections( QgsDataSourceURI( offlineLayer->source() ).database() );
291293
// remove offline layer
292294
QgsMapLayerRegistry::instance()->removeMapLayers(
293295
( QStringList() << qgisLayerId ) );
294296

297+
295298
// disable offline project
296299
QString projectTitle = QgsProject::instance()->title();
297300
projectTitle.remove( QRegExp( " \\(offline\\)$" ) );
@@ -459,6 +462,7 @@ QgsVectorLayer* QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlit
459462
return nullptr;
460463

461464
QString tableName = layer->id();
465+
QgsDebugMsg( QString( "Creating offline table %1 ..." ).arg( tableName ) );
462466

463467
// create table
464468
QString sql = QString( "CREATE TABLE '%1' (" ).arg( tableName );
@@ -541,9 +545,10 @@ QgsVectorLayer* QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlit
541545
if ( rc == SQLITE_OK )
542546
{
543547
// add new layer
544-
QgsVectorLayer* newLayer = new QgsVectorLayer( QString( "dbname='%1' table='%2'%3 sql=" )
545-
.arg( offlineDbPath,
546-
tableName, layer->hasGeometryType() ? "(Geometry)" : "" ),
548+
QString connectionString = QString( "dbname='%1' table='%2'%3 sql=" )
549+
.arg( offlineDbPath,
550+
tableName, layer->hasGeometryType() ? "(Geometry)" : "" );
551+
QgsVectorLayer* newLayer = new QgsVectorLayer( connectionString,
547552
layer->name() + " (offline)", "spatialite" );
548553
if ( newLayer->isValid() )
549554
{
@@ -1296,33 +1301,39 @@ void QgsOfflineEditing::committedGeometriesChanges( const QString& qgisLayerId,
12961301
void QgsOfflineEditing::startListenFeatureChanges()
12971302
{
12981303
QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer *>( sender() );
1299-
// enable logging
1300-
connect( vLayer->editBuffer(), SIGNAL( committedAttributesAdded( const QString&, const QList<QgsField>& ) ),
1301-
this, SLOT( committedAttributesAdded( const QString&, const QList<QgsField>& ) ) );
1304+
// enable logging, check if editBuffer is not null
1305+
if ( vLayer->editBuffer() )
1306+
{
1307+
connect( vLayer->editBuffer(), SIGNAL( committedAttributesAdded( const QString&, const QList<QgsField>& ) ),
1308+
this, SLOT( committedAttributesAdded( const QString&, const QList<QgsField>& ) ) );
1309+
connect( vLayer->editBuffer(), SIGNAL( committedAttributeValuesChanges( const QString&, const QgsChangedAttributesMap& ) ),
1310+
this, SLOT( committedAttributeValuesChanges( const QString&, const QgsChangedAttributesMap& ) ) );
1311+
connect( vLayer->editBuffer(), SIGNAL( committedGeometriesChanges( const QString&, const QgsGeometryMap& ) ),
1312+
this, SLOT( committedGeometriesChanges( const QString&, const QgsGeometryMap& ) ) );
1313+
}
13021314
connect( vLayer, SIGNAL( committedFeaturesAdded( const QString&, const QgsFeatureList& ) ),
13031315
this, SLOT( committedFeaturesAdded( const QString&, const QgsFeatureList& ) ) );
13041316
connect( vLayer, SIGNAL( committedFeaturesRemoved( const QString&, const QgsFeatureIds& ) ),
13051317
this, SLOT( committedFeaturesRemoved( const QString&, const QgsFeatureIds& ) ) );
1306-
connect( vLayer->editBuffer(), SIGNAL( committedAttributeValuesChanges( const QString&, const QgsChangedAttributesMap& ) ),
1307-
this, SLOT( committedAttributeValuesChanges( const QString&, const QgsChangedAttributesMap& ) ) );
1308-
connect( vLayer->editBuffer(), SIGNAL( committedGeometriesChanges( const QString&, const QgsGeometryMap& ) ),
1309-
this, SLOT( committedGeometriesChanges( const QString&, const QgsGeometryMap& ) ) );
13101318
}
13111319

13121320
void QgsOfflineEditing::stopListenFeatureChanges()
13131321
{
13141322
QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer *>( sender() );
1315-
// disable logging
1316-
disconnect( vLayer->editBuffer(), SIGNAL( committedAttributesAdded( const QString&, const QList<QgsField>& ) ),
1317-
this, SLOT( committedAttributesAdded( const QString&, const QList<QgsField>& ) ) );
1323+
// disable logging, check if editBuffer is not null
1324+
if ( vLayer->editBuffer() )
1325+
{
1326+
disconnect( vLayer->editBuffer(), SIGNAL( committedAttributesAdded( const QString&, const QList<QgsField>& ) ),
1327+
this, SLOT( committedAttributesAdded( const QString&, const QList<QgsField>& ) ) );
1328+
disconnect( vLayer->editBuffer(), SIGNAL( committedAttributeValuesChanges( const QString&, const QgsChangedAttributesMap& ) ),
1329+
this, SLOT( committedAttributeValuesChanges( const QString&, const QgsChangedAttributesMap& ) ) );
1330+
disconnect( vLayer->editBuffer(), SIGNAL( committedGeometriesChanges( const QString&, const QgsGeometryMap& ) ),
1331+
this, SLOT( committedGeometriesChanges( const QString&, const QgsGeometryMap& ) ) );
1332+
}
13181333
disconnect( vLayer, SIGNAL( committedFeaturesAdded( const QString&, const QgsFeatureList& ) ),
13191334
this, SLOT( committedFeaturesAdded( const QString&, const QgsFeatureList& ) ) );
13201335
disconnect( vLayer, SIGNAL( committedFeaturesRemoved( const QString&, const QgsFeatureIds& ) ),
13211336
this, SLOT( committedFeaturesRemoved( const QString&, const QgsFeatureIds& ) ) );
1322-
disconnect( vLayer->editBuffer(), SIGNAL( committedAttributeValuesChanges( const QString&, const QgsChangedAttributesMap& ) ),
1323-
this, SLOT( committedAttributeValuesChanges( const QString&, const QgsChangedAttributesMap& ) ) );
1324-
disconnect( vLayer->editBuffer(), SIGNAL( committedGeometriesChanges( const QString&, const QgsGeometryMap& ) ),
1325-
this, SLOT( committedGeometriesChanges( const QString&, const QgsGeometryMap& ) ) );
13261337
}
13271338

13281339
void QgsOfflineEditing::layerAdded( QgsMapLayer* layer )

0 commit comments

Comments
 (0)