swift
abouthtmlcomponent.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2018 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "abouthtmlcomponent.h"
5 
6 #include <QDesktopServices>
7 #include <QFile>
8 #include <QPointer>
9 #include <QTimer>
10 
11 #include "ui_abouthtmlcomponent.h"
12 
13 #include "gui/guiapplication.h"
14 #include "misc/directoryutils.h"
15 #include "misc/fileutils.h"
16 #include "misc/swiftdirectories.h"
17 
18 using namespace swift::misc;
19 
20 namespace swift::gui::components
21 {
22  CAboutHtmlComponent::CAboutHtmlComponent(QWidget *parent) : QFrame(parent), ui(new Ui::CAboutHtmlComponent)
23  {
24  ui->setupUi(this);
25  const QPointer<CAboutHtmlComponent> myself(this);
26  connect(ui->tbr_About, &QTextBrowser::anchorClicked, this, &CAboutHtmlComponent::onAnchorClicked,
27  Qt::QueuedConnection);
28 
29  QTimer::singleShot(2500, this, [=] {
30  if (!myself) { return; }
31  myself->loadAbout();
32  });
33  }
34 
36 
37  void CAboutHtmlComponent::loadAbout()
38  {
39  if (!sGui || sGui->isShuttingDown()) { return; }
40 
41  // make links absolute
42  static const QString htmlFixed = [=] {
43  // workaround:
44  // 1) Only reading as HTML gives proper formatting
45  // 2) Reading the file resource fails (likely because of the style sheet)
46  const QString html = CFileUtils::readFileToString(CSwiftDirectories::aboutFilePath());
47  return html;
48 
49  // no longer replacing the URLs, doing this on anchor clicked
50  // const QString legalDir = sGui->getGlobalSetup().getLegalDirectoryUrl().getFullUrl();
51  // return QString(html).replace(QLatin1String("href=\"./"), "href=\"" + legalDir);
52  }();
53 
54  ui->tbr_About->setHtml(htmlFixed);
55  ui->tbr_About->setOpenLinks(false);
56 
57  // base URL
58  // ui->tbr_About->document()->setMetaInformation(QTextDocument::DocumentUrl,
59  // "https://datastore.swift-project.org/legal");
60  }
61 
62  void CAboutHtmlComponent::onAnchorClicked(const QUrl &url)
63  {
64  if (!url.isRelative())
65  {
66  QDesktopServices::openUrl(url);
67  return;
68  }
69  const QString possibleLegalFile =
70  CFileUtils::appendFilePaths(CSwiftDirectories::legalDirectory(), url.fileName());
71  QFile f(possibleLegalFile);
72  if (f.exists()) { QDesktopServices::openUrl(QUrl::fromLocalFile(possibleLegalFile)); };
73  }
74 } // namespace swift::gui::components
bool isShuttingDown() const
Is application shutting down?
Display the HTML info "about swift".
SWIFT_GUI_EXPORT swift::gui::CGuiApplication * sGui
Single instance of GUI application object.
High level reusable GUI components.
Definition: aboutdialog.cpp:13
Free functions in swift::misc.
auto singleShot(int msec, QObject *target, F &&task)
Starts a single-shot timer which will call a task in the thread of the given object when it times out...
Definition: threadutils.h:30