| 1 | # -*- coding:utf-8 -*- |
|---|
| 2 | ## src/filetransfers_window.py |
|---|
| 3 | ## |
|---|
| 4 | ## Copyright (C) 2003-2008 Yann Leboulanger <asterix AT lagaule.org> |
|---|
| 5 | ## Copyright (C) 2005-2006 Dimitur Kirov <dkirov AT gmail.com> |
|---|
| 6 | ## Copyright (C) 2005-2007 Nikos Kouremenos <kourem AT gmail.com> |
|---|
| 7 | ## Copyright (C) 2006 Travis Shirk <travis AT pobox.com> |
|---|
| 8 | ## |
|---|
| 9 | ## This file is part of Gajim. |
|---|
| 10 | ## |
|---|
| 11 | ## Gajim is free software; you can redistribute it and/or modify |
|---|
| 12 | ## it under the terms of the GNU General Public License as published |
|---|
| 13 | ## by the Free Software Foundation; version 3 only. |
|---|
| 14 | ## |
|---|
| 15 | ## Gajim is distributed in the hope that it will be useful, |
|---|
| 16 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 17 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 18 | ## GNU General Public License for more details. |
|---|
| 19 | ## |
|---|
| 20 | ## You should have received a copy of the GNU General Public License |
|---|
| 21 | ## along with Gajim. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 22 | ## |
|---|
| 23 | |
|---|
| 24 | import gtk |
|---|
| 25 | import gobject |
|---|
| 26 | import pango |
|---|
| 27 | import os |
|---|
| 28 | import time |
|---|
| 29 | |
|---|
| 30 | import gtkgui_helpers |
|---|
| 31 | import tooltips |
|---|
| 32 | import dialogs |
|---|
| 33 | |
|---|
| 34 | from common import gajim |
|---|
| 35 | from common import helpers |
|---|
| 36 | |
|---|
| 37 | C_IMAGE = 0 |
|---|
| 38 | C_LABELS = 1 |
|---|
| 39 | C_FILE = 2 |
|---|
| 40 | C_TIME = 3 |
|---|
| 41 | C_PROGRESS = 4 |
|---|
| 42 | C_PERCENT = 5 |
|---|
| 43 | C_SID = 6 |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | class FileTransfersWindow: |
|---|
| 47 | def __init__(self): |
|---|
| 48 | self.files_props = {'r' : {}, 's': {}} |
|---|
| 49 | self.height_diff = 0 |
|---|
| 50 | self.xml = gtkgui_helpers.get_glade('filetransfers.glade') |
|---|
| 51 | self.window = self.xml.get_widget('file_transfers_window') |
|---|
| 52 | self.tree = self.xml.get_widget('transfers_list') |
|---|
| 53 | self.cancel_button = self.xml.get_widget('cancel_button') |
|---|
| 54 | self.pause_button = self.xml.get_widget('pause_restore_button') |
|---|
| 55 | self.cleanup_button = self.xml.get_widget('cleanup_button') |
|---|
| 56 | self.notify_ft_checkbox = self.xml.get_widget( |
|---|
| 57 | 'notify_ft_complete_checkbox') |
|---|
| 58 | notify = gajim.config.get('notify_on_file_complete') |
|---|
| 59 | if notify: |
|---|
| 60 | self.notify_ft_checkbox.set_active(True) |
|---|
| 61 | else: |
|---|
| 62 | self.notify_ft_checkbox.set_active(False) |
|---|
| 63 | self.model = gtk.ListStore(gtk.gdk.Pixbuf, str, str, str, str, int, str) |
|---|
| 64 | self.tree.set_model(self.model) |
|---|
| 65 | col = gtk.TreeViewColumn() |
|---|
| 66 | |
|---|
| 67 | render_pixbuf = gtk.CellRendererPixbuf() |
|---|
| 68 | |
|---|
| 69 | col.pack_start(render_pixbuf, expand = True) |
|---|
| 70 | render_pixbuf.set_property('xpad', 3) |
|---|
| 71 | render_pixbuf.set_property('ypad', 3) |
|---|
| 72 | render_pixbuf.set_property('yalign', .0) |
|---|
| 73 | col.add_attribute(render_pixbuf, 'pixbuf', 0) |
|---|
| 74 | self.tree.append_column(col) |
|---|
| 75 | |
|---|
| 76 | col = gtk.TreeViewColumn(_('File')) |
|---|
| 77 | renderer = gtk.CellRendererText() |
|---|
| 78 | col.pack_start(renderer, expand=False) |
|---|
| 79 | col.add_attribute(renderer, 'markup' , C_LABELS) |
|---|
| 80 | renderer.set_property('yalign', 0.) |
|---|
| 81 | renderer = gtk.CellRendererText() |
|---|
| 82 | col.pack_start(renderer, expand=True) |
|---|
| 83 | col.add_attribute(renderer, 'markup' , C_FILE) |
|---|
| 84 | renderer.set_property('xalign', 0.) |
|---|
| 85 | renderer.set_property('yalign', 0.) |
|---|
| 86 | renderer.set_property('ellipsize', pango.ELLIPSIZE_END) |
|---|
| 87 | col.set_resizable(True) |
|---|
| 88 | col.set_expand(True) |
|---|
| 89 | self.tree.append_column(col) |
|---|
| 90 | |
|---|
| 91 | col = gtk.TreeViewColumn(_('Time')) |
|---|
| 92 | renderer = gtk.CellRendererText() |
|---|
| 93 | col.pack_start(renderer, expand=False) |
|---|
| 94 | col.add_attribute(renderer, 'markup' , C_TIME) |
|---|
| 95 | renderer.set_property('yalign', 0.5) |
|---|
| 96 | renderer.set_property('xalign', 0.5) |
|---|
| 97 | renderer = gtk.CellRendererText() |
|---|
| 98 | renderer.set_property('ellipsize', pango.ELLIPSIZE_END) |
|---|
| 99 | col.set_resizable(True) |
|---|
| 100 | col.set_expand(False) |
|---|
| 101 | self.tree.append_column(col) |
|---|
| 102 | |
|---|
| 103 | col = gtk.TreeViewColumn(_('Progress')) |
|---|
| 104 | renderer = gtk.CellRendererProgress() |
|---|
| 105 | renderer.set_property('yalign', 0.5) |
|---|
| 106 | renderer.set_property('xalign', 0.5) |
|---|
| 107 | col.pack_start(renderer, expand = False) |
|---|
| 108 | col.add_attribute(renderer, 'text' , C_PROGRESS) |
|---|
| 109 | col.add_attribute(renderer, 'value' , C_PERCENT) |
|---|
| 110 | col.set_resizable(True) |
|---|
| 111 | col.set_expand(False) |
|---|
| 112 | self.tree.append_column(col) |
|---|
| 113 | |
|---|
| 114 | self.images = {} |
|---|
| 115 | self.icons = { |
|---|
| 116 | 'upload': gtk.STOCK_GO_UP, |
|---|
| 117 | 'download': gtk.STOCK_GO_DOWN, |
|---|
| 118 | 'stop': gtk.STOCK_STOP, |
|---|
| 119 | 'waiting': gtk.STOCK_REFRESH, |
|---|
| 120 | 'pause': gtk.STOCK_MEDIA_PAUSE, |
|---|
| 121 | 'continue': gtk.STOCK_MEDIA_PLAY, |
|---|
| 122 | 'ok': gtk.STOCK_APPLY, |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | self.tree.get_selection().set_mode(gtk.SELECTION_SINGLE) |
|---|
| 126 | self.tree.get_selection().connect('changed', self.selection_changed) |
|---|
| 127 | self.tooltip = tooltips.FileTransfersTooltip() |
|---|
| 128 | self.file_transfers_menu = self.xml.get_widget('file_transfers_menu') |
|---|
| 129 | self.open_folder_menuitem = self.xml.get_widget('open_folder_menuitem') |
|---|
| 130 | self.cancel_menuitem = self.xml.get_widget('cancel_menuitem') |
|---|
| 131 | self.pause_menuitem = self.xml.get_widget('pause_menuitem') |
|---|
| 132 | self.continue_menuitem = self.xml.get_widget('continue_menuitem') |
|---|
| 133 | self.remove_menuitem = self.xml.get_widget('remove_menuitem') |
|---|
| 134 | self.xml.signal_autoconnect(self) |
|---|
| 135 | |
|---|
| 136 | def find_transfer_by_jid(self, account, jid): |
|---|
| 137 | ''' find all transfers with peer 'jid' that belong to 'account' ''' |
|---|
| 138 | active_transfers = [[],[]] # ['senders', 'receivers'] |
|---|
| 139 | |
|---|
| 140 | # 'account' is the sender |
|---|
| 141 | for file_props in self.files_props['s'].values(): |
|---|
| 142 | if file_props['tt_account'] == account: |
|---|
| 143 | receiver_jid = unicode(file_props['receiver']).split('/')[0] |
|---|
| 144 | if jid == receiver_jid: |
|---|
| 145 | if not self.is_transfer_stopped(file_props): |
|---|
| 146 | active_transfers[0].append(file_props) |
|---|
| 147 | |
|---|
| 148 | # 'account' is the recipient |
|---|
| 149 | for file_props in self.files_props['r'].values(): |
|---|
| 150 | if file_props['tt_account'] == account: |
|---|
| 151 | sender_jid = unicode(file_props['sender']).split('/')[0] |
|---|
| 152 | if jid == sender_jid: |
|---|
| 153 | if not self.is_transfer_stopped(file_props): |
|---|
| 154 | active_transfers[1].append(file_props) |
|---|
| 155 | return active_transfers |
|---|
| 156 | |
|---|
| 157 | def show_completed(self, jid, file_props): |
|---|
| 158 | ''' show a dialog saying that file (file_props) has been transferred''' |
|---|
| 159 | def on_open(widget, file_props): |
|---|
| 160 | dialog.destroy() |
|---|
| 161 | if 'file-name' not in file_props: |
|---|
| 162 | return |
|---|
| 163 | (path, file) = os.path.split(file_props['file-name']) |
|---|
| 164 | if os.path.exists(path) and os.path.isdir(path): |
|---|
| 165 | helpers.launch_file_manager(path) |
|---|
| 166 | self.tree.get_selection().unselect_all() |
|---|
| 167 | |
|---|
| 168 | if file_props['type'] == 'r': |
|---|
| 169 | # file path is used below in 'Save in' |
|---|
| 170 | (file_path, file_name) = os.path.split(file_props['file-name']) |
|---|
| 171 | else: |
|---|
| 172 | file_name = file_props['name'] |
|---|
| 173 | sectext = '\t' + _('Filename: %s') % file_name |
|---|
| 174 | sectext += '\n\t' + _('Size: %s') % \ |
|---|
| 175 | helpers.convert_bytes(file_props['size']) |
|---|
| 176 | if file_props['type'] == 'r': |
|---|
| 177 | jid = unicode(file_props['sender']).split('/')[0] |
|---|
| 178 | sender_name = gajim.contacts.get_first_contact_from_jid( |
|---|
| 179 | file_props['tt_account'], jid).get_shown_name() |
|---|
| 180 | sender = sender_name |
|---|
| 181 | else: |
|---|
| 182 | #You is a reply of who sent a file |
|---|
| 183 | sender = _('You') |
|---|
| 184 | sectext += '\n\t' +_('Sender: %s') % sender |
|---|
| 185 | sectext += '\n\t' +_('Recipient: ') |
|---|
| 186 | if file_props['type'] == 's': |
|---|
| 187 | jid = unicode(file_props['receiver']).split('/')[0] |
|---|
| 188 | receiver_name = gajim.contacts.get_first_contact_from_jid( |
|---|
| 189 | file_props['tt_account'], jid).get_shown_name() |
|---|
| 190 | recipient = receiver_name |
|---|
| 191 | else: |
|---|
| 192 | #You is a reply of who received a file |
|---|
| 193 | recipient = _('You') |
|---|
| 194 | sectext += recipient |
|---|
| 195 | if file_props['type'] == 'r': |
|---|
| 196 | sectext += '\n\t' +_('Saved in: %s') % file_path |
|---|
| 197 | dialog = dialogs.HigDialog(None, gtk.MESSAGE_INFO, gtk.BUTTONS_NONE, |
|---|
| 198 | _('File transfer completed'), sectext) |
|---|
| 199 | if file_props['type'] == 'r': |
|---|
| 200 | button = gtk.Button(_('_Open Containing Folder')) |
|---|
| 201 | button.connect('clicked', on_open, file_props) |
|---|
| 202 | dialog.action_area.pack_start(button) |
|---|
| 203 | ok_button = dialog.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK) |
|---|
| 204 | def on_ok(widget): |
|---|
| 205 | dialog.destroy() |
|---|
| 206 | ok_button.connect('clicked', on_ok) |
|---|
| 207 | dialog.show_all() |
|---|
| 208 | |
|---|
| 209 | def show_request_error(self, file_props): |
|---|
| 210 | ''' show error dialog to the recipient saying that transfer |
|---|
| 211 | has been canceled''' |
|---|
| 212 | dialogs.InformationDialog(_('File transfer cancelled'), _('Connection with peer cannot be established.')) |
|---|
| 213 | self.tree.get_selection().unselect_all() |
|---|
| 214 | |
|---|
| 215 | def show_send_error(self, file_props): |
|---|
| 216 | ''' show error dialog to the sender saying that transfer |
|---|
| 217 | has been canceled''' |
|---|
| 218 | dialogs.InformationDialog(_('File transfer cancelled'), |
|---|
| 219 | _('Connection with peer cannot be established.')) |
|---|
| 220 | self.tree.get_selection().unselect_all() |
|---|
| 221 | |
|---|
| 222 | def show_stopped(self, jid, file_props, error_msg = ''): |
|---|
| 223 | if file_props['type'] == 'r': |
|---|
| 224 | file_name = os.path.basename(file_props['file-name']) |
|---|
| 225 | else: |
|---|
| 226 | file_name = file_props['name'] |
|---|
| 227 | sectext = '\t' + _('Filename: %s') % file_name |
|---|
| 228 | sectext += '\n\t' + _('Recipient: %s') % jid |
|---|
| 229 | if error_msg: |
|---|
| 230 | sectext += '\n\t' + _('Error message: %s') % error_msg |
|---|
| 231 | dialogs.ErrorDialog(_('File transfer stopped by the contact at the other ' |
|---|
| 232 | 'end'), sectext) |
|---|
| 233 | self.tree.get_selection().unselect_all() |
|---|
| 234 | |
|---|
| 235 | def show_file_send_request(self, account, contact): |
|---|
| 236 | |
|---|
| 237 | desc_entry = gtk.Entry() |
|---|
| 238 | |
|---|
| 239 | def on_ok(widget): |
|---|
| 240 | file_dir = None |
|---|
| 241 | files_path_list = dialog.get_filenames() |
|---|
| 242 | files_path_list = gtkgui_helpers.decode_filechooser_file_paths( |
|---|
| 243 | files_path_list) |
|---|
| 244 | desc = desc_entry.get_text() |
|---|
| 245 | for file_path in files_path_list: |
|---|
| 246 | if self.send_file(account, contact, file_path, desc) and file_dir is None: |
|---|
| 247 | file_dir = os.path.dirname(file_path) |
|---|
| 248 | if file_dir: |
|---|
| 249 | gajim.config.set('last_send_dir', file_dir) |
|---|
| 250 | dialog.destroy() |
|---|
| 251 | |
|---|
| 252 | dialog = dialogs.FileChooserDialog(_('Choose File to Send...'), |
|---|
| 253 | gtk.FILE_CHOOSER_ACTION_OPEN, (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL), |
|---|
| 254 | gtk.RESPONSE_OK, |
|---|
| 255 | True, # select multiple true as we can select many files to send |
|---|
| 256 | gajim.config.get('last_send_dir'), |
|---|
| 257 | on_response_ok = on_ok, |
|---|
| 258 | on_response_cancel = lambda e:dialog.destroy() |
|---|
| 259 | ) |
|---|
| 260 | |
|---|
| 261 | btn = gtk.Button(_('_Send')) |
|---|
| 262 | btn.set_property('can-default', True) |
|---|
| 263 | # FIXME: add send icon to this button (JUMP_TO) |
|---|
| 264 | dialog.add_action_widget(btn, gtk.RESPONSE_OK) |
|---|
| 265 | dialog.set_default_response(gtk.RESPONSE_OK) |
|---|
| 266 | |
|---|
| 267 | desc_hbox = gtk.HBox(False, 5) |
|---|
| 268 | desc_hbox.pack_start(gtk.Label(_('Description: ')),False,False,0) |
|---|
| 269 | desc_hbox.pack_start(desc_entry,True,True,0) |
|---|
| 270 | |
|---|
| 271 | dialog.vbox.pack_start(desc_hbox, False, False, 0) |
|---|
| 272 | |
|---|
| 273 | btn.show() |
|---|
| 274 | desc_hbox.show_all() |
|---|
| 275 | |
|---|
| 276 | def send_file(self, account, contact, file_path, file_desc=''): |
|---|
| 277 | ''' start the real transfer(upload) of the file ''' |
|---|
| 278 | if gtkgui_helpers.file_is_locked(file_path): |
|---|
| 279 | pritext = _('Gajim cannot access this file') |
|---|
| 280 | sextext = _('This file is being used by another process.') |
|---|
| 281 | dialogs.ErrorDialog(pritext, sextext) |
|---|
| 282 | return |
|---|
| 283 | |
|---|
| 284 | if isinstance(contact, str): |
|---|
| 285 | if contact.find('/') == -1: |
|---|
| 286 | return |
|---|
| 287 | (jid, resource) = contact.split('/', 1) |
|---|
| 288 | contact = gajim.contacts.create_contact(jid=jid, resource=resource) |
|---|
| 289 | (file_dir, file_name) = os.path.split(file_path) |
|---|
| 290 | file_props = self.get_send_file_props(account, contact, |
|---|
| 291 | file_path, file_name, file_desc) |
|---|
| 292 | if file_props is None: |
|---|
| 293 | return False |
|---|
| 294 | self.add_transfer(account, contact, file_props) |
|---|
| 295 | gajim.connections[account].send_file_request(file_props) |
|---|
| 296 | return True |
|---|
| 297 | |
|---|
| 298 | def _start_receive(self, file_path, account, contact, file_props): |
|---|
| 299 | file_dir = os.path.dirname(file_path) |
|---|
| 300 | if file_dir: |
|---|
| 301 | gajim.config.set('last_save_dir', file_dir) |
|---|
| 302 | file_props['file-name'] = file_path |
|---|
| 303 | self.add_transfer(account, contact, file_props) |
|---|
| 304 | gajim.connections[account].send_file_approval(file_props) |
|---|
| 305 | |
|---|
| 306 | def show_file_request(self, account, contact, file_props): |
|---|
| 307 | ''' show dialog asking for comfirmation and store location of new |
|---|
| 308 | file requested by a contact''' |
|---|
| 309 | if file_props is None or 'name' not in file_props: |
|---|
| 310 | return |
|---|
| 311 | sec_text = '\t' + _('File: %s') % file_props['name'] |
|---|
| 312 | if 'size' in file_props: |
|---|
| 313 | sec_text += '\n\t' + _('Size: %s') % \ |
|---|
| 314 | helpers.convert_bytes(file_props['size']) |
|---|
| 315 | if 'mime-type' in file_props: |
|---|
| 316 | sec_text += '\n\t' + _('Type: %s') % file_props['mime-type'] |
|---|
| 317 | if 'desc' in file_props: |
|---|
| 318 | sec_text += '\n\t' + _('Description: %s') % file_props['desc'] |
|---|
| 319 | prim_text = _('%s wants to send you a file:') % contact.jid |
|---|
| 320 | dialog, dialog2 = None, None |
|---|
| 321 | |
|---|
| 322 | def on_response_ok(account, contact, file_props): |
|---|
| 323 | |
|---|
| 324 | def on_ok(widget, account, contact, file_props): |
|---|
| 325 | file_path = dialog2.get_filename() |
|---|
| 326 | file_path = gtkgui_helpers.decode_filechooser_file_paths( |
|---|
| 327 | (file_path,))[0] |
|---|
| 328 | if os.path.exists(file_path): |
|---|
| 329 | # check if we have write permissions |
|---|
| 330 | if not os.access(file_path, os.W_OK): |
|---|
| 331 | file_name = os.path.basename(file_path) |
|---|
| 332 | dialogs.ErrorDialog(_('Cannot overwrite existing file "%s"' % file_name), |
|---|
| 333 | _('A file with this name already exists and you do not have permission to overwrite it.')) |
|---|
| 334 | return |
|---|
| 335 | stat = os.stat(file_path) |
|---|
| 336 | dl_size = stat.st_size |
|---|
| 337 | file_size = file_props['size'] |
|---|
| 338 | dl_finished = dl_size >= file_size |
|---|
| 339 | |
|---|
| 340 | def on_response(response): |
|---|
| 341 | if response < 0: |
|---|
| 342 | return |
|---|
| 343 | elif response == 100: |
|---|
| 344 | file_props['offset'] = dl_size |
|---|
| 345 | dialog2.destroy() |
|---|
| 346 | self._start_receive(file_path, account, contact, file_props) |
|---|
| 347 | |
|---|
| 348 | dialog = dialogs.FTOverwriteConfirmationDialog( |
|---|
| 349 | _('This file already exists'), _('What do you want to do?'), |
|---|
| 350 | propose_resume=not dl_finished, on_response=on_response) |
|---|
| 351 | dialog.set_transient_for(dialog2) |
|---|
| 352 | dialog.set_destroy_with_parent(True) |
|---|
| 353 | return |
|---|
| 354 | else: |
|---|
| 355 | dirname = os.path.dirname(file_path) |
|---|
| 356 | if not os.access(dirname, os.W_OK) and os.name != 'nt': |
|---|
| 357 | # read-only bit is used to mark special folder under windows, |
|---|
| 358 | # not to mark that a folder is read-only. See ticket #3587 |
|---|
| 359 | dialogs.ErrorDialog(_('Directory "%s" is not writable') % dirname, _('You do not have permission to create files in this directory.')) |
|---|
| 360 | return |
|---|
| 361 | dialog2.destroy() |
|---|
| 362 | self._start_receive(file_path, account, contact, file_props) |
|---|
| 363 | |
|---|
| 364 | def on_cancel(widget, account, contact, file_props): |
|---|
| 365 | dialog2.destroy() |
|---|
| 366 | gajim.connections[account].send_file_rejection(file_props) |
|---|
| 367 | |
|---|
| 368 | dialog2 = dialogs.FileChooserDialog( |
|---|
| 369 | title_text = _('Save File as...'), |
|---|
| 370 | action = gtk.FILE_CHOOSER_ACTION_SAVE, |
|---|
| 371 | buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, |
|---|
| 372 | gtk.STOCK_SAVE, gtk.RESPONSE_OK), |
|---|
| 373 | default_response = gtk.RESPONSE_OK, |
|---|
| 374 | current_folder = gajim.config.get('last_save_dir'), |
|---|
| 375 | on_response_ok = (on_ok, account, contact, file_props), |
|---|
| 376 | on_response_cancel = (on_cancel, account, contact, file_props)) |
|---|
| 377 | |
|---|
| 378 | dialog2.set_current_name(file_props['name']) |
|---|
| 379 | dialog2.connect('delete-event', lambda widget, event: |
|---|
| 380 | on_cancel(widget, account, contact, file_props)) |
|---|
| 381 | |
|---|
| 382 | def on_response_cancel(account, file_props): |
|---|
| 383 | gajim.connections[account].send_file_rejection(file_props) |
|---|
| 384 | |
|---|
| 385 | dialog = dialogs.NonModalConfirmationDialog(prim_text, sec_text, |
|---|
| 386 | on_response_ok = (on_response_ok, account, contact, file_props), |
|---|
| 387 | on_response_cancel = (on_response_cancel, account, file_props)) |
|---|
| 388 | dialog.connect('delete-event', lambda widget, event: |
|---|
| 389 | on_response_cancel(widget, account, file_props)) |
|---|
| 390 | dialog.popup() |
|---|
| 391 | |
|---|
| 392 | def get_icon(self, ident): |
|---|
| 393 | return self.images.setdefault(ident, |
|---|
| 394 | self.window.render_icon(self.icons[ident], gtk.ICON_SIZE_MENU)) |
|---|
| 395 | |
|---|
| 396 | def set_status(self, typ, sid, status): |
|---|
| 397 | ''' change the status of a transfer to state 'status' ''' |
|---|
| 398 | iter = self.get_iter_by_sid(typ, sid) |
|---|
| 399 | if iter is None: |
|---|
| 400 | return |
|---|
| 401 | sid = self.model[iter][C_SID].decode('utf-8') |
|---|
| 402 | file_props = self.files_props[sid[0]][sid[1:]] |
|---|
| 403 | if status == 'stop': |
|---|
| 404 | file_props['stopped'] = True |
|---|
| 405 | elif status == 'ok': |
|---|
| 406 | file_props['completed'] = True |
|---|
| 407 | self.model.set(iter, C_IMAGE, self.get_icon(status)) |
|---|
| 408 | path = self.model.get_path(iter) |
|---|
| 409 | self.select_func(path) |
|---|
| 410 | |
|---|
| 411 | def _format_percent(self, percent): |
|---|
| 412 | ''' add extra spaces from both sides of the percent, so that |
|---|
| 413 | progress string has always a fixed size''' |
|---|
| 414 | _str = ' ' |
|---|
| 415 | if percent != 100.: |
|---|
| 416 | _str += ' ' |
|---|
| 417 | if percent < 10: |
|---|
| 418 | _str += ' ' |
|---|
| 419 | _str += unicode(percent) + '% \n' |
|---|
| 420 | return _str |
|---|
| 421 | |
|---|
| 422 | def _format_time(self, _time): |
|---|
| 423 | times = { 'hours': 0, 'minutes': 0, 'seconds': 0 } |
|---|
| 424 | _time = int(_time) |
|---|
| 425 | times['seconds'] = _time % 60 |
|---|
| 426 | if _time >= 60: |
|---|
| 427 | _time /= 60 |
|---|
| 428 | times['minutes'] = _time % 60 |
|---|
| 429 | if _time >= 60: |
|---|
| 430 | times['hours'] = _time / 60 |
|---|
| 431 | |
|---|
| 432 | #Print remaining time in format 00:00:00 |
|---|
| 433 | #You can change the places of (hours), (minutes), (seconds) - |
|---|
| 434 | #they are not translatable. |
|---|
| 435 | return _('%(hours)02.d:%(minutes)02.d:%(seconds)02.d') % times |
|---|
| 436 | |
|---|
| 437 | def _get_eta_and_speed(self, full_size, transfered_size, file_props): |
|---|
| 438 | if len(file_props['transfered_size']) == 0: |
|---|
| 439 | return 0., 0. |
|---|
| 440 | elif len(file_props['transfered_size']) == 1: |
|---|
| 441 | speed = round(float(transfered_size) / file_props['elapsed-time']) |
|---|
| 442 | else: |
|---|
| 443 | # first and last are (time, transfered_size) |
|---|
| 444 | first = file_props['transfered_size'][0] |
|---|
| 445 | last = file_props['transfered_size'][-1] |
|---|
| 446 | transfered = last[1] - first[1] |
|---|
| 447 | tim = last[0] - first[0] |
|---|
| 448 | if tim == 0: |
|---|
| 449 | return 0., 0. |
|---|
| 450 | speed = round(float(transfered) / tim) |
|---|
| 451 | if speed == 0.: |
|---|
| 452 | return 0., 0. |
|---|
| 453 | remaining_size = full_size - transfered_size |
|---|
| 454 | eta = remaining_size / speed |
|---|
| 455 | return eta, speed |
|---|
| 456 | |
|---|
| 457 | def _remove_transfer(self, iter_, sid, file_props): |
|---|
| 458 | self.model.remove(iter_) |
|---|
| 459 | if 'tt_account' in file_props: |
|---|
| 460 | # file transfer is set |
|---|
| 461 | account = file_props['tt_account'] |
|---|
| 462 | if account in gajim.connections: |
|---|
| 463 | # there is a connection to the account |
|---|
| 464 | gajim.connections[account].remove_transfer(file_props) |
|---|
| 465 | if file_props['type'] == 'r': # we receive a file |
|---|
| 466 | other = file_props['sender'] |
|---|
| 467 | else: # we send a file |
|---|
| 468 | other = file_props['receiver'] |
|---|
| 469 | if isinstance(other, unicode): |
|---|
| 470 | jid = gajim.get_jid_without_resource(other) |
|---|
| 471 | else: # It's a Contact instance |
|---|
| 472 | jid = other.jid |
|---|
| 473 | for ev_type in ('file-error', 'file-completed', 'file-request-error', |
|---|
| 474 | 'file-send-error', 'file-stopped'): |
|---|
| 475 | for event in gajim.events.get_events(account, jid, [ev_type]): |
|---|
| 476 | if event.parameters['sid'] == file_props['sid']: |
|---|
| 477 | gajim.events.remove_events(account, jid, event) |
|---|
| 478 | gajim.interface.roster.draw_contact(jid, account) |
|---|
| 479 | gajim.interface.roster.show_title() |
|---|
| 480 | del(self.files_props[sid[0]][sid[1:]]) |
|---|
| 481 | del(file_props) |
|---|
| 482 | |
|---|
| 483 | def set_progress(self, typ, sid, transfered_size, iter_ = None): |
|---|
| 484 | ''' change the progress of a transfer with new transfered size''' |
|---|
| 485 | if sid not in self.files_props[typ]: |
|---|
| 486 | return |
|---|
| 487 | file_props = self.files_props[typ][sid] |
|---|
| 488 | full_size = int(file_props['size']) |
|---|
| 489 | if full_size == 0: |
|---|
| 490 | percent = 0 |
|---|
| 491 | else: |
|---|
| 492 | percent = round(float(transfered_size) / full_size * 100, 1) |
|---|
| 493 | if iter_ is None: |
|---|
| 494 | iter_ = self.get_iter_by_sid(typ, sid) |
|---|
| 495 | if iter_ is not None: |
|---|
| 496 | just_began = False |
|---|
| 497 | if self.model[iter_][C_PERCENT] == 0 and int(percent > 0): |
|---|
| 498 | just_began = True |
|---|
| 499 | text = self._format_percent(percent) |
|---|
| 500 | if transfered_size == 0: |
|---|
| 501 | text += '0' |
|---|
| 502 | else: |
|---|
| 503 | text += helpers.convert_bytes(transfered_size) |
|---|
| 504 | text += '/' + helpers.convert_bytes(full_size) |
|---|
| 505 | # Kb/s |
|---|
| 506 | |
|---|
| 507 | # remaining time |
|---|
| 508 | if 'offset' in file_props and file_props['offset']: |
|---|
| 509 | transfered_size -= file_props['offset'] |
|---|
| 510 | full_size -= file_props['offset'] |
|---|
| 511 | |
|---|
| 512 | if file_props['elapsed-time'] > 0: |
|---|
| 513 | file_props['transfered_size'].append((file_props['last-time'], transfered_size)) |
|---|
| 514 | if len(file_props['transfered_size']) > 6: |
|---|
| 515 | file_props['transfered_size'].pop(0) |
|---|
| 516 | eta, speed = self._get_eta_and_speed(full_size, transfered_size, |
|---|
| 517 | file_props) |
|---|
| 518 | |
|---|
| 519 | self.model.set(iter_, C_PROGRESS, text) |
|---|
| 520 | self.model.set(iter_, C_PERCENT, int(percent)) |
|---|
| 521 | text = self._format_time(eta) |
|---|
| 522 | text += '\n' |
|---|
| 523 | #This should make the string Kb/s, |
|---|
| 524 | #where 'Kb' part is taken from %s. |
|---|
| 525 | #Only the 's' after / (which means second) should be translated. |
|---|
| 526 | text += _('(%(filesize_unit)s/s)') % {'filesize_unit': |
|---|
| 527 | helpers.convert_bytes(speed)} |
|---|
| 528 | self.model.set(iter_, C_TIME, text) |
|---|
| 529 | |
|---|
| 530 | # try to guess what should be the status image |
|---|
| 531 | if file_props['type'] == 'r': |
|---|
| 532 | status = 'download' |
|---|
| 533 | else: |
|---|
| 534 | status = 'upload' |
|---|
| 535 | if 'paused' in file_props and file_props['paused'] == True: |
|---|
| 536 | status = 'pause' |
|---|
| 537 | elif 'stalled' in file_props and file_props['stalled'] == True: |
|---|
| 538 | status = 'waiting' |
|---|
| 539 | if 'connected' in file_props and file_props['connected'] == False: |
|---|
| 540 | status = 'stop' |
|---|
| 541 | self.model.set(iter_, 0, self.get_icon(status)) |
|---|
| 542 | if transfered_size == full_size: |
|---|
| 543 | self.set_status(typ, sid, 'ok') |
|---|
| 544 | elif just_began: |
|---|
| 545 | path = self.model.get_path(iter_) |
|---|
| 546 | self.select_func(path) |
|---|
| 54 |
|---|