using System; using Gtk; using Gdk; namespace lliurex { namespace controlcenter { public class ApplicationBox : Gtk.HBox { public ConfigApplication conf; private Gtk.Label lblstatus; private Gtk.Image imgstatus; public 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 || conf.users.Count>0) { Console.WriteLine("[Application Box] Checking groups & users configuration in {0}.app...",conf.name); string username=Core.getCore().user; string[] groupsArray = null; groupsArray = Core.getCore().groups_array; foreach(string str in groupsArray) { if (found) break; foreach(string str2 in conf.groups) { if(str.Equals("admin") && !found) { found=true; Console.WriteLine("[Application Box] User is in admin group. Allowing this button..."); } if(str.Equals(str2) && !found) { found=true; Console.WriteLine("[Application Box] Found group {0} in {1}.app. Allowing this button...",str,conf.name); } if (found) break; } } // Code to look for users here if (!found) { foreach (string str in conf.users) { if(str.Equals(username)) { found=true; Console.WriteLine("[Application Box] Found user {0} in {1}.app. Allowing this button...",str,conf.name); } if (found) break; } } } else { Console.WriteLine("[Application Box] No group or users configuration found in {0}.app. Allowing this button for everyone...",conf.name); 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(); if (conf.locks.Capacity!=0) { Console.WriteLine("we gotta do something here for " + conf.name); if(conf.status==Core.S_RUNNING) { foreach(string n in conf.locks) { foreach(ConfigApplication conf2 in Core.getCore().applications) { if(n==conf2.name) { Console.WriteLine("[ApplicationBox] I have to lock " + conf2.name); } } } //btnapp.Sensitive=false; } } } public void lockButtons() { Console.WriteLine("[ApplicationBox] Locking buttons..."); foreach(ConfigApplication conf2 in Core.getCore().applications) { Console.WriteLine(conf2.name); } } public void SetStatusText() { String tmp=""; String predep=""; int counter=0; // Console.WriteLine(conf.prereq.Capacity); foreach (String n in conf.prereq ) { if (counter==0) { Console.WriteLine(n); foreach(ConfigApplication conf2 in Core.getCore().applications) { if(n==conf2.name && conf2.status!=Core.S_RUNNING) predep=conf2.locale_name; } } else { foreach(ConfigApplication conf2 in Core.getCore().applications) { if(n==conf2.name && conf2.status!=Core.S_RUNNING) predep=predep + ", " + conf2.locale_name; } } counter++; } Console.WriteLine(predep); counter=0; if(conf.service) { if((conf.status & Core.S_CONF_NEEDED) == Core.S_CONF_NEEDED && predep.Length!=0) { //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; } if(conf.status==Core.S_RUNNING) tmp=Mono.Unix.Catalog.GetString("Running"); if(conf.status==Core.S_CONF_NEEDED && predep.Length==0) 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); } Core.getCore().win.lockButtons(); 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; if(conf.using_.Equals("")) { pinfo.FileName = conf.script_path; if (conf.service) pinfo.Arguments ="exec"; } else { pinfo.FileName = conf.using_; if(conf.service) pinfo.Arguments=conf.script_path + " exec"; else pinfo.Arguments=conf.script_path; } Console.WriteLine(pinfo.FileName + pinfo.Arguments); //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(); } } } }