[controller] Move more logic to the controller

This commit is contained in:
Hylke Bons 2010-11-18 00:29:31 +00:00
parent 7cc7f98424
commit c111680d02
4 changed files with 184 additions and 172 deletions

View file

@ -63,7 +63,10 @@ namespace SparkleShare {
public SparkleController ()
{
Console.WriteLine (SparkleShare.UserName + "<<<<<<");
UserName = "";
UserEmail = "";
SetProcessName ("sparkleshare");
InstallLauncher ();
@ -120,10 +123,7 @@ namespace SparkleShare {
} else {
SparkleShare.UserName = SparkleShare.GetUserName ();
SparkleShare.UserEmail = SparkleShare.GetUserEmail ();
SparkleShare.AddKey ();
AddKey ();
}
@ -455,7 +455,176 @@ namespace SparkleShare {
}
// Adds the user's SparkleShare key to the ssh-agent,
// so all activity is done with this key
public void AddKey ()
{
string keys_path = SparklePaths.SparkleKeysPath;
string key_file_name = "sparkleshare." + UserEmail + ".key";
Process process = new Process ();
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "ssh-add";
process.StartInfo.Arguments = Path.Combine (keys_path, key_file_name);
process.Start ();
}
// Looks up the user's name from the global configuration
public string UserName
{
get {
string global_config_file_path = SparkleHelpers.CombineMore (SparklePaths.SparkleConfigPath, "config");
if (!File.Exists (global_config_file_path))
return "";
StreamReader reader = new StreamReader (global_config_file_path);
// Discard the first line
reader.ReadLine ();
string line = reader.ReadLine ();
reader.Close ();
return line.Substring (line.IndexOf ("=") + 2);
}
set {
WriteUserInfo (value, UserEmail);
}
}
// Looks up the user's email from the global configuration
public string UserEmail
{
get {
string global_config_file_path = SparkleHelpers.CombineMore (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);
// TODO: Properly look at the variable name
// Discard the first two lines
reader.ReadLine ();
reader.ReadLine ();
string line = reader.ReadLine ();
reader.Close ();
return line.Substring (line.IndexOf ("=") + 2);
} 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")) {
string email = "";
email = file_name.Substring (file_name.IndexOf (".") + 1);
email = email.Substring (0, email.LastIndexOf ("."));
return email;
}
}
return "";
}
}
set {
WriteUserInfo (UserName, value);
}
}
private void WriteUserInfo (string user_name, string user_email)
{
string global_config_file_path = SparkleHelpers.CombineMore (SparklePaths.SparkleConfigPath, "config");
// 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);
writer.Close ();
SparkleHelpers.DebugInfo ("Config", "Created '" + global_config_file_path + "'");
}
// Generates and installs an RSA keypair to identify this system
public void GenerateKeyPair ()
{
string keys_path = SparklePaths.SparkleKeysPath;
string key_file_name = "sparkleshare." + UserEmail + ".key";
Process process = new Process () {
EnableRaisingEvents = true
};
if (!Directory.Exists (keys_path))
Directory.CreateDirectory (keys_path);
if (!File.Exists (key_file_name)) {
process.StartInfo.WorkingDirectory = keys_path;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.FileName = "ssh-keygen";
// -t is the crypto type
// -P is the password (none)
// -f is the file name to store the private key in
process.StartInfo.Arguments = "-t rsa -P \"\" -f " + key_file_name;
process.Start ();
process.Exited += delegate {
SparkleHelpers.DebugInfo ("Config", "Created key '" + key_file_name + "'");
SparkleHelpers.DebugInfo ("Config", "Created key '" + key_file_name + ".pub'");
};
}
}
// Sets the unix process name to 'sparkleshare' instead of 'mono'
private void SetProcessName (string name)
{

View file

@ -91,7 +91,7 @@ namespace SparkleShare {
};
EmailEntry = new Entry (SparkleShare.UserEmail);
EmailEntry = new Entry (SparkleShare.Controller.UserEmail);
EmailEntry.Changed += delegate {
CheckAccountForm ();
};
@ -121,7 +121,14 @@ namespace SparkleShare {
NextButton.ShowAll ();
Configure ();
SparkleShare.Controller.UserName = NameEntry.Text;
SparkleShare.Controller.UserEmail = EmailEntry.Text;
SparkleShare.Controller.GenerateKeyPair ();
SparkleShare.Controller.AddKey ();
ShowServerForm ();
};
@ -759,73 +766,6 @@ namespace SparkleShare {
}
// Configures SparkleShare with the user's information
private void Configure ()
{
string config_file_path = SparkleHelpers.CombineMore (SparklePaths.SparkleConfigPath, "config");
string name = NameEntry.Text;
string email = EmailEntry.Text;
// Write the user's information to a text file
TextWriter writer = new StreamWriter (config_file_path);
writer.WriteLine ("[user]\n" +
"\tname = " + name + "\n" +
"\temail = " + email);
writer.Close ();
SparkleHelpers.DebugInfo ("Config", "Created '" + config_file_path + "'");
// Set the user's name and email globally
SparkleShare.UserName = name;
SparkleShare.UserEmail = email;
GenerateKeyPair ();
SparkleShare.AddKey ();
}
// Generates and installs an RSA keypair to identify this system
private void GenerateKeyPair ()
{
string user_email = EmailEntry.Text;
string keys_path = SparklePaths.SparkleKeysPath;
string key_file_name = "sparkleshare." + user_email + ".key";
Process process = new Process () {
EnableRaisingEvents = true
};
if (!Directory.Exists (keys_path))
Directory.CreateDirectory (keys_path);
if (!File.Exists (key_file_name)) {
process.StartInfo.WorkingDirectory = keys_path;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.FileName = "ssh-keygen";
// -t is the crypto type
// -P is the password (none)
// -f is the file name to store the private key in
process.StartInfo.Arguments = "-t rsa -P \"\" -f " + key_file_name;
process.Start ();
process.Exited += delegate {
SparkleHelpers.DebugInfo ("Config", "Created key '" + key_file_name + "'");
SparkleHelpers.DebugInfo ("Config", "Created key '" + key_file_name + ".pub'");
};
}
}
// Checks to see if an email address is valid

View file

@ -69,7 +69,7 @@ namespace SparkleShare {
// The location of the user's public key for SparkleShare
string public_key_file_path = SparkleHelpers.CombineMore (SparklePaths.HomePath, ".ssh",
"sparkleshare." + SparkleShare.UserEmail + ".key.pub");
"sparkleshare." + SparkleShare.Controller.UserEmail + ".key.pub");
if (!File.Exists (public_key_file_path))
return;

View file

@ -30,10 +30,6 @@ namespace SparkleShare {
public static SparkleController Controller;
public static SparkleUI UI;
// TODO: Move to Controller
public static string UserName = "";
public static string UserEmail = "";
// Short alias for the translations
@ -142,99 +138,6 @@ namespace SparkleShare {
}
// Looks up the user's name from the global configuration
public static string GetUserName ()
{
string global_config_file_path = SparkleHelpers.CombineMore (SparklePaths.SparkleConfigPath, "config");
StreamReader reader = new StreamReader (global_config_file_path);
// Discard the first line
reader.ReadLine ();
string line = reader.ReadLine ();
reader.Close ();
string name = line.Substring (line.IndexOf ("=") + 2);
return name;
}
// Looks up the user's email from the global configuration
public static string GetUserEmail ()
{
string email = "";
string global_config_file_path = SparkleHelpers.CombineMore (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);
// TODO: Properly look at the variable name
// Discard the first two lines
reader.ReadLine ();
reader.ReadLine ();
string line = reader.ReadLine ();
reader.Close ();
email = line.Substring (line.IndexOf ("=") + 2);
return email;
// Secondly, look at the user's private key file name
} else {
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")) {
email = file_name.Substring (file_name.IndexOf (".") + 1);
email = email.Substring (0, email.LastIndexOf ("."));
return email;
}
}
return "";
}
}
// Adds the user's SparkleShare key to the ssh-agent,
// so all activity is done with this key
public static void AddKey ()
{
string keys_path = SparklePaths.SparkleKeysPath;
string key_file_name = "sparkleshare." + UserEmail + ".key";
Process process = new Process ();
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "ssh-add";
process.StartInfo.Arguments = Path.Combine (keys_path, key_file_name);
process.Start ();
}
}
}