KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > zirc > base > IRCconnexion


1 package zirc.base ;
2
3 import java.io.* ;
4 import java.net.* ;
5 import java.util.* ;
6
7 import java.awt.* ;
8 import javax.swing.* ;
9
10 import zirc.gui.* ;
11 import zirc.gui.smiliesWindow.* ;
12 import zirc.threads.* ;
13
14 //zIrc, irc client.
15
// Copyright (C) 2004 CoolBytes(Stephane claret, Andre Aymon, Alban Zumofen) coolbytes@hotmail.com
16
//
17
// This program is free software; you can redistribute it and/or
18
// modify it under the terms of the GNU General Public License
19
// as published by the Free Software Foundation; either version 2
20
// of the License, or (at your option) any later version.
21
//
22
// This program is distributed in the hope that it will be useful,
23
// but WITHOUT ANY WARRANTY; without even the implied warranty of
24
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
// GNU General Public License for more details.
26

27 /**
28  * <p>Title: IRCconnexion</p>
29  * <p>Description: classe qui encapsule tte les donnees d'une connexion</p>
30  * <p>Copyright: Copyright (c) 2004</p>
31  * <p>Company: CoolBytes(Stephane claret, Andre Aymon, Alban Zumofen) coolbytes@hotmail.com</p>
32  * @version 1.0
33  */

