home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1259 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  6.7 KB  |  161 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
  5. __docformat__ = 'restructuredtext en'
  6. __license__ = 'GPL v3'
  7. import os
  8. from PyQt4.Qt import QDialog, QVBoxLayout, QHBoxLayout, QTreeWidget, QLabel, QPushButton, QDialogButtonBox, QApplication, QTreeWidgetItem, QLineEdit, Qt
  9. from calibre.gui2.dialogs.confirm_delete import confirm
  10. from calibre.library.check_library import CheckLibrary, CHECKS
  11. from calibre.library.database2 import delete_file, delete_tree
  12. from calibre import prints
  13.  
  14. class Item(QTreeWidgetItem):
  15.     pass
  16.  
  17.  
  18. class CheckLibraryDialog(QDialog):
  19.     
  20.     def __init__(self, parent, db):
  21.         QDialog.__init__(self, parent)
  22.         self.db = db
  23.         self.setWindowTitle(_('Check Library'))
  24.         self._layout = QVBoxLayout(self)
  25.         self.setLayout(self._layout)
  26.         self.log = QTreeWidget(self)
  27.         self.log.itemChanged.connect(self.item_changed)
  28.         self._layout.addWidget(self.log)
  29.         self.check = QPushButton(_('&Run the check'))
  30.         self.check.setDefault(False)
  31.         self.check.clicked.connect(self.run_the_check)
  32.         self.copy = QPushButton(_('Copy &to clipboard'))
  33.         self.copy.setDefault(False)
  34.         self.copy.clicked.connect(self.copy_to_clipboard)
  35.         self.ok = QPushButton('&Done')
  36.         self.ok.setDefault(True)
  37.         self.ok.clicked.connect(self.accept)
  38.         self.delete = QPushButton('Delete &marked')
  39.         self.delete.setDefault(False)
  40.         self.delete.clicked.connect(self.delete_marked)
  41.         self.bbox = QDialogButtonBox(self)
  42.         self.bbox.addButton(self.check, QDialogButtonBox.ActionRole)
  43.         self.bbox.addButton(self.delete, QDialogButtonBox.ActionRole)
  44.         self.bbox.addButton(self.copy, QDialogButtonBox.ActionRole)
  45.         self.bbox.addButton(self.ok, QDialogButtonBox.AcceptRole)
  46.         h = QHBoxLayout()
  47.         ln = QLabel(_('Names to ignore:'))
  48.         h.addWidget(ln)
  49.         self.name_ignores = QLineEdit()
  50.         self.name_ignores.setText(db.prefs.get('check_library_ignore_names', ''))
  51.         self.name_ignores.setToolTip(_('Enter comma-separated standard file name wildcards, such as synctoy*.dat'))
  52.         ln.setBuddy(self.name_ignores)
  53.         h.addWidget(self.name_ignores)
  54.         le = QLabel(_('Extensions to ignore'))
  55.         h.addWidget(le)
  56.         self.ext_ignores = QLineEdit()
  57.         self.ext_ignores.setText(db.prefs.get('check_library_ignore_extensions', ''))
  58.         self.ext_ignores.setToolTip(_('Enter comma-separated extensions without a leading dot. Used only in book folders'))
  59.         le.setBuddy(self.ext_ignores)
  60.         h.addWidget(self.ext_ignores)
  61.         self._layout.addLayout(h)
  62.         self._layout.addWidget(self.bbox)
  63.         self.resize(750, 500)
  64.         self.bbox.setEnabled(True)
  65.         self.run_the_check()
  66.  
  67.     
  68.     def accept(self):
  69.         self.db.prefs['check_library_ignore_extensions'] = unicode(self.ext_ignores.text())
  70.         self.db.prefs['check_library_ignore_names'] = unicode(self.name_ignores.text())
  71.         QDialog.accept(self)
  72.  
  73.     
  74.     def box_to_list(self, txt):
  75.         return _[1]
  76.  
  77.     
  78.     def run_the_check(self):
  79.         checker = CheckLibrary(self.db.library_path, self.db)
  80.         checker.scan_library(self.box_to_list(unicode(self.name_ignores.text())), self.box_to_list(unicode(self.ext_ignores.text())))
  81.         plaintext = []
  82.         
  83.         def builder(tree, checker, check):
  84.             (attr, h, checkable) = check
  85.             list = getattr(checker, attr, None)
  86.             if list is None:
  87.                 return None
  88.             tl = Item([
  89.                 h])
  90.             for problem in list:
  91.                 it = Item()
  92.                 if checkable:
  93.                     it.setFlags(Qt.ItemIsEnabled | Qt.ItemIsUserCheckable)
  94.                     it.setCheckState(1, False)
  95.                 else:
  96.                     it.setFlags(Qt.ItemIsEnabled)
  97.                 it.setText(0, problem[0])
  98.                 it.setText(1, problem[1])
  99.                 tl.addChild(it)
  100.                 self.all_items.append(it)
  101.                 plaintext.append(','.join([
  102.                     h,
  103.                     problem[0],
  104.                     problem[1]]))
  105.             
  106.             tree.addTopLevelItem(tl)
  107.  
  108.         t = self.log
  109.         t.clear()
  110.         t.setColumnCount(2)
  111.         t.setHeaderLabels([
  112.             _('Name'),
  113.             _('Path from library')])
  114.         self.all_items = []
  115.         for check in CHECKS:
  116.             builder(t, checker, check)
  117.         
  118.         t.setColumnWidth(0, 200)
  119.         t.setColumnWidth(1, 400)
  120.         self.delete.setEnabled(False)
  121.         self.text_results = '\n'.join(plaintext)
  122.  
  123.     
  124.     def item_changed(self, item, column):
  125.         for it in self.all_items:
  126.             if it.checkState(1):
  127.                 self.delete.setEnabled(True)
  128.                 return None
  129.         
  130.  
  131.     
  132.     def delete_marked(self):
  133.         if not confirm('<p>' + _('The marked files and folders will be <b>permanently deleted</b>. Are you sure?') + '</p>', 'check_library_editor_delete', self):
  134.             return None
  135.         items = sorted(self.all_items, key = (lambda x: len(x.text(1))), reverse = True)
  136.         for it in items:
  137.             if it.checkState(1):
  138.                 
  139.                 try:
  140.                     p = os.path.join(self.db.library_path, unicode(it.text(1)))
  141.                     if os.path.isdir(p):
  142.                         delete_tree(p)
  143.                     else:
  144.                         delete_file(p)
  145.                 prints('failed to delete', os.path.join(self.db.library_path, unicode(it.text(1))))
  146.  
  147.                 continue
  148.         
  149.         self.run_the_check()
  150.  
  151.     
  152.     def copy_to_clipboard(self):
  153.         QApplication.clipboard().setText(self.text_results)
  154.  
  155.  
  156. if __name__ == '__main__':
  157.     app = QApplication([])
  158.     d = CheckLibraryDialog()
  159.     d.exec_()
  160.  
  161.