Exemple #1
0
 def _load_tab(self, new_tab, data):
     """Load yaml data into a newly opened tab."""
     entries = []
     for histentry in data['history']:
         user_data = {}
         if 'zoom' in data:
             user_data['zoom'] = data['zoom']
         if 'scroll-pos' in data:
             pos = data['scroll-pos']
             user_data['scroll-pos'] = QPoint(pos['x'], pos['y'])
         active = histentry.get('active', False)
         url = QUrl.fromEncoded(histentry['url'].encode('ascii'))
         if 'original-url' in histentry:
             orig_url = QUrl.fromEncoded(
                 histentry['original-url'].encode('ascii'))
         else:
             orig_url = url
         entry = tabhistory.TabHistoryItem(
             url=url, original_url=orig_url, title=histentry['title'],
             active=active, user_data=user_data)
         entries.append(entry)
         if active:
             new_tab.titleChanged.emit(histentry['title'])
     try:
         new_tab.page().load_history(entries)
     except ValueError as e:
         raise SessionError(e)
Exemple #2
0
 def command(self, command):
     """Perform one command."""
     command = command.split()
     cmd = command[0]
     args = command[1:]
     
     win = app.activeWindow()
     if not win:
         import mainwindow
         win = mainwindow.MainWindow()
         win.show()
     
     if cmd == b'open':
         url = QUrl.fromEncoded(args[0])
         win.openUrl(url, self.encoding)
             
     elif cmd == b'encoding':
         self.encoding = str(args[0])
     elif cmd == b'activate_window':
         win.activateWindow()
         win.raise_()
     elif cmd == b'set_current':
         url = QUrl.fromEncoded(args[0])
         try:
             win.setCurrentDocument(app.openUrl(url)) # already loaded
         except IOError:
             pass
     elif cmd == b'set_cursor':
         line, column = map(int, args)
         cursor = win.textCursor()
         pos = cursor.document().findBlockByNumber(line - 1).position() + column
         cursor.setPosition(pos)
         win.currentView().setTextCursor(cursor)
     elif cmd == b'bye':
         self.close()
Exemple #3
0
 def _load_tab(self, new_tab, data):
     """Load yaml data into a newly opened tab."""
     entries = []
     for histentry in data['history']:
         user_data = {}
         if 'zoom' in data:
             user_data['zoom'] = data['zoom']
         if 'scroll-pos' in data:
             pos = data['scroll-pos']
             user_data['scroll-pos'] = QPoint(pos['x'], pos['y'])
         active = histentry.get('active', False)
         url = QUrl.fromEncoded(histentry['url'].encode('ascii'))
         if 'original-url' in histentry:
             orig_url = QUrl.fromEncoded(
                 histentry['original-url'].encode('ascii'))
         else:
             orig_url = url
         entry = tabhistory.TabHistoryItem(
             url=url, original_url=orig_url, title=histentry['title'],
             active=active, user_data=user_data)
         entries.append(entry)
         if active:
             new_tab.titleChanged.emit(histentry['title'])
     try:
         new_tab.page().load_history(entries)
     except ValueError as e:
         raise SessionError(e)
Exemple #4
0
    def command(self, command):
        """Perform one command."""
        command = command.split()
        cmd = command[0]
        args = command[1:]

        win = app.activeWindow()
        if not win:
            import mainwindow
            win = mainwindow.MainWindow()
            win.show()

        if cmd == b'open':
            url = QUrl.fromEncoded(args[0])
            win.openUrl(url, self.encoding)

        elif cmd == b'encoding':
            self.encoding = str(args[0])
        elif cmd == b'activate_window':
            win.activateWindow()
            win.raise_()
        elif cmd == b'set_current':
            url = QUrl.fromEncoded(args[0])
            try:
                win.setCurrentDocument(app.openUrl(url)) # already loaded
            except IOError:
                pass
        elif cmd == b'set_cursor':
            line, column = map(int, args)
            cursor = win.textCursor()
            pos = cursor.document().findBlockByNumber(line - 1).position() + column
            cursor.setPosition(pos)
            win.currentView().setTextCursor(cursor)
        elif cmd == b'bye':
            self.close()
Exemple #5
0
    def __createPageContextMenu(self, menu):
        """
        Private method to populate the basic context menu.

        @param menu reference to the menu to be populated
        @type QMenu
        """
        ##
        menu.addSeparator()
        menu.addAction(
            self.tr("Open native web"),
            lambda: QDesktopServices.openUrl(self.url())
        )
        ##
        language = QLocale.system().name()
        if not language:
            languages = []
        else:
            languages = MyEngineView.expand(QLocale(language).language())
        if languages:
            menu.addSeparator()
            language = languages[0]
            langCode = language.split("[")[1][:2]
            googleTranslatorUrl = QUrl.fromEncoded(
                b"http://translate.google.com/translate?sl=auto&tl=" +
                langCode.encode() +
                b"&u=" +
                QUrl.toPercentEncoding(bytes(self.url().toEncoded()).decode()))
            menu.addAction(
                self.tr("Google Translate"),
                lambda :self.load(googleTranslatorUrl)
                           )#TODO: maybe has google bug blank.
 def location(self):
     """
     Public method to get the subscription location.
     
     @return URL of the subscription location (QUrl)
     """
     return QUrl.fromEncoded(self.__location)
    def suggestionsUrl(self, searchTerm):
        """
        Public method to get a URL ready for suggestions.
        
        @param searchTerm term to search for (string)
        @return URL (QUrl)
        """
        if not self._suggestionsUrlTemplate:
            return QUrl()

        ret = QUrl.fromEncoded(
            QByteArray(
                self.parseTemplate(
                    searchTerm, self._suggestionsUrlTemplate).encode("utf-8")))

        if self.__searchMethod != "post":
            if qVersion() >= "5.0.0":
                from PyQt5.QtCore import QUrlQuery
                urlQuery = QUrlQuery(ret)
                for parameter in self._suggestionsParameters:
                    urlQuery.addQueryItem(
                        parameter[0],
                        self.parseTemplate(searchTerm, parameter[1]))
                ret.setQuery(urlQuery)
            else:
                for parameter in self._suggestionsParameters:
                    ret.addQueryItem(
                        parameter[0],
                        self.parseTemplate(searchTerm, parameter[1]))

        return ret
