Cosmetic change, plugin work, various reaorganization (Frontend, Control, CilBackend)
[circ.git] / CirC / CilControls / MainControl.cs
blobbd9bcd676d6de4b1fd08c971d533b3996b6bfdfe
2 using System;
3 using System.Threading;
4 using System.Collections.Generic;
5 using Circ.Backend;
6 using Circ.Cil;
7 using Circ.Frontend;
8 using Circ.Controller;
9 using Mono.Addins;
11 namespace Circ
13 public class MainControl: IMainControl
15 Thread presentationThread;
16 bool disposed = false;
18 IList<ConnectionControl> connControls = new List<ConnectionControl>();
20 IFrontend frontend;
21 IBackend backend;
22 ConnectionFactory factory;
24 public MainControl()
26 IrcConnection.ConnectionCreated += ConnectionCreatedHandler;
27 StartTheWholeThingUp();
30 ~MainControl()
32 Dispose(false);
35 public void Dispose()
37 Dispose(true);
38 System.GC.SuppressFinalize(this);
41 protected void Dispose(bool managedRes)
43 if (disposed)
44 return;
46 foreach (ConnectionControl ctrl in connControls)
47 ctrl.Connection.Dispose();
49 disposed = true;
52 void StartTheWholeThingUp()
54 Logger.Debug("MainControl fetching Frontend & Backend");
55 frontend = AcquireFrontend(Options.Frontend);
56 backend = AcquireBackend(Options.Backend);
58 if (frontend == null || backend == null)
59 System.Console.WriteLine("Fuck : {0} {1}", frontend == null, backend == null);
61 GuiFactory.Factory = frontend.ToolkitFactory;
63 Logger.Debug("Frontend & Backend fetched");
64 InitializePresentation();
67 public void Idle()
69 presentationThread.Join();
72 public void ConnectNewServer(ConnectionInfo ci)
75 1) Type typeOfGenericClass = System.Type.GetType("MyGenericClass`1")
76 2) Type typeParameters = typeof(myGenericTypeToCreate)
77 3) Type genericType = typeOfGenericClass.MakeGericType(typeparameters)
78 4) ConstructorInfo ci = genericType.GetConstrucors()[0]
80 if (factory == null)
81 factory = backend.GetConnectionFactory();
82 factory.RequestConnection(ci);
85 /*public void JoinChan(string serverName, string chan)
87 foreach(ConnectionControl ctrl in connControls) {
88 if (ctrl.Connection.Server == serverName)
89 ctrl.Connection.JoinChannel(chan);
91 }*/
93 public void WaitForInit()
95 while (!frontend.IsInitialized)
98 System.Threading.Thread.Sleep(500);
101 public IConnectionControl GetConnectionControl(string serverName)
103 IConnectionControl temp = null;
105 foreach (IConnectionControl ctrl in connControls) {
106 if (ctrl.ServerName == serverName) {
107 temp = ctrl;
108 break;
112 return temp;
115 void InitializePresentation()
117 Logger.Debug("Initializing Presentation layer");
118 presentationThread = new Thread(delegate() { frontend.StartPresentation(this); });
119 presentationThread.IsBackground = false;
120 presentationThread.Name = "PresentationThreadChannel";
121 presentationThread.Start();
124 void ConnectionCreatedHandler(object sender, EventArgs e)
126 this.connControls.Add(new ConnectionControl((IrcConnection)sender, this));
129 IFrontend AcquireFrontend(string id)
131 foreach (TypeExtensionNode node in AddinManager.GetExtensionNodes("/Circ/Frontends")) {
132 if (node.Id == id) {
133 return (IFrontend)node.CreateInstance();
136 return null;
139 IBackend AcquireBackend(string id)
142 foreach (TypeExtensionNode node in AddinManager.GetExtensionNodes("/Circ/Backends")) {
143 if (node.Id == id) {
144 return (IBackend)node.CreateInstance();
147 return null;
150 internal IFrontend Frontend {
151 get {
152 return frontend;
156 internal IBackend Backend {
157 get {
158 return backend;