2012-12-18 17:37:55 -08:00
|
|
|
"""
|
|
|
|
A PyQT4 dialog to confirm rename
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
2014-03-23 10:30:23 -07:00
|
|
|
Copyright 2012-2014 Anthony Beville
|
2012-12-18 17:37:55 -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
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
from PyQt4 import QtCore, QtGui, uic
|
|
|
|
from settings import ComicTaggerSettings
|
|
|
|
from settingswindow import SettingsWindow
|
|
|
|
from filerenamer import FileRenamer
|
2013-02-13 13:54:07 -08:00
|
|
|
from comicarchive import MetaDataStyle
|
2013-01-21 20:09:08 -08:00
|
|
|
|
2012-12-18 17:37:55 -08:00
|
|
|
import os
|
|
|
|
import utils
|
|
|
|
|
|
|
|
class RenameWindow(QtGui.QDialog):
|
|
|
|
|
2013-01-18 19:12:25 -08:00
|
|
|
def __init__( self, parent, comic_archive_list, data_style, settings ):
|
2012-12-18 17:37:55 -08:00
|
|
|
super(RenameWindow, self).__init__(parent)
|
|
|
|
|
2013-02-06 14:03:53 -08:00
|
|
|
uic.loadUi(ComicTaggerSettings.getUIFile('renamewindow.ui' ), self)
|
2013-01-21 20:09:08 -08:00
|
|
|
self.label.setText("Preview (based on {0} tags):".format(MetaDataStyle.name[data_style]))
|
2012-12-18 17:37:55 -08:00
|
|
|
|
2013-02-04 19:51:50 -08:00
|
|
|
self.setWindowFlags(self.windowFlags() |
|
|
|
|
QtCore.Qt.WindowSystemMenuHint |
|
|
|
|
QtCore.Qt.WindowMaximizeButtonHint)
|
|
|
|
|
2012-12-18 17:37:55 -08:00
|
|
|
self.settings = settings
|
2013-01-18 19:12:25 -08:00
|
|
|
self.comic_archive_list = comic_archive_list
|
|
|
|
self.data_style = data_style
|
2012-12-18 17:37:55 -08:00
|
|
|
|
|
|
|
self.btnSettings.clicked.connect( self.modifySettings )
|
|
|
|
self.configRenamer()
|
|
|
|
self.doPreview()
|
|
|
|
|
|
|
|
def configRenamer( self ):
|
2013-01-18 19:12:25 -08:00
|
|
|
self.renamer = FileRenamer( None )
|
2012-12-18 17:37:55 -08:00
|
|
|
self.renamer.setTemplate( self.settings.rename_template )
|
|
|
|
self.renamer.setIssueZeroPadding( self.settings.rename_issue_number_padding )
|
|
|
|
self.renamer.setSmartCleanup( self.settings.rename_use_smart_string_cleanup )
|
|
|
|
|
|
|
|
def doPreview( self ):
|
2013-01-18 19:12:25 -08:00
|
|
|
self.rename_list = []
|
2013-01-20 00:36:21 -08:00
|
|
|
while self.twList.rowCount() > 0:
|
|
|
|
self.twList.removeRow(0)
|
|
|
|
|
|
|
|
self.twList.setSortingEnabled(False)
|
|
|
|
|
2013-01-18 19:12:25 -08:00
|
|
|
for ca in self.comic_archive_list:
|
2013-01-20 17:07:45 -08:00
|
|
|
|
|
|
|
new_ext = None # default
|
|
|
|
if self.settings.rename_extension_based_on_archive:
|
|
|
|
if ca.isZip():
|
|
|
|
new_ext = ".cbz"
|
|
|
|
elif ca.isRar():
|
|
|
|
new_ext = ".cbr"
|
|
|
|
|
2013-01-18 19:12:25 -08:00
|
|
|
md = ca.readMetadata(self.data_style)
|
|
|
|
if md.isEmpty:
|
2014-04-08 00:10:18 -07:00
|
|
|
md = ca.metadataFromFilename(self.settings.parse_scan_info)
|
2013-01-18 19:12:25 -08:00
|
|
|
self.renamer.setMetadata( md )
|
2013-01-20 17:07:45 -08:00
|
|
|
new_name = self.renamer.determineName( ca.path, ext=new_ext )
|
2013-01-18 19:12:25 -08:00
|
|
|
|
2013-01-20 00:36:21 -08:00
|
|
|
row = self.twList.rowCount()
|
|
|
|
self.twList.insertRow( row )
|
|
|
|
folder_item = QtGui.QTableWidgetItem()
|
|
|
|
old_name_item = QtGui.QTableWidgetItem()
|
|
|
|
new_name_item = QtGui.QTableWidgetItem()
|
|
|
|
|
|
|
|
item_text = os.path.split(ca.path)[0]
|
|
|
|
folder_item.setFlags(QtCore.Qt.ItemIsSelectable| QtCore.Qt.ItemIsEnabled)
|
|
|
|
self.twList.setItem(row, 0, folder_item)
|
|
|
|
folder_item.setText( item_text )
|
|
|
|
folder_item.setData( QtCore.Qt.ToolTipRole, item_text )
|
|
|
|
|
|
|
|
item_text = os.path.split(ca.path)[1]
|
|
|
|
old_name_item.setFlags(QtCore.Qt.ItemIsSelectable| QtCore.Qt.ItemIsEnabled)
|
|
|
|
self.twList.setItem(row, 1, old_name_item)
|
|
|
|
old_name_item.setText( item_text )
|
|
|
|
old_name_item.setData( QtCore.Qt.ToolTipRole, item_text )
|
|
|
|
|
|
|
|
new_name_item.setFlags(QtCore.Qt.ItemIsSelectable| QtCore.Qt.ItemIsEnabled)
|
|
|
|
self.twList.setItem(row, 2, new_name_item)
|
|
|
|
new_name_item.setText( new_name )
|
|
|
|
new_name_item.setData( QtCore.Qt.ToolTipRole, new_name )
|
|
|
|
|
2013-01-18 19:12:25 -08:00
|
|
|
dict_item = dict()
|
|
|
|
dict_item['archive'] = ca
|
|
|
|
dict_item['new_name'] = new_name
|
|
|
|
self.rename_list.append( dict_item)
|
|
|
|
|
2013-01-20 00:36:21 -08:00
|
|
|
# Adjust column sizes
|
|
|
|
self.twList.setVisible( False )
|
|
|
|
self.twList.resizeColumnsToContents()
|
|
|
|
self.twList.setVisible( True )
|
|
|
|
if self.twList.columnWidth(0) > 200:
|
|
|
|
self.twList.setColumnWidth(0, 200)
|
|
|
|
|
|
|
|
self.twList.setSortingEnabled(True)
|
2012-12-18 17:37:55 -08:00
|
|
|
|
|
|
|
def modifySettings( self ):
|
|
|
|
settingswin = SettingsWindow( self, self.settings )
|
|
|
|
settingswin.setModal(True)
|
|
|
|
settingswin.showRenameTab()
|
|
|
|
settingswin.exec_()
|
|
|
|
if settingswin.result():
|
|
|
|
self.configRenamer()
|
|
|
|
self.doPreview()
|
|
|
|
|
|
|
|
def accept( self ):
|
|
|
|
|
2013-01-18 22:15:33 -08:00
|
|
|
progdialog = QtGui.QProgressDialog("", "Cancel", 0, len(self.rename_list), self)
|
|
|
|
progdialog.setWindowTitle( "Renaming Archives" )
|
|
|
|
progdialog.setWindowModality(QtCore.Qt.WindowModal)
|
2013-01-20 22:39:44 -08:00
|
|
|
progdialog.show()
|
2013-01-18 22:15:33 -08:00
|
|
|
|
|
|
|
for idx,item in enumerate(self.rename_list):
|
|
|
|
|
|
|
|
QtCore.QCoreApplication.processEvents()
|
|
|
|
if progdialog.wasCanceled():
|
|
|
|
break
|
|
|
|
progdialog.setValue(idx)
|
2013-01-21 16:19:59 -08:00
|
|
|
idx += 1
|
2013-01-20 22:39:44 -08:00
|
|
|
progdialog.setLabelText( item['new_name'] )
|
2013-01-18 19:12:25 -08:00
|
|
|
|
|
|
|
if item['new_name'] == os.path.basename( item['archive'].path ):
|
|
|
|
print item['new_name'] , "Filename is already good!"
|
2013-01-18 22:15:33 -08:00
|
|
|
continue
|
2013-01-18 19:12:25 -08:00
|
|
|
|
2013-01-20 00:36:21 -08:00
|
|
|
if not item['archive'].isWritable(check_rar_status=False):
|
2013-01-18 22:15:33 -08:00
|
|
|
continue
|
2013-01-18 19:12:25 -08:00
|
|
|
|
|
|
|
folder = os.path.dirname( os.path.abspath( item['archive'].path ) )
|
|
|
|
new_abs_path = utils.unique_file( os.path.join( folder, item['new_name'] ) )
|
|
|
|
|
2013-01-21 16:19:59 -08:00
|
|
|
os.rename( item['archive'].path, new_abs_path)
|
2013-01-18 19:12:25 -08:00
|
|
|
|
|
|
|
item['archive'].rename( new_abs_path )
|
2013-01-18 22:15:33 -08:00
|
|
|
|
|
|
|
progdialog.close()
|
2013-01-23 10:54:44 -08:00
|
|
|
|
|
|
|
QtGui.QDialog.accept(self)
|