using System; using Gtk; using System.Collections.Generic; using Gdk; namespace lliurex { namespace controlcenter { /** * * Core class * **/ public class Core { public const uint S_NONE = 0; public const uint S_CONFIGURED = 1; public const uint S_RUNNING = 2; public const uint S_NOT_LAUNCHED = 3; public const uint S_CONF_NEEDED = 4; private bool no_gui; private bool no_info; private static Core unique_instance = null; public String locale; public String short_locale; public String[] groups_array; public String user; public String name; public List applications; public MainWindow win; public Gdk.Pixbuf pixdefault; public Gdk.Pixbuf piximportant; public Gdk.Pixbuf pixoffline; public Gdk.Pixbuf pixonline; public Gdk.Pixbuf pixuser; public Gdk.Pixbuf pixrunning; public Gdk.Pixbuf pixinfo; public Gdk.Pixbuf pixerror; public StatusManager sm; public UniconfManager um; public lliurex.controlcenter.WarningDialog warndialog; public SqlManager sql; /* * Static Core instance getter */ public static Core getCore() { if (Core.unique_instance==null) { Core.unique_instance = new Core(); } return Core.unique_instance; } public bool IsGUI { get { return !no_gui; } } public bool IsVerbosed { get { return no_info; } } /* * Core can only be instanced through static getter */ private Core() { no_gui=false; no_info=false; } public void Init(bool no_gui, bool no_info) { this.no_gui=no_gui; this.no_info=no_info; this.user=System.Environment.UserName; string std_out=""; string std_error=""; lliurex.utils.Commands.run("groups","",out std_out,out std_error); char[] split_chr= { ' '}; Console.WriteLine("[Core] " + this.user + " groups:"); std_out=std_out.Replace(this.user+" ",""); this.groups_array = std_out.Split(split_chr); foreach(string grp in this.groups_array) Console.WriteLine("\t"+ grp); Mono.Unix.Catalog.Init("zero-center","/usr/share/locale/"); //locale string resolution String lang = Environment.GetEnvironmentVariable("LANG"); String [] tmp = lang.Split('.'); locale=tmp[0]; if (no_info==false) { Console.WriteLine("Locale: " + locale); } tmp=locale.Split('_'); if(tmp!=null)short_locale=tmp[0]; if (no_info==false) { Console.WriteLine("short Locale: " + short_locale); } // SqlManager initalization sql=new lliurex.controlcenter.SqlManager(); // StatusManager initialization sm=new lliurex.controlcenter.StatusManager(); if (no_info==false) { Console.WriteLine("[Status Manager] List of scripts running in the system:"); sm.printReadList(); } //properties = new List(); if(!no_gui)Application.Init(); try { System.IO.DirectoryInfo info = new System.IO.DirectoryInfo("/usr/share/zero-center/applications"); applications = new List(); ScanConfigDirectory(info); } catch(System.IO.DirectoryNotFoundException ex) { System.Console.Error.WriteLine(ex.Message); } /* foreach(System.IO.FileInfo f in info.GetFiles()) { System.Console.WriteLine("Reading: " + f.Name); PropertyReader p = new PropertyReader(f.FullName); ConfigApplication app = new ConfigApplication(p); applications.Add(app); } */ if(!no_gui) { pixdefault = Gtk.IconTheme.Default.LoadIcon("applications-system",24,IconLookupFlags.ForceSvg); piximportant = Gtk.IconTheme.Default.LoadIcon("emblem-important",24,IconLookupFlags.ForceSvg); pixoffline = Gtk.IconTheme.Default.LoadIcon("network-offline",24,IconLookupFlags.ForceSvg); pixonline = Gtk.IconTheme.Default.LoadIcon("network-receive",24,IconLookupFlags.ForceSvg); pixrunning = Gtk.IconTheme.Default.LoadIcon("gtk-apply",24,IconLookupFlags.ForceSvg); pixuser = Gtk.IconTheme.Default.LoadIcon("user-info",24,IconLookupFlags.ForceSvg); pixinfo = Gtk.IconTheme.Default.LookupIcon("gtk-dialog-info",24,Gtk.IconLookupFlags.ForceSvg).LoadIcon(); pixerror = Gtk.IconTheme.Default.LookupIcon("gtk-dialog-error",24,Gtk.IconLookupFlags.ForceSvg).LoadIcon(); win = new MainWindow (); win.Show (); if (!sql.sql_ready) { warndialog=new lliurex.controlcenter.WarningDialog(); warndialog.msg="Database is not ready. States won't be saved"; } Application.Run (); } } //recursive directory app scan private void ScanConfigDirectory(System.IO.DirectoryInfo path) { //Console.WriteLine("\n[Domain] {0} [User] {1}\n",System.Environment.UserDomainName, System.Environment.UserName); foreach(System.IO.FileInfo f in path.GetFiles()) { if(f.Extension==".app") { if(no_info==false) { System.Console.WriteLine("Reading: " + f.Name); } PropertyReader p = new PropertyReader(f.FullName); ConfigApplication app = new ConfigApplication(p); // chekcing if the plugin is running or not and // setting ConfigApplication status value should be here // ?? probably :I // StatusManager needed here, then update app status int result=sm.getState(app.name); if (app.service) { if (result!=404) { if (result==2) { app.status=S_RUNNING; //Console.WriteLine(app.status); } if(result==0) { app.status=S_NONE; } if (result==1) { app.status=S_CONFIGURED; } if(result==4) { app.status=S_CONF_NEEDED; } } } applications.Add(app); } } foreach(System.IO.DirectoryInfo d in path.GetDirectories()) { ScanConfigDirectory(d); } } public bool IsDependenciesConfigured(ConfigApplication conf) { bool ret = true; if (conf.pres!=null) { foreach(ConfigApplication c in applications) { foreach(String str in conf.prereq) { if(str==c.name) { uint tmp = c.status; Console.WriteLine("* depends " + str+":"+tmp); if(tmp!=Core.S_CONFIGURED && tmp!=Core.S_RUNNING ) { ret=false; break; } } } } } return ret; } /* * * CLI commands * */ public void ListApps() { Console.WriteLine("Name\t\t\tCategory\tStatus"); Console.WriteLine("------------------------------------------------"); foreach(ConfigApplication cf in applications) { Console.WriteLine(cf.name + "\t\t"+cf.category+"\t\t"+cf.status); } } public void Launch(String name) { bool success=false; foreach(ConfigApplication cf in applications) { if(name==cf.name) { if(cf.service) if(!IsDependenciesConfigured(cf)) { Console.Error.WriteLine("Plugin have some dependencies not configured yet"); break; } Console.WriteLine("# Running "+cf.name+"..."); success=true; //System.Diagnostics.Process.Start(cf.script_path); System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true ; p.StartInfo.RedirectStandardError = true; p.StartInfo.FileName = cf.script_path; Console.WriteLine(cf.script_path); //p.StartInfo.FileName = "ls"; p.Start(); //capturing standard output and error string output=p.StandardOutput.ReadToEnd(); string error=p.StandardError.ReadToEnd(); if (error.Length!=0) { Console.WriteLine("# !! # There has been an error:"); Console.WriteLine(error); } else { // Do what we need to do with the output // // Testing string output Console.WriteLine(output); } p.WaitForExit(); break; } } if(!success) Console.Error.Write("Error launching '{0}': Plugin not found\n",name); } }//class } }