Exemple #8
0
 def suggestionsUrl(self, searchTerm):
     """
     Public method to get a URL ready for suggestions.
     
     @param searchTerm term to search for (string)
     @return URL (QUrl)
     """
     if not self._suggestionsUrlTemplate:
         return QUrl()
     
     ret = QUrl.fromEncoded(QByteArray(self.parseTemplate(
         searchTerm, self._suggestionsUrlTemplate).encode("utf-8")))
     
     if self.__searchMethod != "post":
         if qVersion() >= "5.0.0":
             from PyQt5.QtCore import QUrlQuery
             urlQuery = QUrlQuery(ret)
             for parameter in self._suggestionsParameters:
                 urlQuery.addQueryItem(
                     parameter[0],
                     self.parseTemplate(searchTerm, parameter[1]))
             ret.setQuery(urlQuery)
         else:
             for parameter in self._suggestionsParameters:
                 ret.addQueryItem(
                     parameter[0],
                     self.parseTemplate(searchTerm, parameter[1]))
     
     return ret
Exemple #9
0
 def location(self):
     """
     Public method to get the subscription location.
     
     @return URL of the subscription location (QUrl)
     """
     return QUrl.fromEncoded(self.__location)
Exemple #10
0
 def __guessUrlFromPath(self, path):
     """
     Private method to guess an URL given a path string.
     
     @param path path string to guess an URL for (string)
     @return guessed URL (QUrl)
     """
     manager = self.__mainWindow.openSearchManager()
     path = Utilities.fromNativeSeparators(path)
     url = manager.convertKeywordSearchToUrl(path)
     if url.isValid():
         return url
     
     try:
         url = QUrl.fromUserInput(path)
     except AttributeError:
         url = QUrl(path)
     
     if url.scheme() == "about" and \
        url.path() == "home":
         url = QUrl("eric:home")
     
     if url.scheme() in ["s", "search"]:
         url = manager.currentEngine().searchUrl(url.path().strip())
     
     if url.scheme() != "" and \
        (url.host() != "" or url.path() != ""):
         return url
     
     urlString = Preferences.getHelp("DefaultScheme") + path.strip()
     url = QUrl.fromEncoded(urlString.encode("utf-8"), QUrl.TolerantMode)
     
     return url
Exemple #11
0
    def __guessUrlFromPath(self, path):
        """
        Private method to guess an URL given a path string.
        
        @param path path string to guess an URL for (string)
        @return guessed URL (QUrl)
        """
        manager = self.__mainWindow.openSearchManager()
        path = Utilities.fromNativeSeparators(path)
        url = manager.convertKeywordSearchToUrl(path)
        if url.isValid():
            return url

        try:
            url = QUrl.fromUserInput(path)
        except AttributeError:
            url = QUrl(path)

        if url.scheme() == "about" and \
           url.path() == "home":
            url = QUrl("eric:home")

        if url.scheme() in ["s", "search"]:
            url = manager.currentEngine().searchUrl(url.path().strip())

        if url.scheme() != "" and \
           (url.host() != "" or url.path() != ""):
            return url

        urlString = Preferences.getHelp("DefaultScheme") + path.strip()
        url = QUrl.fromEncoded(urlString.encode("utf-8"), QUrl.TolerantMode)

        return url
Exemple #12
0
    def _navigate_incdec(self, url, incdec, tab, background, window):
        """Helper method for :navigate when `where' is increment/decrement.

        Args:
            url: The current url.
            incdec: Either 'increment' or 'decrement'.
            tab: Whether to open the link in a new tab.
            background: Open the link in a new background tab.
            window: Open the link in a new window.
        """
        encoded = bytes(url.toEncoded()).decode('ascii')
        # Get the last number in a string
        match = re.match(r'(.*\D|^)(\d+)(.*)', encoded)
        if not match:
            raise cmdexc.CommandError("No number found in URL!")
        pre, number, post = match.groups()
        if not number:
            raise cmdexc.CommandError("No number found in URL!")
        try:
            val = int(number)
        except ValueError:
            raise cmdexc.CommandError(
                "Could not parse number '{}'.".format(number))
        if incdec == 'decrement':
            if val <= 0:
                raise cmdexc.CommandError("Can't decrement {}!".format(val))
            val -= 1
        elif incdec == 'increment':
            val += 1
        else:
            raise ValueError("Invalid value {} for indec!".format(incdec))
        urlstr = ''.join([pre, str(val), post]).encode('ascii')
        new_url = QUrl.fromEncoded(urlstr)
        self._open(new_url, tab, background, window)
