00001 #include "Connessione.h"
00002 #include "ChannelGui.h"
00003 #include "QueryGui.h"
00004 #include "StatusGui.h"
00005 #include "ClientGui.h"
00006 #include "wxStringIRC.h"
00007
00008
00009 BEGIN_EVENT_TABLE(Connessione, wxEvtHandler)
00010 EVT_SOCKET(SOCKET_ID, Connessione::OnSocketEvent)
00011 END_EVENT_TABLE()
00012
00013
00014 Connessione::Connessione(wxString addr, int port, int timeout, bool rfcCompliant)
00015 :serverAddr(addr), serverPort(port), serverTimeout(timeout), rfc_compliant(rfcCompliant){
00016 serverSocket = new wxSocketClient();
00017 serverSocket->SetEventHandler(*this, SOCKET_ID);
00018 serverSocket->SetNotify(wxSOCKET_INPUT_FLAG);
00019
00020 CreateStatus(_("~Status"));
00021 status = static_cast<StatusGui*>(GetTerminal(_("~Status")));
00022 wxString title = _("Status");
00023 title.Append(_(": ")).Append(addr);
00024 status->SetTitle(title);
00025 parserIn = new ParserIn(this);
00026 parserOut = new ParserOut(this);
00027 }
00028
00029 Connessione::~Connessione(){
00030 Disconnect();
00031 serverSocket->Notify(FALSE);
00032 serverSocket->Discard();
00033 serverSocket->Destroy();
00034 wxTerminalsMap::iterator it;
00035 for(it = terminalsMap.begin(); it != terminalsMap.end(); ++it)
00036 it->second->Destroy();
00037 delete parserIn;
00038 delete parserOut;
00039 }
00040
00041 bool Connessione::Connect(){
00042 wxIPV4address ipAddr;
00043 ipAddr.Hostname(serverAddr);
00044 ipAddr.Service(serverPort);
00045 serverSocket->Notify(TRUE);
00046 serverSocket->SetTimeout(serverTimeout);
00047 serverSocket->Connect(ipAddr, TRUE);
00048 if (!serverSocket->IsConnected())
00049 {
00050 serverSocket->Close();
00051 wxMessageBox(_("Impossibile connettersi all'host specificato!"), _("Errore!"));
00052 return false;
00053 }
00054 else{
00055 status->receiveMsg(_("Connessione effettuata! Attendere prego...\n"));
00056 status->receiveMsg(_("La connessione ad alcuni server può richiedere piu' di un minuto!\n"));
00057 return true;
00058 }
00059 }
00060
00061 bool Connessione::Disconnect(){
00062 serverSocket->Close();
00063 if(serverSocket->IsDisconnected())
00064 return true;
00065 else
00066 return false;
00067 }
00068
00069 bool Connessione::sendCommand(wxString command){
00070 wxChar* buf;
00071 size_t len;
00072 len = (wxStrlen(command) + 1) * sizeof(wxChar);
00073 buf = new wxChar[wxStrlen(command) + 1];
00074 wxStrcpy(buf, command);
00075 serverSocket->Write(buf, len);
00076 serverSocket->Write(_("\n"), 1);
00077
00078 delete buf;
00079
00080 if(!serverSocket->Error()){
00081 return true;
00082 }
00083 else {
00084 return false;
00085 }
00086 }
00087
00088 void Connessione::readSocket(){
00089 wxChar c;
00090 serverSocket->Read(&c, sizeof(wxChar));
00091 cmdBuffer << c;
00092 if(c == 0x0A){
00093 wxString buf = cmdBuffer;
00094 cmdBuffer.clear();
00095 buf = buf.Mid(0, (buf.Len() - 2));
00096 parserIn->getMessage(buf);
00097 }
00098 }
00099
00100 void Connessione::OnSocketEvent(wxSocketEvent& event)
00101 {
00102 switch(event.GetSocketEvent())
00103 {
00104 case wxSOCKET_INPUT : readSocket(); break;
00105 default : break;
00106 }
00107 }
00108
00109 void Connessione::CreateChannel(wxString nome)
00110 {
00111 wxString title = nome;
00112 if(rfc_compliant){
00113 wxStringIRC nome_lower = nome;
00114 nome = nome_lower.Lower();
00115 }
00116 else
00117 nome.MakeLower();
00118 if(terminalsMap.count(nome)){
00119 wxMessageBox(_("Gia' esiste un Terminale con questo nome!"), _("Errore!"));
00120 exit(1);
00121 }
00122 ClientGui* client = ClientGui::Instance(NULL);
00123
00124 terminalsMap[nome] = new ChannelGui(this, client);
00125 terminalsMap[nome]->SetName(nome);
00126 terminalsMap[nome]->SetTitle(title);
00127 terminalsMap[nome]->Show(TRUE);
00128 }
00129
00130 void Connessione::CreateQuery(wxString nome)
00131 {
00132 wxString title = nome;
00133 if(rfc_compliant){
00134 wxStringIRC nome_lower = nome;
00135 nome = nome_lower.Lower();
00136 }
00137 else
00138 nome.MakeLower();
00139 if(terminalsMap.count(nome)){
00140 wxMessageBox(_("Gia' esiste un Terminale con questo nome!"), _("Errore!"));
00141 exit(1);
00142 }
00143 ClientGui* client = ClientGui::Instance(NULL);
00144 terminalsMap[nome] = new QueryGui(this, client);
00145 terminalsMap[nome]->SetName(nome);
00146 terminalsMap[nome]->SetTitle(title);
00147 terminalsMap[nome]->Show(TRUE);
00148 }
00149
00150 void Connessione::CreateStatus(wxString nome)
00151 {
00152 wxString title = nome;
00153 if(rfc_compliant){
00154 wxStringIRC nome_lower = nome;
00155 nome = nome_lower.Lower();
00156 }
00157 else
00158 nome.MakeLower();
00159 if(terminalsMap.count(nome)){
00160 wxMessageBox(_("Gia' esiste un Terminale con questo nome!"), _("Errore!"));
00161 exit(1);
00162 }
00163 ClientGui* client = ClientGui::Instance(NULL);
00164 terminalsMap[nome] = new StatusGui(this, client);
00165 terminalsMap[nome]->SetName(nome);
00166 terminalsMap[nome]->SetTitle(title);
00167 terminalsMap[nome]->Show(TRUE);
00168 }
00169
00170 bool Connessione::TerminalExists(wxString nome)
00171 {
00172 if(rfc_compliant){
00173 wxStringIRC nome_lower = nome;
00174 nome = nome_lower.Lower();
00175 }
00176 else
00177 nome.MakeLower();
00178 return terminalsMap.count(nome);
00179
00180 }
00181
00182 void Connessione::DeleteTerminal(wxString nome)
00183 {
00184 if(rfc_compliant){
00185 wxStringIRC nome_lower = nome;
00186 nome = nome_lower.Lower();
00187 }
00188 else
00189 nome.MakeLower();
00190 if(!terminalsMap.count(nome)){
00191 wxMessageBox(_(nome), _("Errore!"));
00192 exit(1);
00193 }
00194 terminalsMap[nome]->Destroy();
00195 terminalsMap.erase(nome);
00196
00197 #ifdef TESTING
00198 if(terminalsMap.empty())
00199 wxMessageBox(_("Non ci sono più Terminali!"), _("Informazione!"));
00200 #endif
00201 }
00202
00203 TerminalGui* Connessione::GetTerminal(wxString nome)
00204 {
00205 if(rfc_compliant){
00206 wxStringIRC nome_lower = nome;
00207 nome = nome_lower.Lower();
00208 }
00209 else
00210 nome.MakeLower();
00211 if(terminalsMap[nome] == NULL){
00212 wxString msg = _("Il Terminale a cui cerchi di accedere non esiste! Nome: ");
00213 msg.Append(nome);
00214 wxMessageBox(msg, _("Errore!"));
00215 exit(1);
00216 }
00217
00218 return terminalsMap[nome];
00219 }
00220
00221 ParserOut* Connessione::getParserOut(){
00222 return parserOut;
00223 }
00224
00225 wxString Connessione::GetMyNick()
00226 {
00227 return parserIn->mynick;
00228
00229 }
00230
00231 StatusGui* Connessione::GetStatus()
00232 {
00233 return status;
00234 }
00235
00236 void Connessione::UserQuit(wxString name, wxString msg)
00237 {
00238 wxTerminalsMap::iterator it;
00239
00240 for(it = terminalsMap.begin(); it != terminalsMap.end(); ++it){
00241 if(it->first.StartsWith(_("#")) || it->first.StartsWith(_("+")) ||
00242 it->first.StartsWith(_("&")) || it->first.StartsWith(_("!"))){
00243 ChannelGui* canale = static_cast<ChannelGui*>(GetTerminal(it->first));
00244
00245 canale->receiveMsg(name+" è uscito dal server: "+msg, STYLE_BLUE);
00246
00247 canale->removeUser(name);
00248 }
00249 }
00250 }
00251
00252 void Connessione::UserChNick(wxString oldname, wxString newname)
00253 {
00254 wxTerminalsMap::iterator it;
00255
00256 for(it = terminalsMap.begin(); it != terminalsMap.end(); ++it){
00257 if(it->first.StartsWith(_("#")) || it->first.StartsWith(_("+")) ||
00258 it->first.StartsWith(_("&")) || it->first.StartsWith(_("!"))){
00259 ChannelGui* canale = static_cast<ChannelGui*>(GetTerminal(it->first));
00260
00261 if(canale->changeNick(oldname, newname))
00262
00263 canale->receiveMsg(oldname+" ha cambiato nick in "+newname+"\n", STYLE_BLUE);
00264 }
00265 }
00266 }