def configure_webview_inspector_ui(self): if self.plugin_prefs.get('inspector_enabled'): self.webview.setMinimumSize(QSize(600, 300)) self.inspector = QWebInspector() self.inspector.setMinimumSize(QSize(600, 300)) self.inspector.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.l.addWidget(self.inspector) self.inspector.setPage(self.webview.page()) else: self.webview.setMinimumSize(QSize(600, 600)) if self.inspector is not None: self.l.removeWidget(self.inspector) self.inspector.setParent(None) self.inspector = None
def __init__(self): QWidget.__init__(self) self.l = QVBoxLayout() self.setLayout(self.l) #add checkbox self.debug_checkbox = QCheckBox('Debug Mode') self.debug_checkbox.setChecked(prefs['debug_mode']) self.l.addWidget(self.debug_checkbox) self.config_url = QUrl('https://'+prefs['api_base']+'/link/start') self.webview = QTWebView(bearer_token=prefs['token']) self.webview.load(self.config_url) if prefs['debug_mode']: self.webview.setMinimumSize(QSize(600, 300)) else: self.webview.setMinimumSize(QSize(600, 600)) self.global_settings = self.webview.page().settings() #QWebSettings.globalSettings() self.global_settings.setAttribute(QWebSettings.DeveloperExtrasEnabled, True) self.global_settings.setAttribute(QWebSettings.JavascriptEnabled, True) self.global_settings.setAttribute(QWebSettings.JavascriptCanOpenWindows, True) self.global_settings.setAttribute(QWebSettings.JavascriptCanCloseWindows, True) self.global_settings.setAttribute(QWebSettings.PluginsEnabled, True) self.global_settings.setAttribute(QWebSettings.LocalContentCanAccessRemoteUrls, True) self.global_settings.setAttribute(QWebSettings.LocalContentCanAccessFileUrls, True) self.global_settings.setAttribute(QWebSettings.XSSAuditingEnabled, False) self.global_settings.setAttribute(QWebSettings.PrivateBrowsingEnabled, True) self.webview.show() self.l.addWidget(self.webview) if prefs['debug_mode']: self.inspector = QWebInspector() self.inspector.setMinimumSize(QSize(600, 300)) self.inspector.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding); self.l.addWidget(self.inspector) self.inspector.setPage(self.webview.page())
class ConfigWidget(QWidget): def __init__(self): self.plugin_prefs = prefs self.restart_required = False self.inspector = None QWidget.__init__(self) self.l = QVBoxLayout() self.setLayout(self.l) #add checkbox self.inspector_checkbox = QCheckBox('Show Inspector') self.inspector_checkbox.setChecked( self.plugin_prefs.get('inspector_enabled', False)) self.inspector_checkbox.stateChanged.connect( self._inspector_enabled_changed) self.inspector_checkbox.setToolTip( 'Enable Javascript Console for debugging WebView requests to QuietThyme API' ) self.l.addWidget(self.inspector_checkbox) self.beta_checkbox = QCheckBox('Beta Mode') self.beta_checkbox.setChecked(self.plugin_prefs.get( 'beta_mode', False)) self.beta_checkbox.stateChanged.connect(self._set_restart_required) self.beta_checkbox.stateChanged.connect(self._beta_mode_changed) self.beta_checkbox.setToolTip( 'Tell Calibre to communicate with the Beta version of QuietThyme') self.l.addWidget(self.beta_checkbox) self.config_url = QUrl(self.plugin_prefs.get('web_base') + '/storage') self.webview = QTWebView(bearer_token=self.plugin_prefs.get('token')) self.webview.load(self.config_url) self.global_settings = self.webview.page().settings( ) #QWebSettings.globalSettings() self.global_settings.setAttribute( QWebSettings.LocalStorageEnabled, True) #required since this is where we store tokens. self.global_settings.setAttribute(QWebSettings.PrivateBrowsingEnabled, False) self.global_settings.setAttribute(QWebSettings.JavascriptEnabled, True) self.global_settings.setAttribute( QWebSettings.JavascriptCanOpenWindows, True) self.global_settings.setAttribute( QWebSettings.JavascriptCanCloseWindows, True) self.global_settings.setAttribute(QWebSettings.PluginsEnabled, True) self.global_settings.setAttribute( QWebSettings.LocalContentCanAccessRemoteUrls, True) self.global_settings.setAttribute( QWebSettings.LocalContentCanAccessFileUrls, True) self.global_settings.setAttribute(QWebSettings.XSSAuditingEnabled, False) self.global_settings.setAttribute(QWebSettings.DeveloperExtrasEnabled, True) # ensure that we get a random offline/localstorage/cache path for the brower (so that the localstorage data is not persistent across sessions) path = None while True: potential_path = QTemporaryDir() if potential_path.isValid(): path = potential_path.path() break self.global_settings.setOfflineStoragePath(path) self.global_settings.setOfflineWebApplicationCachePath(path) self.global_settings.enablePersistentStorage(path) self.global_settings.setLocalStoragePath(path) self.webview.show() self.l.addWidget(self.webview) self.configure_webview_inspector_ui() self.webview.urlChanged.connect(self._url_changed) self.webview.loadFinished.connect(self._load_finished) ################################################################################################################ # Configure UI methods # def configure_webview_inspector_ui(self): if self.plugin_prefs.get('inspector_enabled'): self.webview.setMinimumSize(QSize(600, 300)) self.inspector = QWebInspector() self.inspector.setMinimumSize(QSize(600, 300)) self.inspector.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.l.addWidget(self.inspector) self.inspector.setPage(self.webview.page()) else: self.webview.setMinimumSize(QSize(600, 600)) if self.inspector is not None: self.l.removeWidget(self.inspector) self.inspector.setParent(None) self.inspector = None def save_settings(self): self.plugin_prefs.set('inspector_enabled', self.inspector_checkbox.isChecked()) self.plugin_prefs.set('beta_mode', self.beta_checkbox.isChecked()) # # If restart needed, inform user if self.restart_required: # do_restart = show_restart_warning('Restart calibre for the changes to be applied.', # parent=self.l) # if do_restart: # self.gui.quit(restart=True) # TODO: figure out if theres a way to call self.gui.quit() msg = QMessageBox() msg.setIcon(QMessageBox.Warning) msg.setText("Restart Required") msg.setInformativeText( "A configuration change requires you to restart Calibre, You should do so now," ) msg.setWindowTitle("Restart Required") msg.setStandardButtons(QMessageBox.Ok) msg.exec_() def validate(self): # TODO: validate the that the api_endpoint and web endpoint are valid urls. return True ################################################################################################################ # Event Handlers # def _url_changed(self, url): logger.debug(sys._getframe().f_code.co_name, url) def _load_finished(self, ok): logger.debug(sys._getframe().f_code.co_name, ok) if self.webview.page().mainFrame().url() == self.config_url: logger.debug( "Loading finished and the url is the same as the desired destination url, ", self.config_url) self.webview.page().mainFrame().evaluateJavaScript(""" console.log("GET LOCALSTORAGE TOKEN") """) token = self.webview.page().mainFrame().evaluateJavaScript(""" localStorage.getItem('id_token'); """) logger.debug("Got JWT Token", token) self.plugin_prefs.set('token', token) def _set_restart_required(self, state): ''' Set restart_required flag to show show dialog when closing dialog ''' logger.debug(sys._getframe().f_code.co_name, "restart required") self.restart_required = True def _beta_mode_changed(self, state): logger.debug(sys._getframe().f_code.co_name, self.beta_checkbox.isChecked()) self.plugin_prefs.set('beta_mode', self.beta_checkbox.isChecked()) # if the beta mode has changed, we need to reset the api endpoints, and then wipe the token (not valid between envs) self.plugin_prefs.set('token', '') if self.plugin_prefs.get('beta_mode'): self.plugin_prefs.set('api_base', beta_api_base) self.plugin_prefs.set('web_base', beta_web_base) else: self.plugin_prefs.set('api_base', master_api_base) self.plugin_prefs.set('web_base', master_web_base) # after we reset the token, we need to generate a new QTWebView with the new config, and then reload the login page. self.config_url = QUrl(self.plugin_prefs.get('web_base') + '/storage') self.webview.set_bearer_token(self.plugin_prefs.get('token')) self.webview.load(self.config_url) def _inspector_enabled_changed(self, state): logger.debug(sys._getframe().f_code.co_name, self.inspector_checkbox.isChecked()) self.plugin_prefs.set('inspector_enabled', self.inspector_checkbox.isChecked()) self.configure_webview_inspector_ui()
class ConfigWidget(QWidget): def __init__(self): QWidget.__init__(self) self.l = QVBoxLayout() self.setLayout(self.l) #add checkbox self.debug_checkbox = QCheckBox('Debug Mode') self.debug_checkbox.setChecked(prefs['debug_mode']) self.l.addWidget(self.debug_checkbox) self.config_url = QUrl('https://'+prefs['api_base']+'/link/start') self.webview = QTWebView(bearer_token=prefs['token']) self.webview.load(self.config_url) if prefs['debug_mode']: self.webview.setMinimumSize(QSize(600, 300)) else: self.webview.setMinimumSize(QSize(600, 600)) self.global_settings = self.webview.page().settings() #QWebSettings.globalSettings() self.global_settings.setAttribute(QWebSettings.DeveloperExtrasEnabled, True) self.global_settings.setAttribute(QWebSettings.JavascriptEnabled, True) self.global_settings.setAttribute(QWebSettings.JavascriptCanOpenWindows, True) self.global_settings.setAttribute(QWebSettings.JavascriptCanCloseWindows, True) self.global_settings.setAttribute(QWebSettings.PluginsEnabled, True) self.global_settings.setAttribute(QWebSettings.LocalContentCanAccessRemoteUrls, True) self.global_settings.setAttribute(QWebSettings.LocalContentCanAccessFileUrls, True) self.global_settings.setAttribute(QWebSettings.XSSAuditingEnabled, False) self.global_settings.setAttribute(QWebSettings.PrivateBrowsingEnabled, True) self.webview.show() self.l.addWidget(self.webview) if prefs['debug_mode']: self.inspector = QWebInspector() self.inspector.setMinimumSize(QSize(600, 300)) self.inspector.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding); self.l.addWidget(self.inspector) self.inspector.setPage(self.webview.page()) # self.webview.urlChanged.connect(_url_changed) # self.webview.loadFinished.connect(_load_finished) ################################################################################################################ # Event Handlers # # def _url_changed(url): # print('url changed: ', url) # # def _load_finished(ok): # print('load finished, ok: ', ok) # if self.webview.page().mainFrame().url() == self.config_url: # print('requested url = current url') # # token = self.webview.page().mainFrame().evaluateJavaScript(""" # # getAuthToken(); # # """) # # print(token) # # prefs['token'] = token def save_settings(self): prefs['debug_mode'] = self.debug_checkbox.isChecked() def validate(self): return True #self.webview.page().mainFrame().url() == self.config_url