La ricerca viene ora effettuata con locate, anche se ci sono

Leonardo Robol [2010-02-06 17:28]
La ricerca viene ora effettuata con locate, anche se ci sono
ancora molte cose da migliorare.
Filename
File.cs
FileTreeView.cs
MainWindow.cs
Protocol.cs
diff --git a/File.cs b/File.cs
index 6dceafc..dec97cf 100644
--- a/File.cs
+++ b/File.cs
@@ -52,10 +52,16 @@ namespace Dizzy
 		{
 			// Determino il tipo di file
 			type = new FileType (path);
-
+			string [] a;
+			if (user == "")
+			{
+				a = path.Split('/');
+				if (a.Length >= 2)
+					user = a[2];
+			}
 			this.user = user;
 			this.path = path;
-			Console.WriteLine("Path set to: {0}", this.path);
+			// Console.WriteLine("Path set to: {0}", this.path);
 			string[] pieces = path.Split ('/');

 			name = pieces[pieces.Length - 1];
diff --git a/FileTreeView.cs b/FileTreeView.cs
index 2cb6d57..3b505ab 100644
--- a/FileTreeView.cs
+++ b/FileTreeView.cs
@@ -69,6 +69,11 @@ namespace Dizzy
 			fileListStore.Clear ();
 		}

+		public void SearchInProgress ()
+		{
+			this.AddFile (new File("Ricerca in corso...", "..."));
+		}
+
 		public File GetFileFromPath (TreePath path)
 		{
 			TreeIter iter = new TreeIter ();
diff --git a/MainWindow.cs b/MainWindow.cs
index cb83325..c2ceda4 100644
--- a/MainWindow.cs
+++ b/MainWindow.cs
@@ -10,6 +10,8 @@ public partial class MainWindow : Gtk.Window
 	TaskTreeView tasks;
 	GlobalConfig config;

+	bool connected = false;
+
 	public MainWindow (ref GlobalConfig config) : base(Gtk.WindowType.Toplevel)
 	{

@@ -27,7 +29,26 @@ public partial class MainWindow : Gtk.Window
 		if (downloadpath != "")
 			downloadpathchooser.SetFilename(downloadpath);

-		this.protocol = new Protocol (ref config);
+		this.Connect ();
+	}
+
+	protected void Connect ()
+	{
+		this.connected = true;
+		try {
+			this.protocol = new Protocol (ref config);
+		} catch (Exception ex) {
+			this.connected = false;
+			Gtk.MessageDialog d = new Gtk.MessageDialog(this,
+			                      						DialogFlags.Modal,
+			                      						MessageType.Error,
+			                      						ButtonsType.Ok,
+			                                            true,"");
+			d.Markup = "Errore di autenticazione. Non è stato possibile connettersi" +
+				"all'host <b>poisson.phc.unipi.it</b>.";
+			d.Run ();
+			d.Destroy ();
+		}
 	}


@@ -44,7 +65,12 @@ public partial class MainWindow : Gtk.Window

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

 	protected virtual void OnRowActivated (object o, Gtk.RowActivatedArgs args)
@@ -53,7 +79,9 @@ public partial class MainWindow : Gtk.Window
 		File f = this.files.GetFileFromPath (args.Path);
 		//	this.tasks.AddTask (f.name, 24);
 		string download_folder = this.downloadpathchooser.Filename;
-		this.protocol.Download(f, ref tasks, download_folder);
+
+		if(!connected) {this.Connect ();}
+		if(connected)  {this.protocol.Download(f, ref tasks, download_folder);}

 	}

diff --git a/Protocol.cs b/Protocol.cs
index bc86c6c..e47c1ee 100644
--- a/Protocol.cs
+++ b/Protocol.cs
@@ -5,6 +5,7 @@ using System.Threading;
 using System.Text.RegularExpressions;
 using System.IO;
 using Tamir.SharpSsh;
+using Dizzy;

 namespace Dizzy
 {
@@ -29,14 +30,11 @@ namespace Dizzy
 		                          int transferredBytes, int totalBytes, string message);


-		ArrayList fileList = new ArrayList ();
 		ArrayList threads = new ArrayList  ();

-		Thread updater;
+		Thread finder;
 		SFTPConnection sftpUpdater;

-		bool bootStrapped = false;
-
 		public Protocol (ref GlobalConfig config)
 		{
 			this.user = config.GetValue("user");
@@ -52,46 +50,23 @@ namespace Dizzy
 				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)
+			if(this.finder != null && this.finder.IsAlive)
 			{
-				f.Clear ();
-
-				foreach(File entry in this.fileList)
-				{
-					Match m = Regex.Match(entry.name, keyword.Replace(" ", "|"), RegexOptions.IgnoreCase);
-					if(m.Success)
-					   f.AddFile(entry);
-				}
+				Console.WriteLine("Ricerca in corso, tento di interromperla.");
+				this.finder.Abort ();
 			}
+
+			FileSearch fs = new FileSearch (this.user, this.password, keyword, ref f);
+			this.finder = new Thread (new ThreadStart (fs.DoSearch));
+			this.finder.Start ();
 		}

-		// 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 ()
 		{
 			foreach(FileTransfer t in this.transfers)
@@ -104,11 +79,10 @@ namespace Dizzy
 					t.Abort ();
 			}

-			if (this.updater.IsAlive)
+			if (this.finder.IsAlive)
 			{
-				this.updater.Abort ();
+				this.finder.Abort ();
 			}
-			this.sftpUpdater.Disconnect ();
 		}


@@ -316,4 +290,38 @@ namespace Dizzy
 			this.tasks.DeleteRow (this.iter);
 		}
 	}
+
+
+	class FileSearch
+{
+	SshExec sshexec;
+	string username, password, keywords;
+	FileTreeView f;
+
+	public FileSearch (string username, string password, string keywords, ref FileTreeView f)
+	{
+		this.username = username;
+		this.password = password;
+		this.keywords = keywords;
+		this.f = f;
+	}
+
+	public void DoSearch ()
+	{
+		sshexec = new SshExec("poisson.phc.unipi.it", this.username, this.password);
+		sshexec.Connect ();
+
+		string command = "locate -i -e --regex /nobackup/.+" + this.keywords;
+		string output = sshexec.RunCommand (command);
+		f.Clear ();
+		foreach(string line in Regex.Split(output, "\n"))
+		{
+			if (line.StartsWith("/nobackup/") && Regex.Match(line,".*[.].*").Success)
+				this.f.AddFile (new File(line, ""));
+		}
+		sshexec.Close ();
+	}
+
 }
+
+}
\ No newline at end of file
ViewGit