diff options
| author | Mike Skec <skec@protonmail.ch> | 2021-01-03 13:01:47 +1100 |
|---|---|---|
| committer | Felix Queißner <felix@ib-queissner.de> | 2021-01-06 10:51:18 +0100 |
| commit | d815c8badabb347537f26612a5edab3a71cba866 (patch) | |
| tree | 31cad5baf317fbe1bad63ae7b9a0cd3104317d42 /src/browsertab.cpp | |
| parent | 522184bf155d22728d1a829e5f30847ae3e7aeb7 (diff) | |
| download | kristall-d815c8badabb347537f26612a5edab3a71cba866.tar.gz | |
Basic caching functionality implemented
Diffstat (limited to 'src/browsertab.cpp')
| -rw-r--r-- | src/browsertab.cpp | 60 |
1 files changed, 50 insertions, 10 deletions
diff --git a/src/browsertab.cpp b/src/browsertab.cpp index 3d52659..7fa0ad2 100644 --- a/src/browsertab.cpp +++ b/src/browsertab.cpp @@ -125,7 +125,7 @@ BrowserTab::~BrowserTab() delete ui; } -void BrowserTab::navigateTo(const QUrl &url, PushToHistory mode) +void BrowserTab::navigateTo(const QUrl &url, PushToHistory mode, bool no_read_cache) { if (kristall::protocols.isSchemeSupported(url.scheme()) != ProtocolSetup::Enabled) { @@ -143,7 +143,7 @@ void BrowserTab::navigateTo(const QUrl &url, PushToHistory mode) this->successfully_loaded = false; this->timer.start(); - if(not this->startRequest(url, ProtocolHandler::Default)) { + if(not this->startRequest(url, ProtocolHandler::Default, no_read_cache)) { QMessageBox::critical(this, "Kristall", QString("Failed to execute request to %1").arg(url.toString())); return; } @@ -185,7 +185,7 @@ void BrowserTab::scrollToAnchor(QString const &anchor) void BrowserTab::reloadPage() { if (current_location.isValid()) - this->navigateTo(this->current_location, DontPush); + this->navigateTo(this->current_location, DontPush, true); } void BrowserTab::focusUrlBar() @@ -418,12 +418,17 @@ static QByteArray convertToUtf8(QByteArray const & input, QString const & charSe void BrowserTab::on_requestComplete(const QByteArray &ref_data, const QString &mime_text) { + MimeType mime = MimeParser::parse(mime_text); + this->on_requestCompleteMime(ref_data, mime); +} + +void BrowserTab::on_requestCompleteMime(const QByteArray &ref_data, const MimeType &mime) +{ + QByteArray data; + this->ui->media_browser->stopPlaying(); this->network_timeout_timer.stop(); - QByteArray data = ref_data; - MimeType mime = MimeParser::parse(mime_text); - qDebug() << "Loaded" << ref_data.length() << "bytes of type" << mime.type << "/" << mime.subtype; // for(auto & key : mime.parameters.keys()) { // qDebug() << key << mime.parameters[key]; @@ -449,6 +454,10 @@ void BrowserTab::on_requestComplete(const QByteArray &ref_data, const QString &m } } } + else + { + data = ref_data; + } this->successfully_loaded = true; this->page_title = ""; @@ -465,6 +474,14 @@ void BrowserTab::on_requestComplete(const QByteArray &ref_data, const QString &m emit this->fileLoaded(this->current_stats); this->updateMouseCursor(false); + + // Finally, put file in cache if we are not in an internal + // location. Don't cache if we read this page from cache. + if (!this->is_internal_location && + !this->was_read_from_cache) + { + this->mainWindow->cachePage(this->current_location, data, mime); + } } void BrowserTab::renderPage(const QByteArray &data, const MimeType &mime) @@ -1248,12 +1265,14 @@ void BrowserTab::addProtocolHandler(std::unique_ptr<ProtocolHandler> &&handler) this->protocol_handlers.emplace_back(std::move(handler)); } -bool BrowserTab::startRequest(const QUrl &url, ProtocolHandler::RequestOptions options) +bool BrowserTab::startRequest(const QUrl &url, ProtocolHandler::RequestOptions options, bool no_read_cache) { this->updateMouseCursor(true); this->current_server_certificate = QSslCertificate { }; + this->was_read_from_cache = false; + this->current_handler = nullptr; for(auto & ptr : this->protocol_handlers) { @@ -1339,13 +1358,34 @@ bool BrowserTab::startRequest(const QUrl &url, ProtocolHandler::RequestOptions o if(not try_enable_certificate()) return false; - this->is_internal_location = (url.scheme() == "about"); + QString urlstr = url.toString(QUrl::FullyEncoded); + + this->is_internal_location = (url.scheme() == "about" || url.scheme() == "file"); this->current_location = url; - this->setUrlBarText(url.toString(QUrl::FormattingOptions(QUrl::FullyEncoded))); + this->setUrlBarText(urlstr); this->network_timeout_timer.start(kristall::options.network_timeout); - return this->current_handler->startRequest(url.adjusted(QUrl::RemoveFragment), options); + const auto req = [this, &url, &options]() + { + return this->current_handler->startRequest(url.adjusted(QUrl::RemoveFragment), options); + }; + + if (no_read_cache) return req(); + + // Check if we have the page in our cache. + urlstr = url.toString(QUrl::FullyEncoded | QUrl::RemoveFragment); + if (std::shared_ptr<CachedPage> pg = mainWindow->cacheFind(urlstr); pg != nullptr) + { + qDebug() << "Reading page from cache"; + this->was_read_from_cache = true; + this->on_requestCompleteMime(pg->body, pg->mime); + return true; + } + else + { + return req(); + } } void BrowserTab::updateMouseCursor(bool waiting) |
