diff --git a/src/core/qgsofflineediting.cpp b/src/core/qgsofflineediting.cpp index ac9523d6aa2c..cfb6015e13f8 100644 --- a/src/core/qgsofflineediting.cpp +++ b/src/core/qgsofflineediting.cpp @@ -287,11 +287,14 @@ void QgsOfflineEditing::synchronize() 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 QgsMapLayerRegistry::instance()->removeMapLayers( ( QStringList() << qgisLayerId ) ); + // disable offline project QString projectTitle = QgsProject::instance()->title(); projectTitle.remove( QRegExp( " \\(offline\\)$" ) ); @@ -459,6 +462,7 @@ QgsVectorLayer* QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlit return nullptr; QString tableName = layer->id(); + QgsDebugMsg( QString( "Creating offline table %1 ..." ).arg( tableName ) ); // create table QString sql = QString( "CREATE TABLE '%1' (" ).arg( tableName ); @@ -541,9 +545,10 @@ QgsVectorLayer* QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlit if ( rc == SQLITE_OK ) { // add new layer - QgsVectorLayer* newLayer = new QgsVectorLayer( QString( "dbname='%1' table='%2'%3 sql=" ) - .arg( offlineDbPath, - tableName, layer->hasGeometryType() ? "(Geometry)" : "" ), + QString connectionString = QString( "dbname='%1' table='%2'%3 sql=" ) + .arg( offlineDbPath, + tableName, layer->hasGeometryType() ? "(Geometry)" : "" ); + QgsVectorLayer* newLayer = new QgsVectorLayer( connectionString, layer->name() + " (offline)", "spatialite" ); if ( newLayer->isValid() ) { @@ -1296,33 +1301,39 @@ void QgsOfflineEditing::committedGeometriesChanges( const QString& qgisLayerId, void QgsOfflineEditing::startListenFeatureChanges() { QgsVectorLayer* vLayer = qobject_cast( sender() ); - // enable logging - connect( vLayer->editBuffer(), SIGNAL( committedAttributesAdded( const QString&, const QList& ) ), - this, SLOT( committedAttributesAdded( const QString&, const QList& ) ) ); + // enable logging, check if editBuffer is not null + if ( vLayer->editBuffer() ) + { + connect( vLayer->editBuffer(), SIGNAL( committedAttributesAdded( const QString&, const QList& ) ), + this, SLOT( committedAttributesAdded( const QString&, const QList& ) ) ); + 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& ) ), this, SLOT( committedFeaturesAdded( const QString&, const QgsFeatureList& ) ) ); connect( vLayer, SIGNAL( 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() { QgsVectorLayer* vLayer = qobject_cast( sender() ); - // disable logging - disconnect( vLayer->editBuffer(), SIGNAL( committedAttributesAdded( const QString&, const QList& ) ), - this, SLOT( committedAttributesAdded( const QString&, const QList& ) ) ); + // disable logging, check if editBuffer is not null + if ( vLayer->editBuffer() ) + { + disconnect( vLayer->editBuffer(), SIGNAL( committedAttributesAdded( const QString&, const QList& ) ), + this, SLOT( committedAttributesAdded( const QString&, const QList& ) ) ); + 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& ) ), this, SLOT( committedFeaturesAdded( const QString&, const QgsFeatureList& ) ) ); disconnect( vLayer, SIGNAL( 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 )