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
27 #include "FriendListCtrl.h" // Interface declarations
29 #include <common/MenuIDs.h>
30 #include <common/MacrosProgramSpecific.h>
32 #include "amule.h" // Needed for theApp
33 #include "amuleDlg.h" // Needed for CamuleDlg
34 #include "ClientDetailDialog.h" // Needed for CClientDetailDialog
35 #include "AddFriend.h" // Needed for CAddFriend
36 #include "ChatWnd.h" // Needed for CChatWnd
37 #include "Friend.h" // Needed for CFriend
38 #include "muuli_wdr.h"
40 #include "FriendList.h" // Needed for the friends list
42 BEGIN_EVENT_TABLE(CFriendListCtrl
, CMuleListCtrl
)
43 EVT_RIGHT_DOWN(CFriendListCtrl::OnRightClick
)
44 EVT_LIST_ITEM_ACTIVATED(ID_FRIENDLIST
, CFriendListCtrl::OnItemActivated
)
46 EVT_MENU(MP_MESSAGE
, CFriendListCtrl::OnSendMessage
)
47 EVT_MENU(MP_REMOVEFRIEND
, CFriendListCtrl::OnRemoveFriend
)
48 EVT_MENU(MP_ADDFRIEND
, CFriendListCtrl::OnAddFriend
)
49 EVT_MENU(MP_DETAIL
, CFriendListCtrl::OnShowDetails
)
50 EVT_MENU(MP_SHOWLIST
, CFriendListCtrl::OnViewFiles
)
51 EVT_MENU(MP_FRIENDSLOT
, CFriendListCtrl::OnSetFriendslot
)
53 EVT_CHAR(CFriendListCtrl::OnKeyPressed
)
57 CFriendListCtrl::CFriendListCtrl(wxWindow
* parent
, int id
, const wxPoint
& pos
, wxSize siz
, int flags
)
58 : CMuleListCtrl(parent
, id
, pos
, siz
, flags
)
60 InsertColumn(0, _("Username"), wxLIST_FORMAT_LEFT
, siz
.GetWidth() - 4);
63 CFriendListCtrl::~CFriendListCtrl()
68 void CFriendListCtrl::RemoveFriend(CFriend
* toremove
)
74 sint32 itemnr
= FindItem(-1, reinterpret_cast<wxUIntPtr
>(toremove
));
83 void CFriendListCtrl::UpdateFriend(CFriend
* toupdate
)
89 sint32 itemnr
= FindItem(-1, reinterpret_cast<wxUIntPtr
>(toupdate
));
91 itemnr
= InsertItem(GetItemCount(), wxEmptyString
);
92 SetItemPtrData(itemnr
, reinterpret_cast<wxUIntPtr
>(toupdate
));
95 SetItem(itemnr
, 0, toupdate
->GetName());
96 SetItemTextColour(itemnr
, toupdate
->GetLinkedClient().IsLinked() ? *wxBLUE
: *wxBLACK
);
100 void CFriendListCtrl::OnItemActivated(wxListEvent
& WXUNUSED(event
))
102 int cursel
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
104 CFriend
* cur_friend
= (CFriend
*)GetItemData(cursel
);
106 /* ignore this one, it is not activated anymore :) */
107 if (cur_friend
== NULL
) {
111 theApp
->amuledlg
->m_chatwnd
->StartSession(cur_friend
);
116 void CFriendListCtrl::OnRightClick(wxMouseEvent
& event
)
118 int cursel
= CheckSelection(event
);
120 CFriend
* cur_friend
= NULL
;
122 wxMenu
* menu
= new wxMenu(_("Friends"));
124 if ( cursel
!= -1 ) {
125 cur_friend
= (CFriend
*)GetItemData(cursel
);
126 menu
->Append(MP_DETAIL
, _("Show &Details"));
127 menu
->Enable(MP_DETAIL
, cur_friend
->GetLinkedClient().IsLinked());
130 menu
->Append(MP_ADDFRIEND
, _("Add a friend"));
132 if (cursel
!= (-1)) {
133 menu
->Append(MP_REMOVEFRIEND
, _("Remove Friend"));
134 menu
->Append(MP_MESSAGE
, _("Send &Message"));
135 menu
->Append(MP_SHOWLIST
, _("View Files"));
136 menu
->AppendCheckItem(MP_FRIENDSLOT
, _("Establish Friend Slot"));
137 if (cur_friend
->GetLinkedClient().IsLinked()) {
138 menu
->Enable(MP_FRIENDSLOT
, true);
139 menu
->Check(MP_FRIENDSLOT
, cur_friend
->HasFriendSlot());
141 menu
->Enable(MP_FRIENDSLOT
, false);
145 PopupMenu(menu
, event
.GetPosition());
149 void CFriendListCtrl::OnSendMessage(wxCommandEvent
& WXUNUSED(event
)) {
150 long index
= GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
152 while( index
!= -1 ) {
153 CFriend
* cur_friend
= (CFriend
*)GetItemData(index
);
154 theApp
->amuledlg
->m_chatwnd
->StartSession(cur_friend
);
157 theApp
->friendlist
->StartChatSession(cur_friend
);
160 index
= GetNextItem( index
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
165 void CFriendListCtrl::OnRemoveFriend(wxCommandEvent
& WXUNUSED(event
))
168 if (GetSelectedItemCount() == 1) {
169 question
= _("Are you sure that you wish to delete the selected friend?");
171 question
= _("Are you sure that you wish to delete the selected friends?");
174 if ( wxMessageBox( question
, _("Cancel"), wxICON_QUESTION
| wxYES_NO
, this) == wxYES
) {
175 long index
= GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
177 while( index
!= -1 ) {
178 CFriend
* cur_friend
= (CFriend
*)GetItemData(index
);
179 theApp
->friendlist
->RemoveFriend(cur_friend
);
180 // -1 because we changed the list and removed that item.
181 index
= GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
187 void CFriendListCtrl::OnAddFriend(wxCommandEvent
& WXUNUSED(event
))
189 CAddFriend(this).ShowModal();
193 void CFriendListCtrl::OnShowDetails(wxCommandEvent
& WXUNUSED(event
))
195 long index
= GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
197 while( index
!= -1 ) {
198 CFriend
* cur_friend
= (CFriend
*)GetItemData(index
);
199 if (cur_friend
->GetLinkedClient().IsLinked()) {
200 CClientDetailDialog(this, cur_friend
->GetLinkedClient()).ShowModal();
202 index
= GetNextItem( index
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
208 void CFriendListCtrl::OnViewFiles(wxCommandEvent
& WXUNUSED(event
))
210 long index
= GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
212 while( index
!= -1 ) {
213 CFriend
* cur_friend
= (CFriend
*)GetItemData(index
);
214 theApp
->friendlist
->RequestSharedFileList(cur_friend
);
215 index
= GetNextItem( index
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
221 void CFriendListCtrl::OnSetFriendslot(wxCommandEvent
& event
)
224 long index
= GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
225 CFriend
* cur_friend
= (CFriend
*)GetItemData(index
);
226 theApp
->friendlist
->SetFriendSlot(cur_friend
, event
.IsChecked());
227 index
= GetNextItem( index
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
229 wxMessageBox(_("You are not allowed to set more than one friendslot.\n Only one slot was assigned."), _("Multiple selection"), wxOK
| wxICON_ERROR
, this);
234 void CFriendListCtrl::OnKeyPressed(wxKeyEvent
& event
)
236 // Check if delete was pressed
237 if ((event
.GetKeyCode() == WXK_DELETE
) || (event
.GetKeyCode() == WXK_NUMPAD_DELETE
)) {
238 if (GetItemCount()) {
240 evt
.SetId( MP_REMOVEFRIEND
);
241 OnRemoveFriend( evt
);
247 // File_checked_for_headers