From b43328e9019752867f0c994c5b9bda6efcf451fa Mon Sep 17 00:00:00 2001 From: Leonardo Robol Date: Wed, 10 Feb 2010 18:49:48 +0100 Subject: [PATCH] Mmmm.... sento puzza di release stabile! Sembra proprio che ora che ho spostato tutte le chiamate alla grafica nel thread principale il programma abbia smesso di crashare. --- Dizzy/EventManager.cs | 52 ++++++++++++++++++++++++++++ Dizzy/Log.cs | 12 +++---- Dizzy/Main.cs | 2 +- Dizzy/MainWindow.cs | 6 ++-- Dizzy/Protocol.cs | 70 ++++++++++---------------------------- gtk-gui/gui.stetic | 93 --------------------------------------------------- 6 files changed, 78 insertions(+), 157 deletions(-) diff --git a/Dizzy/EventManager.cs b/Dizzy/EventManager.cs index ea9300e..703f821 100644 --- a/Dizzy/EventManager.cs +++ b/Dizzy/EventManager.cs @@ -1,6 +1,7 @@ using System; using Gtk; +using System.Collections.Generic; namespace Dizzy { @@ -33,6 +34,8 @@ namespace Dizzy public EventManager () {} + protected static Dictionary iters = new Dictionary (); + // Metodo generico per segnalare un'errore dell'applicazione. public static void ErrorMessage (string message) { @@ -136,5 +139,54 @@ namespace Dizzy return false; }); } + + public static void TaskTreeViewAddTask (string filename, int perc) + { + if (taskTreeView == null) + return; + GLib.Idle.Add (delegate { + EventManager.AddIter(filename, taskTreeView.AddTask (filename, perc)); + return false; + }); + + } + + public static void TaskTreeViewSetProgress (TreeIter iter, int perc) + { + if (taskTreeView == null) + return; + GLib.Idle.Add(delegate { + taskTreeView.SetProgress (iter, perc); + return false; + }); + } + + public static void TaskTreeViewRemove (TreeIter iter) + { + if (taskTreeView == null) + return; + GLib.Idle.Add (delegate { + taskTreeView.DeleteRow (iter); + return false; + }); + } + + public static void AddIter (string filename, TreeIter iter) + { + iters.Add(filename, iter); + } + + public static TreeIter GetIter (string filename) + { + try {return iters[filename];} + catch (Exception e) + { + // Aspettiamo nella speranza che le cose vadano meglio + Log.Warning ("Sto richiamando GetIter () per ottenre l'iter (" + e.Message + ")"); + System.Threading.Thread.Sleep(200); + } + return GetIter(filename); + } + } } diff --git a/Dizzy/Log.cs b/Dizzy/Log.cs index 9e322b0..6be287e 100644 --- a/Dizzy/Log.cs +++ b/Dizzy/Log.cs @@ -21,8 +21,8 @@ namespace Dizzy ConsoleColor initial = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.DarkRed; Console.Write (" ==> "); - Console.ForegroundColor = initial; Console.WriteLine (message); + Console.ForegroundColor = initial; } @@ -32,8 +32,8 @@ namespace Dizzy ConsoleColor initial = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Cyan; Console.Write (" ==> "); - Console.ForegroundColor = initial; Console.WriteLine (message); + Console.ForegroundColor = initial; } @@ -43,21 +43,19 @@ namespace Dizzy ConsoleColor initial = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Green; Console.Write (" ==> "); - Console.ForegroundColor = initial; Console.WriteLine (message); - + Console.ForegroundColor = initial; try {StatusBarUpdate(message);} catch (Exception e) { Log.Error ("Impossibile loggare sulla statusbar" + e.Message); } } public static void Lock (string message) { - ConsoleColor initial = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.DarkGray; Console.Write (" ==> "); - Console.ForegroundColor = initial; + Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine (message); + Console.ForegroundColor = initial; } } } diff --git a/Dizzy/Main.cs b/Dizzy/Main.cs index b22d3d1..4f8260c 100644 --- a/Dizzy/Main.cs +++ b/Dizzy/Main.cs @@ -21,7 +21,7 @@ namespace Dizzy win = new MainWindow (ref config); win.ShowAll (); - + Log.Info ("Starting application"); Application.Run (); } diff --git a/Dizzy/MainWindow.cs b/Dizzy/MainWindow.cs index 811d410..1f86831 100644 --- a/Dizzy/MainWindow.cs +++ b/Dizzy/MainWindow.cs @@ -85,7 +85,7 @@ public partial class MainWindow : Gtk.Window string download_folder = this.downloadpathchooser.Filename; - this.protocol.Download(f, ref tasks, download_folder); + this.protocol.Download(f, download_folder); } @@ -108,8 +108,8 @@ public partial class MainWindow : Gtk.Window { // 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); + + this.protocol.Upload(uploadChooserButton.Filename); } } \ No newline at end of file diff --git a/Dizzy/Protocol.cs b/Dizzy/Protocol.cs index 12858c0..102c777 100644 --- a/Dizzy/Protocol.cs +++ b/Dizzy/Protocol.cs @@ -200,7 +200,7 @@ namespace Dizzy } - public void Download(File f, ref TaskTreeView tasks, string downloadFolder) + public void Download(File f, string downloadFolder) { // Preparo gli argomento da passare alla funzione. if (!this.config.authenticated) @@ -209,7 +209,6 @@ namespace Dizzy ArrayList args = new ArrayList (); args.Add (f); - args.Add (tasks); args.Add (downloadFolder); Thread t = new Thread (new ParameterizedThreadStart(_Download)); t.Start (args); @@ -222,22 +221,21 @@ namespace Dizzy // solo un oggetto a Download e quindi è il meglio che // mi riesce di fare. File f = (File) ((ArrayList) args)[0]; - TaskTreeView tasks = (TaskTreeView) ((ArrayList) args)[1]; string downloadFolder = (string) ((ArrayList) args)[2]; Log.Info ("Avvio il download di " + f.name); try { - FileTransfer transfer = new FileTransfer (f, ref tasks, this.config.GetValue("user"), this.config.password, downloadFolder); + FileTransfer transfer = new FileTransfer (f, this.config.GetValue("user"), this.config.password, downloadFolder); this.transfers.Add (transfer); } catch (Exception e) { Log.Error (e.Message); this.config.authenticated = false; - this.Download (f, ref tasks, downloadFolder); + this.Download (f, downloadFolder); } } - public void Upload(string f, ref TaskTreeView tasks) + public void Upload(string f) { // Preparo gli argomento da passare alla funzione. if (!this.config.authenticated) @@ -246,7 +244,6 @@ namespace Dizzy ArrayList args = new ArrayList (); args.Add (f); - args.Add (tasks); Thread t = new Thread (new ParameterizedThreadStart(_Upload)); t.Start (args); this.threads.Add (t); @@ -258,17 +255,16 @@ namespace Dizzy // 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); + FileUpload transfer = new FileUpload (f, 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); + this.Upload (f); } } @@ -398,7 +394,6 @@ namespace Dizzy public class FileTransfer { - TaskTreeView tasks; File file; Gtk.TreeIter iter; @@ -407,13 +402,12 @@ namespace Dizzy public delegate void Handler(string src, string dest, int transferredBytes, int totalBytes, string message); - public FileTransfer (File f, ref TaskTreeView tasks, string user, string password, string downloadFolder) + public FileTransfer (File f, string user, string password, string downloadFolder) { // Connessione al server this.sftp = new SFTPConnection (user, password); this.sftp.Connect (); - this.tasks = tasks; this.file = f; sftp.TransferStarted += new SFTPConnection.SFTPEvent(OnTransferStarted); @@ -432,20 +426,21 @@ namespace Dizzy public void OnTransferStarted (string src, string dest, int transferredBytes, int totalBytes, string message) { - this.iter = this.tasks.AddTask(file.name, 0); + EventManager.TaskTreeViewAddTask (file.name, 0); + this.iter = EventManager.GetIter (file.name); } public void OnTransferProgress(string src, string dest, int transferredBytes, int totalBytes, string message) { int perc = transferredBytes / (totalBytes / 100); - this.tasks.SetProgress(this.iter, perc); + EventManager.TaskTreeViewSetProgress (this.iter, perc); } public void OnTransferStopped (string src, string dest, int transferredBytes, int totalBytes, string message) { - this.tasks.DeleteRow (this.iter); + EventManager.TaskTreeViewRemove (iter); } } @@ -453,7 +448,6 @@ namespace Dizzy public class FileUpload { - TaskTreeView tasks; string filename; Gtk.TreeIter iter; @@ -462,13 +456,12 @@ namespace Dizzy 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) + public FileUpload (string filename, 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); @@ -487,55 +480,26 @@ namespace Dizzy public void OnTransferStarted (string src, string dest, int transferredBytes, int totalBytes, string message) { - this.iter = this.tasks.AddTask(filename, 0); + EventManager.TaskTreeViewAddTask (filename, 0); + this.iter = EventManager.GetIter (filename); } public void OnTransferProgress(string src, string dest, int transferredBytes, int totalBytes, string message) { int perc = transferredBytes / (totalBytes / 100); - this.tasks.SetProgress(this.iter, perc); + EventManager.TaskTreeViewSetProgress (iter, perc); } public void OnTransferStopped (string src, string dest, int transferredBytes, int totalBytes, string message) { - this.tasks.DeleteRow (this.iter); + EventManager.TaskTreeViewRemove (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 (); - } - -} + public class FileList { diff --git a/gtk-gui/gui.stetic b/gtk-gui/gui.stetic index ff37542..6195a84 100644 --- a/gtk-gui/gui.stetic +++ b/gtk-gui/gui.stetic @@ -706,97 +706,4 @@ Libro - - - CenterOnParent - 2 - False - - - - 2 - - - - 6 - - - - 64 - 64 - stock:gtk-dialog-error Menu - - - 0 - True - False - False - - - - - - label4 - True - - - 1 - True - False - False - - - - - 0 - True - False - False - - - - - - - - 10 - 5 - 2 - End - - - - True - True - True - StockItem - gtk-cancel - -6 - gtk-cancel - - - False - False - - - - - - True - True - True - StockItem - gtk-ok - -5 - gtk-ok - - - 1 - False - False - - - - - \ No newline at end of file -- 2.1.4