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
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 "ChatSelector.h" // Needed for CChatSelector
34 #include "muuli_wdr.h" // Needed for messagePage
35 #include "OtherFunctions.h"
37 BEGIN_EVENT_TABLE(CChatWnd
, wxPanel
)
38 EVT_TEXT_ENTER(IDC_CMESSAGE
, CChatWnd::OnBnClickedCsend
)
39 EVT_BUTTON(IDC_CSEND
, CChatWnd::OnBnClickedCsend
)
40 EVT_BUTTON(IDC_CCLOSE
, CChatWnd::OnBnClickedCclose
)
41 EVT_MULENOTEBOOK_ALL_PAGES_CLOSED(IDC_CHATSELECTOR
, CChatWnd::OnAllPagesClosed
)
45 CChatWnd::CChatWnd(wxWindow
* pParent
)
46 : wxPanel(pParent
, -1)
48 wxSizer
* content
= messagePage(this, true);
49 content
->Show(this, true);
51 chatselector
= CastChild( IDC_CHATSELECTOR
, CChatSelector
);
52 friendlistctrl
= CastChild( ID_FRIENDLIST
, CFriendListCtrl
);
55 void CChatWnd::StartSession(CDlgFriend
* friend_client
, bool setfocus
)
58 if ( !friend_client
->m_name
.IsEmpty() ) {
60 theApp
->amuledlg
->SetActiveDialog(CamuleDlg::DT_CHAT_WND
, this);
62 chatselector
->StartSession(GUI_ID(friend_client
->m_ip
, friend_client
->m_port
), friend_client
->m_name
, true);
65 // Check to enable the window controls if needed
66 CheckNewButtonsState();
70 void CChatWnd::OnBnClickedCsend(wxCommandEvent
& WXUNUSED(evt
))
72 wxString message
= CastChild(IDC_CMESSAGE
, wxTextCtrl
)->GetValue();
78 void CChatWnd::OnBnClickedCclose(wxCommandEvent
& WXUNUSED(evt
))
80 chatselector
->EndSession();
84 void CChatWnd::OnAllPagesClosed(wxNotebookEvent
& WXUNUSED(evt
))
86 CastChild(IDC_CMESSAGE
, wxTextCtrl
)->Clear();
87 // Check to disable the window controls
88 CheckNewButtonsState();
92 CDlgFriend
* CChatWnd::FindFriend(const CMD4Hash
& userhash
, uint32 dwIP
, uint16 nPort
)
94 return friendlistctrl
->FindFriend(userhash
, dwIP
, nPort
);
98 void CChatWnd::AddFriend(CUpDownClient
* toadd
)
100 friendlistctrl
->AddFriend(toadd
);
104 void CChatWnd::AddFriend(const CMD4Hash
& userhash
, const wxString
& name
, uint32 lastUsedIP
, uint32 lastUsedPort
)
106 friendlistctrl
->AddFriend( userhash
, name
, lastUsedIP
, lastUsedPort
);
109 void CChatWnd::RemoveFriend(const CMD4Hash
& userhash
, uint32 lastUsedIP
, uint32 lastUsedPort
)
111 friendlistctrl
->RemoveFriend(friendlistctrl
->FindFriend(userhash
, lastUsedIP
, lastUsedPort
));
114 void CChatWnd::RefreshFriend(const CMD4Hash
& userhash
, const wxString
& name
, uint32 lastUsedIP
, uint32 lastUsedPort
)
116 CDlgFriend
* toupdate
= friendlistctrl
->FindFriend(userhash
, lastUsedIP
, lastUsedPort
);
118 if (!name
.IsEmpty()) {
119 toupdate
->m_name
= name
;
122 // If name is empty, this is a disconnection/deletion event
123 toupdate
->islinked
= !name
.IsEmpty();
124 friendlistctrl
->RefreshFriend(toupdate
);
125 chatselector
->RefreshFriend(GUI_ID(toupdate
->m_ip
, toupdate
->m_port
), toupdate
->m_name
);
129 void CChatWnd::ProcessMessage(uint64 sender
, const wxString
& message
)
131 if ( !theApp
->amuledlg
->IsDialogVisible(CamuleDlg::DT_CHAT_WND
) ) {
132 theApp
->amuledlg
->SetMessageBlink(true);
134 if (chatselector
->ProcessMessage(sender
, message
)) {
135 // Check to enable the window controls if needed
136 CheckNewButtonsState();
140 void CChatWnd::ConnectionResult(bool success
, const wxString
& message
, uint64 id
)
142 chatselector
->ConnectionResult(success
, message
, id
);
145 void CChatWnd::SendMessage(const wxString
& message
, const wxString
& client_name
, uint64 to_id
)
148 if (chatselector
->SendMessage( message
, client_name
, to_id
)) {
149 CastChild(IDC_CMESSAGE
, wxTextCtrl
)->Clear();
152 // Check to enable the window controls if needed
153 CheckNewButtonsState();
154 CastChild(IDC_CMESSAGE
, wxTextCtrl
)->SetFocus();
158 void CChatWnd::CheckNewButtonsState()
160 switch (chatselector
->GetPageCount()) {
162 GetParent()->FindWindow(IDC_CSEND
)->Enable(false);
163 GetParent()->FindWindow(IDC_CCLOSE
)->Enable(false);
164 GetParent()->FindWindow(IDC_CMESSAGE
)->Enable(false);
167 GetParent()->FindWindow(IDC_CSEND
)->Enable(true);
168 GetParent()->FindWindow(IDC_CCLOSE
)->Enable(true);
169 GetParent()->FindWindow(IDC_CMESSAGE
)->Enable(true);
172 // Nothing to be done here. Keep current state, which should be enabled.
173 wxASSERT(GetParent()->FindWindow(IDC_CSEND
)->IsEnabled());
174 wxASSERT(GetParent()->FindWindow(IDC_CCLOSE
)->IsEnabled());
175 wxASSERT(GetParent()->FindWindow(IDC_CMESSAGE
)->IsEnabled());
181 bool CChatWnd::IsIdValid(uint64 id
)
183 return chatselector
->GetTabByClientID(id
) >= 0;
187 void CChatWnd::ShowCaptchaResult(uint64 id
, bool ok
)
189 chatselector
->ShowCaptchaResult(id
, ok
);
192 // File_checked_for_headers