2 // This file is part of the aMule Project.
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 )
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
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.
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 <common/MenuIDs.h> // IDs for the chat-popup menu
30 #include "ChatWnd.h" // Interface declarations
32 #include "amule.h" // Needed for theApp
33 #include "amuleDlg.h" // Needed for CamuleDlg
34 #include "ClientList.h" // Needed for CClientList
35 #include "updownclient.h" // Needed for CUpDownClient
36 #include "FriendListCtrl.h" // Needed for CFriendListCtrl
37 #include "Friend.h" // Needed for CFriend
38 #include "ChatSelector.h" // Needed for CChatSelector
39 #include "muuli_wdr.h" // Needed for messagePage
40 #include "OtherFunctions.h"
42 BEGIN_EVENT_TABLE(CChatWnd
, wxPanel
)
43 EVT_RIGHT_DOWN(CChatWnd::OnNMRclickChatTab
)
45 EVT_MENU(MP_CLOSE_TAB
, CChatWnd::OnPopupClose
)
46 EVT_MENU(MP_CLOSE_ALL_TABS
, CChatWnd::OnPopupCloseAll
)
47 EVT_MENU(MP_CLOSE_OTHER_TABS
, CChatWnd::OnPopupCloseOthers
)
48 EVT_MENU(MP_ADDFRIEND
, CChatWnd::OnAddFriend
)
50 EVT_TEXT_ENTER(IDC_CMESSAGE
, CChatWnd::OnBnClickedCsend
)
51 EVT_BUTTON(IDC_CSEND
, CChatWnd::OnBnClickedCsend
)
52 EVT_BUTTON(IDC_CCLOSE
, CChatWnd::OnBnClickedCclose
)
53 EVT_MULENOTEBOOK_ALL_PAGES_CLOSED(IDC_CHATSELECTOR
, CChatWnd::OnAllPagesClosed
)
57 CChatWnd::CChatWnd(wxWindow
* pParent
)
58 : wxPanel(pParent
, -1)
60 wxSizer
* content
= messagePage(this, true);
61 content
->Show(this, true);
63 chatselector
= CastChild( IDC_CHATSELECTOR
, CChatSelector
);
64 // We want to use our own popup menu
65 chatselector
->SetPopupHandler(this);
68 friendlistctrl
= CastChild( ID_FRIENDLIST
, CFriendListCtrl
);
71 void CChatWnd::StartSession(CDlgFriend
* friend_client
, bool setfocus
)
74 if ( !friend_client
->m_name
.IsEmpty() ) {
76 theApp
->amuledlg
->SetActiveDialog(CamuleDlg::DT_CHAT_WND
, this);
78 chatselector
->StartSession(GUI_ID(friend_client
->m_ip
, friend_client
->m_port
), friend_client
->m_name
, true);
81 // Check to enable the window controls if needed
82 CheckNewButtonsState();
86 void CChatWnd::OnNMRclickChatTab(wxMouseEvent
& evt
)
88 // Only handle events from the chat-notebook
89 if (evt
.GetEventObject() != (wxObject
*)chatselector
)
92 if (chatselector
->GetSelection() == -1) {
96 // Avoid opening another menu when it's already open
98 m_menu
= new wxMenu(_("Chat"));
100 m_menu
->Append(MP_CLOSE_TAB
, wxString(_("Close tab")));
101 m_menu
->Append(MP_CLOSE_ALL_TABS
, wxString(_("Close all tabs")));
102 m_menu
->Append(MP_CLOSE_OTHER_TABS
, wxString(_("Close other tabs")));
104 m_menu
->AppendSeparator();
106 wxMenuItem
* addFriend
= m_menu
->Append(MP_ADDFRIEND
, _("Add to Friends"));
108 // Disable this client if it is already a friend
109 CUpDownClient
* client
= chatselector
->GetCurrentClient();
110 if (client
&& client
->IsFriend()) {
111 addFriend
->Enable(false);
114 PopupMenu(m_menu
, evt
.GetPosition());
122 void CChatWnd::OnPopupClose(wxCommandEvent
& WXUNUSED(evt
))
124 chatselector
->DeletePage(chatselector
->GetSelection());
128 void CChatWnd::OnPopupCloseAll(wxCommandEvent
& WXUNUSED(evt
))
130 chatselector
->DeleteAllPages();
134 void CChatWnd::OnPopupCloseOthers(wxCommandEvent
& WXUNUSED(evt
))
136 wxNotebookPage
* current
= chatselector
->GetPage(chatselector
->GetSelection());
138 for (int i
= chatselector
->GetPageCount() - 1; i
>= 0; i
--) {
139 if (current
!= chatselector
->GetPage(i
))
140 chatselector
->DeletePage( i
);
145 void CChatWnd::OnAddFriend(wxCommandEvent
& WXUNUSED(evt
))
147 // Get the client that the session is open to
148 CUpDownClient
* client
= chatselector
->GetCurrentClient();
150 // Add the client as friend unless it's already a friend
151 if (client
&& !client
->IsFriend()) {
157 void CChatWnd::OnBnClickedCsend(wxCommandEvent
& WXUNUSED(evt
))
159 wxString message
= CastChild(IDC_CMESSAGE
, wxTextCtrl
)->GetValue();
161 SendMessage(message
);
165 void CChatWnd::OnBnClickedCclose(wxCommandEvent
& WXUNUSED(evt
))
167 chatselector
->EndSession();
171 void CChatWnd::OnAllPagesClosed(wxNotebookEvent
& WXUNUSED(evt
))
173 CastChild(IDC_CMESSAGE
, wxTextCtrl
)->Clear();
174 // Check to disable the window controls
175 CheckNewButtonsState();
179 CDlgFriend
* CChatWnd::FindFriend(const CMD4Hash
& userhash
, uint32 dwIP
, uint16 nPort
)
181 return friendlistctrl
->FindFriend(userhash
, dwIP
, nPort
);
185 void CChatWnd::AddFriend(CUpDownClient
* toadd
)
187 friendlistctrl
->AddFriend(toadd
);
191 void CChatWnd::AddFriend(const CMD4Hash
& userhash
, const wxString
& name
, uint32 lastUsedIP
, uint32 lastUsedPort
)
193 friendlistctrl
->AddFriend( userhash
, name
, lastUsedIP
, lastUsedPort
);
196 void CChatWnd::RemoveFriend(const CMD4Hash
& userhash
, uint32 lastUsedIP
, uint32 lastUsedPort
)
198 friendlistctrl
->RemoveFriend(friendlistctrl
->FindFriend(userhash
, lastUsedIP
, lastUsedPort
));
201 void CChatWnd::RefreshFriend(CFriend
* Friend
, bool connected
)
203 CDlgFriend
* toupdate
= friendlistctrl
->FindFriend(Friend
->GetUserHash(), Friend
->GetIP(), Friend
->GetPort());
205 toupdate
->m_name
= Friend
->GetName();
206 toupdate
->islinked
= connected
;
207 if (toupdate
->m_ip
== Friend
->GetIP() && toupdate
->m_port
== Friend
->GetPort()) {
208 chatselector
->RefreshFriend(GUI_ID(toupdate
->m_ip
, toupdate
->m_port
), toupdate
->m_name
);
210 // Friend changed IP - drop Chat session
211 chatselector
->EndSession(GUI_ID(toupdate
->m_ip
, toupdate
->m_port
));
213 toupdate
->m_ip
= Friend
->GetIP();
214 toupdate
->m_port
= Friend
->GetPort();
216 friendlistctrl
->RefreshFriend(toupdate
);
220 void CChatWnd::ProcessMessage(uint64 sender
, const wxString
& message
)
222 if ( !theApp
->amuledlg
->IsDialogVisible(CamuleDlg::DT_CHAT_WND
) ) {
223 theApp
->amuledlg
->SetMessageBlink(true);
225 if (chatselector
->ProcessMessage(sender
, message
)) {
226 // Check to enable the window controls if needed
227 CheckNewButtonsState();
231 void CChatWnd::ConnectionResult(bool success
, const wxString
& message
, uint64 id
)
233 chatselector
->ConnectionResult(success
, message
, id
);
236 void CChatWnd::SendMessage(const wxString
& message
, const wxString
& client_name
, uint64 to_id
)
239 if (chatselector
->SendMessage( message
, client_name
, to_id
)) {
240 CastChild(IDC_CMESSAGE
, wxTextCtrl
)->Clear();
243 // Check to enable the window controls if needed
244 CheckNewButtonsState();
245 CastChild(IDC_CMESSAGE
, wxTextCtrl
)->SetFocus();
249 void CChatWnd::CheckNewButtonsState()
251 switch (chatselector
->GetPageCount()) {
253 GetParent()->FindWindow(IDC_CSEND
)->Enable(false);
254 GetParent()->FindWindow(IDC_CCLOSE
)->Enable(false);
255 GetParent()->FindWindow(IDC_CMESSAGE
)->Enable(false);
258 GetParent()->FindWindow(IDC_CSEND
)->Enable(true);
259 GetParent()->FindWindow(IDC_CCLOSE
)->Enable(true);
260 GetParent()->FindWindow(IDC_CMESSAGE
)->Enable(true);
263 // Nothing to be done here. Keep current state, which should be enabled.
264 wxASSERT(GetParent()->FindWindow(IDC_CSEND
)->IsEnabled());
265 wxASSERT(GetParent()->FindWindow(IDC_CCLOSE
)->IsEnabled());
266 wxASSERT(GetParent()->FindWindow(IDC_CMESSAGE
)->IsEnabled());
272 bool CChatWnd::IsIdValid(uint64 id
)
274 return chatselector
->GetTabByClientID(id
) >= 0;
278 void CChatWnd::ShowCaptchaResult(uint64 id
, bool ok
)
280 chatselector
->ShowCaptchaResult(id
, ok
);
283 void CChatWnd::EndSession(uint64 id
)
285 chatselector
->EndSession(id
);
288 // File_checked_for_headers