File indexing completed on 2024-04-28 05:45:44

0001 /*
0002     SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
0003     SPDX-FileCopyrightText: 2012-2018 Andrius Štikonas <andrius@stikonas.eu>
0004     SPDX-FileCopyrightText: 2015 Teo Mrnjavac <teo@kde.org>
0005     SPDX-FileCopyrightText: 2016 Chantara Tith <tith.chantara@gmail.com>
0006 
0007     SPDX-License-Identifier: GPL-3.0-or-later
0008 */
0009 
0010 #include "core/device.h"
0011 #include "core/device_p.h"
0012 #include "core/partitiontable.h"
0013 #include "core/smartstatus.h"
0014 
0015 #include "util/capacity.h"
0016 
0017 #include <KLocalizedString>
0018 
0019 /** Constructs a Device with an empty PartitionTable.
0020     @param name the Device's name, usually some string defined by the manufacturer
0021     @param deviceNode the Device's node, for example "/dev/sda"
0022 */
0023 Device::Device(std::shared_ptr<DevicePrivate> d_ptr,
0024                const QString& name,
0025                const QString& deviceNode,
0026                const qint64 logicalSectorSize,
0027                const qint64 totalLogicalSectors,
0028                const QString& iconName,
0029                Device::Type type)
0030     : QObject()
0031     , d(d_ptr)
0032 {
0033     d->m_Name = name.length() > 0 ? name : i18n("Unknown Device");
0034     d->m_DeviceNode = deviceNode;
0035     d->m_LogicalSectorSize = logicalSectorSize;
0036     d->m_TotalLogical = totalLogicalSectors;
0037     d->m_PartitionTable = nullptr;
0038     d->m_IconName = iconName.isEmpty() ? QStringLiteral("drive-harddisk") : iconName;
0039     d->m_SmartStatus = type == Device::Type::Disk_Device ? std::make_shared<SmartStatus>(deviceNode) : nullptr;
0040     d->m_Type = type;
0041 }
0042 
0043 /** Copy constructor for Device.
0044  * @param other the other Device.
0045  */
0046 Device::Device(const Device& other)
0047     : QObject()
0048     , d(std::make_shared<DevicePrivate>())
0049 {
0050     d->m_Name = other.d->m_Name;
0051     d->m_DeviceNode = other.d->m_DeviceNode;
0052     d->m_LogicalSectorSize = other.d->m_LogicalSectorSize;
0053     d->m_TotalLogical = other.d->m_TotalLogical;
0054     d->m_PartitionTable = nullptr;
0055     d->m_IconName = other.d->m_IconName;
0056     d->m_SmartStatus = nullptr;
0057     d->m_Type = other.d->m_Type;
0058     d->m_SmartStatus = other.d->m_SmartStatus;
0059 
0060     if (other.d->m_PartitionTable)
0061         d->m_PartitionTable = new PartitionTable(*other.d->m_PartitionTable);
0062 }
0063 
0064 /** Destructs a Device. */
0065 Device::~Device()
0066 {
0067     delete d->m_PartitionTable;
0068 }
0069 
0070 bool Device::operator==(const Device& other) const
0071 {
0072     return d->m_DeviceNode == other.d->m_DeviceNode;
0073 }
0074 
0075 bool Device::operator!=(const Device& other) const
0076 {
0077     return !(other == *this);
0078 }
0079 
0080 QString Device::prettyName() const
0081 {
0082     return xi18nc("@item:inlistbox Device name – Capacity (device node)", "%1 – %2 (%3)", name(), Capacity::formatByteSize(capacity()), deviceNode());
0083 }
0084 
0085 QString& Device::name()
0086 {
0087     return d->m_Name;
0088 }
0089 
0090 const QString& Device::name() const
0091 {
0092     return d->m_Name;
0093 }
0094 
0095 const QString& Device::deviceNode() const
0096 {
0097     return d->m_DeviceNode;
0098 }
0099 
0100 qint64 Device::logicalSize() const
0101 {
0102     return d->m_LogicalSectorSize;
0103 }
0104 
0105 qint64 Device::totalLogical() const
0106 {
0107     return d->m_TotalLogical;
0108 }
0109 
0110 PartitionTable* Device::partitionTable()
0111 {
0112     return d->m_PartitionTable;
0113 }
0114 
0115 const PartitionTable* Device::partitionTable() const
0116 {
0117     return d->m_PartitionTable;
0118 }
0119 
0120 void Device::setPartitionTable(PartitionTable* ptable)
0121 {
0122     d->m_PartitionTable = ptable;
0123 }
0124 
0125 const QString& Device::iconName() const
0126 {
0127     return d->m_IconName;
0128 }
0129 
0130 void Device::setIconName(const QString& name)
0131 {
0132     d->m_IconName = name;
0133 }
0134 
0135 SmartStatus& Device::smartStatus()
0136 {
0137     return *(d->m_SmartStatus);
0138 }
0139 
0140 const SmartStatus& Device::smartStatus() const
0141 {
0142     return *(d->m_SmartStatus);
0143 }
0144 
0145 Device::Type Device::type() const
0146 {
0147     return d->m_Type;
0148 }