Move three methods from filetransfer_window to protocol/bytestream.

One of the methods was duplicated. Additionally, apply a very few coding standards.
This commit is contained in:
Stephan Erb 2009-12-10 23:53:39 +01:00
parent 24e2047fe3
commit 9bfb5753c2
2 changed files with 82 additions and 92 deletions

View File

@ -36,13 +36,27 @@ from common import helpers
from common import dataforms
class ConnectionBytestream:
def is_transfer_paused(file_props):
if 'stopped' in file_props and file_props['stopped']:
return False
if 'completed' in file_props and file_props['completed']:
return False
if 'disconnect_cb' not in file_props:
return False
return file_props['paused']
def __init__(self):
self.files_props = {}
self.awaiting_xmpp_ping_id = None
def is_transfer_active(file_props):
if 'stopped' in file_props and file_props['stopped']:
return False
if 'completed' in file_props and file_props['completed']:
return False
if 'started' not in file_props or not file_props['started']:
return False
if 'paused' not in file_props:
return True
return not file_props['paused']
def is_transfer_stopped(self, file_props):
def is_transfer_stopped(file_props):
if 'error' in file_props and file_props['error'] != 0:
return True
if 'completed' in file_props and file_props['completed']:
@ -53,6 +67,15 @@ class ConnectionBytestream:
return False
return True
class ConnectionBytestream:
def __init__(self):
self.files_props = {}
self.awaiting_xmpp_ping_id = None
def send_success_connect_reply(self, streamhost):
"""
Send reply to the initiator of FT that we made a connection
@ -74,7 +97,7 @@ class ConnectionBytestream:
Stop all active transfer for contact
"""
for file_props in self.files_props.values():
if self.is_transfer_stopped(file_props):
if is_transfer_stopped(file_props):
continue
receiver_jid = unicode(file_props['receiver'])
if contact.get_full_jid() == receiver_jid:
@ -176,13 +199,13 @@ class ConnectionBytestream:
file_props['sid'], code=406)
return
iq = xmpp.Iq(name='iq', to=unicode(receiver), typ='set')
iq = xmpp.Iq(to=unicode(receiver), typ='set')
file_props['request-id'] = 'id_' + file_props['sid']
iq.setID(file_props['request-id'])
query = iq.setTag('query', namespace=xmpp.NS_BYTESTREAM)
query.setAttr('mode', 'plain')
query.setAttr('sid', file_props['sid'])
self._add_streamhosts_to_query(query, sender, port, fd_add_hosts)
self._add_streamhosts_to_query(query, sender, port, ft_add_hosts)
try:
# The ip we're connected to server with
my_ips = [self.peerhost[0]]

View File

