Exemple #1
0
 def _map_to_signal(self, url: QUrl):
     """
     This slot delegates clicked link signal to show the same html as
     before (eg. link to a point in the term has been clicked)
     or to delegates the filename given in the link as emitted
     signal_link_clicked(filename) signal.
     """
     if url.fileName() == "" and url.path() == "/":
         logging.debug(str(url))
         self.ui.contentWebView.setHtml(self._html, url)
     else:
         self.signal_link_clicked.emit(url.toLocalFile().split("/")[-1])
def qute_pdfjs(url: QUrl) -> _HandlerRet:
    """Handler for qute://pdfjs.

    Return the pdf.js viewer or redirect to original URL if the file does not
    exist.
    """
    if url.path() == '/file':
        filename = QUrlQuery(url).queryItemValue('filename')
        if not filename:
            raise UrlInvalidError("Missing filename")
        if '/' in filename or os.sep in filename:
            raise RequestDeniedError("Path separator in filename.")

        path = _pdf_path(filename)
        with open(path, 'rb') as f:
            data = f.read()

        mimetype = utils.guess_mimetype(filename, fallback=True)
        return mimetype, data

    if url.path() == '/web/viewer.html':
        query = QUrlQuery(url)
        filename = query.queryItemValue("filename")
        if not filename:
            raise UrlInvalidError("Missing filename")

        path = _pdf_path(filename)
        if not os.path.isfile(path):
            source = query.queryItemValue('source')
            if not source:  # This may happen with old URLs stored in history
                raise UrlInvalidError("Missing source")
            raise Redirect(QUrl(source))

        data = pdfjs.generate_pdfjs_page(filename, url)
        return 'text/html', data

    try:
        data = pdfjs.get_pdfjs_res(url.path())
    except pdfjs.PDFJSNotFound as e:
        # Logging as the error might get lost otherwise since we're not showing
        # the error page if a single asset is missing. This way we don't lose
        # information, as the failed pdfjs requests are still in the log.
        log.misc.warning(
            "pdfjs resource requested but not found: {}".format(e.path))
        raise NotFoundError("Can't find pdfjs resource '{}'".format(e.path))
    else:
        mimetype = utils.guess_mimetype(url.fileName(), fallback=True)
        return mimetype, data
Exemple #3
0
 def add_dropdown_item(self, dropdown_menu: QtWidgets.QMenu,
                       url: QtCore.QUrl):
     dropdown_menu.addAction(
         url.fileName()).triggered.connect(lambda: self.play_url(url))