Skip to content

Commit aa69737

Browse files
committedAug 20, 2017
Prevent import layer over itself
1 parent f867b65 commit aa69737

File tree

1 file changed

+61
-52
lines changed

1 file changed

+61
-52
lines changed
 

‎src/providers/ogr/qgsgeopackagedataitems.cpp

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,6 @@ bool QgsGeoPackageConnectionItem::handleDrop( const QMimeData *data, Qt::DropAct
321321
if ( !QgsMimeDataUtils::isUriList( data ) )
322322
return false;
323323

324-
// TODO: probably should show a GUI with settings etc
325324
QString uri;
326325

327326
QStringList importResults;
@@ -332,71 +331,81 @@ bool QgsGeoPackageConnectionItem::handleDrop( const QMimeData *data, Qt::DropAct
332331
{
333332
if ( u.layerType == QStringLiteral( "vector" ) )
334333
{
335-
// open the source layer
336-
bool owner;
337-
QString error;
338-
QgsVectorLayer *srcLayer = u.vectorLayer( owner, error );
339-
if ( !srcLayer )
334+
// Check that we are not copying over self
335+
if ( u.uri.startsWith( mPath ) )
340336
{
341-
importResults.append( tr( "%1: %2" ).arg( u.name ).arg( error ) );
337+
importResults.append( tr( "You cannot import layer %1 over itself!" ).arg( u.name ) );
342338
hasError = true;
343-
continue;
344-
}
345339

346-
if ( srcLayer->isValid() )
340+
}
341+
else
347342
{
348-
uri = mPath;
349-
QgsDebugMsgLevel( "URI " + uri, 3 );
343+
// open the source layer
344+
bool owner;
345+
QString error;
346+
QgsVectorLayer *srcLayer = u.vectorLayer( owner, error );
347+
if ( !srcLayer )
348+
{
349+
importResults.append( tr( "%1: %2" ).arg( u.name ).arg( error ) );
350+
hasError = true;
351+
continue;
352+
}
350353

351-
// check if the destination layer already exists
352-
bool exists = false;
353-
// Q_FOREACH won't detach ...
354-
for ( const auto child : children() )
354+
if ( srcLayer->isValid() )
355355
{
356-
if ( child->name() == u.name )
356+
uri = mPath;
357+
QgsDebugMsgLevel( "URI " + uri, 3 );
358+
359+
// check if the destination layer already exists
360+
bool exists = false;
361+
// Q_FOREACH won't detach ...
362+
for ( const auto child : children() )
357363
{
358-
exists = true;
364+
if ( child->name() == u.name )
365+
{
366+
exists = true;
367+
}
359368
}
360-
}
361-
if ( ! exists || QMessageBox::question( nullptr, tr( "Overwrite Layer" ),
362-
tr( "Destination layer <b>%1</b> already exists. Do you want to overwrite it?" ).arg( u.name ), QMessageBox::Yes | QMessageBox::No ) == QMessageBox::Yes )
363-
{
369+
if ( ! exists || QMessageBox::question( nullptr, tr( "Overwrite Layer" ),
370+
tr( "Destination layer <b>%1</b> already exists. Do you want to overwrite it?" ).arg( u.name ), QMessageBox::Yes | QMessageBox::No ) == QMessageBox::Yes )
371+
{
364372

365-
QVariantMap options;
366-
options.insert( QStringLiteral( "driverName" ), QStringLiteral( "GPKG" ) );
367-
options.insert( QStringLiteral( "update" ), true );
368-
options.insert( QStringLiteral( "overwrite" ), true );
369-
options.insert( QStringLiteral( "layerName" ), u.name );
373+
QVariantMap options;
374+
options.insert( QStringLiteral( "driverName" ), QStringLiteral( "GPKG" ) );
375+
options.insert( QStringLiteral( "update" ), true );
376+
options.insert( QStringLiteral( "overwrite" ), true );
377+
options.insert( QStringLiteral( "layerName" ), u.name );
370378

371-
std::unique_ptr< QgsVectorLayerExporterTask > exportTask( new QgsVectorLayerExporterTask( srcLayer, uri, QStringLiteral( "ogr" ), srcLayer->crs(), options, owner ) );
379+
std::unique_ptr< QgsVectorLayerExporterTask > exportTask( new QgsVectorLayerExporterTask( srcLayer, uri, QStringLiteral( "ogr" ), srcLayer->crs(), options, owner ) );
372380

373-
// when export is successful:
374-
connect( exportTask.get(), &QgsVectorLayerExporterTask::exportComplete, this, [ = ]()
375-
{
376-
// this is gross - TODO - find a way to get access to messageBar from data items
377-
QMessageBox::information( nullptr, tr( "Import to GeoPackage database" ), tr( "Import was successful." ) );
378-
refreshConnections();
379-
} );
380-
381-
// when an error occurs:
382-
connect( exportTask.get(), &QgsVectorLayerExporterTask::errorOccurred, this, [ = ]( int error, const QString & errorMessage )
383-
{
384-
if ( error != QgsVectorLayerExporter::ErrUserCanceled )
381+
// when export is successful:
382+
connect( exportTask.get(), &QgsVectorLayerExporterTask::exportComplete, this, [ = ]()
385383
{
386-
QgsMessageOutput *output = QgsMessageOutput::createMessageOutput();
387-
output->setTitle( tr( "Import to GeoPackage database" ) );
388-
output->setMessage( tr( "Failed to import some layers!\n\n" ) + errorMessage, QgsMessageOutput::MessageText );
389-
output->showMessage();
390-
}
391-
} );
384+
// this is gross - TODO - find a way to get access to messageBar from data items
385+
QMessageBox::information( nullptr, tr( "Import to GeoPackage database" ), tr( "Import was successful." ) );
386+
refreshConnections();
387+
} );
392388

393-
QgsApplication::taskManager()->addTask( exportTask.release() );
389+
// when an error occurs:
390+
connect( exportTask.get(), &QgsVectorLayerExporterTask::errorOccurred, this, [ = ]( int error, const QString & errorMessage )
391+
{
392+
if ( error != QgsVectorLayerExporter::ErrUserCanceled )
393+
{
394+
QgsMessageOutput *output = QgsMessageOutput::createMessageOutput();
395+
output->setTitle( tr( "Import to GeoPackage database" ) );
396+
output->setMessage( tr( "Failed to import some layers!\n\n" ) + errorMessage, QgsMessageOutput::MessageText );
397+
output->showMessage();
398+
}
399+
} );
400+
401+
QgsApplication::taskManager()->addTask( exportTask.release() );
402+
}
403+
}
404+
else
405+
{
406+
importResults.append( tr( "%1: Not a valid layer!" ).arg( u.name ) );
407+
hasError = true;
394408
}
395-
}
396-
else
397-
{
398-
importResults.append( tr( "%1: Not a valid layer!" ).arg( u.name ) );
399-
hasError = true;
400409
}
401410
}
402411
else

0 commit comments

Comments
 (0)
Please sign in to comment.