debian: fix build-deps for focal
[amule.git] / src / FriendListCtrl.cpp
blobacb06cfd9b199e0b92035cbc0696136023eaee50
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2011 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.
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"
39 #include "SafeFile.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)
54 END_EVENT_TABLE()
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)
70 if (!toremove) {
71 return;
74 sint32 itemnr = FindItem(-1, reinterpret_cast<wxUIntPtr>(toremove));
76 if ( itemnr == -1 )
77 return;
79 DeleteItem(itemnr);
83 void CFriendListCtrl::UpdateFriend(CFriend* toupdate)
85 if (!toupdate) {
86 return;
89 sint32 itemnr = FindItem(-1, reinterpret_cast<wxUIntPtr>(toupdate));
90 if (itemnr == -1) {
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 = reinterpret_cast<CFriend*>(GetItemData(cursel));
106 /* ignore this one, it is not activated anymore :) */
107 if (cur_friend == NULL) {
108 return;
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 = reinterpret_cast<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());
140 } else {
141 menu->Enable(MP_FRIENDSLOT, false);
145 PopupMenu(menu, event.GetPosition());
146 delete menu;
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 = reinterpret_cast<CFriend*>(GetItemData(index));
154 theApp->amuledlg->m_chatwnd->StartSession(cur_friend);
155 //#warning CORE/GUI!
156 #ifndef CLIENT_GUI
157 theApp->friendlist->StartChatSession(cur_friend);
158 #endif
160 index = GetNextItem( index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
165 void CFriendListCtrl::OnRemoveFriend(wxCommandEvent& WXUNUSED(event))
167 wxString question;
168 if (GetSelectedItemCount() == 1) {
169 question = _("Are you sure that you wish to delete the selected friend?");
170 } else {
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 = reinterpret_cast<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 = reinterpret_cast<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 = reinterpret_cast<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)
223 // Get item
224 long index = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
225 CFriend* cur_friend = reinterpret_cast<CFriend*>(GetItemData(index));
226 theApp->friendlist->SetFriendSlot(cur_friend, event.IsChecked());
227 index = GetNextItem( index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
228 if (index != -1) {
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()) {
239 wxCommandEvent evt;
240 evt.SetId( MP_REMOVEFRIEND );
241 OnRemoveFriend( evt );
243 } else {
244 event.Skip();
247 // File_checked_for_headers