Exemple #13
0
    def _navigate_incdec(self, url, incdec, tab, background, window):
        """Helper method for :navigate when `where' is increment/decrement.

        Args:
            url: The current url.
            incdec: Either 'increment' or 'decrement'.
            tab: Whether to open the link in a new tab.
            background: Open the link in a new background tab.
            window: Open the link in a new window.
        """
        encoded = bytes(url.toEncoded()).decode('ascii')
        # Get the last number in a string
        match = re.match(r'(.*\D|^)(\d+)(.*)', encoded)
        if not match:
            raise cmdexc.CommandError("No number found in URL!")
        pre, number, post = match.groups()
        if not number:
            raise cmdexc.CommandError("No number found in URL!")
        try:
            val = int(number)
        except ValueError:
            raise cmdexc.CommandError("Could not parse number '{}'.".format(
                number))
        if incdec == 'decrement':
            if val <= 0:
                raise cmdexc.CommandError("Can't decrement {}!".format(val))
            val -= 1
        elif incdec == 'increment':
            val += 1
        else:
            raise ValueError("Invalid value {} for indec!".format(incdec))
        urlstr = ''.join([pre, str(val), post]).encode('ascii')
        new_url = QUrl.fromEncoded(urlstr)
        self._open(new_url, tab, background, window)
    def _load_tab(self, new_tab, data):
        """Load yaml data into a newly opened tab."""
        entries = []
        for histentry in data['history']:
            user_data = {}

            if 'zoom' in data:
                # The zoom was accidentally stored in 'data' instead of per-tab
                # earlier.
                # See https://github.com/qutebrowser/qutebrowser/issues/728
                user_data['zoom'] = data['zoom']
            elif 'zoom' in histentry:
                user_data['zoom'] = histentry['zoom']

            if 'scroll-pos' in data:
                # The scroll position was accidentally stored in 'data' instead
                # of per-tab earlier.
                # See https://github.com/qutebrowser/qutebrowser/issues/728
                pos = data['scroll-pos']
                user_data['scroll-pos'] = QPoint(pos['x'], pos['y'])
            elif 'scroll-pos' in histentry:
                pos = histentry['scroll-pos']
                user_data['scroll-pos'] = QPoint(pos['x'], pos['y'])

            if 'pinned' in histentry:
                new_tab.data.pinned = histentry['pinned']

            active = histentry.get('active', False)
            url = QUrl.fromEncoded(histentry['url'].encode('ascii'))
            if 'original-url' in histentry:
                orig_url = QUrl.fromEncoded(
                    histentry['original-url'].encode('ascii'))
            else:
                orig_url = url
            entry = TabHistoryItem(url=url,
                                   original_url=orig_url,
                                   title=histentry['title'],
                                   active=active,
                                   user_data=user_data)
            entries.append(entry)
            if active:
                new_tab.title_changed.emit(histentry['title'])
        try:
            new_tab.history.load_items(entries)
        except ValueError as e:
            raise SessionError(e)
Exemple #15
0
    def _load_tab(self, new_tab, data):
        """Load yaml data into a newly opened tab."""
        entries = []
        for histentry in data['history']:
            user_data = {}

            if 'zoom' in data:
                # The zoom was accidentally stored in 'data' instead of per-tab
                # earlier.
                # See https://github.com/qutebrowser/qutebrowser/issues/728
                user_data['zoom'] = data['zoom']
            elif 'zoom' in histentry:
                user_data['zoom'] = histentry['zoom']

            if 'scroll-pos' in data:
                # The scroll position was accidentally stored in 'data' instead
                # of per-tab earlier.
                # See https://github.com/qutebrowser/qutebrowser/issues/728
                pos = data['scroll-pos']
                user_data['scroll-pos'] = QPoint(pos['x'], pos['y'])
            elif 'scroll-pos' in histentry:
                pos = histentry['scroll-pos']
                user_data['scroll-pos'] = QPoint(pos['x'], pos['y'])

            if 'pinned' in histentry:
                new_tab.data.pinned = histentry['pinned']

            active = histentry.get('active', False)
            url = QUrl.fromEncoded(histentry['url'].encode('ascii'))
            if 'original-url' in histentry:
                orig_url = QUrl.fromEncoded(
                    histentry['original-url'].encode('ascii'))
            else:
                orig_url = url
            entry = TabHistoryItem(url=url, original_url=orig_url,
                                   title=histentry['title'], active=active,
                                   user_data=user_data)
            entries.append(entry)
            if active:
                new_tab.title_changed.emit(histentry['title'])
        try:
            new_tab.history.load_items(entries)
        except ValueError as e:
            raise SessionError(e)
Exemple #16
0
    def urlForShortcut(self, key):
        """
        Public method to get the URL for the given shortcut key.
        
        @param key shortcut key (integer)
        @return URL for the key (QUrl)
        """
        if key < 0 or len(self.__webPages) <= key:
            return QUrl()

        return QUrl.fromEncoded(self.__webPages[key].url.encode("utf-8"))
Exemple #17
0
 def loadImage(self):
     """
     Public method to load the image of the engine.
     """
     if self.__networkAccessManager is None or not self._imageUrl:
         return
     
     reply = self.__networkAccessManager.get(
         QNetworkRequest(QUrl.fromEncoded(self._imageUrl.encode("utf-8"))))
     reply.finished.connect(self.__imageObtained)
     self.__replies.append(reply)
Exemple #18
0
 def urlForShortcut(self, key):
     """
     Public method to get the URL for the given shortcut key.
     
     @param key shortcut key (integer)
     @return URL for the key (QUrl)
     """
     if key < 0 or len(self.__webPages) <= key:
         return QUrl()
     
     return QUrl.fromEncoded(self.__webPages[key].url.encode("utf-8"))
    def loadImage(self):
        """
        Public method to load the image of the engine.
        """
        if self.__networkAccessManager is None or not self._imageUrl:
            return

        reply = self.__networkAccessManager.get(
            QNetworkRequest(QUrl.fromEncoded(self._imageUrl.encode("utf-8"))))
        reply.finished.connect(self.__imageObtained)
        self.__replies.append(reply)
Exemple #20
0
def parse_gnome_copied_files(data: bytes) -> Tuple[Qt.DropAction, List[QUrl]]:
    lines = data.split(b'\n')

    if lines[0] == "copy":
        action = Qt.CopyAction
    elif lines[0] == "cut":
        action = Qt.MoveAction
    else:
        raise Exception("unknown action: {}".format(lines[0]))

    urls = [QUrl.fromEncoded(d) for d in lines[1:]]

    return action, urls
Exemple #21
0
def parse_gnome_copied_files(data: bytes) -> Tuple[Qt.DropAction, List[QUrl]]:
    lines = data.split(b'\n')

    if lines[0] == "copy":
        action = Qt.CopyAction
    elif lines[0] == "cut":
        action = Qt.MoveAction
    else:
        raise Exception("unknown action: {}".format(lines[0]))

    urls = [QUrl.fromEncoded(d) for d in lines[1:]]

    return action, urls
Exemple #22
0
    def loadThumbnail(self, url, loadTitle=False):
        """
        Public slot to load a thumbnail of the given URL.
        
        @param url URL of the thumbnail (string)
        @param loadTitle flag indicating to get the title for the thumbnail
            from the site (boolean)
        """
        if not url:
            return

        from .PageThumbnailer import PageThumbnailer
        thumbnailer = PageThumbnailer(self)
        thumbnailer.setUrl(QUrl.fromEncoded(url.encode("utf-8")))
        thumbnailer.setLoadTitle(loadTitle)
        thumbnailer.thumbnailCreated.connect(self.__thumbnailCreated)
        self.__thumbnailers.append(thumbnailer)

        thumbnailer.start()
Exemple #23
0
 def loadThumbnail(self, url, loadTitle=False):
     """
     Public slot to load a thumbnail of the given URL.
     
     @param url URL of the thumbnail (string)
     @param loadTitle flag indicating to get the title for the thumbnail
         from the site (boolean)
     """
     if not url:
         return
     
     from .PageThumbnailer import PageThumbnailer
     thumbnailer = PageThumbnailer(self)
     thumbnailer.setUrl(QUrl.fromEncoded(url.encode("utf-8")))
     thumbnailer.setLoadTitle(loadTitle)
     thumbnailer.thumbnailCreated.connect(self.__thumbnailCreated)
     self.__thumbnailers.append(thumbnailer)
     
     thumbnailer.start()
Exemple #24
0
    def loadRequiredSubscription(self, location, title):
        """
        Public method to load a subscription required by another one.
        
        @param location location of the required subscription (string)
        @param title title of the required subscription (string)
        """
        # Step 1: check, if the subscription is in the list of subscriptions
        urlString = "abp:subscribe?location={0}&title={1}".format(
            location, title)
        for subscription in self.__subscriptions:
            if subscription.url().toString().startswith(urlString):
                # We found it!
                return

        # Step 2: if it is not, get it
        url = QUrl.fromEncoded(urlString.encode("utf-8"))
        adBlockSubscription = AdBlockSubscription(url, False, self)
        self.addSubscription(adBlockSubscription)
        self.requiredSubscriptionLoaded.emit(adBlockSubscription)
Exemple #25
0
 def loadRequiredSubscription(self, location, title):
     """
     Public method to load a subscription required by another one.
     
     @param location location of the required subscription (string)
     @param title title of the required subscription (string)
     """
     # Step 1: check, if the subscription is in the list of subscriptions
     urlString = "abp:subscribe?location={0}&title={1}".format(
         location, title)
     for subscription in self.__subscriptions:
         if subscription.url().toString().startswith(urlString):
             # We found it!
             return
     
     # Step 2: if it is not, get it
     url = QUrl.fromEncoded(urlString.encode("utf-8"))
     adBlockSubscription = AdBlockSubscription(url, False, self)
     self.addSubscription(adBlockSubscription)
     self.requiredSubscriptionLoaded.emit(adBlockSubscription)
Exemple #26
0
 def dropEvent(self, evt):
     """
     Protected method to handle drop events.
     
     @param evt reference to the drop event (QDropEvent)
     """
     mimeData = evt.mimeData()
     
     url = QUrl()
     if mimeData.hasUrls():
         url = mimeData.urls()[0]
     elif mimeData.hasText():
         url = QUrl.fromEncoded(mimeData.text().encode(), QUrl.TolerantMode)
     
     if url.isEmpty() or not url.isValid():
         E5LineEdit.dropEvent(self, evt)
         return
     
     self.setText(str(url.toEncoded(), encoding="utf-8"))
     self.selectAll()
     
     evt.acceptProposedAction()
