/!\ Warning: this commit doesn't compile /!
[circ.git] / CirC / CilControls / MainControl.cs
blob043ee437037d72638dccc2d2050ae4cb8bb04946
1 #region License
2 /* Circ : Main program
3 * Copyright (C) 2007 LAVAL Jérémie
5 * This file is licensed under the terms of the LGPL.
7 * For the complete licence see the file COPYING.
8 */
9 #endregion
10 using System;
11 using System.Threading;
12 using System.Collections.Generic;
13 using Circ.Backend;
14 using Circ.Cil;
15 using Circ.Frontend;
16 using Circ.Controller;
17 using Mono.Addins;
19 namespace Circ
21 public sealed class MainControl: IMainControl, IDisposable
23 Thread presentationThread;
24 bool disposed = false;
26 IList<ConnectionControl> connControls = new List<ConnectionControl>();
28 IFrontend frontend;
29 IBackend backend;
30 ConnectionFactory factory;
31 CommandProcesser cmdProc = CommandProcesser.Instance;
33 public MainControl()
35 IrcConnection.ConnectionCreated += ConnectionCreatedHandler;
36 InitializeDefaultCommand();
37 StartTheWholeThing();
40 ~MainControl()
42 Dispose(false);
45 public void Dispose()
47 Dispose(true);
48 System.GC.SuppressFinalize(this);
51 protected void Dispose(bool managedRes)
53 if (disposed)
54 return;
56 foreach (ConnectionControl ctrl in connControls)
57 ctrl.Dispose();
59 disposed = true;
62 void StartTheWholeThing()
64 Logger.Debug("MainControl fetching Frontend & Backend");
65 frontend = AcquireFrontend(Options.GetStringConfig("frontend"));
66 backend = AcquireBackend(Options.GetStringConfig("backend"));
68 if (frontend == null || backend == null)
69 Logger.Error(string.Format("Backend not loaded {0} ; Frontend not loaded : {1}", backend == null, frontend == null), null);
71 GuiFactory.Factory = frontend.ToolkitFactory;
72 ConnectionFactory.Factory = backend.GetConnectionFactory();
74 Logger.Debug("Frontend & Backend fetched");
75 InitializePresentation();
78 void InitializeDefaultCommand()
80 cmdProc.RegisterCommand("c", delegate(object sender, string[] args) {
81 if (args.Length != 2) {
82 frontend.ShowErrorMessage("The connection command you typed is ill-formed. The correct form is : \"/c irc.yourserver.org yourNick\"");
83 return;
85 ConnectionFactory.Factory.RequestConnection(Circ.Backend.ConnectionInfo.GetDefault(args[0], args[1]));
86 });
87 cmdProc.RegisterCommand("j", delegate(object sender, string[] args) {
88 if (args.Length != 1) {
89 frontend.ShowErrorMessage("Wrong parameters for the join command");
90 return;
92 if (args[0][0] != Rfc.ChannelStartChar) {
93 frontend.ShowErrorMessage("Malformed chan");
94 return;
97 IrcConnection temp = GetConnectionFromObject(sender);
98 if (temp == null) {
99 Logger.Debug("Unable to get the IrcConnection");
100 return;
102 temp.JoinChannel(args[0]);
104 cmdProc.RegisterCommand("n", delegate (object sender, string[] args) {
105 if (args.Length != 1) {
106 frontend.ShowErrorMessage("Wrong number of parameter for nick command");
107 return;
109 IrcConnection temp = GetConnectionFromObject(sender);
110 if (temp == null)
111 return;
112 temp.Nick = args[0];
116 public void Idle()
118 presentationThread.Join();
121 public IrcConnection ConnectNewServer(ConnectionInfo ci)
124 //1) Type typeOfGenericClass = System.Type.GetType("MyGenericClass`1")
125 //2) Type typeParameters = typeof(myGenericTypeToCreate)
126 //3) Type genericType = typeOfGenericClass.MakeGericType(typeparameters)
127 //4) ConstructorInfo ci = genericType.GetConstrucors()[0]
129 return factory.RequestConnection(ci);
132 public void CommandEntered(object ctrl, string cmd)
134 string[] args = null;
135 string identifier = null;
136 // If there are no space, there are no args
137 if (cmd.IndexOf(' ') != -1) {
138 string[] temp = cmd.Split(' ');
139 identifier = temp[0];
140 args = new string[temp.Length - 1];
141 Array.Copy(temp, 1, args, 0, args.Length);
142 } else {
143 identifier = cmd;
145 cmdProc.ProcessCommand(ctrl, identifier, args);
148 /*public void JoinChan(string serverName, string chan)
150 foreach(ConnectionControl ctrl in connControls) {
151 if (ctrl.Connection.Server == serverName)
152 ctrl.Connection.JoinChannel(chan);
156 public void WaitForInit()
158 while (!frontend.IsInitialized)
161 System.Threading.Thread.Sleep(300);
164 public IConnectionControl GetConnectionControl(string serverName)
166 IConnectionControl temp = null;
168 foreach (IConnectionControl ctrl in connControls) {
169 if (ctrl.Backend.Info.Server == serverName) {
170 temp = ctrl;
171 break;
175 return temp;
178 void InitializePresentation()
180 Logger.Debug("Initializing Presentation layer");
182 presentationThread = new Thread(delegate() {
183 frontend.StartPresentation(this);
186 presentationThread.IsBackground = false;
187 presentationThread.Name = "PresentationThreadChannel";
188 presentationThread.Start();
191 IrcConnection GetConnectionFromObject(object input)
193 IrcConnection temp = null;
194 if ((temp = input as IrcConnection) == null) {
195 IrcChannel chanTemp;
196 if ((chanTemp = input as IrcChannel) == null)
197 return null;
198 temp = chanTemp.ParentConnection;
200 return temp;
203 void ConnectionCreatedHandler(object sender, EventArgs e)
205 this.connControls.Add(new ConnectionControl((IrcConnection)sender, this));
208 IFrontend AcquireFrontend(string id)
210 foreach (TypeExtensionNode node in AddinManager.GetExtensionNodes("/Circ/Frontends")) {
211 if (node.Id == id) {
212 return (IFrontend)node.CreateInstance();
215 return null;
218 IBackend AcquireBackend(string id)
221 foreach (TypeExtensionNode node in AddinManager.GetExtensionNodes("/Circ/Backends")) {
222 if (node.Id == id) {
223 return (IBackend)node.CreateInstance();
226 return null;
229 public IFrontend Frontend {
230 get {
231 return frontend;
235 public IBackend Backend {
236 get {
237 return backend;