home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1258 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  9.6 KB  |  244 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
  6. import time
  7. from threading import Thread
  8. from PyQt4.QtCore import Qt, QObject, SIGNAL, QVariant, pyqtSignal, QAbstractTableModel, QCoreApplication, QTimer
  9. from PyQt4.QtGui import QDialog, QItemSelectionModel
  10. from calibre.gui2.dialogs.fetch_metadata_ui import Ui_FetchMetadata
  11. from calibre.gui2 import error_dialog, NONE, info_dialog, config
  12. from calibre.gui2.widgets import ProgressIndicator
  13. from calibre import strftime
  14. from calibre.customize.ui import get_isbndb_key, set_isbndb_key
  15. _hung_fetchers = set([])
  16.  
  17. class Fetcher(Thread):
  18.     
  19.     def __init__(self, title, author, publisher, isbn, key):
  20.         Thread.__init__(self)
  21.         self.daemon = True
  22.         self.title = title
  23.         self.author = author
  24.         self.publisher = publisher
  25.         self.isbn = isbn
  26.         self.key = key
  27.         self.results = []
  28.         self.exceptions = []
  29.  
  30.     
  31.     def run(self):
  32.         search = search
  33.         import calibre.ebooks.metadata.fetch
  34.         (self.results, self.exceptions) = None(search, self.title, self.author, self.publisher, self.isbn if self.key else None)
  35.  
  36.  
  37.  
  38. class Matches(QAbstractTableModel):
  39.     
  40.     def __init__(self, matches):
  41.         self.matches = matches
  42.         QAbstractTableModel.__init__(self)
  43.  
  44.     
  45.     def rowCount(self, *args):
  46.         return len(self.matches)
  47.  
  48.     
  49.     def columnCount(self, *args):
  50.         return 6
  51.  
  52.     
  53.     def headerData(self, section, orientation, role):
  54.         if role != Qt.DisplayRole:
  55.             return NONE
  56.         text = ''
  57.         if orientation == Qt.Horizontal:
  58.             if section == 0:
  59.                 text = _('Title')
  60.             elif section == 1:
  61.                 text = _('Author(s)')
  62.             elif section == 2:
  63.                 text = _('Author Sort')
  64.             elif section == 3:
  65.                 text = _('Publisher')
  66.             elif section == 4:
  67.                 text = _('ISBN')
  68.             elif section == 5:
  69.                 text = _('Published')
  70.             
  71.             return QVariant(text)
  72.         return QVariant(section + 1)
  73.  
  74.     
  75.     def summary(self, row):
  76.         return self.matches[row].comments
  77.  
  78.     
  79.     def data(self, index, role):
  80.         row = index.row()
  81.         col = index.column()
  82.         if role == Qt.DisplayRole:
  83.             book = self.matches[row]
  84.             res = None
  85.             if col == 0:
  86.                 res = book.title
  87.             elif col == 1:
  88.                 res = ', '.join(book.authors)
  89.             elif col == 2:
  90.                 res = book.author_sort
  91.             elif col == 3:
  92.                 res = book.publisher
  93.             elif col == 4:
  94.                 res = book.isbn
  95.             elif col == 5:
  96.                 if hasattr(book.pubdate, 'timetuple'):
  97.                     res = strftime('%b %Y', book.pubdate.timetuple())
  98.                 
  99.             
  100.             if not res:
  101.                 return NONE
  102.             return QVariant(res)
  103.         return NONE
  104.  
  105.  
  106.  
  107. class FetchMetadata(QDialog, Ui_FetchMetadata):
  108.     HANG_TIME = 75
  109.     queue_reject = pyqtSignal()
  110.     
  111.     def __init__(self, parent, isbn, title, author, publisher, timeout):
  112.         QDialog.__init__(self, parent)
  113.         Ui_FetchMetadata.__init__(self)
  114.         self.setupUi(self)
  115.         for fetcher in list(_hung_fetchers):
  116.             if not fetcher.is_alive():
  117.                 _hung_fetchers.remove(fetcher)
  118.                 continue
  119.         
  120.         self.pi = ProgressIndicator(self)
  121.         self.timeout = timeout
  122.         QObject.connect(self.fetch, SIGNAL('clicked()'), self.fetch_metadata)
  123.         self.queue_reject.connect(self.reject, Qt.QueuedConnection)
  124.         isbndb_key = get_isbndb_key()
  125.         if not isbndb_key:
  126.             isbndb_key = ''
  127.         
  128.         self.key.setText(isbndb_key)
  129.         None(self.setWindowTitle if title else _('Unknown'))
  130.         self.isbn = isbn
  131.         self.title = title
  132.         self.author = author.strip()
  133.         self.publisher = publisher
  134.         self.previous_row = None
  135.         self.warning.setVisible(False)
  136.         self.connect(self.matches, SIGNAL('activated(QModelIndex)'), self.chosen)
  137.         self.connect(self.matches, SIGNAL('entered(QModelIndex)'), self.show_summary)
  138.         self.matches.setMouseTracking(True)
  139.         self.fetch_metadata()
  140.         self.opt_get_social_metadata.setChecked(config['get_social_metadata'])
  141.         self.opt_overwrite_author_title_metadata.setChecked(config['overwrite_author_title_metadata'])
  142.  
  143.     
  144.     def show_summary(self, current, *args):
  145.         row = current.row()
  146.         if row != self.previous_row:
  147.             summ = self.model.summary(row)
  148.             None(self.summary.setText if summ else '')
  149.             self.previous_row = row
  150.         
  151.  
  152.     
  153.     def fetch_metadata(self):
  154.         self.warning.setVisible(False)
  155.         key = str(self.key.text())
  156.         if key:
  157.             set_isbndb_key(key)
  158.         else:
  159.             key = None
  160.         title = None
  161.         author = None
  162.         publisher = None
  163.         isbn = None
  164.         if self.isbn:
  165.             isbn = self.isbn
  166.         
  167.         if self.title:
  168.             title = self.title
  169.         
  170.         if self.author and not (self.author == _('Unknown')):
  171.             author = self.author
  172.         
  173.         self.fetch.setEnabled(False)
  174.         self.setCursor(Qt.WaitCursor)
  175.         QCoreApplication.instance().processEvents()
  176.         self.fetcher = Fetcher(title, author, publisher, isbn, key)
  177.         self.fetcher.start()
  178.         self.pi.start(_('Finding metadata...'))
  179.         self._hangcheck = QTimer(self)
  180.         self.connect(self._hangcheck, SIGNAL('timeout()'), self.hangcheck, Qt.QueuedConnection)
  181.         self.start_time = time.time()
  182.         self._hangcheck.start(100)
  183.  
  184.     
  185.     def hangcheck(self):
  186.         if self.fetcher.is_alive() and time.time() - self.start_time < self.HANG_TIME:
  187.             return None
  188.         self._hangcheck.stop()
  189.         
  190.         try:
  191.             if self.fetcher.is_alive():
  192.                 error_dialog(self, _('Could not find metadata'), _('The metadata download seems to have stalled. Try again later.')).exec_()
  193.                 self.terminate()
  194.                 return self.queue_reject.emit()
  195.             self.model = Matches(self.fetcher.results)
  196.             warnings = _[1]
  197.             if self.model.rowCount() < 1:
  198.                 info_dialog(self, _('No metadata found'), _('No metadata found, try adjusting the title and author or the ISBN key.')).exec_()
  199.                 return None
  200.             self.matches.setModel(self.model)
  201.             QObject.connect(self.matches.selectionModel(), SIGNAL('currentRowChanged(QModelIndex, QModelIndex)'), self.show_summary)
  202.             self.model.reset()
  203.             self.matches.selectionModel().select(self.model.index(0, 0), QItemSelectionModel.Select | QItemSelectionModel.Rows)
  204.             self.matches.setCurrentIndex(self.model.index(0, 0))
  205.         finally:
  206.             self.fetch.setEnabled(True)
  207.             self.unsetCursor()
  208.             self.matches.resizeColumnsToContents()
  209.             self.pi.stop()
  210.  
  211.  
  212.     
  213.     def terminate(self):
  214.         if hasattr(self, 'fetcher') and self.fetcher.is_alive():
  215.             _hung_fetchers.add(self.fetcher)
  216.         
  217.         if hasattr(self, '_hangcheck') and self._hangcheck.isActive():
  218.             self._hangcheck.stop()
  219.         
  220.  
  221.     
  222.     def __enter__(self, *args):
  223.         return self
  224.  
  225.     
  226.     def __exit__(self, *args):
  227.         self.terminate()
  228.  
  229.     
  230.     def selected_book(self):
  231.         
  232.         try:
  233.             return self.matches.model().matches[self.matches.currentIndex().row()]
  234.         except:
  235.             return None
  236.  
  237.  
  238.     
  239.     def chosen(self, index):
  240.         self.matches.setCurrentIndex(index)
  241.         self.accept()
  242.  
  243.  
  244.