From b1a83c914506722855bfc4628ab36a700b386282 Mon Sep 17 00:00:00 2001
From: Minoru Akagi <akaginch@gmail.com>
Date: Sat, 20 Apr 2013 12:10:22 +0900
Subject: [PATCH] fix SelectEncoding capability of OgrProvider (fix #7647)

QgsVectorDataProvider: rename SetEncoding capability to SelectEncoding
---
 python/core/qgsvectordataprovider.sip |  2 +-
 src/app/qgsvectorlayerproperties.cpp  | 13 ++++++++++---
 src/core/qgsvectordataprovider.h      |  4 ++--
 src/providers/ogr/qgsogrprovider.cpp  | 11 ++++++++++-
 4 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/python/core/qgsvectordataprovider.sip b/python/core/qgsvectordataprovider.sip
index ef9ff79..856b22c 100644
--- a/python/core/qgsvectordataprovider.sip
+++ b/python/core/qgsvectordataprovider.sip
@@ -27,7 +27,7 @@ class QgsVectorDataProvider : QgsDataProvider
       RandomSelectGeometryAtId =     1024,
       SequentialSelectGeometryAtId = 2048,
       CreateAttributeIndex =         4096,
-      SetEncoding =                  8192,
+      SelectEncoding =               8192,
     };
 
     /** bitmask of all provider's editing capabilities */
diff --git a/src/app/qgsvectorlayerproperties.cpp b/src/app/qgsvectorlayerproperties.cpp
index 42d5605..fc294d2 100644
--- a/src/app/qgsvectorlayerproperties.cpp
+++ b/src/app/qgsvectorlayerproperties.cpp
@@ -144,7 +144,7 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
       pbnIndex->setEnabled( false );
     }
 
-    if ( capabilities & QgsVectorDataProvider::SetEncoding )
+    if ( capabilities & QgsVectorDataProvider::SelectEncoding )
     {
       cboProviderEncoding->addItems( QgsVectorDataProvider::availableEncodings() );
       QString enc = layer->dataProvider()->encoding();
@@ -156,9 +156,16 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
       }
       cboProviderEncoding->setCurrentIndex( encindex );
     }
+    else if ( layer->dataProvider()->name() == "ogr" )
+    {
+      // if OGR_L_TestCapability(OLCStringsAsUTF8) returns true, OGR provider encoding can be set to only UTF-8
+      // so make encoding box grayed out
+      cboProviderEncoding->addItem( layer->dataProvider()->encoding() );
+      cboProviderEncoding->setEnabled( false );
+    }
     else
     {
-      // currently only encoding can be set in this group, so hide it completely
+      // other providers do not use mEncoding, so hide the group completely
       grpProviderOptions->hide();
     }
   }
@@ -386,7 +393,7 @@ void QgsVectorLayerProperties::apply()
   // provider-specific options
   if ( layer->dataProvider() )
   {
-    if ( layer->dataProvider()->capabilities() & QgsVectorDataProvider::SetEncoding )
+    if ( layer->dataProvider()->capabilities() & QgsVectorDataProvider::SelectEncoding )
     {
       layer->setProviderEncoding( cboProviderEncoding->currentText() );
     }
diff --git a/src/core/qgsvectordataprovider.h b/src/core/qgsvectordataprovider.h
index bdd71d7..19d8eaf 100644
--- a/src/core/qgsvectordataprovider.h
+++ b/src/core/qgsvectordataprovider.h
@@ -84,8 +84,8 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
       /** DEPRECATED - do not use */
       SequentialSelectGeometryAtId = 1 << 11,
       CreateAttributeIndex =         1 << 12,
-      /** Uses mEncoding for conversion of 8-bit strings to unicode */
-      SetEncoding =                  1 << 13,
+      /** allows user to select encoding */
+      SelectEncoding =               1 << 13,
     };
 
     /** bitmask of all provider's editing capabilities */
diff --git a/src/providers/ogr/qgsogrprovider.cpp b/src/providers/ogr/qgsogrprovider.cpp
index dca68b7..89e447b 100644
--- a/src/providers/ogr/qgsogrprovider.cpp
+++ b/src/providers/ogr/qgsogrprovider.cpp
@@ -1198,7 +1198,7 @@ bool QgsOgrProvider::deleteFeature( QgsFeatureId id )
 
 int QgsOgrProvider::capabilities() const
 {
-  int ability = SetEncoding;
+  int ability = 0;
 
   // collect abilities reported by OGR
   if ( ogrLayer )
@@ -1289,6 +1289,15 @@ int QgsOgrProvider::capabilities() const
       ability |= DeleteAttributes;
     }
 
+#if defined(OLCStringsAsUTF8)
+    if ( !OGR_L_TestCapability( ogrLayer, OLCStringsAsUTF8 ) )
+    {
+      ability |= SelectEncoding;
+    }
+#else
+    ability |= SelectEncoding;
+#endif
+
     // OGR doesn't handle shapefiles without attributes, ie. missing DBFs well, fixes #803
     if ( ogrDriverName == "ESRI Shapefile" )
     {
-- 
1.8.0.msysgit.0

