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(""); this.TooltipText=conf.comment; imgstatus = new Gtk.Image(Core.getCore().pixoffline); //btnapp = new Gtk.Button(conf.locale_name); btnapp = new Gtk.Button(); bool found=false; if(conf.groups.Count>0) { Console.WriteLine("[Application Box] Detecting groups..."); string std_out=""; string std_error=""; lliurex.utils.Commands.run("groups",System.Environment.UserName,out std_out,out std_error); std_out=std_out.Remove(0,System.Environment.UserName.Length+3); string[] groupsArray = null; char[] split_chr= { ' '}; groupsArray = std_out.Split(split_chr); foreach(string str in groupsArray) { foreach(string str2 in conf.groups) { if(str.Equals("admin")) found=true; if(str.Equals(str2)) { found=true; Console.WriteLine("[Application Box] Found {0}. Allowing this button...",str); } } } } else found=true; Gtk.VBox vbox = new Gtk.VBox(); // Console.WriteLine(btnapp.Label.Length); btnapp.SetSizeRequest(240,85); btnapp.Relief = ReliefStyle.None; //btnapp.Image=new Gtk.Image(conf.icon); Gtk.Image img=new Gtk.Image(conf.icon); vbox.Add(img); if (conf.locale_name.Length < 35) vbox.Add(new Gtk.Label(conf.locale_name)); else { string tmp=conf.locale_name; string tmp2=tmp.Insert(35,"\n"); vbox.Add(new Gtk.Label(tmp2)); } vbox.Show(); btnapp.Add(vbox); btnapp.Clicked+=new EventHandler(this.OnClick); if(found) { this.PackStart(btnapp); this.PackStart(imgstatus); this.PackStart(lblstatus); this.SetChildPacking(imgstatus,false,false,5,PackType.End); this.SetChildPacking(lblstatus,false,true,5,PackType.End); this.SetChildPacking(btnapp,false,false,5,PackType.Start); this.ShowAll(); } SetStatusText(); } public 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; 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().pixrunning; } // } // else // { if(conf.status==Core.S_RUNNING) tmp=Mono.Unix.Catalog.GetString("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"); imgstatus.Pixbuf = Core.getCore().pixonline; } if(conf.status==Core.S_NONE) tmp=Mono.Unix.Catalog.GetString("Not launched yet"); } else { //imgstatus.Pixbuf = Core.getCore().pixuser; imgstatus.Pixbuf = null; } 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); Core.getCore().win.Info("Process " + current_script.StartInfo.FileName+ " terminated with status " + current_script.ExitCode); } 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; if (conf.service) 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(Mono.Unix.Catalog.GetString("Failed to execute script ")+conf.locale_name); } } else { Console.WriteLine("Script path not found: " + conf.script_path); Core.getCore().win.Error(Mono.Unix.Catalog.GetString("Script path not found: ") + conf.script_path); } } //SetStatusText(); } } } }