Add a recursive setting - fix #219

This commit is contained in:
Hugo Posnic 2024-05-06 20:12:27 +02:00
parent b05e9044a4
commit fb987241e8
7 changed files with 54 additions and 20 deletions

View File

@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file.
## UNRELEASED ## UNRELEASED
### Added ### Added
- Add a "Recursive Compression" setting.
- Add Bulgarian translation. Thank's to @twlvnn. - Add Bulgarian translation. Thank's to @twlvnn.
- Add Hindi translation. Thank's to @Scrambled777. - Add Hindi translation. Thank's to @Scrambled777.
@ -12,6 +13,7 @@ All notable changes to this project will be documented in this file.
### Fixed ### Fixed
- Fix opening files with "Open With...". Thank's to @ARAKHN1D. - Fix opening files with "Open With...". Thank's to @ARAKHN1D.
- Fix DnD with nested folders (recursive).
## 1.9.1 - 2024-04-12 ## 1.9.1 - 2024-04-12
### Fixed ### Fixed

View File

@ -6,6 +6,11 @@
<summary>Save into a new file</summary> <summary>Save into a new file</summary>
<description>Save the compressed image into a new file.</description> <description>Save the compressed image into a new file.</description>
</key> </key>
<key type="b" name="recursive">
<default>true</default>
<summary>Enable recursive compression in folders</summary>
<description>This setting enable compression in a recursive way in folders.</description>
</key>
<key type="b" name="metadata"> <key type="b" name="metadata">
<default>true</default> <default>true</default>
<summary>Keep metadata</summary> <summary>Keep metadata</summary>

View File

@ -3,7 +3,7 @@
<menu id="window-menu"> <menu id="window-menu">
<section> <section>
<item> <item>
<attribute name="label" translatable="yes">Bulk Compress Directory (Recursive)</attribute> <attribute name="label" translatable="yes">Bulk Compress Directory</attribute>
<attribute name="action">win.convert-dir</attribute> <attribute name="action">win.convert-dir</attribute>
</item> </item>
<item> <item>

View File

@ -26,6 +26,18 @@
<property name="title" translatable="yes">New File Suffix</property> <property name="title" translatable="yes">New File Suffix</property>
</object> </object>
</child> </child>
<child>
<object class="AdwActionRow">
<property name="title" translatable="yes">Recursive Compression</property>
<property name="subtitle" translatable="yes">Enable or disable recursive compression through subdirectories</property>
<property name="activatable-widget">toggle_recursive</property>
<child>
<object class="GtkSwitch" id="toggle_recursive">
<property name="valign">center</property>
</object>
</child>
</object>
</child>
<child> <child>
<object class="AdwActionRow"> <object class="AdwActionRow">
<property name="title" translatable="yes">Keep Metadata</property> <property name="title" translatable="yes">Keep Metadata</property>
@ -76,8 +88,8 @@
</child> </child>
<child> <child>
<object class="AdwPreferencesPage"> <object class="AdwPreferencesPage">
<property name="name">compression</property> <property name="name">formats</property>
<property name="title" translatable="yes">Compression</property> <property name="title" translatable="yes">Formats</property>
<property name="icon-name">image-x-generic-symbolic</property> <property name="icon-name">image-x-generic-symbolic</property>
<child> <child>
<object class="AdwPreferencesGroup"> <object class="AdwPreferencesGroup">

View File

