Commit 70d1977c authored by dkirov's avatar dkirov
Browse files

check file permissions before saving avatar

catch FileChooser 'response' signal, instead
of dialog buttons 'clicked' signals (gtk+-2.10)
parent 2897130a
......@@ -837,27 +837,27 @@ class FileChooserDialog(gtk.FileChooserDialog):
self.set_current_folder(current_folder)
else:
self.set_current_folder(helpers.get_documents_path())
buttons = self.action_area.get_children()
possible_responses = {gtk.STOCK_OPEN: on_response_ok,
gtk.STOCK_SAVE: on_response_ok,
gtk.STOCK_CANCEL: on_response_cancel}
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.response_ok, self.response_cancel = \
on_response_ok, on_response_cancel
# in gtk+-2.10 clicked signal on some of the buttons in a dialog
# is emitted twice, so we cannot rely on 'clicked' signal
self.connect('response', self.on_dialog_response)
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):
self.destroy()
......
......@@ -246,11 +246,16 @@ _('Connection with peer cannot be established.'))
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 = dialog.add_button(_('_Send'), gtk.RESPONSE_OK)
btn.set_use_stock(True) # FIXME: add send icon to this button (JUMP_TO)
btn.connect('clicked', on_ok)
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)
btn.show()
def send_file(self, account, contact, file_path):
''' start the real transfer(upload) of the file '''
......
......@@ -680,6 +680,14 @@ default_name = ''):
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?'),
False)
......@@ -688,6 +696,13 @@ default_name = ''):
response = dialog2.get_response()
if response < 0:
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
pixbuf = None
......@@ -710,8 +725,8 @@ default_name = ''):
try:
pixbuf.save(file_path, type_)
except:
#XXX Check for permissions
os.remove(file_path)
if os.path.exists(file_path):
os.remove(file_path)
new_file_path = '.'.join(file_path.split('.')[:-1]) + '.jpeg'
dialog2 = 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},
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment