/////////////////////////////////////////////////////////////////////////////////// // Copyright (C) 2026 Jon Beniston, M7RCE // // Some code by AI // // // // This program 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 as version 3 of the License, or // // (at your option) any later version. // // // // This program 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 V3 for more details. // // // // You should have received a copy of the GNU General Public License // // along with this program. If not, see . // /////////////////////////////////////////////////////////////////////////////////// #include #include "journalinebrowser.h" JournalineBrowser::JournalineBrowser(QWidget *parent) : QTextBrowser(parent), m_object(0) { setOpenLinks(false); connect(this, &QTextBrowser::anchorClicked, this, &JournalineBrowser::openLink); showPage(true); } QString JournalineBrowser::html(const Objects& objects, int objectId) { const auto found = objects.find(objectId); if (found == objects.end()) { return tr("

Waiting for this page...

"); } const modemdr::JournalineObject& object = found->second; const auto text = [](const std::string& utf8) { return QString::fromUtf8(utf8.data(), static_cast(utf8.size())).toHtmlEscaped().replace('\n', QStringLiteral("
")); }; QString html = QStringLiteral("

%1

").arg(text(object.title)); if (object.type == modemdr::JournalineObject::Menu) { html += QStringLiteral("
    "); for (const auto& item : object.items) { // A link once the object it opens has arrived if (objects.count(item.linkId)) { html += QStringLiteral("
  • %2
  • ").arg(QString::number(item.linkId), text(item.text)); } else { html += QStringLiteral("
  • %1
  • ").arg(text(item.text)); } } html += QStringLiteral("
"); } else if (object.type == modemdr::JournalineObject::List) { html += QStringLiteral(""); bool open = false; for (const auto& item : object.items) { if (item.newRow || !open) { html += open ? QStringLiteral("") : QStringLiteral(""); open = true; } html += QStringLiteral("").arg(text(item.text)); } html += open ? QStringLiteral("
%1
") : QStringLiteral(""); } else { for (const auto& item : object.items) { html += QStringLiteral("

%1

").arg(text(item.text)); } } return html; } void JournalineBrowser::setObjects(std::shared_ptr objects, bool anotherService) { m_objects = objects; if (anotherService) { m_object = 0; m_history.clear(); } showPage(anotherService); } void JournalineBrowser::showPage(bool top) { const QString html = (!m_objects || m_objects->empty()) ? tr("

No Journaline service

") : JournalineBrowser::html(*m_objects, m_object); // Updates arrive every few seconds: redraw only a page that changed, keeping the reader's // place (and selection) if (html != m_shownHtml) { const int scroll = verticalScrollBar()->value(); setHtml(html); verticalScrollBar()->setValue(top ? 0 : scroll); m_shownHtml = html; } else if (top) { verticalScrollBar()->setValue(0); } emit navigationChanged(); } void JournalineBrowser::openLink(const QUrl& link) { bool ok = false; const int objectId = link.path().toInt(&ok); if ((link.scheme() != QLatin1String("jml")) || !ok) { return; } m_history.push_back(m_object); m_object = objectId; showPage(true); } void JournalineBrowser::goHome() { if (m_object != 0) { m_history.push_back(m_object); m_object = 0; showPage(true); } } void JournalineBrowser::goBack() { if (!m_history.empty()) { m_object = m_history.back(); m_history.pop_back(); showPage(true); } }