diff --git a/qutebrowser/browser/dirbrowser.py b/qutebrowser/browser/dirbrowser.py new file mode 100644 index 000000000..18b4e0bc9 --- /dev/null +++ b/qutebrowser/browser/dirbrowser.py @@ -0,0 +1,55 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2015 Antoni Boucher (antoyo) +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see . + +"""The directory browser page.""" + +import os + +from qutebrowser.utils import jinja + + +def dirbrowser(url): + """Get the directory browser web page. + + Args: + url: The directory path. + + Return: + The HTML of the web page. + """ + title = "Browse directory: {}".format(url) + template = jinja.env.get_template('dirbrowser.html') + # pylint: disable=no-member + # https://bitbucket.org/logilab/pylint/issue/490/ + + def is_file(file): + return os.path.isfile(os.path.join(url, file)) + + def is_dir(file): + return os.path.isdir(os.path.join(url, file)) + + parent = os.path.dirname(url) + all_files = os.listdir(url) + files = sorted([(file, os.path.join(url, file)) for file in all_files if + is_file(file)]) + directories = sorted([(file, os.path.join(url, file)) for file in + all_files if is_dir(file)]) + html = template.render(title=title, url=url, icon='', parent=parent, + files=files, directories=directories) + return html diff --git a/qutebrowser/browser/webpage.py b/qutebrowser/browser/webpage.py index 16460eaad..db8cb0871 100644 --- a/qutebrowser/browser/webpage.py +++ b/qutebrowser/browser/webpage.py @@ -31,6 +31,7 @@ from PyQt5.QtWebKitWidgets import QWebPage from qutebrowser.config import config from qutebrowser.browser import http, tabhistory +from qutebrowser.browser.dirbrowser import dirbrowser from qutebrowser.browser.network import networkmanager from qutebrowser.utils import (message, usertypes, log, jinja, qtutils, utils, objreg, debug) @@ -158,6 +159,12 @@ class BrowserPage(QWebPage): if QUrl(elem.attribute('src')) == info.url: elem.setAttribute('style', 'display: none') return False + elif (error_str.endswith('Path is a directory') and + info.url.scheme() == 'file'): + html = dirbrowser(info.url.url()[7:]) + errpage.content = html.encode('utf-8') + errpage.encoding = 'utf-8' + return True else: self._ignore_load_started = True self.error_occurred = True diff --git a/qutebrowser/html/dirbrowser.html b/qutebrowser/html/dirbrowser.html new file mode 100644 index 000000000..19659caf5 --- /dev/null +++ b/qutebrowser/html/dirbrowser.html @@ -0,0 +1,43 @@ +{% extends "base.html" %} +{% block style %} +{{ super() }} +#dirbrowserContainer { + background: #fff; + min-width: 35em; + max-width: 35em; + position: absolute; + top: 2em; + left: 1em; + padding: 10px; + border: 2px solid #eee; + -webkit-border-radius: 5px; +} + +#dirbrowserTitleText { + font-size: 118%; + font-weight: bold; +} +{% endblock %} + +{% block content %} +
+
+

Browse directory: {{url}}

+
+ +
    + {% if parent %} +
  • ..
  • + {% endif %} + + {% for item in directories %} +
  • {{item.0}}
  • + {% endfor %} + + {% for item in files %} +
  • {{item.0}}
  • + {% endfor %} +
+
+ +{% endblock %}