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(); btnapp.Sensitive=conf.enabled; bool found=false; bool admin_found=false; string test_group=""; 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) { test_group=str.Trim(); test_group=test_group.Trim('\n'); if (found) break; foreach(string str2 in conf.groups) { if((test_group.Equals("admin") || test_group.Equals("root")) && !found) { admin_found=true; Console.WriteLine("[Application Box] User is in admin group. Allowing this button..."); } if(test_group.Equals(str2) && !found) { found=true; Console.WriteLine("[Application Box] Found group {0} in {1}.app. Allowing this button...",test_group,conf.name); } if (found) break; } } // Code to look for users here if (!found) { if(username=="root") { Console.WriteLine("[Application Box] User is root. Allowing {0}...",conf.name); found=true; } else foreach (string user_group in conf.users) { if(user_group.Equals(username) || user_group.Equals(".*")) { found=true; Console.WriteLine("[Application Box] Found user {0} in {1}.app. Allowing this button...",test_group,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(260,120); 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)); } */ Gtk.Label tmp_label=new Gtk.Label(); tmp_label.WidthChars=31; tmp_label.Justify=Gtk.Justification.Center; tmp_label.LineWrap=true; tmp_label.Text=conf.locale_name; vbox.Add(tmp_label); vbox.Show(); btnapp.Add(vbox); btnapp.Clicked+=new EventHandler(this.OnClick); if(found || admin_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) { if(conf.status==Core.S_RUNNING || conf.status==Core.S_CONFIGURED ) { 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); } } } } } } public void lockButtons() { Console.WriteLine("[ApplicationBox] Locking buttons..."); foreach(ConfigApplication conf2 in Core.getCore().applications) { Console.WriteLine(conf2.name); } } public void SetStatusText() { if(lliurex.controlcenter.Core.getCore().sql.sql_ready) { 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 && conf2.status!=Core.S_CONFIGURED)) 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("Service is 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"); if(conf.status==Core.S_NOT_LAUNCHED) tmp=Mono.Unix.Catalog.GetString("Service is stopped"); } else { //imgstatus.Pixbuf = Core.getCore().pixuser; imgstatus.Pixbuf = null; if (conf.date) { System.Collections.Generic.Dictionary tmp2=new System.Collections.Generic.Dictionary(); tmp2=lliurex.controlcenter.Core.getCore().sm.values[conf.name]; foreach(System.Collections.Generic.KeyValuePair n in tmp2) { tmp="Last run on " + n.Value.ToShortDateString(); } } } 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(Mono.Unix.Catalog.GetString("Process ") + current_script.StartInfo.FileName+ Mono.Unix.Catalog.GetString(" terminated with status ") + current_script.ExitCode); } else { Console.WriteLine(std_output); Core.getCore().win.Info(Mono.Unix.Catalog.GetString("Process ") + current_script.StartInfo.FileName+ Mono.Unix.Catalog.GetString(" 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); */ lliurex.controlcenter.Core.getCore().sm.refresh(); } Core.getCore().win.lockButtons(); Core.getCore().win.refresStatus(); 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); if(conf.modal) { 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 { string args=""; string command=""; if(conf.using_.Equals("")) { command = conf.script_path; if (conf.service) args ="exec"; } else { command = conf.using_; if(conf.service) args=conf.script_path + " exec"; else args=conf.script_path; } System.Threading.ThreadStart downloadTStart; downloadTStart = delegate { lliurex.utils.Commands.run(command,args); }; System.Threading.Thread downloadThread= new System.Threading.Thread(downloadTStart); downloadThread.IsBackground=true; downloadThread.Start(); } } 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(); } } } }