Skip to content

Commit ae7b98b

Browse files
committed
feat: normalize disk size
only for specific devices. Log: as above. Bug: https://pms.uniontech.com/bug-view-329231.html Task: https://pms.uniontech.com/task-view-368603.html
1 parent 39c0532 commit ae7b98b

3 files changed

Lines changed: 55 additions & 1 deletion

File tree

service/diskoperation/DeviceStorage.cpp

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ void DeviceStorage::getDiskInfoInterface(const QString &devicePath, QString &int
608608
return;
609609
}
610610

611-
void DeviceStorage::updateForHWDevice(const QString &devicePath)
611+
void DeviceStorage::updateForHWDevice(const QString &/*devicePath*/)
612612
{
613613
if (m_model != Utils::readContent("/proc/bootdevice/product_name").trimmed())
614614
return;
@@ -622,6 +622,58 @@ void DeviceStorage::updateForHWDevice(const QString &devicePath)
622622
// hide mode and vendor
623623
m_model = "";
624624
m_vendor = "";
625+
626+
// Normalize disk size
627+
if (!m_size.isEmpty()) {
628+
// Convert size string to bytes
629+
qint64 bytes = 0;
630+
QString sizeStr = m_size.toLower();
631+
632+
if (sizeStr.contains("bytes")) {
633+
sizeStr = sizeStr.split("bytes").first().trimmed();
634+
bytes = sizeStr.toLongLong();
635+
} else {
636+
// Handle GB/TB directly
637+
double value = sizeStr.split(" ").first().toDouble();
638+
if (sizeStr.contains("gb")) {
639+
bytes = value * 1000 * 1000 * 1000;
640+
} else if (sizeStr.contains("tb")) {
641+
bytes = value * 1000 * 1000 * 1000 * 1000;
642+
}
643+
}
644+
645+
// Convert to standardized format
646+
if (bytes > 0) {
647+
// Convert to GB and round to standard sizes
648+
int gb = qRound(bytes / (1000.0 * 1000 * 1000));
649+
if (gb <= 300) {
650+
m_size = "256 GB";
651+
} else if (gb <= 600) {
652+
m_size = "512 GB";
653+
} else if (gb <= 1200){
654+
m_size = "1 TB";
655+
} else if (gb <= 2200) {
656+
m_size = "2 TB";
657+
}
658+
}
659+
}
660+
}
661+
662+
void DeviceStorage::formatDeviceSize()
663+
{
664+
static QRegularExpression numRegex("(\\d+)(?:\\.\\d+)?\\s*(TB|GB|MB)");
665+
QRegularExpressionMatch match = numRegex.match(m_size);
666+
if (match.hasMatch()) {
667+
double number = match.captured(1).toDouble();
668+
QString unit = match.captured(2);
669+
// 如果小数部分为0,转换为整数显示,否则保留小数
670+
if(qFuzzyCompare(number, qRound(number))) {
671+
m_size = QString("%1 %2").arg(qRound(number)).arg(unit);
672+
} else {
673+
m_size = QString("%1 %2").arg(number, 0, 'f', 2).arg(unit);
674+
}
675+
return;
676+
}
625677
}
626678

627679
void DeviceStorage::getMapInfoFromSmartctl(QMap<QString, QString> &mapInfo, const QString &info, const QString &ch)

service/diskoperation/DeviceStorage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class DeviceStorage
3636

3737
/**@brief:为HW设备更新信息*/
3838
void updateForHWDevice(const QString &devicePath);
39+
void formatDeviceSize();
3940

4041

4142
private:

service/diskoperation/partedcore.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ HardDiskInfo PartedCore::getDeviceHardInfo(const QString &devicepath)
146146
device.getDiskInfoInterface(devicepath, device.m_interface, device.m_model);
147147

148148
device.updateForHWDevice(devicepath);
149+
device.formatDeviceSize();
149150

150151
hdinfo.m_model = device.m_model;
151152
hdinfo.m_vendor = device.m_vendor;

0 commit comments

Comments
 (0)