-
-
Notifications
You must be signed in to change notification settings - Fork 412
/
view.cc
380 lines (294 loc) · 11.8 KB
/
view.cc
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// rTorrent - BitTorrent client
// Copyright (C) 2005-2011, Jari Sundell
//
// 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#include "config.h"
#include <algorithm>
#include <functional>
#include <rak/functional.h>
#include <rak/functional_fun.h>
#include <torrent/download.h>
#include <torrent/exceptions.h>
#include "rpc/parse_commands.h"
#include "rpc/object_storage.h"
#include "control.h"
#include "download.h"
#include "download_list.h"
#include "manager.h"
#include "view.h"
namespace core {
// Also add focus thingie here?
struct view_downloads_compare : std::binary_function<Download*, Download*, bool> {
view_downloads_compare(const torrent::Object& cmd) : m_command(cmd) {}
bool operator () (Download* d1, Download* d2) const {
try {
if (m_command.is_empty())
return false;
if (!m_command.is_dict_key())
return rpc::parse_command_single(rpc::make_target_pair(d1, d2), m_command.as_string()).as_value();
// torrent::Object tmp_command = m_command;
// uint32_t flags = tmp_command.flags() & torrent::Object::mask_function;
// tmp_command.unset_flags(torrent::Object::mask_function);
// tmp_command.set_flags((flags >> 1) & torrent::Object::mask_function);
// rpc::parse_command_execute(rpc::make_target_pair(d1, d2), &tmp_command);
// return rpc::commands.call_command(tmp_command.as_dict_key().c_str(), tmp_command.as_dict_obj(),
// rpc::make_target_pair(d1, d2)).as_value();
return rpc::commands.call_command(m_command.as_dict_key().c_str(), m_command.as_dict_obj(),
rpc::make_target_pair(d1, d2)).as_value();
} catch (torrent::input_error& e) {
control->core()->push_log(e.what());
return false;
}
}
const torrent::Object& m_command;
};
struct view_downloads_filter : std::unary_function<Download*, bool> {
view_downloads_filter(const torrent::Object& cmd, const torrent::Object& cmd2) : m_command(cmd), m_command2(cmd2) {}
bool operator () (Download* d1) const {
return this->evalCmd(m_command, d1) && this->evalCmd(m_command2, d1);
}
bool evalCmd(const torrent::Object& cmd, Download* d1) const {
if (cmd.is_empty())
return true;
try {
torrent::Object result;
if (cmd.is_dict_key()) {
// torrent::Object tmp_command = cmd;
// uint32_t flags = tmp_command.flags() & torrent::Object::mask_function;
// tmp_command.unset_flags(torrent::Object::mask_function);
// tmp_command.set_flags((flags >> 1) & torrent::Object::mask_function);
// rpc::parse_command_execute(rpc::make_target(d1), &tmp_command);
// result = rpc::commands.call_command(tmp_command.as_dict_key().c_str(), tmp_command.as_dict_obj(),
// rpc::make_target(d1));
result = rpc::commands.call_command(cmd.as_dict_key().c_str(), cmd.as_dict_obj(), rpc::make_target(d1));
} else {
result = rpc::parse_command_single(rpc::make_target(d1), cmd.as_string());
}
switch (result.type()) {
// case torrent::Object::TYPE_RAW_BENCODE: return !result.as_raw_bencode().empty();
case torrent::Object::TYPE_VALUE: return result.as_value();
case torrent::Object::TYPE_STRING: return !result.as_string().empty();
case torrent::Object::TYPE_LIST: return !result.as_list().empty();
case torrent::Object::TYPE_MAP: return !result.as_map().empty();
default: return false;
}
// The default filter action is to return true, to not filter
// the download out.
return true;
} catch (torrent::input_error& e) {
control->core()->push_log(e.what());
return false;
}
}
const torrent::Object& m_command;
const torrent::Object& m_command2;
};
void
View::emit_changed() {
priority_queue_erase(&taskScheduler, &m_delayChanged);
priority_queue_insert(&taskScheduler, &m_delayChanged, cachedTime);
}
void
View::emit_changed_now() {
for (signal_void::iterator itr = m_signal_changed.begin(), last = m_signal_changed.end(); itr != last; itr++)
(*itr)();
}
View::~View() {
if (m_name.empty())
return;
clear_filter_on();
priority_queue_erase(&taskScheduler, &m_delayChanged);
}
void
View::initialize(const std::string& name) {
if (!m_name.empty())
throw torrent::internal_error("View::initialize(...) called on an already initialized view.");
if (name.empty())
throw torrent::internal_error("View::initialize(...) called with an empty name.");
core::DownloadList* dlist = control->core()->download_list();
m_name = name;
// Urgh, wrong. No filtering being done.
std::for_each(dlist->begin(), dlist->end(), rak::bind1st(std::mem_fun(&View::push_back), this));
m_size = base_type::size();
m_focus = 0;
set_last_changed(rak::timer());
m_delayChanged.slot() = std::bind(&View::emit_changed_now, this);
}
void
View::erase(Download* download) {
iterator itr = std::find(base_type::begin(), base_type::end(), download);
if (itr >= end_visible()) {
erase_internal(itr);
} else {
erase_internal(itr);
rpc::call_object_nothrow(m_event_removed, rpc::make_target(download));
}
}
void
View::set_visible(Download* download) {
iterator itr = std::find(begin_filtered(), end_filtered(), download);
if (itr == end_filtered())
return;
// Don't optimize erase since we want to keep the order of the
// non-visible elements.
base_type::erase(itr);
insert_visible(download);
rpc::call_object_nothrow(m_event_added, rpc::make_target(download));
}
void
View::set_not_visible(Download* download) {
iterator itr = std::find(begin_visible(), end_visible(), download);
if (itr == end_visible())
return;
m_size--;
m_focus -= (m_focus > position(itr));
// Don't optimize erase since we want to keep the order of the
// non-visible elements.
base_type::erase(itr);
base_type::push_back(download);
rpc::call_object_nothrow(m_event_removed, rpc::make_target(download));
}
void
View::next_focus() {
if (empty())
return;
m_focus = (m_focus + 1) % (size() + 1);
emit_changed();
}
void
View::prev_focus() {
if (empty())
return;
m_focus = (m_focus - 1 + size() + 1) % (size() + 1);
emit_changed();
}
void
View::sort() {
Download* curFocus = focus() != end_visible() ? *focus() : NULL;
// Don't go randomly switching around equivalent elements.
std::stable_sort(begin(), end_visible(), view_downloads_compare(m_sortCurrent));
m_focus = position(std::find(begin(), end_visible(), curFocus));
emit_changed();
}
void
View::filter() {
// Do NOT allow filter STARTED and STOPPED views: they are special
if (m_name == "started" || m_name == "stopped")
return;
// Parition the list in two steps so we know which elements changed.
iterator splitVisible = std::stable_partition(begin_visible(), end_visible(), view_downloads_filter(m_filter, m_temp_filter));
iterator splitFiltered = std::stable_partition(begin_filtered(), end_filtered(), view_downloads_filter(m_filter, m_temp_filter));
base_type changed(splitVisible, splitFiltered);
iterator splitChanged = changed.begin() + std::distance(splitVisible, end_visible());
m_size = std::distance(begin(), std::copy(splitChanged, changed.end(), splitVisible));
std::copy(changed.begin(), splitChanged, begin_filtered());
// Fix this...
m_focus = std::min(m_focus, m_size);
// The commands are allowed to remove itself from or change View
// sorting since the commands are being called on the 'changed'
// vector. But this will cause undefined behavior if elements are
// removed.
//
// Consider if View should lock itself (and throw) if erase events
// are triggered on a Download in the 'changed' list. This can be
// done by using a base_type* member variable, and making sure we
// set the elements to NULL as we trigger commands on them. Or
// perhaps always clear them, thus not throwing anything.
if (!m_event_removed.is_empty())
std::for_each(changed.begin(), splitChanged,
std::bind(&rpc::call_object_d_nothrow, m_event_removed, std::placeholders::_1));
if (!m_event_added.is_empty())
std::for_each(changed.begin(), splitChanged,
std::bind(&rpc::call_object_d_nothrow, m_event_added, std::placeholders::_1));
emit_changed();
}
void
View::filter_by(const torrent::Object& condition, View::base_type& result) {
// std::copy_if(begin_visible(), end_visible(), result.begin(), view_downloads_filter(condition));
view_downloads_filter matches = view_downloads_filter(condition, m_temp_filter);
for (iterator itr = begin_visible(); itr != end_visible(); ++itr)
if (matches(*itr))
result.push_back(*itr);
}
void
View::filter_download(core::Download* download) {
iterator itr = std::find(base_type::begin(), base_type::end(), download);
if (itr == base_type::end())
throw torrent::internal_error("View::filter_download(...) could not find download.");
if (view_downloads_filter(m_filter, m_temp_filter)(download)) {
if (itr >= end_visible()) {
erase_internal(itr);
insert_visible(download);
rpc::call_object_nothrow(m_event_added, rpc::make_target(download));
} else {
// This makes sure the download is sorted even if it is
// already visible.
//
// Consider removing this.
erase_internal(itr);
insert_visible(download);
}
} else {
if (itr >= end_visible())
return;
erase_internal(itr);
base_type::push_back(download);
rpc::call_object_nothrow(m_event_removed, rpc::make_target(download));
}
emit_changed();
}
void
View::set_filter_on_event(const std::string& event) {
control->object_storage()->set_str_multi_key(event, "!view." + m_name, "view.filter_download=" + m_name);
}
void
View::clear_filter_on() {
control->object_storage()->rlookup_clear("!view." + m_name);
}
inline void
View::insert_visible(Download* d) {
iterator itr = std::find_if(begin_visible(), end_visible(), std::bind1st(view_downloads_compare(m_sortNew), d));
m_size++;
m_focus += (m_focus >= position(itr));
base_type::insert(itr, d);
}
inline void
View::erase_internal(iterator itr) {
if (itr == end_filtered())
throw torrent::internal_error("View::erase_visible(...) iterator out of range.");
m_size -= (itr < end_visible());
m_focus -= (m_focus > position(itr));
base_type::erase(itr);
}
}