home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1155 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  11.9 KB  |  267 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 QInputDialog, QPixmap, QMenu
  10. from calibre.gui2 import error_dialog, choose_files, choose_dir, warning_dialog, info_dialog
  11. from calibre.gui2.widgets import IMAGE_EXTENSIONS
  12. from calibre.ebooks import BOOK_EXTENSIONS
  13. from calibre.utils.filenames import ascii_filename
  14. from calibre.constants import preferred_encoding, filesystem_encoding
  15. from calibre.gui2.actions import InterfaceAction
  16.  
  17. class AddAction(InterfaceAction):
  18.     name = 'Add Books'
  19.     action_spec = (_('Add books'), 'add_book.png', _('Add books to the calibre library/device from files on your computer'), _('A'))
  20.     action_type = 'current'
  21.     
  22.     def genesis(self):
  23.         self._add_filesystem_book = self.Dispatcher(self._AddAction__add_filesystem_book)
  24.         self.add_menu = QMenu()
  25.         self.add_menu.addAction(_('Add books from a single directory'), self.add_books)
  26.         self.add_menu.addAction(_('Add books from directories, including sub-directories (One book per directory, assumes every ebook file is the same book in a different format)'), self.add_recursive_single)
  27.         self.add_menu.addAction(_('Add books from directories, including sub directories (Multiple books per directory, assumes every ebook file is a different book)'), self.add_recursive_multiple)
  28.         self.add_menu.addSeparator()
  29.         self.add_menu.addAction(_('Add Empty book. (Book entry with no formats)'), self.add_empty)
  30.         self.add_menu.addAction(_('Add from ISBN'), self.add_from_isbn)
  31.         self.qaction.setMenu(self.add_menu)
  32.         self.qaction.triggered.connect(self.add_books)
  33.  
  34.     
  35.     def location_selected(self, loc):
  36.         enabled = loc == 'library'
  37.         for action in list(self.add_menu.actions())[1:]:
  38.             action.setEnabled(enabled)
  39.         
  40.  
  41.     
  42.     def add_recursive(self, single):
  43.         root = choose_dir(self.gui, 'recursive book import root dir dialog', 'Select root folder')
  44.         if not root:
  45.             return None
  46.         Adder = Adder
  47.         import calibre.gui2.add
  48.         self._adder = Adder(self.gui, self.gui.library_view.model().db, self.Dispatcher(self._files_added), spare_server = self.gui.spare_server)
  49.         self._adder.add_recursive(root, single)
  50.  
  51.     
  52.     def add_recursive_single(self, *args):
  53.         self.add_recursive(True)
  54.  
  55.     
  56.     def add_recursive_multiple(self, *args):
  57.         self.add_recursive(False)
  58.  
  59.     
  60.     def add_empty(self, *args):
  61.         (num, ok) = QInputDialog.getInt(self.gui, _('How many empty books?'), _('How many empty books should be added?'), 1, 1, 100)
  62.         if ok:
  63.             MetaInformation = MetaInformation
  64.             import calibre.ebooks.metadata
  65.             for x in xrange(num):
  66.                 self.gui.library_view.model().db.import_book(MetaInformation(None), [])
  67.             
  68.             self.gui.library_view.model().books_added(num)
  69.         
  70.  
  71.     
  72.     def add_isbns(self, isbns):
  73.         MetaInformation = MetaInformation
  74.         import calibre.ebooks.metadata
  75.         ids = set([])
  76.         for x in isbns:
  77.             mi = MetaInformation(None)
  78.             mi.isbn = x
  79.             ids.add(self.gui.library_view.model().db.import_book(mi, []))
  80.         
  81.         self.gui.library_view.model().books_added(len(isbns))
  82.         self.gui.iactions['Edit Metadata'].do_download_metadata(ids)
  83.  
  84.     
  85.     def files_dropped(self, paths):
  86.         to_device = self.gui.stack.currentIndex() != 0
  87.         self._add_books(paths, to_device)
  88.  
  89.     
  90.     def files_dropped_on_book(self, event, paths):
  91.         accept = False
  92.         if self.gui.current_view() is not self.gui.library_view:
  93.             return None
  94.         db = self.gui.library_view.model().db
  95.         current_idx = self.gui.library_view.currentIndex()
  96.         if not current_idx.isValid():
  97.             return None
  98.         cid = db.id(current_idx.row())
  99.         for path in paths:
  100.             ext = os.path.splitext(path)[1].lower()
  101.             if ext in IMAGE_EXTENSIONS:
  102.                 pmap = QPixmap()
  103.                 pmap.load(path)
  104.                 if not pmap.isNull():
  105.                     accept = True
  106.                     db.set_cover(cid, pmap)
  107.                 
  108.             pmap.isNull()
  109.             if ext in BOOK_EXTENSIONS:
  110.                 db.add_format_with_hooks(cid, ext, path, index_is_id = True)
  111.                 accept = True
  112.                 continue
  113.             None if ext else self.gui.current_view() is not self.gui.library_view
  114.         
  115.         if accept:
  116.             event.accept()
  117.             self.gui.library_view.model().current_changed(current_idx, current_idx)
  118.         
  119.  
  120.     
  121.     def __add_filesystem_book(self, paths, allow_device = True):
  122.         if isinstance(paths, basestring):
  123.             paths = [
  124.                 paths]
  125.         
  126.         books = _[1]
  127.  
  128.     
  129.     def add_filesystem_book(self, paths, allow_device = True):
  130.         self._add_filesystem_book(paths, allow_device = allow_device)
  131.  
  132.     
  133.     def add_from_isbn(self, *args):
  134.         AddFromISBN = AddFromISBN
  135.         import calibre.gui2.dialogs.add_from_isbn
  136.         d = AddFromISBN(self.gui)
  137.         if d.exec_() == d.Accepted:
  138.             self.add_isbns(d.isbns)
  139.         
  140.  
  141.     
  142.     def add_books(self, *args):
  143.         filters = [
  144.             (_('Books'), BOOK_EXTENSIONS),
  145.             (_('EPUB Books'), [
  146.                 'epub']),
  147.             (_('LRF Books'), [
  148.                 'lrf']),
  149.             (_('HTML Books'), [
  150.                 'htm',
  151.                 'html',
  152.                 'xhtm',
  153.                 'xhtml']),
  154.             (_('LIT Books'), [
  155.                 'lit']),
  156.             (_('MOBI Books'), [
  157.                 'mobi',
  158.                 'prc',
  159.                 'azw']),
  160.             (_('Topaz books'), [
  161.                 'tpz',
  162.                 'azw1']),
  163.             (_('Text books'), [
  164.                 'txt',
  165.                 'rtf']),
  166.             (_('PDF Books'), [
  167.                 'pdf']),
  168.             (_('SNB Books'), [
  169.                 'snb']),
  170.             (_('Comics'), [
  171.                 'cbz',
  172.                 'cbr',
  173.                 'cbc']),
  174.             (_('Archives'), [
  175.                 'zip',
  176.                 'rar'])]
  177.         to_device = self.gui.stack.currentIndex() != 0
  178.         if to_device:
  179.             filters = [
  180.                 (_('Supported books'), self.gui.device_manager.device.FORMATS)]
  181.         
  182.         books = choose_files(self.gui, 'add books dialog dir', 'Select books', filters = filters)
  183.         if not books:
  184.             return None
  185.         self._add_books(books, to_device)
  186.  
  187.     
  188.     def _add_books(self, paths, to_device, on_card = None):
  189.         if on_card is None:
  190.             if self.gui.stack.currentIndex() == 2:
  191.                 pass
  192.             elif self.gui.stack.currentIndex() == 3:
  193.                 pass
  194.             
  195.             on_card = None
  196.         
  197.         if not paths:
  198.             return None
  199.         Adder = Adder
  200.         import calibre.gui2.add
  201.         self._AddAction__adder_func = partial(self._files_added, on_card = on_card)
  202.         self._adder = paths(Adder, self.gui if to_device else self.gui.library_view.model().db, self.Dispatcher(self._AddAction__adder_func), spare_server = self.gui.spare_server)
  203.         self._adder.add(paths)
  204.  
  205.     
  206.     def _files_added(self, paths = [], names = [], infos = [], on_card = None):
  207.         if paths:
  208.             self.gui.upload_books(paths, list(map(ascii_filename, names)), infos, on_card = on_card)
  209.             self.gui.status_bar.show_message(_('Uploading books to device.'), 2000)
  210.         
  211.         if getattr(self._adder, 'number_of_books_added', 0) > 0:
  212.             self.gui.library_view.model().books_added(self._adder.number_of_books_added)
  213.             if hasattr(self.gui, 'db_images'):
  214.                 self.gui.db_images.reset()
  215.             
  216.         
  217.         if getattr(self._adder, 'merged_books', False):
  218.             books = []([ _[1] if isinstance(x, unicode) else x.decode(preferred_encoding, 'replace') for x in self._adder.merged_books ])
  219.             info_dialog(self.gui, _('Merged some books'), _('Some duplicates were found and merged into the following existing books:'), det_msg = books, show = True)
  220.         
  221.         if getattr(self._adder, 'critical', None):
  222.             det_msg = []
  223.             for name, log in self._adder.critical.items():
  224.                 if isinstance(name, str):
  225.                     name = name.decode(filesystem_encoding, 'replace')
  226.                 
  227.                 det_msg.append(name + '\n' + log)
  228.             
  229.             warning_dialog(self.gui, _('Failed to read metadata'), _('Failed to read metadata from the following') + ':', det_msg = '\n\n'.join(det_msg), show = True)
  230.         
  231.         if hasattr(self._adder, 'cleanup'):
  232.             self._adder.cleanup()
  233.         
  234.         self._adder = None
  235.  
  236.     
  237.     def _add_from_device_adder(self, paths = [], names = [], infos = [], on_card = None, model = None):
  238.         self._files_added(paths, names, infos, on_card = on_card)
  239.         self.gui.set_books_in_library(booklists = [
  240.             model.db], reset = True)
  241.         self.gui.refresh_ondevice()
  242.  
  243.     
  244.     def add_books_from_device(self, view, paths = None):
  245.         backloading_err = self.gui.device_manager.device.BACKLOADING_ERROR_MESSAGE
  246.         if backloading_err is not None:
  247.             return error_dialog(self.gui, _('Add to library'), backloading_err, show = True)
  248.         ve = self.gui.device_manager.device.VIRTUAL_BOOK_EXTENSIONS
  249.         
  250.         def ext(x):
  251.             ans = os.path.splitext(x)[1]
  252.             ans = None if len(ans) > 1 else ans
  253.             return ans.lower()
  254.  
  255.         remove = [](_[2])
  256.         if not paths or len(paths) == 0:
  257.             d = error_dialog(self.gui, _('Add to library'), _('No book files found'))
  258.             d.exec_()
  259.             return None
  260.         Adder = Adder
  261.         import calibre.gui2.add
  262.         self._AddAction__adder_func = partial(self._add_from_device_adder, on_card = None, model = view.model())
  263.         self._adder = Adder(self.gui, self.gui.library_view.model().db, self.Dispatcher(self._AddAction__adder_func), spare_server = self.gui.spare_server)
  264.         self._adder.add(paths)
  265.  
  266.  
  267.