Upstream tarball 9667
[amule.git] / src / ChatWnd.cpp
blob7eb8d83269d1db345f63a761e6af3c804de7d841
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2008 Merkur ( devs@emule-project.net / http://www.emule-project.net )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <wx/app.h>
28 #include "ChatWnd.h" // Interface declarations
30 #include "amule.h" // Needed for theApp
31 #include "amuleDlg.h" // Needed for CamuleDlg
32 #include "FriendListCtrl.h" // Needed for CFriendListCtrl
33 #include "Friend.h" // Needed for CFriend
34 #include "ChatSelector.h" // Needed for CChatSelector
35 #include "muuli_wdr.h" // Needed for messagePage
36 #include "OtherFunctions.h"
38 BEGIN_EVENT_TABLE(CChatWnd, wxPanel)
39 EVT_TEXT_ENTER(IDC_CMESSAGE, CChatWnd::OnBnClickedCsend)
40 EVT_BUTTON(IDC_CSEND, CChatWnd::OnBnClickedCsend)
41 EVT_BUTTON(IDC_CCLOSE, CChatWnd::OnBnClickedCclose)
42 EVT_MULENOTEBOOK_ALL_PAGES_CLOSED(IDC_CHATSELECTOR, CChatWnd::OnAllPagesClosed)
43 END_EVENT_TABLE()
46 CChatWnd::CChatWnd(wxWindow* pParent)
47 : wxPanel(pParent, -1)
49 wxSizer* content = messagePage(this, true);
50 content->Show(this, true);
52 chatselector = CastChild( IDC_CHATSELECTOR, CChatSelector );
53 friendlistctrl = CastChild( ID_FRIENDLIST, CFriendListCtrl );
56 void CChatWnd::StartSession(CDlgFriend* friend_client, bool setfocus)
59 if ( !friend_client->m_name.IsEmpty() ) {
60 if (setfocus) {
61 theApp->amuledlg->SetActiveDialog(CamuleDlg::DT_CHAT_WND, this);
63 chatselector->StartSession(GUI_ID(friend_client->m_ip, friend_client->m_port), friend_client->m_name, true);
66 // Check to enable the window controls if needed
67 CheckNewButtonsState();
71 void CChatWnd::OnBnClickedCsend(wxCommandEvent& WXUNUSED(evt))
73 wxString message = CastChild(IDC_CMESSAGE, wxTextCtrl)->GetValue();
75 SendMessage(message);
79 void CChatWnd::OnBnClickedCclose(wxCommandEvent& WXUNUSED(evt))
81 chatselector->EndSession();
85 void CChatWnd::OnAllPagesClosed(wxNotebookEvent& WXUNUSED(evt))
87 CastChild(IDC_CMESSAGE, wxTextCtrl)->Clear();
88 // Check to disable the window controls
89 CheckNewButtonsState();
93 CDlgFriend* CChatWnd::FindFriend(const CMD4Hash& userhash, uint32 dwIP, uint16 nPort)
95 return friendlistctrl->FindFriend(userhash, dwIP, nPort);
99 void CChatWnd::AddFriend(CUpDownClient* toadd)
101 friendlistctrl->AddFriend(toadd);
105 void CChatWnd::AddFriend(const CMD4Hash& userhash, const wxString& name, uint32 lastUsedIP, uint32 lastUsedPort)
107 friendlistctrl->AddFriend( userhash, name, lastUsedIP, lastUsedPort);
110 void CChatWnd::RemoveFriend(const CMD4Hash& userhash, uint32 lastUsedIP, uint32 lastUsedPort)
112 friendlistctrl->RemoveFriend(friendlistctrl->FindFriend(userhash, lastUsedIP, lastUsedPort));
115 void CChatWnd::RefreshFriend(CFriend* Friend, bool connected)
117 CDlgFriend* toupdate = friendlistctrl->FindFriend(Friend->GetUserHash(), Friend->GetIP(), Friend->GetPort());
118 if (toupdate) {
119 toupdate->m_name = Friend->GetName();
120 toupdate->islinked = connected;
121 if (toupdate->m_ip == Friend->GetIP() && toupdate->m_port == Friend->GetPort()) {
122 chatselector->RefreshFriend(GUI_ID(toupdate->m_ip, toupdate->m_port), toupdate->m_name);
123 } else {
124 // Friend changed IP - drop Chat session
125 chatselector->EndSession(GUI_ID(toupdate->m_ip, toupdate->m_port));
126 // and update IP
127 toupdate->m_ip = Friend->GetIP();
128 toupdate->m_port = Friend->GetPort();
130 friendlistctrl->RefreshFriend(toupdate);
134 void CChatWnd::ProcessMessage(uint64 sender, const wxString& message)
136 if ( !theApp->amuledlg->IsDialogVisible(CamuleDlg::DT_CHAT_WND) ) {
137 theApp->amuledlg->SetMessageBlink(true);
139 if (chatselector->ProcessMessage(sender, message)) {
140 // Check to enable the window controls if needed
141 CheckNewButtonsState();
145 void CChatWnd::ConnectionResult(bool success, const wxString& message, uint64 id)
147 chatselector->ConnectionResult(success, message, id);
150 void CChatWnd::SendMessage(const wxString& message, const wxString& client_name, uint64 to_id)
153 if (chatselector->SendMessage( message, client_name, to_id )) {
154 CastChild(IDC_CMESSAGE, wxTextCtrl)->Clear();
157 // Check to enable the window controls if needed
158 CheckNewButtonsState();
159 CastChild(IDC_CMESSAGE, wxTextCtrl)->SetFocus();
163 void CChatWnd::CheckNewButtonsState()
165 switch (chatselector->GetPageCount()) {
166 case 0:
167 GetParent()->FindWindow(IDC_CSEND)->Enable(false);
168 GetParent()->FindWindow(IDC_CCLOSE)->Enable(false);
169 GetParent()->FindWindow(IDC_CMESSAGE)->Enable(false);
170 break;
171 case 1:
172 GetParent()->FindWindow(IDC_CSEND)->Enable(true);
173 GetParent()->FindWindow(IDC_CCLOSE)->Enable(true);
174 GetParent()->FindWindow(IDC_CMESSAGE)->Enable(true);
175 break;
176 default:
177 // Nothing to be done here. Keep current state, which should be enabled.
178 wxASSERT(GetParent()->FindWindow(IDC_CSEND)->IsEnabled());
179 wxASSERT(GetParent()->FindWindow(IDC_CCLOSE)->IsEnabled());
180 wxASSERT(GetParent()->FindWindow(IDC_CMESSAGE)->IsEnabled());
181 break;
186 bool CChatWnd::IsIdValid(uint64 id)
188 return chatselector->GetTabByClientID(id) >= 0;
192 void CChatWnd::ShowCaptchaResult(uint64 id, bool ok)
194 chatselector->ShowCaptchaResult(id, ok);
197 void CChatWnd::EndSession(uint64 id)
199 chatselector->EndSession(id);
202 // File_checked_for_headers