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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
  6. __docformat__ = 'restructuredtext en'
  7. import os
  8. from functools import partial
  9. from PyQt4.Qt import QModelIndex, QMenu
  10. from calibre.gui2 import error_dialog, Dispatcher
  11. from calibre.gui2.tools import convert_single_ebook, convert_bulk_ebook
  12. from calibre.utils.config import prefs
  13. from calibre.gui2.actions import InterfaceAction
  14. from calibre.customize.ui import plugin_for_input_format
  15.  
  16. class ConvertAction(InterfaceAction):
  17.     name = 'Convert Books'
  18.     action_spec = (_('Convert books'), 'convert.png', None, _('C'))
  19.     dont_add_to = frozenset([
  20.         'toolbar-device',
  21.         'context-menu-device'])
  22.     action_type = 'current'
  23.     
  24.     def genesis(self):
  25.         cm = QMenu()
  26.         cm.addAction(_('Convert individually'), partial(self.convert_ebook, False, bulk = False))
  27.         cm.addAction(_('Bulk convert'), partial(self.convert_ebook, False, bulk = True))
  28.         cm.addSeparator()
  29.         ac = cm.addAction(_('Create catalog of books in your calibre library'))
  30.         ac.triggered.connect(self.gui.iactions['Generate Catalog'].generate_catalog)
  31.         self.qaction.setMenu(cm)
  32.         self.qaction.triggered.connect(self.convert_ebook)
  33.         self.convert_menu = cm
  34.         self.conversion_jobs = { }
  35.  
  36.     
  37.     def location_selected(self, loc):
  38.         enabled = loc == 'library'
  39.         self.qaction.setEnabled(enabled)
  40.  
  41.     
  42.     def auto_convert(self, book_ids, on_card, format):
  43.         previous = self.gui.library_view.currentIndex()
  44.         rows = [ x.row() for x in self.gui.library_view.selectionModel().selectedRows() ]
  45.         (jobs, changed, bad) = convert_single_ebook(self.gui, self.gui.library_view.model().db, book_ids, True, format)
  46.         if jobs == []:
  47.             return None
  48.         self.queue_convert_jobs(jobs, changed, bad, rows, previous, self.book_auto_converted, extra_job_args = [
  49.             on_card])
  50.  
  51.     
  52.     def auto_convert_mail(self, to, fmts, delete_from_library, book_ids, format):
  53.         previous = self.gui.library_view.currentIndex()
  54.         rows = [ x.row() for x in self.gui.library_view.selectionModel().selectedRows() ]
  55.         (jobs, changed, bad) = convert_single_ebook(self.gui, self.gui.library_view.model().db, book_ids, True, format)
  56.         if jobs == []:
  57.             return None
  58.         self.queue_convert_jobs(jobs, changed, bad, rows, previous, self.book_auto_converted_mail, extra_job_args = [
  59.             delete_from_library,
  60.             to,
  61.             fmts])
  62.  
  63.     
  64.     def auto_convert_news(self, book_ids, format):
  65.         previous = self.gui.library_view.currentIndex()
  66.         rows = [ x.row() for x in self.gui.library_view.selectionModel().selectedRows() ]
  67.         (jobs, changed, bad) = convert_single_ebook(self.gui, self.gui.library_view.model().db, book_ids, True, format)
  68.         if jobs == []:
  69.             return None
  70.         self.queue_convert_jobs(jobs, changed, bad, rows, previous, self.book_auto_converted_news)
  71.  
  72.     
  73.     def auto_convert_catalogs(self, book_ids, format):
  74.         previous = self.gui.library_view.currentIndex()
  75.         rows = [ x.row() for x in self.gui.library_view.selectionModel().selectedRows() ]
  76.         (jobs, changed, bad) = convert_single_ebook(self.gui, self.gui.library_view.model().db, book_ids, True, format)
  77.         if jobs == []:
  78.             return None
  79.         self.queue_convert_jobs(jobs, changed, bad, rows, previous, self.book_auto_converted_catalogs)
  80.  
  81.     
  82.     def get_books_for_conversion(self):
  83.         rows = [ r.row() for r in self.gui.library_view.selectionModel().selectedRows() ]
  84.         if not rows or len(rows) == 0:
  85.             d = error_dialog(self.gui, _('Cannot convert'), _('No books selected'))
  86.             d.exec_()
  87.             return None
  88.         return [ self.gui.library_view.model().db.id(r) for r in rows ]
  89.  
  90.     
  91.     def convert_ebook(self, checked, bulk = None):
  92.         book_ids = self.get_books_for_conversion()
  93.         if book_ids is None:
  94.             return None
  95.         previous = self.gui.library_view.currentIndex()
  96.         rows = [ x.row() for x in self.gui.library_view.selectionModel().selectedRows() ]
  97.         num = 0
  98.  
  99.     
  100.     def queue_convert_jobs(self, jobs, changed, bad, rows, previous, converted_func, extra_job_args = []):
  101.         for func, args, desc, fmt, id, temp_files in jobs:
  102.             input_file = args[0]
  103.             input_fmt = os.path.splitext(input_file)[1]
  104.             core_usage = 1
  105.             if input_fmt:
  106.                 input_fmt = input_fmt[1:]
  107.                 plugin = plugin_for_input_format(input_fmt)
  108.                 if plugin is not None:
  109.                     core_usage = plugin.core_usage
  110.                 
  111.             
  112.             if id not in bad:
  113.                 job = self.gui.job_manager.run_job(Dispatcher(converted_func), func, args = args, description = desc, core_usage = core_usage)
  114.                 args = [
  115.                     temp_files,
  116.                     fmt,
  117.                     id] + extra_job_args
  118.                 self.conversion_jobs[job] = tuple(args)
  119.                 continue
  120.         
  121.         if changed:
  122.             self.gui.library_view.model().refresh_rows(rows)
  123.             current = self.gui.library_view.currentIndex()
  124.             self.gui.library_view.model().current_changed(current, previous)
  125.         
  126.  
  127.     
  128.     def book_auto_converted(self, job):
  129.         (temp_files, fmt, book_id, on_card) = self.conversion_jobs[job]
  130.         self.book_converted(job)
  131.         self.gui.sync_to_device(on_card, False, specific_format = fmt, send_ids = [
  132.             book_id], do_auto_convert = False)
  133.  
  134.     
  135.     def book_auto_converted_mail(self, job):
  136.         (temp_files, fmt, book_id, delete_from_library, to, fmts) = self.conversion_jobs[job]
  137.         self.book_converted(job)
  138.         self.gui.send_by_mail(to, fmts, delete_from_library, specific_format = fmt, send_ids = [
  139.             book_id], do_auto_convert = False)
  140.  
  141.     
  142.     def book_auto_converted_news(self, job):
  143.         (temp_files, fmt, book_id) = self.conversion_jobs[job]
  144.         self.book_converted(job)
  145.         self.gui.sync_news(send_ids = [
  146.             book_id], do_auto_convert = False)
  147.  
  148.     
  149.     def book_auto_converted_catalogs(self, job):
  150.         (temp_files, fmt, book_id) = self.conversion_jobs[job]
  151.         self.book_converted(job)
  152.         self.gui.sync_catalogs(send_ids = [
  153.             book_id], do_auto_convert = False)
  154.  
  155.     
  156.     def book_converted(self, job):
  157.         (temp_files, fmt, book_id) = self.conversion_jobs.pop(job)[:3]
  158.         
  159.         try:
  160.             if job.failed:
  161.                 self.gui.job_exception(job)
  162.                 return None
  163.             data = open(temp_files[-1].name, 'rb')
  164.             self.gui.library_view.model().db.add_format(book_id, fmt, data, index_is_id = True)
  165.             data.close()
  166.             self.gui.status_bar.show_message(job.description + ' completed', 2000)
  167.         finally:
  168.             for f in temp_files:
  169.                 
  170.                 try:
  171.                     if os.path.exists(f.name):
  172.                         os.remove(f.name)
  173.                 continue
  174.                 continue
  175.  
  176.             
  177.  
  178.         self.gui.tags_view.recount()
  179.         if self.gui.current_view() is self.gui.library_view:
  180.             current = self.gui.library_view.currentIndex()
  181.             if current.isValid():
  182.                 self.gui.library_view.model().current_changed(current, QModelIndex())
  183.             
  184.         
  185.  
  186.  
  187.