@@ -132,52 +132,53 @@ void QgsOgrProvider::repack()
132
132
QByteArray layerName = OGR_FD_GetName ( OGR_L_GetLayerDefn ( ogrOrigLayer ) );
133
133
134
134
// run REPACK on shape files
135
- if ( mDataModified )
136
- {
137
- QByteArray sql = QByteArray ( " REPACK " ) + layerName; // don't quote the layer name as it works with spaces in the name and won't work if the name is quoted
138
- QgsDebugMsg ( QString ( " SQL: %1" ).arg ( FROM8 ( sql ) ) );
139
- OGR_DS_ExecuteSQL ( ogrDataSource, sql.constData (), NULL , NULL );
135
+ QByteArray sql = QByteArray ( " REPACK " ) + layerName; // don't quote the layer name as it works with spaces in the name and won't work if the name is quoted
136
+ QgsDebugMsg ( QString ( " SQL: %1" ).arg ( FROM8 ( sql ) ) );
137
+ OGR_DS_ExecuteSQL ( ogrDataSource, sql.constData (), NULL , NULL );
140
138
141
- if ( mFilePath .endsWith ( " .shp" , Qt::CaseInsensitive ) || mFilePath .endsWith ( " .dbf" , Qt::CaseInsensitive ) )
139
+ if ( mFilePath .endsWith ( " .shp" , Qt::CaseInsensitive ) || mFilePath .endsWith ( " .dbf" , Qt::CaseInsensitive ) )
140
+ {
141
+ QString packedDbf ( mFilePath .left ( mFilePath .size () - 4 ) + " _packed.dbf" );
142
+ if ( QFile::exists ( packedDbf ) )
142
143
{
143
- QString packedDbf ( mFilePath .left ( mFilePath .size () - 4 ) + " _packed.dbf" );
144
- if ( QFile::exists ( packedDbf ) )
145
- {
146
- QgsMessageLog::logMessage ( tr ( " Possible corruption after REPACK detected. %1 still exists. This may point to a permission or locking problem of the original DBF." ).arg ( packedDbf ), tr ( " OGR" ), QgsMessageLog::CRITICAL );
144
+ QgsMessageLog::logMessage ( tr ( " Possible corruption after REPACK detected. %1 still exists. This may point to a permission or locking problem of the original DBF." ).arg ( packedDbf ), tr ( " OGR" ), QgsMessageLog::CRITICAL );
147
145
148
- OGR_DS_Destroy ( ogrDataSource );
149
- ogrLayer = ogrOrigLayer = 0 ;
146
+ OGR_DS_Destroy ( ogrDataSource );
147
+ ogrLayer = ogrOrigLayer = 0 ;
150
148
151
- ogrDataSource = OGROpen ( TO8F ( mFilePath ), true , NULL );
152
- if ( ogrDataSource )
149
+ ogrDataSource = OGROpen ( TO8F ( mFilePath ), true , NULL );
150
+ if ( ogrDataSource )
151
+ {
152
+ if ( mLayerName .isNull () )
153
153
{
154
- if ( mLayerName .isNull () )
155
- {
156
- ogrOrigLayer = OGR_DS_GetLayer ( ogrDataSource, mLayerIndex );
157
- }
158
- else
159
- {
160
- ogrOrigLayer = OGR_DS_GetLayerByName ( ogrDataSource, TO8 ( mLayerName ) );
161
- }
162
-
163
- if ( !ogrOrigLayer )
164
- {
165
- QgsMessageLog::logMessage ( tr ( " Original layer could not be reopened." ), tr ( " OGR" ), QgsMessageLog::CRITICAL );
166
- valid = false ;
167
- }
168
-
169
- ogrLayer = ogrOrigLayer;
154
+ ogrOrigLayer = OGR_DS_GetLayer ( ogrDataSource, mLayerIndex );
170
155
}
171
156
else
172
157
{
173
- QgsMessageLog::logMessage ( tr ( " Original datasource could not be reopened." ), tr ( " OGR" ), QgsMessageLog::CRITICAL );
174
- valid = false ;
158
+ ogrOrigLayer = OGR_DS_GetLayerByName ( ogrDataSource, TO8 ( mLayerName ) );
175
159
}
160
+
161
+ if ( !ogrOrigLayer )
162
+ {
163
+ QgsMessageLog::logMessage ( tr ( " Original layer could not be reopened." ), tr ( " OGR" ), QgsMessageLog::CRITICAL );
164
+ mValid = false ;
165
+ }
166
+
167
+ ogrLayer = ogrOrigLayer;
168
+ }
169
+ else
170
+ {
171
+ QgsMessageLog::logMessage ( tr ( " Original datasource could not be reopened." ), tr ( " OGR" ), QgsMessageLog::CRITICAL );
172
+ mValid = false ;
176
173
}
177
174
}
178
175
179
- mDataModified = false ;
180
176
}
177
+
178
+ long oldcount = mFeaturesCounted ;
179
+ recalculateFeatureCount ();
180
+ if ( oldcount != mFeaturesCounted )
181
+ emit dataChanged ();
181
182
}
182
183
183
184
@@ -265,14 +266,12 @@ QgsOgrProvider::QgsOgrProvider( QString const & uri )
265
266
, mIsSubLayer( false )
266
267
, mOgrGeometryTypeFilter( wkbUnknown )
267
268
, ogrDriver( 0 )
268
- , valid ( false )
269
+ , mValid ( false )
269
270
, geomType( wkbUnknown )
270
- , featuresCounted( -1 )
271
- , mDataModified( false )
271
+ , mFeaturesCounted( -1 )
272
272
, mWriteAccess( false )
273
+ , mShapefileMayBeCorrupted( false )
273
274
{
274
- QgsCPLErrorHandler handler;
275
-
276
275
QgsApplication::registerOgrDrivers ();
277
276
278
277
QSettings settings;
@@ -414,8 +413,8 @@ QgsOgrProvider::QgsOgrProvider( QString const & uri )
414
413
// check that the initial encoding setting is fit for this layer
415
414
setEncoding ( encoding () );
416
415
417
- valid = setSubsetString ( mSubsetString );
418
- if ( valid )
416
+ mValid = setSubsetString ( mSubsetString );
417
+ if ( mValid )
419
418
{
420
419
QgsDebugMsg ( " Data source is valid" );
421
420
}
@@ -480,7 +479,7 @@ bool QgsOgrProvider::setSubsetString( QString theSQL, bool updateFeatureCount )
480
479
{
481
480
QgsCPLErrorHandler handler;
482
481
483
- if ( theSQL == mSubsetString && featuresCounted >= 0 )
482
+ if ( theSQL == mSubsetString && mFeaturesCounted >= 0 )
484
483
return true ;
485
484
486
485
OGRLayerH prevLayer = ogrLayer;
@@ -609,7 +608,7 @@ OGRwkbGeometryType QgsOgrProvider::ogrWkbGeometryTypeFromName( QString typeName
609
608
QStringList QgsOgrProvider::subLayers () const
610
609
{
611
610
QgsDebugMsg ( " Entered." );
612
- if ( !valid )
611
+ if ( !mValid )
613
612
{
614
613
return QStringList ();
615
614
}
@@ -954,7 +953,7 @@ QGis::WkbType QgsOgrProvider::geometryType() const
954
953
*/
955
954
long QgsOgrProvider::featureCount () const
956
955
{
957
- return featuresCounted ;
956
+ return mFeaturesCounted ;
958
957
}
959
958
960
959
@@ -969,7 +968,7 @@ const QgsFields & QgsOgrProvider::fields() const
969
968
// actually has features
970
969
bool QgsOgrProvider::isValid ()
971
970
{
972
- return valid ;
971
+ return mValid ;
973
972
}
974
973
975
974
bool QgsOgrProvider::addFeature ( QgsFeature& f )
@@ -1347,6 +1346,7 @@ bool QgsOgrProvider::changeGeometryValues( QgsGeometryMap & geometry_map )
1347
1346
theNewGeometry = 0 ;
1348
1347
continue ;
1349
1348
}
1349
+ mShapefileMayBeCorrupted = true ;
1350
1350
1351
1351
OGR_F_Destroy ( theOGRFeature );
1352
1352
}
@@ -1389,8 +1389,6 @@ bool QgsOgrProvider::createAttributeIndex( int field )
1389
1389
1390
1390
bool QgsOgrProvider::deleteFeatures ( const QgsFeatureIds & id )
1391
1391
{
1392
- QgsCPLErrorHandler handler;
1393
-
1394
1392
bool returnvalue = true ;
1395
1393
for ( QgsFeatureIds::const_iterator it = id.begin (); it != id.end (); ++it )
1396
1394
{
@@ -1432,6 +1430,8 @@ bool QgsOgrProvider::deleteFeature( QgsFeatureId id )
1432
1430
return false ;
1433
1431
}
1434
1432
1433
+ mShapefileMayBeCorrupted = true ;
1434
+
1435
1435
return true ;
1436
1436
}
1437
1437
@@ -2457,6 +2457,11 @@ bool QgsOgrProvider::syncToDisc()
2457
2457
pushError ( tr ( " OGR error syncing to disk: %1" ).arg ( CPLGetLastErrorMsg () ) );
2458
2458
}
2459
2459
2460
+ if ( mShapefileMayBeCorrupted )
2461
+ repack ();
2462
+
2463
+ mShapefileMayBeCorrupted = false ;
2464
+
2460
2465
// for shapefiles: is there already a spatial index?
2461
2466
if ( !mFilePath .isEmpty () )
2462
2467
{
@@ -2478,8 +2483,6 @@ bool QgsOgrProvider::syncToDisc()
2478
2483
}
2479
2484
}
2480
2485
2481
- mDataModified = true ;
2482
-
2483
2486
return true ;
2484
2487
}
2485
2488
@@ -2496,11 +2499,11 @@ void QgsOgrProvider::recalculateFeatureCount()
2496
2499
// so we remove it if there's any and then put it back
2497
2500
if ( mOgrGeometryTypeFilter == wkbUnknown )
2498
2501
{
2499
- featuresCounted = OGR_L_GetFeatureCount ( ogrLayer, true );
2502
+ mFeaturesCounted = OGR_L_GetFeatureCount ( ogrLayer, true );
2500
2503
}
2501
2504
else
2502
2505
{
2503
- featuresCounted = 0 ;
2506
+ mFeaturesCounted = 0 ;
2504
2507
OGR_L_ResetReading ( ogrLayer );
2505
2508
setRelevantFields ( ogrLayer, true , QgsAttributeList () );
2506
2509
OGR_L_ResetReading ( ogrLayer );
@@ -2511,7 +2514,7 @@ void QgsOgrProvider::recalculateFeatureCount()
2511
2514
if ( geom )
2512
2515
{
2513
2516
OGRwkbGeometryType gType = OGR_G_GetGeometryType ( geom );
2514
- if ( gType == mOgrGeometryTypeFilter ) featuresCounted ++;
2517
+ if ( gType == mOgrGeometryTypeFilter ) mFeaturesCounted ++;
2515
2518
}
2516
2519
OGR_F_Destroy ( fet );
2517
2520
}
0 commit comments