def on_response_ok(account, contact, file_props): def on_ok(widget, account, contact, file_props): file_path = dialog2.get_filename() file_path = gtkgui_helpers.decode_filechooser_file_paths( (file_path,))[0] if os.path.exists(file_path): # check if we have write permissions if not os.access(file_path, os.W_OK): file_name = os.path.basename(file_path) dialogs.ErrorDialog(_('Cannot overwrite existing file "%s"' % file_name), _('A file with this name already exists and you do not have permission to overwrite it.')) return stat = os.stat(file_path) dl_size = stat.st_size file_size = file_props['size'] dl_finished = dl_size >= file_size def on_response(response): if response < 0: return elif response == 100: file_props['offset'] = dl_size dialog2.destroy() self._start_receive(file_path, account, contact, file_props) dialog = dialogs.FTOverwriteConfirmationDialog( _('This file already exists'), _('What do you want to do?'), propose_resume=not dl_finished, on_response=on_response) dialog.set_transient_for(dialog2) dialog.set_destroy_with_parent(True) return else: dirname = os.path.dirname(file_path) if not os.access(dirname, os.W_OK) and os.name != 'nt': # read-only bit is used to mark special folder under windows, # not to mark that a folder is read-only. See ticket #3587 dialogs.ErrorDialog(_('Directory "%s" is not writable') % dirname, _('You do not have permission to create files in this directory.')) return dialog2.destroy() self._start_receive(file_path, account, contact, file_props) def on_cancel(widget, account, contact, file_props): dialog2.destroy() gajim.connections[account].send_file_rejection(file_props) dialog2 = dialogs.FileChooserDialog( title_text = _('Save File as...'), action = gtk.FILE_CHOOSER_ACTION_SAVE, buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_SAVE, gtk.RESPONSE_OK), default_response = gtk.RESPONSE_OK, current_folder = gajim.config.get('last_save_dir'), on_response_ok = (on_ok, account, contact, file_props), on_response_cancel = (on_cancel, account, contact, file_props)) dialog2.set_current_name(file_props['name']) dialog2.connect('delete-event', lambda widget, event: on_cancel(widget, account, contact, file_props))
def on_save_as_menuitem_activate(self, menu, data): filepath = data["filepath"] original_filename = data["original_filename"] def on_continue(response, target_path): if response < 0: return shutil.copy(filepath, target_path) dialog.destroy() def on_ok(widget): target_path = dialog.get_filename() if os.path.exists(target_path): # check if we have write permissions if not os.access(target_path, os.W_OK): file_name = os.path.basename(target_path) dialogs.ErrorDialog( _('Cannot overwrite existing file "%s"') % file_name, _('A file with this name already exists and you do ' 'not have permission to overwrite it.')) return dialog2 = dialogs.FTOverwriteConfirmationDialog( _('This file already exists'), _('What do you want to do?'), propose_resume=False, on_response=(on_continue, target_path), transient_for=dialog) dialog2.set_destroy_with_parent(True) else: dirname = os.path.dirname(target_path) if not os.access(dirname, os.W_OK): dialogs.ErrorDialog( _('Directory "%s" is not writable') % dirname, _('You do not have permission to ' 'create files in this directory.')) return on_continue(0, target_path) def on_cancel(widget): dialog.destroy() dialog = dialogs.FileChooserDialog( title_text=_('Save Image as...'), action=gtk.FILE_CHOOSER_ACTION_SAVE, buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_SAVE, gtk.RESPONSE_OK), default_response=gtk.RESPONSE_OK, current_folder=gajim.config.get('last_save_dir'), on_response_ok=on_ok, on_response_cancel=on_cancel) dialog.set_current_name(original_filename) dialog.connect('delete-event', lambda widget, event: on_cancel(widget))
def show_file_send_request(self, account, contact): win = Gtk.ScrolledWindow() win.set_shadow_type(Gtk.ShadowType.IN) win.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.NEVER) from message_textview import MessageTextView desc_entry = MessageTextView() win.add(desc_entry) def on_ok(widget): file_dir = None files_path_list = dialog.get_filenames() text_buffer = desc_entry.get_buffer() desc = text_buffer.get_text(text_buffer.get_start_iter(), text_buffer.get_end_iter(), True) for file_path in files_path_list: if self.send_file(account, contact, file_path, desc) \ and file_dir is None: file_dir = os.path.dirname(file_path) if file_dir: gajim.config.set('last_send_dir', file_dir) dialog.destroy() dialog = dialogs.FileChooserDialog(_('Choose File to Send…'), Gtk.FileChooserAction.OPEN, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL), Gtk.ResponseType.OK, True, # select multiple true as we can select many files to send gajim.config.get('last_send_dir'), on_response_ok=on_ok, on_response_cancel=lambda e:dialog.destroy(), transient_for=gajim.interface.roster.window ) btn = Gtk.Button.new_with_mnemonic(_('_Send')) btn.set_property('can-default', True) # FIXME: add send icon to this button (JUMP_TO) dialog.add_action_widget(btn, Gtk.ResponseType.OK) dialog.set_default_response(Gtk.ResponseType.OK) desc_hbox = Gtk.HBox(homogeneous=False, spacing=5) desc_hbox.pack_start(Gtk.Label.new(_('Description: ')), False, False, 0) desc_hbox.pack_start(win, True, True, 0) dialog.vbox.pack_start(desc_hbox, False, False, 0) btn.show() desc_hbox.show_all()
def show_file_send_request(self, account, contact): desc_entry = gtk.Entry() def on_ok(widget): file_dir = None files_path_list = dialog.get_filenames() files_path_list = gtkgui_helpers.decode_filechooser_file_paths( files_path_list) desc = desc_entry.get_text() for file_path in files_path_list: if self.send_file(account, contact, file_path, desc) and file_dir is None: file_dir = os.path.dirname(file_path) if file_dir: gajim.config.set('last_send_dir', file_dir) dialog.destroy() dialog = dialogs.FileChooserDialog(_('Choose File to Send...'), gtk.FILE_CHOOSER_ACTION_OPEN, (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL), gtk.RESPONSE_OK, True, # select multiple true as we can select many files to send gajim.config.get('last_send_dir'), on_response_ok = on_ok, on_response_cancel = lambda e:dialog.destroy() ) btn = gtk.Button(_('_Send')) btn.set_property('can-default', True) # FIXME: add send icon to this button (JUMP_TO) dialog.add_action_widget(btn, gtk.RESPONSE_OK) dialog.set_default_response(gtk.RESPONSE_OK) desc_hbox = gtk.HBox(False, 5) desc_hbox.pack_start(gtk.Label(_('Description: ')),False,False,0) desc_hbox.pack_start(desc_entry,True,True,0) dialog.vbox.pack_start(desc_hbox, False, False, 0) btn.show() desc_hbox.show_all()
def on_avatar_save_as_menuitem_activate(widget, jid, account, default_name=''): def on_continue(response, file_path): if response < 0: return # Get pixbuf pixbuf = None is_fake = False if account and gajim.contacts.is_pm_from_jid(account, jid): is_fake = True pixbuf = get_avatar_pixbuf_from_cache(jid, is_fake, False) ext = file_path.split('.')[-1] type_ = '' if not ext: # Silently save as Jpeg image file_path += '.jpeg' type_ = 'jpeg' elif ext == 'jpg': type_ = 'jpeg' else: type_ = ext # Save image try: pixbuf.save(file_path, type_) except: if os.path.exists(file_path): os.remove(file_path) new_file_path = '.'.join(file_path.split('.')[:-1]) + '.jpeg' def on_ok(file_path, pixbuf): pixbuf.save(file_path, 'jpeg') dialogs.ConfirmationDialog( _('Extension not supported'), _('Image cannot be saved in %(type)s format. Save as %(new_filename)s?' ) % { 'type': type_, 'new_filename': new_file_path }, on_response_ok=(on_ok, new_file_path, pixbuf)) else: dialog.destroy() def on_ok(widget): def on_ok2(file_path, pixbuf): pixbuf.save(file_path, 'jpeg') dialog.destroy() file_path = dialog.get_filename() file_path = decode_filechooser_file_paths((file_path, ))[0] if os.path.exists(file_path): # check if we have write permissions if not os.access(file_path, os.W_OK): file_name = os.path.basename(file_path) dialogs.ErrorDialog( _('Cannot overwrite existing file "%s"' % file_name), _('A file with this name already exists and you do not have ' 'permission to overwrite it.')) return dialog2 = dialogs.FTOverwriteConfirmationDialog( _('This file already exists'), _('What do you want to do?'), propose_resume=False, on_response=(on_continue, file_path)) dialog2.set_transient_for(dialog) dialog2.set_destroy_with_parent(True) else: dirname = os.path.dirname(file_path) if not os.access(dirname, os.W_OK): dialogs.ErrorDialog(_('Directory "%s" is not writable') % \ dirname, _('You do not have permission to create files in this' ' directory.')) return on_continue(0, file_path) def on_cancel(widget): dialog.destroy() dialog = dialogs.FileChooserDialog( title_text=_('Save Image as...'), action=gtk.FILE_CHOOSER_ACTION_SAVE, buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_SAVE, gtk.RESPONSE_OK), default_response=gtk.RESPONSE_OK, current_folder=gajim.config.get('last_save_dir'), on_response_ok=on_ok, on_response_cancel=on_cancel) dialog.set_current_name(default_name) dialog.connect('delete-event', lambda widget, event: on_cancel(widget))
def on_avatar_save_as_menuitem_activate(widget, jid, default_name=''): def on_continue(response, file_path): if response < 0: return pixbuf = get_avatar_pixbuf_from_cache(jid) extension = os.path.splitext(file_path)[1] if not extension: # Silently save as Jpeg image image_format = 'jpeg' file_path += '.jpeg' elif extension == 'jpg': image_format = 'jpeg' else: image_format = extension[1:] # remove leading dot # Save image try: pixbuf.savev(file_path, image_format, [], []) except Exception as e: log.debug('Error saving avatar: %s' % str(e)) if os.path.exists(file_path): os.remove(file_path) new_file_path = '.'.join(file_path.split('.')[:-1]) + '.jpeg' def on_ok(file_path, pixbuf): pixbuf.savev(file_path, 'jpeg', [], []) dialogs.ConfirmationDialog( _('Extension not supported'), _('Image cannot be saved in %(type)s format. Save as ' '%(new_filename)s?') % { 'type': image_format, 'new_filename': new_file_path }, on_response_ok=(on_ok, new_file_path, pixbuf)) else: dialog.destroy() def on_ok(widget): file_path = dialog.get_filename() if os.path.exists(file_path): # check if we have write permissions if not os.access(file_path, os.W_OK): file_name = os.path.basename(file_path) dialogs.ErrorDialog(_('Cannot overwrite existing file "%s"') % \ file_name, _('A file with this name already exists and you ' 'do not have permission to overwrite it.')) return dialog2 = dialogs.FTOverwriteConfirmationDialog( _('This file already exists'), _('What do you want to do?'), propose_resume=False, on_response=(on_continue, file_path), transient_for=dialog) dialog2.set_destroy_with_parent(True) else: dirname = os.path.dirname(file_path) if not os.access(dirname, os.W_OK): dialogs.ErrorDialog(_('Directory "%s" is not writable') % \ dirname, _('You do not have permission to create files in ' 'this directory.')) return on_continue(0, file_path) def on_cancel(widget): dialog.destroy() dialog = dialogs.FileChooserDialog( title_text=_('Save Image as…'), action=Gtk.FileChooserAction.SAVE, buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_SAVE, Gtk.ResponseType.OK), default_response=Gtk.ResponseType.OK, current_folder=gajim.config.get('last_save_dir'), on_response_ok=on_ok, on_response_cancel=on_cancel) dialog.set_current_name(default_name + '.jpeg') dialog.connect('delete-event', lambda widget, event: on_cancel(widget))
def on_file_request_accepted(self, account, contact, file_props): def on_ok(widget, account, contact, file_props): file_path = dialog2.get_filename() if os.path.exists(file_path): # check if we have write permissions if not os.access(file_path, os.W_OK): file_name = GLib.markup_escape_text(os.path.basename( file_path)) dialogs.ErrorDialog( _('Cannot overwrite existing file "%s"' % file_name), _('A file with this name already exists and you do not ' 'have permission to overwrite it.')) return stat = os.stat(file_path) dl_size = stat.st_size file_size = file_props.size dl_finished = dl_size >= file_size def on_response(response): if response < 0: return elif response == 100: file_props.offset = dl_size dialog2.destroy() self._start_receive(file_path, account, contact, file_props) dialog = dialogs.FTOverwriteConfirmationDialog( _('This file already exists'), _('What do you want to do?'), propose_resume=not dl_finished, on_response=on_response, transient_for=dialog2) dialog.set_destroy_with_parent(True) return else: dirname = os.path.dirname(file_path) if not os.access(dirname, os.W_OK) and os.name != 'nt': # read-only bit is used to mark special folder under # windows, not to mark that a folder is read-only. # See ticket #3587 dialogs.ErrorDialog(_('Directory "%s" is not writable') % \ dirname, _('You do not have permission to create files ' 'in this directory.')) return dialog2.destroy() self._start_receive(file_path, account, contact, file_props) def on_cancel(widget, account, contact, file_props): dialog2.destroy() gajim.connections[account].send_file_rejection(file_props) dialog2 = dialogs.FileChooserDialog( title_text=_('Save File as…'), action=Gtk.FileChooserAction.SAVE, buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_SAVE, Gtk.ResponseType.OK), default_response=Gtk.ResponseType.OK, current_folder=gajim.config.get('last_save_dir'), on_response_ok=(on_ok, account, contact, file_props), on_response_cancel=(on_cancel, account, contact, file_props)) dialog2.set_current_name(file_props.name) dialog2.connect('delete-event', lambda widget, event: on_cancel(widget, account, contact, file_props))