[chronojump] Fixed personName for being used on SQL and filename (encoder)



commit 3a91ed20ce5deebd943eb3cdcfc87923fc43161f
Author: Xavier de Blas <xaviblas gmail com>
Date:   Tue Apr 18 15:45:21 2017 +0200

    Fixed personName for being used on SQL and filename (encoder)

 src/gui/person.cs |   13 +++++++------
 src/util.cs       |   25 ++++++++++++++++++++++++-
 2 files changed, 31 insertions(+), 7 deletions(-)
---
diff --git a/src/gui/person.cs b/src/gui/person.cs
index 85b1218..fea0926 100644
--- a/src/gui/person.cs
+++ b/src/gui/person.cs
@@ -1509,7 +1509,8 @@ public class PersonAddModifyWindow
                string errorMessage = "";
 
                //Check if person name exists and weight is > 0
-               string personName = entry1.Text;
+               string personName = Util.MakeValidSQLAndFileName(entry1.Text);
+
                if(personName == "")
                        errorMessage += "\n" + Catalog.GetString("Please, write the name of the person.");
                if((double) spinbutton_weight.Value == 0)
@@ -1519,12 +1520,11 @@ public class PersonAddModifyWindow
                        return;
                }
 
-
                bool personExists;
                if(adding)
-                       personExists = Sqlite.Exists (false, Constants.PersonTable, 
Util.RemoveTilde(personName));
+                       personExists = Sqlite.Exists (false, Constants.PersonTable, personName);
                else
-                       personExists = SqlitePerson.ExistsAndItsNotMe (currentPerson.UniqueID, 
Util.RemoveTilde(personName));
+                       personExists = SqlitePerson.ExistsAndItsNotMe (currentPerson.UniqueID, personName);
 
                if(personExists) 
                        errorMessage += string.Format(Catalog.GetString("Person: '{0}' exists. Please, use 
another name"), 
@@ -1583,11 +1583,12 @@ public class PersonAddModifyWindow
                        SqliteRun.Update(mRun.UniqueID, mRun.Type, mRun.Distance.ToString(), 
mRun.Time.ToString(), mRun.PersonID, mRun.Description);
                }
 
+               string personName = Util.MakeValidSQLAndFileName(entry1.Text);
 
                if(adding) {
                        //here we add rows in the database
                        LogB.Information("Going to insert person");
-                       currentPerson = new Person (entry1.Text, sex, dateTime, 
+                       currentPerson = new Person (personName, sex, dateTime,
                                        Constants.RaceUndefinedID,
                                        Convert.ToInt32(Util.FindOnArray(':', 2, 0, 
UtilGtk.ComboGetActive(combo_countries), countries)),
                                        textview_description.Buffer.Text,
@@ -1604,7 +1605,7 @@ public class PersonAddModifyWindow
                        LogB.Information("inserted both");
                } else {
                        //here we update rows in the database
-                       currentPerson = new Person (currentPerson.UniqueID, entry1.Text, sex, dateTime, 
+                       currentPerson = new Person (currentPerson.UniqueID, personName, sex, dateTime,
                                        Constants.RaceUndefinedID,
                                        Convert.ToInt32(Util.FindOnArray(':', 2, 0, 
UtilGtk.ComboGetActive(combo_countries), countries)),
                                        textview_description.Buffer.Text,
diff --git a/src/util.cs b/src/util.cs
index e2cf55c..291e7cf 100644
--- a/src/util.cs
+++ b/src/util.cs
@@ -278,6 +278,29 @@ public class Util
                return false;
        }
 
+       public static string MakeValidSQLAndFileName(string name)
+       {
+               name = MakeValidFileName(MakeValidSQL(name));
+               name = name.Replace(@"\","");
+
+               return name;
+       }
+       //http://stackoverflow.com/a/847251
+       public static string MakeValidFileName(string name)
+       {
+               string invalidChars = System.Text.RegularExpressions.Regex.Escape( new string( 
System.IO.Path.GetInvalidFileNameChars() ) );
+               string invalidRegStr = string.Format( @"([{0}]*\.+$)|([{0}]+)", invalidChars );
+
+               return System.Text.RegularExpressions.Regex.Replace( name, invalidRegStr, "_" );
+       }
+       public static string MakeValidSQL(string str)
+       {
+               StringBuilder myStringBuilder = new StringBuilder(str);
+               myStringBuilder.Replace("'", " ");
+               myStringBuilder.Replace("\"", " ");
+               return myStringBuilder.ToString();
+       }
+
        public static string RemoveTilde(string myString) 
        {
                StringBuilder myStringBuilder = new StringBuilder(myString);
@@ -304,7 +327,7 @@ public class Util
                myStringBuilder.Replace(".", " ");
                return myStringBuilder.ToString();
        }
-       
+
        public static string RemoveTab(string myString) 
        {
                StringBuilder myStringBuilder = new StringBuilder(myString);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]