filetransferwindow.py 5.42 KB
Newer Older
Saul Ibarra's avatar
Saul Ibarra committed
1 2 3

import os

Dan Pascu's avatar
Dan Pascu committed
4 5 6 7
from PyQt5 import uic
from PyQt5.QtCore import Qt, QUrl
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtWidgets import QAction, QMenu
Saul Ibarra's avatar
Saul Ibarra committed
8 9 10 11 12 13

from application.notification import IObserver, NotificationCenter
from application.python import Null
from application.system import makedirs
from zope.interface import implements

Dan Pascu's avatar
Dan Pascu committed
14
from blink.configuration.settings import BlinkSettings
Saul Ibarra's avatar
Saul Ibarra committed
15 16 17 18 19
from blink.resources import Resources
from blink.sessions import FileTransferDelegate, FileTransferModel
from blink.widgets.util import ContextMenuActions


20 21 22
__all__ = ['FileTransferWindow']


Saul Ibarra's avatar
Saul Ibarra committed
23 24
ui_class, base_class = uic.loadUiType(Resources.get('filetransfer_window.ui'))

25

Saul Ibarra's avatar
Saul Ibarra committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
class FileTransferWindow(base_class, ui_class):
    implements(IObserver)

    def __init__(self, parent=None):
        super(FileTransferWindow, self).__init__(parent)
        with Resources.directory:
            self.setupUi(self)

        self.model = FileTransferModel(self)
        self.listview.setModel(self.model)
        self.listview.setItemDelegate(FileTransferDelegate(self.listview))
        self.listview.customContextMenuRequested.connect(self._SH_ContextMenuRequested)

        self.context_menu = QMenu(self.listview)
        self.actions = ContextMenuActions()
        self.actions.open_file = QAction("Open", self, triggered=self._AH_OpenFile)
42
        self.actions.open_file_folder = QAction("Open File Folder", self, triggered=self._AH_OpenFileFolder)
Saul Ibarra's avatar
Saul Ibarra committed
43
        self.actions.cancel_transfer = QAction("Cancel", self, triggered=self._AH_CancelTransfer)
44
        self.actions.retry_transfer = QAction("Retry", self, triggered=self._AH_RetryTransfer)
Saul Ibarra's avatar
Saul Ibarra committed
45
        self.actions.remove_entry = QAction("Remove From List", self, triggered=self._AH_RemoveEntry)
Dan Pascu's avatar
Dan Pascu committed
46
        self.actions.open_downloads_folder = QAction("Open Transfers Folder", self, triggered=self._AH_OpenTransfersFolder)
Saul Ibarra's avatar
Saul Ibarra committed
47 48 49 50
        self.actions.clear_list = QAction("Clear List", self, triggered=self._AH_ClearList)

        self.model.itemAdded.connect(self.update_status)
        self.model.itemRemoved.connect(self.update_status)
51
        self.model.modelReset.connect(self.update_status)
Saul Ibarra's avatar
Saul Ibarra committed
52 53

        notification_center = NotificationCenter()
54 55
        notification_center.add_observer(self, name='BlinkFileTransferWillRetry')
        notification_center.add_observer(self, name='BlinkFileTransferDidEnd')
Saul Ibarra's avatar
Saul Ibarra committed
56 57

    def show(self, activate=True):
Dan Pascu's avatar
Dan Pascu committed
58 59
        settings = BlinkSettings()
        makedirs(settings.transfers_directory.normalized)
Saul Ibarra's avatar
Saul Ibarra committed
60 61 62 63 64 65 66 67 68
        self.setAttribute(Qt.WA_ShowWithoutActivating, not activate)
        super(FileTransferWindow, self).show()
        self.raise_()
        if activate:
            self.activateWindow()

    def update_status(self):
        total = len(self.model.items)
        active = len([item for item in self.model.items if not item.ended])
69
        text = u'%d %s' % (total, 'transfer' if total == 1 else 'transfers')
Saul Ibarra's avatar
Saul Ibarra committed
70 71 72 73 74 75 76 77
        if active > 0:
            text += u' (%d active)' % active
        self.status_label.setText(text)

    def handle_notification(self, notification):
        handler = getattr(self, '_NH_%s' % notification.name, Null)
        handler(notification)

78
    def _NH_BlinkFileTransferWillRetry(self, notification):
79 80
        self.update_status()

81
    def _NH_BlinkFileTransferDidEnd(self, notification):
Saul Ibarra's avatar
Saul Ibarra committed
82 83 84 85 86 87 88 89 90
        self.update_status()

    def _SH_ContextMenuRequested(self, pos):
        menu = self.context_menu
        menu.clear()
        index = self.listview.indexAt(pos)
        if index.isValid():
            item = index.data(Qt.UserRole)
            if item.ended:
91
                if not item.failed:
Saul Ibarra's avatar
Saul Ibarra committed
92
                    menu.addAction(self.actions.open_file)
93
                    menu.addAction(self.actions.open_file_folder)
94 95
                elif item.direction == 'outgoing':
                    menu.addAction(self.actions.retry_transfer)
Saul Ibarra's avatar
Saul Ibarra committed
96 97
                menu.addAction(self.actions.remove_entry)
            else:
98 99 100
                if item.direction == 'outgoing':
                    menu.addAction(self.actions.open_file)
                    menu.addAction(self.actions.open_file_folder)
Saul Ibarra's avatar
Saul Ibarra committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
                menu.addAction(self.actions.cancel_transfer)
            menu.addSeparator()
            menu.addAction(self.actions.open_downloads_folder)
            menu.addAction(self.actions.clear_list)
        elif self.model.rowCount() > 0:
            menu.addAction(self.actions.open_downloads_folder)
            menu.addAction(self.actions.clear_list)
        else:
            menu.addAction(self.actions.open_downloads_folder)
        menu.exec_(self.mapToGlobal(pos))

    def _AH_OpenFile(self):
        item = self.listview.selectedIndexes()[0].data(Qt.UserRole)
        QDesktopServices.openUrl(QUrl.fromLocalFile(item.filename))

116
    def _AH_OpenFileFolder(self):
Saul Ibarra's avatar
Saul Ibarra committed
117
        item = self.listview.selectedIndexes()[0].data(Qt.UserRole)
118
        QDesktopServices.openUrl(QUrl.fromLocalFile(os.path.dirname(item.filename)))
Saul Ibarra's avatar
Saul Ibarra committed
119 120 121 122 123

    def _AH_CancelTransfer(self):
        item = self.listview.selectedIndexes()[0].data(Qt.UserRole)
        item.end()

124 125
    def _AH_RetryTransfer(self):
        item = self.listview.selectedIndexes()[0].data(Qt.UserRole)
126
        item.retry()
127

Saul Ibarra's avatar
Saul Ibarra committed
128 129 130 131
    def _AH_RemoveEntry(self):
        item = self.listview.selectedIndexes()[0].data(Qt.UserRole)
        self.model.removeItem(item)

Dan Pascu's avatar
Dan Pascu committed
132 133 134
    def _AH_OpenTransfersFolder(self):
        settings = BlinkSettings()
        QDesktopServices.openUrl(QUrl.fromLocalFile(settings.transfers_directory.normalized))
Saul Ibarra's avatar
Saul Ibarra committed
135 136 137 138 139 140

    def _AH_ClearList(self):
        self.model.clear_ended()

del ui_class, base_class