2 * Copyright (C) 2008 Nicolas Ternisien <nicolas.ternisien@gmail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "kcm_partition.h"
21 #include <QStringList>
23 #include <KPluginFactory>
24 #include <KPluginLoader>
26 #include <kaboutdata.h>
35 #include <QVBoxLayout>
36 #include <QHBoxLayout>
37 #include <QTreeWidget>
38 #include <QTreeWidgetItemIterator>
42 #include <kseparator.h>
43 #include <kdiskfreespace.h>
45 #include <solid/device.h>
46 #include <solid/deviceinterface.h>
48 #include "usedSizeWidget.h"
50 K_PLUGIN_FACTORY(KCMPartitionFactory
,
51 registerPlugin
<KCMPartition
>();
53 K_EXPORT_PLUGIN(KCMPartitionFactory("kcm_partition"))
55 KCMPartition::KCMPartition(QWidget
*parent
, const QVariantList
&) :
56 KCModule(KCMPartitionFactory::componentData(), parent
) {
58 KAboutData
*about
= new KAboutData(I18N_NOOP("kcm_partition"), 0,
59 ki18n("KDE Partitions Information Control Module"),
60 0, KLocalizedString(), KAboutData::License_GPL
,
61 ki18n( "(c) 2008 Nicolas Ternisien"));
63 about
->addAuthor(ki18n("Nicolas Ternisien"), KLocalizedString(), "nicolas.ternisien@gmail.com");
66 QHBoxLayout
* layout
= new QHBoxLayout(this);
67 layout
->setSpacing(0);
70 tree
= new QTreeWidget(this);
71 layout
->addWidget(tree
);
72 tree
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
73 tree
->setAllColumnsShowFocus(true);
74 tree
->setRootIsDecorated(true);
75 tree
->setAlternatingRowColors(true);
76 tree
->setSortingEnabled(true);
77 tree
->setWhatsThis(i18n("This list displays partitions of your system.") );
80 headers
<< i18n("Mount Point") << i18n("Label") << i18n("Type") << i18n("Total Size") << i18n("Free Size") << i18n("Used Size");
81 tree
->setHeaderLabels(headers
);
85 KCMPartition::~KCMPartition() {
89 void KCMPartition::load() {
90 kDebug() << "Loading partition information..." << endl
;
92 QMap
<QString
, QTreeWidgetItem
*> rootDevicesItem
;
95 QList
<Solid::Device
> devices
= Solid::Device::listFromType(Solid::DeviceInterface::StorageDrive
, QString());
97 //Root Devices (Hard Disks, CD-Rom drives,...)
98 foreach(const Solid::Device
&device
, devices
) {
99 if(device
.is
<Solid::StorageDrive
>() == false) {
103 const Solid::StorageDrive
* drive
= device
.as
<Solid::StorageDrive
>();
105 QStringList itemContent
;
107 itemContent
<< i18nc("Device (Vendor)", "%1 (%2)", device
.product(), device
.vendor()) << findDriveType(drive
) << findBusType(drive
);
109 QTreeWidgetItem
* item
= new QTreeWidgetItem(tree
, itemContent
);
110 item
->setIcon(0, KIcon(device
.icon()));
111 item
->setExpanded(true);
113 rootDevicesItem
.insert(device
.udi(), item
);
117 QList
<Solid::Device
> storageVolumes
= Solid::Device::listFromType(Solid::DeviceInterface::StorageVolume
, QString());
118 QList
<QString
> accessPaths
;
120 foreach(const Solid::Device
&device
, storageVolumes
) {
122 if(device
.is
<Solid::StorageVolume
>() == false) {
126 const Solid::StorageVolume
* volume
= device
.as
<Solid::StorageVolume
>();
128 QStringList itemContent
;
130 if (device
.is
<Solid::StorageAccess
>()) {
131 const Solid::StorageAccess
* access
= device
.as
<Solid::StorageAccess
>();
132 itemContent
<< access
->filePath();
134 accessPaths
.append(access
->filePath());
137 itemContent
<< i18n("none");
140 itemContent
<< volume
->label() << volume
->fsType() << KGlobal::locale()->formatByteSize(volume
->size());
142 QTreeWidgetItem
* item
;
143 if (rootDevicesItem
.contains(device
.parentUdi())) {
144 QTreeWidgetItem
* root
= rootDevicesItem
.value(device
.parentUdi());
146 item
= new QTreeWidgetItem(root
, itemContent
);
149 item
= new QTreeWidgetItem(tree
, itemContent
);
152 item
->setIcon(0, KIcon(device
.icon()));
153 item
->setTextAlignment(TOTAL_SIZE_INDEX
, Qt::AlignRight
);
154 item
->setTextAlignment(FREE_SIZE_INDEX
, Qt::AlignRight
);
156 UsedSizeWidget
* usedSizeWidget
= new UsedSizeWidget(tree
);
157 usedSizeWidget
->setObjectName(device
.udi());
158 tree
->setItemWidget(item
, USED_SIZE_INDEX
, usedSizeWidget
);
161 UsedSizeWidget* otherUsedSizeWidget = (UsedSizeWidget*) tree->itemWidget(item, USED_SIZE_INDEX);
162 if (otherUsedSizeWidget==NULL) {
163 kError() << "BUG BUG BUG !!! Just added used size widget of " << itemContent << " is null !!!" << endl;
171 tree
->sortItems(0, Qt::AscendingOrder
);
173 //Do this later to be sure all UsedSizeWidget will be ready
174 foreach(const QString
&accessPath
, accessPaths
) {
175 connect(KDiskFreeSpace::findUsageInfo(accessPath
), SIGNAL(foundMountPoint (const QString
&, quint64
, quint64
, quint64
)), this, SLOT(mountPointInfo(const QString
&, quint64
, quint64
, quint64
)) );
180 QString
KCMPartition::findBusType(const Solid::StorageDrive
* drive
) const {
181 switch (drive
->bus()) {
182 case Solid::StorageDrive::Ide
:
184 case Solid::StorageDrive::Usb
:
186 case Solid::StorageDrive::Ieee1394
:
187 return i18n("IEEE 1394");
188 case Solid::StorageDrive::Scsi
:
190 case Solid::StorageDrive::Sata
:
191 return i18n("S-ATA");
192 case Solid::StorageDrive::Platform
:
193 return i18n("Platform");
196 return i18n("Unknown Bus");
199 QString
KCMPartition::findDriveType(const Solid::StorageDrive
* drive
) const {
200 switch (drive
->driveType()) {
201 case Solid::StorageDrive::HardDisk
:
202 return i18n("Hard Disk");
203 case Solid::StorageDrive::CdromDrive
:
204 return i18n("CD/DVD Drive");
205 case Solid::StorageDrive::Floppy
:
206 return i18n("Floppy Drive");
207 case Solid::StorageDrive::Tape
:
209 case Solid::StorageDrive::CompactFlash
:
210 return i18n("Compact Flash");
211 case Solid::StorageDrive::MemoryStick
:
212 return i18n("Memory Stick");
213 case Solid::StorageDrive::SmartMedia
:
214 return i18n("Smart Media");
215 case Solid::StorageDrive::SdMmc
:
216 return i18n("SD/MMC");
217 case Solid::StorageDrive::Xd
:
221 return i18n("Unknown Type");
224 QString
KCMPartition::quickHelp() const {
225 return i18n("This display shows information about partitions and hard disks of your system.");
228 void KCMPartition::mountPointInfo(const QString
& mountPoint
, quint64 kibSize
, quint64 kibUsed
, quint64 kibAvail
) {
230 QTreeWidgetItemIterator
it(tree
, QTreeWidgetItemIterator::All
);
231 while ( *it
!= NULL
) {
232 QTreeWidgetItem
* currentItem
= *it
;
234 if (currentItem
->text(0) == mountPoint
) {
235 currentItem
->setText(TOTAL_SIZE_INDEX
, KGlobal::locale()->formatByteSize(kibSize
*1000));
236 currentItem
->setText(FREE_SIZE_INDEX
, KGlobal::locale()->formatByteSize(kibAvail
*1000));
238 UsedSizeWidget
* usedSizeWidget
= (UsedSizeWidget
*) tree
->itemWidget(currentItem
, USED_SIZE_INDEX
);
240 if (usedSizeWidget
!=NULL
) {
241 usedSizeWidget
->setUsedSize(mountPoint
, kibSize
*1000, kibUsed
*1000, kibAvail
*1000);
245 kError() << "BUG BUG BUG AGAIN !!! Used Size Widget null for " << mountPoint << endl;
262 void KCMPartition::resizeContent() const {
263 for(int i
=0; i
<tree
->columnCount(); ++i
) {
264 tree
->resizeColumnToContents(i
);
268 #include "kcm_partition.moc"