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=""; String predep=""; int counter=0; foreach (String n in conf.prereq ) { if (counter==0) { foreach(ConfigApplication conf2 in Core.getCore().applications) { if(n==conf2.name) predep=conf2.locale_name; } } else { foreach(ConfigApplication conf2 in Core.getCore().applications) { if(n==conf2.name) predep=predep + ", " + conf2.locale_name; } } counter++; } counter=0; // String prereq2=""; /* foreach (String n in conf.prereq ) { foreach(ConfigApplication conf2 in Core.getCore().applications) { if(n==conf2.name && conf2.pres.Length !=0) { prereq2=prereq2 + " " + } } } */ if(conf.service) { if((conf.status & Core.S_CONF_NEEDED) == Core.S_CONF_NEEDED) //tmp=tmp + predep + " "+Mono.Unix.Catalog.GetString("setup needed")+","; tmp=Mono.Unix.Catalog.GetString("Setup needed:\n") + predep; if((conf.status & Core.S_RUNNING) == Core.S_RUNNING) { tmp=tmp + " "+Mono.Unix.Catalog.GetString("Running")+","; imgstatus.Pixbuf = Core.getCore().pixonline; } } else { if(conf.status==Core.S_RUNNING) tmp="Running"; // string tmp2=""; // string tmp3=""; // lliurex.utils.Commands.run(conf.script_path,"status",out tmp2,out tmp3); // Console.WriteLine("> " + conf.script_path + " status " + tmp2); if(conf.status==Core.S_CONF_NEEDED) tmp=Mono.Unix.Catalog.GetString("Needs to be launched"); if(conf.status==Core.S_CONFIGURED) tmp=Mono.Unix.Catalog.GetString("Configured"); if(conf.status==Core.S_NONE) tmp=Mono.Unix.Catalog.GetString("Not launched yet"); 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(); // Console.WriteLine(conf.status); } 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) { int index=conf.script_path.IndexOf(".zmd"); bool letsgo=false; if (index==-1) letsgo=true; else if (System.IO.File.Exists(conf.script_path)) letsgo=true; if(letsgo) { //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.Arguments ="exec"; //pinfo.FileName = "ls"; //Console.WriteLine(conf.script_path); 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(); } } } }