home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1175 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  7.8 KB  |  186 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. import time
  9. from functools import partial
  10. from PyQt4.Qt import Qt, QMenu
  11. from calibre.constants import isosx
  12. from calibre.gui2 import error_dialog, Dispatcher, question_dialog, config, open_local_file
  13. from calibre.gui2.dialogs.choose_format import ChooseFormatDialog
  14. from calibre.utils.config import prefs
  15. from calibre.ptempfile import PersistentTemporaryFile
  16. from calibre.gui2.actions import InterfaceAction
  17.  
  18. class ViewAction(InterfaceAction):
  19.     name = 'View'
  20.     action_spec = (_('View'), 'view.png', None, _('V'))
  21.     action_type = 'current'
  22.     
  23.     def genesis(self):
  24.         self.persistent_files = []
  25.         self.metadata_view_id = None
  26.         self.qaction.triggered.connect(self.view_book)
  27.         self.view_menu = QMenu()
  28.         self.view_menu.addAction(_('View'), partial(self.view_book, False))
  29.         ac = self.view_menu.addAction(_('View specific format'))
  30.         None(ac.setShortcut if isosx else Qt.AltModifier + Qt.Key_V)
  31.         self.qaction.setMenu(self.view_menu)
  32.         ac.triggered.connect(self.view_specific_format, type = Qt.QueuedConnection)
  33.  
  34.     
  35.     def location_selected(self, loc):
  36.         enabled = loc == 'library'
  37.         for action in list(self.view_menu.actions())[1:]:
  38.             action.setEnabled(enabled)
  39.         
  40.  
  41.     
  42.     def view_format(self, row, format):
  43.         fmt_path = self.gui.library_view.model().db.format_abspath(row, format)
  44.         if fmt_path:
  45.             self._view_file(fmt_path)
  46.         
  47.  
  48.     
  49.     def view_format_by_id(self, id_, format):
  50.         fmt_path = self.gui.library_view.model().db.format_abspath(id_, format, index_is_id = True)
  51.         if fmt_path:
  52.             self._view_file(fmt_path)
  53.         
  54.  
  55.     
  56.     def metadata_view_format(self, fmt):
  57.         fmt_path = self.gui.library_view.model().db.format_abspath(self.metadata_view_id, fmt, index_is_id = True)
  58.         if fmt_path:
  59.             self._view_file(fmt_path)
  60.         
  61.  
  62.     
  63.     def book_downloaded_for_viewing(self, job):
  64.         if job.failed:
  65.             self.gui.device_job_exception(job)
  66.             return None
  67.         self._view_file(job.result)
  68.  
  69.     
  70.     def _launch_viewer(self, name = None, viewer = 'ebook-viewer', internal = True):
  71.         self.gui.setCursor(Qt.BusyCursor)
  72.         
  73.         try:
  74.             if internal:
  75.                 args = [
  76.                     viewer]
  77.                 if isosx and 'ebook' in viewer:
  78.                     args.append('--raise-window')
  79.                 
  80.                 if name is not None:
  81.                     args.append(name)
  82.                 
  83.                 self.gui.job_manager.launch_gui_app(viewer, kwargs = dict(args = args))
  84.             else:
  85.                 open_local_file(name)
  86.                 time.sleep(2)
  87.         finally:
  88.             self.gui.unsetCursor()
  89.  
  90.  
  91.     
  92.     def _view_file(self, name):
  93.         ext = os.path.splitext(name)[1].upper().replace('.', '')
  94.         viewer = None if ext == 'LRF' else 'ebook-viewer'
  95.         internal = ext in config['internally_viewed_formats']
  96.         self._launch_viewer(name, viewer, internal)
  97.  
  98.     
  99.     def view_specific_format(self, triggered):
  100.         rows = self.gui.library_view.selectionModel().selectedRows()
  101.         if not rows or len(rows) == 0:
  102.             d = error_dialog(self.gui, _('Cannot view'), _('No book selected'))
  103.             d.exec_()
  104.             return None
  105.         row = rows[0].row()
  106.         formats = self.gui.library_view.model().db.formats(row).upper().split(',')
  107.         d = ChooseFormatDialog(self.gui, _('Choose the format to view'), formats)
  108.         if d.exec_() == d.Accepted:
  109.             format = d.format()
  110.             self.view_format(row, format)
  111.         
  112.  
  113.     
  114.     def _view_check(self, num, max_ = 3):
  115.         if num <= max_:
  116.             return True
  117.         return question_dialog(self.gui, _('Multiple Books Selected'), _('You are attempting to open %d books. Opening too many books at once can be slow and have a negative effect on the responsiveness of your computer. Once started the process cannot be stopped until complete. Do you wish to continue?') % num, show_copy_button = False)
  118.  
  119.     
  120.     def view_folder(self, *args):
  121.         rows = self.gui.current_view().selectionModel().selectedRows()
  122.         if not rows or len(rows) == 0:
  123.             d = error_dialog(self.gui, _('Cannot open folder'), _('No book selected'))
  124.             d.exec_()
  125.             return None
  126.         if not self._view_check(len(rows)):
  127.             return None
  128.         for row in rows:
  129.             path = self.gui.library_view.model().db.abspath(row.row())
  130.             open_local_file(path)
  131.         
  132.  
  133.     
  134.     def view_folder_for_id(self, id_):
  135.         path = self.gui.library_view.model().db.abspath(id_, index_is_id = True)
  136.         open_local_file(path)
  137.  
  138.     
  139.     def view_book(self, triggered):
  140.         rows = self.gui.current_view().selectionModel().selectedRows()
  141.         self._view_books(rows)
  142.  
  143.     
  144.     def view_specific_book(self, index):
  145.         self._view_books([
  146.             index])
  147.  
  148.     
  149.     def _view_books(self, rows):
  150.         if not rows or len(rows) == 0:
  151.             self._launch_viewer()
  152.             return None
  153.         if not self._view_check(len(rows)):
  154.             return None
  155.         if self.gui.current_view() is self.gui.library_view:
  156.             for row in rows:
  157.                 formats = self.gui.library_view.model().db.formats(row)
  158.                 title = self.gui.library_view.model().db.title(row)
  159.                 if not formats:
  160.                     error_dialog(self.gui, _('Cannot view'), _('%s has no available formats.') % (title,), show = True)
  161.                     continue
  162.                 
  163.                 formats = formats.upper().split(',')
  164.                 in_prefs = False
  165.                 for format in prefs['input_format_order']:
  166.                     if format in formats:
  167.                         in_prefs = True
  168.                         self.view_format(row, format)
  169.                         break
  170.                         continue
  171.                 
  172.                 if not in_prefs:
  173.                     self.view_format(row, formats[0])
  174.                     continue
  175.             
  176.         else:
  177.             paths = self.gui.current_view().model().paths(rows)
  178.             for path in paths:
  179.                 pt = PersistentTemporaryFile('_viewer_' + os.path.splitext(path)[1])
  180.                 self.persistent_files.append(pt)
  181.                 pt.close()
  182.                 self.gui.device_manager.view_book(Dispatcher(self.book_downloaded_for_viewing), path, pt.name)
  183.             
  184.  
  185.  
  186.