2015-02-21 18:30:32 -08:00
|
|
|
"""A PyQT4 dialog to confirm rename"""
|
2012-12-18 17:37:55 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
# Copyright 2012-2014 Anthony Beville
|
2012-12-18 17:37:55 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
2012-12-18 17:37:55 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2012-12-18 17:37:55 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2012-12-18 17:37:55 -08:00
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
import os
|
2012-12-18 17:37:55 -08:00
|
|
|
|
|
|
|
from PyQt4 import QtCore, QtGui, uic
|
2015-02-13 15:08:07 -08:00
|
|
|
|
2012-12-18 17:37:55 -08:00
|
|
|
from settings import ComicTaggerSettings
|
|
|
|
from settingswindow import SettingsWindow
|
|
|
|
from filerenamer import FileRenamer
|
2013-02-13 13:54:07 -08:00
|
|
|
from comicarchive import MetaDataStyle
|
2012-12-18 17:37:55 -08:00
|
|
|
import utils
|
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
|
2012-12-18 17:37:55 -08:00
|
|
|
class RenameWindow(QtGui.QDialog):
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
def __init__(self, parent, comic_archive_list, data_style, settings):
|
2015-02-12 14:57:46 -08:00
|
|
|
super(RenameWindow, self).__init__(parent)
|
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
uic.loadUi(ComicTaggerSettings.getUIFile('renamewindow.ui'), self)
|
2015-02-15 02:44:00 -08:00
|
|
|
self.label.setText(
|
2015-02-15 03:55:04 -08:00
|
|
|
"Preview (based on {0} tags):".format(
|
|
|
|
MetaDataStyle.name[data_style]))
|
2015-02-12 14:57:46 -08:00
|
|
|
|
|
|
|
self.setWindowFlags(self.windowFlags() |
|
2015-02-15 02:44:00 -08:00
|
|
|
QtCore.Qt.WindowSystemMenuHint |
|
|
|
|
QtCore.Qt.WindowMaximizeButtonHint)
|
2015-02-12 14:57:46 -08:00
|
|
|
|
|
|
|
self.settings = settings
|
|
|
|
self.comic_archive_list = comic_archive_list
|
|
|
|
self.data_style = data_style
|
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
self.btnSettings.clicked.connect(self.modifySettings)
|
2015-02-12 14:57:46 -08:00
|
|
|
self.configRenamer()
|
|
|
|
self.doPreview()
|
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
def configRenamer(self):
|
|
|
|
self.renamer = FileRenamer(None)
|
|
|
|
self.renamer.setTemplate(self.settings.rename_template)
|
2015-02-15 02:44:00 -08:00
|
|
|
self.renamer.setIssueZeroPadding(
|
|
|
|
self.settings.rename_issue_number_padding)
|
|
|
|
self.renamer.setSmartCleanup(
|
|
|
|
self.settings.rename_use_smart_string_cleanup)
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
def doPreview(self):
|
2015-02-12 14:57:46 -08:00
|
|
|
self.rename_list = []
|
|
|
|
while self.twList.rowCount() > 0:
|
|
|
|
self.twList.removeRow(0)
|
|
|
|
|
|
|
|
self.twList.setSortingEnabled(False)
|
|
|
|
|
|
|
|
for ca in self.comic_archive_list:
|
|
|
|
|
|
|
|
new_ext = None # default
|
|
|
|
if self.settings.rename_extension_based_on_archive:
|
|
|
|
if ca.isZip():
|
|
|
|
new_ext = ".cbz"
|
|
|
|
elif ca.isRar():
|
|
|
|
new_ext = ".cbr"
|
|
|
|
|
|
|
|
md = ca.readMetadata(self.data_style)
|
|
|
|
if md.isEmpty:
|
|
|
|
md = ca.metadataFromFilename(self.settings.parse_scan_info)
|
2015-02-13 15:08:07 -08:00
|
|
|
self.renamer.setMetadata(md)
|
|
|
|
new_name = self.renamer.determineName(ca.path, ext=new_ext)
|
2015-02-12 14:57:46 -08:00
|
|
|
|
|
|
|
row = self.twList.rowCount()
|
2015-02-13 15:08:07 -08:00
|
|
|
self.twList.insertRow(row)
|
2015-02-15 02:44:00 -08:00
|
|
|
folder_item = QtGui.QTableWidgetItem()
|
2015-02-12 14:57:46 -08:00
|
|
|
old_name_item = QtGui.QTableWidgetItem()
|
|
|
|
new_name_item = QtGui.QTableWidgetItem()
|
|
|
|
|
|
|
|
item_text = os.path.split(ca.path)[0]
|
2015-02-15 02:44:00 -08:00
|
|
|
folder_item.setFlags(
|
|
|
|
QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
|
2015-02-12 14:57:46 -08:00
|
|
|
self.twList.setItem(row, 0, folder_item)
|
2015-02-13 15:08:07 -08:00
|
|
|
folder_item.setText(item_text)
|
|
|
|
folder_item.setData(QtCore.Qt.ToolTipRole, item_text)
|
2015-02-12 14:57:46 -08:00
|
|
|
|
|
|
|
item_text = os.path.split(ca.path)[1]
|
2015-02-15 02:44:00 -08:00
|
|
|
old_name_item.setFlags(
|
|
|
|
QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
|
2015-02-12 14:57:46 -08:00
|
|
|
self.twList.setItem(row, 1, old_name_item)
|
2015-02-13 15:08:07 -08:00
|
|
|
old_name_item.setText(item_text)
|
|
|
|
old_name_item.setData(QtCore.Qt.ToolTipRole, item_text)
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2015-02-15 02:44:00 -08:00
|
|
|
new_name_item.setFlags(
|
|
|
|
QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
|
2015-02-12 14:57:46 -08:00
|
|
|
self.twList.setItem(row, 2, new_name_item)
|
2015-02-13 15:08:07 -08:00
|
|
|
new_name_item.setText(new_name)
|
|
|
|
new_name_item.setData(QtCore.Qt.ToolTipRole, new_name)
|
2015-02-12 14:57:46 -08:00
|
|
|
|
|
|
|
dict_item = dict()
|
|
|
|
dict_item['archive'] = ca
|
|
|
|
dict_item['new_name'] = new_name
|
2015-02-13 15:08:07 -08:00
|
|
|
self.rename_list.append(dict_item)
|
2015-02-12 14:57:46 -08:00
|
|
|
|
|
|
|
# Adjust column sizes
|
2015-02-13 15:08:07 -08:00
|
|
|
self.twList.setVisible(False)
|
2015-02-12 14:57:46 -08:00
|
|
|
self.twList.resizeColumnsToContents()
|
2015-02-13 15:08:07 -08:00
|
|
|
self.twList.setVisible(True)
|
2015-02-12 14:57:46 -08:00
|
|
|
if self.twList.columnWidth(0) > 200:
|
|
|
|
self.twList.setColumnWidth(0, 200)
|
|
|
|
|
|
|
|
self.twList.setSortingEnabled(True)
|
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
def modifySettings(self):
|
|
|
|
settingswin = SettingsWindow(self, self.settings)
|
2015-02-12 14:57:46 -08:00
|
|
|
settingswin.setModal(True)
|
|
|
|
settingswin.showRenameTab()
|
|
|
|
settingswin.exec_()
|
|
|
|
if settingswin.result():
|
|
|
|
self.configRenamer()
|
|
|
|
self.doPreview()
|
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
def accept(self):
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2015-02-15 02:44:00 -08:00
|
|
|
progdialog = QtGui.QProgressDialog(
|
|
|
|
"", "Cancel", 0, len(self.rename_list), self)
|
2015-02-13 15:08:07 -08:00
|
|
|
progdialog.setWindowTitle("Renaming Archives")
|
2015-02-12 14:57:46 -08:00
|
|
|
progdialog.setWindowModality(QtCore.Qt.WindowModal)
|
|
|
|
progdialog.show()
|
|
|
|
|
2015-02-15 02:44:00 -08:00
|
|
|
for idx, item in enumerate(self.rename_list):
|
2015-02-12 14:57:46 -08:00
|
|
|
|
|
|
|
QtCore.QCoreApplication.processEvents()
|
|
|
|
if progdialog.wasCanceled():
|
|
|
|
break
|
|
|
|
progdialog.setValue(idx)
|
|
|
|
idx += 1
|
2015-02-13 15:08:07 -08:00
|
|
|
progdialog.setLabelText(item['new_name'])
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
if item['new_name'] == os.path.basename(item['archive'].path):
|
2015-02-15 02:44:00 -08:00
|
|
|
print item['new_name'], "Filename is already good!"
|
2015-02-12 14:57:46 -08:00
|
|
|
continue
|
|
|
|
|
|
|
|
if not item['archive'].isWritable(check_rar_status=False):
|
|
|
|
continue
|
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
folder = os.path.dirname(os.path.abspath(item['archive'].path))
|
2015-02-15 02:44:00 -08:00
|
|
|
new_abs_path = utils.unique_file(
|
|
|
|
os.path.join(folder, item['new_name']))
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
os.rename(item['archive'].path, new_abs_path)
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
item['archive'].rename(new_abs_path)
|
2015-02-12 14:57:46 -08:00
|
|
|
|
|
|
|
progdialog.close()
|
|
|
|
|
|
|
|
QtGui.QDialog.accept(self)
|