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.FileChooserAction.SAVE, buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_SAVE, Gtk.ResponseType.OK), default_response=Gtk.ResponseType.OK, current_folder=app.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 gajim.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: app.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 app.config.get('last_send_dir'), on_response_ok=on_ok, on_response_cancel=lambda e: dialog.destroy(), transient_for=app.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 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() app.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=app.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_avatar_save_as_menuitem_activate(widget, avatar, default_name=''): def on_continue(response, file_path): if response < 0: return if isinstance(avatar, str): # We got a SHA pixbuf = app.interface.get_avatar(avatar) else: # We got a pixbuf pixbuf = avatar 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.error('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=app.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))