kpmcore/src/util/report.cpp

165 lines
4.5 KiB
C++
Raw Normal View History

/*************************************************************************
* Copyright (C) 2008 by Volker Lanz <vl@fidra.de> *
2016-03-02 19:00:31 +00:00
* Copyright (C) 2016 by Andrius Štikonas <andrius@stikonas.eu> *
* *
* 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; either 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 for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
*************************************************************************/
#include "util/report.h"
#include "util/htmlreport.h"
#include "backend/corebackend.h"
#include "backend/corebackendmanager.h"
#include <KLocalizedString>
#include <sys/utsname.h>
/** Creates a new Report instance.
2018-08-01 00:58:24 +01:00
@param p pointer to the parent instance. May be nullptr if this is a new root Report.
2015-07-13 15:16:36 +01:00
@param cmd the command
*/
Report::Report(Report* p, const QString& cmd) :
2015-07-13 15:16:36 +01:00
QObject(),
m_Parent(p),
m_Children(),
m_Command(cmd),
m_Output(),
m_Status()
{
}
/** Destroys a Report instance and all its children. */
Report::~Report()
{
2015-07-13 15:16:36 +01:00
qDeleteAll(children());
}
/** Creates a new child for this Report and appends it to its list of children.
2015-07-13 15:16:36 +01:00
@param cmd the command
@return pointer to a new Report child
*/
Report* Report::newChild(const QString& cmd)
{
2015-07-13 15:16:36 +01:00
Report* r = new Report(this, cmd);
m_Children.append(r);
return r;
}
/**
2015-07-13 15:16:36 +01:00
@return the Report converted to HTML
@see toText()
*/
QString Report::toHtml() const
{
2015-07-13 15:16:36 +01:00
QString s;
2015-07-13 15:16:36 +01:00
if (parent() == root())
s += QStringLiteral("<div>\n");
else if (parent() != nullptr)
2015-07-13 15:16:36 +01:00
s += QStringLiteral("<div style='margin-left:24px;margin-top:12px;margin-bottom:12px'>\n");
2015-07-13 15:16:36 +01:00
if (!command().isEmpty())
s += QStringLiteral("\n<b>") + command().toHtmlEscaped() + QStringLiteral("</b>\n\n");
2015-07-13 15:16:36 +01:00
if (!output().isEmpty())
s += QStringLiteral("<pre>") + output().toHtmlEscaped() + QStringLiteral("</pre>\n\n");
2015-07-13 15:16:36 +01:00
if (children().size() == 0)
s += QStringLiteral("<br/>\n");
else
2016-08-08 20:45:41 +01:00
for (const auto &child : children())
2016-06-01 21:00:31 +01:00
s += child->toHtml();
2015-07-13 15:16:36 +01:00
if (!status().isEmpty())
s += QStringLiteral("<b>") + status().toHtmlEscaped() + QStringLiteral("</b><br/>\n\n");
if (parent() != nullptr)
2015-07-13 15:16:36 +01:00
s += QStringLiteral("</div>\n\n");
2015-07-13 15:16:36 +01:00
return s;
}
/**
2015-07-13 15:16:36 +01:00
@return the Report converted to plain text
@see toHtml()
*/
QString Report::toText() const
{
2015-07-13 15:16:36 +01:00
QString s;
2015-07-13 15:16:36 +01:00
if (!command().isEmpty()) {
s += QStringLiteral("==========================================================================================\n");
s += command() + QStringLiteral("\n");
s += QStringLiteral("==========================================================================================\n");
}
2015-07-13 15:16:36 +01:00
if (!output().isEmpty())
s += output() + QStringLiteral("\n");
2016-08-08 20:45:41 +01:00
for (const auto &child : children())
2016-06-01 21:00:31 +01:00
s += child->toText();
2015-07-13 15:16:36 +01:00
return s;
}
/** Adds a string to this Report's output.
2015-07-13 15:16:36 +01:00
This is usually not what you want. In most cases, you will want to create a new child Report.
2015-07-13 15:16:36 +01:00
@param s the string to add to the output
2015-07-13 15:16:36 +01:00
@see newChild()
*/
void Report::addOutput(const QString& s)
{
2015-07-13 15:16:36 +01:00
m_Output += s;
root()->emitOutputChanged();
}
void Report::emitOutputChanged()
{
2020-09-15 02:36:46 +01:00
Q_EMIT outputChanged();
}
/** @return the root Report */
Report* Report::root()
{
2015-07-13 15:16:36 +01:00
Report* rval = this;
while (rval->parent() != nullptr)
2015-07-13 15:16:36 +01:00
rval = rval->parent();
2015-07-13 15:16:36 +01:00
return rval;
}
/**
2015-07-13 15:16:36 +01:00
@overload
*/
const Report* Report::root() const
{
2015-07-13 15:16:36 +01:00
const Report* rval = this;
while (rval->parent() != nullptr)
2015-07-13 15:16:36 +01:00
rval = rval->parent();
2015-07-13 15:16:36 +01:00
return rval;
}
/** @return a Report line to write to */
ReportLine Report::line()
{
2015-07-13 15:16:36 +01:00
return ReportLine(*this);
}