kpmcore/src/util/capacity.cpp

131 lines
3.1 KiB
C++
Raw Normal View History

/*
SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
SPDX-FileCopyrightText: 2012-2018 Andrius Štikonas <andrius@stikonas.eu>
SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "util/capacity.h"
#include "core/partition.h"
#include "core/device.h"
#include <KFormat>
#include <KLocalizedString>
#include <QDebug>
const QString Capacity::m_InvalidString = QStringLiteral("---");
/** Creates a new Capacity instance.
2015-07-13 15:16:36 +01:00
@param size the size in bytes
*/
Capacity::Capacity(qint64 size) :
2015-07-13 15:16:36 +01:00
m_Size(size)
{
}
/** Creates a new Capacity instance.
2015-07-13 15:16:36 +01:00
@param p the Partition
@param t type of Capacity
*/
Capacity::Capacity(const Partition& p, Type t) :
2015-07-13 15:16:36 +01:00
m_Size(-1)
{
2015-07-13 15:16:36 +01:00
switch (t) {
2018-04-09 15:14:34 +01:00
case Type::Used: m_Size = p.used(); break;
case Type::Available: m_Size = p.available(); break;
case Type::Total: m_Size = p.capacity();
2015-07-13 15:16:36 +01:00
}
}
/** Creates a new Capacity instance.
2015-07-13 15:16:36 +01:00
@param d the Device
*/
Capacity::Capacity(const Device& d) :
2015-07-13 15:16:36 +01:00
m_Size(d.capacity())
{
}
/** Returns the Capacity as qint64 converted to the given Unit.
2015-07-13 15:16:36 +01:00
@param u the Unit to use
@return the Capacity in the given Unit as qint64
*/
qint64 Capacity::toInt(Unit u) const
{
2018-04-09 15:14:34 +01:00
return static_cast<qint64>(m_Size / unitFactor(Unit::Byte, u));
}
/** Returns the Capacity as double converted to the given Unit.
2015-07-13 15:16:36 +01:00
@param u the Unit to use
@return the Capacity in the given Unit as double
*/
double Capacity::toDouble(Unit u) const
{
2018-04-09 15:14:34 +01:00
return static_cast<double>(m_Size) / unitFactor(Unit::Byte, u);
}
/** Returns a factor to convert between two Units.
2015-07-13 15:16:36 +01:00
@param from the Unit to convert from
@param to the Unit to convert to
@return the factor to use for conversion
*/
qint64 Capacity::unitFactor(Unit from, Unit to)
{
2015-07-13 15:16:36 +01:00
Q_ASSERT(from <= to);
2015-07-13 15:16:36 +01:00
if (from > to) {
2018-04-09 15:14:34 +01:00
qWarning() << "from: " << static_cast<uint>(from) << ", to: " << static_cast<uint>(to);
2015-07-13 15:16:36 +01:00
return 1;
}
2015-07-13 15:16:36 +01:00
qint64 result = 1;
2018-04-09 15:14:34 +01:00
qint32 a = static_cast<uint>(from);
qint32 b = static_cast<uint>(to);
2015-07-13 15:16:36 +01:00
while (b-- > a)
result *= 1024;
2015-07-13 15:16:36 +01:00
return result;
}
/** Returns the name of a given Unit.
2015-07-13 15:16:36 +01:00
@param u the Unit to find the name for
@return the name
*/
QString Capacity::unitName(Unit u, qint64 val)
{
2015-07-13 15:16:36 +01:00
static QString unitNames[] = {
xi18ncp("@item:intext unit", "Byte", "Bytes", val),
xi18nc("@item:intext unit", "KiB"),
xi18nc("@item:intext unit", "MiB"),
xi18nc("@item:intext unit", "GiB"),
xi18nc("@item:intext unit", "TiB"),
xi18nc("@item:intext unit", "PiB"),
xi18nc("@item:intext unit", "EiB"),
xi18nc("@item:intext unit", "ZiB"),
xi18nc("@item:intext unit", "YiB")
2015-07-13 15:16:36 +01:00
};
if (static_cast<quint32>(u) >= sizeof(unitNames) / sizeof(unitNames[0]))
return xi18nc("@item:intext unit", "(unknown unit)");
2015-07-13 15:16:36 +01:00
2018-04-09 15:14:34 +01:00
return unitNames[static_cast<quint32>(u)];
}
/** Determine if the capacity is valid.
2015-07-13 15:16:36 +01:00
@return true if it is valid
*/
bool Capacity::isValid() const
{
2015-07-13 15:16:36 +01:00
return m_Size >= 0;
}
QString Capacity::formatByteSize(double size, int precision)
{
2015-07-13 15:16:36 +01:00
if (size < 0)
return invalidString();
return KFormat().formatByteSize(size, precision);
}