Exemple #27
0
    def searchUrl(self, searchTerm):
        """
        Public method to get a URL ready for searching.
        
        @param searchTerm term to search for (string)
        @return URL (QUrl)
        """
        if not self._searchUrlTemplate:
            return QUrl()

        ret = QUrl.fromEncoded(
            self.parseTemplate(searchTerm,
                               self._searchUrlTemplate).encode("utf-8"))

        if self.__searchMethod != "post":
            urlQuery = QUrlQuery(ret)
            for parameter in self._searchParameters:
                urlQuery.addQueryItem(
                    parameter[0], self.parseTemplate(searchTerm, parameter[1]))
            ret.setQuery(urlQuery)

        return ret
Exemple #28
0
    def __loadSubscriptions(self):
        """
        Private method to load the set of subscriptions.
        """
        if self.__subscriptionsLoaded:
            return

        subscriptions = Preferences.getHelp("AdBlockSubscriptions")
        if subscriptions:
            for subscription in subscriptions:
                if subscription.startswith(
                        self.__defaultSubscriptionUrlString):
                    break
            else:
                subscriptions.insert(0, self.__defaultSubscriptionUrlString)
            for subscription in subscriptions:
                if subscription.startswith(self.__customSubscriptionUrlString):
                    break
            else:
                subscriptions.append(self.__customSubscriptionUrlString)
        else:
            subscriptions = [
                self.__defaultSubscriptionUrlString,
                self.__customSubscriptionUrlString
            ]
        for subscription in subscriptions:
            url = QUrl.fromEncoded(subscription.encode("utf-8"))
            adBlockSubscription = AdBlockSubscription(
                url,
                subscription.startswith(self.__customSubscriptionUrlString),
                self,
                subscription.startswith(self.__defaultSubscriptionUrlString))
            adBlockSubscription.rulesChanged.connect(self.rulesChanged)
            adBlockSubscription.changed.connect(self.rulesChanged)
            adBlockSubscription.enabledChanged.connect(self.rulesChanged)
            self.__subscriptions.append(adBlockSubscription)

        self.__subscriptionsLoaded = True
Exemple #29
0
    def dropEvent(self, evt):
        """
        Protected method to handle drop events.
        
        @param evt reference to the drop event (QDropEvent)
        """
        mimeData = evt.mimeData()

        url = QUrl()
        if mimeData.hasUrls():
            url = mimeData.urls()[0]
        elif mimeData.hasText():
            url = QUrl.fromEncoded(mimeData.text().encode("utf-8"),
                                   QUrl.TolerantMode)

        if url.isEmpty() or not url.isValid():
            E5LineEdit.dropEvent(self, evt)
            return

        self.setText(str(url.toEncoded(), encoding="utf-8"))
        self.selectAll()

        evt.acceptProposedAction()
Exemple #30
0
 def block(self, request):
     """
     Public method to check for a network block.
     
     @param request reference to the request object (QNetworkRequest)
     @return reply object (QNetworkReply) or None
     """
     url = request.url()
     urlString = bytes(url.toEncoded()).decode()
     urlDomain = url.host()
     urlScheme = url.scheme()
     refererHost = QUrl.fromEncoded(request.rawHeader(b"Referer")).host()
     
     import Helpviewer.HelpWindow
     manager = Helpviewer.HelpWindow.HelpWindow.adBlockManager()
     if not manager.isEnabled() or \
        not self.canRunOnScheme(urlScheme) or \
        manager.isHostExcepted(urlDomain) or \
        manager.isHostExcepted(refererHost):
         return None
     
     for subscription in manager.subscriptions():
         if subscription.isEnabled():
             blockedRule = subscription.match(request, urlDomain, urlString)
             if blockedRule:
                 webPage = request.attribute(QNetworkRequest.User + 100)
                 if webPage is not None:
                     if not self.__canBeBlocked(webPage.url()):
                         return None
                     
                     webPage.addAdBlockRule(blockedRule, url)
                 
                 reply = AdBlockBlockedNetworkReply(
                     request, subscription, blockedRule, self)
                 return reply
     
     return None
    def __loadSubscriptions(self):
        """
        Private method to load the set of subscriptions.
        """
        if self.__subscriptionsLoaded:
            return

        subscriptions = Preferences.getWebBrowser("AdBlockSubscriptions")
        if subscriptions:
            for subscription in subscriptions:
                if subscription.startswith(self.__customSubscriptionUrlString):
                    break
            else:
                subscriptions.append(self.__customSubscriptionUrlString)
        else:
            subscriptions = ([self.__defaultSubscriptionUrlString] +
                             self.__additionalDefaultSubscriptionUrlStrings +
                             [self.__customSubscriptionUrlString])
        for subscription in subscriptions:
            url = QUrl.fromEncoded(subscription.encode("utf-8"))
            adBlockSubscription = AdBlockSubscription(
                url,
                subscription.startswith(self.__customSubscriptionUrlString),
                self,
                subscription.startswith(self.__defaultSubscriptionUrlString))
            adBlockSubscription.rulesChanged.connect(self.rulesChanged)
            adBlockSubscription.changed.connect(self.rulesChanged)
            adBlockSubscription.enabledChanged.connect(self.rulesChanged)
            adBlockSubscription.rulesEnabledChanged.connect(
                self.__updateMatcher)
            adBlockSubscription.rulesEnabledChanged.connect(
                self.__saveTimer.changeOccurred)
            self.__subscriptions.append(adBlockSubscription)

        self.__subscriptionsLoaded = True

        self.__updateMatcher()
    def block(self, request):
        """
        Public method to check for a network block.
        
        @param request reference to the request object (QNetworkRequest)
        @return reply object (QNetworkReply) or None
        """
        url = request.url()
        urlString = bytes(url.toEncoded()).decode()
        urlDomain = url.host()
        urlScheme = url.scheme()
        refererHost = QUrl.fromEncoded(request.rawHeader("Referer")).host()

        import Helpviewer.HelpWindow
        manager = Helpviewer.HelpWindow.HelpWindow.adBlockManager()
        if not manager.isEnabled() or \
           not self.canRunOnScheme(urlScheme) or \
           manager.isHostExcepted(urlDomain) or \
           manager.isHostExcepted(refererHost):
            return None

        for subscription in manager.subscriptions():
            if subscription.isEnabled():
                blockedRule = subscription.match(request, urlDomain, urlString)
                if blockedRule:
                    webPage = request.attribute(QNetworkRequest.User + 100)
                    if webPage is not None:
                        if not self.__canBeBlocked(webPage.url()):
                            return None

                        webPage.addAdBlockRule(blockedRule, url)

                    reply = AdBlockBlockedNetworkReply(request, subscription,
                                                       blockedRule, self)
                    return reply

        return None
