File indexing completed on 2024-04-21 05:46:10
0001 /* 0002 SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de> 0003 SPDX-FileCopyrightText: 2012-2018 Andrius Štikonas <andrius@stikonas.eu> 0004 0005 SPDX-License-Identifier: GPL-3.0-or-later 0006 */ 0007 0008 #include "util/capacity.h" 0009 0010 #include "core/partition.h" 0011 #include "core/device.h" 0012 0013 #include <KFormat> 0014 #include <KLocalizedString> 0015 0016 #include <QDebug> 0017 0018 const QString Capacity::m_InvalidString = QStringLiteral("---"); 0019 0020 /** Creates a new Capacity instance. 0021 @param size the size in bytes 0022 */ 0023 Capacity::Capacity(qint64 size) : 0024 m_Size(size) 0025 { 0026 } 0027 0028 /** Creates a new Capacity instance. 0029 @param p the Partition 0030 @param t type of Capacity 0031 */ 0032 Capacity::Capacity(const Partition& p, Type t) : 0033 m_Size(-1) 0034 { 0035 switch (t) { 0036 case Type::Used: m_Size = p.used(); break; 0037 case Type::Available: m_Size = p.available(); break; 0038 case Type::Total: m_Size = p.capacity(); 0039 } 0040 } 0041 0042 /** Creates a new Capacity instance. 0043 @param d the Device 0044 */ 0045 Capacity::Capacity(const Device& d) : 0046 m_Size(d.capacity()) 0047 { 0048 } 0049 0050 /** Returns the Capacity as qint64 converted to the given Unit. 0051 @param u the Unit to use 0052 @return the Capacity in the given Unit as qint64 0053 */ 0054 qint64 Capacity::toInt(Unit u) const 0055 { 0056 return static_cast<qint64>(m_Size / unitFactor(Unit::Byte, u)); 0057 } 0058 0059 /** Returns the Capacity as double converted to the given Unit. 0060 @param u the Unit to use 0061 @return the Capacity in the given Unit as double 0062 */ 0063 double Capacity::toDouble(Unit u) const 0064 { 0065 return static_cast<double>(m_Size) / unitFactor(Unit::Byte, u); 0066 } 0067 0068 /** Returns a factor to convert between two Units. 0069 @param from the Unit to convert from 0070 @param to the Unit to convert to 0071 @return the factor to use for conversion 0072 */ 0073 qint64 Capacity::unitFactor(Unit from, Unit to) 0074 { 0075 Q_ASSERT(from <= to); 0076 0077 if (from > to) { 0078 qWarning() << "from: " << static_cast<uint>(from) << ", to: " << static_cast<uint>(to); 0079 return 1; 0080 } 0081 0082 qint64 result = 1; 0083 0084 qint32 a = static_cast<uint>(from); 0085 qint32 b = static_cast<uint>(to); 0086 0087 while (b-- > a) 0088 result *= 1024; 0089 0090 return result; 0091 } 0092 0093 /** Returns the name of a given Unit. 0094 @param u the Unit to find the name for 0095 @return the name 0096 */ 0097 QString Capacity::unitName(Unit u, qint64 val) 0098 { 0099 static QString unitNames[] = { 0100 xi18ncp("@item:intext unit", "Byte", "Bytes", val), 0101 xi18nc("@item:intext unit", "KiB"), 0102 xi18nc("@item:intext unit", "MiB"), 0103 xi18nc("@item:intext unit", "GiB"), 0104 xi18nc("@item:intext unit", "TiB"), 0105 xi18nc("@item:intext unit", "PiB"), 0106 xi18nc("@item:intext unit", "EiB"), 0107 xi18nc("@item:intext unit", "ZiB"), 0108 xi18nc("@item:intext unit", "YiB") 0109 }; 0110 0111 if (static_cast<quint32>(u) >= sizeof(unitNames) / sizeof(unitNames[0])) 0112 return xi18nc("@item:intext unit", "(unknown unit)"); 0113 0114 return unitNames[static_cast<quint32>(u)]; 0115 } 0116 0117 /** Determine if the capacity is valid. 0118 @return true if it is valid 0119 */ 0120 bool Capacity::isValid() const 0121 { 0122 return m_Size >= 0; 0123 } 0124 0125 QString Capacity::formatByteSize(double size, int precision) 0126 { 0127 if (size < 0) 0128 return invalidString(); 0129 return KFormat().formatByteSize(size, precision); 0130 }