Skip to content

Commit

Permalink
[bugfix] offline editing converting offline twice
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
elpaso authored and dakcarto committed Apr 22, 2016
1 parent 21a2f41 commit fa9862b
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions src/core/qgsofflineediting.cpp
Expand Up @@ -287,11 +287,14 @@ void QgsOfflineEditing::synchronize()
showWarning( remoteLayer->commitErrors().join( "\n" ) ); showWarning( remoteLayer->commitErrors().join( "\n" ) );
} }
} }

// Invalidate the connection to force a reload if the project is put offline
// again with the same path
offlineLayer->dataProvider()->invalidateConnections( QgsDataSourceURI( offlineLayer->source() ).database() );
// remove offline layer // remove offline layer
QgsMapLayerRegistry::instance()->removeMapLayers( QgsMapLayerRegistry::instance()->removeMapLayers(
( QStringList() << qgisLayerId ) ); ( QStringList() << qgisLayerId ) );



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


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


// create table // create table
QString sql = QString( "CREATE TABLE '%1' (" ).arg( tableName ); QString sql = QString( "CREATE TABLE '%1' (" ).arg( tableName );
Expand Down Expand Up @@ -541,9 +545,10 @@ QgsVectorLayer* QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlit
if ( rc == SQLITE_OK ) if ( rc == SQLITE_OK )
{ {
// add new layer // add new layer
QgsVectorLayer* newLayer = new QgsVectorLayer( QString( "dbname='%1' table='%2'%3 sql=" ) QString connectionString = QString( "dbname='%1' table='%2'%3 sql=" )
.arg( offlineDbPath, .arg( offlineDbPath,
tableName, layer->hasGeometryType() ? "(Geometry)" : "" ), tableName, layer->hasGeometryType() ? "(Geometry)" : "" );
QgsVectorLayer* newLayer = new QgsVectorLayer( connectionString,
layer->name() + " (offline)", "spatialite" ); layer->name() + " (offline)", "spatialite" );
if ( newLayer->isValid() ) if ( newLayer->isValid() )
{ {
Expand Down Expand Up @@ -1296,33 +1301,39 @@ void QgsOfflineEditing::committedGeometriesChanges( const QString& qgisLayerId,
void QgsOfflineEditing::startListenFeatureChanges() void QgsOfflineEditing::startListenFeatureChanges()
{ {
QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer *>( sender() ); QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer *>( sender() );
// enable logging // enable logging, check if editBuffer is not null
connect( vLayer->editBuffer(), SIGNAL( committedAttributesAdded( const QString&, const QList<QgsField>& ) ), if ( vLayer->editBuffer() )
this, SLOT( committedAttributesAdded( const QString&, const QList<QgsField>& ) ) ); {
connect( vLayer->editBuffer(), SIGNAL( committedAttributesAdded( const QString&, const QList<QgsField>& ) ),
this, SLOT( committedAttributesAdded( const QString&, const QList<QgsField>& ) ) );
connect( vLayer->editBuffer(), SIGNAL( committedAttributeValuesChanges( const QString&, const QgsChangedAttributesMap& ) ),
this, SLOT( committedAttributeValuesChanges( const QString&, const QgsChangedAttributesMap& ) ) );
connect( vLayer->editBuffer(), SIGNAL( committedGeometriesChanges( const QString&, const QgsGeometryMap& ) ),
this, SLOT( committedGeometriesChanges( const QString&, const QgsGeometryMap& ) ) );
}
connect( vLayer, SIGNAL( committedFeaturesAdded( const QString&, const QgsFeatureList& ) ), connect( vLayer, SIGNAL( committedFeaturesAdded( const QString&, const QgsFeatureList& ) ),
this, SLOT( committedFeaturesAdded( const QString&, const QgsFeatureList& ) ) ); this, SLOT( committedFeaturesAdded( const QString&, const QgsFeatureList& ) ) );
connect( vLayer, SIGNAL( committedFeaturesRemoved( const QString&, const QgsFeatureIds& ) ), connect( vLayer, SIGNAL( committedFeaturesRemoved( const QString&, const QgsFeatureIds& ) ),
this, SLOT( committedFeaturesRemoved( const QString&, const QgsFeatureIds& ) ) ); this, SLOT( committedFeaturesRemoved( const QString&, const QgsFeatureIds& ) ) );
connect( vLayer->editBuffer(), SIGNAL( committedAttributeValuesChanges( const QString&, const QgsChangedAttributesMap& ) ),
this, SLOT( committedAttributeValuesChanges( const QString&, const QgsChangedAttributesMap& ) ) );
connect( vLayer->editBuffer(), SIGNAL( committedGeometriesChanges( const QString&, const QgsGeometryMap& ) ),
this, SLOT( committedGeometriesChanges( const QString&, const QgsGeometryMap& ) ) );
} }


void QgsOfflineEditing::stopListenFeatureChanges() void QgsOfflineEditing::stopListenFeatureChanges()
{ {
QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer *>( sender() ); QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer *>( sender() );
// disable logging // disable logging, check if editBuffer is not null
disconnect( vLayer->editBuffer(), SIGNAL( committedAttributesAdded( const QString&, const QList<QgsField>& ) ), if ( vLayer->editBuffer() )
this, SLOT( committedAttributesAdded( const QString&, const QList<QgsField>& ) ) ); {
disconnect( vLayer->editBuffer(), SIGNAL( committedAttributesAdded( const QString&, const QList<QgsField>& ) ),
this, SLOT( committedAttributesAdded( const QString&, const QList<QgsField>& ) ) );
disconnect( vLayer->editBuffer(), SIGNAL( committedAttributeValuesChanges( const QString&, const QgsChangedAttributesMap& ) ),
this, SLOT( committedAttributeValuesChanges( const QString&, const QgsChangedAttributesMap& ) ) );
disconnect( vLayer->editBuffer(), SIGNAL( committedGeometriesChanges( const QString&, const QgsGeometryMap& ) ),
this, SLOT( committedGeometriesChanges( const QString&, const QgsGeometryMap& ) ) );
}
disconnect( vLayer, SIGNAL( committedFeaturesAdded( const QString&, const QgsFeatureList& ) ), disconnect( vLayer, SIGNAL( committedFeaturesAdded( const QString&, const QgsFeatureList& ) ),
this, SLOT( committedFeaturesAdded( const QString&, const QgsFeatureList& ) ) ); this, SLOT( committedFeaturesAdded( const QString&, const QgsFeatureList& ) ) );
disconnect( vLayer, SIGNAL( committedFeaturesRemoved( const QString&, const QgsFeatureIds& ) ), disconnect( vLayer, SIGNAL( committedFeaturesRemoved( const QString&, const QgsFeatureIds& ) ),
this, SLOT( committedFeaturesRemoved( const QString&, const QgsFeatureIds& ) ) ); this, SLOT( committedFeaturesRemoved( const QString&, const QgsFeatureIds& ) ) );
disconnect( vLayer->editBuffer(), SIGNAL( committedAttributeValuesChanges( const QString&, const QgsChangedAttributesMap& ) ),
this, SLOT( committedAttributeValuesChanges( const QString&, const QgsChangedAttributesMap& ) ) );
disconnect( vLayer->editBuffer(), SIGNAL( committedGeometriesChanges( const QString&, const QgsGeometryMap& ) ),
this, SLOT( committedGeometriesChanges( const QString&, const QgsGeometryMap& ) ) );
} }


void QgsOfflineEditing::layerAdded( QgsMapLayer* layer ) void QgsOfflineEditing::layerAdded( QgsMapLayer* layer )
Expand Down

0 comments on commit fa9862b

Please sign in to comment.