Commit 3d07622e authored by Philipp Hörist's avatar Philipp Hörist

Refactor FileTransfer

FileTransferProgress

- Make it more generic, so it fits downloads and uploads

FileTransfer

- Add DECRYPTING state
- Move state descriptions into FileTransfer
- Add cancel() method
- Add filename property
parent 068274fe
......@@ -833,7 +833,8 @@ GIO_TLS_ERRORS = {
class FTState(Enum):
PREPARING = 'prepare'
ENCRYPTING = 'encrypt'
ENCRYPTING = 'encrypting'
DECRYPTING = 'decrypting'
STARTED = 'started'
IN_PROGRESS = 'progress'
FINISHED = 'finished'
......@@ -847,6 +848,10 @@ class FTState(Enum):
def is_encrypting(self):
return self == FTState.ENCRYPTING
@property
def is_decrypting(self):
return self == FTState.DECRYPTING
@property
def is_started(self):
return self == FTState.STARTED
......
......@@ -17,10 +17,14 @@ from gajim.common.helpers import Observable
from gajim.common.const import FTState
class FileTransfer(Observable):
def __init__(self, account):
_state_descriptions = {}
def __init__(self, account, cancel_func=None):
Observable.__init__(self)
self._account = account
self._cancel_func = cancel_func
self._seen = 0
self.size = 0
......@@ -42,6 +46,17 @@ class FileTransfer(Observable):
return False
return self._seen >= self.size
@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, '')
def set_preparing(self):
self._state = FTState.PREPARING
self.notify('state-changed', FTState.PREPARING)
......@@ -50,6 +65,10 @@ class FileTransfer(Observable):
self._state = FTState.ENCRYPTING
self.notify('state-changed', FTState.ENCRYPTING)
def set_decrypting(self):
self._state = FTState.DECRYPTING
self.notify('state-changed', FTState.DECRYPTING)
def set_started(self):
self._state = FTState.STARTED
self.notify('state-changed', FTState.STARTED)
......
......@@ -30,6 +30,7 @@ from gajim.common import app
from gajim.common import ged
from gajim.common.i18n import _
from gajim.common.helpers import get_tls_error_phrase
from gajim.common.const import FTState
from gajim.common.filetransfer import FileTransfer
from gajim.common.modules.base import BaseModule
from gajim.common.connection_handlers_events import InformationEvent
......@@ -129,9 +130,10 @@ class HTTPUpload(BaseModule):
self._log.info("Detected MIME type of file: %s", mime)
try:
transfer = HTTPFileTransfer(path,
transfer = HTTPFileTransfer(self._account,
self._cancel_upload,
path,
contact,
self._account,
mime,
encryption,
groupchat)
......@@ -148,7 +150,7 @@ class HTTPUpload(BaseModule):
else:
self._request_slot(transfer)
def cancel_upload(self, transfer):
def _cancel_upload(self, transfer):
message = self._queued_messages.get(id(transfer))
if message is None:
return
......@@ -164,7 +166,7 @@ class HTTPUpload(BaseModule):
self._log.info('Sending request for slot')
self._nbxmpp('HTTPUpload').request_slot(
jid=self.component,
filename=os.path.basename(transfer.path),
filename=transfer.filename,
size=transfer.size,
content_type=transfer.mime,
callback=self._received_slot,
......@@ -302,14 +304,22 @@ class HTTPUpload(BaseModule):
class HTTPFileTransfer(FileTransfer):
_state_descriptions = {
FTState.ENCRYPTING: _('Encrypting file…'),
FTState.PREPARING: _('Requesting HTTP File Upload Slot…'),
FTState.STARTED: _('Uploading via HTTP File Upload…'),
}
def __init__(self,
account,
cancel_func,
path,
contact,
account,
mime,
encryption,
groupchat):
FileTransfer.__init__(self, account)
FileTransfer.__init__(self, account, cancel_func=cancel_func)
self._path = path
self._encryption = encryption
......@@ -346,18 +356,22 @@ class HTTPFileTransfer(FileTransfer):
def headers(self):
return self._headers
@property
def path(self):
return self._path
def get_transformed_uri(self):
if self._uri_transform_func is not None:
return self._uri_transform_func(self.get_uri)
return self.get_uri
@property
def path(self):
return self._path
def set_uri_transform_func(self, func):
self._uri_transform_func = func
@property
def filename(self):
return os.path.basename(self._path)
def set_error(self, text=''):
self._close()
super().set_error(text)
......
......@@ -168,15 +168,15 @@
</packing>
</child>
<child>
<object class="GtkButton" id="cancel_upload_button">
<property name="label" translatable="yes">_Cancel Upload</property>
<object class="GtkButton" id="cancel_button">
<property name="label" translatable="yes">_Cancel</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">center</property>
<property name="margin_top">12</property>
<property name="use_underline">True</property>
<signal name="clicked" handler="_on_cancel_upload_button_clicked" swapped="no"/>
<signal name="clicked" handler="_on_cancel_button_clicked" swapped="no"/>
<style>
<class name="destructive-action"/>
</style>
......
......@@ -25,18 +25,17 @@ from gajim.gtk.util import get_builder
from gajim.gtk.util import EventHelper
class HTTPUploadProgressWindow(Gtk.ApplicationWindow, EventHelper):
class FileTransferProgress(Gtk.ApplicationWindow, EventHelper):
def __init__(self, transfer):
Gtk.ApplicationWindow.__init__(self)
EventHelper.__init__(self)
self.set_name('HTTPUploadProgressWindow')
self.set_name('FileTransferProgress')
self.set_application(app.app)
self.set_position(Gtk.WindowPosition.CENTER)
self.set_show_menubar(False)
self.set_title(_('File Transfer'))
self._destroyed = False
self._con = app.connections[transfer.account]
self._transfer = transfer
self._transfer.connect('state-changed', self._on_transfer_state_change)
self._transfer.connect('progress', self._on_transfer_progress)
......@@ -48,8 +47,8 @@ class HTTPUploadProgressWindow(Gtk.ApplicationWindow, EventHelper):
self._start_time = time.time()
self._ui = get_builder('httpupload_progress_dialog.ui')
self._ui.file_name_label.set_text(os.path.basename(transfer.path))
self._ui = get_builder('filetransfer_progress.ui')
self._ui.file_name_label.set_text(transfer.filename)
self.add(self._ui.box)
self._pulse = GLib.timeout_add(100, self._pulse_progressbar)
......@@ -58,31 +57,27 @@ class HTTPUploadProgressWindow(Gtk.ApplicationWindow, EventHelper):
self.connect('destroy', self._on_destroy)
self._ui.connect_signals(self)
def _on_transfer_state_change(self, _transfer, _signal_name, state):
if state.is_preparing:
self._ui.label.set_text(_('Requesting HTTP File Upload Slot…'))
elif state.is_finished or state.is_error:
def _on_transfer_state_change(self, transfer, _signal_name, state):
if state.is_finished or state.is_error:
self.destroy()
return
elif state.is_started:
self._ui.label.set_text(_('Uploading via HTTP File Upload…'))
elif state.is_encrypting:
self._ui.label.set_text(_('Encrypting file…'))
description = transfer.get_state_description()
if description:
self._ui.label.set_text(description)
def _pulse_progressbar(self):
self._ui.progressbar.pulse()
return True
def _on_cancel_upload_button_clicked(self, _widget):
def _on_cancel_button_clicked(self, _widget):
self.destroy()
def _on_destroy(self, *args):
self._con.get_module('HTTPUpload').cancel_upload(self._transfer)
self._destroyed = True
self._transfer.cancel()
self._transfer.disconnect(self)
self._transfer = None
self._destroyed = True
if self._pulse is not None:
GLib.source_remove(self._pulse)
......
......@@ -102,7 +102,7 @@ from gajim.gtk.filechoosers import FileChooserDialog
from gajim.gtk.emoji_data import emoji_data
from gajim.gtk.emoji_data import emoji_ascii_data
from gajim.gtk.filetransfer import FileTransfersWindow
from gajim.gtk.http_upload_progress import HTTPUploadProgressWindow
from gajim.gtk.filetransfer_progress import FileTransferProgress
from gajim.gtk.roster_item_exchange import RosterItemExchangeWindow
from gajim.gtk.util import get_show_in_roster
from gajim.gtk.util import get_show_in_systray
......@@ -858,7 +858,7 @@ class Interface:
@staticmethod
def show_httpupload_progress(transfer):
HTTPUploadProgressWindow(transfer)
FileTransferProgress(transfer)
def send_httpupload(self, chat_control):
accept_cb = partial(self.on_file_dialog_ok, chat_control)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment