forked from Tribler/tribler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTopSearchPanel.py
463 lines (387 loc) · 18.7 KB
/
TopSearchPanel.py
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# Written by Niels Zeilemaker
import os
import sys
import logging
import wx.animate
from Tribler import LIBRARYNAME
from Tribler.Core.simpledefs import NTFY_TORRENTS
from Tribler.Main.Utility.GuiDBTuples import CollectedTorrent, Torrent
from Tribler.Main.Utility.GuiDBHandler import startWorker, cancelWorker
from Tribler.Main.vwxGUI import forceWxThread, TRIBLER_RED, SEPARATOR_GREY, GRADIENT_LGREY, GRADIENT_DGREY
from Tribler.Main.vwxGUI.GuiUtility import GUIUtility
from Tribler.Main.vwxGUI.GuiImageManager import GuiImageManager
from Tribler.Main.vwxGUI.widgets import ActionButton, FancyPanel, TextCtrlAutoComplete, ProgressButton
from Tribler.Main.Dialogs.AddTorrent import AddTorrent
from Tribler.Main.Dialogs.RemoveTorrent import RemoveTorrent
class TopSearchPanel(FancyPanel):
def __init__(self, parent):
self._logger = logging.getLogger(self.__class__.__name__)
self._logger.debug("TopSearchPanel: __init__")
self.loaded_bitmap = None
self.guiutility = GUIUtility.getInstance()
self.utility = self.guiutility.utility
self.installdir = self.utility.getPath()
self.tdb = self.utility.session.open_dbhandler(NTFY_TORRENTS)
self.collectedTorrents = {}
FancyPanel.__init__(self, parent, border=wx.BOTTOM)
self.SetBorderColour(SEPARATOR_GREY)
self.SetBackgroundColour(GRADIENT_LGREY, GRADIENT_DGREY)
self.AddComponents()
self.Bind(wx.EVT_SIZE, self.OnResize)
def AddComponents(self):
self.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT))
self._logger.debug("TopSearchPanel: OnCreate")
gui_image_manager = GuiImageManager.getInstance()
# TODO martijn: on OS X, the wx.SearchCtrl element is bugged (doesn't show a placeholder unless focussed).
# for now, we replaced the occurrences of wx.SearchCtrl with wx.TextCtrl elements.
if sys.platform == 'darwin':
self.searchField = wx.TextCtrl(self, -1, style=wx.TE_PROCESS_ENTER)
self.searchField.Bind(wx.EVT_TEXT_ENTER, self.OnSearchKeyDown)
self.searchField.SetHint('Search Files or Channels')
self.searchField.SetMinSize((400, 22))
else:
self.searchFieldPanel = FancyPanel(self, radius=5, border=wx.ALL)
self.searchFieldPanel.SetBorderColour(SEPARATOR_GREY, highlight=TRIBLER_RED)
self.searchField = TextCtrlAutoComplete(self.searchFieldPanel, style=wx.NO_BORDER,
entrycallback=self.complete)
# Since we have set the style to wx.NO_BORDER, the default height will be
# too large. Therefore, we need to set the correct height.
_, height = self.GetTextExtent("Gg")
self.searchField.SetMinSize((-1, height))
self.searchFieldPanel.SetMinSize((400, 25))
self.searchFieldPanel.SetBackgroundColour(self.searchField.GetBackgroundColour())
self.searchField.Bind(wx.EVT_KILL_FOCUS, self.searchFieldPanel.OnKillFocus)
self.searchField.Bind(wx.EVT_TEXT_ENTER, self.OnSearchKeyDown)
self.go = ProgressButton(self, -1)
self.go.SetMinSize((50, 25))
self.go.Bind(wx.EVT_LEFT_UP, self.OnSearchKeyDown)
ag_fname = os.path.join(self.guiutility.utility.getPath(),
LIBRARYNAME, 'Main', 'vwxGUI', 'images', 'search_new.gif')
self.ag = wx.animate.GIFAnimationCtrl(self, -1, ag_fname)
self.ag.UseBackgroundColour(True)
self.ag.SetBackgroundColour(wx.Colour(244, 244, 244))
self.ag.Hide()
download_bmp = gui_image_manager.getImage(u"download.png")
self.download_btn = ActionButton(self, -1, download_bmp)
self.download_btn.Enable(False)
upload_bmp = gui_image_manager.getImage(u"upload.png")
self.upload_btn = ActionButton(self, -1, upload_bmp)
self.upload_btn.Enable(False)
stop_bmp = gui_image_manager.getImage(u"pause.png")
self.stop_btn = ActionButton(self, -1, stop_bmp)
self.stop_btn.Enable(False)
delete_bmp = gui_image_manager.getImage(u"delete.png")
self.delete_btn = ActionButton(self, -1, delete_bmp)
self.delete_btn.Enable(False)
play_bmp = gui_image_manager.getImage(u"play.png")
self.play_btn = ActionButton(self, -1, play_bmp)
self.play_btn.Enable(False)
add_bmp = gui_image_manager.getImage(u"add.png")
self.add_btn = ActionButton(self, -1, add_bmp)
self.SetButtonHandler(self.add_btn, self.OnAdd, 'Download an external torrent.')
settings_bmp = gui_image_manager.getImage(u"settings.png")
self.settings_btn = ActionButton(self, -1, settings_bmp)
self.SetButtonHandler(self.settings_btn, self.OnSettings, 'Change settings.')
mainSizer = wx.BoxSizer(wx.HORIZONTAL)
if sys.platform != 'darwin':
vSizer = wx.BoxSizer(wx.VERTICAL)
vSizer.AddStretchSpacer()
vSizer.Add(self.searchField, 0, wx.EXPAND | wx.RESERVE_SPACE_EVEN_IF_HIDDEN | wx.LEFT | wx.RIGHT, 5)
vSizer.AddStretchSpacer()
self.searchFieldPanel.SetSizer(vSizer)
vSizer.Layout()
# Add searchbox etc.
self.searchSizer = wx.BoxSizer(wx.VERTICAL)
searchBoxSizer = wx.BoxSizer(wx.HORIZONTAL)
if sys.platform == 'darwin':
searchBoxSizer.Add(self.searchField, 1, wx.CENTER | wx.RESERVE_SPACE_EVEN_IF_HIDDEN)
else:
searchBoxSizer.Add(self.searchFieldPanel, 1, wx.CENTER | wx.RESERVE_SPACE_EVEN_IF_HIDDEN)
searchBoxSizer.Add(self.go, 0, wx.CENTER | wx.LEFT | wx.RESERVE_SPACE_EVEN_IF_HIDDEN, 5) # add searchbutton
searchBoxSizer.Add(self.ag, 0, wx.CENTER | wx.LEFT | wx.RESERVE_SPACE_EVEN_IF_HIDDEN, 5) # add animation
self.searchSizer.Add(searchBoxSizer, 1, wx.EXPAND)
# finished searchSizer, add to mainSizer
mainSizer.Add(self.searchSizer, 0, wx.EXPAND | wx.LEFT, 10)
mainSizer.AddSpacer((40, 0))
# add buttons
buttonSizer = wx.BoxSizer(wx.HORIZONTAL)
buttonSizer.Add(self.download_btn, 0, wx.CENTER | wx.RIGHT, 5)
buttonSizer.Add(self.upload_btn, 0, wx.CENTER | wx.RIGHT, 5)
buttonSizer.Add(self.stop_btn, 0, wx.CENTER | wx.RIGHT, 5)
buttonSizer.Add(self.delete_btn, 0, wx.CENTER | wx.RIGHT, 5)
buttonSizer.Add(self.play_btn, 0, wx.CENTER | wx.RIGHT, 5)
buttonSizer.AddSpacer((35, 0))
buttonSizer.AddStretchSpacer()
buttonSizer.Add(self.add_btn, 0, wx.CENTER | wx.RIGHT, 5)
buttonSizer.Add(self.settings_btn, 0, wx.CENTER | wx.RIGHT, 5)
mainSizer.Add(buttonSizer, 1, wx.EXPAND)
self.SetSizer(mainSizer)
self.Layout()
def OnResize(self, event):
self.Refresh()
event.Skip()
def OnSearchKeyDown(self, event=None):
if self.go.IsEnabled():
self._logger.debug("TopSearchPanel: OnSearchKeyDown")
if getattr(self.searchField, 'ShowDropDown', False):
self.searchField.ShowDropDown(False)
self.guiutility.dosearch()
self.go.Enable(False)
wx.CallLater(2500, self.go.Enable, True)
def OnSettings(self, event):
self.guiutility.ShowPage('settings')
def OnAdd(self, event):
dlg = AddTorrent(None, self.guiutility.frame)
dlg.CenterOnParent()
dlg.ShowModal()
dlg.Destroy()
def OnStats(self, event):
self.guiutility.ShowPage('stats')
def StartSearch(self):
if getattr(self.searchField, 'ShowDropDown', False):
self.searchField.ShowDropDown(False)
self.guiutility.frame.searchlist.ResetBottomWindow()
self.Freeze()
self.go.SetValue(0)
self.guiutility.frame.top_bg.ag.Play()
self.guiutility.frame.top_bg.ag.Show()
self.Thaw()
def ShowSearching(self, max):
if not self or not self.go:
return
self.go.SetRange(max + 16)
cancelWorker(u"FakeResult")
startWorker(None, self.FakeResult, uId=u"FakeResult", delay=0.25, workerType="ThreadPool")
@forceWxThread
def FakeResult(self, times=1):
if not self:
return
newValue = min(self.go.GetValue() + 1, self.go.GetRange())
if times < 16:
self.go.SetValue(newValue)
startWorker(None, self.FakeResult, wargs=(times + 1,), uId=u"FakeResult", delay=0.25,
workerType="ThreadPool")
def NewResult(self):
maxValue = self.go.GetRange()
newValue = min(self.go.GetValue() + 1, maxValue)
self.guiutility.frame.top_bg.go.SetValue(newValue)
if newValue == maxValue:
return True
return False
def SetFinished(self):
self.Freeze()
self.ag.Stop()
self.ag.Hide()
self.go.SetValue(self.go.GetRange())
self.Layout()
self.Thaw()
def complete(self, term):
ignore_list = ["http://", "https://", "magnet:"]
for ignore in ignore_list:
if term.startswith(ignore):
return []
"""autocompletes term."""
if len(term) > 1:
return self.tdb.getAutoCompleteTerms(term, max_terms=7)
return []
def SearchFocus(self):
if self.guiutility.guiPage == 'home':
if getattr(self.GetParent(), 'home', False):
self.GetParent().home.SearchFocus()
else:
self.searchField.SetFocus()
self.searchField.SelectAll()
def AddCollectedTorrent(self, coltorrent):
self.collectedTorrents[coltorrent.infohash] = coltorrent
self.TorrentsChanged()
def GetSelectedTorrents(self):
torrents = None
page = self.guiutility.guiPage
if page in ['search_results', 'selectedchannel', 'playlist', 'my_files']:
list = self.guiutility.GetSelectedPage()
items = list.GetExpandedItems()
torrents = [item[1].original_data for item in items if isinstance(item[1].original_data, Torrent)
or isinstance(item[1].original_data, CollectedTorrent)]
return torrents
def TorrentsChanged(self):
self.RefreshTorrents(self.GetSelectedTorrents())
def RefreshTorrents(self, torrents):
inDownloads = self.guiutility.guiPage == 'my_files'
if torrents:
isMultiple = len(torrents) > 1
usedCollectedTorrents = set()
# we have 7 different states, able to resume seeding, resume downloading,
# download, stop seeding, stop downloading, delete, or play
# TODO(emilon): This is so ugly. At least we should use a named tuple.
states = [0, 0, 0, 0, 0, 0, 0]
for torrent in torrents:
if 'stopped' in torrent.state:
if 'completed' in torrent.state:
states[0] += 1
else:
states[1] += 1
elif not torrent.state:
states[2] += 1
if 'active' in torrent.state:
if 'completed' in torrent.state:
states[3] += 1
else:
states[4] += 1
if torrent.state or inDownloads:
states[5] += 1
if "metadata" not in torrent.state and torrent.infohash in self.collectedTorrents:
coltorrent = self.collectedTorrents[torrent.infohash]
if coltorrent.isPlayable():
states[6] += 1
usedCollectedTorrents.add(torrent.infohash)
else:
# If the torrent isn't collected we assume its playable and let the core cancel the VOD if it isn't.
states[6] += 1
enableDownload = states[1] + states[2]
if enableDownload:
if isMultiple:
self.SetButtonHandler(
self.download_btn,
self.OnDownload,
'Resume downloading %d torrent(s).' %
enableDownload)
elif states[1]:
self.SetButtonHandler(self.download_btn, self.OnResume, 'Resume downloading this torrent.')
else:
self.SetButtonHandler(self.download_btn, self.OnDownload, 'Start downloading this torrent.')
else:
self.SetButtonHandler(self.download_btn, None)
enableUpload = states[0]
if enableUpload:
if isMultiple:
self.SetButtonHandler(
self.upload_btn,
self.OnUpload,
'Resume seeding %d torrent(s).' %
enableUpload)
else:
self.SetButtonHandler(self.upload_btn, self.OnUpload, 'Resume seeding this torrent.')
else:
self.SetButtonHandler(self.upload_btn, None)
enableStop = states[3] + states[4]
if enableStop:
if isMultiple:
self.SetButtonHandler(self.stop_btn, self.OnStop, 'Stop %d torrent(s).' % enableStop)
elif states[3]:
self.SetButtonHandler(self.stop_btn, self.OnStop, 'Stop seeding this torrent.')
else:
self.SetButtonHandler(self.stop_btn, self.OnStop, 'Stop downloading this torrent.')
else:
self.SetButtonHandler(self.stop_btn, None)
if states[5] > 1:
self.SetButtonHandler(self.delete_btn, self.OnDelete, 'Delete %d torrent(s).' % states[5])
elif states[5]:
self.SetButtonHandler(self.delete_btn, self.OnDelete, 'Delete this torrent.')
else:
self.SetButtonHandler(self.delete_btn, None)
if isMultiple:
self.SetButtonHandler(self.play_btn, self.OnPlay, 'Start playing one of the selected torrents.')
elif states[6]:
self.SetButtonHandler(self.play_btn, self.OnPlay, 'Start playing this torrent.')
else:
self.SetButtonHandler(self.play_btn, None)
for infohash in self.collectedTorrents.keys():
if infohash not in usedCollectedTorrents:
del self.collectedTorrents[infohash]
else:
self.ClearButtonHandlers()
def SetButtonHandler(self, button, handler=None, tooltip=None):
button.Enable(bool(handler))
if handler:
button.Bind(wx.EVT_LEFT_UP, handler)
if tooltip and sys.platform != 'darwin':
button.SetToolTipString(tooltip)
else:
button.SetToolTip(None)
else:
button.SetToolTip(None)
def ClearButtonHandlers(self):
self.SetButtonHandler(self.download_btn, None)
self.SetButtonHandler(self.upload_btn, None)
self.SetButtonHandler(self.play_btn, None)
self.SetButtonHandler(self.stop_btn, None)
self.SetButtonHandler(self.delete_btn, None)
def OnDownload(self, event=None, torrents=None):
refresh_library = False
torrents = torrents if torrents is not None else self.GetSelectedTorrents()
for torrent in torrents:
if 'stopped' in torrent.state:
self.guiutility.library_manager.resumeTorrent(torrent)
else:
if self.guiutility.frame.selectedchannellist.IsShownOnScreen():
self.guiutility.frame.selectedchannellist.StartDownload(torrent)
else:
self.guiutility.torrentsearch_manager.downloadTorrent(torrent)
refresh_library = True
if event:
button = event.GetEventObject()
button.Enable(False)
wx.CallLater(3000, button.Enable, True)
if refresh_library:
wx.CallLater(1000, self.guiutility.frame.librarylist.do_or_schedule_refresh, True)
def OnUpload(self, event):
for torrent in self.GetSelectedTorrents():
if 'completed' in torrent.state:
self.guiutility.library_manager.resumeTorrent(torrent)
if event:
button = event.GetEventObject()
button.Enable(False)
def OnPlay(self, event):
# Select the first playable torrent or not collected torrent. Return if none can be found
torrent = None
for t in self.GetSelectedTorrents():
if t.infohash in self.collectedTorrents:
coltor = self.collectedTorrents[t.infohash]
if coltor.isPlayable():
torrent = coltor
break
else:
torrent = t
break
if not torrent:
return
self.guiutility.library_manager.playTorrent(torrent.infohash)
button = event.GetEventObject()
button.Enable(False)
def OnResume(self, event=None):
for torrent in self.GetSelectedTorrents():
self.guiutility.library_manager.resumeTorrent(torrent)
if event:
button = event.GetEventObject()
button.Enable(False)
def OnStop(self, event=None):
for torrent in self.GetSelectedTorrents():
self.guiutility.library_manager.stopTorrent(torrent.infohash)
if event:
button = event.GetEventObject()
button.Enable(False)
def OnDelete(self, event=None, silent=False, delete=False):
torrents = self.GetSelectedTorrents()
if not silent:
dlg = RemoveTorrent(None, torrents)
button_id = dlg.ShowModal()
else:
button_id = wx.ID_DELETE if delete else wx.ID_DEFAULT
refresh_library = False
if button_id in [wx.ID_DEFAULT, wx.ID_DELETE]:
for torrent in torrents:
self.guiutility.library_manager.deleteTorrent(torrent, button_id == wx.ID_DELETE)
refresh_library = True
if not silent:
if dlg.newName:
if dlg.newName.IsChanged():
dlg2 = wx.MessageDialog(None, 'Do you want to save your changes made to this torrent?',
'Save changes?', wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
if dlg2.ShowModal() == wx.ID_YES:
self.guiutility.channelsearch_manager.modifyTorrent(torrent.channel.id,
torrent.channeltorrent_id,
{'name': dlg.newName.GetValue()})
dlg2.Destroy()
dlg.Destroy()
if refresh_library:
wx.CallLater(1000, self.guiutility.frame.librarylist.do_or_schedule_refresh, True)