WebBrowser/SafeBrowsing/SafeBrowsingDialog.py

Sat, 29 Jul 2017 19:41:16 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 29 Jul 2017 19:41:16 +0200
branch
safe_browsing
changeset 5820
b610cb5b501a
child 5821
6c7766cde4c1
permissions
-rw-r--r--

Started implementing the safe browsing manager and management dialog.

# -*- coding: utf-8 -*-

# Copyright (c) 2017 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a dialog to configure safe browsing support.
"""

from __future__ import unicode_literals

from PyQt5.QtCore import pyqtSlot, Qt
from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QAbstractButton, \
    QApplication

from E5Gui import E5MessageBox

from .Ui_SafeBrowsingDialog import Ui_SafeBrowsingDialog

import UI.PixmapCache
import Preferences


class SafeBrowsingDialog(QDialog, Ui_SafeBrowsingDialog):
    """
    Class implementing a dialog to configure safe browsing support.
    """
    def __init__(self, manager, parent=None):
        """
        Constructor
        
        @param manager reference to the safe browsing manager
        @type SafeBrowsingManager
        @param parent reference to the parent widget
        @type QWidget
        """
        super(SafeBrowsingDialog, self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(Qt.Window)
        
        self.__manager = manager
        
        self.__saveButton = self.buttonBox.addButton(
            self.tr("Save"), QDialogButtonBox.ActionRole)
        
        self.iconLabel.setPixmap(
            UI.PixmapCache.getPixmap("safeBrowsing48.png"))
        
        self.__gsbHelpDialog = None
        
        self.__enabled = Preferences.getWebBrowser("SafeBrowsingEnabled")
        self.__apiKey = Preferences.getWebBrowser("SafeBrowsingApiKey")
        
        self.buttonBox.setFocus()
        
        msh = self.minimumSizeHint()
        self.resize(max(self.width(), msh.width()), msh.height())
    
    def show(self):
        """
        Public slot to show the dialog.
        """
        self.gsbGroupBox.setChecked(self.__enabled)
        self.gsbApiKeyEdit.setText(self.__apiKey)
        
        self.__updateCacheButtons()
        
        super(SafeBrowsingDialog, self).show()
    
    @pyqtSlot()
    def on_gsbHelpButton_clicked(self):
        """
        Private slot to show some help text "How to create a safe
        browsing API key.".
        """
        if self.__gsbHelpDialog is None:
            from E5Gui.E5SimpleHelpDialog import E5SimpleHelpDialog
            from . import SafeBrowsingHelp
            
            helpStr = SafeBrowsingHelp()
            self.__gsbHelpDialog = E5SimpleHelpDialog(
                title=self.tr("Google Safe Browsing API Help"),
                helpStr=helpStr, parent=self)
        
        self.__gsbHelpDialog.show()
    
    @pyqtSlot(QAbstractButton)
    def on_buttonBox_clicked(self, button):
        """
        Private slot called by a button of the button box clicked.
        
        @param button button that was clicked (QAbstractButton)
        """
        if button == self.buttonBox.button(QDialogButtonBox.Close):
            self.close()
        elif button == self.__saveButton:
            self.__save()
    
    @pyqtSlot()
    def __save(self):
        """
        Private slot to save the configuration.
        
        @return flag indicating success
        @rtype bool
        """
        self.__enabled = self.gsbGroupBox.isChecked()
        self.__apiKey = self.gsbApiKeyEdit.text()
        
        Preferences.setWebBrowser("SafeBrowsingEnabled", self.__enabled)
        Preferences.setWebBrowser("SafeBrowsingApiKey", self.__apiKey)
        
        self.__manager.configurationChanged()
        
        self.__updateCacheButtons()
        
        return True
    
    def closeEvent(self, evt):
        """
        Protected method to handle close events.
        
        @param evt reference to the close event
        @type QCloseEvent
        """
        if self.__okToClose():
            evt.accept()
        else:
            evt.ignore()
    
    def __isModified(self):
        """
        Private method to check, if the dialog contains modified data.
        
        @return flag indicating the presence of modified data
        @rtype bool
        """
        return (
            self.__enabled != self.gsbGroupBox.isChecked() or
            self.__apiKey != self.gsbApiKeyEdit.text()
        )
    
    def __okToClose(self):
        """
        Private method to check, if it is safe to close the dialog.
        
        @return flag indicating safe to close
        @rtype bool
        """
        if self.__isModified():
            res = E5MessageBox.okToClearData(
                self,
                self.tr("Safe Browsing Management"),
                self.tr("""The dialog contains unsaved changes."""),
                self.__save)
            if not res:
                return False
        return True
    
    def __updateCacheButtons(self):
        """
        Private method to set enabled state of the cache buttons.
        """
        enable = self.__enabled and bool(self.__apiKey)
        
        self.updateCacheButton.setEnabled(enable)
        self.clearCacheButton.setEnabled(enable)
    
    @pyqtSlot()
    def on_updateCacheButton_clicked(self):
        """
        Private slot to update the local cache database.
        """
        QApplication.setOverrideCursor(Qt.WaitCursor)
        ok, error = self.__manager.updateHashPrefixCache()
        QApplication.restoreOverrideCursor()
##        QApplication.processEvents()
        if not ok:
            if error:
                E5MessageBox.critical(
                    self,
                    self.tr("Update Safe Browsing Cache"),
                    self.tr("""<p>Updating the Safe Browsing cache failed."""
                            """</p><p>Reason: {0}</p>""").format(error))
            else:
                E5MessageBox.critical(
                    self,
                    self.tr("Update Safe Browsing Cache"),
                    self.tr("""<p>Updating the Safe Browsing cache failed."""
                            """</p>"""))
    
    @pyqtSlot()
    def on_clearCacheButton_clicked(self):
        """
        Private slot to clear the local cache database.
        """
        self.__manager.fullCacheCleanup()

eric ide

mercurial