diff options
| author | Felix (xq) Queißner <git@mq32.de> | 2020-06-29 00:46:48 +0200 |
|---|---|---|
| committer | Felix (xq) Queißner <git@mq32.de> | 2020-06-29 00:46:48 +0200 |
| commit | e2bd7d695474314aa910a36c664e4dc1c7e39d8c (patch) | |
| tree | 41e618965eaad77128940b84f490aa2a94370e5b /src | |
| parent | 1360cbe9cd11c99a23847600c820a900897dd381 (diff) | |
| download | kristall-e2bd7d695474314aa910a36c664e4dc1c7e39d8c.tar.gz | |
Implements style preview for kristall document styles. Allows importing them via the preview site.
Diffstat (limited to 'src')
| -rw-r--r-- | src/about/style-display.gemini | 21 | ||||
| -rw-r--r-- | src/about/style-preview.gemini | 7 | ||||
| -rw-r--r-- | src/browsertab.cpp | 122 | ||||
| -rw-r--r-- | src/browsertab.hpp | 2 | ||||
| -rw-r--r-- | src/builtins.qrc | 1 | ||||
| -rw-r--r-- | src/dialogs/settingsdialog.cpp | 4 |
6 files changed, 145 insertions, 12 deletions
diff --git a/src/about/style-display.gemini b/src/about/style-display.gemini new file mode 100644 index 0000000..295c1da --- /dev/null +++ b/src/about/style-display.gemini @@ -0,0 +1,21 @@ +# H1 Header +## H2 Header +### H3 Header +Plain text document here. +* List A +* List B +=> rela-link Same-Site Link +=> //foreign.host/ Foreign Site Link +=> https://foreign.host/ Cross-Protocol Link +> Multi-lined +> block quotes +``` + ▄▄▄ ██▀███ ▄▄▄█████▓ + ▒████▄ ▓██ ▒ ██▒▓ ██▒ ▓▒ + ▒██ ▀█▄ ▓██ ░▄█ ▒▒ ▓██░ ▒░ + ░██▄▄▄▄██ ▒██▀▀█▄ ░ ▓██▓ ░ + ▓█ ▓██▒░██▓ ▒██▒ ▒██▒ ░ + ▒▒ ▓▒█░░ ▒▓ ░▒▓░ ▒ ░░ + ▒ ▒▒ ░ ░▒ ░ ▒░ ░ + ░ ▒ ░░ ░ ░ + ░ ░ ░ diff --git a/src/about/style-preview.gemini b/src/about/style-preview.gemini index 295c1da..aaf1282 100644 --- a/src/about/style-preview.gemini +++ b/src/about/style-preview.gemini @@ -2,13 +2,17 @@ ## H2 Header ### H3 Header Plain text document here. + * List A * List B + => rela-link Same-Site Link => //foreign.host/ Foreign Site Link => https://foreign.host/ Cross-Protocol Link + > Multi-lined > block quotes + ``` ▄▄▄ ██▀███ ▄▄▄█████▓ ▒████▄ ▓██ ▒ ██▒▓ ██▒ ▓▒ @@ -19,3 +23,6 @@ Plain text document here. ▒ ▒▒ ░ ░▒ ░ ▒░ ░ ░ ▒ ░░ ░ ░ ░ ░ ░ +``` + +=> kristall+ctrl:install-theme Install theme in Kristall diff --git a/src/browsertab.cpp b/src/browsertab.cpp index 8c6fb37..47163d3 100644 --- a/src/browsertab.cpp +++ b/src/browsertab.cpp @@ -304,10 +304,6 @@ void BrowserTab::on_hostCertificateLoaded(const QSslCertificate &cert) static QByteArray convertToUtf8(QByteArray const & input, QString const & charSet) { - QFile temp { "/tmp/raw.dat" }; - temp.open(QFile::WriteOnly); - IoUtil::writeAll(temp, input); - auto charset_u8 = charSet.toUpper().toUtf8(); // TRANSLIT will try to mix-match other code points to reflect to correct encoding @@ -405,7 +401,7 @@ void BrowserTab::on_requestComplete(const QByteArray &ref_data, const QString &m } } - this->current_mime = mime_text; + this->current_mime = mime; this->current_buffer = ref_data; this->graphics_scene.clear(); @@ -455,6 +451,35 @@ void BrowserTab::on_requestComplete(const QByteArray &ref_data, const QString &m document->setDocumentMargin(doc_style.margin); document->setHtml(QString::fromUtf8(data)); } + else if (not plaintext_only and mime.is("text","x-kristall-theme")) + { + // ugly workaround for QSettings needing a file + QFile temp_file { kristall::dirs::cache_root.absoluteFilePath("preview-theme.kthm") }; + + if(temp_file.open(QFile::WriteOnly)) { + IoUtil::writeAll(temp_file, data); + temp_file.close(); + } + + QSettings temp_settings { + temp_file.fileName(), + QSettings::IniFormat + }; + + DocumentStyle preview_style; + preview_style.load(temp_settings); + + QFile src { ":/about/style-preview.gemini" }; + src.open(QFile::ReadOnly); + + document = GeminiRenderer::render( + src.readAll(), + this->current_location, + preview_style, + this->outline); + + this->ui->text_browser->setStyleSheet(QString("QTextBrowser { background-color: %1; }").arg(preview_style.background_color.name())); + } else if (not plaintext_only and mime.is("text","markdown")) { document = MarkdownRenderer::render( @@ -684,12 +709,14 @@ void BrowserTab::on_text_browser_anchorClicked(const QUrl &url, bool open_in_new // used for fake-buttons if(url.scheme() == "kristall+ctrl") { - if(this->is_internal_location) { + bool is_theme_preview = this->current_mime.is("text", "x-kristall-theme"); + + if(this->is_internal_location or is_theme_preview) { QString opt = url.path(); qDebug() << "kristall control action" << opt; // this will bypass the TLS security - if(opt == "ignore-tls") { + if(not is_theme_preview and opt == "ignore-tls") { auto response = QMessageBox::question( this, "Kristall", @@ -702,11 +729,11 @@ void BrowserTab::on_text_browser_anchorClicked(const QUrl &url, bool open_in_new } } // - else if(opt == "ignore-tls-safe") { + else if(not is_theme_preview and opt == "ignore-tls-safe") { this->startRequest(this->current_location, ProtocolHandler::IgnoreTlsErrors); } // Add this page to the list of trusted hosts and continue - else if(opt == "add-fingerprint") { + else if(not is_theme_preview and opt == "add-fingerprint") { auto answer = QMessageBox::question( this, "Kristall", @@ -731,6 +758,83 @@ void BrowserTab::on_text_browser_anchorClicked(const QUrl &url, bool open_in_new this->startRequest(this->current_location, ProtocolHandler::Default); } + else if(opt == "install-theme") { + + if(is_theme_preview) + { + // ugly workaround for QSettings needing a file + QFile temp_file { kristall::dirs::cache_root.absoluteFilePath("preview-theme.kthm") }; + + if(temp_file.open(QFile::WriteOnly)) { + IoUtil::writeAll(temp_file, this->current_buffer); + temp_file.close(); + } + + QSettings temp_settings { + temp_file.fileName(), + QSettings::IniFormat + }; + + QString name; + if(auto name_var = temp_settings.value("name"); name_var.isNull()) + { + QInputDialog input { this }; + input.setInputMode(QInputDialog::TextInput); + input.setLabelText(tr("This style has no embedded name. Please enter a name for the preset:")); + input.setTextValue(this->current_location.fileName().split(".", QString::SkipEmptyParts).first()); + + if(input.exec() != QDialog::Accepted) + return; + + name = input.textValue().trimmed(); + } + else + { + name = name_var.toString(); + } + + auto answer = QMessageBox::question( + this, + "Kristall", + tr("Do you want to add the style %1 to your collection?").arg(name) + ); + if(answer != QMessageBox::Yes) + return; + + QString fileName; + + int index = 0; + do + { + fileName = DocumentStyle::createFileNameFromName(name, index); + index += 1; + } while(kristall::dirs::styles.exists(fileName)); + + QFile target_file { kristall::dirs::styles.absoluteFilePath(fileName) }; + + if(target_file.open(QFile::WriteOnly)) { + IoUtil::writeAll(target_file, this->current_buffer); + target_file.close(); + } + + QSettings final_settings { + target_file.fileName(), + QSettings::IniFormat + }; + final_settings.setValue("name", name); + final_settings.sync(); + + QMessageBox::information( + this, + "Kristall", + tr("The theme %1 was successfully added to your theme collection!").arg(name) + ); + } + else + { + qDebug() << "install-theme triggered from non-theme document!"; + } + } } else { QMessageBox::critical( this, diff --git a/src/browsertab.hpp b/src/browsertab.hpp index 2905fcb..7e7f446 100644 --- a/src/browsertab.hpp +++ b/src/browsertab.hpp @@ -169,7 +169,7 @@ public: QSslCertificate current_server_certificate; QByteArray current_buffer; - QString current_mime; + MimeType current_mime; QElapsedTimer timer; CryptoIdentity current_identity; diff --git a/src/builtins.qrc b/src/builtins.qrc index 566f96b..6a6d56c 100644 --- a/src/builtins.qrc +++ b/src/builtins.qrc @@ -76,5 +76,6 @@ <file>icons/light/objects/volume-off.svg</file> <file>icons/light/objects/volume-on.svg</file> <file>icons/light/index.theme</file> + <file>about/style-display.gemini</file> </qresource> </RCC> diff --git a/src/dialogs/settingsdialog.cpp b/src/dialogs/settingsdialog.cpp index 6f69c40..d1e7dbf 100644 --- a/src/dialogs/settingsdialog.cpp +++ b/src/dialogs/settingsdialog.cpp @@ -239,9 +239,9 @@ GenericSettings SettingsDialog::options() const void SettingsDialog::reloadStylePreview() { - QFile document_src { ":/about/style-preview.gemini" }; + QFile document_src { ":/about/style-display.gemini" }; bool ok = document_src.open(QFile::ReadOnly); - assert(ok and "failed to find style-preview.gemini!"); + assert(ok and "failed to find style-display.gemini!"); auto const document = document_src.readAll(); |
