Aggiunta possiblità di caricare file anche se un pochino buggata.

Leonardo Robol [2010-02-10 17:05]
Aggiunta possiblità di caricare file anche se un pochino buggata.
Filename
Dizzy/MainWindow.cs
Dizzy/Protocol.cs
gtk-gui/MainWindow.cs
gtk-gui/gui.stetic
diff --git a/Dizzy/MainWindow.cs b/Dizzy/MainWindow.cs
index 1be172a..811d410 100644
--- a/Dizzy/MainWindow.cs
+++ b/Dizzy/MainWindow.cs
@@ -104,6 +104,12 @@ public partial class MainWindow : Gtk.Window
 		this.config.authenticated = false;
 	}

-
-
-}
+	protected virtual void OnUploadRequested (object sender, System.EventArgs e)
+	{
+		// Come prima cosa otteniamo il file che il povero utente desidera
+		// caricare sul server.
+		string filename = uploadChooserButton.Filename;
+		this.protocol.Upload(filename, ref this.tasks);
+
+	}
+}
\ No newline at end of file
diff --git a/Dizzy/Protocol.cs b/Dizzy/Protocol.cs
index fa33361..12858c0 100644
--- a/Dizzy/Protocol.cs
+++ b/Dizzy/Protocol.cs
@@ -237,12 +237,50 @@ namespace Dizzy
 			}
 		}

