[controller] Move more logic to the controller
This commit is contained in:
parent
7cc7f98424
commit
c111680d02
|
@ -63,7 +63,10 @@ namespace SparkleShare {
|
||||||
|
|
||||||
public SparkleController ()
|
public SparkleController ()
|
||||||
{
|
{
|
||||||
Console.WriteLine (SparkleShare.UserName + "<<<<<<");
|
|
||||||
|
UserName = "";
|
||||||
|
UserEmail = "";
|
||||||
|
|
||||||
SetProcessName ("sparkleshare");
|
SetProcessName ("sparkleshare");
|
||||||
|
|
||||||
InstallLauncher ();
|
InstallLauncher ();
|
||||||
|
@ -120,10 +123,7 @@ namespace SparkleShare {
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
SparkleShare.UserName = SparkleShare.GetUserName ();
|
AddKey ();
|
||||||
SparkleShare.UserEmail = SparkleShare.GetUserEmail ();
|
|
||||||
|
|
||||||
SparkleShare.AddKey ();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -456,6 +456,175 @@ 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'
|
// Sets the unix process name to 'sparkleshare' instead of 'mono'
|
||||||
private void SetProcessName (string name)
|
private void SetProcessName (string name)
|
||||||
{
|
{
|
||||||
|
|
|
@ -91,7 +91,7 @@ namespace SparkleShare {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
EmailEntry = new Entry (SparkleShare.UserEmail);
|
EmailEntry = new Entry (SparkleShare.Controller.UserEmail);
|
||||||
EmailEntry.Changed += delegate {
|
EmailEntry.Changed += delegate {
|
||||||
CheckAccountForm ();
|
CheckAccountForm ();
|
||||||
};
|
};
|
||||||
|
@ -121,7 +121,14 @@ namespace SparkleShare {
|
||||||
|
|
||||||
NextButton.ShowAll ();
|
NextButton.ShowAll ();
|
||||||
|
|
||||||
Configure ();
|
|
||||||
|
SparkleShare.Controller.UserName = NameEntry.Text;
|
||||||
|
SparkleShare.Controller.UserEmail = EmailEntry.Text;
|
||||||
|
|
||||||
|
SparkleShare.Controller.GenerateKeyPair ();
|
||||||
|
SparkleShare.Controller.AddKey ();
|
||||||
|
|
||||||
|
|
||||||
ShowServerForm ();
|
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
|
// Checks to see if an email address is valid
|
||||||
|
|
|
@ -69,7 +69,7 @@ namespace SparkleShare {
|
||||||
|
|
||||||
// The location of the user's public key for SparkleShare
|
// The location of the user's public key for SparkleShare
|
||||||
string public_key_file_path = SparkleHelpers.CombineMore (SparklePaths.HomePath, ".ssh",
|
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))
|
if (!File.Exists (public_key_file_path))
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -31,10 +31,6 @@ namespace SparkleShare {
|
||||||
public static SparkleController Controller;
|
public static SparkleController Controller;
|
||||||
public static SparkleUI UI;
|
public static SparkleUI UI;
|
||||||
|
|
||||||
// TODO: Move to Controller
|
|
||||||
public static string UserName = "";
|
|
||||||
public static string UserEmail = "";
|
|
||||||
|
|
||||||
|
|
||||||
// Short alias for the translations
|
// Short alias for the translations
|
||||||
public static string _ (string s)
|
public static string _ (string s)
|
||||||
|
@ -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 ();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue