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.
10 //
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.
15 //
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
24 //
25 
26 
27 #include "FriendListCtrl.h"	// Interface declarations
28 
29 #include <common/MenuIDs.h>
30 #include <common/MacrosProgramSpecific.h>
31 
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
41 
BEGIN_EVENT_TABLE(CFriendListCtrl,CMuleListCtrl)42 BEGIN_EVENT_TABLE(CFriendListCtrl, CMuleListCtrl)
43 	EVT_RIGHT_DOWN(CFriendListCtrl::OnRightClick)
44 	EVT_LIST_ITEM_ACTIVATED(ID_FRIENDLIST, CFriendListCtrl::OnItemActivated)
45 
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)
52 
53 	EVT_CHAR(CFriendListCtrl::OnKeyPressed)
54 END_EVENT_TABLE()
55 
56 
57 CFriendListCtrl::CFriendListCtrl(wxWindow* parent, int id, const wxPoint& pos, wxSize siz, int flags)
58 : CMuleListCtrl(parent, id, pos, siz, flags)
59 {
60   InsertColumn(0, _("Username"), wxLIST_FORMAT_LEFT, siz.GetWidth() - 4);
61 }
62 
~CFriendListCtrl()63 CFriendListCtrl::~CFriendListCtrl()
64 {
65 }
66 
67 
RemoveFriend(CFriend * toremove)68 void CFriendListCtrl::RemoveFriend(CFriend* toremove)
69 {
70 	if (!toremove) {
71 		return;
72 	}
73 
74 	sint32 itemnr = FindItem(-1, reinterpret_cast<wxUIntPtr>(toremove));
75 
76 	if ( itemnr == -1 )
77 		return;
78 
79 	DeleteItem(itemnr);
80 }
81 
82 
UpdateFriend(CFriend * toupdate)83 void CFriendListCtrl::UpdateFriend(CFriend* toupdate)
84 {
85 	if (!toupdate) {
86 		return;
87 	}
88 
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));
93 	}
94 
95 	SetItem(itemnr, 0, toupdate->GetName());
96 	SetItemTextColour(itemnr, toupdate->GetLinkedClient().IsLinked() ? *wxBLUE : *wxBLACK);
97 }
98 
99 
OnItemActivated(wxListEvent & WXUNUSED (event))100 void CFriendListCtrl::OnItemActivated(wxListEvent& WXUNUSED(event))
101 {
102 	int cursel = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
103 
104 	CFriend* cur_friend = reinterpret_cast<CFriend*>(GetItemData(cursel));
105 
106 	/* ignore this one, it is not activated anymore :) */
107 	if (cur_friend == NULL) {
108 		return;
109 	}
110 
111 	theApp->amuledlg->m_chatwnd->StartSession(cur_friend);
112 
113 }
114 
115 
OnRightClick(wxMouseEvent & event)116 void CFriendListCtrl::OnRightClick(wxMouseEvent& event)
117 {
118 	int cursel = CheckSelection(event);
119 
120 	CFriend* cur_friend = NULL;
121 
122 	wxMenu* menu = new wxMenu(_("Friends"));
123 
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());
128 	}
129 
130 	menu->Append(MP_ADDFRIEND, _("Add a friend"));
131 
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);
142 		}
143 	}
144 
145 	PopupMenu(menu, event.GetPosition());
146 	delete menu;
147 }
148 
OnSendMessage(wxCommandEvent & WXUNUSED (event))149 void CFriendListCtrl::OnSendMessage(wxCommandEvent& WXUNUSED(event)) {
150 	long index = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
151 
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
159 
160 		index = GetNextItem( index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
161 	}
162 }
163 
164 
OnRemoveFriend(wxCommandEvent & WXUNUSED (event))165 void CFriendListCtrl::OnRemoveFriend(wxCommandEvent& WXUNUSED(event))
166 {
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?");
172 	}
173 
174 	if ( wxMessageBox( question, _("Cancel"), wxICON_QUESTION | wxYES_NO, this) == wxYES ) {
175 		long index = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
176 
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 );
182 		}
183 	}
184 }
185 
186 
OnAddFriend(wxCommandEvent & WXUNUSED (event))187 void CFriendListCtrl::OnAddFriend(wxCommandEvent& WXUNUSED(event))
188 {
189 	CAddFriend(this).ShowModal();
190 }
191 
192 
OnShowDetails(wxCommandEvent & WXUNUSED (event))193 void CFriendListCtrl::OnShowDetails(wxCommandEvent& WXUNUSED(event))
194 {
195 	long index = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
196 
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();
201 		}
202 		index = GetNextItem( index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
203 	}
204 
205 }
206 
207 
OnViewFiles(wxCommandEvent & WXUNUSED (event))208 void CFriendListCtrl::OnViewFiles(wxCommandEvent& WXUNUSED(event))
209 {
210 	long index = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
211 
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 );
216 	}
217 
218 }
219 
220 
OnSetFriendslot(wxCommandEvent & event)221 void CFriendListCtrl::OnSetFriendslot(wxCommandEvent& event)
222 {
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);
230 	}
231 }
232 
233 
OnKeyPressed(wxKeyEvent & event)234 void CFriendListCtrl::OnKeyPressed(wxKeyEvent& event)
235 {
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 );
242 		}
243 	} else {
244 		event.Skip();
245 	}
246 }
247 // File_checked_for_headers
248