From 79e2cda77740fe460746e68895fc738c45c5b656 Mon Sep 17 00:00:00 2001 From: Hylke Bons Date: Tue, 10 May 2011 00:30:20 +0100 Subject: [PATCH] controller: fetcher: Use and xml based config file and add temporary migration method --- SparkleLib/SparkleFetcher.cs | 22 ++++-- SparkleShare/SparkleController.cs | 124 +++++++++++++++--------------- 2 files changed, 78 insertions(+), 68 deletions(-) diff --git a/SparkleLib/SparkleFetcher.cs b/SparkleLib/SparkleFetcher.cs index c3400371..d94b6d9c 100644 --- a/SparkleLib/SparkleFetcher.cs +++ b/SparkleLib/SparkleFetcher.cs @@ -19,6 +19,7 @@ using System; using System.IO; using System.Diagnostics; using System.Threading; +using System.Xml; // TODO: move to subclass later namespace SparkleLib { @@ -106,15 +107,11 @@ namespace SparkleLib { // the newly cloned repository private void InstallConfiguration () { - string global_config_file_path = Path.Combine (SparklePaths.SparkleConfigPath, "config"); + string global_config_file_path = Path.Combine (SparklePaths.SparkleConfigPath, "config.xml"); if (File.Exists (global_config_file_path)) { - StreamReader reader = new StreamReader (global_config_file_path); - string user_info = reader.ReadToEnd (); - reader.Close (); - string repo_config_file_path = SparkleHelpers.CombineMore (this.target_folder, ".git", "config"); - string config = String.Join ("\n", File.ReadAllLines (repo_config_file_path)); + string config = String.Join (Environment.NewLine, File.ReadAllLines (repo_config_file_path)); // Be case sensitive explicitly to work on Mac config = config.Replace ("ignorecase = true", "ignorecase = false"); @@ -123,8 +120,19 @@ namespace SparkleLib { config = config.Replace ("filemode = true", "filemode = false"); // Add user info - config += Environment.NewLine + user_info; + string n = Environment.NewLine; + XmlDocument xml = new XmlDocument(); + xml.Load (global_config_file_path); + XmlNode node_name = xml.SelectSingleNode ("//user/name/text()"); + XmlNode node_email = xml.SelectSingleNode ("//user/email/text()"); + + config += n + + "[user]" + n + + "\tname = " + node_name.Value + n + + "\temail = " + node_email.Value + n; + + // Write the config to the file TextWriter writer = new StreamWriter (repo_config_file_path); writer.WriteLine (config); writer.Close (); diff --git a/SparkleShare/SparkleController.cs b/SparkleShare/SparkleController.cs index 8f0a4f94..e15197ec 100644 --- a/SparkleShare/SparkleController.cs +++ b/SparkleShare/SparkleController.cs @@ -20,11 +20,11 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Net; -using System.Threading; -using System.Text.RegularExpressions; -using System.Xml; using System.Security.Cryptography; using System.Text; +using System.Text.RegularExpressions; +using System.Threading; +using System.Xml; using Mono.Unix; using SparkleLib; @@ -90,7 +90,9 @@ namespace SparkleShare { public SparkleController () - { + {Console.WriteLine (UserName + " " + UserEmail); + SparklePath = SparklePaths.SparklePath; + // Remove temporary file if (Directory.Exists (SparklePaths.SparkleTmpPath)) Directory.Delete (SparklePaths.SparkleTmpPath, true); @@ -104,12 +106,17 @@ namespace SparkleShare { FolderSize = GetFolderSize (); - SparklePath = SparklePaths.SparklePath; - string global_config_file_path = Path.Combine (SparklePaths.SparkleConfigPath, "config"); + string global_config_file_path = Path.Combine (SparklePaths.SparkleConfigPath, "config.xml"); + string old_global_config_file_path = Path.Combine (SparklePaths.SparkleConfigPath, "config"); // Show the introduction screen if SparkleShare isn't configured if (!File.Exists (global_config_file_path)) { - FirstRun = true; + if (File.Exists (old_global_config_file_path)) { + MigrateConfig (); + FirstRun = false; + } else { + FirstRun = true; + } } else { FirstRun = false; AddKey (); @@ -172,7 +179,31 @@ namespace SparkleShare { new Thread (new ThreadStart (PopulateRepositories)).Start (); } - + + // TODO: remove this later + private void MigrateConfig () { + string global_config_file_path = Path.Combine (SparklePaths.SparkleConfigPath, "config.xml"); + string old_global_config_file_path = Path.Combine (SparklePaths.SparkleConfigPath, "config"); + + StreamReader reader = new StreamReader (old_global_config_file_path); + string global_config_file = reader.ReadToEnd (); + reader.Close (); + + Regex regex = new Regex (@"name.+= (.+)"); + Match match = regex.Match (global_config_file); + + string user_name = match.Groups [1].Value; + + regex = new Regex (@"email.+= (.+)"); + match = regex.Match (global_config_file); + + string user_email = match.Groups [1].Value; + + WriteUserInfo (user_name, user_email); + File.Delete (old_global_config_file_path); + } + + // Uploads the user's public key to the server public bool AcceptInvitation (string server, string folder, string token) { @@ -721,22 +752,16 @@ namespace SparkleShare { public string UserName { get { - string global_config_file_path = Path.Combine (SparklePaths.SparkleConfigPath, "config"); + string global_config_file_path = Path.Combine (SparklePaths.SparkleConfigPath, "config.xml"); if (!File.Exists (global_config_file_path)) return ""; - StreamReader reader = new StreamReader (global_config_file_path); - string global_config_file = reader.ReadToEnd (); - reader.Close (); - - Regex regex = new Regex (@"name.+= (.+)"); - Match match = regex.Match (global_config_file); - - if (match.Success) - return match.Groups [1].Value; - else - return ""; + XmlDocument xml = new XmlDocument(); + xml.Load (global_config_file_path); + + XmlNode node = xml.SelectSingleNode("//user/name/text()"); + return node.Value; } set { @@ -749,43 +774,16 @@ namespace SparkleShare { public string UserEmail { get { - string global_config_file_path = Path.Combine (SparklePaths.SparkleConfigPath, "config"); - - // Look in the global config file first - if (File.Exists (global_config_file_path)) { - StreamReader reader = new StreamReader (global_config_file_path); - string global_config_file = reader.ReadToEnd (); - reader.Close (); - - Regex regex = new Regex (@"email.+= (.+)"); - Match match = regex.Match (global_config_file); - - if (match.Success) - return match.Groups [1].Value; - else - return ""; - } else { // Secondly, look at the user's private key file name - string keys_path = SparklePaths.SparkleKeysPath; - - if (!Directory.Exists (keys_path)) - return ""; - - foreach (string file_path in Directory.GetFiles (keys_path)) { - string file_name = System.IO.Path.GetFileName (file_path); - - if (file_name.StartsWith ("sparkleshare.") && file_name.EndsWith (".key")) { - Regex regex = new Regex (@"sparkleshare\.(.+)\.key"); - Match match = regex.Match (file_name); - - if (match.Success) - return match.Groups [1].Value; - else - return ""; - } - } - + string global_config_file_path = Path.Combine (SparklePaths.SparkleConfigPath, "config.xml"); + + if (!File.Exists (global_config_file_path)) return ""; - } + + XmlDocument xml = new XmlDocument(); + xml.Load (global_config_file_path); + + XmlNode node = xml.SelectSingleNode("//user/email/text()"); + return node.Value; } set { @@ -796,14 +794,18 @@ namespace SparkleShare { private void WriteUserInfo (string user_name, string user_email) { - string global_config_file_path = Path.Combine (SparklePaths.SparkleConfigPath, "config"); + string global_config_file_path = Path.Combine (SparklePaths.SparkleConfigPath, "config.xml"); - // TODO: Make XML based. don't forget to change the code in fetcher // Write the user's information to a text file TextWriter writer = new StreamWriter (global_config_file_path); - writer.WriteLine ("[user]\n" + - "\tname = " + user_name + "\n" + - "\temail = " + user_email); + string n = Environment.NewLine; + writer.WriteLine ("" + n + + "" + n + + " " + n + + " " + user_name + "" + n + + " " + user_email + "" + n + + " " + n + + "" + n); writer.Close (); SparkleHelpers.DebugInfo ("Config", "Updated '" + global_config_file_path + "'");