check file permissions before saving avatar
catch FileChooser 'response' signal, instead of dialog buttons 'clicked' signals (gtk+-2.10)
This commit is contained in:
parent
2897130af8
commit
70d1977c00
|
@ -837,27 +837,27 @@ class FileChooserDialog(gtk.FileChooserDialog):
|
||||||
self.set_current_folder(current_folder)
|
self.set_current_folder(current_folder)
|
||||||
else:
|
else:
|
||||||
self.set_current_folder(helpers.get_documents_path())
|
self.set_current_folder(helpers.get_documents_path())
|
||||||
|
self.response_ok, self.response_cancel = \
|
||||||
buttons = self.action_area.get_children()
|
on_response_ok, on_response_cancel
|
||||||
possible_responses = {gtk.STOCK_OPEN: on_response_ok,
|
# in gtk+-2.10 clicked signal on some of the buttons in a dialog
|
||||||
gtk.STOCK_SAVE: on_response_ok,
|
# is emitted twice, so we cannot rely on 'clicked' signal
|
||||||
gtk.STOCK_CANCEL: on_response_cancel}
|
self.connect('response', self.on_dialog_response)
|
||||||
for b in buttons:
|
|
||||||
for response in possible_responses:
|
|
||||||
if b.get_label() == response:
|
|
||||||
if not possible_responses[response]:
|
|
||||||
b.connect('clicked', self.just_destroy)
|
|
||||||
elif isinstance(possible_responses[response], tuple):
|
|
||||||
if len(possible_responses[response]) == 1:
|
|
||||||
b.connect('clicked', possible_responses[response][0])
|
|
||||||
else:
|
|
||||||
b.connect('clicked', *possible_responses[response])
|
|
||||||
else:
|
|
||||||
b.connect('clicked', possible_responses[response])
|
|
||||||
break
|
|
||||||
|
|
||||||
self.show_all()
|
self.show_all()
|
||||||
|
|
||||||
|
def on_dialog_response(self, dialog, response):
|
||||||
|
if response in (gtk.RESPONSE_CANCEL, gtk.RESPONSE_CLOSE):
|
||||||
|
if self.response_cancel:
|
||||||
|
if isinstance(self.response_cancel, tuple):
|
||||||
|
self.response_cancel[0](dialog, *self.response_cancel[1:])
|
||||||
|
else:
|
||||||
|
self.response_cancel(dialog)
|
||||||
|
elif response == gtk.RESPONSE_OK:
|
||||||
|
if self.response_ok:
|
||||||
|
if isinstance(self.response_ok, tuple):
|
||||||
|
self.response_ok[0](dialog, *self.response_ok[1:])
|
||||||
|
else:
|
||||||
|
self.response_ok(dialog)
|
||||||
|
|
||||||
def just_destroy(self, widget):
|
def just_destroy(self, widget):
|
||||||
self.destroy()
|
self.destroy()
|
||||||
|
|
||||||
|
|
|
@ -246,11 +246,16 @@ _('Connection with peer cannot be established.'))
|
||||||
gtk.RESPONSE_OK,
|
gtk.RESPONSE_OK,
|
||||||
True, # select multiple true as we can select many files to send
|
True, # select multiple true as we can select many files to send
|
||||||
gajim.config.get('last_send_dir'),
|
gajim.config.get('last_send_dir'),
|
||||||
|
on_response_ok = on_ok,
|
||||||
|
on_response_cancel = lambda e:dialog.destroy()
|
||||||
)
|
)
|
||||||
|
|
||||||
btn = dialog.add_button(_('_Send'), gtk.RESPONSE_OK)
|
btn = gtk.Button(_('_Send'))
|
||||||
btn.set_use_stock(True) # FIXME: add send icon to this button (JUMP_TO)
|
btn.set_property('can-default', True)
|
||||||
btn.connect('clicked', on_ok)
|
# FIXME: add send icon to this button (JUMP_TO)
|
||||||
|
dialog.add_action_widget(btn, gtk.RESPONSE_OK)
|
||||||
|
dialog.set_default_response(gtk.RESPONSE_OK)
|
||||||
|
btn.show()
|
||||||
|
|
||||||
def send_file(self, account, contact, file_path):
|
def send_file(self, account, contact, file_path):
|
||||||
''' start the real transfer(upload) of the file '''
|
''' start the real transfer(upload) of the file '''
|
||||||
|
|
|
@ -680,6 +680,14 @@ default_name = ''):
|
||||||
file_path = dialog.get_filename()
|
file_path = dialog.get_filename()
|
||||||
file_path = decode_filechooser_file_paths((file_path,))[0]
|
file_path = decode_filechooser_file_paths((file_path,))[0]
|
||||||
if os.path.exists(file_path):
|
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(
|
dialog2 = dialogs.FTOverwriteConfirmationDialog(
|
||||||
_('This file already exists'), _('What do you want to do?'),
|
_('This file already exists'), _('What do you want to do?'),
|
||||||
False)
|
False)
|
||||||
|
@ -688,6 +696,13 @@ default_name = ''):
|
||||||
response = dialog2.get_response()
|
response = dialog2.get_response()
|
||||||
if response < 0:
|
if response < 0:
|
||||||
return
|
return
|
||||||
|
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
|
||||||
|
|
||||||
# Get pixbuf
|
# Get pixbuf
|
||||||
pixbuf = None
|
pixbuf = None
|
||||||
|
@ -710,7 +725,7 @@ default_name = ''):
|
||||||
try:
|
try:
|
||||||
pixbuf.save(file_path, type_)
|
pixbuf.save(file_path, type_)
|
||||||
except:
|
except:
|
||||||
#XXX Check for permissions
|
if os.path.exists(file_path):
|
||||||
os.remove(file_path)
|
os.remove(file_path)
|
||||||
new_file_path = '.'.join(file_path.split('.')[:-1]) + '.jpeg'
|
new_file_path = '.'.join(file_path.split('.')[:-1]) + '.jpeg'
|
||||||
dialog2 = dialogs.ConfirmationDialog(_('Extension not supported'),
|
dialog2 = dialogs.ConfirmationDialog(_('Extension not supported'),
|
||||||
|
|
Loading…
Reference in New Issue