using System; using Gtk; using Gdk; namespace lliurex { namespace controlcenter { public class ApplicationBox : Gtk.HBox { private ConfigApplication conf; private Gtk.Label lblstatus; private Gtk.Image imgstatus; private Gtk.Button btnapp; private System.Diagnostics.Process current_script; private string std_output; private string std_error; public ApplicationBox(ConfigApplication conf) : base(false,3) { this.conf=conf; //ToDo //hardcoded lblstatus = new Gtk.Label(""); imgstatus = new Gtk.Image(Core.getCore().pixoffline); btnapp = new Gtk.Button(conf.locale_name); btnapp.SetSizeRequest(240,60); btnapp.Relief = ReliefStyle.None; btnapp.Image=new Gtk.Image(conf.icon); btnapp.Clicked+=new EventHandler(this.OnClick); this.PackStart(imgstatus); this.PackStart(lblstatus); this.PackStart(btnapp); this.SetChildPacking(imgstatus,false,false,5,PackType.Start); this.SetChildPacking(lblstatus,false,true,5,PackType.Start); this.SetChildPacking(btnapp,false,false,5,PackType.End); this.ShowAll(); SetStatusText(); } private void SetStatusText() { String tmp=""; if(conf.service) { if((conf.status & Core.S_CONF_NEEDED) == Core.S_CONF_NEEDED)tmp=tmp + " "+Mono.Unix.Catalog.GetString("setup needed")+","; if((conf.status & Core.S_RUNNING) == Core.S_RUNNING) { tmp=tmp + " "+Mono.Unix.Catalog.GetString("running")+","; imgstatus.Pixbuf = Core.getCore().pixonline; } } else { tmp=""; imgstatus.Pixbuf = Core.getCore().pixuser; } lblstatus.Text = tmp.TrimEnd(','); } private bool timer_callback() { if(current_script!=null) { Console.WriteLine("waiting for process"); std_output=current_script.StandardOutput.ReadToEnd(); std_error=current_script.StandardError.ReadToEnd(); if(current_script.HasExited) { //current_script.WaitForExit(); //Console.WriteLine("Process " + current_script.StartInfo.FileName+ " terminated with status " + current_script.ExitCode); if (std_error.Length!=0) { Console.Error.WriteLine("# !! # There has been an error:"); std_error=std_error.Trim('\n'); Console.Error.WriteLine(std_error); Core.getCore().win.Info(std_error); } else { //Console.WriteLine(std_output); //Core.getCore().win.Info("Process " + current_script.StartInfo.FileName+ " terminated with status " + current_script.ExitCode); Core.getCore().win.Info(std_output); } // Core.getCore().win.Info("Process " + current_script.StartInfo.FileName+ " terminated with status " + current_script.ExitCode); if(current_script.ExitCode==0) { conf.status = conf.status | Core.S_RUNNING; conf.status = conf.status & (~Core.S_CONF_NEEDED); SetStatusText(); } MainWindow.getInstance().SetSensible(true); return false; }else return true; } return false; } private void OnClick(object sender,EventArgs e) { Console.WriteLine("Click on "+conf.name); bool run = false; if(conf.service)run = Core.getCore().IsDependenciesConfigured(conf); else run = true; if(run) { if(System.IO.File.Exists(conf.script_path)) { //conf.status = conf.status | Core.S_RUNNING; //conf.status = conf.status & (~Core.S_CONF_NEEDED); Console.WriteLine("Running script: " + conf.script_path); System.Diagnostics.ProcessStartInfo pinfo = new System.Diagnostics.ProcessStartInfo(); pinfo.UseShellExecute = false; pinfo.RedirectStandardOutput = true; pinfo.RedirectStandardError = true; pinfo.FileName = conf.script_path; //pinfo.FileName = "ls"; current_script = System.Diagnostics.Process.Start(pinfo); if(current_script!=null) { MainWindow.getInstance().SetSensible(false); GLib.Timeout.Add (500, new GLib.TimeoutHandler (timer_callback)); } else { Console.WriteLine("Failed to execute script "+conf.locale_name); Core.getCore().win.Error("Failed to execute script "+conf.locale_name); } } else { Console.WriteLine("Script path not found: " + conf.script_path); Core.getCore().win.Error("Script path not found: " + conf.script_path); } } //SetStatusText(); } } } }