2015-06-04 01:29:22 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* Copyright (C) 2008 by Volker Lanz <vl@fidra.de> *
|
|
|
|
* *
|
|
|
|
* 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/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
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
Capacity::Capacity(qint64 size) :
|
2015-07-13 15:16:36 +01:00
|
|
|
m_Size(size)
|
2015-06-04 01:29:22 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Creates a new Capacity instance.
|
2015-07-13 15:16:36 +01:00
|
|
|
@param p the Partition
|
|
|
|
@param t type of Capacity
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
Capacity::Capacity(const Partition& p, Type t) :
|
2015-07-13 15:16:36 +01:00
|
|
|
m_Size(-1)
|
2015-06-04 01:29:22 +01:00
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
switch (t) {
|
|
|
|
case Used: m_Size = p.used(); break;
|
|
|
|
case Available: m_Size = p.available(); break;
|
|
|
|
case Total: m_Size = p.capacity(); break;
|
|
|
|
default: break;
|
|
|
|
}
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Creates a new Capacity instance.
|
2015-07-13 15:16:36 +01:00
|
|
|
@param d the Device
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
Capacity::Capacity(const Device& d) :
|
2015-07-13 15:16:36 +01:00
|
|
|
m_Size(d.capacity())
|
2015-06-04 01:29:22 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
qint64 Capacity::toInt(Unit u) const
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
return static_cast<qint64>(m_Size / unitFactor(Byte, u));
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** 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
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
double Capacity::toDouble(Unit u) const
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
return static_cast<double>(m_Size) / unitFactor(Byte, u);
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** 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
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
qint64 Capacity::unitFactor(Unit from, Unit to)
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
Q_ASSERT(from <= to);
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
if (from > to) {
|
|
|
|
qWarning() << "from: " << from << ", to: " << to;
|
|
|
|
return 1;
|
|
|
|
}
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
qint64 result = 1;
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
qint32 a = from;
|
|
|
|
qint32 b = to;
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
while (b-- > a)
|
|
|
|
result *= 1024;
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
return result;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** 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
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
QString Capacity::unitName(Unit u, qint64 val)
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
static QString unitNames[] = {
|
|
|
|
i18ncp("@info/plain unit", "Byte", "Bytes", val),
|
|
|
|
i18nc("@info/plain unit", "KiB"),
|
|
|
|
i18nc("@info/plain unit", "MiB"),
|
|
|
|
i18nc("@info/plain unit", "GiB"),
|
|
|
|
i18nc("@info/plain unit", "TiB"),
|
|
|
|
i18nc("@info/plain unit", "PiB"),
|
|
|
|
i18nc("@info/plain unit", "EiB"),
|
|
|
|
i18nc("@info/plain unit", "ZiB"),
|
|
|
|
i18nc("@info/plain unit", "YiB")
|
|
|
|
};
|
|
|
|
|
|
|
|
if (static_cast<quint32>(u) >= sizeof(unitNames) / sizeof(unitNames[0]))
|
|
|
|
return i18nc("@info/plain unit", "(unknown unit)");
|
|
|
|
|
|
|
|
return unitNames[u];
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Determine if the capacity is valid.
|
2015-07-13 15:16:36 +01:00
|
|
|
@return true if it is valid
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
bool Capacity::isValid() const
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
return m_Size >= 0;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|