1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2011 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4#
5
6"""
7Module implementing VirusTotal configuration page (web browser variant).
8"""
9
10from PyQt5.QtCore import pyqtSlot
11
12from .ConfigurationPageBase import ConfigurationPageBase
13from .Ui_WebBrowserVirusTotalPage import Ui_WebBrowserVirusTotalPage
14
15import Preferences
16
17
18class WebBrowserVirusTotalPage(ConfigurationPageBase,
19                               Ui_WebBrowserVirusTotalPage):
20    """
21    Class implementing VirusTotal configuration page (web browser variant).
22    """
23    def __init__(self):
24        """
25        Constructor
26        """
27        super().__init__()
28        self.setupUi(self)
29        self.setObjectName("HelpVirusTotalPage")
30
31        self.testResultLabel.setHidden(True)
32
33        from WebBrowser.VirusTotal.VirusTotalApi import VirusTotalAPI
34        self.__vt = VirusTotalAPI(self)
35        self.__vt.checkServiceKeyFinished.connect(
36            self.__checkServiceKeyFinished)
37
38        # set initial values
39        self.vtEnabledCheckBox.setChecked(
40            Preferences.getWebBrowser("VirusTotalEnabled"))
41        self.vtSecureCheckBox.setChecked(
42            Preferences.getWebBrowser("VirusTotalSecure"))
43        self.vtServiceKeyEdit.setText(
44            Preferences.getWebBrowser("VirusTotalServiceKey"))
45
46    def save(self):
47        """
48        Public slot to save the VirusTotal configuration.
49        """
50        Preferences.setWebBrowser(
51            "VirusTotalEnabled",
52            self.vtEnabledCheckBox.isChecked())
53        Preferences.setWebBrowser(
54            "VirusTotalSecure",
55            self.vtSecureCheckBox.isChecked())
56        Preferences.setWebBrowser(
57            "VirusTotalServiceKey",
58            self.vtServiceKeyEdit.text())
59
60    @pyqtSlot(str)
61    def on_vtServiceKeyEdit_textChanged(self, txt):
62        """
63        Private slot to handle changes of the service key.
64
65        @param txt entered service key (string)
66        """
67        self.testButton.setEnabled(txt != "")
68
69    @pyqtSlot()
70    def on_testButton_clicked(self):
71        """
72        Private slot to test the entered service key.
73        """
74        self.testResultLabel.setHidden(False)
75        self.testResultLabel.setText(
76            self.tr("Checking validity of the service key..."))
77        protocol = "https" if self.vtSecureCheckBox.isChecked() else "http"
78        self.__vt.checkServiceKeyValidity(
79            self.vtServiceKeyEdit.text(), protocol)
80
81    @pyqtSlot(bool, str)
82    def __checkServiceKeyFinished(self, result, msg):
83        """
84        Private slot to receive the result of the service key check.
85
86        @param result flag indicating a successful check (boolean)
87        @param msg network error message (str)
88        """
89        if result:
90            self.testResultLabel.setText(
91                self.tr("The service key is valid."))
92        else:
93            if msg == "":
94                self.testResultLabel.setText(self.tr(
95                    '<font color="#FF0000">The service key is'
96                    ' not valid.</font>'))
97            else:
98                self.testResultLabel.setText(self.tr(
99                    '<font color="#FF0000"><b>Error:</b> {0}</font>')
100                    .format(msg))
101
102
103def create(dlg):
104    """
105    Module function to create the configuration page.
106
107    @param dlg reference to the configuration dialog
108    @return reference to the instantiated page (ConfigurationPageBase)
109    """
110    page = WebBrowserVirusTotalPage()
111    return page
112