Skip to content

Commit

Permalink
Also warn about read-only files just before saving
Browse files Browse the repository at this point in the history
  • Loading branch information
kovidgoyal committed Dec 25, 2022
1 parent d185190 commit 5c30cc1
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/calibre/gui2/tweak_book/boss.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class Boss(QObject):
def __init__(self, parent, notify=None):
QObject.__init__(self, parent)
self.global_undo = GlobalUndoHistory()
self.file_was_readonly = False
self.container_count = 0
self.tdir = None
self.save_manager = SaveManager(parent, notify)
Expand Down Expand Up @@ -318,9 +319,6 @@ def open_book(self, path=None, edit_file=None, clear_notify_data=True, open_fold
if not os.path.exists(path):
return error_dialog(self.gui, _('File not found'), _(
'The file %s does not exist.') % path, show=True)
if not os.access(path, os.W_OK):
warning_dialog(self.gui, _('Read-only file'), _(
'The file {} is read-only. Saving changes to it will either fail or cause its permissions to be reset.').format(path), show=True)
isdir = os.path.isdir(path)
ext = path.rpartition('.')[-1].upper()
if ext not in SUPPORTED and not isdir:
Expand All @@ -332,6 +330,11 @@ def open_book(self, path=None, edit_file=None, clear_notify_data=True, open_fold
' Convert your book to one of these formats first.') % _(' and ').join(sorted(SUPPORTED)),
show=True)

self.file_was_readonly = not os.access(path, os.W_OK)
if self.file_was_readonly:
warning_dialog(self.gui, _('Read-only file'), _(
'The file {} is read-only. Saving changes to it will either fail or cause its permissions to be reset.').format(path), show=True)

for name in tuple(editors):
self.close_editor(name)
self.gui.preview.clear()
Expand Down Expand Up @@ -1302,6 +1305,11 @@ def save_book(self):
self.global_undo.update_path_to_ebook(path)
else:
return
if os.path.exists(c.path_to_ebook) and not os.access(c.path_to_ebook, os.W_OK):
if not question_dialog(self.gui, _('File is read-only'), _(
'The file at {} is read-only. The editor will try to reset its permissions before saving. Proceed with saving?'
).format(c.path_to_ebook), override_icon='dialog_warning.png', yes_text=_('&Save'), no_text=_('&Cancel'), yes_icon='save.png'):
return
self.gui.action_save.setEnabled(False)
tdir = self.mkdtemp(prefix='save-')
container = clone_container(c, tdir)
Expand Down

0 comments on commit 5c30cc1

Please sign in to comment.