blob: ebd5c9f41004d6bf1e8d8524a436e0e0ffe09a29 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
#include "abouthandler.hpp"
#include "kristall.hpp"
#include "ioutil.hpp"
#include <QUrl>
#include <QFile>
AboutHandler::AboutHandler()
{
}
bool AboutHandler::supportsScheme(const QString &scheme) const
{
return (scheme == "about");
}
bool AboutHandler::startRequest(const QUrl &url, ProtocolHandler::RequestOptions options)
{
Q_UNUSED(options)
if (url.path() == "blank")
{
emit this->requestComplete("", "text/gemini");
}
else if (url.path() == "favourites")
{
QByteArray document;
document.append(tr("# Favourites\n").toUtf8());
QString current_group;
for (auto const &fav : kristall::globals().favourites.allFavourites())
{
if(current_group != fav.first) {
document.append("\n");
document.append(QString("## %1\n").arg(fav.first).toUtf8());
current_group = fav.first;
}
if(fav.second->title.isEmpty()) {
document.append("=> " + fav.second->destination.toString().toUtf8() + "\n");
} else {
document.append("=> " + fav.second->destination.toString().toUtf8() + " " + fav.second->title.toUtf8() + "\n");
}
}
emit this->requestComplete(document, "text/gemini");
}
else if (url.path() == "cache")
{
QByteArray document;
document.append(QString(tr("# Cache information\n")).toUtf8());
auto& cache = kristall::globals().cache.getPages();
long unsigned cache_usage = 0;
int cached_count = 0;
for (auto it = cache.begin(); it != cache.end(); ++it, ++cached_count)
{
cache_usage += (long unsigned)it->second->body.size();
}
document.append(QString(
tr("In-memory cache usage:\n"
"* %1 used\n"
"* %2 pages in cache\n"))
.arg(IoUtil::size_human(cache_usage), QString::number(cached_count)).toUtf8());
emit this->requestComplete(document, "text/gemini");
}
else
{
QFile file(QString(":/about/%1.gemini").arg(url.path()));
if (file.open(QFile::ReadOnly))
{
emit this->requestComplete(file.readAll(), "text/gemini");
}
else
{
emit this->networkError(ResourceNotFound, QObject::tr("The requested resource does not exist."));
}
}
return true;
}
bool AboutHandler::isInProgress() const
{
return false;
}
bool AboutHandler::cancelRequest()
{
return true;
}
|