@ -26,6 +26,7 @@ SETTINGS_SCHEMA = 'com.github.huluti.Curtail'
class CurtailPrefsWindow(Adw.PreferencesWindow): class CurtailPrefsWindow(Adw.PreferencesWindow):
__gtype_name__ = 'CurtailPrefsWindow' __gtype_name__ = 'CurtailPrefsWindow'
toggle_recursive = Gtk.Template.Child()
toggle_metadata = Gtk.Template.Child() toggle_metadata = Gtk.Template.Child()
toggle_file_attributes = Gtk.Template.Child() toggle_file_attributes = Gtk.Template.Child()
toggle_new_file = Gtk.Template.Child() toggle_new_file = Gtk.Template.Child()
@ -53,6 +54,11 @@ class CurtailPrefsWindow(Adw.PreferencesWindow):
def build_ui(self): def build_ui(self):
# Compression settings # Compression settings
# Recursive
self.toggle_recursive.set_active(self._settings.get_boolean('recursive'))
self.toggle_recursive.connect('notify::active', self.on_bool_changed,
'recursive')
# Keep metadata # Keep metadata
self.toggle_metadata.set_active(self._settings.get_boolean('metadata')) self.toggle_metadata.set_active(self._settings.get_boolean('metadata'))
self.toggle_metadata.connect('notify::active', self.on_bool_changed, self.toggle_metadata.connect('notify::active', self.on_bool_changed,

View File

@ -109,15 +109,26 @@ def create_image_from_file(filename, max_width, max_height):
return image return image
def get_image_files_from_folder(folder_path): def get_image_files_from_folder(folder_path):
images = []
for file in os.listdir(folder_path):
path = os.path.join(folder_path, file)
if os.path.isfile(path):
if get_file_type(path) is not None:
image_file = Gio.File.new_for_path(path)
images.append(image_file)
return images
def get_image_files_from_folder_recursive(folder_path):
images = [] images = []
for root, dirs, files in os.walk(folder_path): for root, dirs, files in os.walk(folder_path):
for file in files: for file in files:
path = os.path.join(root, file) path = os.path.join(root, file)
if get_file_type(path) != None: if get_file_type(path) is not None:
image_file = Gio.File.new_for_path(path) image_file = Gio.File.new_for_path(path)
images.append(image_file) images.append(image_file)
return images return images
def debug_infos(): def debug_infos():

View File

@ -197,8 +197,7 @@ class CurtailWindow(Adw.ApplicationWindow):
filenames = list() filenames = list()
for file in files: for file in files:
filenames.append(file.get_uri()) filenames.append(file.get_uri())
final_filenames = self.handle_filenames(filenames) self.compress_filenames(filenames)
self.compress_filenames(final_filenames)
dialog.open_multiple(self, None, handle_response) dialog.open_multiple(self, None, handle_response)
@ -210,13 +209,8 @@ class CurtailWindow(Adw.ApplicationWindow):
if response == "compress": if response == "compress":
filenames = list() filenames = list()
for folder in folders: for folder in folders:
images = get_image_files_from_folder(folder.get_path()) filenames.append(folder.get_path())
self.compress_filenames(filenames)
for image in images:
filenames.append(image.get_uri())
final_filenames = self.handle_filenames(filenames)
self.compress_filenames(final_filenames)
try: try:
folders = dialog.select_multiple_folders_finish(result) folders = dialog.select_multiple_folders_finish(result)
@ -261,9 +255,7 @@ class CurtailWindow(Adw.ApplicationWindow):
filenames = [] filenames = []
for file in files: for file in files:
filenames.append(file.get_uri()) filenames.append(file.get_uri())
self.compress_filenames(filenames)
final_filenames = self.handle_filenames(filenames)
self.compress_filenames(final_filenames)
def handle_filenames(self, filenames): def handle_filenames(self, filenames):
final_filenames = [] final_filenames = []
@ -273,9 +265,13 @@ class CurtailWindow(Adw.ApplicationWindow):
path = Path(filename) path = Path(filename)
if path.is_dir(): if path.is_dir():
for new_filename in path.rglob("*"): if self._settings.get_boolean('recursive'):
new_filename = self.clean_filename(new_filename) images = get_image_files_from_folder_recursive(path)
final_filenames.append(new_filename) else:
images = get_image_files_from_folder(path)
for image in images:
image = self.clean_filename(image)
final_filenames.append(image)
else: else:
final_filenames.append(filename) final_filenames.append(filename)
@ -307,6 +303,8 @@ class CurtailWindow(Adw.ApplicationWindow):
return new_filename return new_filename
def compress_filenames(self, filenames): def compress_filenames(self, filenames):
filenames = self.handle_filenames(filenames)
result_items = [] result_items = []
for filename in filenames: for filename in filenames:
error_message = False error_message = False