34
35 public class IRCconnexion
36 {
37   IRCconnexion ircConnexion = this ;
38
39   //la langue
40
private Locale language;
41   //la collection des smilies
42
SmiliesCollection smilies ;
43
44   //array statique qui contient tte les connexions serveurs
45
static ArrayList serverArray = new ArrayList() ;
46
47   //la jdialog qui affiche la liste des chan
48
ListDialog chanList ;
49
50   //objets GUI (les fen�tres doivent avoir des getters pour les txtarea)
51
private AbstractChatFrame FrameWithFocus = null ;
52   private StatusFrame statusFrm ; //fenetre de status/service
53
private ArrayList chatFramesArray = new ArrayList() ; //fenetres de discussion.
54
private ArrayList privateFrameArray = new ArrayList() ; //fenetres de dials priv�s
55

56   private MainFrame mainFrm ; //fenetre m�re de l'applic
57

58   //les threads
59
private ConnectThread ct ;
60
61   //un lanceur de tache
62
private ThreadProcessor tLanceur = new ThreadProcessor() ;
63
64   //variables du socket principal
65
private BufferedReader mainSockIn ;
66   private PrintWriter mainSockOut ;
67   private Socket mainSock ;
68
69   //informations serveurs.
70
private String JavaDoc mainSockPort ;
71   private String JavaDoc mainSockServer ;
72
73   //infos utilisateur
74
private String JavaDoc user_operatingSys ;
75   private String JavaDoc user_realName ;
76   private String JavaDoc user_hote ;
77   private String JavaDoc user_nickName ;
78   private String JavaDoc user_serverName ;
79   private String JavaDoc user_email ;
80
81   //information IDENT
82
private String JavaDoc user_nomUser ; //nom de l'user
83
private static int identPort = 113 ;
84
85   //les message part et quit
86
String JavaDoc partMSG ;
87   String JavaDoc quitMSG ;
88
89   //FONT officielle IRC
90
public static Font FIXEDSYS ;
91
92   public IRCconnexion(String JavaDoc name, String JavaDoc email, String JavaDoc nick, String JavaDoc server, String JavaDoc port, MainFrame _fenetre, SmiliesCollection _smilies,
93                       String JavaDoc _quitMSG, String JavaDoc _partMSG,Locale _language)
94   {
95     //constructeur simple
96
mainSockPort = port ;
97     mainSockServer = server ;
98     mainFrm = _fenetre ;
99     user_email = email ;
100     user_nickName = nick ;
101     user_nomUser = getLocalHostName() ;
102     user_operatingSys = System.getProperty("os.version") ;
103     user_realName = name ;
104     user_hote = getLocalHostName() ;
105     user_serverName = server ;
106     smilies = _smilies ;
107     quitMSG = _quitMSG ;
108     partMSG = _partMSG ;
109     language=_language;
110
111     //instancier la police
112
chargeFixedSys() ;
113
114     //on cree le dialog chanlist
115
chanList = new ListDialog(mainFrm, this,language ) ;
116
117     //on ajoute la connexion serveur a l'array
118
serverArray.add(this) ;
119   }
120
121   private String JavaDoc getLocalHostName()
122   {
123     String JavaDoc lName = null ;
124     //trouver le localhost, a faire avec firetrucchose
125
try
126     {
127       lName = InetAddress.getLocalHost().getHostName() ;
128     }
129     catch (UnknownHostException ex)
130     {
131       ex.printStackTrace() ;
132     }
133     return lName ;
134   }
135
136   public void connect()
137   {
138     //
139
//connecter le socket en lan�ant le thread de connexion
140
//
141

142     statusFrm = createStatusFrame() ; //ici creer la fenetre de status de la connexion
143

144     //thread de connexion
145
ct = new ConnectThread(this) ;
146     ct.start() ;
147   }
148
149   //deconnexion
150
public void disconnect()
151   {
152     ct.stopConnexion() ;
153
154     //on vide l'array des chan
155
while (chatFramesArray.size() > 0)
156     {
157       ((ChatFrame)(chatFramesArray.get(0))).destroy() ;
158     }
159
160     //on vide celui des priv�s
161
while (privateFrameArray.size() > 0)
162     {
163       ((PrivateFrame)(privateFrameArray.get(0))).destroy() ;
164     }
165
166   }
167
168   private StatusFrame createStatusFrame()
169   {
170     //
171
//cr�e une fenetre de status pour la connexion et ajoute a la mainFrm
172
//
173
StatusFrame frm = new StatusFrame(this) ;
174     frm.setTitle("Status " + mainSockServer + " ( " + mainSockPort + " )") ;
175     mainFrm.getMdiPanel().add(frm) ;
176     frm.setVisible(true) ;
177
178     return frm ;
179   }
180
181   public ChatFrame createChatFrame(String JavaDoc chanName)
182   {
183     //
184
//cr�e une fenetre de discussion (chan)
185
//
186
ChatFrame frm = new ChatFrame(this, chanName) ;
187     frm.setTitle(chanName) ;
188     mainFrm.getMdiPanel().add(frm) ;
189     frm.setVisible(true) ;
190
191     //ajouter a l arraylist des chatframes
192
chatFramesArray.add(frm) ;
193     return frm ;
194   }
195
196   public PrivateFrame createPrivateFrame(String JavaDoc user)
197   {
198     //
199
//cr�e une fenetre de discussion priv�e avec user
200
//
201
PrivateFrame frm = new PrivateFrame(this, user) ;
202     frm.setTitle(user) ;
203     mainFrm.getMdiPanel().add(frm) ;
204     frm.setVisible(true) ;
205
206     //ajouter a l arraylist des chatframes
207
privateFrameArray.add(frm) ;
208     return frm ;
209   }
210
211   public void sendCommand(String JavaDoc command)
212   {
213     //
214
//Lance une commande
215
//
216
try
217     {
218       mainSockOut.println(command) ;
219       mainSockOut.flush() ;
220     }
221     catch (NullPointerException JavaDoc ex)
222     {
223
224     }
225   }
226
227   public void addChanToList(String JavaDoc line)
228   {
229     String JavaDoc chan = "" ;
230     String JavaDoc users = "" ;
231     //on parse directement pour gagner un peu en perf
232
StringTokenizer str = new StringTokenizer(line, " ") ;
233     str.nextElement() ;
234     try
235     {
236       str.nextElement() ;
237       str.nextElement() ;
238       chan = str.nextElement().toString() ;
239       users = str.nextElement().toString() ;
240     }
241     catch (NoSuchElementException e)
242     {}
243
244     //si le nom du chan est plus long que 3 char on continue
245
if (chan.length() > 3)
246     { //ajout du chan et du nbre de users
247
chanList.addChan(chan + " " + users + " users") ;
248     }
249
250     if (line.equals("END"))
251     {
252       chanList.addChan("END") ;
253       chanList.setLocation((int)mainFrm.getBounds().getX()+40,(int)mainFrm.getBounds().getY()+40);
254       chanList.show() ;
255     }
256   }
257
258   public void ajouteTache(PseudoThread _tr)
259   {
260     tLanceur.addThread(_tr) ;
261   }
262
263   public static void chargeFixedSys()
264   {
265     //
266
//charge la police IRC si ca n'a pas d�j� �t� fait
267
//
268

269     if (FIXEDSYS == null)
270     {
271       File fp = new File("fichiers" + File.separatorChar + "fixedSys.ttf") ;
272       System.out.println(fp.getAbsolutePath()) ;
273
274       try
275       {
276         //instancie la font IRC
277
FIXEDSYS = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(fp)) ;
278       }
279       catch (Exception JavaDoc e)
280       {
281         System.out.println("Exception instanciation fonte irc") ; ;
282         //si la fonte irc est pas dispo, on en prend une autre
283
FIXEDSYS = Font.getFont("verdana") ;
284       }
285
286     }
287
288   }
289
290   //le parse de la liste des ports
291
public int[] getServerPortList()
292   {
293     ArrayList portArray = new ArrayList() ;
294     StringTokenizer str = new StringTokenizer(GetMainSockPort() + "", ",") ;
295     while (str.hasMoreElements()) //on trie les , en premier
296
{
297       portArray.add(str.nextElement()) ;
298     }
299
300     for (int i = 0 ; i < portArray.size() ; i++) //ensuite on regarde ceux ont le - (port range)
301
{
302       if (portArray.get(i).toString().length() > 4)
303       {
304         StringTokenizer str2 = new StringTokenizer(portArray.get(i).toString(), "-") ;
305         int p1 = Integer.parseInt(str2.nextElement().toString()) ;
306         int p2 = Integer.parseInt(str2.nextElement().toString()) ;
307         int pmin = Math.min(p1, p2) ;
308         int pmax = Math.max(p1, p2) ;
309         for (int j = pmin ; j <= pmax ; j++)
310         {
311           portArray.add(j + "") ;
312         }
313       }
314     }
315
316     //enlever les objet type range de ports ou les ports entrez incorectement >4 chars
317
for (int i = 0 ; i < portArray.size() ; i++)
318     {
319       if (portArray.get(i).toString().length() > 4)
320       {
321         portArray.remove(i) ;
322       }
323     }
324
325     int[] portList = new int[portArray.size()] ;
326     //test c tt bon ca roule
327
for (int i = 0 ; i < portArray.size() ; i++)
328     {
329       portList[i] = Integer.parseInt(portArray.get(i).toString()) ;
330     }
331
332     //on trie le tableau
333
Arrays.sort(portList) ;
334
335     return portList ;
336   }
337
338 //
339
//Getters / Setters
340
//
341

