From b5f242dc77a192aaa09dbb37bd3ad843fd95ff8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ionu=C8=9B=20Cioc=C3=AErlan?= Date: Thu, 6 Jun 2013 20:19:13 +0300 Subject: [PATCH] new renaming setting --- picard/file.py | 3 ++- picard/ui/options/renaming.py | 44 ++++++++++++++++++++++++++++++++--- ui/options_renaming.ui | 37 +++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 4 deletions(-) diff --git a/picard/file.py b/picard/file.py index b68f62923..b2c59425d 100644 --- a/picard/file.py +++ b/picard/file.py @@ -280,7 +280,8 @@ class File(QtCore.QObject, Item): new_filename = self._script_to_filename(format, metadata, settings) if not settings['move_files']: new_filename = os.path.basename(new_filename) - new_filename = make_short_filename(new_dirname, new_filename, settings['windows_compatibility']) + new_filename = make_short_filename(new_dirname, new_filename, + settings['windows_compatibility'], settings['move_files_ancestor']) # win32 compatibility fixes if settings['windows_compatibility'] or sys.platform == 'win32': new_filename = new_filename.replace('./', '_/').replace('.\\', '_\\') diff --git a/picard/ui/options/renaming.py b/picard/ui/options/renaming.py index 73829eedd..62ed6c1be 100644 --- a/picard/ui/options/renaming.py +++ b/picard/ui/options/renaming.py @@ -43,6 +43,7 @@ class RenamingOptionsPage(OptionsPage): TextOption("setting", "file_naming_format", "$if2(%albumartist%,%artist%)/%album%/$if($gt(%totaldiscs%,1),%discnumber%-,)$num(%tracknumber%,2)$if(%compilation%, %artist% -,) %title%"), BoolOption("setting", "move_files", False), TextOption("setting", "move_files_to", ""), + TextOption("setting", "move_files_ancestor", ""), BoolOption("setting", "move_additional_files", False), TextOption("setting", "move_additional_files_pattern", "*.jpg *.png"), BoolOption("setting", "delete_empty_dirs", True), @@ -58,6 +59,7 @@ class RenamingOptionsPage(OptionsPage): self.ui.rename_files.clicked.connect(self.update_examples) self.ui.move_files.clicked.connect(self.update_examples) self.ui.move_files_to.editingFinished.connect(self.update_examples) + self.ui.move_files_ancestor.editingFinished.connect(self.update_examples) self.ui.rename_files.stateChanged.connect(self.ui.ascii_filenames.setEnabled) self.ui.rename_files.stateChanged.connect(self.ui.file_naming_format.setEnabled) @@ -65,6 +67,9 @@ class RenamingOptionsPage(OptionsPage): if not sys.platform == "win32": self.ui.rename_files.stateChanged.connect(self.ui.windows_compatibility.setEnabled) + self.ui.rename_files.stateChanged.connect(self.toggle_move_files_ancestor_enabled) + self.ui.windows_compatibility.stateChanged.connect(self.toggle_move_files_ancestor_enabled) + self.ui.move_files.stateChanged.connect(self.toggle_move_files_ancestor_enabled) self.ui.move_files.stateChanged.connect(self.ui.delete_empty_dirs.setEnabled) self.ui.move_files.stateChanged.connect(self.ui.move_files_to.setEnabled) @@ -75,6 +80,16 @@ class RenamingOptionsPage(OptionsPage): self.ui.file_naming_format_default.clicked.connect(self.set_file_naming_format_default) self.highlighter = TaggerScriptSyntaxHighlighter(self.ui.file_naming_format.document()) self.ui.move_files_to_browse.clicked.connect(self.move_files_to_browse) + self.ui.move_files_ancestor_browse.clicked.connect(self.move_files_ancestor_browse) + + def toggle_move_files_ancestor_enabled(self): + enabled = self.ui.rename_files.isChecked() and \ + self.ui.windows_compatibility.isChecked() and \ + self.ui.windows_compatibility.isEnabled() and \ + self.ui.move_files.isChecked() + parentLayout = self.ui.move_files_ancestorLayout + for i in range(parentLayout.count()): + parentLayout.itemAt(i).widget().setEnabled(enabled) def check_formats(self): self.test() @@ -88,7 +103,8 @@ class RenamingOptionsPage(OptionsPage): 'move_files': self.ui.move_files.isChecked(), 'use_va_format': False, # TODO remove 'file_naming_format': unicode(self.ui.file_naming_format.toPlainText()), - 'move_files_to': os.path.normpath(unicode(self.ui.move_files_to.text())) + 'move_files_to': os.path.normpath(unicode(self.ui.move_files_to.text())), + 'move_files_ancestor': unicode(self.ui.move_files_ancestor.text()) } try: if self.config.setting["enable_tagger_script"]: @@ -123,6 +139,8 @@ class RenamingOptionsPage(OptionsPage): self.ui.file_naming_format.setPlainText(self.config.setting["file_naming_format"]) self.ui.move_files_to.setText(self.config.setting["move_files_to"]) self.ui.move_files_to.setCursorPosition(0) + self.ui.move_files_ancestor.setText(self.config.setting["move_files_ancestor"]) + self.ui.move_files_ancestor.setCursorPosition(0) self.ui.move_additional_files.setChecked(self.config.setting["move_additional_files"]) self.ui.move_additional_files_pattern.setText(self.config.setting["move_additional_files_pattern"]) self.ui.delete_empty_dirs.setChecked(self.config.setting["delete_empty_dirs"]) @@ -130,8 +148,21 @@ class RenamingOptionsPage(OptionsPage): def check(self): self.check_format() - if self.ui.move_files.isChecked() and not unicode(self.ui.move_files_to.text()).strip(): - raise OptionsCheckError(_("Error"), _("The location to move files to must not be empty.")) + if self.ui.move_files.isChecked(): + move_files_to = unicode(self.ui.move_files_to.text()).strip() + if not move_files_to: + raise OptionsCheckError(_("Error"), _("The location to move files to must not be empty.")) + if self.ui.windows_compatibility.isChecked() and self.ui.windows_compatibility.isEnabled(): + move_files_ancestor = unicode(self.ui.move_files_ancestor.text()).strip() + if move_files_ancestor: + move_files_to = os.path.abspath(move_files_to) + move_files_ancestor = os.path.abspath(move_files_ancestor) + if not move_files_to.startswith(move_files_ancestor) or \ + move_files_to.split(move_files_ancestor)[1][:1] not in (os.path.sep, ""): + raise OptionsCheckError( + _("Error"), + _("Compatibility must be applied against an ancestor of where files are moved to.") + ) def check_format(self): parser = ScriptParser() @@ -151,6 +182,7 @@ class RenamingOptionsPage(OptionsPage): self.tagger.window.enable_renaming_action.setChecked(self.config.setting["rename_files"]) self.config.setting["move_files"] = self.ui.move_files.isChecked() self.config.setting["move_files_to"] = os.path.normpath(unicode(self.ui.move_files_to.text())) + self.config.setting["move_files_ancestor"] = unicode(self.ui.move_files_ancestor.text()) self.config.setting["move_additional_files"] = self.ui.move_additional_files.isChecked() self.config.setting["move_additional_files_pattern"] = unicode(self.ui.move_additional_files_pattern.text()) self.config.setting["delete_empty_dirs"] = self.ui.delete_empty_dirs.isChecked() @@ -220,6 +252,12 @@ class RenamingOptionsPage(OptionsPage): path = os.path.normpath(unicode(path)) self.ui.move_files_to.setText(path) + def move_files_ancestor_browse(self): + path = QtGui.QFileDialog.getExistingDirectory(self, "", self.ui.move_files_ancestor.text()) + if path: + path = os.path.normpath(unicode(path)) + self.ui.move_files_ancestor.setText(path) + def test(self): self.ui.renaming_error.setStyleSheet(""); self.ui.renaming_error.setText("") diff --git a/ui/options_renaming.ui b/ui/options_renaming.ui index 8e05f07ec..7131be922 100644 --- a/ui/options_renaming.ui +++ b/ui/options_renaming.ui @@ -72,6 +72,43 @@ + + + + 2 + + + + + false + + + Apply compatibility against: + + + + + + + false + + + + + + + + + + false + + + Browse... + + + + +