From 27e58ca806158de23357384b503c6529765df18c Mon Sep 17 00:00:00 2001 From: Leonardo Robol Date: Thu, 11 Feb 2010 20:05:13 +0100 Subject: [PATCH] Riorganizzato il codice dei trasferimenti, in attesa ancora di qualche miglioramento. --- Dizzy.csproj | 5 + Dizzy.make | 7 +- Dizzy/DizzyErrorDialog.cs | 23 ++++ Dizzy/FileDownload.cs | 29 +++++ Dizzy/FileList.cs | 53 +++++++++ Dizzy/FileTransfer.cs | 72 ++++++++++++ Dizzy/FileUpload.cs | 28 +++++ Dizzy/Protocol.cs | 288 +--------------------------------------------- Dizzy/SFTPConnection.cs | 134 +++++++++++++++++++++ 9 files changed, 352 insertions(+), 287 deletions(-) create mode 100644 Dizzy/DizzyErrorDialog.cs create mode 100644 Dizzy/FileDownload.cs create mode 100644 Dizzy/FileList.cs create mode 100644 Dizzy/FileTransfer.cs create mode 100644 Dizzy/FileUpload.cs create mode 100644 Dizzy/SFTPConnection.cs diff --git a/Dizzy.csproj b/Dizzy.csproj index 2e60825..1d316b7 100644 --- a/Dizzy.csproj +++ b/Dizzy.csproj @@ -96,6 +96,11 @@ + + + + + diff --git a/Dizzy.make b/Dizzy.make index 61165c0..beda841 100644 --- a/Dizzy.make +++ b/Dizzy.make @@ -82,7 +82,12 @@ FILES = \ Dizzy/TaskTreeView.cs \ Dizzy/AssemblyInfo.cs \ Dizzy/Log.cs \ - Dizzy/EventManager.cs + Dizzy/EventManager.cs \ + Dizzy/SFTPConnection.cs \ + Dizzy/FileUpload.cs \ + Dizzy/FileList.cs \ + Dizzy/FileDownload.cs \ + Dizzy/FileTransfer.cs DATA_FILES = diff --git a/Dizzy/DizzyErrorDialog.cs b/Dizzy/DizzyErrorDialog.cs new file mode 100644 index 0000000..572f491 --- /dev/null +++ b/Dizzy/DizzyErrorDialog.cs @@ -0,0 +1,23 @@ + +using System; + +namespace Dizzy +{ + + + public partial class DizzyErrorDialog : Gtk.MessageDialog + { + + public DizzyErrorDialog (Gtk.Window parent_window, string markup) : base(parent_window, + Gtk.DialogFlags.Modal, + Gtk.MessageType.Error, + Gtk.ButtonsType.Ok, + true, + "") + + { + this.Build (); + this.Markup = markup; + } + } +} diff --git a/Dizzy/FileDownload.cs b/Dizzy/FileDownload.cs new file mode 100644 index 0000000..46a5241 --- /dev/null +++ b/Dizzy/FileDownload.cs @@ -0,0 +1,29 @@ + +using System; + +namespace Dizzy +{ + + public class FileDownlad : FileTransfer + { + + File file; + string downloadFolder; + + public FileDownlad(File f, + string user, + string password, + string downloadFolder) : base(user, password, f.name) + { + this.file = f; + this.downloadFolder = downloadFolder; + } + + public override void Start () + { + this.sftp.Download(this.file, this.downloadFolder); + this.sftp.Disconnect (); + } + } + +} diff --git a/Dizzy/FileList.cs b/Dizzy/FileList.cs new file mode 100644 index 0000000..6590f73 --- /dev/null +++ b/Dizzy/FileList.cs @@ -0,0 +1,53 @@ + +using System; +using System.Data.SQLite; +using System.Collections; + +namespace Dizzy +{ + + + public class FileList + { + + GlobalConfig config; + SQLiteConnection connection; + + public FileList(ref GlobalConfig config) { + + this.config = config; + + string connectionString = "Data Source=" + this.config.ListFileName () + ";Version=3"; + this.connection = new SQLiteConnection(connectionString); + } + + public ArrayList Search(string keyword, string type, string user) { + + this.connection.Open (); + ArrayList matches = new ArrayList (); + + // Eseguiamo la query + SQLiteCommand sqlCmd = this.connection.CreateCommand (); + sqlCmd.CommandText = "SELECT * from files WHERE name LIKE '%" + keyword + "%'"; + SQLiteDataReader reader = sqlCmd.ExecuteReader (); + + File tmp; + while(reader.Read()) + { + tmp = new File(reader.GetString(0), // Path + reader.GetString(2), // User + reader.GetString(1), // Name + reader.GetInt32(3)); // Size + + if ( (type == "Qualsiasi" || tmp.type.Name() == type) && + (user == "" || user == tmp.user) ) + { + matches.Add (tmp); + } + } + + this.connection.Close (); + return matches; + } + } +} diff --git a/Dizzy/FileTransfer.cs b/Dizzy/FileTransfer.cs new file mode 100644 index 0000000..2f55cd9 --- /dev/null +++ b/Dizzy/FileTransfer.cs @@ -0,0 +1,72 @@ + +using System; + +namespace Dizzy +{ + + public abstract class FileTransfer + { + + // Questo è il riferimento all'elemento della taskview che + // creeremo per monitorare il progress. + protected Gtk.TreeIter iter; + + // La connessione vera e propria al server + protected SFTPConnection sftp; + + // L'etichetta da mettere sul trasferimento + protected string label; + + public delegate void Handler(string src, string dest, int transferredBytes, + int totalBytes, string message); + + public FileTransfer (string user, string password, string label) + { + // Connessione al server + this.sftp = new SFTPConnection (user, password); + this.sftp.Connect (); + + // Colleghiamo gli eventi a dei pratici handler + sftp.TransferStarted += new SFTPConnection.SFTPEvent(OnTransferStarted); + sftp.TransferProgress += new SFTPConnection.SFTPEvent(OnTransferProgress); + sftp.TransferStopped += new SFTPConnection.SFTPEvent(OnTransferStopped); + + this.label = label; + } + + public virtual void Start () {} + + public void Stop () + { + // Queste chiamate dovrebbero essere piuttosto standard in tutti + // i trasferimenti di file che per il momento ci possiamo immaginare. + this.sftp.Abort (); + this.sftp.Disconnect (); + } + + + /* + * HANDLER: Queste funzioni aggiornarenno i progress etichettandoli usando + * l'etichetta this.label, che deve essere istanziata dal costruttore + */ + public void OnTransferStarted (string src, string dest, int transferredBytes, + int totalBytes, string message) + { + EventManager.TaskTreeViewAddTask (label, 0); + this.iter = EventManager.GetIter (label); + } + + public void OnTransferProgress(string src, string dest, int transferredBytes, + int totalBytes, string message) + { + int perc = transferredBytes / (totalBytes / 100); + EventManager.TaskTreeViewSetProgress (this.iter, perc); + } + + public void OnTransferStopped (string src, string dest, int transferredBytes, + int totalBytes, string message) + { + EventManager.TaskTreeViewRemove (iter); + } + } +} diff --git a/Dizzy/FileUpload.cs b/Dizzy/FileUpload.cs new file mode 100644 index 0000000..bcf1207 --- /dev/null +++ b/Dizzy/FileUpload.cs @@ -0,0 +1,28 @@ + +using System; + +namespace Dizzy +{ + + public class FileUpload : FileTransfer + { + + + public FileUpload (string filename, + string user, + string password) : base(user, password, filename) + { + // Per il momento non c'è niente di realmente utile + // da fare qui. + } + + public override void Start () + { + sftp.Upload (this.label); + sftp.Disconnect (); + } + + } +} + + \ No newline at end of file diff --git a/Dizzy/Protocol.cs b/Dizzy/Protocol.cs index bc238c4..ee6a979 100644 --- a/Dizzy/Protocol.cs +++ b/Dizzy/Protocol.cs @@ -185,7 +185,7 @@ namespace Dizzy { foreach(FileTransfer t in this.transfers) { - Log.Info ("Stopping transfer"); + Log.Info ("Stopping transfers"); t.Stop (); } @@ -217,7 +217,7 @@ namespace Dizzy Log.Info ("Avvio il download di " + f.name); try { - FileTransfer transfer = new FileTransfer (f, this.config.GetValue("user"), this.config.password, downloadFolder); + FileDownlad transfer = new FileDownlad (f, this.config.GetValue("user"), this.config.password, downloadFolder); this.transfers.Add (transfer); transfer.Start (); } catch (Exception e) { @@ -268,292 +268,8 @@ namespace Dizzy } - public class SFTPConnection - { - - public Sftp sftp; - string user; - public delegate void SFTPEvent (string src, string dest, int transferredBytes, - int totalBytes, string message); - - public event SFTPEvent TransferStarted; - public event SFTPEvent TransferProgress; - public event SFTPEvent TransferStopped; - - public delegate void Del (string src, string dest, - int transferredBytes, int totalBytes, string message); - - public SFTPConnection (string user, string password) - { - - // Inizializziamo il protocollo - this.user = user; - this.sftp = new Sftp ("poisson.phc.unipi.it", user); - this.sftp.Password = password; - - this.sftp.OnTransferStart += new FileTransferEvent(this.TransferStartedHandler); - this.sftp.OnTransferProgress += new FileTransferEvent(this.TransferProgressHandler); - this.sftp.OnTransferEnd += new FileTransferEvent(this.TransferStoppedHandler); - - } - - public void Abort () { - this.sftp.Cancel (); - } - - private void TransferStartedHandler(string src, string dest, int transferredBytes, - int totalBytes, string message) - { - if(TransferStarted != null) - TransferStarted(src, dest, transferredBytes, totalBytes, message); - } - - private void TransferProgressHandler(string src, string dest, int transferredBytes, - int totalBytes, string message) - { - if (TransferProgress != null) - TransferProgress(src, dest, transferredBytes, totalBytes, message); - } - - private void TransferStoppedHandler(string src, string dest, int transferredBytes, - int totalBytes, string message) - { - if (TransferStopped != null) - TransferStopped(src, dest, transferredBytes, totalBytes, message); - } - - public void Connect() - { - sftp.Connect (22); - } - - public ArrayList SearchFiles() - { - ArrayList matches = new ArrayList (); - Console.WriteLine(" => Scan della nobackup"); - // ArrayList users = sftp.GetFileList ("/nobackup/"); - ArrayList users = new ArrayList(); - users.Add("robol"); - users.Add("bianchi"); - foreach(string folder in users) - { - if (folder == ".") - continue; - else if (folder == "..") - continue; - Console.WriteLine(" => Scan di {0}", folder); - try { matches.AddRange (GetFolderContent(folder)); } - catch(Exception e) { Console.WriteLine (e.Message); } - } - Console.WriteLine(" => Scan terminato"); - return matches; - } - - protected ArrayList GetFolderContent(string folder) - { - ArrayList matches, files; - File f; - string user; - files = sftp.GetFileList("/nobackup/" + folder + "/"); - matches = new ArrayList (); - foreach(string entry in files) - { - // Controllo che non sia una cartella. Evidentemente - // questo metodo non è affidabile, ma per ora è il meglio - // che SFTP ci permetta di fare. - if(entry.IndexOf(".") == -1) - matches.AddRange(GetFolderContent(folder + "/" + entry)); - else if(!entry.StartsWith(".")) - { - user = folder; - if(user.Contains("/")) - user = user.Substring(0, user.IndexOf("/")); - f = new File("/nobackup/" + folder + "/" + entry, user ); - matches.Add(f); - } - } - return matches; - } - - public void Download(File f, string downloadFolder) - { - this.sftp.Get (f.path, downloadFolder + Path.DirectorySeparatorChar + f.name); - } - - public void Upload(string filename) - { - this.sftp.Put(filename, "/nobackup/" + this.user + "/"); - } - - public void Disconnect () - { - sftp.Close (); - } - } - public class FileTransfer - { - - File file; - Gtk.TreeIter iter; - - SFTPConnection sftp; - string downloadFolder; - - public delegate void Handler(string src, string dest, int transferredBytes, - int totalBytes, string message); - - public FileTransfer (File f, string user, string password, string downloadFolder) - { - // Connessione al server - this.sftp = new SFTPConnection (user, password); - this.sftp.Connect (); - this.downloadFolder = downloadFolder; - - this.file = f; - sftp.TransferStarted += new SFTPConnection.SFTPEvent(OnTransferStarted); - sftp.TransferProgress += new SFTPConnection.SFTPEvent(OnTransferProgress); - sftp.TransferStopped += new SFTPConnection.SFTPEvent(OnTransferStopped); - - } - - public void Start () - { - sftp.Download (this.file, this.downloadFolder); - sftp.Disconnect (); - } - - public void Stop () - { - this.sftp.Abort (); - this.sftp.Disconnect (); - } - - public void OnTransferStarted (string src, string dest, int transferredBytes, - int totalBytes, string message) - { - EventManager.TaskTreeViewAddTask (file.name, 0); - this.iter = EventManager.GetIter (file.name); - } - - public void OnTransferProgress(string src, string dest, int transferredBytes, - int totalBytes, string message) - { - int perc = transferredBytes / (totalBytes / 100); - EventManager.TaskTreeViewSetProgress (this.iter, perc); - } - - public void OnTransferStopped (string src, string dest, int transferredBytes, - int totalBytes, string message) - { - EventManager.TaskTreeViewRemove (iter); - } - } - - - public class FileUpload - { - - string filename; - Gtk.TreeIter iter; - - SFTPConnection sftp; - - public delegate void Handler(string src, string dest, int transferredBytes, - int totalBytes, string message); - - public FileUpload (string filename, string user, string password) - { - // Connessione al server - this.sftp = new SFTPConnection (user, password); - this.sftp.Connect (); - - this.filename = filename; - - sftp.TransferStarted += new SFTPConnection.SFTPEvent(OnTransferStarted); - sftp.TransferProgress += new SFTPConnection.SFTPEvent(OnTransferProgress); - sftp.TransferStopped += new SFTPConnection.SFTPEvent(OnTransferStopped); - - } - - public void Start () - { - sftp.Upload (this.filename); - sftp.Disconnect (); - } - - public void Stop () - { - this.sftp.Disconnect (); - } - - public void OnTransferStarted (string src, string dest, int transferredBytes, - int totalBytes, string message) - { - EventManager.TaskTreeViewAddTask (filename, 0); - this.iter = EventManager.GetIter (filename); - } - - public void OnTransferProgress(string src, string dest, int transferredBytes, - int totalBytes, string message) - { - int perc = transferredBytes / (totalBytes / 100); - EventManager.TaskTreeViewSetProgress (iter, perc); - } - - public void OnTransferStopped (string src, string dest, int transferredBytes, - int totalBytes, string message) - { - EventManager.TaskTreeViewRemove (iter); - } - } - - - - public class FileList - { - - GlobalConfig config; - SQLiteConnection connection; - - public FileList(ref GlobalConfig config) { - - this.config = config; - - string connectionString = "Data Source=" + this.config.ListFileName () + ";Version=3"; - this.connection = new SQLiteConnection(connectionString); - } - - public ArrayList Search(string keyword, string type, string user) { - - this.connection.Open (); - ArrayList matches = new ArrayList (); - - // Eseguiamo la query - SQLiteCommand sqlCmd = this.connection.CreateCommand (); - sqlCmd.CommandText = "SELECT * from files WHERE name LIKE '%" + keyword + "%'"; - SQLiteDataReader reader = sqlCmd.ExecuteReader (); - - File tmp; - while(reader.Read()) - { - tmp = new File(reader.GetString(0), // Path - reader.GetString(2), // User - reader.GetString(1), // Name - reader.GetInt32(3)); // Size - - if ( (type == "Qualsiasi" || tmp.type.Name() == type) && - (user == "" || user == tmp.user) ) - { - matches.Add (tmp); - } - } - - this.connection.Close (); - return matches; - } - } } \ No newline at end of file diff --git a/Dizzy/SFTPConnection.cs b/Dizzy/SFTPConnection.cs new file mode 100644 index 0000000..f407a78 --- /dev/null +++ b/Dizzy/SFTPConnection.cs @@ -0,0 +1,134 @@ + +using System; +using Tamir.SharpSsh; +using System.Collections; +using System.IO; + +namespace Dizzy +{ + + public class SFTPConnection + { + + public Sftp sftp; + string user; + public delegate void SFTPEvent (string src, string dest, int transferredBytes, + int totalBytes, string message); + + public event SFTPEvent TransferStarted; + public event SFTPEvent TransferProgress; + public event SFTPEvent TransferStopped; + + public delegate void Del (string src, string dest, + int transferredBytes, int totalBytes, string message); + + public SFTPConnection (string user, string password) + { + + // Inizializziamo il protocollo + this.user = user; + this.sftp = new Sftp ("poisson.phc.unipi.it", user); + this.sftp.Password = password; + + this.sftp.OnTransferStart += new FileTransferEvent(this.TransferStartedHandler); + this.sftp.OnTransferProgress += new FileTransferEvent(this.TransferProgressHandler); + this.sftp.OnTransferEnd += new FileTransferEvent(this.TransferStoppedHandler); + + } + + public void Abort () { + this.sftp.Cancel (); + } + + private void TransferStartedHandler(string src, string dest, int transferredBytes, + int totalBytes, string message) + { + if(TransferStarted != null) + TransferStarted(src, dest, transferredBytes, totalBytes, message); + } + + private void TransferProgressHandler(string src, string dest, int transferredBytes, + int totalBytes, string message) + { + if (TransferProgress != null) + TransferProgress(src, dest, transferredBytes, totalBytes, message); + } + + private void TransferStoppedHandler(string src, string dest, int transferredBytes, + int totalBytes, string message) + { + if (TransferStopped != null) + TransferStopped(src, dest, transferredBytes, totalBytes, message); + } + + public void Connect() + { + sftp.Connect (22); + } + + public ArrayList SearchFiles() + { + ArrayList matches = new ArrayList (); + Console.WriteLine(" => Scan della nobackup"); + // ArrayList users = sftp.GetFileList ("/nobackup/"); + ArrayList users = new ArrayList(); + users.Add("robol"); + users.Add("bianchi"); + foreach(string folder in users) + { + if (folder == ".") + continue; + else if (folder == "..") + continue; + Console.WriteLine(" => Scan di {0}", folder); + try { matches.AddRange (GetFolderContent(folder)); } + catch(Exception e) { Console.WriteLine (e.Message); } + } + Console.WriteLine(" => Scan terminato"); + return matches; + } + + protected ArrayList GetFolderContent(string folder) + { + ArrayList matches, files; + File f; + string user; + files = sftp.GetFileList("/nobackup/" + folder + "/"); + matches = new ArrayList (); + foreach(string entry in files) + { + // Controllo che non sia una cartella. Evidentemente + // questo metodo non è affidabile, ma per ora è il meglio + // che SFTP ci permetta di fare. + if(entry.IndexOf(".") == -1) + matches.AddRange(GetFolderContent(folder + "/" + entry)); + else if(!entry.StartsWith(".")) + { + user = folder; + if(user.Contains("/")) + user = user.Substring(0, user.IndexOf("/")); + f = new File("/nobackup/" + folder + "/" + entry, user ); + matches.Add(f); + } + } + return matches; + } + + public void Download(File f, string downloadFolder) + { + this.sftp.Get (f.path, downloadFolder + Path.DirectorySeparatorChar + f.name); + } + + public void Upload(string filename) + { + this.sftp.Put(filename, "/nobackup/" + this.user + "/"); + } + + public void Disconnect () + { + sftp.Close (); + } + } + + +} -- 2.1.4