Skip to content

Commit

Permalink
Use upcounting filename index when exporting
Browse files Browse the repository at this point in the history
"first_imported_file-0001.pdf" instead of suggesting overwriting
"first_imported_file.pdf". Count up one on every export.

Close pdfarranger#758
  • Loading branch information
kbengs committed Nov 22, 2022
1 parent 004d6be commit 65f5d95
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions pdfarranger/pdfarranger.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ def __init__(self, *args, **kwargs):
self.export_process = None
self.post_action = None
self.export_file = None
self.export_counter = 1
self.drag_path = None
self.drag_pos = Gtk.IconViewDropPosition.DROP_RIGHT
self.window_width_old = 0
Expand Down Expand Up @@ -958,6 +959,7 @@ def clear_data(self):
self.metadata = {}
self.undomanager.clear()
self.set_export_file(None)
self.export_counter = 1
self.set_unsaved(False)
self.update_statusbar()
malloc_trim()
Expand Down Expand Up @@ -1018,7 +1020,8 @@ def close_application(self, _widget=None, _event=None, _data=None):

def choose_export_pdf_name(self, mode):
"""Handles choosing a name for exporting """
title = _('Save As…') if mode == GLib.Variant('i', 0) else _('Export…')
save_all = mode == GLib.Variant('i', 0)
title = _("Save As…") if save_all else _("Export…")

chooser = Gtk.FileChooserNative.new(title=title,
parent=self.window,
Expand All @@ -1027,10 +1030,16 @@ def choose_export_pdf_name(self, mode):
cancel_label=_("_Cancel"))
chooser.set_do_overwrite_confirmation(True)
if len(self.pdfqueue) > 0:
f = self.pdfqueue[0].filename
# could be an image thanks to img2pdf
if f.endswith(".pdf"):
chooser.set_filename(f)
if save_all:
f = self.pdfqueue[0].filename
if f.endswith(".pdf"):
chooser.set_filename(f) # Set name to existing file
else:
f = self.pdfqueue[0].basename
shortname, ext = os.path.splitext(f)
f = shortname + "-" + str(self.export_counter).zfill(4) + ext
if f.endswith(".pdf"):
chooser.set_current_name(f) # Set name to new file
chooser.set_current_folder(self.export_directory)
filter_list = self.__create_filters(['pdf', 'all'])
for f in filter_list[1:]:
Expand All @@ -1041,6 +1050,8 @@ def choose_export_pdf_name(self, mode):
chooser.destroy()
if response == Gtk.ResponseType.ACCEPT:
self.save(mode, file_out)
if not save_all:
self.export_counter += 1
else:
self.post_action = None

Expand Down

0 comments on commit 65f5d95

Please sign in to comment.