diff --git a/Dizzy/AuthDialog.cs b/Dizzy/AuthDialog.cs index 074bfb2..5fefda9 100644 --- a/Dizzy/AuthDialog.cs +++ b/Dizzy/AuthDialog.cs @@ -7,19 +7,34 @@ namespace Dizzy public partial class AuthDialog : Gtk.Dialog { - GlobalConfig config; protected virtual void OnOkClicked (object sender, System.EventArgs e) { - this.config.password = passwordEntry.Text; - this.config.InsertValue("user", userEntry.Text); - this.config.authenticated = true; + Authenticate (); } - public AuthDialog (ref GlobalConfig config) + protected void Authenticate () + { + GlobalConfig.password = passwordEntry.Text; + GlobalConfig.InsertValue("user", userEntry.Text); + GlobalConfig.authenticated = true; + } + + public AuthDialog () { - this.config = config; this.Build (); } + protected virtual void OnUserEntryActivated (object sender, System.EventArgs e) + { + buttonOk.Click (); + } + + protected virtual void OnPasswordEntryActivated (object sender, System.EventArgs e) + { + buttonOk.Click (); + } + + + } } diff --git a/Dizzy/EventManager.cs b/Dizzy/EventManager.cs index efa2a88..b51c5e3 100644 --- a/Dizzy/EventManager.cs +++ b/Dizzy/EventManager.cs @@ -24,6 +24,7 @@ namespace Dizzy public static MessageDialog searchInProgress = null; public static MessageDialog listUpdate = null; + public static AuthDialog authDialog = null; // Gestione dei file e dei task. Questi devono essere inizializzati // Appena sono creati. @@ -32,7 +33,8 @@ namespace Dizzy public static void RegisterFileTreeView (ref FileTreeView f) { fileTreeView = f; } public static void RegisterTaskTreeView (ref TaskTreeView t) { taskTreeView = t; } - public EventManager () {} + public EventManager () { + } protected static Dictionary<string,TreeIter> iters = new Dictionary<string, TreeIter> (); @@ -73,6 +75,27 @@ namespace Dizzy }); } + public static void AuthenticationRequired () { + GLib.Idle.Add (delegate { + Log.Info ("Uei, sembra che qualcuno mi abbia chiesto di autenticarsi!"); + if (EventManager.authDialog == null) + { + EventManager.authDialog = new AuthDialog (); + EventManager.authDialog.Run (); + EventManager.authDialog.Destroy (); + EventManager.authDialog = null; + GlobalConfig.authenticated = true; + } + return false; + }); + + } + + public static void WaitForAuthentication () { + while (!GlobalConfig.authenticated) + System.Threading.Thread.Sleep (100); + } + public static void SearchFinished () { if (searchInProgress == null) { diff --git a/Dizzy/FileList.cs b/Dizzy/FileList.cs index 6590f73..2e4e0e4 100644 --- a/Dizzy/FileList.cs +++ b/Dizzy/FileList.cs @@ -10,14 +10,10 @@ 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"; + public FileList() { + string connectionString = "Data Source=" + GlobalConfig.ListFileName () + ";Version=3"; this.connection = new SQLiteConnection(connectionString); } diff --git a/Dizzy/GlobalConfig.cs b/Dizzy/GlobalConfig.cs index 65d9276..6f64b0f 100644 --- a/Dizzy/GlobalConfig.cs +++ b/Dizzy/GlobalConfig.cs @@ -6,13 +6,15 @@ namespace Dizzy { - public class GlobalConfig + public static class GlobalConfig { - public string password; - public bool authenticated = false; - private SQLiteConnection conn; + public static string password; + public static bool authenticated; + private static SQLiteConnection conn; - public GlobalConfig () + private static string val; + + static GlobalConfig () { conn = new SQLiteConnection ("Data Source=" + DataBaseName () + ";Version=3;"); // Controlliamo che il database sia correttamente @@ -20,14 +22,13 @@ namespace Dizzy Init (); } - public string GetValue(string field) + public static string GetValue(string field) { - lock (this) { - string val; - + + lock(typeof(GlobalConfig)) { SQLiteCommand sqlCmd; - this.conn.Open (); - sqlCmd = this.conn.CreateCommand (); + conn.Open (); + sqlCmd = conn.CreateCommand (); sqlCmd.CommandText = "SELECT value FROM config WHERE field = '" + field + "';"; SQLiteDataReader reader = sqlCmd.ExecuteReader (); @@ -35,70 +36,67 @@ namespace Dizzy val = reader.GetString(0); else val = ""; - this.conn.Close (); + conn.Close (); return val; } } - public void InsertValue(string field, string val) + public static void InsertValue(string field, string val) { - lock (this) - { - + lock(typeof(GlobalConfig)) { SQLiteCommand sqlCmd; - this.conn.Open (); - sqlCmd = this.conn.CreateCommand (); + conn.Open (); + sqlCmd = conn.CreateCommand (); sqlCmd.CommandText = "INSERT OR REPLACE INTO config VALUES ('" + field + "', '" + val + "');"; sqlCmd.ExecuteNonQuery (); - this.conn.Close (); + conn.Close (); } } - protected void Init () + private static void Init () { - + lock(typeof(GlobalConfig)) { SQLiteCommand sqlCmd; - this.conn.Open (); - sqlCmd = this.conn.CreateCommand (); + conn.Open (); + sqlCmd = conn.CreateCommand (); sqlCmd.CommandText = "SELECT * FROM sqlite_master WHERE type='table';"; SQLiteDataReader reader = sqlCmd.ExecuteReader (); if(!reader.HasRows) { - this.conn.Close (); + conn.Close (); InitialSetup (); } else - this.conn.Close (); - - } + conn.Close (); + } + } - protected void InitialSetup () + private static void InitialSetup () { - lock (this) - { + // Dobbiamo creare la struttura del database SQLiteCommand sqlCmd; - this.conn.Open (); + conn.Open (); // Tabelle delle configurazioni - sqlCmd = this.conn.CreateCommand (); + sqlCmd = conn.CreateCommand (); sqlCmd.CommandText = "CREATE TABLE config (field TEXT PRIMARY KEY, value TEXT);"; sqlCmd.ExecuteNonQuery (); // Tabella della lista - sqlCmd = this.conn.CreateCommand (); + sqlCmd = conn.CreateCommand (); sqlCmd.CommandText = "CREATE TABLE list (path TEXT PRIMARY KEY, user TEXT);"; sqlCmd.ExecuteNonQuery (); - this.conn.Close (); - } + conn.Close (); + } - public string ConfigDir () + public static string ConfigDir () { string dir = ""; if (System.Environment.OSVersion.ToString ().Contains("Windows")) @@ -118,14 +116,14 @@ namespace Dizzy return dir; } - public string ListFileName () + public static string ListFileName () { - return this.ConfigDir () + System.IO.Path.DirectorySeparatorChar + "index.db"; + return ConfigDir () + System.IO.Path.DirectorySeparatorChar + "index.db"; } - protected string DataBaseName () + private static string DataBaseName () { - string db = this.ConfigDir () + System.IO.Path.DirectorySeparatorChar + "config.sqlite"; + string db = ConfigDir () + System.IO.Path.DirectorySeparatorChar + "config.sqlite"; if (!System.IO.File.Exists (db)) System.IO.File.Create (db); return db; diff --git a/Dizzy/Main.cs b/Dizzy/Main.cs index 4f8260c..ef63b9d 100644 --- a/Dizzy/Main.cs +++ b/Dizzy/Main.cs @@ -11,15 +11,10 @@ namespace Dizzy { Application.Init (); - // Inizializziamo la configurazione generale. - // Forse sarebbe il caso di salvarla su file e - // poi passarla per riferimento a tutte le form. - GlobalConfig config = new GlobalConfig (); - // Cosa vogliamo fare MainWindow win; - win = new MainWindow (ref config); + win = new MainWindow (); win.ShowAll (); Log.Info ("Starting application"); Application.Run (); diff --git a/Dizzy/MainWindow.cs b/Dizzy/MainWindow.cs index 593e42a..701ba2c 100644 --- a/Dizzy/MainWindow.cs +++ b/Dizzy/MainWindow.cs @@ -8,14 +8,13 @@ public partial class MainWindow : Gtk.Window Protocol protocol; FileTreeView files; TaskTreeView tasks; - GlobalConfig config; AuthDialog a; - public MainWindow (ref GlobalConfig config) : base(Gtk.WindowType.Toplevel) + public MainWindow () : base(Gtk.WindowType.Toplevel) { Build (); - this.config = config; + GlobalConfig.authenticated = false; // Inizializziamo la vista dei file. files = new Dizzy.FileTreeView (filelist); @@ -27,13 +26,13 @@ public partial class MainWindow : Gtk.Window // Carichiamo qualche impostazione di default - string downloadpath = config.GetValue("download_folder"); + string downloadpath = GlobalConfig.GetValue("download_folder"); if (downloadpath != "") downloadpathchooser.SetFilename(downloadpath); - this.protocol = new Protocol (ref this.config); + this.protocol = new Protocol (); this.protocol.AuthenticationRequired += this.Connect; Log.StatusBarUpdate += this.OnStatusBarUpdate; @@ -50,7 +49,7 @@ public partial class MainWindow : Gtk.Window { if (this.a != null) return; - this.a = new AuthDialog (ref this.config); + this.a = new AuthDialog (); a.Run (); a.Destroy (); a = null; @@ -76,6 +75,11 @@ public partial class MainWindow : Gtk.Window protected virtual void OnSearchRequested (object sender, System.EventArgs e) { + Search (); + } + + protected void Search () + { this.files.Clear (); this.protocol.Search (searchBox.Text, searchUserBox.Text, typeBox.ActiveText); } @@ -93,7 +97,7 @@ public partial class MainWindow : Gtk.Window protected virtual void OnDownloadPathSelectionChanged (object sender, System.EventArgs e) { - config.InsertValue("download_folder", downloadpathchooser.Filename); + GlobalConfig.InsertValue("download_folder", downloadpathchooser.Filename); } protected virtual void OnListUpdateRequired (object sender, System.EventArgs e) @@ -103,7 +107,7 @@ public partial class MainWindow : Gtk.Window protected virtual void OnDisconnectionRequested (object sender, System.EventArgs e) { - this.config.authenticated = false; + GlobalConfig.authenticated = false; } protected virtual void OnUploadRequested (object sender, System.EventArgs e) @@ -133,6 +137,18 @@ public partial class MainWindow : Gtk.Window this.protocol.DisconnectPath (GUID); } + protected virtual void OnSearchBoxActivated (object sender, System.EventArgs e) + { + Search (); + } + + protected virtual void OnSearchUserBoxActivated (object sender, System.EventArgs e) + { + Search (); + } + + + diff --git a/Dizzy/Protocol.cs b/Dizzy/Protocol.cs index 2b944bf..a979fb2 100644 --- a/Dizzy/Protocol.cs +++ b/Dizzy/Protocol.cs @@ -31,12 +31,11 @@ namespace Dizzy ArrayList threads = new ArrayList (); - Thread finder; + Thread finder, listUpdater; // Dati letti dal thread che cerca per capire cosa cercare string keyword, user, type; - GlobalConfig config; public delegate void ProtocolEvent (); @@ -45,10 +44,8 @@ namespace Dizzy // con while(!this.config.authenticated) {} public event ProtocolEvent AuthenticationRequired; - public Protocol (ref GlobalConfig config) + public Protocol () { - this.config = config; - // this.UpdateFileList (); } public void Search(string keyword, string user, string type) @@ -58,7 +55,7 @@ namespace Dizzy Log.Info ("Ricerca in corso"); return; } - if (!System.IO.File.Exists(this.config.ListFileName ())) + if (!System.IO.File.Exists(GlobalConfig.ListFileName ())) { // Dovremmo notificare l'utente che non c'è la lista Log.Warning ("La lista non è presente, chiedo di scaricarla"); @@ -88,7 +85,7 @@ namespace Dizzy // una ricerca. Inoltre abbiamo la quasi certezza che ne venga // chiamata solo un'istanza nello stesso momento. EventManager.SearchStarted (); - FileList list = new FileList (ref this.config); + FileList list = new FileList (); ArrayList files; try { files = list.Search (this.keyword, this.type, this.user); } @@ -114,7 +111,7 @@ namespace Dizzy public void UpdateFileList () { - /*if(this.listUpdater != null && this.listUpdater.IsAlive) + if(this.listUpdater != null && this.listUpdater.IsAlive) { Log.Warning ("La lista è già in fase di aggiornamento, non faccio nulla"); return; @@ -122,8 +119,8 @@ namespace Dizzy this.listUpdater = new Thread (new ThreadStart (this._UpdateFileList)); - this.listUpdater.Start (); */ - _UpdateFileList (); + this.listUpdater.Start (); + // _UpdateFileList (); } @@ -133,13 +130,14 @@ namespace Dizzy // SFTPConnection s; Sftp s; - if (!this.config.authenticated) + if (!GlobalConfig.authenticated) { Log.Info ("I dati di autenticazione non sono presenti"); - this.AuthenticationRequired (); + // this.AuthenticationRequired (); + EventManager.AuthenticationRequired (); // Aspetto che l'utente abbia fatto. - while(!this.config.authenticated) {} + while(!GlobalConfig.authenticated) {} Log.Info ("Ho acquisito utente e password"); } @@ -149,18 +147,18 @@ namespace Dizzy // di avere. // s = new SFTPConnection (this.config.GetValue("user"), // this.config.password); - s = new Sftp("poisson.phc.unipi.it", this.config.GetValue("user"), - this.config.password); + s = new Sftp("poisson.phc.unipi.it", GlobalConfig.GetValue("user"), + GlobalConfig.password); try { s.Connect (); } catch (Exception e) { Log.Error ("Autenticazione errata (" + e.Message + ")"); - this.config.authenticated = false; + GlobalConfig.authenticated = false; } - if(!this.config.authenticated) + if(!GlobalConfig.authenticated) { EventManager.ErrorMessage ("Autenticazione fallita"); } @@ -170,7 +168,7 @@ namespace Dizzy // s.Download (new File("/nobackup/robol/index.db", "robol"), // this.config.ConfigDir ()); - s.Get("/nobackup/robol/index.db", this.config.ListFileName()); + s.Get("/nobackup/robol/index.db", GlobalConfig.ListFileName()); Log.Info ("Lista Aggiornata"); s.Close (); @@ -207,10 +205,15 @@ namespace Dizzy public void Download(File f, string downloadFolder) { + Log.Info ("Cominciamo il download di " + f.name); // Preparo gli argomento da passare alla funzione. - if (!this.config.authenticated) - this.AuthenticationRequired (); - while(!this.config.authenticated) {} + if (!GlobalConfig.authenticated) + { + Log.Info ("Chiedo di effettuare l'autenticazione"); + EventManager.AuthenticationRequired (); + } + + ArrayList args = new ArrayList (); args.Add (f); @@ -229,13 +232,17 @@ namespace Dizzy string downloadFolder = (string) ((ArrayList) args)[1]; Log.Info ("Avvio il download di " + f.name); + EventManager.WaitForAuthentication (); + + Log.Info ("Bene, ora dovrei avere i dati!"); + FileDownlad transfer; try { - transfer = new FileDownlad (f, this.config.GetValue("user"), this.config.password, downloadFolder); + transfer = new FileDownlad (f, GlobalConfig.GetValue("user"), GlobalConfig.password, downloadFolder); this.transfers.Add (transfer); } catch (Exception e) { Log.Error (e.Message); - this.config.authenticated = false; + GlobalConfig.authenticated = false; EventManager.ErrorMessage ("Autenticazione fallita"); return; @@ -252,9 +259,9 @@ namespace Dizzy public void Upload(string f) { // Preparo gli argomento da passare alla funzione. - if (!this.config.authenticated) - this.AuthenticationRequired (); - while(!this.config.authenticated) {} + if (!GlobalConfig.authenticated) + EventManager.AuthenticationRequired (); + while(!GlobalConfig.authenticated) {} ArrayList args = new ArrayList (); args.Add (f); @@ -273,13 +280,13 @@ namespace Dizzy FileUpload transfer; try { - transfer = new FileUpload (f, this.config.GetValue("user"), this.config.password); + transfer = new FileUpload (f, GlobalConfig.GetValue("user"), GlobalConfig.password); this.transfers.Add (transfer); } catch (Exception e) { Log.Error (e.Message); - this.config.authenticated = false; + GlobalConfig.authenticated = false; EventManager.ErrorMessage ("Autenticazione fallita"); return; } diff --git a/gtk-gui/Dizzy.AuthDialog.cs b/gtk-gui/Dizzy.AuthDialog.cs index 6695d49..d4bdd90 100644 --- a/gtk-gui/Dizzy.AuthDialog.cs +++ b/gtk-gui/Dizzy.AuthDialog.cs @@ -82,7 +82,6 @@ namespace Dizzy { w4.YOptions = ((Gtk.AttachOptions)(4)); // Container child table1.Gtk.Table+TableChild this.passwordEntry = new Gtk.Entry(); - this.passwordEntry.CanDefault = true; this.passwordEntry.CanFocus = true; this.passwordEntry.Name = "passwordEntry"; this.passwordEntry.IsEditable = true; @@ -97,7 +96,6 @@ namespace Dizzy { w5.YOptions = ((Gtk.AttachOptions)(4)); // Container child table1.Gtk.Table+TableChild this.userEntry = new Gtk.Entry(); - this.userEntry.CanDefault = true; this.userEntry.CanFocus = true; this.userEntry.Name = "userEntry"; this.userEntry.IsEditable = true; @@ -125,7 +123,6 @@ namespace Dizzy { w9.LayoutStyle = ((Gtk.ButtonBoxStyle)(4)); // Container child dialog1_ActionArea.Gtk.ButtonBox+ButtonBoxChild this.buttonCancel = new Gtk.Button(); - this.buttonCancel.CanDefault = true; this.buttonCancel.CanFocus = true; this.buttonCancel.Name = "buttonCancel"; this.buttonCancel.UseStock = true; @@ -153,9 +150,9 @@ namespace Dizzy { } this.DefaultWidth = 400; this.DefaultHeight = 154; - this.buttonOk.HasDefault = true; this.Show(); - this.passwordEntry.Activated += new System.EventHandler(this.OnOkClicked); + this.userEntry.Activated += new System.EventHandler(this.OnUserEntryActivated); + this.passwordEntry.Activated += new System.EventHandler(this.OnPasswordEntryActivated); this.buttonOk.Activated += new System.EventHandler(this.OnOkClicked); this.buttonOk.Clicked += new System.EventHandler(this.OnOkClicked); } diff --git a/gtk-gui/MainWindow.cs b/gtk-gui/MainWindow.cs index 3fa436f..ed15da2 100644 --- a/gtk-gui/MainWindow.cs +++ b/gtk-gui/MainWindow.cs @@ -102,7 +102,7 @@ public partial class MainWindow { this.notebook1 = new Gtk.Notebook(); this.notebook1.CanFocus = true; this.notebook1.Name = "notebook1"; - this.notebook1.CurrentPage = 1; + this.notebook1.CurrentPage = 0; // Container child notebook1.Gtk.Notebook+NotebookChild this.vbox3 = new Gtk.VBox(); this.vbox3.Name = "vbox3"; @@ -415,6 +415,8 @@ public partial class MainWindow { } this.Show(); this.DeleteEvent += new Gtk.DeleteEventHandler(this.OnDeleteEvent); + this.searchUserBox.Activated += new System.EventHandler(this.OnSearchUserBoxActivated); + this.searchBox.Activated += new System.EventHandler(this.OnSearchBoxActivated); this.button1.Clicked += new System.EventHandler(this.OnSearchRequested); this.filelist.RowActivated += new Gtk.RowActivatedHandler(this.OnRowActivated); this.tasklist.CursorChanged += new System.EventHandler(this.OnTasklistCursorChanged); diff --git a/gtk-gui/gui.stetic b/gtk-gui/gui.stetic index cd9154e..02db08d 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">1</property> + <property name="CurrentPage">0</property> <child> <widget class="Gtk.VBox" id="vbox3"> <property name="MemberName" /> @@ -105,6 +105,7 @@ <property name="CanFocus">True</property> <property name="IsEditable">True</property> <property name="InvisibleChar">•</property> + <signal name="Activated" handler="OnSearchBoxActivated" /> </widget> <packing> <property name="LeftAttach">1</property> @@ -125,6 +126,7 @@ <property name="CanFocus">True</property> <property name="IsEditable">True</property> <property name="InvisibleChar">•</property> + <signal name="Activated" handler="OnSearchUserBoxActivated" /> </widget> <packing> <property name="TopAttach">1</property> @@ -618,12 +620,11 @@ Libro</property> <child> <widget class="Gtk.Entry" id="passwordEntry"> <property name="MemberName" /> - <property name="CanDefault">True</property> <property name="CanFocus">True</property> <property name="IsEditable">True</property> <property name="Visibility">False</property> <property name="InvisibleChar">•</property> - <signal name="Activated" handler="OnOkClicked" /> + <signal name="Activated" handler="OnPasswordEntryActivated" /> </widget> <packing> <property name="TopAttach">1</property> @@ -643,10 +644,10 @@ Libro</property> <child> <widget class="Gtk.Entry" id="userEntry"> <property name="MemberName" /> - <property name="CanDefault">True</property> <property name="CanFocus">True</property> <property name="IsEditable">True</property> <property name="InvisibleChar">•</property> + <signal name="Activated" handler="OnUserEntryActivated" /> </widget> <packing> <property name="LeftAttach">1</property> @@ -689,7 +690,6 @@ Libro</property> <child> <widget class="Gtk.Button" id="buttonCancel"> <property name="MemberName" /> - <property name="CanDefault">True</property> <property name="CanFocus">True</property> <property name="UseStock">True</property> <property name="Type">StockItem</property> @@ -706,7 +706,6 @@ Libro</property> <widget class="Gtk.Button" id="buttonOk"> <property name="MemberName" /> <property name="CanDefault">True</property> - <property name="HasDefault">True</property> <property name="CanFocus">True</property> <property name="UseStock">True</property> <property name="Type">StockItem</property>