Aggiunto primo supporto alla ricerca e sperimentale caricamento

Leonardo Robol [2010-02-01 08:14]
Aggiunto primo supporto alla ricerca e sperimentale caricamento
di file. Importate librerie sqlite per implementare un database
locale con la lista dei file remoti.
Filename
FileTreeView.cs
MainWindow.cs
Protocol.cs
System.Data.SQLite.dll
gtk-gui/MainWindow.cs
gtk-gui/gui.stetic
diff --git a/FileTreeView.cs b/FileTreeView.cs
index 67ab71a..9a5d2c0 100644
--- a/FileTreeView.cs
+++ b/FileTreeView.cs
@@ -75,5 +75,10 @@ namespace Dizzy
 			                           file.path);
 		}

+		public void Clear ()
+		{
+			fileListStore.Clear ();
+		}
+
 	}
 }
diff --git a/MainWindow.cs b/MainWindow.cs
index 1cc4494..cf17566 100644
--- a/MainWindow.cs
+++ b/MainWindow.cs
@@ -1,37 +1,39 @@
 using System;
 using Gtk;
+using Dizzy;

 public partial class MainWindow : Gtk.Window
 {
-
+
+	Protocol protocol;
+	FileTreeView files;
+	TaskTreeView tasks;

 	public MainWindow () : base(Gtk.WindowType.Toplevel)
 	{
+
 		Build ();

 		// Inizializziamo la vista dei file.
-		Dizzy.FileTreeView files = new Dizzy.FileTreeView (filelist);
+		files = new Dizzy.FileTreeView (filelist);

 		// .. e anche quella dei download
-		Dizzy.TaskTreeView tasks = new Dizzy.TaskTreeView (tasklist);
-
-		// Aggiungiamo un file di prova.
-		Dizzy.File f = new Dizzy.File("/nobackup/robol/Films/File di prova.avi", 1232131, "robol");
-		files.AddFile (f);
-		TreeIter it = tasks.AddTask("prova", 56);
-		tasks.SetProgress(it, 23);
-		tasks.DeleteRow(it);
+		tasks = new Dizzy.TaskTreeView (tasklist);

+		this.protocol = new Protocol ("robol", "E2omhj99");
 	}


 	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
 	{
+		protocol.Disconnect ();
 		Application.Quit ();
 		a.RetVal = true;
 	}
-

-
+	protected virtual void OnSearchRequested (object sender, System.EventArgs e)
+	{
+		this.protocol.Search(searchBox.Text, ref this.files);
+	}

 }
