Skip to content

Commit

Permalink
followup r13591: force KML encoding to utf-8 in save as dialog and mo…
Browse files Browse the repository at this point in the history
…ve filename/encoding validation to vector file write

git-svn-id: http://svn.osgeo.org/qgis/trunk@13592 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed May 29, 2010
1 parent 4e207a1 commit 24ce8da
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 67 deletions.
10 changes: 10 additions & 0 deletions src/app/ogr/qgsvectorlayersaveasdialog.cpp
Expand Up @@ -74,6 +74,16 @@ void QgsVectorLayerSaveAsDialog::on_mFormatComboBox_currentIndexChanged( int idx
{
browseFilename->setEnabled( true );
leFilename->setEnabled( true );

if( format() == "KML" )
{
mEncodingComboBox->setCurrentIndex( mEncodingComboBox->findText( "UTF-8" ) );
mEncodingComboBox->setDisabled( true );
}
else
{
mEncodingComboBox->setEnabled( true );
}
}

void QgsVectorLayerSaveAsDialog::on_browseFilename_clicked()
Expand Down
62 changes: 1 addition & 61 deletions src/app/qgisapp.cpp
Expand Up @@ -2700,19 +2700,6 @@ void QgisApp::loadOGRSublayers( QString layertype, QString uri, QStringList list
}
}

/** This helper checks to see whether the file name appears to be a valid vector file name */
bool QgisApp::isValidShapeFileName( QString theFileNameQString )
{
return theFileNameQString.endsWith( ".shp", Qt::CaseInsensitive );
}

/** Overloaded of the above function provided for convenience that takes a qstring pointer */
bool QgisApp::isValidShapeFileName( QString * theFileNameQString )
{
//dereference and delegate
return isValidShapeFileName( *theFileNameQString );
}

#ifndef HAVE_POSTGRESQL
void QgisApp::addDatabaseLayer() {}
#else
Expand Down Expand Up @@ -3030,31 +3017,7 @@ void QgisApp::newVectorLayer()
openFileDialog->selectFilter( lastUsedFilter );
}

int res;
while (( res = openFileDialog->exec() ) == QDialog::Accepted )
{
fileName = openFileDialog->selectedFiles().first();

if ( fileformat == "ESRI Shapefile" )
{
if ( !isValidShapeFileName( fileName ) )
{
fileName += ".shp";
}

if ( !isValidShapeFileName( fileName ) )
{
QMessageBox::information( this,
tr( "New Shapefile" ),
tr( "Shapefiles must end on .shp" ) );
continue;
}
}

break;
}

if ( res == QDialog::Rejected )
if( openFileDialog->exec() == QDialog::Rejected )
{
delete openFileDialog;
return;
Expand Down Expand Up @@ -3864,29 +3827,6 @@ void QgisApp::saveAsVectorFileGeneral( bool saveOnlySelection )
destCRS = QgsCoordinateReferenceSystem( dialog->crs(), QgsCoordinateReferenceSystem::InternalCrsId );
}

// overwrite the file - user will already have been prompted
// to verify they want to overwrite by the file dialog above
// might not even exists in the given case.
// add the extension if not present
if ( format == "ESRI Shapefile" )
{
if ( !vectorFilename.endsWith( ".shp", Qt::CaseInsensitive ) )
{
vectorFilename += ".shp";
}
QgsVectorFileWriter::deleteShapeFile( vectorFilename );
}

//GE does not open files without extensions. Therefore we append it automatically for kml files
if ( format == "KML" )
{
if ( !vectorFilename.endsWith( ".kml", Qt::CaseInsensitive ) )
{
vectorFilename += ".kml";
}
encoding = "UTF-8";
}

// ok if the file existed it should be deleted now so we can continue...
QApplication::setOverrideCursor( Qt::WaitCursor );

Expand Down
6 changes: 1 addition & 5 deletions src/app/qgisapp.h
Expand Up @@ -742,11 +742,7 @@ class QgisApp : public QMainWindow
* It won't force a refresh.
*/
bool addRasterLayer( QgsRasterLayer * theRasterLayer );
//@todo We should move these next two into vector layer class
/** This helper checks to see whether the file name appears to be a valid shape file name */
bool isValidShapeFileName( QString theFileNameQString );
/** Overloaded version of the above function provided for convenience that takes a qstring pointer */
bool isValidShapeFileName( QString * theFileNameQString );

/** add this file to the recently opened/saved projects list
* pass settings by reference since creating more than one
* instance simultaneously results in data loss.
Expand Down
24 changes: 23 additions & 1 deletion src/core/qgsvectorfilewriter.cpp
Expand Up @@ -43,7 +43,7 @@

QgsVectorFileWriter::QgsVectorFileWriter(
const QString &theVectorFileName,
const QString& fileEncoding,
const QString &theFileEncoding,
const QgsFieldMap& fields,
QGis::WkbType geometryType,
const QgsCoordinateReferenceSystem* srs,
Expand All @@ -54,6 +54,7 @@ QgsVectorFileWriter::QgsVectorFileWriter(
, mError( NoError )
{
QString vectorFileName = theVectorFileName;
QString fileEncoding = theFileEncoding;

// find driver in OGR
OGRSFDriverH poDriver;
Expand Down Expand Up @@ -91,6 +92,27 @@ QgsVectorFileWriter::QgsVectorFileWriter(
}
fieldNames << name;
}

deleteShapeFile( vectorFileName );
}
else if( driverName == "KML" )
{
if( !vectorFileName.endsWith( ".kml", Qt::CaseInsensitive ) )
{
vectorFileName += ".kml";
}

if( fileEncoding.compare( "UTF-8", Qt::CaseInsensitive )!=0 )
{
QgsDebugMsg( "forced UTF-8 encoding for KML" );
fileEncoding = "UTF-8";
}

QFile::remove( vectorFileName );
}
else
{
QFile::remove( vectorFileName );
}

// create the data source
Expand Down

0 comments on commit 24ce8da

Please sign in to comment.