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 //
6 // Any parts of this program derived from the xMule, lMule or eMule project,
7 // or contributed by third-party developers are copyrighted by their
8 // respective authors.
9 //
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 2 of the License, or
13 // (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
23 //
24 
25 #ifndef GENERICCLIENTLISTCTRL_H
26 #define GENERICCLIENTLISTCTRL_H
27 
28 #include <map>				// Needed for std::multimap
29 #include <vector>			// Needed for std::vector
30 #include <wx/brush.h>
31 
32 #include "Types.h"			// Needed for uint8
33 #include "Constants.h"		// Needed for DownloadItemType
34 #include "MuleListCtrl.h"	// Needed for CMuleListCtrl
35 #include "amuleDlg.h"		// Needed for CamuleDlg::DialogType
36 
37 class CPartFile;
38 class CClientRef;
39 class wxBitmap;
40 class wxRect;
41 class wxDC;
42 
43 struct ClientCtrlItem_Struct;
44 
45 enum GenericColumnEnum {
46 	ColumnUserName = 0,
47 	ColumnUserDownloaded,
48 	ColumnUserUploaded,
49 	ColumnUserSpeedDown,
50 	ColumnUserSpeedUp,
51 	ColumnUserProgress,
52 	ColumnUserAvailable,
53 	ColumnUserVersion,
54 	ColumnUserQueueRankLocal,
55 	ColumnUserQueueRankRemote,
56 	ColumnUserOrigin,
57 	ColumnUserFileNameDownload,
58 	ColumnUserFileNameUpload,
59 	ColumnUserFileNameDownloadRemote,
60 	ColumnUserSharedFiles,
61 	ColumnInvalid
62 };
63 
64 struct CGenericClientListCtrlColumn {
65 	GenericColumnEnum cid;
66 	wxString title;
67 	int width;
68 };
69 
70 struct GenericColumnInfo {
GenericColumnInfoGenericColumnInfo71 	GenericColumnInfo(int n, CGenericClientListCtrlColumn* col) : n_columns(n), columns(col) { };
72 	int n_columns;
73 	CGenericClientListCtrlColumn* columns;
74 };
75 
76 typedef std::vector<CKnownFile*> CKnownFileVector;
77 
78 /**
79  * This class is responsible for representing clients in a generic way.
80  */
81 
82 class CGenericClientListCtrl : public CMuleListCtrl
83 {
84 public:
85 	/**
86 	 * Constructor.
87 	 *
88 	 * @see CMuleListCtrl::CMuleListCtrl for documentation of parameters.
89 	 */
90 	 CGenericClientListCtrl(
91 				const wxString& tablename,
92 	            wxWindow *parent,
93                 wxWindowID winid,
94                 const wxPoint &pos,
95                 const wxSize &size,
96                 long style,
97                 const wxValidator& validator,
98                 const wxString &name);
99 
100 	/**
101 	 * Destructor.
102 	 */
103 	virtual	~CGenericClientListCtrl();
104 
105 	/**
106 	 * Initializes the control. We need a 2-stage initialization so the derived class members can be called.
107 	 */
108 	 void InitColumnData();
109 
110 	/**
111 	 * Adds a source belonging to the specified file.
112 	 *
113 	 * @param owner The owner of this specific source-entry, must be a valid pointer.
114 	 * @param source The client object to be added, must be a valid pointer.
115 	 * @param type If the source is a current source, or a A4AF source.
116 	 *
117 	 * Please note that the specified client will only be added to the list if it's
118 	 * owner is shown, otherwise the source will simply be ignored.
119 	 * Duplicates wont be added.
120 	 */
121 	void AddSource( CKnownFile* owner, const CClientRef& source, SourceItemType type );
122 
123 	/**
124 	 * Removes a source from the list.
125 	 *
126 	 * @param source ID of the source to be removed.
127 	 * @param owner Either a specific file, or NULL to remove the source from all files.
128 	 */
129 	void RemoveSource( uint32 source, const CKnownFile* owner );
130 
131 	/**
132 	 * Shows the clients of specific files.
133 	 *
134 	 * @param file A valid, sorted vector of files whose clients will be shown.
135 	 *
136 	 * WARNING: The received vector *MUST* be odered with std::sort.
137 	 *
138 	 */
139 	void ShowSources( const CKnownFileVector& files );
140 
141 	/**
142 	 * Updates the state of the specified item, possibly causing a redrawing.
143 	 *
144 	 * @param toupdate ID of the client to be updated.
145 	 * @param type If the source is a current source, or a A4AF source.
146 	 *
147 	 */
148 	void UpdateItem(uint32 toupdate, SourceItemType type);
149 
SetShowing(bool status)150 	void SetShowing( bool status ) { m_showing = status; }
GetShowing()151 	bool GetShowing() const { return m_showing; }
152 
153 protected:
154 	// The columns with their attributes; MUST be defined by the derived class.
155 	GenericColumnInfo m_columndata;
156 	static int wxCALLBACK SortProc(wxUIntPtr item1, wxUIntPtr item2, long sortData);
157 
158 private:
159 	/**
160      *
161 	 * Must be overriden by the derived class and return the dialog where this list is.
162      * @see CamuleDlg::DialogType
163 	 *
164      */
165 	virtual CamuleDlg::DialogType GetParentDialog() = 0;
166 
167 	/**
168 	 * Updates the displayed number representing the amount of clients currently shown.
169 	 */
170 	void ShowSourcesCount( int diff );
171 
172 	/**
173 	 * Overloaded function needed for custom drawing of items.
174 	 */
175 	virtual void OnDrawItem( int item, wxDC* dc, const wxRect& rect, const wxRect& rectHL, bool highlighted );
176 
177 	/**
178 	 * Draws a client item.
179 	 */
180 	void	DrawClientItem( wxDC* dc, int nColumn, const wxRect& rect, ClientCtrlItem_Struct* item, int iTextOffset, int iBitmapOffset, int iBitmapXSize ) const;
181 
182 	/**
183 	 * Draws the download status (chunk) bar for a client.
184 	 */
185 	void	DrawSourceStatusBar( const CClientRef& source, wxDC* dc, const wxRect& rect, bool  bFlat) const;
186 
187 	/**
188 	  * Draaws the file parts bar for a client.
189 	  */
190 	void	DrawStatusBar( const CClientRef& client, wxDC* dc, const wxRect& rect1 ) const;
191 
192 	/**
193 	 * @see CMuleListCtrl::GetTTSText
194 	 * Just a dummy
195 	 */
GetTTSText(unsigned)196 	virtual wxString GetTTSText(unsigned) const { return wxEmptyString; }
197 
198 	/**
199 	 * Set "show sources" or "show peers" flag in Known File
200 	 */
201 	virtual void SetShowSources(CKnownFile *, bool) const = 0;
202 
203 	/**
204 	 * Translate the CID to a unique string for saving column sizes
205 	 * @see CMuleListCtrl::InsertColumn
206 	 */
207 	wxString TranslateCIDToName(GenericColumnEnum cid);
208 
209 	static int Compare( const CClientRef& client1, const CClientRef& client2, long lParamColumnSort);
210 
211 	// Event-handlers for clients.
212 	void	OnSwapSource( wxCommandEvent& event );
213 	void	OnViewFiles( wxCommandEvent& event );
214 	void	OnAddFriend( wxCommandEvent& event );
215 	void	OnSetFriendslot( wxCommandEvent& event );
216 	void	OnSendMessage( wxCommandEvent& event );
217 	void	OnViewClientInfo( wxCommandEvent& event );
218 
219 	// Misc event-handlers
220 	void	OnItemActivated( wxListEvent& event );
221 	void	OnMouseRightClick( wxListEvent& event );
222 	void	OnMouseMiddleClick( wxListEvent& event );
223 	void	OnKeyPressed( wxKeyEvent& event );
224 
225 	//! The type of list used to store items on the listctrl. We use the unique ECID as key.
226 	typedef std::multimap<uint32, ClientCtrlItem_Struct*> ListItems;
227 	//! Shortcut to the pair-type used on the list.
228 	typedef ListItems::value_type ListItemsPair;
229 	//! This pair is used when searching for equal-ranges.
230 	typedef std::pair< ListItems::iterator, ListItems::iterator > ListIteratorPair;
231 
232 	//! This list contains everything shown on the list. Sources are only to
233 	//! be found on this list if they are being displayed
234 	ListItems	m_ListItems;
235 
236 	//! Pointer to the current menu object, used to avoid multiple menus.
237 	wxMenu*		m_menu;
238 	//! Cached brush object.
239 	wxBrush	m_hilightBrush;
240 	//! Cached brush object.
241 	wxBrush	m_hilightUnfocusBrush;
242 
243 	//! The number of displayed sources
244 	int m_clientcount;
245 
246 	//! The files being shown, if any.
247 	CKnownFileVector m_knownfiles;
248 
249 	DECLARE_EVENT_TABLE()
250 
251 	bool m_showing;
252 
253 	void RawAddSource(CKnownFile* owner, CClientRef source, SourceItemType type);
254 	void RawRemoveSource( ListItems::iterator& it );
255 
256 	virtual bool IsShowingDownloadSources() const = 0;
257 };
258 
259 #endif
260 // File_checked_for_headers
261