File indexing completed on 2024-04-21 05:46:11

0001 /*
0002     SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
0003     SPDX-FileCopyrightText: 2014 Andrius Štikonas <andrius@stikonas.eu>
0004 
0005     SPDX-License-Identifier: GPL-3.0-or-later
0006 */
0007 
0008 #include "util/report.h"
0009 #include "util/htmlreport.h"
0010 
0011 #include "backend/corebackend.h"
0012 #include "backend/corebackendmanager.h"
0013 
0014 #include <KLocalizedString>
0015 
0016 #include <sys/utsname.h>
0017 
0018 /** Creates a new Report instance.
0019     @param p pointer to the parent instance. May be nullptr if this is a new root Report.
0020     @param cmd the command
0021 */
0022 Report::Report(Report* p, const QString& cmd) :
0023     QObject(),
0024     m_Parent(p),
0025     m_Children(),
0026     m_Command(cmd),
0027     m_Output(),
0028     m_Status()
0029 {
0030 }
0031 
0032 /** Destroys a Report instance and all its children. */
0033 Report::~Report()
0034 {
0035     qDeleteAll(children());
0036 }
0037 
0038 /** Creates a new child for this Report and appends it to its list of children.
0039     @param cmd the command
0040     @return pointer to a new Report child
0041 */
0042 Report* Report::newChild(const QString& cmd)
0043 {
0044     Report* r = new Report(this, cmd);
0045     m_Children.append(r);
0046     return r;
0047 }
0048 
0049 /**
0050     @return the Report converted to HTML
0051     @see toText()
0052 */
0053 QString Report::toHtml() const
0054 {
0055     QString s;
0056 
0057     if (parent() == root())
0058         s += QStringLiteral("<div>\n");
0059     else if (parent() != nullptr)
0060         s += QStringLiteral("<div style='margin-left:24px;margin-top:12px;margin-bottom:12px'>\n");
0061 
0062     if (!command().isEmpty())
0063         s += QStringLiteral("\n<b>") + command().toHtmlEscaped() + QStringLiteral("</b>\n\n");
0064 
0065     if (!output().isEmpty())
0066         s += QStringLiteral("<pre>") + output().toHtmlEscaped() + QStringLiteral("</pre>\n\n");
0067 
0068     if (children().size() == 0)
0069         s += QStringLiteral("<br/>\n");
0070     else
0071         for (const auto &child : children())
0072             s += child->toHtml();
0073 
0074     if (!status().isEmpty())
0075         s += QStringLiteral("<b>") + status().toHtmlEscaped() + QStringLiteral("</b><br/>\n\n");
0076 
0077     if (parent() != nullptr)
0078         s += QStringLiteral("</div>\n\n");
0079 
0080     return s;
0081 }
0082 
0083 /**
0084     @return the Report converted to plain text
0085     @see toHtml()
0086 */
0087 QString Report::toText() const
0088 {
0089     QString s;
0090 
0091     if (!command().isEmpty()) {
0092         s += QStringLiteral("==========================================================================================\n");
0093         s += command() + QStringLiteral("\n");
0094         s += QStringLiteral("==========================================================================================\n");
0095     }
0096 
0097     if (!output().isEmpty())
0098         s += output() + QStringLiteral("\n");
0099 
0100     for (const auto &child : children())
0101         s += child->toText();
0102 
0103     return s;
0104 }
0105 
0106 /** Adds a string to this Report's output.
0107 
0108     This is usually not what you want. In most cases, you will want to create a new child Report.
0109 
0110     @param s the string to add to the output
0111 
0112     @see newChild()
0113 */
0114 void Report::addOutput(const QString& s)
0115 {
0116     m_Output += s;
0117     root()->emitOutputChanged();
0118 }
0119 
0120 void Report::emitOutputChanged()
0121 {
0122     Q_EMIT outputChanged();
0123 }
0124 
0125 /** @return the root Report */
0126 Report* Report::root()
0127 {
0128     Report* rval = this;
0129 
0130     while (rval->parent() != nullptr)
0131         rval = rval->parent();
0132 
0133     return rval;
0134 }
0135 
0136 /**
0137     @overload
0138 */
0139 const Report* Report::root() const
0140 {
0141     const Report* rval = this;
0142 
0143     while (rval->parent() != nullptr)
0144         rval = rval->parent();
0145 
0146     return rval;
0147 }
0148 
0149 /** @return a Report line to write to */
0150 ReportLine Report::line()
0151 {
0152     return ReportLine(*this);
0153 }
0154 
0155 #include "moc_report.cpp"