1 | |
---|
2 | // Copyright Eóin O'Callaghan 2006 - 2008. |
---|
3 | // Distributed under the Boost Software License, Version 1.0. |
---|
4 | // (See accompanying file LICENSE_1_0.txt or copy at |
---|
5 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
6 | |
---|
7 | #include "stdAfx.hpp" |
---|
8 | |
---|
9 | #include <boost/iterator/filter_iterator.hpp> |
---|
10 | #include <winstl/controls/listview_sequence.hpp> |
---|
11 | |
---|
12 | #include "Halite.hpp" |
---|
13 | |
---|
14 | #include "HaliteListView.hpp" |
---|
15 | #include "HaliteWindow.hpp" |
---|
16 | #include "HaliteListViewDlg.hpp" |
---|
17 | |
---|
18 | #include "halTorrent.hpp" |
---|
19 | |
---|
20 | HaliteListViewCtrl::HaliteListViewCtrl(HaliteWindow& HalWindow) : |
---|
21 | halWindow_(HalWindow), |
---|
22 | iniClass("listviews/halite", "HaliteListView") |
---|
23 | { |
---|
24 | HalWindow.connectUiUpdate(bind(&HaliteListViewCtrl::uiUpdate, this, _1)); |
---|
25 | |
---|
26 | std::vector<wstring> names; |
---|
27 | wstring column_names = hal::app().res_wstr(LISTVIEW_ID_COLUMNNAMES); |
---|
28 | |
---|
29 | boost::split(names, column_names, boost::is_any_of(L";")); |
---|
30 | |
---|
31 | array<int, NumberOfColumns_s> widths = {100,110,60,60,60,42,45,61,45,45,45,45,45,45,45,45,45,45,45,45,45}; |
---|
32 | array<int, NumberOfColumns_s> order = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; |
---|
33 | array<bool, NumberOfColumns_s> visible = {true,true,true,true,true,true,true,true,true,true,true,\ |
---|
34 | true,true,true,true,true,true,true,true,true,true}; |
---|
35 | |
---|
36 | SetDefaults(names, widths, order, visible); |
---|
37 | Load(); |
---|
38 | } |
---|
39 | |
---|
40 | void HaliteListViewCtrl::OnShowWindow(UINT, INT) |
---|
41 | { |
---|
42 | SetExtendedListViewStyle(WS_EX_CLIENTEDGE|LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP|LVS_EX_DOUBLEBUFFER); |
---|
43 | SetSortListViewExtendedStyle(SORTLV_USESHELLBITMAPS, SORTLV_USESHELLBITMAPS); |
---|
44 | |
---|
45 | ApplyDetails(); |
---|
46 | |
---|
47 | SetColumnSortType(0, LVCOLSORT_CUSTOM, new ColumnAdapters::Name()); |
---|
48 | SetColumnSortType(1, LVCOLSORT_CUSTOM, new ColumnAdapters::State()); |
---|
49 | SetColumnSortType(2, LVCOLSORT_CUSTOM, new ColumnAdapters::Progress()); |
---|
50 | SetColumnSortType(3, LVCOLSORT_CUSTOM, new ColumnAdapters::SpeedDown()); |
---|
51 | SetColumnSortType(4, LVCOLSORT_CUSTOM, new ColumnAdapters::SpeedUp()); |
---|
52 | SetColumnSortType(5, LVCOLSORT_CUSTOM, new ColumnAdapters::Peers()); |
---|
53 | SetColumnSortType(6, LVCOLSORT_CUSTOM, new ColumnAdapters::Seeds()); |
---|
54 | SetColumnSortType(7, LVCOLSORT_CUSTOM, new ColumnAdapters::ETA()); |
---|
55 | SetColumnSortType(8, LVCOLSORT_CUSTOM, new ColumnAdapters::DistributedCopies()); |
---|
56 | SetColumnSortType(9, LVCOLSORT_CUSTOM, new ColumnAdapters::Tracker()); |
---|
57 | SetColumnSortType(10, LVCOLSORT_CUSTOM, new ColumnAdapters::UpdateTrackerIn()); |
---|
58 | SetColumnSortType(11, LVCOLSORT_CUSTOM, new ColumnAdapters::Ratio()); |
---|
59 | SetColumnSortType(12, LVCOLSORT_CUSTOM, new ColumnAdapters::TotalWanted()); |
---|
60 | SetColumnSortType(13, LVCOLSORT_CUSTOM, new ColumnAdapters::Completed()); |
---|
61 | SetColumnSortType(14, LVCOLSORT_CUSTOM, new ColumnAdapters::Remaining()); |
---|
62 | SetColumnSortType(15, LVCOLSORT_CUSTOM, new ColumnAdapters::Downloaded()); |
---|
63 | SetColumnSortType(16, LVCOLSORT_CUSTOM, new ColumnAdapters::Uploaded()); |
---|
64 | SetColumnSortType(17, LVCOLSORT_CUSTOM, new ColumnAdapters::ActiveTime()); |
---|
65 | SetColumnSortType(18, LVCOLSORT_CUSTOM, new ColumnAdapters::SeedingTime()); |
---|
66 | SetColumnSortType(19, LVCOLSORT_CUSTOM, new ColumnAdapters::StartTime()); |
---|
67 | SetColumnSortType(20, LVCOLSORT_CUSTOM, new ColumnAdapters::FinishTime()); |
---|
68 | } |
---|
69 | |
---|
70 | void HaliteListViewCtrl::OnDestroy() |
---|
71 | { |
---|
72 | saveSettings(); |
---|
73 | } |
---|
74 | |
---|
75 | void HaliteListViewCtrl::saveSettings() |
---|
76 | { |
---|
77 | GetListViewDetails(); |
---|
78 | Save(); |
---|
79 | } |
---|
80 | |
---|
81 | void HaliteListViewCtrl::uiUpdate(const hal::TorrentDetails& tD) |
---|
82 | { |
---|
83 | hal::try_update_lock<listClass> lock(*this); |
---|
84 | if (lock) |
---|
85 | { |
---|
86 | |
---|
87 | foreach (const hal::TorrentDetail_ptr td, tD.torrents()) |
---|
88 | { |
---|
89 | LV_FINDINFO findInfo; |
---|
90 | findInfo.flags = LVFI_STRING; |
---|
91 | findInfo.psz = const_cast<LPTSTR>(td->name().c_str()); |
---|
92 | |
---|
93 | int itemPos = FindItem(&findInfo, -1); |
---|
94 | if (itemPos < 0) |
---|
95 | itemPos = AddItem(0, 0, td->name().c_str(), 0); |
---|
96 | |
---|
97 | for (size_t i=1; i<NumberOfColumns_s; ++i) |
---|
98 | { |
---|
99 | SetItemText(itemPos, i, getColumnAdapter(i)->print(td).c_str()); |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | int iCol = GetSortColumn(); |
---|
104 | if (autoSort() && iCol >= 0 && iCol < m_arrColSortType.GetSize()) |
---|
105 | DoSortItems(iCol, IsSortDescending()); |
---|
106 | |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | HaliteListViewCtrl::tD HaliteListViewCtrl::CustomItemConversion(LVCompareParam* param, int iSortCol) |
---|
111 | { |
---|
112 | boost::array<wchar_t, MAX_PATH> buffer; |
---|
113 | GetItemText(param->iItem, 0, buffer.c_array(), buffer.size()); |
---|
114 | wstring torrent = buffer.data(); |
---|
115 | |
---|
116 | return hal::bittorrent().torrentDetails().get(torrent); |
---|
117 | } |
---|
118 | |
---|
119 | LRESULT HaliteListViewCtrl::OnResume(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled) |
---|
120 | { |
---|
121 | std::for_each(manager().allSelected().begin(), manager().allSelected().end(), |
---|
122 | bind((void (hal::bit::*)(const std::wstring&))&hal::bit::resumeTorrent, |
---|
123 | &hal::bittorrent(), _1)); |
---|
124 | |
---|
125 | return 0; |
---|
126 | } |
---|
127 | |
---|
128 | LRESULT HaliteListViewCtrl::OnPause(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled) |
---|
129 | { |
---|
130 | std::for_each(manager().allSelected().begin(), manager().allSelected().end(), |
---|
131 | bind((void (hal::bit::*)(const std::wstring&))&hal::bit::pauseTorrent, |
---|
132 | &hal::bittorrent(), _1)); |
---|
133 | |
---|
134 | return 0; |
---|
135 | } |
---|
136 | |
---|
137 | LRESULT HaliteListViewCtrl::OnStop(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled) |
---|
138 | { |
---|
139 | std::for_each(manager().allSelected().begin(), manager().allSelected().end(), |
---|
140 | bind((void (hal::bit::*)(const std::wstring&))&hal::bit::stopTorrent, |
---|
141 | &hal::bittorrent(), _1)); |
---|
142 | |
---|
143 | return 0; |
---|
144 | } |
---|
145 | |
---|
146 | LRESULT HaliteListViewCtrl::OnRemoveFocused(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled) |
---|
147 | { |
---|
148 | hal::bittorrent().removeTorrent(hal::to_utf8(manager_.selected())); |
---|
149 | |
---|
150 | clearFocused(); |
---|
151 | return 0; |
---|
152 | } |
---|
153 | |
---|
154 | LRESULT HaliteListViewCtrl::OnRemove(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled) |
---|
155 | { |
---|
156 | std::for_each(manager().allSelected().begin(), manager().allSelected().end(), |
---|
157 | bind((void (hal::bit::*)(const std::wstring&))&hal::bit::removeTorrent, |
---|
158 | &hal::bittorrent(), _1)); |
---|
159 | |
---|
160 | clearSelected(); |
---|
161 | return 0; |
---|
162 | } |
---|
163 | |
---|
164 | LRESULT HaliteListViewCtrl::OnRecheck(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled) |
---|
165 | { |
---|
166 | std::for_each(manager().allSelected().begin(), manager().allSelected().end(), |
---|
167 | bind((void (hal::bit::*)(const std::wstring&))&hal::bit::recheckTorrent, |
---|
168 | &hal::bittorrent(), _1)); |
---|
169 | |
---|
170 | return 0; |
---|
171 | } |
---|
172 | |
---|
173 | LRESULT HaliteListViewCtrl::OnRemoveWipeFiles(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled) |
---|
174 | { |
---|
175 | if(MessageBox(hal::app().res_wstr(HAL_LISTVIEW_CONFIRMDELETE).c_str(), |
---|
176 | hal::app().res_wstr(HAL_HALITE).c_str(), MB_YESNO) == IDYES) |
---|
177 | { |
---|
178 | std::for_each(manager().allSelected().begin(), manager().allSelected().end(), |
---|
179 | bind((void (hal::bit::*)(const std::wstring&))&hal::bit::removeTorrentWipeFiles, |
---|
180 | &hal::bittorrent(), _1)); |
---|
181 | |
---|
182 | clearSelected(); |
---|
183 | } |
---|
184 | return 0; |
---|
185 | } |
---|
186 | |
---|
187 | LRESULT HaliteListViewCtrl::OnDownloadFolder(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled) |
---|
188 | { |
---|
189 | HAL_DEV_MSG(L"OnDownloadFolder"); |
---|
190 | |
---|
191 | std::set<wpath> uniquePaths; |
---|
192 | |
---|
193 | for(std::set<wstring>::const_iterator i=manager().allSelected().begin(), e=manager().allSelected().end(); |
---|
194 | i != e; ++i) |
---|
195 | { |
---|
196 | wpath saveDir = hal::bittorrent().get(*i).save_directory; |
---|
197 | HAL_DEV_MSG(wformat(L"Name %1%, Save dir: %2%.") % *i % saveDir); |
---|
198 | |
---|
199 | uniquePaths.insert(saveDir); |
---|
200 | } |
---|
201 | |
---|
202 | SHELLEXECUTEINFO sei = { sizeof(SHELLEXECUTEINFO) }; |
---|
203 | |
---|
204 | for(std::set<wpath>::const_iterator i=uniquePaths.begin(), e=uniquePaths.end(); |
---|
205 | i != e; ++i) |
---|
206 | { |
---|
207 | wstring p = (*i).file_string(); |
---|
208 | |
---|
209 | HAL_DEV_MSG(wformat(L"Unique Save dir: %1%.") % p); |
---|
210 | |
---|
211 | sei.lpDirectory = p.c_str(); |
---|
212 | sei.lpFile = p.c_str(); |
---|
213 | sei.lpVerb = L"open"; |
---|
214 | sei.nShow = true; |
---|
215 | |
---|
216 | if (!::ShellExecuteEx(&sei)) |
---|
217 | HAL_DEV_MSG(L"Fail"); |
---|
218 | else |
---|
219 | HAL_DEV_MSG(L"Success"); |
---|
220 | } |
---|
221 | |
---|
222 | return 0; |
---|
223 | } |
---|
224 | |
---|
225 | LRESULT HaliteListViewCtrl::OnEditFolders(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled) |
---|
226 | { |
---|
227 | HAL_DEV_MSG(L"OnEditFolders"); |
---|
228 | |
---|
229 | if (hal::bit::torrent t = hal::bittorrent().get(manager_.selected())) |
---|
230 | { |
---|
231 | wstring saveDirectory = static_cast<wpath>(t.save_directory).native_file_string(); |
---|
232 | wstring moveToDirectory = static_cast<wpath>(t.move_to_directory).native_file_string(); |
---|
233 | |
---|
234 | bool useMoveTo = (L"" != moveToDirectory); |
---|
235 | bool disableSaveDir = !t.in_session; |
---|
236 | |
---|
237 | HaliteListViewAdjustDlg addTorrent(hal::app().res_wstr(HAL_ADDT_TITLE), saveDirectory, moveToDirectory, |
---|
238 | useMoveTo, disableSaveDir); |
---|
239 | |
---|
240 | if (IDOK == addTorrent.DoModal()) |
---|
241 | { |
---|
242 | if (!disableSaveDir) t.save_directory = saveDirectory; |
---|
243 | |
---|
244 | if (useMoveTo) |
---|
245 | t.move_to_directory = moveToDirectory; |
---|
246 | else |
---|
247 | t.move_to_directory = L""; |
---|
248 | } |
---|
249 | } |
---|
250 | |
---|
251 | return 0; |
---|
252 | } |
---|
253 | |
---|
254 | //LRESULT HaliteListViewCtrl::OnDeleteItem(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/) |
---|
255 | //{ |
---|
256 | // LPNMLISTVIEW pnmv=(LPNMLISTVIEW)pnmh; |
---|
257 | // T* pItem=(T*)GetItemData(pnmv->iItem); |
---|
258 | // ATLASSERT(pItem); |
---|
259 | // if (pItem) // Delete attached structure |
---|
260 | // delete pItem; |
---|
261 | // return 0; |
---|
262 | //} |
---|