controller: fetcher: Use and xml based config file and add temporary migration method

This commit is contained in:
Hylke Bons 2011-05-10 00:30:20 +01:00
parent 9d97548d3a
commit 79e2cda777
2 changed files with 78 additions and 68 deletions

View file

@ -19,6 +19,7 @@ using System;
using System.IO; using System.IO;
using System.Diagnostics; using System.Diagnostics;
using System.Threading; using System.Threading;
using System.Xml; // TODO: move to subclass later
namespace SparkleLib { namespace SparkleLib {
@ -106,15 +107,11 @@ namespace SparkleLib {
// the newly cloned repository // the newly cloned repository
private void InstallConfiguration () 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)) { 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 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 // Be case sensitive explicitly to work on Mac
config = config.Replace ("ignorecase = true", "ignorecase = false"); config = config.Replace ("ignorecase = true", "ignorecase = false");
@ -123,8 +120,19 @@ namespace SparkleLib {
config = config.Replace ("filemode = true", "filemode = false"); config = config.Replace ("filemode = true", "filemode = false");
// Add user info // 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); TextWriter writer = new StreamWriter (repo_config_file_path);
writer.WriteLine (config); writer.WriteLine (config);
writer.Close (); writer.Close ();

View file

@ -20,11 +20,11 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Threading;
using System.Text.RegularExpressions;
using System.Xml;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Xml;
using Mono.Unix; using Mono.Unix;
using SparkleLib; using SparkleLib;
@ -90,7 +90,9 @@ namespace SparkleShare {
public SparkleController () public SparkleController ()
{ {Console.WriteLine (UserName + " " + UserEmail);
SparklePath = SparklePaths.SparklePath;
// Remove temporary file // Remove temporary file
if (Directory.Exists (SparklePaths.SparkleTmpPath)) if (Directory.Exists (SparklePaths.SparkleTmpPath))
Directory.Delete (SparklePaths.SparkleTmpPath, true); Directory.Delete (SparklePaths.SparkleTmpPath, true);
@ -104,12 +106,17 @@ namespace SparkleShare {
FolderSize = GetFolderSize (); FolderSize = GetFolderSize ();
SparklePath = SparklePaths.SparklePath; string global_config_file_path = Path.Combine (SparklePaths.SparkleConfigPath, "config.xml");
string global_config_file_path = Path.Combine (SparklePaths.SparkleConfigPath, "config"); string old_global_config_file_path = Path.Combine (SparklePaths.SparkleConfigPath, "config");
// Show the introduction screen if SparkleShare isn't configured // Show the introduction screen if SparkleShare isn't configured
if (!File.Exists (global_config_file_path)) { if (!File.Exists (global_config_file_path)) {
FirstRun = true; if (File.Exists (old_global_config_file_path)) {
MigrateConfig ();
FirstRun = false;
} else {
FirstRun = true;
}
} else { } else {
FirstRun = false; FirstRun = false;
AddKey (); AddKey ();
@ -172,7 +179,31 @@ namespace SparkleShare {
new Thread (new ThreadStart (PopulateRepositories)).Start (); 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 // Uploads the user's public key to the server
public bool AcceptInvitation (string server, string folder, string token) public bool AcceptInvitation (string server, string folder, string token)
{ {
@ -721,22 +752,16 @@ namespace SparkleShare {
public string UserName public string UserName
{ {
get { 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)) if (!File.Exists (global_config_file_path))
return ""; return "";
StreamReader reader = new StreamReader (global_config_file_path); XmlDocument xml = new XmlDocument();
string global_config_file = reader.ReadToEnd (); xml.Load (global_config_file_path);
reader.Close ();
XmlNode node = xml.SelectSingleNode("//user/name/text()");
Regex regex = new Regex (@"name.+= (.+)"); return node.Value;
Match match = regex.Match (global_config_file);
if (match.Success)
return match.Groups [1].Value;
else
return "";
} }
set { set {
@ -749,43 +774,16 @@ namespace SparkleShare {
public string UserEmail public string UserEmail
{ {
get { get {
string global_config_file_path = Path.Combine (SparklePaths.SparkleConfigPath, "config"); string global_config_file_path = Path.Combine (SparklePaths.SparkleConfigPath, "config.xml");
// Look in the global config file first if (!File.Exists (global_config_file_path))
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 "";
}
}
return ""; return "";
}
XmlDocument xml = new XmlDocument();
xml.Load (global_config_file_path);
XmlNode node = xml.SelectSingleNode("//user/email/text()");
return node.Value;
} }
set { set {
@ -796,14 +794,18 @@ namespace SparkleShare {
private void WriteUserInfo (string user_name, string user_email) 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 // Write the user's information to a text file
TextWriter writer = new StreamWriter (global_config_file_path); TextWriter writer = new StreamWriter (global_config_file_path);
writer.WriteLine ("[user]\n" + string n = Environment.NewLine;
"\tname = " + user_name + "\n" + writer.WriteLine ("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + n +
"\temail = " + user_email); "<sparkleshare>" + n +
" <user>" + n +
" <name>" + user_name + "</name>" + n +
" <email>" + user_email + "</email>" + n +
" </user>" + n +
"</sparkleshare>" + n);
writer.Close (); writer.Close ();
SparkleHelpers.DebugInfo ("Config", "Updated '" + global_config_file_path + "'"); SparkleHelpers.DebugInfo ("Config", "Updated '" + global_config_file_path + "'");