home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1361 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  6.4 KB  |  136 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 time
  8. from PyQt4.Qt import Qt, QUrl, QDialog, QSize, QVBoxLayout, QLabel, QPlainTextEdit, QDialogButtonBox
  9. from calibre.gui2.preferences import ConfigWidgetBase, test_widget
  10. from calibre.gui2.preferences.server_ui import Ui_Form
  11. from calibre.utils.search_query_parser import saved_searches
  12. from calibre.library.server import server_config
  13. from calibre.utils.config import ConfigProxy
  14. from calibre.gui2 import error_dialog, config, open_url, warning_dialog, Dispatcher
  15.  
  16. class ConfigWidget(ConfigWidgetBase, Ui_Form):
  17.     
  18.     def genesis(self, gui):
  19.         self.gui = gui
  20.         self.proxy = ConfigProxy(server_config())
  21.         db = self.db = gui.library_view.model().db
  22.         self.server = self.gui.content_server
  23.         r = self.register
  24.         r('port', self.proxy)
  25.         r('username', self.proxy)
  26.         r('password', self.proxy)
  27.         r('max_cover', self.proxy)
  28.         r('max_opds_items', self.proxy)
  29.         r('max_opds_ungrouped_items', self.proxy)
  30.         (self.show_server_password.stateChanged[int].connect,)((lambda s: None(self.opt_password.setEchoMode if s == Qt.Checked else self.opt_password.Password)))
  31.         self.opt_password.setEchoMode(self.opt_password.Password)
  32.         restrictions = sorted(saved_searches().names(), cmp = (lambda x, y: cmp(x.lower(), y.lower())))
  33.         choices = [] + [ (x, x) for x in restrictions ]
  34.         r('cs_restriction', db.prefs, choices = choices)
  35.         self.start_button.setEnabled(not getattr(self.server, 'is_running', False))
  36.         self.test_button.setEnabled(not self.start_button.isEnabled())
  37.         self.stop_button.setDisabled(self.start_button.isEnabled())
  38.         self.start_button.clicked.connect(self.start_server)
  39.         self.stop_button.clicked.connect(self.stop_server)
  40.         self.test_button.clicked.connect(self.test_server)
  41.         self.view_logs.clicked.connect(self.view_server_logs)
  42.         r('autolaunch_server', config)
  43.  
  44.     
  45.     def set_server_options(self):
  46.         c = self.proxy
  47.         c.set('port', self.opt_port.value())
  48.         c.set('username', unicode(self.opt_username.text()).strip())
  49.         p = unicode(self.opt_password.text()).strip()
  50.         if not p:
  51.             p = None
  52.         
  53.         c.set('password', p)
  54.  
  55.     
  56.     def start_server(self):
  57.         self.set_server_options()
  58.         start_threaded_server = start_threaded_server
  59.         import calibre.library.server.main
  60.         self.server = start_threaded_server(self.db, server_config().parse())
  61.         while not (self.server.is_running) and self.server.exception is None:
  62.             time.sleep(1)
  63.         if self.server.exception is not None:
  64.             error_dialog(self, _('Failed to start content server'), unicode(self.server.exception)).exec_()
  65.             return None
  66.         self.start_button.setEnabled(False)
  67.         self.test_button.setEnabled(True)
  68.         self.stop_button.setEnabled(True)
  69.  
  70.     
  71.     def stop_server(self):
  72.         stop_threaded_server = stop_threaded_server
  73.         import calibre.library.server.main
  74.         stop_threaded_server(self.server)
  75.         self.server = None
  76.         self.start_button.setEnabled(True)
  77.         self.test_button.setEnabled(False)
  78.         self.stop_button.setEnabled(False)
  79.  
  80.     
  81.     def test_server(self):
  82.         open_url(QUrl('http://127.0.0.1:' + str(self.opt_port.value())))
  83.  
  84.     
  85.     def view_server_logs(self):
  86.         log_access_file = log_access_file
  87.         log_error_file = log_error_file
  88.         import calibre.library.server
  89.         d = QDialog(self)
  90.         d.resize(QSize(800, 600))
  91.         layout = QVBoxLayout()
  92.         d.setLayout(layout)
  93.         layout.addWidget(QLabel(_('Error log:')))
  94.         el = QPlainTextEdit(d)
  95.         layout.addWidget(el)
  96.         
  97.         try:
  98.             el.setPlainText(open(log_error_file, 'rb').read().decode('utf8', 'replace'))
  99.         except IOError:
  100.             el.setPlainText('No error log found')
  101.  
  102.         layout.addWidget(QLabel(_('Access log:')))
  103.         al = QPlainTextEdit(d)
  104.         layout.addWidget(al)
  105.         
  106.         try:
  107.             al.setPlainText(open(log_access_file, 'rb').read().decode('utf8', 'replace'))
  108.         except IOError:
  109.             al.setPlainText('No access log found')
  110.  
  111.         bx = QDialogButtonBox(QDialogButtonBox.Ok)
  112.         layout.addWidget(bx)
  113.         bx.accepted.connect(d.accept)
  114.         d.show()
  115.  
  116.     
  117.     def commit(self):
  118.         ConfigWidgetBase.commit(self)
  119.         warning_dialog(self, _('Restart needed'), _('You need to restart the server for changes to take effect'), show = True)
  120.         return False
  121.  
  122.     
  123.     def refresh_gui(self, gui):
  124.         gui.content_server = self.server
  125.         if gui.content_server is not None:
  126.             gui.content_server.state_callback = Dispatcher(gui.iactions['Connect Share'].content_server_state_changed)
  127.             gui.content_server.state_callback(gui.content_server.is_running)
  128.         
  129.  
  130.  
  131. if __name__ == '__main__':
  132.     from PyQt4.Qt import QApplication
  133.     app = QApplication([])
  134.     test_widget('Sharing', 'Server')
  135.  
  136.