+		public void Upload(string f, ref TaskTreeView tasks)
+		{
+			// Preparo gli argomento da passare alla funzione.
+			if (!this.config.authenticated)
+				this.AuthenticationRequired ();
+			while(!this.config.authenticated) {}
+
+			ArrayList args = new ArrayList ();
+			args.Add (f);
+			args.Add (tasks);
+			Thread t = new Thread (new ParameterizedThreadStart(_Upload));
+			t.Start (args);
+			this.threads.Add (t);
+		}
+
+		protected void _Upload(object args)
+		{
+			// Questi cast sono piuttosto brutali ma posso passare
+			// solo un oggetto a Download e quindi è il meglio che
+			// mi riesce di fare.
+			string f = (string) ((ArrayList) args)[0];
+			TaskTreeView tasks = (TaskTreeView) ((ArrayList) args)[1];
+			Log.Info ("Avvio l'upload di " + f);
+
+			try {
+				FileUpload transfer = new FileUpload (f, ref tasks, this.config.GetValue("user"), this.config.password);
+				this.transfers.Add (transfer);
+			} catch (Exception e) {
+
+				Log.Error (e.Message);
+				this.config.authenticated = false;
+				this.Upload (f, ref tasks);
+			}
+		}
+
+
+
 	}

 	public class SFTPConnection
 	{

 		public Sftp sftp;
+		string user;
 		public delegate void SFTPEvent (string src, string dest, int transferredBytes,
 		                                int totalBytes, string message);

@@ -257,6 +295,7 @@ namespace Dizzy
 		{

 			// Inizializziamo il protocollo
+			this.user = user;
 			this.sftp = new Sftp ("poisson.phc.unipi.it", user);
 			this.sftp.Password = password;

@@ -345,6 +384,11 @@ namespace Dizzy
 			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 ();
@@ -406,6 +450,61 @@ namespace Dizzy
 	}


+	public class FileUpload
+	{
+
+		TaskTreeView tasks;
+		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, ref TaskTreeView tasks, string user, string password)
+		{
+			// Connessione al server
+			this.sftp = new SFTPConnection (user, password);
+			this.sftp.Connect ();
+
+			this.tasks = tasks;
+			this.filename = filename;
+
+			sftp.TransferStarted += new SFTPConnection.SFTPEvent(OnTransferStarted);
+			sftp.TransferProgress += new SFTPConnection.SFTPEvent(OnTransferProgress);
+			sftp.TransferStopped += new SFTPConnection.SFTPEvent(OnTransferStopped);
+
+			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)
+		{
+			this.iter = this.tasks.AddTask(filename, 0);
+		}
+
+		public void OnTransferProgress(string src, string dest, int transferredBytes,
+		                               int totalBytes, string message)
+		{
+			int perc = transferredBytes / (totalBytes / 100);
+			this.tasks.SetProgress(this.iter, perc);
+		}
+
+		public void OnTransferStopped (string src, string dest, int transferredBytes,
+		                               int totalBytes, string message)
+		{
+			this.tasks.DeleteRow (this.iter);
+		}
+	}
+
+
 	class FileSearch
 {
 	SshExec sshexec;
diff --git a/gtk-gui/MainWindow.cs b/gtk-gui/MainWindow.cs
index 950bdc5..c77515f 100644
--- a/gtk-gui/MainWindow.cs
+++ b/gtk-gui/MainWindow.cs
@@ -76,7 +76,7 @@ public partial class MainWindow {

     private Gtk.Button button184;

-    private Gtk.FileChooserButton filechooserbutton1;
+    private Gtk.FileChooserButton uploadChooserButton;

     private Gtk.Label label7;

@@ -100,7 +100,7 @@ public partial class MainWindow {
         this.notebook1 = new Gtk.Notebook();
         this.notebook1.CanFocus = true;
         this.notebook1.Name = "notebook1";
-        this.notebook1.CurrentPage = 0;
+        this.notebook1.CurrentPage = 2;
         // Container child notebook1.Gtk.Notebook+NotebookChild
         this.vbox3 = new Gtk.VBox();
         this.vbox3.Name = "vbox3";
@@ -365,10 +365,10 @@ public partial class MainWindow {
         w27.XOptions = ((Gtk.AttachOptions)(4));
         w27.YOptions = ((Gtk.AttachOptions)(4));
         // Container child table2.Gtk.Table+TableChild
-        this.filechooserbutton1 = new Gtk.FileChooserButton(Mono.Unix.Catalog.GetString("Seleziona un file"), ((Gtk.FileChooserAction)(0)));
-        this.filechooserbutton1.Name = "filechooserbutton1";
-        this.table2.Add(this.filechooserbutton1);
-        Gtk.Table.TableChild w28 = ((Gtk.Table.TableChild)(this.table2[this.filechooserbutton1]));
+        this.uploadChooserButton = new Gtk.FileChooserButton(Mono.Unix.Catalog.GetString("Seleziona un file"), ((Gtk.FileChooserAction)(0)));
+        this.uploadChooserButton.Name = "uploadChooserButton";
+        this.table2.Add(this.uploadChooserButton);
+        Gtk.Table.TableChild w28 = ((Gtk.Table.TableChild)(this.table2[this.uploadChooserButton]));
         w28.TopAttach = ((uint)(4));
         w28.BottomAttach = ((uint)(5));
         w28.XOptions = ((Gtk.AttachOptions)(4));
@@ -403,6 +403,7 @@ public partial class MainWindow {
         this.button1.Clicked += new System.EventHandler(this.OnSearchRequested);
         this.filelist.RowActivated += new Gtk.RowActivatedHandler(this.OnRowActivated);
         this.downloadpathchooser.SelectionChanged += new System.EventHandler(this.OnDownloadPathSelectionChanged);
+        this.button184.Clicked += new System.EventHandler(this.OnUploadRequested);
         this.button183.Clicked += new System.EventHandler(this.OnListUpdateRequired);
         this.button182.Clicked += new System.EventHandler(this.OnDisconnectionRequested);
     }
diff --git a/gtk-gui/gui.stetic b/gtk-gui/gui.stetic
index 4d2582b..ff37542 100644
--- a/gtk-gui/gui.stetic
+++ b/gtk-gui/gui.stetic
@@ -25,7 +25,7 @@
           <widget class="Gtk.Notebook" id="notebook1">
             <property name="MemberName" />
             <property name="CanFocus">True</property>
-            <property name="CurrentPage">0</property>
+            <property name="CurrentPage">2</property>
             <child>
               <widget class="Gtk.VBox" id="vbox3">
                 <property name="MemberName" />
@@ -447,6 +447,7 @@ Libro</property>
                     <property name="Type">TextOnly</property>
                     <property name="Label" translatable="yes">Carica file</property>
                     <property name="UseUnderline">True</property>
+                    <signal name="Clicked" handler="OnUploadRequested" />
                   </widget>
                   <packing>
                     <property name="TopAttach">4</property>
@@ -465,7 +466,7 @@ Libro</property>
                   </packing>
                 </child>
                 <child>
-                  <widget class="Gtk.FileChooserButton" id="filechooserbutton1">
+                  <widget class="Gtk.FileChooserButton" id="uploadChooserButton">
                     <property name="MemberName" />
                   </widget>
                   <packing>
ViewGit