aboutsummaryrefslogtreecommitdiff
path: root/src/schemes/NullSchemeHandler.cpp
blob: 203e673d0de590d9f12a655620349661918017f7 (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
#include "NullSchemeHandler.hpp"
#include <qstringview.h>
#include <qwebengineurlrequestjob.h>

void NullSchemeHandler::requestStarted(QWebEngineUrlRequestJob *job) {
  auto url = job->requestUrl();

  if (url.host() == "docs") {
    QBuffer *buffer = new QBuffer(job);
    buffer->setData(read_static_docs_file(url.path()));
    buffer->open(QIODevice::ReadOnly);
    job->reply(get_content_type(url.path()), buffer);
  } else {
    job->reply("text/html", new QBuffer(job));
  }
}

QByteArray NullSchemeHandler::read_static_docs_file(const QString &path) {
  QFile file(NULL_DOCS_DIR + path);
  if (!file.exists())
    return read_index_html();

  if (file.open(QIODevice::ReadOnly)) {
    auto contents = file.readAll();
    file.close();
    return contents;
  }
  return QByteArray{};
}

QByteArray NullSchemeHandler::read_index_html() {
  QFile file(NULL_DOCS_DIR + QString("/index.html"));
  qDebug() << ":::" << file.exists();
  if (file.open(QIODevice::ReadOnly)) {
    auto contents = file.readAll();
    file.close();
    return contents;
  }
  return QByteArray{};
}

QByteArray NullSchemeHandler::get_content_type(const QString &path) {
  if (path.endsWith(".css"))
    return "text/css";
  return "text/html";
}