-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSWappSearchFilesPage.cpp
354 lines (291 loc) · 10 KB
/
RSWappSearchFilesPage.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
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
#include <Wt/WContainerWidget>
#include <Wt/WAbstractTableModel>
#include <Wt/WVBoxLayout>
#include <Wt/WHBoxLayout>
#include <Wt/WTreeView>
#include <Wt/WTableView>
#include <Wt/WTextArea>
#include <Wt/WTextEdit>
#include <Wt/WPushButton>
#include <Wt/WModelIndex>
#include <Wt/WTimer>
#include <Wt/WLineEdit>
#include <Wt/WCheckBox>
#include "RSWappSearchFilesPage.h"
#include <retroshare/rsfiles.h>
#include <retroshare/rstypes.h>
#include <retroshare/rsturtle.h>
#include <retroshare/rsnotify.h>
#define COLUMN_FILENAME 0
#define COLUMN_SIZE 1
#define COLUMN_AGE 2
#define COLUMN_FLAGS 3
#define COLUMN_GROUPS 4
static Wt::WString make_big_number(uint64_t n)
{
if(n >= 1000000)
return Wt::WString("{1}{2}").arg((int)(n/1000000)).arg((int)(n%1000000)) ;
else
return Wt::WString("{1}").arg((int)n) ;
}
class LocalSearchFilesModel: public Wt::WAbstractTableModel
{
public:
LocalSearchFilesModel(RsFiles *mfiles,Wt::WObject *parent = 0)
: Wt::WAbstractTableModel(parent), mFiles(mfiles),_mtx("LocalSearchFilesModel")
{
}
virtual int rowCount(const Wt::WModelIndex& parent = Wt::WModelIndex()) const
{
RsStackMutex mtx(_mtx) ;
if (!parent.isValid())
return _searchResults.size() ;
else
return 0;
}
virtual int columnCount(const Wt::WModelIndex& parent = Wt::WModelIndex()) const
{
if (!parent.isValid())
return 6;
else
return 0;
}
virtual boost::any data(const Wt::WModelIndex& index, int role = Wt::DisplayRole) const
{
RsStackMutex mtx(_mtx) ;
std::cerr << "data row: " << index.row() << std::endl;
if(index.column() >= 6 || index.row() >= (int)_searchResults.size())
return boost::any();
//DirDetails dd;
//dd.count;
switch (role)
{
case Wt::DisplayRole:
switch(index.column())
{
case COLUMN_FILENAME : return Wt::WString(_searchResults[index.row()].name) ;
case COLUMN_SIZE : return make_big_number(_searchResults[index.row()].count) ;
case COLUMN_AGE: return make_big_number(_searchResults[index.row()].age);
default:
return boost::any();
}
case Wt::UserRole:
switch(index.column())
{
default: return Wt::WString(_searchResults[index.row()].hash) ;
}
case Wt::ToolTipRole:
Wt::WString(_searchResults[index.row()].hash) ;
default:
return boost::any();
}
}
virtual boost::any headerData(int section, Wt::Orientation orientation = Wt::Horizontal, int role = Wt::DisplayRole) const
{
RsStackMutex mtx(_mtx) ;
static Wt::WString col_names[6] = {
Wt::WString("File Name"),
Wt::WString("Size"),
Wt::WString("Age"),
Wt::WString("*"),
Wt::WString("*"),
Wt::WString("*") } ;
//updateTransfersList() ;
if (orientation == Wt::Horizontal)
switch (role)
{
case Wt::DisplayRole:
return col_names[section] ;
default:
return boost::any();
}
else
return boost::any();
}
std::list<DirDetails> getItems(std::list<int> jobList) const{
std::list<DirDetails> items;
for(std::list<int>::iterator resultsIter = jobList.begin(); resultsIter != jobList.end(); resultsIter ++)
{
int index = *resultsIter;
items.push_back(_searchResults[index]) ;
}
return items;
}
void displayList(const std::list<DirDetails>& dList) const
{
std::cerr << "Updating search model rows=" << rowCount() << ", columns=" << columnCount() << std::endl;
RsStackMutex mtx(_mtx) ;
_searchResults.clear() ;
DirDetails dd;
for(std::list<DirDetails>::const_iterator resultsIter = dList.begin(); resultsIter != dList.end(); resultsIter ++)
{
std::cerr << "RSWUI search result: " << dd.hash << std::endl;
dd = *resultsIter;
_searchResults.push_back(dd) ;
}
}
void addToList(uint32_t id,const std::list<DirDetails>& lst)
{
{
RsStackMutex mtx(_mtx) ;
for(std::list<DirDetails>::const_iterator it(lst.begin());it!=lst.end();++it)
_searchResults.push_back(*it) ;
}
}
virtual void refresh()
{
std::cerr << "Updating search model rows=" << rowCount() << ", columns=" << columnCount() << std::endl;
dataChanged().emit(index(0,0),index(rowCount(),columnCount())) ;
}
private:
mutable std::vector<DirDetails> _searchResults ;
RsFiles *mFiles ;
mutable RsMutex _mtx ;
};
RSWappSearchFilesPage::RSWappSearchFilesPage(Wt::WContainerWidget *parent,RsFiles *mfiles)
: WCompositeWidget(parent),mFiles(mfiles)
{
setImplementation(_impl = new Wt::WContainerWidget()) ;
//_treeView = new Wt::WTreeView(_impl);
_tableView = new Wt::WTableView(_impl);
Wt::WVBoxLayout *layout = new Wt::WVBoxLayout() ;
_impl->setLayout(layout) ;
search_box = new Wt::WLineEdit(_impl) ;
search_box->setText("mp3") ;
search_box->enterPressed().connect(this,&RSWappSearchFilesPage::searchClicked) ;
//search_box->setHeight(50) ;
localcb = new Wt::WCheckBox(Wt::WString("Search Local"),_impl) ;
remotecb = new Wt::WCheckBox(Wt::WString("Search Remote"),_impl) ;
distantcb = new Wt::WCheckBox(Wt::WString("Search Distant"),_impl) ;
localcb->setChecked(false);
remotecb->setChecked(true);
distantcb->setChecked(true);
Wt::WPushButton *btn = new Wt::WPushButton("Search!") ;
btn->clicked().connect(this,&RSWappSearchFilesPage::searchClicked) ;
Wt::WContainerWidget *hSearchBox = new Wt::WContainerWidget();
Wt::WHBoxLayout *hSearchLayout = new Wt::WHBoxLayout ;
hSearchBox->setLayout(hSearchLayout);
hSearchLayout->addWidget(search_box) ;
hSearchLayout->addWidget(localcb);
hSearchLayout->addWidget(distantcb);
hSearchLayout->addWidget(remotecb);
hSearchLayout->addWidget(btn) ;
layout->addWidget(hSearchBox) ;
search_box->setWidth(1000);
_tableView->setAlternatingRowColors(true);
_tableView->setSelectionMode(Wt::ExtendedSelection);
_tableView->setDragEnabled(true);
_tableView->setColumnWidth(0, 250);
_tableView->setColumnWidth(1, 150);
_tableView->setColumnWidth(2, 250);
_tableView->setColumnWidth(3, 150);
_tableView->setColumnWidth(4, 150);
_tableView->setColumnWidth(5, 100);
_shared_files_model = new LocalSearchFilesModel(mfiles) ;
_tableView->setModel(_shared_files_model) ;
_tableView->doubleClicked().connect(this,&RSWappSearchFilesPage::tableClicked) ;
layout->addWidget(_tableView,1) ;
_tableView->setHeight(300) ;
Wt::WPushButton *dlbtn = new Wt::WPushButton("Download selected") ;
dlbtn->clicked().connect(this,&RSWappSearchFilesPage::searchClicked) ;
layout->addWidget(dlbtn) ;
_timer = new Wt::WTimer(this) ;
_timer->setInterval(5000) ;
_timer->timeout().connect(this,&RSWappSearchFilesPage::refresh) ;
_timer->start() ;
}
void RSWappSearchFilesPage::tableClicked()
{
//_tableView->selectedIndexes().begin().
Wt::WModelIndex index;
std::list<int> jobList;
const Wt::WModelIndexSet selectedRows = _tableView->selectedIndexes();
for (Wt::WModelIndexSet::iterator i = selectedRows.begin();
i != selectedRows.end(); ++i) {
index = *i;
jobList.push_back(index.row());
}
DirDetails dd;
std::list<DirDetails> items = _shared_files_model->getItems(jobList);
for (std::list<DirDetails>::iterator i = items.begin();
i != items.end(); ++i) {
dd = *i;
FileInfo finfo ;
rsFiles->FileDetails(dd.hash, RS_FILE_HINTS_REMOTE, finfo) ;
std::list<std::string> srcIds;
for(std::list<TransferInfo>::const_iterator it(finfo.peers.begin());it!=finfo.peers.end();++it)
{
srcIds.push_back((*it).peerId) ;
}
if (rsFiles->FileRequest(dd.name, dd.hash, dd.count, "", RS_FILE_REQ_ANONYMOUS_ROUTING, srcIds)) {
std::cerr << "\n\n DOWNLOADING: " << dd.name << ", " << dd.hash << ", " << dd.count <<std::endl;
} else {
std::cerr << "\n\n SKIPDL: " << dd.name << ", " << dd.hash << ", " << dd.count <<std::endl;
//fileExist.append(link.name());
}
}
//dryRunSignal().emit(jobList);
}
void RSWappSearchFilesPage::searchClicked()
{
std::cerr << "SEARCHCLICKED"<<std::endl;
std::string lstr = search_box->text().toUTF8() ;
std::istringstream iss(lstr);
/*std::vector<std::string> tokens;
tokens = {std::istream_iterator<std::string>{iss},
std::istream_iterator<std::string>{}};*/
std::list<std::string> strings;
std::string s;
while (std::getline(iss, s, ' ')) {
std::cout << s << std::endl;
strings.push_back(s);
}
if(distantcb->checkState())
{
TurtleRequestId req_id ;
std::cerr << "Init turtle search" << std::endl;
req_id = rsTurtle->turtleSearch(strings.front()) ;
std::cerr << "New turtle search id = " << std::hex << req_id << std::dec << std::endl;
}
std::list<DirDetails> results;
if(localcb->checkState()){
FileSearchFlags fsf = RS_FILE_HINTS_LOCAL;
std::list<DirDetails> initialResults;
mFiles->SearchKeywords(strings,initialResults,fsf);
std::cerr << "RESULTLEN: "<< initialResults.size() <<std::endl;
for (std::list<DirDetails>::iterator i = initialResults.begin(); i != initialResults.end(); ++i)
results.push_back(*i);
}
if(remotecb->checkState()){
FileSearchFlags fsf = RS_FILE_HINTS_REMOTE;
std::list<DirDetails> initialResults;
mFiles->SearchKeywords(strings,initialResults,fsf);
std::cerr << "RESULTLEN: "<< initialResults.size() <<std::endl;
for (std::list<DirDetails>::iterator i = initialResults.begin(); i != initialResults.end(); ++i)
results.push_back(*i);
}
_shared_files_model->displayList(results);
refresh();
}
void RSWappSearchFilesPage::refresh() {
_shared_files_model->refresh();
_tableView->refresh();
_tableView->setHeight(300);//Without this line - wt wont update for me (wt3.3)
std::cerr << "Refreshing search page." << std::endl;
}
void RSWappSearchFilesPage::notifyTurtleSearchResult(uint32_t search_id,const std::list<TurtleFileInfo>& files)
{
std::list<DirDetails> dfiles ;
for(std::list<TurtleFileInfo>::const_iterator it(files.begin());it!=files.end();++it)
{
DirDetails d ;
d.hash = (*it).hash ;
d.count = (*it).size ;
d.age = 0 ;
d.name = (*it).name ;
dfiles.push_back(d) ;
std::cerr << "notifySearchResult: hash=" << d.hash << ", size=" << d.count << ", name=" << d.name << std::endl;
}
std::cerr << "added new items to model..." << std::endl;
_shared_files_model->addToList(search_id,dfiles) ;
}