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.
11 using System
.Threading
;
12 using System
.Collections
.Generic
;
16 using Circ
.Controller
;
21 public sealed class MainControl
: IMainControl
, IDisposable
23 Thread presentationThread
;
24 bool disposed
= false;
26 IList
<ConnectionControl
> connControls
= new List
<ConnectionControl
>();
30 ConnectionFactory factory
;
31 CommandProcesser cmdProc
= CommandProcesser
.Instance
;
35 IrcConnection
.ConnectionCreated
+= ConnectionCreatedHandler
;
36 InitializeDefaultCommand();
48 System
.GC
.SuppressFinalize(this);
51 protected void Dispose(bool managedRes
)
56 foreach (ConnectionControl ctrl
in connControls
)
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\"");
85 ConnectionFactory
.Factory
.RequestConnection(Circ
.Backend
.ConnectionInfo
.GetDefault(args
[0], args
[1]));
87 cmdProc
.RegisterCommand("j", delegate(object sender
, string[] args
) {
88 if (args
.Length
!= 1) {
89 frontend
.ShowErrorMessage("Wrong parameters for the join command");
92 if (args
[0][0] != Rfc
.ChannelStartChar
) {
93 frontend
.ShowErrorMessage("Malformed chan");
97 IrcConnection temp
= GetConnectionFromObject(sender
);
99 Logger
.Debug("Unable to get the IrcConnection");
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");
109 IrcConnection temp
= GetConnectionFromObject(sender
);
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
);
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
) {
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) {
196 if ((chanTemp
= input
as IrcChannel
) == null)
198 temp
= chanTemp
.ParentConnection
;
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")) {
212 return (IFrontend
)node
.CreateInstance();
218 IBackend
AcquireBackend(string id
)
221 foreach (TypeExtensionNode node
in AddinManager
.GetExtensionNodes("/Circ/Backends")) {
223 return (IBackend
)node
.CreateInstance();
229 public IFrontend Frontend
{
235 public IBackend Backend
{