def __updateMatcher(self): """ Private slot to update the adblock matcher. """ from WebBrowser.WebBrowserWindow import WebBrowserWindow WebBrowserWindow.networkManager().removeUrlInterceptor( self.__interceptor) if self.__enabled: self.__matcher.update() else: self.__matcher.clear() WebBrowserWindow.networkManager().installUrlInterceptor( self.__interceptor)
def __downloadDictionary(self): """ Private slot to download a dictionary. """ if self.__isOnline(): if self.__dictionariesToDownload: url = self.__dictionariesToDownload.pop(0) self.statusLabel.setText(url) self.__downloadCancelled = False request = QNetworkRequest(QUrl(url)) request.setAttribute(QNetworkRequest.CacheLoadControlAttribute, QNetworkRequest.AlwaysNetwork) reply = WebBrowserWindow.networkManager().get(request) reply.finished.connect(lambda: self.__installDictionary(reply)) reply.downloadProgress.connect(self.__downloadProgress) self.__replies.append(reply) else: self.__installationFinished() else: E5MessageBox.warning( self, self.tr("Error downloading dictionary file"), self.tr( """<p>Could not download the requested dictionary file""" """ from {0}.</p><p>Error: {1}</p>""").format( url, self.tr("Computer is offline."))) self.__installationFinished()
def __populateList(self): """ Private method to populate the list of available plugins. """ self.dictionariesList.clear() self.downloadProgress.setValue(0) url = self.dictionariesUrlEdit.text() if self.__isOnline(): self.__refreshButton.setEnabled(False) self.__installButton.setEnabled(False) self.__uninstallButton.setEnabled(False) self.__cancelButton.setEnabled(True) self.statusLabel.setText(url) self.__downloadCancelled = False request = QNetworkRequest(QUrl(url)) request.setAttribute(QNetworkRequest.CacheLoadControlAttribute, QNetworkRequest.AlwaysNetwork) reply = WebBrowserWindow.networkManager().get(request) reply.finished.connect(lambda: self.__listFileDownloaded(reply)) reply.downloadProgress.connect(self.__downloadProgress) self.__replies.append(reply) else: E5MessageBox.warning( self, self.tr("Error populating list of dictionaries"), self.tr("""<p>Could not download the dictionaries list""" """ from {0}.</p><p>Error: {1}</p>""").format( url, self.tr("Computer is offline.")))
def setView(self, view, inspectElement=False): """ Public method to connect a view to this inspector. @param view reference to the view object @type WebBrowserView @param inspectElement flag indicating to start a web inspection @type bool """ self.__view = view if not self.isEnabled(): return self.__inspectElement = inspectElement try: self.page().setInspectedPage(self.__view.page()) except AttributeError: # pre Qt 5.11 port = Preferences.getWebBrowser("WebInspectorPort") inspectorUrl = QUrl("http://localhost:{0}".format(port)) from WebBrowser.WebBrowserWindow import WebBrowserWindow self.__reply = WebBrowserWindow.networkManager().get( QNetworkRequest(inspectorUrl.resolved(QUrl("json/list")))) self.__reply.finished.connect(self.__inspectorReplyFinished)
def getThreatLists(self): """ Public method to retrieve all available threat lists. @return tuple containing list of threat lists and an error message @rtype tuple of (list of dict containing 'threatType', 'platformType' and 'threatEntryType', bool) """ url = QUrl(self.GsbUrlTemplate.format("threatLists", self.__apiKey)) req = QNetworkRequest(url) reply = WebBrowserWindow.networkManager().get(req) while reply.isRunning(): QCoreApplication.processEvents(QEventLoop.AllEvents, 200) # max. 200 ms processing res = None error = "" if reply.error() != QNetworkReply.NoError: error = reply.errorString() self.networkError.emit(error) else: result = self.__extractData(reply) res = result["threatLists"] reply.deleteLater() return res, error
def __init__(self, parent=None): """ Constructor @param parent reference to the parent object @type QObject """ super(AdBlockManager, self).__init__(parent) self.__loaded = False self.__subscriptionsLoaded = False self.__enabled = False self.__adBlockDialog = None self.__adBlockExceptionsDialog = None self.__adBlockNetwork = None self.__adBlockPage = None self.__subscriptions = [] self.__exceptedHosts = Preferences.getWebBrowser("AdBlockExceptions") self.__saveTimer = AutoSaver(self, self.save) self.__limitedEasyList = Preferences.getWebBrowser( "AdBlockUseLimitedEasyList") self.__defaultSubscriptionUrlString = ( "abp:subscribe?location=" "https://easylist-downloads.adblockplus.org/easylist.txt&" "title=EasyList") self.__additionalDefaultSubscriptionUrlStrings = ( "abp:subscribe?location=https://raw.githubusercontent.com/" "hoshsadiq/adblock-nocoin-list/master/nocoin.txt&" "title=NoCoin", ) self.__customSubscriptionUrlString = (bytes( self.__customSubscriptionUrl().toEncoded()).decode()) self.__mutex = QMutex() self.__matcher = AdBlockMatcher(self) self.rulesChanged.connect(self.__saveTimer.changeOccurred) self.rulesChanged.connect(self.__rulesChanged) self.__interceptor = AdBlockUrlInterceptor(self) from WebBrowser.WebBrowserWindow import WebBrowserWindow WebBrowserWindow.networkManager().installUrlInterceptor( self.__interceptor)
def certificateError(self, error): """ Public method to handle SSL certificate errors. @param error object containing the certificate error information @type QWebEngineCertificateError @return flag indicating to ignore this error @rtype bool """ return WebBrowserWindow.networkManager().certificateError( error, self.view())
def getThreatsUpdate(self, clientStates): """ Public method to fetch hash prefix updates for the given threat list. @param clientStates dictionary of client states with keys like (threatType, platformType, threatEntryType) @type dict @return tuple containing the list of threat updates and an error message @rtype tuple of (list of dict, bool) """ requestBody = { "client": { "clientId": self.ClientId, "clientVersion": self.ClientVersion, }, "listUpdateRequests": [], } for (threatType, platformType, threatEntryType), currentState in (clientStates.items()): requestBody["listUpdateRequests"].append({ "threatType": threatType, "platformType": platformType, "threatEntryType": threatEntryType, "state": currentState, "constraints": { "supportedCompressions": ["RAW"], } }) data = QByteArray(json.dumps(requestBody).encode("utf-8")) url = QUrl( self.GsbUrlTemplate.format("threatListUpdates:fetch", self.__apiKey)) req = QNetworkRequest(url) req.setHeader(QNetworkRequest.ContentTypeHeader, "application/json") reply = WebBrowserWindow.networkManager().post(req, data) while reply.isRunning(): QCoreApplication.processEvents(QEventLoop.AllEvents, 200) # max. 200 ms processing res = None error = "" if reply.error() != QNetworkReply.NoError: error = reply.errorString() self.networkError.emit(error) else: result = self.__extractData(reply) res = result["listUpdateResponses"] reply.deleteLater() return res, error
def __init__(self, parent=None): """ Constructor @param parent parent widget of this window (QWidget) """ super(WebBrowserPage, self).__init__(WebBrowserWindow.webProfile(), parent) self.__printer = None self.__badSite = False self.__registerProtocolHandlerRequest = None self.featurePermissionRequested.connect( self.__featurePermissionRequested) self.authenticationRequired.connect( lambda url, auth: WebBrowserWindow.networkManager().authentication( url, auth, self)) self.proxyAuthenticationRequired.connect( WebBrowserWindow.networkManager().proxyAuthentication) self.fullScreenRequested.connect(self.__fullScreenRequested) self.urlChanged.connect(self.__urlChanged) self.contentsSizeChanged.connect(self.__contentsSizeChanged) try: self.registerProtocolHandlerRequested.connect( self.__registerProtocolHandlerRequested) except AttributeError: # defined for Qt >= 5.11 pass # Workaround for changing webchannel world inside # acceptNavigationRequest not working self.__channelUrl = QUrl() self.__channelWorldId = -1 self.__setupChannelTimer = QTimer(self) self.__setupChannelTimer.setSingleShot(True) self.__setupChannelTimer.setInterval(100) self.__setupChannelTimer.timeout.connect(self.__setupChannelTimeout)
def __reloadFeed(self, itm): """ Private method to reload the given feed. @param itm feed item to be reloaded (QTreeWidgetItem) """ urlString = itm.data(0, FeedsManager.UrlStringRole) if urlString == "": return for child in itm.takeChildren(): del child from WebBrowser.WebBrowserWindow import WebBrowserWindow request = QNetworkRequest(QUrl(urlString)) reply = WebBrowserWindow.networkManager().get(request) reply.finished.connect(lambda: self.__feedLoaded(reply)) self.__replies[id(reply)] = (reply, itm)
def __addEngineByUrl(self, url): """ Private method to add a new search engine given its URL. @param url URL of the engine definition file (QUrl) @return flag indicating success (boolean) """ if not url.isValid(): return False from WebBrowser.WebBrowserWindow import WebBrowserWindow reply = WebBrowserWindow.networkManager().get(QNetworkRequest(url)) reply.finished.connect(lambda: self.__engineFromUrlAvailable(reply)) reply.setParent(self) self.__replies.append(reply) return True
def __init__(self, parent=None): """ Constructor @param parent reference to the parent widget (QWidget) """ super(DownloadManager, self).__init__(parent) self.setupUi(self) self.setWindowFlags(Qt.Window) self.__winTaskbarButton = None self.__saveTimer = AutoSaver(self, self.save) self.__model = DownloadModel(self) self.__manager = WebBrowserWindow.networkManager() self.__iconProvider = None self.__downloads = [] self.__downloadDirectory = "" self.__loaded = False self.__rowHeightMultiplier = 1.1 self.setDownloadDirectory(Preferences.getUI("DownloadPath")) self.downloadsView.setShowGrid(False) self.downloadsView.verticalHeader().hide() self.downloadsView.horizontalHeader().hide() self.downloadsView.setAlternatingRowColors(True) self.downloadsView.horizontalHeader().setStretchLastSection(True) self.downloadsView.setModel(self.__model) self.downloadsView.setContextMenuPolicy(Qt.CustomContextMenu) self.downloadsView.customContextMenuRequested.connect( self.__customContextMenuRequested) self.__clearShortcut = QShortcut(QKeySequence("Ctrl+L"), self) self.__clearShortcut.activated.connect(self.on_cleanupButton_clicked) self.__load() self.__updateTimer = QBasicTimer()
def updateNow(self): """ Public method to update the subscription immediately. """ if self.__downloading is not None: return if not self.location().isValid(): return if self.location().scheme() == "file": self.__lastUpdate = QDateTime.currentDateTime() self.__loadRules() return from WebBrowser.WebBrowserWindow import WebBrowserWindow reply = WebBrowserWindow.networkManager().get( QNetworkRequest(self.location())) reply.finished.connect( lambda: self.__rulesDownloaded(reply)) self.__downloading = reply
def on_imagesTree_currentItemChanged(self, current, previous): """ Private slot to show a preview of the selected image. @param current current image entry (QTreeWidgetItem) @param previous old current entry (QTreeWidgetItem) """ if current is None: return imageUrl = QUrl(current.text(1)) if imageUrl.isRelative(): imageUrl = self.__baseUrl.resolved(imageUrl) pixmap = QPixmap() loading = False if imageUrl.scheme() == "data": encodedUrl = current.text(1).encode("utf-8") imageData = encodedUrl[encodedUrl.find(b",") + 1:] pixmap = WebBrowserTools.pixmapFromByteArray(imageData) elif imageUrl.scheme() == "file": pixmap = QPixmap(imageUrl.toLocalFile()) elif imageUrl.scheme() == "qrc": pixmap = QPixmap(imageUrl.toString()[3:]) else: if self.__imageReply is not None: self.__imageReply.deleteLater() self.__imageReply = None from WebBrowser.WebBrowserWindow import WebBrowserWindow self.__imageReply = WebBrowserWindow.networkManager().get( QNetworkRequest(imageUrl)) self.__imageReply.finished.connect(self.__imageReplyFinished) loading = True self.__showLoadingText() if not loading: self.__showPixmap(pixmap)
def __init__(self, url, manager, mode): """ Constructor @param url URL to download script from @type QUrl @param manager reference to the GreaseMonkey manager @type GreaseMonkeyManager @param mode download mode @type int (one of DownloadMainScript, DownloadRequireScript) """ super(GreaseMonkeyDownloader, self).__init__() self.__manager = manager self.__reply = WebBrowserWindow.networkManager().get( QNetworkRequest(url)) if mode == GreaseMonkeyDownloader.DownloadMainScript: self.__reply.finished.connect(self.__scriptDownloaded) else: self.__reply.finished.connect(self.__requireDownloaded) self.__fileName = ""
def lookupUrl(self, url, platforms): """ Public method to send an URL to Google for checking. @param url URL to be checked @type QUrl @param platforms list of platform types to check against @type list of str @return tuple containing the list of threat list info objects and an error message @rtype tuple of (list of ThreatList, str) """ error = "" # sanitize the URL by removing user info and query data url = url.adjusted(QUrl.RemoveUserInfo | QUrl.RemoveQuery | QUrl.RemoveFragment) urlStr = url.toString() # check the local cache first if urlStr in self.__lookupApiCache: if (self.__lookupApiCache[urlStr]["validUntil"] > QDateTime.currentDateTime()): # cached entry is still valid return self.__lookupApiCache[urlStr]["threatInfo"], error else: del self.__lookupApiCache[urlStr] # no valid entry found, ask the safe browsing server requestBody = { "client": { "clientId": self.ClientId, "clientVersion": self.ClientVersion, }, "threatInfo": { "threatTypes": SafeBrowsingAPIClient.definedThreatTypes(), "platformTypes": platforms, "threatEntryTypes": SafeBrowsingAPIClient.definedThreatEntryTypes(), "threatEntries": [ { "url": urlStr }, ], }, } data = QByteArray(json.dumps(requestBody).encode("utf-8")) url = QUrl( self.GsbUrlTemplate.format("threatMatches:find", self.__apiKey)) req = QNetworkRequest(url) req.setHeader(QNetworkRequest.ContentTypeHeader, "application/json") reply = WebBrowserWindow.networkManager().post(req, data) while reply.isRunning(): QCoreApplication.processEvents(QEventLoop.AllEvents, 200) # max. 200 ms processing threats = [] if reply.error() != QNetworkReply.NoError: error = reply.errorString() self.networkError.emit(error) else: res = json.loads(str(reply.readAll(), "utf-8")) if res and "matches" in res: cacheDuration = 0 for match in res["matches"]: threatInfo = ThreatList( match["threatType"], match["platformType"], match["threatEntryType"], ) threats.append(threatInfo) if "cacheDuration" in match: cacheDurationSec = int( match["cacheDuration"].strip().rstrip("s").split( ".")[0]) if cacheDurationSec > cacheDuration: cacheDuration = cacheDurationSec if cacheDuration > 0 and bool(threats): validUntil = QDateTime.currentDateTime().addSecs( cacheDuration) self.__lookupApiCache[urlStr] = { "validUntil": validUntil, "threatInfo": threats } reply.deleteLater() return threats, error
def getFullHashes(self, prefixes, clientState): """ Public method to find full hashes matching hash prefixes. @param prefixes list of hash prefixes to find @type list of str (Python 2) or list of bytes (Python 3) @param clientState dictionary of client states with keys like (threatType, platformType, threatEntryType) @type dict @return dictionary containing the list of found hashes and the negative cache duration @rtype dict """ requestBody = { "client": { "clientId": self.ClientId, "clientVersion": self.ClientVersion, }, "clientStates": [], "threatInfo": { "threatTypes": [], "platformTypes": [], "threatEntryTypes": [], "threatEntries": [], }, } for prefix in prefixes: requestBody["threatInfo"]["threatEntries"].append( {"hash": base64.b64encode(prefix).decode("ascii")}) for (threatType, platformType, threatEntryType), currentState in (clientState.items()): requestBody["clientStates"].append(currentState) if threatType not in requestBody["threatInfo"]["threatTypes"]: requestBody["threatInfo"]["threatTypes"].append(threatType) if (platformType not in requestBody["threatInfo"]["platformTypes"]): requestBody["threatInfo"]["platformTypes"].append(platformType) if (threatEntryType not in requestBody["threatInfo"]["threatEntryTypes"]): requestBody["threatInfo"]["threatEntryTypes"].append( threatEntryType) data = QByteArray(json.dumps(requestBody).encode("utf-8")) url = QUrl(self.GsbUrlTemplate.format("fullHashes:find", self.__apiKey)) req = QNetworkRequest(url) req.setHeader(QNetworkRequest.ContentTypeHeader, "application/json") reply = WebBrowserWindow.networkManager().post(req, data) while reply.isRunning(): QCoreApplication.processEvents(QEventLoop.AllEvents, 200) # max. 200 ms processing res = [] if reply.error() != QNetworkReply.NoError: self.networkError.emit(reply.errorString()) else: res = self.__extractData(reply) reply.deleteLater() return res