forked from Tribler/tribler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tribler_window.py
1315 lines (1100 loc) · 51.8 KB
/
tribler_window.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import logging
import os
import signal
import sys
import time
from base64 import b64encode
from pathlib import Path
from PyQt5 import QtCore, uic
from PyQt5.QtCore import (
QCoreApplication,
QDir,
QObject,
QRect,
QSize, QStringListModel,
QTimer,
QUrl,
Qt,
pyqtSignal,
pyqtSlot,
)
from PyQt5.QtGui import (
QDesktopServices,
QFontDatabase,
QIcon,
QKeyEvent,
QKeySequence,
QPixmap,
)
from PyQt5.QtWidgets import (
QAction,
QApplication,
QCompleter,
QFileDialog,
QLineEdit,
QListWidget,
QMainWindow,
QShortcut,
QStyledItemDelegate,
QSystemTrayIcon,
QTreeWidget,
)
from psutil import LINUX
from tribler.core.upgrade.version_manager import VersionHistory
from tribler.core.utilities.network_utils import default_network_utils
from tribler.core.utilities.rest_utils import (
FILE_SCHEME,
MAGNET_SCHEME,
scheme_from_url,
url_is_valid_file,
url_to_path,
)
from tribler.core.utilities.unicode import hexlify
from tribler.core.utilities.utilities import parse_query
from tribler.core.version import version_id
from tribler.gui.app_manager import AppManager
from tribler.gui.core_manager import CoreManager
from tribler.gui.debug_window import DebugWindow
from tribler.gui.defs import (
BUTTON_TYPE_CONFIRM,
BUTTON_TYPE_NORMAL,
CATEGORY_SELECTOR_FOR_POPULAR_ITEMS,
DARWIN,
DEFAULT_API_PORT,
PAGE_CHANNEL_CONTENTS,
PAGE_DISCOVERED,
PAGE_DISCOVERING,
PAGE_DOWNLOADS,
PAGE_LOADING,
PAGE_POPULAR,
PAGE_SEARCH_RESULTS,
PAGE_SETTINGS,
PAGE_TRUST,
PAGE_TRUST_GRAPH_PAGE,
SHUTDOWN_WAITING_PERIOD,
)
from tribler.gui.dialogs.addtopersonalchanneldialog import AddToChannelDialog
from tribler.gui.dialogs.confirmationdialog import ConfirmationDialog
from tribler.gui.dialogs.createtorrentdialog import CreateTorrentDialog
from tribler.gui.dialogs.new_channel_dialog import NewChannelDialog
from tribler.gui.dialogs.startdownloaddialog import StartDownloadDialog
from tribler.gui.error_handler import ErrorHandler
from tribler.gui.event_request_manager import EventRequestManager
from tribler.gui.exceptions import TriblerGuiTestException
from tribler.gui.tribler_action_menu import TriblerActionMenu
from tribler.gui.tribler_request_manager import (
TriblerNetworkRequest,
TriblerRequestManager,
request_manager,
)
from tribler.gui.upgrade_manager import UpgradeManager
from tribler.gui.utilities import (
connect,
create_api_key,
disconnect,
format_api_key,
get_font_path,
get_gui_setting,
get_image_path,
get_ui_file_path,
is_dir_writable,
set_api_key,
show_message_box,
tr,
)
from tribler.gui.widgets.channelsmenulistwidget import ChannelsMenuListWidget
from tribler.gui.widgets.instanttooltipstyle import InstantTooltipStyle
from tribler.gui.widgets.tablecontentmodel import (
DiscoveredChannelsModel,
PopularTorrentsModel,
)
from tribler.gui.widgets.triblertablecontrollers import (
PopularContentTableViewController,
)
# fmt: off
fc_loading_list_item, _ = uic.loadUiType(get_ui_file_path('loading_list_item.ui'))
CHECKBOX_STYLESHEET = """
QCheckBox::indicator { width: 16px; height: 16px;}
QCheckBox::indicator:checked { image: url("%s"); }
QCheckBox::indicator:unchecked { image: url("%s"); }
QCheckBox::indicator:checked::disabled { image: url("%s"); }
QCheckBox::indicator:unchecked::disabled { image: url("%s"); }
QCheckBox::indicator:indeterminate { image: url("%s"); }
""" % (
get_image_path('toggle-checked.svg', convert_slashes_to_forward=True),
get_image_path('toggle-unchecked.svg', convert_slashes_to_forward=True),
get_image_path('toggle-checked-disabled.svg', convert_slashes_to_forward=True),
get_image_path('toggle-unchecked-disabled.svg', convert_slashes_to_forward=True),
get_image_path('toggle-undefined.svg', convert_slashes_to_forward=True),
)
class MagnetHandler(QObject):
def __init__(self, window):
QObject.__init__(self)
self.window = window
@pyqtSlot(QUrl)
def on_open_magnet_link(self, url):
self.window.start_download_from_uri(url)
class TriblerWindow(QMainWindow):
resize_event = pyqtSignal()
escape_pressed = pyqtSignal()
tribler_crashed = pyqtSignal(str)
received_search_completions = pyqtSignal(object)
def __init__(
self,
app_manager: AppManager,
settings,
root_state_dir,
core_args=None,
core_env=None,
api_port=None,
api_key=None,
run_core=True,
):
QMainWindow.__init__(self)
self._logger = logging.getLogger(self.__class__.__name__)
self.app_manager = app_manager
QCoreApplication.setOrganizationDomain("nl")
QCoreApplication.setOrganizationName("TUDelft")
QCoreApplication.setApplicationName("Tribler")
self.setWindowIcon(QIcon(QPixmap(get_image_path('tribler.png'))))
self.root_state_dir = Path(root_state_dir)
self.gui_settings = settings
api_port = api_port or default_network_utils.get_first_free_port(
start=int(get_gui_setting(self.gui_settings, "api_port", DEFAULT_API_PORT))
)
if not default_network_utils.is_port_free(api_port):
raise RuntimeError(
"Tribler configuration conflicts with the current OS state: "
"REST API port %i already in use" % api_port
)
api_key = format_api_key(api_key or get_gui_setting(self.gui_settings, "api_key", None) or create_api_key())
set_api_key(self.gui_settings, api_key)
request_manager.port, request_manager.key = api_port, api_key
self.tribler_started = False
self.tribler_settings = None
self.tribler_version = version_id
self.debug_window = None
self.core_args = core_args
self.core_env = core_env
self.error_handler = ErrorHandler(self)
self.events_manager = EventRequestManager(api_port, api_key, self.error_handler)
self.core_manager = CoreManager(self.root_state_dir, api_port, api_key, app_manager, self.events_manager)
self.version_history = VersionHistory(self.root_state_dir)
self.upgrade_manager = UpgradeManager(self.version_history)
self.pending_requests = {}
self.pending_uri_requests = []
self.dialog = None
self.create_dialog = None
self.chosen_dir = None
self.new_version_dialog_postponed = False
self.start_download_dialog_active = False
self.selected_torrent_files = []
self.start_time = time.time()
self.token_refresh_timer = None
self.shutdown_timer = None
self.add_torrent_url_dialog_active = False
# We use colored Emojis at several locations in our user interface. Not all Linux distributions have a font
# available to render these characters. If we are on Linux, try to load the .ttf file from the Tribler data
# directory, if the font is not installed yet. This font should be embedded by PyInstaller when building the
# executable.
if LINUX and "Noto Color Emoji" not in QFontDatabase().families():
emoji_ttf_path = get_font_path("NotoColorEmoji.ttf")
if os.path.exists(emoji_ttf_path):
result = QFontDatabase.addApplicationFont(emoji_ttf_path)
if result == -1:
self.logger.warning("Failed to load font %s!", emoji_ttf_path)
sys.excepthook = self.error_handler.gui_error
uic.loadUi(get_ui_file_path('mainwindow.ui'), self)
TriblerRequestManager.window = self
self.tribler_status_bar.hide()
self.token_balance_widget.mouseReleaseEvent = self.on_token_balance_click
self.magnet_handler = MagnetHandler(self.window)
QDesktopServices.setUrlHandler("magnet", self.magnet_handler, "on_open_magnet_link")
self.debug_pane_shortcut = QShortcut(QKeySequence("Ctrl+d"), self)
connect(self.debug_pane_shortcut.activated, self.clicked_debug_panel_button)
self.import_torrent_shortcut = QShortcut(QKeySequence("Ctrl+o"), self)
connect(self.import_torrent_shortcut.activated, self.on_add_torrent_browse_file)
self.add_torrent_url_shortcut = QShortcut(QKeySequence("Ctrl+i"), self)
connect(self.add_torrent_url_shortcut.activated, self.on_add_torrent_from_url)
self.tribler_gui_test_exception_shortcut = QShortcut(QKeySequence("Ctrl+Alt+Shift+G"), self)
connect(self.tribler_gui_test_exception_shortcut.activated, self.on_test_tribler_gui_exception)
self.tribler_core_test_exception_shortcut = QShortcut(QKeySequence("Ctrl+Alt+Shift+C"), self)
connect(self.tribler_core_test_exception_shortcut.activated, self.on_test_tribler_core_exception)
connect(self.top_search_bar.clicked, self.clicked_search_bar)
connect(self.top_search_bar.returnPressed, self.on_top_search_bar_return_pressed)
# Remove the focus rect on OS X
for widget in self.findChildren(QLineEdit) + self.findChildren(QListWidget) + self.findChildren(QTreeWidget):
widget.setAttribute(Qt.WA_MacShowFocusRect, 0)
self.menu_buttons = [
self.left_menu_button_downloads,
self.left_menu_button_discovered,
self.left_menu_button_popular,
]
self.search_results_page.initialize(hide_xxx=self.hide_xxx)
connect(
self.core_manager.events_manager.received_remote_query_results, self.search_results_page.update_loading_page
)
self.settings_page.initialize_settings_page(version_history=self.version_history)
self.downloads_page.initialize_downloads_page()
self.loading_page.initialize_loading_page()
self.discovering_page.initialize_discovering_page()
self.discovered_page.initialize_content_page(hide_xxx=self.hide_xxx)
self.popular_page.initialize_content_page(
hide_xxx=self.hide_xxx,
controller_class=PopularContentTableViewController,
categories=CATEGORY_SELECTOR_FOR_POPULAR_ITEMS,
)
self.trust_page.initialize_trust_page()
self.trust_graph_page.initialize_trust_graph()
self.stackedWidget.setCurrentIndex(PAGE_LOADING)
# Create the system tray icon
self.tray_icon = None
# System tray doesn't make sense on Mac
if QSystemTrayIcon.isSystemTrayAvailable():
self.tray_icon = QSystemTrayIcon()
if not DARWIN:
connect(self.tray_icon.activated, self.on_system_tray_icon_activated)
use_monochrome_icon = get_gui_setting(self.gui_settings, "use_monochrome_icon", False, is_bool=True)
self.update_tray_icon(use_monochrome_icon)
# Create the tray icon menu
menu = TriblerActionMenu(self)
menu.addAction(tr('Show Tribler window'), self.raise_window)
menu.addSeparator()
self.create_add_torrent_menu(menu)
menu.addSeparator()
menu.addAction(tr('Show downloads'), self.clicked_menu_button_downloads)
menu.addSeparator()
menu.addAction(tr('Quit Tribler'), self.close_tribler)
self.tray_icon.setContextMenu(menu)
self.debug_panel_button.setHidden(True)
self.top_menu_button.setHidden(True)
self.left_menu.setHidden(True)
self.token_balance_widget.setHidden(True)
self.settings_button.setHidden(True)
self.add_torrent_button.setHidden(True)
self.top_search_bar.setHidden(True)
# Set various icons
self.top_menu_button.setIcon(QIcon(get_image_path('menu.png')))
self.search_completion_model = QStringListModel()
completer = QCompleter()
completer.setModel(self.search_completion_model)
completer.setCompletionMode(QCompleter.UnfilteredPopupCompletion)
self.item_delegate = QStyledItemDelegate()
completer.popup().setItemDelegate(self.item_delegate)
completer.popup().setStyleSheet(
"""
QListView {
background-color: #404040;
}
QListView::item {
color: #D0D0D0;
padding-top: 5px;
padding-bottom: 5px;
}
QListView::item:hover {
background-color: #707070;
}
"""
)
self.top_search_bar.setCompleter(completer)
connect(self.core_manager.events_manager.torrent_finished, self.on_torrent_finished)
connect(self.core_manager.events_manager.new_version_available, self.on_new_version_available)
connect(self.core_manager.events_manager.core_connected, self.on_core_connected)
connect(self.core_manager.events_manager.low_storage_signal, self.on_low_storage)
connect(self.core_manager.events_manager.tribler_shutdown_signal, self.on_tribler_shutdown_state_update)
connect(self.core_manager.events_manager.config_error_signal, self.on_config_error_signal)
# Install signal handler for ctrl+c events
def sigint_handler(*_):
self.close_tribler()
signal.signal(signal.SIGINT, sigint_handler)
# Resize and move the window according to the settings
self.restore_window_geometry()
self.show()
self.add_to_channel_dialog = AddToChannelDialog(self.window())
self.add_torrent_menu = self.create_add_torrent_menu()
self.add_torrent_button.setMenu(self.add_torrent_menu)
self.channels_menu_list = self.findChild(ChannelsMenuListWidget, "channels_menu_list")
connect(self.channels_menu_list.itemClicked, self.open_channel_contents_page)
# The channels content page is only used to show subscribed channels, so we always show xxx
# contents in it.
connect(
self.core_manager.events_manager.node_info_updated,
lambda data: self.channels_menu_list.reload_if_necessary([data]),
)
connect(self.left_menu_button_new_channel.clicked, self.create_new_channel)
connect(self.debug_panel_button.clicked, self.clicked_debug_panel_button)
connect(self.trust_graph_button.clicked, self.clicked_trust_graph_page_button)
# Apply a custom style to our checkboxes, with custom images.
stylesheet = self.styleSheet()
stylesheet += CHECKBOX_STYLESHEET
self.setStyleSheet(stylesheet)
self.core_manager.start(
core_args=self.core_args,
core_env=self.core_env,
run_core=run_core,
upgrade_manager=self.upgrade_manager,
)
def on_test_tribler_gui_exception(self, *_):
raise TriblerGuiTestException("Tribler GUI Test Exception")
def on_test_tribler_core_exception(self, *_):
def dummy_callback(_):
pass
TriblerNetworkRequest("/debug/core_test_exception", dummy_callback, method='POST')
def restore_window_geometry(self):
screen_geometry: QRect = QApplication.desktop().availableGeometry()
size: QSize = self.gui_settings.value("size", self.size())
def restore_size():
self._logger.info(f'Available screen geometry: {screen_geometry}')
self._logger.info(f'Restored window size: {size}')
bounded_size = QSize(
min(size.width(), screen_geometry.width()),
min(size.height(), screen_geometry.height())
)
self._logger.info(f'Resize window to the bounded size: {bounded_size}')
self.resize(bounded_size)
def restore_position():
pos = self.gui_settings.value("pos", self.pos())
self._logger.info(f'Restored window position: {pos}')
window_geometry = QRect(pos, size)
union: QRect = screen_geometry | window_geometry
window_outside_the_screen = union.width() > screen_geometry.width() or \
union.height() > screen_geometry.height()
self._logger.info(f'Is window outside the screen: {window_outside_the_screen}')
actual_position = pos if not window_outside_the_screen else screen_geometry.topLeft()
self._logger.info(f'Move the window to the: {actual_position}')
self.move(actual_position)
restore_size()
restore_position()
def create_new_channel(self, checked):
# TODO: DRY this with tablecontentmodel, possibly using QActions
def update_channels_state(_):
self.channels_menu_list.load_channels()
self.add_to_channel_dialog.clear_channels_tree()
def create_channel_callback(channel_name):
TriblerNetworkRequest(
"channels/mychannel/0/channels",
update_channels_state,
method='POST',
raw_data=json.dumps({"name": channel_name}) if channel_name else None,
)
NewChannelDialog(self, create_channel_callback)
def open_channel_contents_page(self, channel_list_item):
if not channel_list_item.flags() & Qt.ItemIsEnabled:
return
self.channel_contents_page.initialize_root_model_from_channel_info(channel_list_item.channel_info)
self.stackedWidget.setCurrentIndex(PAGE_CHANNEL_CONTENTS)
self.deselect_all_menu_buttons()
def update_tray_icon(self, use_monochrome_icon):
if not QSystemTrayIcon.isSystemTrayAvailable() or not self.tray_icon:
return
if use_monochrome_icon:
self.tray_icon.setIcon(QIcon(QPixmap(get_image_path('monochrome_tribler.png'))))
else:
self.tray_icon.setIcon(QIcon(QPixmap(get_image_path('tribler.png'))))
self.tray_icon.show()
def delete_tray_icon(self):
if self.tray_icon:
try:
self.tray_icon.deleteLater()
except RuntimeError:
# The tray icon might have already been removed when unloading Qt.
# This is due to the C code actually being asynchronous.
logging.debug("Tray icon already removed, no further deletion necessary.")
self.tray_icon = None
def on_low_storage(self, disk_usage_data):
"""
Dealing with low storage space available. First stop the downloads and the core manager and ask user to user to
make free space.
:return:
"""
def close_tribler_gui():
self.close_tribler()
# Since the core has already stopped at this point, it will not terminate the GUI.
# So, we quit the GUI separately here.
self.app_manager.quit_application()
self.downloads_page.stop_loading_downloads()
self.core_manager.stop(quit_app_on_core_finished=False)
close_dialog = ConfirmationDialog(
self.window(),
tr("<b>CRITICAL ERROR</b>"),
tr(
"You are running low on disk space (<100MB). Please make sure to have "
"sufficient free space available and restart Tribler again."
),
[(tr("Close Tribler"), BUTTON_TYPE_NORMAL)],
)
connect(close_dialog.button_clicked, lambda _: close_tribler_gui())
close_dialog.show()
def on_torrent_finished(self, torrent_info):
if "hidden" not in torrent_info or not torrent_info["hidden"]:
self.tray_show_message(tr("Download finished"), tr("Download of %s has finished.") % {torrent_info['name']})
def show_loading_screen(self):
self.top_menu_button.setHidden(True)
self.left_menu.setHidden(True)
self.token_balance_widget.setHidden(True)
self.debug_panel_button.setHidden(True)
self.settings_button.setHidden(True)
self.add_torrent_button.setHidden(True)
self.top_search_bar.setHidden(True)
self.stackedWidget.setCurrentIndex(PAGE_LOADING)
def tray_set_tooltip(self, message):
"""
Set a tooltip message for the tray icon, if possible.
:param message: the message to display on hover
"""
if self.tray_icon:
try:
self.tray_icon.setToolTip(message)
except RuntimeError as e:
logging.error("Failed to set tray tooltip: %s", str(e))
def tray_show_message(self, title, message):
"""
Show a message at the tray icon, if possible.
:param title: the title of the message
:param message: the message to display
"""
if self.tray_icon:
try:
self.tray_icon.showMessage(title, message)
except RuntimeError as e:
logging.error("Failed to set tray message: %s", str(e))
def on_core_connected(self, version):
if self.tribler_started:
self._logger.warning("Received duplicate Tribler Core connected event")
return
self._logger.info("Core connected")
self.tribler_started = True
self.tribler_version = version
self.top_menu_button.setHidden(False)
self.left_menu.setHidden(False)
# self.token_balance_widget.setHidden(False) # restore it after the token balance calculation is fixed
self.settings_button.setHidden(False)
self.add_torrent_button.setHidden(False)
self.top_search_bar.setHidden(False)
self.fetch_settings()
self.downloads_page.start_loading_downloads()
self.setAcceptDrops(True)
self.setWindowTitle(f"Tribler {self.tribler_version}")
autocommit_enabled = (
get_gui_setting(self.gui_settings, "autocommit_enabled", True, is_bool=True) if self.gui_settings else True
)
self.channel_contents_page.initialize_content_page(autocommit_enabled=autocommit_enabled, hide_xxx=False)
self.discovered_page.initialize_root_model(
DiscoveredChannelsModel(
channel_info={"name": tr("Discovered channels")}, endpoint_url="channels", hide_xxx=self.hide_xxx
)
)
connect(self.core_manager.events_manager.discovered_channel, self.discovered_page.model.on_new_entry_received)
self.popular_page.initialize_root_model(
PopularTorrentsModel(channel_info={"name": tr("Popular torrents")}, hide_xxx=self.hide_xxx)
)
self.popular_page.explanation_tooltip_button.setHidden(False)
self.add_to_channel_dialog.load_channel(0)
if not self.gui_settings.value("first_discover", False) and not self.core_manager.use_existing_core:
connect(self.core_manager.events_manager.discovered_channel, self.stop_discovering)
self.window().gui_settings.setValue("first_discover", True)
self.discovering_page.is_discovering = True
self.stackedWidget.setCurrentIndex(PAGE_DISCOVERING)
else:
self.clicked_menu_button_discovered()
self.left_menu_button_discovered.setChecked(True)
self.channels_menu_list.load_channels()
# Toggle debug if developer mode is enabled
self.window().debug_panel_button.setHidden(not get_gui_setting(self.gui_settings, "debug", False, is_bool=True))
QApplication.setStyle(InstantTooltipStyle(QApplication.style()))
@property
def hide_xxx(self):
return get_gui_setting(self.gui_settings, "family_filter", True, is_bool=True)
def stop_discovering(self, response):
if not self.discovering_page.is_discovering:
return
disconnect(self.core_manager.events_manager.discovered_channel, self.stop_discovering)
self.discovering_page.is_discovering = False
if self.stackedWidget.currentIndex() == PAGE_DISCOVERING:
self.clicked_menu_button_discovered()
self.left_menu_button_discovered.setChecked(True)
def on_events_started(self, json_dict):
self.setWindowTitle(f"Tribler {json_dict['version']}")
def show_status_bar(self, message):
self.tribler_status_bar_label.setText(message)
self.tribler_status_bar.show()
def hide_status_bar(self):
self.tribler_status_bar.hide()
def process_uri_request(self):
"""
Process a URI request if we have one in the queue.
"""
if len(self.pending_uri_requests) == 0:
return
uri = self.pending_uri_requests.pop()
if uri.startswith('file') or uri.startswith('magnet'):
self.start_download_from_uri(uri)
def update_recent_download_locations(self, destination):
# Save the download location to the GUI settings
current_settings = get_gui_setting(self.gui_settings, "recent_download_locations", "")
recent_locations = current_settings.split(",") if len(current_settings) > 0 else []
if isinstance(destination, str):
destination = destination.encode('utf-8')
encoded_destination = hexlify(destination)
if encoded_destination in recent_locations:
recent_locations.remove(encoded_destination)
recent_locations.insert(0, encoded_destination)
if len(recent_locations) > 5:
recent_locations = recent_locations[:5]
self.gui_settings.setValue("recent_download_locations", ','.join(recent_locations))
def perform_start_download_request(
self,
uri,
anon_download,
safe_seeding,
destination,
selected_files,
add_to_channel=False,
callback=None,
):
# Check if destination directory is writable
is_writable, error = is_dir_writable(destination)
if not is_writable:
gui_error_message = tr(
"Insufficient write permissions to <i>%s</i> directory. Please add proper "
"write permissions on the directory and add the torrent again. %s"
) % (destination, error)
ConfirmationDialog.show_message(
self.window(), tr("Download error <i>%s</i>") % uri, gui_error_message, "OK"
)
return
anon_hops = int(self.tribler_settings['download_defaults']['number_hops']) if anon_download else 0
safe_seeding = 1 if safe_seeding else 0
post_data = {
"uri": uri,
"anon_hops": anon_hops,
"safe_seeding": safe_seeding,
"destination": destination,
"selected_files": selected_files,
}
TriblerNetworkRequest(
"downloads", callback if callback else self.on_download_added, method='PUT', data=post_data
)
self.update_recent_download_locations(destination)
if add_to_channel:
self.show_add_torrent_to_channel_dialog_from_uri(uri)
def show_add_torrent_to_channel_dialog_from_uri(self, uri):
def on_add_button_pressed(channel_id):
post_data = {}
scheme = scheme_from_url(uri)
if scheme == FILE_SCHEME:
file_path = url_to_path(uri)
content = Path(file_path).read_bytes()
post_data['torrent'] = b64encode(content).decode('ascii')
elif scheme == MAGNET_SCHEME:
post_data['uri'] = uri
if post_data:
TriblerNetworkRequest(
f"channels/mychannel/{channel_id}/torrents",
lambda _: self.tray_show_message(tr("Channel update"), tr("Torrent(s) added to your channel")),
method='PUT',
data=post_data,
)
self.window().add_to_channel_dialog.show_dialog(on_add_button_pressed, confirm_button_text="Add torrent")
def show_add_torrent_to_channel_dialog_from_torrent_data(self, torrent_data):
def on_add_button_pressed(channel_id):
post_data = {'torrent': torrent_data}
if post_data:
TriblerNetworkRequest(
f"channels/mychannel/{channel_id}/torrents",
lambda _: self.tray_show_message(tr("Channel update"), tr("Torrent(s) added to your channel")),
method='PUT',
data=post_data,
)
self.window().add_to_channel_dialog.show_dialog(on_add_button_pressed, confirm_button_text="Add torrent")
def on_new_version_available(self, version):
self.upgrade_manager.on_new_version_available(tribler_window=self, new_version=version)
def on_search_text_change(self, text):
# We do not want to bother the database on petty 1-character queries
if len(text) < 2:
return
TriblerNetworkRequest("search/completions", self.on_received_search_completions, url_params={'q': text})
def on_received_search_completions(self, completions):
if completions is None:
return
self.received_search_completions.emit(completions)
completions_list = completions.get('completions')
if completions_list:
self.search_completion_model.setStringList(completions_list)
def fetch_settings(self):
TriblerNetworkRequest("settings", self.received_settings, capture_core_errors=False)
def received_settings(self, settings):
if not settings:
return
# If we cannot receive the settings, stop Tribler with an option to send the crash report.
if 'error' in settings:
raise RuntimeError(TriblerRequestManager.get_message_from_error(settings))
# If there is any pending dialog (likely download dialog or error dialog of setting not available),
# close the dialog
if self.dialog:
self.dialog.close_dialog()
self.dialog = None
self.tribler_settings = settings['settings']
self.downloads_all_button.click()
# process pending file requests (i.e. someone clicked a torrent file when Tribler was closed)
# We do this after receiving the settings so we have the default download location.
self.process_uri_request()
# Set token balance refresh timer and load the token balance
self.token_refresh_timer = QTimer()
connect(self.token_refresh_timer.timeout, self.load_token_balance)
self.token_refresh_timer.start(60000)
self.load_token_balance()
def on_settings_button_click(self):
self.deselect_all_menu_buttons()
self.stackedWidget.setCurrentIndex(PAGE_SETTINGS)
self.settings_page.load_settings()
def on_token_balance_click(self, _):
self.raise_window()
self.deselect_all_menu_buttons()
self.stackedWidget.setCurrentIndex(PAGE_TRUST)
self.load_token_balance()
self.trust_page.load_history()
def load_token_balance(self):
TriblerNetworkRequest("bandwidth/statistics", self.received_bandwidth_statistics, capture_core_errors=False)
def received_bandwidth_statistics(self, statistics):
if not statistics or "statistics" not in statistics:
return
self.trust_page.received_bandwidth_statistics(statistics)
statistics = statistics["statistics"]
balance = statistics["total_given"] - statistics["total_taken"]
self.set_token_balance(balance)
# If trust page is currently visible, then load the graph as well
if self.stackedWidget.currentIndex() == PAGE_TRUST:
self.trust_page.load_history()
def set_token_balance(self, balance):
if abs(balance) > 1024 ** 4: # Balance is over a TB
balance /= 1024.0 ** 4
self.token_balance_label.setText(f"{balance:.1f} TB")
elif abs(balance) > 1024 ** 3: # Balance is over a GB
balance /= 1024.0 ** 3
self.token_balance_label.setText(f"{balance:.1f} GB")
else:
balance /= 1024.0 ** 2
self.token_balance_label.setText("%d MB" % balance)
def on_system_tray_icon_activated(self, reason):
if reason != QSystemTrayIcon.DoubleClick:
return
if self.isMinimized():
self.raise_window()
else:
self.setWindowState(self.windowState() | Qt.WindowMinimized)
def raise_window(self):
self.setWindowState(self.windowState() & ~Qt.WindowMinimized | Qt.WindowActive)
self.show()
self.raise_()
self.activateWindow()
def create_add_torrent_menu(self, menu=None):
"""
Create a menu to add new torrents. Shows when users click on the tray icon or the big plus button.
"""
menu = menu if menu is not None else TriblerActionMenu(self)
browse_files_action = QAction(tr("Import torrent from file"), self)
browse_directory_action = QAction(tr("Import torrent(s) from directory"), self)
add_url_action = QAction(tr("Import torrent from magnet/URL"), self)
create_torrent_action = QAction(tr("Create torrent from file(s)"), self)
connect(browse_files_action.triggered, self.on_add_torrent_browse_file)
connect(browse_directory_action.triggered, self.on_add_torrent_browse_dir)
connect(add_url_action.triggered, self.on_add_torrent_from_url)
connect(create_torrent_action.triggered, self.on_create_torrent)
menu.addAction(browse_files_action)
menu.addAction(browse_directory_action)
menu.addAction(add_url_action)
menu.addSeparator()
menu.addAction(create_torrent_action)
return menu
def on_create_torrent(self, checked):
self.raise_window() # For the case when the action is triggered by tray icon
if self.create_dialog:
self.create_dialog.close_dialog()
self.create_dialog = CreateTorrentDialog(self)
connect(self.create_dialog.create_torrent_notification, self.on_create_torrent_updates)
connect(self.create_dialog.add_to_channel_selected, self.show_add_torrent_to_channel_dialog_from_torrent_data)
self.create_dialog.show()
def on_create_torrent_updates(self, update_dict):
self.tray_show_message(tr("Torrent updates"), update_dict['msg'])
def on_add_torrent_browse_file(self, *_):
self.raise_window() # For the case when the action is triggered by tray icon
filenames = QFileDialog.getOpenFileNames(
self, tr("Please select the .torrent file"), QDir.homePath(), tr("Torrent files%s") % " (*.torrent)"
)
if len(filenames[0]) > 0:
for filename in filenames[0]:
self.pending_uri_requests.append(Path(filename).as_uri())
self.process_uri_request()
def start_download_from_uri(self, uri):
uri = uri.decode('utf-8') if isinstance(uri, bytes) else uri
if get_gui_setting(self.gui_settings, "ask_download_settings", True, is_bool=True):
# FIXME: instead of using this workaround, make sure the settings are _available_ by this moment
# If tribler settings is not available, fetch the settings and inform the user to try again.
if not self.tribler_settings:
self.fetch_settings()
self.dialog = ConfirmationDialog.show_error(
self,
tr("Download Error"),
tr("Tribler settings is not available yet. Fetching it now. Please try again later."),
)
# By re-adding the download uri to the pending list, the request is re-processed
# when the settings is received
self.pending_uri_requests.append(uri)
return
# Clear any previous dialog if exists
if self.dialog:
self.dialog.close_dialog()
self.dialog = None
self.dialog = StartDownloadDialog(self, uri)
connect(self.dialog.button_clicked, self.on_start_download_action)
self.dialog.show()
self.start_download_dialog_active = True
else:
# FIXME: instead of using this workaround, make sure the settings are _available_ by this moment
# In the unlikely scenario that tribler settings are not available yet, try to fetch settings again and
# add the download uri back to self.pending_uri_requests to process again.
if not self.tribler_settings:
self.fetch_settings()
if uri not in self.pending_uri_requests:
self.pending_uri_requests.append(uri)
return
self.window().perform_start_download_request(
uri,
self.window().tribler_settings['download_defaults']['anonymity_enabled'],
self.window().tribler_settings['download_defaults']['safeseeding_enabled'],
self.tribler_settings['download_defaults']['saveas'],
[],
)
self.process_uri_request()
def on_start_download_action(self, action):
if action == 1:
if self.dialog and self.dialog.dialog_widget:
self.window().perform_start_download_request(
self.dialog.download_uri,
self.dialog.dialog_widget.anon_download_checkbox.isChecked(),
self.dialog.dialog_widget.safe_seed_checkbox.isChecked(),
self.dialog.dialog_widget.destination_input.currentText(),
self.dialog.dialog_widget.files_list_view.get_selected_files_indexes(),
add_to_channel=self.dialog.dialog_widget.add_to_channel_checkbox.isChecked(),
)
else:
ConfirmationDialog.show_error(
self, tr("Tribler UI Error"), tr("Something went wrong. Please try again.")
)
logging.exception("Error while trying to download. Either dialog or dialog.dialog_widget is None")
if self.dialog:
self.dialog.close_dialog()
self.dialog = None
self.start_download_dialog_active = False
if action == 0: # We do this after removing the dialog since process_uri_request is blocking
self.process_uri_request()
def on_add_torrent_browse_dir(self, checked):
self.raise_window() # For the case when the action is triggered by tray icon
chosen_dir = QFileDialog.getExistingDirectory(
self,
tr("Please select the directory containing the .torrent files"),
QDir.homePath(),
QFileDialog.ShowDirsOnly,
)
self.chosen_dir = chosen_dir
if len(chosen_dir) != 0:
self.selected_torrent_files = list(Path(chosen_dir).glob("*.torrent"))
self.dialog = ConfirmationDialog(
self,
tr("Add torrents from directory"),
tr("Add %s torrent files from the following directory to your Tribler channel: \n\n%s")
% (len(self.selected_torrent_files), chosen_dir),
[(tr("ADD"), BUTTON_TYPE_NORMAL), (tr("CANCEL"), BUTTON_TYPE_CONFIRM)],
checkbox_text=tr("Add torrents to My Channel"),
)
connect(self.dialog.button_clicked, self.on_confirm_add_directory_dialog)
self.dialog.show()
def on_confirm_add_directory_dialog(self, action):
if action == 0:
if self.dialog.checkbox.isChecked():
# TODO: add recursive directory scanning
def on_add_button_pressed(channel_id):
if not Path(self.chosen_dir).is_dir():
show_message_box(f'"{self.chosen_dir}" is not a directory')
return
TriblerNetworkRequest(
f"collections/mychannel/{channel_id}/torrents",
lambda _: self.tray_show_message(
tr("Channels update"), tr("%s added to your channel") % self.chosen_dir
),
method='PUT',
data={"torrents_dir": self.chosen_dir},
)
self.window().add_to_channel_dialog.show_dialog(
on_add_button_pressed, confirm_button_text=tr("Add torrent(s)")
)
for torrent_file in self.selected_torrent_files:
self.perform_start_download_request(
torrent_file.as_uri(),