Exemple #33
0
 def __loadSubscriptions(self):
     """
     Private method to load the set of subscriptions.
     """
     if self.__subscriptionsLoaded:
         return
     
     subscriptions = Preferences.getHelp("AdBlockSubscriptions")
     if subscriptions:
         for subscription in subscriptions:
             if subscription.startswith(
                     self.__defaultSubscriptionUrlString):
                 break
         else:
             subscriptions.insert(0, self.__defaultSubscriptionUrlString)
         for subscription in subscriptions:
             if subscription.startswith(self.__customSubscriptionUrlString):
                 break
         else:
             subscriptions.append(self.__customSubscriptionUrlString)
     else:
         subscriptions = [self.__defaultSubscriptionUrlString,
                          self.__customSubscriptionUrlString]
     for subscription in subscriptions:
         url = QUrl.fromEncoded(subscription.encode("utf-8"))
         adBlockSubscription = AdBlockSubscription(
             url,
             subscription.startswith(self.__customSubscriptionUrlString),
             self,
             subscription.startswith(self.__defaultSubscriptionUrlString))
         adBlockSubscription.rulesChanged.connect(self.rulesChanged)
         adBlockSubscription.changed.connect(self.rulesChanged)
         adBlockSubscription.enabledChanged.connect(self.rulesChanged)
         self.__subscriptions.append(adBlockSubscription)
     
     self.__subscriptionsLoaded = True
