filetransfer.py 2.57 KB
Newer Older
Philipp Hörist's avatar
Philipp Hörist committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# This file is part of Gajim.
#
# Gajim 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; version 3 only.
#
# Gajim 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 Gajim. If not, see <http://www.gnu.org/licenses/>.

Philipp Hörist's avatar
Philipp Hörist committed
15
from typing import Dict
Philipp Hörist's avatar
Philipp Hörist committed
16 17 18 19 20

from gajim.common.helpers import Observable
from gajim.common.const import FTState

class FileTransfer(Observable):
Philipp Hörist's avatar
Philipp Hörist committed
21

Philipp Hörist's avatar
Philipp Hörist committed
22
    _state_descriptions = {}  # type: Dict[FTState, str]
Philipp Hörist's avatar
Philipp Hörist committed
23 24

    def __init__(self, account, cancel_func=None):
Philipp Hörist's avatar
Philipp Hörist committed
25 26 27
        Observable.__init__(self)

        self._account = account
Philipp Hörist's avatar
Philipp Hörist committed
28
        self._cancel_func = cancel_func
Philipp Hörist's avatar
Philipp Hörist committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

        self._seen = 0
        self.size = 0

        self._state = None
        self._error_text = ''

    @property
    def account(self):
        return self._account

    @property
    def seen(self):
        return self._seen

    @property
    def is_complete(self):
        if self.size == 0:
            return False
        return self._seen >= self.size

Philipp Hörist's avatar
Philipp Hörist committed
50 51 52 53 54 55 56 57 58 59 60
    @property
    def filename(self):
        raise NotImplementedError

    def cancel(self):
        if self._cancel_func is not None:
            self._cancel_func(self)

    def get_state_description(self):
        return self._state_descriptions.get(self._state, '')

Philipp Hörist's avatar
Philipp Hörist committed
61 62 63 64 65 66 67 68
    def set_preparing(self):
        self._state = FTState.PREPARING
        self.notify('state-changed', FTState.PREPARING)

    def set_encrypting(self):
        self._state = FTState.ENCRYPTING
        self.notify('state-changed', FTState.ENCRYPTING)

Philipp Hörist's avatar
Philipp Hörist committed
69 70 71 72
    def set_decrypting(self):
        self._state = FTState.DECRYPTING
        self.notify('state-changed', FTState.DECRYPTING)

Philipp Hörist's avatar
Philipp Hörist committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    def set_started(self):
        self._state = FTState.STARTED
        self.notify('state-changed', FTState.STARTED)

    def set_error(self, text=''):
        self._error_text = text
        self._state = FTState.ERROR
        self.notify('state-changed', FTState.ERROR)
        self.disconnect_signals()

    def set_in_progress(self):
        self._state = FTState.IN_PROGRESS
        self.notify('state-changed', FTState.IN_PROGRESS)

    def set_finished(self):
        self._state = FTState.FINISHED
        self.notify('state-changed', FTState.FINISHED)
        self.disconnect_signals()

    def update_progress(self):
        self.notify('progress')