342   public StatusFrame GetStatusFrm()
343   {
344     return statusFrm ;
345   }
346
347 //public StatusFrame GetChatFrm()
348
//{
349
// return StatusFrame ;
350
// }
351

352   public MainFrame GetMainFrm()
353   {
354     return mainFrm ;
355   }
356
357   public JDesktopPane getMainDesktopPane()
358   {
359     return mainFrm.getMdiPanel() ;
360   }
361
362   public BufferedReader getMainSockIn()
363   {
364     return mainSockIn ;
365   }
366
367   public void setMainSockIn(BufferedReader _in)
368   {
369     this.mainSockIn = _in ;
370   }
371
372   public void setMainSockPort(String JavaDoc port)
373   {
374     this.mainSockPort = port ;
375   }
376
377   public PrintWriter GetMainSockOut()
378   {
379     return mainSockOut ;
380   }
381
382   public void setMainSockOut(PrintWriter _pw)
383   {
384     this.mainSockOut = _pw ;
385   }
386
387   public Socket getMainSock()
388   {
389     return mainSock ;
390   }
391
392   public void setMainSock(Socket _s)
393   {
394     mainSock = _s ;
395   }
396
397   public void set_userRealName(String JavaDoc name)
398   {
399     this.user_realName = name ;
400   }
401
402   public void set_userEmail(String JavaDoc email)
403   {
404     this.user_email = email ;
405   }
406
407   public void setUser_NickName(String JavaDoc nick)
408   {
409     this.user_nickName = nick ;
410   }
411
412   public void setChatFrameWithFocus(JInternalFrame frm)
413   {
414     if (frm instanceof ChatFrame)
415     {
416       FrameWithFocus = (ChatFrame)frm ;
417     }
418     else
419     {
420         if(frm instanceof PrivateFrame)
421         {
422           FrameWithFocus = (PrivateFrame)frm ;
423         }
424         else
425         {
426           FrameWithFocus = null ;
427         }
428     }
429   }
430
431   public String JavaDoc GetMainSockPort()
432   {
433     return mainSockPort ;
434   }
435
436   public String JavaDoc GetMainSockServer()
437   {
438     return mainSockServer ;
439   }
440
441 //infos utilisateur
442
public String JavaDoc GetUser_operatingSys()
443   {
444     return user_operatingSys ;
445   }
446
447   public String JavaDoc getUser_realName()
448   {
449     return user_realName ;
450   }
451
452   public String JavaDoc GetUser_hote()
453   {
454     return user_hote ;
455   }
456
457   public String JavaDoc GetUser_nickName()
458   {
459     return user_nickName ;
460   }
461
462   public String JavaDoc GetUser_serverName()
463   {
464     return user_serverName ;
465   }
466
467   public String JavaDoc getUser_nomUser()
468   {
469     return user_nomUser ;
470   }
471
472   public static int getIdentPort()
473   {
474     return identPort ;
475   }
476
477   public ArrayList getOpenChanArray()
478   {
479     return chatFramesArray ;
480   }
481
482   public static ArrayList getServerArray()
483   {
484     return serverArray ;
485   }
486
487   public SmiliesCollection getSmiliesCollection()
488   {
489     return smilies ;
490   }
491
492   public void removeChan(ChatFrame frm)
493   {
494     chatFramesArray.remove(frm) ;
495   }
496
497   public ChatFrame GetFenetreDuChan(String JavaDoc _chan)
498   {
499
500     for (int i = 0 ; i < chatFramesArray.size() ; i++)
501     {
502       if (((ChatFrame)(chatFramesArray.get(i))).getChan().equalsIgnoreCase(_chan))
503       {
504         return (ChatFrame)(chatFramesArray.get(i)) ;
505       }
506     }
507
508     return null ;
509   }
510
511   public void removePrivate(PrivateFrame frm)
512   {
513     privateFrameArray.remove(frm) ;
514   }
515
516   public PrivateFrame GetFenetreDuPrive(String JavaDoc _user)
517   {
518
519     for (int i = 0 ; i < privateFrameArray.size() ; i++)
520     {
521       if (((PrivateFrame)(privateFrameArray.get(i))).getCorrespondant().equalsIgnoreCase(_user))
522       {
523         return (PrivateFrame)(privateFrameArray.get(i)) ;
524       }
525     }
526
527     return null ;
528   }
529
530   public ArrayList getAllChatFrames()
531   {
532     return chatFramesArray ;
533   }
534
535   public AbstractChatFrame getChatFrameWithFocus()
536   {
537     return FrameWithFocus ;
538   }
539
540   public ArrayList getAllPrivateFrames()
541   {
542     return privateFrameArray ;
543   }
544
545   public String JavaDoc getQuitMSG()
546   {
547     return quitMSG ;
548   }
549
550   public String JavaDoc getPartMSG()
551   {
552     return partMSG ;
553   }
554
555   public void setLanguage(Locale lang)
556  {
557    chanList.setLanguage(lang);
558  }
559 }
560
Popular Tags