Exemple #34
0
    def _load_tab(self, new_tab, data):  # noqa: C901
        """Load yaml data into a newly opened tab."""
        entries = []
        lazy_load = []  # type: typing.MutableSequence[_JsonType]
        # use len(data['history'])
        # -> dropwhile empty if not session.lazy_session
        lazy_index = len(data['history'])
        gen = itertools.chain(
            itertools.takewhile(lambda _: not lazy_load,
                                enumerate(data['history'])),
            enumerate(lazy_load),
            itertools.dropwhile(lambda i: i[0] < lazy_index,
                                enumerate(data['history'])))

        for i, histentry in gen:
            user_data = {}

            if 'zoom' in data:
                # The zoom was accidentally stored in 'data' instead of per-tab
                # earlier.
                # See https://github.com/qutebrowser/qutebrowser/issues/728
                user_data['zoom'] = data['zoom']
            elif 'zoom' in histentry:
                user_data['zoom'] = histentry['zoom']

            if 'scroll-pos' in data:
                # The scroll position was accidentally stored in 'data' instead
                # of per-tab earlier.
                # See https://github.com/qutebrowser/qutebrowser/issues/728
                pos = data['scroll-pos']
                user_data['scroll-pos'] = QPoint(pos['x'], pos['y'])
            elif 'scroll-pos' in histentry:
                pos = histentry['scroll-pos']
                user_data['scroll-pos'] = QPoint(pos['x'], pos['y'])

            if 'pinned' in histentry:
                new_tab.data.pinned = histentry['pinned']

            if (config.val.session.lazy_restore
                    and histentry.get('active', False)
                    and not histentry['url'].startswith('qute://back')):
                # remove "active" mark and insert back page marked as active
                lazy_index = i + 1
                lazy_load.append({
                    'title':
                    histentry['title'],
                    'url':
                    'qute://back#' + urllib.parse.quote(histentry['title']),
                    'active':
                    True
                })
                histentry['active'] = False

            active = histentry.get('active', False)
            url = QUrl.fromEncoded(histentry['url'].encode('ascii'))

            if 'original-url' in histentry:
                orig_url = QUrl.fromEncoded(
                    histentry['original-url'].encode('ascii'))
            else:
                orig_url = url

            if histentry.get("last_visited"):
                last_visited = QDateTime.fromString(
                    histentry.get("last_visited"),
                    Qt.ISODate,
                )  # type: typing.Optional[QDateTime]
            else:
                last_visited = None

            entry = TabHistoryItem(url=url,
                                   original_url=orig_url,
                                   title=histentry['title'],
                                   active=active,
                                   user_data=user_data,
                                   last_visited=last_visited)
            entries.append(entry)
            if active:
                new_tab.title_changed.emit(histentry['title'])

        try:
            new_tab.history.private_api.load_items(entries)
        except ValueError as e:
            raise SessionError(e)
 def __findForm(self, webPage, data, boundary=None):
     """
     Private method to find the form used for logging in.
     
     @param webPage reference to the web page (QWebPage)
     @param data data to be sent (QByteArray)
     @keyparam boundary boundary string (QByteArray) for multipart
         encoded data, None for urlencoded data
     @return parsed form (LoginForm)
     """
     from .LoginForm import LoginForm
     form = LoginForm()
     if boundary is not None:
         args = self.__extractMultipartQueryItems(data, boundary)
     else:
         if qVersion() >= "5.0.0":
             from PyQt5.QtCore import QUrlQuery
             argsUrl = QUrl.fromEncoded(
                 QByteArray(b"foo://bar.com/?" + QUrl.fromPercentEncoding(
                     data.replace(b"+", b"%20")).encode("utf-8")))
             encodedArgs = QUrlQuery(argsUrl).queryItems()
         else:
             argsUrl = QUrl.fromEncoded(
                 QByteArray(b"foo://bar.com/?" + data.replace(b"+", b"%20"))
             )
             encodedArgs = argsUrl.queryItems()
         args = set()
         for arg in encodedArgs:
             key = arg[0]
             value = arg[1]
             args.add((key, value))
     
     # extract the forms
     from Helpviewer.JavaScriptResources import parseForms_js
     lst = webPage.mainFrame().evaluateJavaScript(parseForms_js)
     for map in lst:
         formHasPasswords = False
         formName = map["name"]
         formIndex = map["index"]
         if isinstance(formIndex, float) and formIndex.is_integer():
             formIndex = int(formIndex)
         elements = map["elements"]
         formElements = set()
         formElementTypes = {}
         deadElements = set()
         for elementMap in elements:
             try:
                 name = elementMap["name"]
                 value = elementMap["value"]
                 type_ = elementMap["type"]
             except KeyError:
                 continue
             if type_ == "password":
                 formHasPasswords = True
             t = (name, value)
             try:
                 if elementMap["autocomplete"] == "off":
                     deadElements.add(t)
             except KeyError:
                 pass
             if name:
                 formElements.add(t)
                 formElementTypes[name] = type_
         if formElements.intersection(args) == args:
             form.hasAPassword = formHasPasswords
             if not formName:
                 form.name = formIndex
             else:
                 form.name = formName
             args.difference_update(deadElements)
             for elt in deadElements:
                 if elt[0] in formElementTypes:
                     del formElementTypes[elt[0]]
             form.elements = list(args)
             form.elementTypes = formElementTypes
             break
     
     return form
 def post(self, request, data):
     """
     Public method to check, if the data to be sent contains login data.
     
     @param request reference to the network request (QNetworkRequest)
     @param data data to be sent (QByteArray)
     """
     # shall passwords be saved?
     if not Preferences.getUser("SavePasswords"):
         return
     
     # observe privacy
     if QWebSettings.globalSettings().testAttribute(
             QWebSettings.PrivateBrowsingEnabled):
         return
     
     if not self.__loaded:
         self.__load()
     
     # determine the url
     refererHeader = request.rawHeader(b"Referer")
     if refererHeader.isEmpty():
         return
     url = QUrl.fromEncoded(refererHeader)
     url = self.__stripUrl(url)
     
     # check that url isn't in __never
     if url.toString() in self.__never:
         return
     
     # check the request type
     navType = request.attribute(QNetworkRequest.User + 101)
     if navType is None:
         return
     if navType != QWebPage.NavigationTypeFormSubmitted:
         return
     
     # determine the QWebPage
     webPage = request.attribute(QNetworkRequest.User + 100)
     if webPage is None:
         return
     
     # determine the requests content type
     contentTypeHeader = request.rawHeader(b"Content-Type")
     if contentTypeHeader.isEmpty():
         return
     multipart = contentTypeHeader.startsWith(b"multipart/form-data")
     if multipart:
         boundary = contentTypeHeader.split(" ")[1].split("=")[1]
     else:
         boundary = None
     
     # find the matching form on the web page
     form = self.__findForm(webPage, data, boundary=boundary)
     if not form.isValid():
         return
     form.url = QUrl(url)
     
     # check, if the form has a password
     if not form.hasAPassword:
         return
     
     # prompt, if the form has never be seen
     key = self.__createKey(url, "")
     if key not in self.__loginForms:
         mb = E5MessageBox.E5MessageBox(
             E5MessageBox.Question,
             self.tr("Save password"),
             self.tr(
                 """<b>Would you like to save this password?</b><br/>"""
                 """To review passwords you have saved and remove them, """
                 """use the password management dialog of the Settings"""
                 """ menu."""),
             modal=True)
         neverButton = mb.addButton(
             self.tr("Never for this site"),
             E5MessageBox.DestructiveRole)
         noButton = mb.addButton(
             self.tr("Not now"), E5MessageBox.RejectRole)
         mb.addButton(E5MessageBox.Yes)
         mb.exec_()
         if mb.clickedButton() == neverButton:
             self.__never.append(url.toString())
             return
         elif mb.clickedButton() == noButton:
             return
     
     # extract user name and password
     user = ""
     password = ""
     for index in range(len(form.elements)):
         element = form.elements[index]
         type_ = form.elementTypes[element[0]]
         if user == "" and \
            type_ == "text":
             user = element[1]
         elif password == "" and \
                 type_ == "password":
             password = element[1]
             form.elements[index] = (element[0], "--PASSWORD--")
     if user and password:
         self.__logins[key] = \
             (user, Utilities.crypto.pwConvert(password, encode=True))
         self.__loginForms[key] = form
         self.changed.emit()
Exemple #37
0
 def load_url(self, url):
     self.url = QUrl.fromEncoded(url)
     self.web.setUrl(self.url)
