From 17e5c86df379fd4cfb082366e58c30ff22ee8cb4 Mon Sep 17 00:00:00 2001 From: Leonardo Robol Date: Tue, 9 Feb 2010 18:57:00 +0100 Subject: [PATCH] Funziona la lista, a grandi linee, ed aggiunta la tab strumenti. --- Dizzy/File.cs | 4 +- Dizzy/GlobalConfig.cs | 5 ++ Dizzy/MainWindow.cs | 27 +++++----- Dizzy/Protocol.cs | 141 ++++++++++++++++++++++++++++++++++++-------------- gtk-gui/gui.stetic | 2 +- 5 files changed, 127 insertions(+), 52 deletions(-) diff --git a/Dizzy/File.cs b/Dizzy/File.cs index dec97cf..1382f3f 100644 --- a/Dizzy/File.cs +++ b/Dizzy/File.cs @@ -46,9 +46,11 @@ namespace Dizzy public string user; public string name; public string path; + public int size; public FileType type; - public File (string path, string user) + public File (string path, string user) : this (path, user, 0) {} + public File (string path, string user, int size) { // Determino il tipo di file type = new FileType (path); diff --git a/Dizzy/GlobalConfig.cs b/Dizzy/GlobalConfig.cs index 408cfb3..65d9276 100644 --- a/Dizzy/GlobalConfig.cs +++ b/Dizzy/GlobalConfig.cs @@ -118,6 +118,11 @@ namespace Dizzy return dir; } + public string ListFileName () + { + return this.ConfigDir () + System.IO.Path.DirectorySeparatorChar + "index.db"; + } + protected string DataBaseName () { string db = this.ConfigDir () + System.IO.Path.DirectorySeparatorChar + "config.sqlite"; diff --git a/Dizzy/MainWindow.cs b/Dizzy/MainWindow.cs index 09fd9de..76e5959 100644 --- a/Dizzy/MainWindow.cs +++ b/Dizzy/MainWindow.cs @@ -9,8 +9,7 @@ public partial class MainWindow : Gtk.Window FileTreeView files; TaskTreeView tasks; GlobalConfig config; - - bool connected = false; + AuthDialog a; public MainWindow (ref GlobalConfig config) : base(Gtk.WindowType.Toplevel) { @@ -37,23 +36,32 @@ public partial class MainWindow : Gtk.Window protected void Connect () { - AuthDialog a = new AuthDialog (ref this.config); + if (this.a != null) + return; + this.a = new AuthDialog (ref this.config); a.Run (); a.Destroy (); + a = null; } protected void OnAuthenticationFailed () { + Console.WriteLine ("Auth Failed"); + return; Gtk.MessageDialog d = new Gtk.MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, true,"Errore di connessione"); + d.Markup = "Errore di autenticazione. Non è stato possibile connettersi" + "all'host poisson.phc.unipi.it."; + d.Run (); + d.Destroy (); + } @@ -72,13 +80,9 @@ public partial class MainWindow : Gtk.Window protected virtual void OnSearchRequested (object sender, System.EventArgs e) { - if(!this.config.authenticated) {this.Connect ();} - - if(this.config.authenticated) - { - this.files.SearchInProgress (); - this.protocol.Search(searchBox.Text, ref this.files); - } + this.files.Clear (); + this.files.SearchInProgress (); + this.protocol.Search (searchBox.Text, ref this.files); } protected virtual void OnRowActivated (object o, Gtk.RowActivatedArgs args) @@ -92,8 +96,7 @@ public partial class MainWindow : Gtk.Window // this.tasks.AddTask (f.name, 24); string download_folder = this.downloadpathchooser.Filename; - if(!connected) {this.Connect ();} - if(connected) {this.protocol.Download(f, ref tasks, download_folder);} + this.protocol.Download(f, ref tasks, download_folder); } diff --git a/Dizzy/Protocol.cs b/Dizzy/Protocol.cs index e0c774d..5c56a77 100644 --- a/Dizzy/Protocol.cs +++ b/Dizzy/Protocol.cs @@ -2,9 +2,11 @@ using System; using System.Collections; using System.Threading; +using System.IO.Compression; using System.Text.RegularExpressions; using System.IO; using Tamir.SharpSsh; +using System.Data.SQLite; using Dizzy; namespace Dizzy @@ -20,9 +22,6 @@ namespace Dizzy public class Protocol { - // I dati del nostro povero utente. - string user, password; - // I transfer attivi ArrayList transfers = new ArrayList (); @@ -34,6 +33,10 @@ namespace Dizzy Thread finder, listUpdater; + // Dati letti dal thread che cerca per capire cosa cercare + string keyword; + FileTreeView fileTreeView; + GlobalConfig config; public delegate void ProtocolEvent (); @@ -42,26 +45,58 @@ namespace Dizzy public Protocol (ref GlobalConfig config) { - this.user = config.GetValue("user"); - this.password = config.password; - this.config = config; - + this.config = config; this.UpdateFileList (); - - } public void Search(string keyword, ref FileTreeView f) { - if(this.finder != null && this.finder.IsAlive) + if (this.finder != null && this.finder.IsAlive) { - Console.WriteLine("Ricerca in corso, tento di interromperla."); - this.finder.Abort (); + Console.WriteLine (" => Ricerca in corso"); + return; + } + if (!System.IO.File.Exists(this.config.ListFileName ())) + { + this.UpdateFileList (); + // Dovremmo notificare l'utente che non c'è la lista + Console.WriteLine (" => Ops, non c'è la lista!"); } + else + { + Console.WriteLine (" => Comincio a cercare..."); + this.keyword = keyword; + this.fileTreeView = f; + this.finder = new Thread(new ThreadStart (this._Search)); + this.finder.Start (); + } + + } + + protected File ReadFile(StreamReader file) + { + string path = file.ReadLine(); + string user = file.ReadLine (); + int size = System.Convert.ToInt32(file.ReadLine (), 10); + return new File(path, user, size); + } + + protected void _Search() + { + // Questa funzione viene chiamata quando qualcuno richiede + // una ricerca. Inoltre abbiamo la quasi certezza che ne venga + // chiamata solo un'istanza nello stesso momento. + FileList list = new FileList (ref this.config); + + ArrayList files = list.Search (this.keyword); - FileSearch fs = new FileSearch (this.user, this.password, keyword, ref f); - this.finder = new Thread (new ThreadStart (fs.DoSearch)); - this.finder.Start (); + this.fileTreeView.Clear (); + if (files.Count == 0) + this.fileTreeView.AddFile (new File("Nessun risultato", " ")); + foreach(File f in files) + { + this.fileTreeView.AddFile (f); + } } public void UpdateFileList () @@ -100,32 +135,16 @@ namespace Dizzy catch (Exception e) { Console.WriteLine ("mmm... {0}", e.Message); this.config.authenticated = false; - this.AuthenticationRequired (); - - while(!this.config.authenticated) {} - - s = new SFTPConnection(this.config.GetValue("user"), - this.config.password); - try {s.Connect ();} - catch (Exception ex) { - Console.WriteLine (" => mm.. {0}", ex.Message); - this.config.authenticated = false; - } - - // A questo punto ci rinunciamo - if(!this.config.authenticated) - { - this.AuthenticationFailed (); - return; - } + this.AuthenticationFailed (); + return; } Console.WriteLine (" => Aggiorno la lista"); - s.Connect (); s.Download (new File("/nobackup/robol/index.db", "robol"), this.config.ConfigDir ()); Console.WriteLine (" => Lista aggiornata"); + s.Disconnect (); } @@ -140,17 +159,22 @@ namespace Dizzy if(t.IsAlive) t.Abort (); } - - if (this.finder != null && this.finder.IsAlive) + if(this.finder != null && this.finder.IsAlive) { this.finder.Abort (); } + } public void Download(File f, ref TaskTreeView tasks, string downloadFolder) { // Preparo gli argomento da passare alla funzione. + if (!this.config.authenticated) + this.AuthenticationRequired (); + while(!this.config.authenticated) {} + Console.WriteLine ("Ok, ora possiamo andare"); + ArrayList args = new ArrayList (); args.Add (f); args.Add (tasks); @@ -169,8 +193,15 @@ namespace Dizzy TaskTreeView tasks = (TaskTreeView) ((ArrayList) args)[1]; string downloadFolder = (string) ((ArrayList) args)[2]; Console.WriteLine (" => Download starting"); - FileTransfer transfer = new FileTransfer (f, ref tasks, this.user, this.password, downloadFolder); - this.transfers.Add (transfer); + + try { + FileTransfer transfer = new FileTransfer (f, ref tasks, this.config.GetValue("user"), this.config.password, downloadFolder); + this.transfers.Add (transfer); + } catch (Exception e) { + Console.WriteLine(e.Message); + this.config.authenticated = false; + this.Download (f, ref tasks, downloadFolder); + } } } @@ -389,5 +420,39 @@ 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) { + + 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 (); + + while(reader.Read()) + { + matches.Add (new File(reader.GetString(0), // Path + reader.GetString(2), // User + reader.GetInt32(3))); // Size + } + this.connection.Close (); + return matches; + } + } } \ No newline at end of file diff --git a/gtk-gui/gui.stetic b/gtk-gui/gui.stetic index 85d0e7c..2b9ee85 100644 --- a/gtk-gui/gui.stetic +++ b/gtk-gui/gui.stetic @@ -6,7 +6,7 @@ - + -- 2.1.4