From 7fee797fa91dae0911fe54c14c6630bb623f6a9f Mon Sep 17 00:00:00 2001
From: Guillaume Lelarge <guillaume@lelarge.info>
Date: Mon, 29 Nov 2010 23:55:06 +0100
Subject: [PATCH] Add option to request BOM write

Before, we always write BOM at the beginning of UTF files. Not all tools
understand BOM files. psql knows about it only since PostgreSQL 9.0, so our
sql files are not usable with psql 8.4 and before. So, this option is
available to allow a user to decide if he wants BOM or not.

Implements #283.
---
 pgadmin/frm/frmOptions.cpp          |    3 +++
 pgadmin/include/utils/sysSettings.h |    2 ++
 pgadmin/ui/frmOptions.xrc           |    8 ++++++++
 pgadmin/utils/utffile.cpp           |    4 ++++
 4 files changed, 17 insertions(+), 0 deletions(-)

diff --git a/pgadmin/frm/frmOptions.cpp b/pgadmin/frm/frmOptions.cpp
index 2cdf11f..1977a08 100644
--- a/pgadmin/frm/frmOptions.cpp
+++ b/pgadmin/frm/frmOptions.cpp
@@ -52,6 +52,7 @@
 #define txtMaxColSize               CTRL_TEXT("txtMaxColSize")
 #define pickerFont                  CTRL_FONTPICKER("pickerFont")
 #define chkUnicodeFile              CTRL_CHECKBOX("chkUnicodeFile")
+#define chkWriteBOM                 CTRL_CHECKBOX("chkWriteBOM")
 #define chkAskSaveConfirm           CTRL_CHECKBOX("chkAskSaveConfirm")
 #define chkAskDelete                CTRL_CHECKBOX("chkAskDelete")
 #define chkShowUsersForPrivileges   CTRL_CHECKBOX("chkShowUsersForPrivileges")
@@ -237,6 +238,7 @@ frmOptions::frmOptions(frmMain *parent)
     
     txtSystemSchemas->SetValue(settings->GetSystemSchemas());
     chkUnicodeFile->SetValue(settings->GetUnicodeFile());
+    chkWriteBOM->SetValue(settings->GetWriteBOM());
     chkSuppressHints->SetValue(settings->GetSuppressGuruHints());
     pickerSlonyPath->SetPath(settings->GetSlonyPath());
     pickerPostgresqlPath->SetPath(settings->GetPostgresqlPath());
@@ -531,6 +533,7 @@ void frmOptions::OnOK(wxCommandEvent &ev)
     settings->SetAutoRollback(chkAutoRollback->GetValue());
     settings->SetDoubleClickProperties(chkDoubleClickProperties->GetValue());
     settings->SetUnicodeFile(chkUnicodeFile->GetValue());
+    settings->SetWriteBOM(chkWriteBOM->GetValue());
     settings->SetSystemFont(pickerFont->GetSelectedFont());
     settings->SetSQLFont(pickerSqlFont->GetSelectedFont());
     settings->SetSuppressGuruHints(chkSuppressHints->GetValue());
diff --git a/pgadmin/include/utils/sysSettings.h b/pgadmin/include/utils/sysSettings.h
index f64a572..d6fbdb6 100644
--- a/pgadmin/include/utils/sysSettings.h
+++ b/pgadmin/include/utils/sysSettings.h
@@ -88,6 +88,8 @@ public:
     void SetExportQuoting(const int i);
     bool GetExportUnicode() const { bool b; Read(wxT("Export/Unicode"), &b, true); return b; }
 	void SetExportUnicode(const bool newval) { Write(wxT("Export/Unicode"), newval); }
+    bool GetWriteBOM() const { bool b; Read(wxT("Export/WriteBOM"), &b, true); return b; }
+	void SetWriteBOM(const bool newval) { Write(wxT("Export/WriteBOM"), newval); }
 	
     // Explain options
     bool GetExplainVerbose() const { bool b; Read(wxT("frmQuery/ExplainVerbose"), &b, false); return b; }
diff --git a/pgadmin/ui/frmOptions.xrc b/pgadmin/ui/frmOptions.xrc
index 5424d76..dfea985 100644
--- a/pgadmin/ui/frmOptions.xrc
+++ b/pgadmin/ui/frmOptions.xrc
@@ -230,6 +230,14 @@
                   <border>4</border>
                 </object>
                 <object class="sizeritem">
+                  <object class="wxCheckBox" name="chkWriteBOM">
+                    <label>Write BOM for UTF files</label>
+                    <checked>1</checked>
+                  </object>
+                  <flag>wxEXPAND|wxALIGN_CENTER_VERTICAL|wxTOP|wxLEFT|wxRIGHT</flag>
+                  <border>4</border>
+                </object>
+                <object class="sizeritem">
                   <object class="wxCheckBox" name="chkAskSaveConfirm">
                     <label>Do not prompt for unsaved files on exit</label>
                     <checked>0</checked>
diff --git a/pgadmin/utils/utffile.cpp b/pgadmin/utils/utffile.cpp
index b5c6fdf..b4ab90e 100644
--- a/pgadmin/utils/utffile.cpp
+++ b/pgadmin/utils/utffile.cpp
@@ -11,6 +11,7 @@
 
 #include "pgAdmin3.h"
 #include "utils/utffile.h"
+#include "utils/sysLogger.h"
 
 wxMBConvUTF16BE wxConvUTF16BE;
 wxMBConvUTF16LE wxConvUTF16LE;
@@ -203,6 +204,9 @@ wxFontEncoding wxUtfFile::GetEncoding()
 
 void wxUtfFile::WriteBOM()
 {
+    if (!settings->GetWriteBOM())
+        return;
+
     wxFile::Seek(0);
     switch (m_encoding)
     {
-- 
1.7.1