Exemple #38
0
def to_qurl(s):
    if isinstance(s, QUrl):
        return s
    return QUrl.fromEncoded(to_bytes(s, encoding='utf8'))
Exemple #39
0
    def _load_tab(self, new_tab, data):
        """Load yaml data into a newly opened tab."""
        entries = []
        lazy_load = []
        # use len(data['history'])
        # -> dropwhile empty if not session.lazy_session
        lazy_index = len(data['history'])
        gen = itertools.chain(
            itertools.takewhile(lambda _: not lazy_load,
                                enumerate(data['history'])),
            enumerate(lazy_load),
            itertools.dropwhile(lambda i: i[0] < lazy_index,
                                enumerate(data['history'])))

        for i, histentry in gen:
            user_data = {}

            if 'zoom' in data:
                # The zoom was accidentally stored in 'data' instead of per-tab
                # earlier.
                # See https://github.com/qutebrowser/qutebrowser/issues/728
                user_data['zoom'] = data['zoom']
            elif 'zoom' in histentry:
                user_data['zoom'] = histentry['zoom']

            if 'scroll-pos' in data:
                # The scroll position was accidentally stored in 'data' instead
                # of per-tab earlier.
                # See https://github.com/qutebrowser/qutebrowser/issues/728
                pos = data['scroll-pos']
                user_data['scroll-pos'] = QPoint(pos['x'], pos['y'])
            elif 'scroll-pos' in histentry:
                pos = histentry['scroll-pos']
                user_data['scroll-pos'] = QPoint(pos['x'], pos['y'])

            if 'pinned' in histentry:
                new_tab.data.pinned = histentry['pinned']

            if (config.val.session.lazy_restore and
                    histentry.get('active', False) and
                    not histentry['url'].startswith('qute://back')):
                # remove "active" mark and insert back page marked as active
                lazy_index = i + 1
                lazy_load.append({
                    'title': histentry['title'],
                    'url':
                        'qute://back#' +
                        urllib.parse.quote(histentry['title']),
                    'active': True
                })
                histentry['active'] = False

            active = histentry.get('active', False)
            url = QUrl.fromEncoded(histentry['url'].encode('ascii'))
            if 'original-url' in histentry:
                orig_url = QUrl.fromEncoded(
                    histentry['original-url'].encode('ascii'))
            else:
                orig_url = url
            entry = TabHistoryItem(url=url, original_url=orig_url,
                                   title=histentry['title'], active=active,
                                   user_data=user_data)
            entries.append(entry)
            if active:
                new_tab.title_changed.emit(histentry['title'])

        try:
            new_tab.history.load_items(entries)
        except ValueError as e:
            raise SessionError(e)
    def process_request(self):
        socket = self.sender()
        request_data = socket.readAll()

        if not self.authorize_request(request_data):
            socket.write('HTTP/1.1 407 Proxy Authentication Required\r\n')
            if self.debug:
                self.log.write('407 Proxy Authentication Required\n\n')
            socket.write('Proxy-Authenticate: Basic realm="test"\r\n')
            socket.write('\r\n')
            socket.disconnectFromHost()
            return
        else:
            # remove Proxy-Authorization header
            start = request_data.indexOf('Proxy-Authorization:')
            end = request_data.lastIndexOf('\r\n')
            request_data.remove(start, end)
            request_data.append('\r\n')

        pos = request_data.indexOf('\r\n')
        request_line = request_data.left(pos)
        request_data.remove(0, pos + 2)

        entries = request_line.split(' ')
        method = entries[0]
        address = entries[1]
        version = entries[2]
        port = '80'

        if address.count(':') > 1:
            protocol, host, port = address.split(':')
        else:
            protocol, host = address.split(':')

        print('address' + str(address))

        #url = QUrl( protocol + host )
        url = QUrl.fromEncoded(address)
        #url.setHost( host )
        #url.setPort( int(port) )

        if not url.isValid():
            if self.debug:
                self.log.write('Invalid URL: %s\n\n', url)
            socket.disconnectFromHost()
            return

        host = url.host()
        port = 80 if (url.port() < 0) else url.port()
        req = url.encodedPath()
        if url.hasQuery():
            req.append('?').append(url.encodedQuery())
        request_line = method + ' ' + req + ' ' + version + '\r\n'
        request_data.prepend(request_line)

        if self.debug:
            self.log.write(method + ' ' + address + ' ' + version + '\n\n')

        key = host + ':' + QString.number(port)
        proxy_socket = socket.findChild(QTcpSocket, key)
        if proxy_socket:
            proxy_socket.setObjectName(key)
            proxy_socket.setProperty('url', url)
            proxy_socket.setProperty('request_data', request_data)
            proxy_socket.write(request_data)
        else:
            proxy_socket = QTcpSocket(socket)
            proxy_socket.setObjectName(key)
            proxy_socket.setProperty('url', url)
            proxy_socket.setProperty('request_data', request_data)
            proxy_socket.connected.connect(self.send_request)
            proxy_socket.readyRead.connect(self.transfer_data)
            proxy_socket.disconnected.connect(self.close_connection)
            proxy_socket.error.connect(self.close_connection)
            proxy_socket.connectToHost(host, port)
Exemple #41
0
def to_qurl(s):
    if isinstance(s, QUrl):
        return s
    return QUrl.fromEncoded(to_bytes(s, encoding='utf8'))
Exemple #42
0
 def load_url(self,url):
     self.url=QUrl.fromEncoded(url)
     self.web.setUrl(self.url)