@ -33,6 +33,8 @@ import dialogs
from common import gajim
from common import helpers
from common.protocol.bytestream import (is_transfer_active, is_transfer_paused,
is_transfer_stopped)
C_IMAGE = 0
C_LABELS = 1
@ -55,11 +57,10 @@ class FileTransfersWindow:
self.cleanup_button = self.xml.get_widget('cleanup_button')
self.notify_ft_checkbox = self.xml.get_widget(
'notify_ft_complete_checkbox')
notify = gajim.config.get('notify_on_file_complete')
if notify:
self.notify_ft_checkbox.set_active(True)
else:
self.notify_ft_checkbox.set_active(False)
shall_notify = gajim.config.get('notify_on_file_complete')
self.notify_ft_checkbox.set_active(shall_notify
)
self.model = gtk.ListStore(gtk.gdk.Pixbuf, str, str, str, str, int, str)
self.tree.set_model(self.model)
col = gtk.TreeViewColumn()
@ -144,7 +145,7 @@ class FileTransfersWindow:
if file_props['tt_account'] == account:
receiver_jid = unicode(file_props['receiver']).split('/')[0]
if jid == receiver_jid:
if not self.is_transfer_stopped(file_props):
if not is_transfer_stopped(file_props):
active_transfers[0].append(file_props)
# 'account' is the recipient
@ -152,7 +153,7 @@ class FileTransfersWindow:
if file_props['tt_account'] == account:
sender_jid = unicode(file_props['sender']).split('/')[0]
if jid == sender_jid:
if not self.is_transfer_stopped(file_props):
if not is_transfer_stopped(file_props):
active_transfers[1].append(file_props)
return active_transfers
@ -238,7 +239,6 @@ _('Connection with peer cannot be established.'))
self.tree.get_selection().unselect_all()
def show_file_send_request(self, account, contact):
desc_entry = gtk.Entry()
def on_ok(widget):
@ -665,8 +665,7 @@ _('Connection with peer cannot be established.'))
elif self.height_diff is 0:
return
pointer = self.tree.get_pointer()
props = self.tree.get_path_at_pos(pointer[0],
pointer[1] - self.height_diff)
props = self.tree.get_path_at_pos(pointer[0], pointer[1] - self.height_diff)
if self.tooltip.timeout > 0:
if not props or self.tooltip.id == props[0]:
self.tooltip.hide_tooltip()
@ -675,37 +674,6 @@ _('Connection with peer cannot be established.'))
# try to open the containing folder
self.on_open_folder_menuitem_activate(widget)
def is_transfer_paused(self, file_props):
if 'stopped' in file_props and file_props['stopped']:
return False
if 'completed' in file_props and file_props['completed']:
return False
if 'disconnect_cb' not in file_props:
return False
return file_props['paused']
def is_transfer_active(self, file_props):
if 'stopped' in file_props and file_props['stopped']:
return False
if 'completed' in file_props and file_props['completed']:
return False
if 'started' not in file_props or not file_props['started']:
return False
if 'paused' not in file_props:
return True
return not file_props['paused']
def is_transfer_stopped(self, file_props):
if 'error' in file_props and file_props['error'] != 0:
return True
if 'completed' in file_props and file_props['completed']:
return True
if 'connected' in file_props and file_props['connected'] == False:
return True
if 'stopped' not in file_props or not file_props['stopped']:
return False
return True
def set_cleanup_sensitivity(self):
"""
Check if there are transfer rows and set cleanup_button sensitive, or
@ -743,7 +711,7 @@ _('Connection with peer cannot be established.'))
self.remove_menuitem.set_sensitive(is_row_selected)
self.open_folder_menuitem.set_sensitive(is_row_selected)
is_stopped = False
if self.is_transfer_stopped(file_props):
if is_transfer_stopped(file_props):
is_stopped = True
self.cancel_button.set_sensitive(not is_stopped)
self.cancel_menuitem.set_sensitive(not is_stopped)
@ -751,11 +719,11 @@ _('Connection with peer cannot be established.'))
# no selection, disable the buttons
self.set_all_insensitive()
elif not is_stopped:
if self.is_transfer_active(file_props):
if is_transfer_active(file_props):
# file transfer is active
self.toggle_pause_continue(True)
self.pause_button.set_sensitive(True)
elif self.is_transfer_paused(file_props):
elif is_transfer_paused(file_props):
# file transfer is paused
self.toggle_pause_continue(False)
self.pause_button.set_sensitive(True)
@ -798,7 +766,7 @@ _('Connection with peer cannot be established.'))
iter_ = self.model.get_iter((i))
sid = self.model[iter_][C_SID].decode('utf-8')
file_props = self.files_props[sid[0]][sid[1:]]
if self.is_transfer_stopped(file_props):
if is_transfer_stopped(file_props):
self._remove_transfer(iter_, sid, file_props)
i -= 1
self.tree.get_selection().unselect_all()
@ -840,7 +808,7 @@ _('Connection with peer cannot be established.'))
self.set_status(file_props['type'], file_props['sid'], types[sid[0]])
self.toggle_pause_continue(True)
file_props['continue_cb']()
elif self.is_transfer_active(file_props):
elif is_transfer_active(file_props):
file_props['paused'] = True
self.set_status(file_props['type'], file_props['sid'], 'pause')
# reset that to compute speed only when we resume
@ -898,8 +866,7 @@ _('Connection with peer cannot be established.'))
def show_context_menu(self, event, iter_):
# change the sensitive propery of the buttons and menuitems
path = None
if iter_ is not None:
if iter_:
path = self.model.get_path(iter_)
self.set_buttons_sensitive(path, True)
@ -950,16 +917,16 @@ _('Connection with peer cannot be established.'))
except TypeError:
self.tree.get_selection().unselect_all()
if event.button == 3: # Right click
if path is not None:
if path:
self.tree.get_selection().select_path(path)
iter_ = self.model.get_iter(path)
self.show_context_menu(event, iter_)
if path is not None:
if path:
return True
def on_open_folder_menuitem_activate(self, widget):
selected = self.tree.get_selection().get_selected()
if selected is None or selected[1] is None:
if not selected or not selected[1]:
return
s_iter = selected[1]
sid = self.model[s_iter][C_SID].decode('utf-8')
@ -981,7 +948,7 @@ _('Connection with peer cannot be established.'))
def on_remove_menuitem_activate(self, widget):
selected = self.tree.get_selection().get_selected()
if selected is None or selected[1] is None:
if not selected or not selected[1]:
return
s_iter = selected[1]
sid = self.model[s_iter][C_SID].decode('utf-8')