forked from irwir/eMule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommentListCtrl.cpp
246 lines (215 loc) · 7.09 KB
/
CommentListCtrl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//this file is part of eMule
//Copyright (C)2002 Merkur ( merkur-@users.sourceforge.net / http://www.emule-project.net )
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#include "stdafx.h"
#include "CommentListCtrl.h"
#include "OtherFunctions.h"
#include "MenuCmds.h"
#include "TitleMenu.h"
#include "emule.h"
#include "UpDownClient.h"
#include "kademlia/kademlia/Entry.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#endif
enum ECols
{
colRating = 0,
colComment,
colFileName,
colUserName,
colOrigin
};
IMPLEMENT_DYNAMIC(CCommentListCtrl, CMuleListCtrl)
BEGIN_MESSAGE_MAP(CCommentListCtrl, CMuleListCtrl)
ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, OnLvnColumnClick)
ON_NOTIFY_REFLECT(LVN_DELETEITEM, OnLvnDeleteItem)
ON_WM_CONTEXTMENU()
END_MESSAGE_MAP()
CCommentListCtrl::CCommentListCtrl()
{
}
CCommentListCtrl::~CCommentListCtrl()
{
}
void CCommentListCtrl::Init(void)
{
SetPrefsKey(_T("CommentListCtrl"));
ASSERT( (GetStyle() & LVS_SINGLESEL) == 0 );
SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP);
InsertColumn(colRating, GetResString(IDS_QL_RATING), LVCFMT_LEFT, 80);
InsertColumn(colComment, GetResString(IDS_COMMENT), LVCFMT_LEFT, 340);
InsertColumn(colFileName, GetResString(IDS_DL_FILENAME), LVCFMT_LEFT, DFLT_FILENAME_COL_WIDTH);
InsertColumn(colUserName, GetResString(IDS_QL_USERNAME), LVCFMT_LEFT, DFLT_CLIENTNAME_COL_WIDTH);
InsertColumn(colOrigin, GetResString(IDS_NETWORK), LVCFMT_LEFT, 80);
CImageList iml;
iml.Create(16, 16, theApp.m_iDfltImageListColorFlags | ILC_MASK, 0, 1);
iml.Add(CTempIconLoader(_T("Rating_NotRated")));
iml.Add(CTempIconLoader(_T("Rating_Fake")));
iml.Add(CTempIconLoader(_T("Rating_Poor")));
iml.Add(CTempIconLoader(_T("Rating_Fair")));
iml.Add(CTempIconLoader(_T("Rating_Good")));
iml.Add(CTempIconLoader(_T("Rating_Excellent")));
CImageList* pimlOld = SetImageList(&iml, LVSIL_SMALL);
iml.Detach();
if (pimlOld)
pimlOld->DeleteImageList();
LoadSettings();
SetSortArrow();
SortItems(SortProc, MAKELONG(GetSortItem(), (GetSortAscending() ? 0 : 1)));
}
int CCommentListCtrl::SortProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
const SComment *item1 = (SComment *)lParam1;
const SComment *item2 = (SComment *)lParam2;
if (item1 == NULL || item2 == NULL)
return 0;
int iResult;
switch (LOWORD(lParamSort))
{
case colRating:
if (item1->m_iRating < item2->m_iRating)
iResult = -1;
else if (item1->m_iRating > item2->m_iRating)
iResult = 1;
else
iResult = 0;
break;
case colComment:
iResult = CompareLocaleStringNoCase(item1->m_strComment, item2->m_strComment);
break;
case colFileName:
iResult = CompareLocaleStringNoCase(item1->m_strFileName, item2->m_strFileName);
break;
case colUserName:
iResult = CompareLocaleStringNoCase(item1->m_strUserName, item2->m_strUserName);
break;
case colOrigin:
if (item1->m_iOrigin < item2->m_iOrigin)
iResult = -1;
else if (item1->m_iOrigin > item2->m_iOrigin)
iResult = 1;
else
iResult = 0;
break;
default:
ASSERT(0);
return 0;
}
if (HIWORD(lParamSort))
iResult = -iResult;
return iResult;
}
void CCommentListCtrl::OnLvnColumnClick(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
// Determine ascending based on whether already sorted on this column
int iSortItem = GetSortItem();
bool bOldSortAscending = GetSortAscending();
bool bSortAscending = (iSortItem != pNMLV->iSubItem) ? true : !bOldSortAscending;
// Item is column clicked
iSortItem = pNMLV->iSubItem;
// Sort table
UpdateSortHistory(MAKELONG(iSortItem, (bSortAscending ? 0 : 0x0001)));
SetSortArrow(iSortItem, bSortAscending);
SortItems(SortProc, MAKELONG(iSortItem, (bSortAscending ? 0 : 0x0001)));
*pResult = 0;
}
void CCommentListCtrl::OnContextMenu(CWnd* /*pWnd*/, CPoint point)
{
UINT flag = MF_STRING;
if (GetNextItem(-1, LVIS_SELECTED | LVIS_FOCUSED) == -1)
flag = MF_GRAYED;
CTitleMenu popupMenu;
popupMenu.CreatePopupMenu();
popupMenu.AppendMenu(MF_STRING | flag, MP_COPYSELECTED, GetResString(IDS_COPY));
GetPopupMenuPos(*this, point);
popupMenu.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);
VERIFY( popupMenu.DestroyMenu() );
}
BOOL CCommentListCtrl::OnCommand(WPARAM wParam, LPARAM lParam)
{
switch (wParam)
{
case MP_COPYSELECTED: {
CString strText;
POSITION posItem = GetFirstSelectedItemPosition();
while (posItem) {
int iItem = GetNextSelectedItem(posItem);
if (iItem >= 0) {
CString strComment = GetItemText(iItem, colComment);
if (!strComment.IsEmpty()) {
if (!strText.IsEmpty())
strText += _T("\r\n");
strText += strComment;
}
}
}
theApp.CopyTextToClipboard(strText);
break;
}
}
return CMuleListCtrl::OnCommand(wParam, lParam);
}
int CCommentListCtrl::FindClientComment(const void* pClientCookie)
{
int iItems = GetItemCount();
for (int i = 0; i < iItems; i++)
{
const SComment* pComment = (SComment*)GetItemData(i);
if (pComment && pComment->m_pClientCookie == pClientCookie)
return i;
}
return -1;
}
void CCommentListCtrl::AddComment(const SComment* pComment)
{
int iItem = InsertItem(LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM,
0, GetRateString(pComment->m_iRating),
0, 0, pComment->m_iRating, (LPARAM)pComment);
SetItemText(iItem, colComment, pComment->m_strComment);
SetItemText(iItem, colFileName, pComment->m_strFileName);
SetItemText(iItem, colUserName, pComment->m_strUserName);
SetItemText(iItem, colOrigin, pComment->m_iOrigin == 0 ? _T("eD2K") : _T("Kad"));
}
void CCommentListCtrl::AddItem(const CUpDownClient* client)
{
const void* pClientCookie = client;
if (FindClientComment(pClientCookie) != -1)
return;
int iRating = client->GetFileRating();
SComment* pComment = new SComment(pClientCookie, iRating, client->GetFileComment(),
client->GetClientFilename(), client->GetUserName(), 0/*eD2K*/);
AddComment(pComment);
}
void CCommentListCtrl::AddItem(const Kademlia::CEntry* entry)
{
const void* pClientCookie = entry;
if (FindClientComment(pClientCookie) != -1)
return;
int iRating = (int)entry->GetIntTagValue(TAG_FILERATING);
SComment* pComment = new SComment(pClientCookie, iRating, entry->GetStrTagValue(TAG_DESCRIPTION),
entry->GetCommonFileName(), _T(""), 1/*Kad*/);
AddComment(pComment);
}
void CCommentListCtrl::OnLvnDeleteItem(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
delete (SComment*)pNMLV->lParam;
*pResult = 0;
}