diff --git a/Protocol.cs b/Protocol.cs
new file mode 100644
index 0000000..2eba354
--- /dev/null
+++ b/Protocol.cs
@@ -0,0 +1,188 @@
+
+using System;
+using System.Collections;
+using System.Threading;
+using System.Text.RegularExpressions;
+using Tamir.SharpSsh;
+
+namespace Dizzy
+{
+
+
+	public class ProtocolException : ApplicationException
+	{
+		public ProtocolException () {}
+		public ProtocolException (string Message) {}
+	}
+
+	public class Protocol
+	{
+
+		// I dati del nostro povero utente.
+		string user, password;
+
+		ArrayList fileList = new ArrayList ();
+
+		Thread updater;
+		SFTPConnection sftpUpdater;
+
+		bool bootStrapped = false;
+
+		public Protocol (string user, string password)
+		{
+			this.user = user;
+			this.password = password;
+
+			try
+			{
+				this.sftpUpdater = new SFTPConnection(this.user, this.password);
+				this.sftpUpdater.Connect ();
+			}
+			catch(Exception e)
+			{
+				throw new ProtocolException("Impossibile connettersi a poisson.phc.unipi.it %% " + e.Message);
+			}
+
+			this.updater = new Thread (new ThreadStart(this._UpdateFileList));
+			this.updater.Start ();
+
+		}
+
+		public void Search(string keyword, ref FileTreeView f)
+		{
+			if (this.bootStrapped)
+			{
+				f.Clear ();
+
+				foreach(File entry in this.fileList)
+				{
+					Match m = Regex.Match(entry.name, keyword.Replace(" ", "|"), RegexOptions.IgnoreCase);
+					if(m.Success)
+					   f.AddFile(entry);
+				}
+			}
+		}
+
+		// Questa ricerca deve girare in un nuovo thread per non
+		// disturbare l'interfaccia grafica.
+		protected void _UpdateFileList ()
+		{
+			this.fileList = this.sftpUpdater.SearchFiles ();
+			if(!this.bootStrapped)
+				this.bootStrapped = true;
+		}
+
+		public void UpdateFileList ()
+		{
+			if(this.updater.IsAlive)
+			{
+				Console.WriteLine (" > Update già in corso");
+				return; // Non è il momento opportuno, l'update è in corso.
+			}
+			this.updater = new Thread (new ThreadStart(this._UpdateFileList));
+			this.updater.Start ();
+		}
+
+		public void Disconnect ()
+		{
+			if (this.updater.IsAlive)
+			{
+				this.updater.Abort ();
+			}
+			this.sftpUpdater.Disconnect ();
+		}
+
+	}
+
+	public class SFTPConnection
+	{
+
+		Sftp sftp;
+
+		public SFTPConnection (string user, string password)
+		{
+
+			// Inizializziamo il protocollo
+			this.sftp = new Sftp ("poisson.phc.unipi.it", user);
+			this.sftp.Password = password;
+
+			this.sftp.OnTransferStart += new FileTransferEvent (OnTransferStart);
+			this.sftp.OnTransferProgress += new FileTransferEvent (OnTransferProgress);
+			this.sftp.OnTransferEnd += new FileTransferEvent (OnTransferEnd);
+
+		}
+
+		private void OnTransferStart (string src, string dest, int transferredBytes, int totalBytes, string message)
+		{
+			System.Console.WriteLine(" => Transfer Starting...");
+		}
+
+		private void OnTransferProgress (string src, string dest, int trasferredBytes, int totalBytes, string message)
+		{
+			System.Console.WriteLine(" => Tranfer progress....");
+		}
+
+		private void OnTransferEnd (string src, string dest, int trasferredBytes, int totalBytes, string message)
+		{
+			System.Console.WriteLine(" => Tranfer Finished!");
+		}
+
+		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(folder + "/" + entry, 0, user );
+					matches.Add(f);
+				}
+			}
+			return matches;
+		}
+
+		public void Disconnect ()
+		{
+			sftp.Close ();
+		}
+	}
+}
diff --git a/System.Data.SQLite.dll b/System.Data.SQLite.dll
new file mode 100644
index 0000000..66f38e7
Binary files /dev/null and b/System.Data.SQLite.dll differ
diff --git a/gtk-gui/MainWindow.cs b/gtk-gui/MainWindow.cs
index 65a5886..ae6039d 100644
--- a/gtk-gui/MainWindow.cs
+++ b/gtk-gui/MainWindow.cs
@@ -14,6 +14,8 @@ public partial class MainWindow {

     private Gtk.VBox vbox1;

+    private Gtk.Label label5;
+
     private Gtk.Notebook notebook1;

     private Gtk.VBox vbox3;
@@ -22,7 +24,7 @@ public partial class MainWindow {

     private Gtk.Label label4;

-    private Gtk.Entry entry1;
+    private Gtk.Entry searchBox;

     private Gtk.Button button1;

@@ -52,12 +54,24 @@ public partial class MainWindow {
         Stetic.Gui.Initialize(this);
         // Widget MainWindow
         this.Name = "MainWindow";
-        this.Title = Mono.Unix.Catalog.GetString("MainWindow");
+        this.Title = Mono.Unix.Catalog.GetString("Dizzy PreRelease");
         this.WindowPosition = ((Gtk.WindowPosition)(4));
+        this.DefaultWidth = 640;
+        this.DefaultHeight = 480;
         // Container child MainWindow.Gtk.Container+ContainerChild
         this.vbox1 = new Gtk.VBox();
         this.vbox1.Name = "vbox1";
         this.vbox1.Spacing = 6;
+        this.vbox1.BorderWidth = ((uint)(5));
+        // Container child vbox1.Gtk.Box+BoxChild
+        this.label5 = new Gtk.Label();
+        this.label5.Name = "label5";
+        this.label5.LabelProp = Mono.Unix.Catalog.GetString("Dizzy (c) Leonardo Robol <leo@robol.it>");
+        this.vbox1.Add(this.label5);
+        Gtk.Box.BoxChild w1 = ((Gtk.Box.BoxChild)(this.vbox1[this.label5]));
+        w1.Position = 0;
+        w1.Expand = false;
+        w1.Padding = ((uint)(1));
         // Container child vbox1.Gtk.Box+BoxChild
         this.notebook1 = new Gtk.Notebook();
         this.notebook1.CanFocus = true;
@@ -77,19 +91,19 @@ public partial class MainWindow {
         this.label4.Name = "label4";
         this.label4.LabelProp = Mono.Unix.Catalog.GetString("Parole chiave");
         this.hbox1.Add(this.label4);
-        Gtk.Box.BoxChild w1 = ((Gtk.Box.BoxChild)(this.hbox1[this.label4]));
-        w1.Position = 0;
-        w1.Expand = false;
-        w1.Fill = false;
+        Gtk.Box.BoxChild w2 = ((Gtk.Box.BoxChild)(this.hbox1[this.label4]));
+        w2.Position = 0;
+        w2.Expand = false;
+        w2.Fill = false;
         // Container child hbox1.Gtk.Box+BoxChild
-        this.entry1 = new Gtk.Entry();
-        this.entry1.CanFocus = true;
-        this.entry1.Name = "entry1";
-        this.entry1.IsEditable = true;
-        this.entry1.InvisibleChar = '•';
-        this.hbox1.Add(this.entry1);
-        Gtk.Box.BoxChild w2 = ((Gtk.Box.BoxChild)(this.hbox1[this.entry1]));
-        w2.Position = 1;
+        this.searchBox = new Gtk.Entry();
+        this.searchBox.CanFocus = true;
+        this.searchBox.Name = "searchBox";
+        this.searchBox.IsEditable = true;
+        this.searchBox.InvisibleChar = '•';
+        this.hbox1.Add(this.searchBox);
+        Gtk.Box.BoxChild w3 = ((Gtk.Box.BoxChild)(this.hbox1[this.searchBox]));
+        w3.Position = 1;
         // Container child hbox1.Gtk.Box+BoxChild
         this.button1 = new Gtk.Button();
         this.button1.CanFocus = true;
@@ -97,15 +111,15 @@ public partial class MainWindow {
         this.button1.UseUnderline = true;
         this.button1.Label = Mono.Unix.Catalog.GetString("Cerca");
         this.hbox1.Add(this.button1);
-        Gtk.Box.BoxChild w3 = ((Gtk.Box.BoxChild)(this.hbox1[this.button1]));
-        w3.Position = 2;
-        w3.Expand = false;
-        w3.Fill = false;
-        this.vbox3.Add(this.hbox1);
-        Gtk.Box.BoxChild w4 = ((Gtk.Box.BoxChild)(this.vbox3[this.hbox1]));
-        w4.Position = 0;
+        Gtk.Box.BoxChild w4 = ((Gtk.Box.BoxChild)(this.hbox1[this.button1]));
+        w4.Position = 2;
         w4.Expand = false;
         w4.Fill = false;
+        this.vbox3.Add(this.hbox1);
+        Gtk.Box.BoxChild w5 = ((Gtk.Box.BoxChild)(this.vbox3[this.hbox1]));
+        w5.Position = 0;
+        w5.Expand = false;
+        w5.Fill = false;
         // Container child vbox3.Gtk.Box+BoxChild
         this.GtkScrolledWindow = new Gtk.ScrolledWindow();
         this.GtkScrolledWindow.Name = "GtkScrolledWindow";
@@ -116,8 +130,8 @@ public partial class MainWindow {
         this.filelist.Name = "filelist";
         this.GtkScrolledWindow.Add(this.filelist);
         this.vbox3.Add(this.GtkScrolledWindow);
-        Gtk.Box.BoxChild w6 = ((Gtk.Box.BoxChild)(this.vbox3[this.GtkScrolledWindow]));
-        w6.Position = 1;
+        Gtk.Box.BoxChild w7 = ((Gtk.Box.BoxChild)(this.vbox3[this.GtkScrolledWindow]));
+        w7.Position = 1;
         this.notebook1.Add(this.vbox3);
         // Notebook tab
         this.label1 = new Gtk.Label();
@@ -139,8 +153,8 @@ public partial class MainWindow {
         this.tasklist.Name = "tasklist";
         this.GtkScrolledWindow1.Add(this.tasklist);
         this.vbox2.Add(this.GtkScrolledWindow1);
-        Gtk.Box.BoxChild w9 = ((Gtk.Box.BoxChild)(this.vbox2[this.GtkScrolledWindow1]));
-        w9.Position = 0;
+        Gtk.Box.BoxChild w10 = ((Gtk.Box.BoxChild)(this.vbox2[this.GtkScrolledWindow1]));
+        w10.Position = 0;
         // Container child vbox2.Gtk.Box+BoxChild
         this.hbox2 = new Gtk.HBox();
         this.hbox2.Name = "hbox2";
@@ -151,24 +165,24 @@ public partial class MainWindow {
         this.label2.Name = "label2";
         this.label2.LabelProp = Mono.Unix.Catalog.GetString("Scarica in");
         this.hbox2.Add(this.label2);
-        Gtk.Box.BoxChild w10 = ((Gtk.Box.BoxChild)(this.hbox2[this.label2]));
-        w10.Position = 0;
-        w10.Expand = false;
-        w10.Fill = false;
+        Gtk.Box.BoxChild w11 = ((Gtk.Box.BoxChild)(this.hbox2[this.label2]));
+        w11.Position = 0;
+        w11.Expand = false;
+        w11.Fill = false;
         // Container child hbox2.Gtk.Box+BoxChild
         this.downloadpathchooser = new Gtk.FileChooserButton(Mono.Unix.Catalog.GetString("Seleziona una cartella"), ((Gtk.FileChooserAction)(2)));
         this.downloadpathchooser.Name = "downloadpathchooser";
         this.hbox2.Add(this.downloadpathchooser);
-        Gtk.Box.BoxChild w11 = ((Gtk.Box.BoxChild)(this.hbox2[this.downloadpathchooser]));
-        w11.Position = 1;
-        this.vbox2.Add(this.hbox2);
-        Gtk.Box.BoxChild w12 = ((Gtk.Box.BoxChild)(this.vbox2[this.hbox2]));
+        Gtk.Box.BoxChild w12 = ((Gtk.Box.BoxChild)(this.hbox2[this.downloadpathchooser]));
         w12.Position = 1;
-        w12.Expand = false;
-        w12.Fill = false;
-        this.notebook1.Add(this.vbox2);
-        Gtk.Notebook.NotebookChild w13 = ((Gtk.Notebook.NotebookChild)(this.notebook1[this.vbox2]));
+        this.vbox2.Add(this.hbox2);
+        Gtk.Box.BoxChild w13 = ((Gtk.Box.BoxChild)(this.vbox2[this.hbox2]));
         w13.Position = 1;
+        w13.Expand = false;
+        w13.Fill = false;
+        this.notebook1.Add(this.vbox2);
+        Gtk.Notebook.NotebookChild w14 = ((Gtk.Notebook.NotebookChild)(this.notebook1[this.vbox2]));
+        w14.Position = 1;
         // Notebook tab
         this.label3 = new Gtk.Label();
         this.label3.Name = "label3";
@@ -176,24 +190,24 @@ public partial class MainWindow {
         this.notebook1.SetTabLabel(this.vbox2, this.label3);
         this.label3.ShowAll();
         this.vbox1.Add(this.notebook1);
-        Gtk.Box.BoxChild w14 = ((Gtk.Box.BoxChild)(this.vbox1[this.notebook1]));
-        w14.Position = 0;
+        Gtk.Box.BoxChild w15 = ((Gtk.Box.BoxChild)(this.vbox1[this.notebook1]));
+        w15.Position = 1;
         // Container child vbox1.Gtk.Box+BoxChild
         this.statusbar2 = new Gtk.Statusbar();
         this.statusbar2.Name = "statusbar2";
         this.statusbar2.Spacing = 6;
         this.vbox1.Add(this.statusbar2);
-        Gtk.Box.BoxChild w15 = ((Gtk.Box.BoxChild)(this.vbox1[this.statusbar2]));
-        w15.Position = 1;
-        w15.Expand = false;
-        w15.Fill = false;
+        Gtk.Box.BoxChild w16 = ((Gtk.Box.BoxChild)(this.vbox1[this.statusbar2]));
+        w16.Position = 2;
+        w16.Expand = false;
+        w16.Fill = false;
         this.Add(this.vbox1);
         if ((this.Child != null)) {
             this.Child.ShowAll();
         }
-        this.DefaultWidth = 400;
-        this.DefaultHeight = 300;
         this.Show();
         this.DeleteEvent += new Gtk.DeleteEventHandler(this.OnDeleteEvent);
+        this.searchBox.Activated += new System.EventHandler(this.OnSearchRequested);
+        this.button1.Clicked += new System.EventHandler(this.OnSearchRequested);
     }
 }
diff --git a/gtk-gui/gui.stetic b/gtk-gui/gui.stetic
index 22a97f4..58d1ecc 100644
--- a/gtk-gui/gui.stetic
+++ b/gtk-gui/gui.stetic
@@ -6,17 +6,32 @@
   </configuration>
   <import>
     <widget-library name="glade-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
-    <widget-library name="../bin/Debug/Dizzy.exe" internal="true" />
+    <widget-library name="../bin/Release/Dizzy.exe" internal="true" />
   </import>
-  <widget class="Gtk.Window" id="MainWindow" design-size="400 300">
+  <widget class="Gtk.Window" id="MainWindow" design-size="474 300">
     <property name="MemberName" />
-    <property name="Title" translatable="yes">MainWindow</property>
+    <property name="Title" translatable="yes">Dizzy PreRelease</property>
     <property name="WindowPosition">CenterOnParent</property>
+    <property name="DefaultWidth">640</property>
+    <property name="DefaultHeight">480</property>
     <signal name="DeleteEvent" handler="OnDeleteEvent" />
     <child>
       <widget class="Gtk.VBox" id="vbox1">
         <property name="MemberName" />
         <property name="Spacing">6</property>
+        <property name="BorderWidth">5</property>
+        <child>
+          <widget class="Gtk.Label" id="label5">
+            <property name="MemberName" />
+            <property name="LabelProp" translatable="yes">Dizzy (c) Leonardo Robol &lt;leo@robol.it&gt;</property>
+          </widget>
+          <packing>
+            <property name="Position">0</property>
+            <property name="AutoSize">False</property>
+            <property name="Expand">False</property>
+            <property name="Padding">1</property>
+          </packing>
+        </child>
         <child>
           <widget class="Gtk.Notebook" id="notebook1">
             <property name="MemberName" />
@@ -44,11 +59,12 @@
                       </packing>
                     </child>
                     <child>
-                      <widget class="Gtk.Entry" id="entry1">
+                      <widget class="Gtk.Entry" id="searchBox">
                         <property name="MemberName" />
                         <property name="CanFocus">True</property>
                         <property name="IsEditable">True</property>
                         <property name="InvisibleChar">•</property>
+                        <signal name="Activated" handler="OnSearchRequested" />
                       </widget>
                       <packing>
                         <property name="Position">1</property>
@@ -62,6 +78,7 @@
                         <property name="Type">TextOnly</property>
                         <property name="Label" translatable="yes">Cerca</property>
                         <property name="UseUnderline">True</property>
+                        <signal name="Clicked" handler="OnSearchRequested" />
                       </widget>
                       <packing>
                         <property name="Position">2</property>
@@ -179,7 +196,7 @@
             </child>
           </widget>
           <packing>
-            <property name="Position">0</property>
+            <property name="Position">1</property>
             <property name="AutoSize">True</property>
           </packing>
         </child>
@@ -195,7 +212,7 @@
             </child>
           </widget>
           <packing>
-            <property name="Position">1</property>
+            <property name="Position">2</property>
             <property name="AutoSize">True</property>
             <property name="Expand">False</property>
             <property name="Fill">False</property>
ViewGit