2 * Copyright (C) 2007 Petri Damsten <damu@iki.fi>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License version 2 as
6 * published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details
13 * You should have received a copy of the GNU Library General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "monitoricon.h"
21 #include <Plasma/Meter>
22 #include <Plasma/Containment>
23 #include <Plasma/Theme>
24 #include <KConfigDialog>
26 #include <QGraphicsLinearLayout>
28 Hdd::Hdd(QObject
*parent
, const QVariantList
&args
)
29 : SM::Applet(parent
, args
)
31 setHasConfigurationInterface(true);
32 setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Preferred
);
33 connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), this, SLOT(themeChanged()));
42 KConfigGroup cg
= persistentConfig();
43 QString
predicateString("IS StorageVolume");
44 setEngine(dataEngine("soliddevice"));
45 setItems(cg
.readEntry("uuids",
46 engine()->query(predicateString
)[predicateString
].toStringList()));
47 setInterval(cg
.readEntry("interval", 2) * 60 * 1000);
49 setTitle(i18n("Disk Space"), true);
53 void Hdd::createConfigurationInterface(KConfigDialog
*parent
)
55 QWidget
*widget
= new QWidget();
58 m_hddModel
.setHorizontalHeaderLabels(QStringList() << i18n("Mount Point")
60 QStandardItem
*parentItem
= m_hddModel
.invisibleRootItem();
61 Plasma::DataEngine::Data data
;
62 QString
predicateString("IS StorageVolume");
64 foreach (const QString
& uuid
, engine()->query(predicateString
)[predicateString
].toStringList()) {
65 if (!isValidDevice(uuid
, &data
)) {
68 QStandardItem
*item1
= new QStandardItem(data
["File Path"].toString());
69 item1
->setEditable(false);
70 item1
->setCheckable(true);
72 if (items().contains(uuid
)) {
73 item1
->setCheckState(Qt::Checked
);
75 QStandardItem
*item2
= new QStandardItem(title(uuid
, data
));
76 item2
->setEditable(true);
77 parentItem
->appendRow(QList
<QStandardItem
*>() << item1
<< item2
);
79 ui
.treeView
->setModel(&m_hddModel
);
80 ui
.treeView
->resizeColumnToContents(0);
81 ui
.intervalSpinBox
->setValue(interval() / 60 / 1000);
83 parent
->addPage(widget
, i18n("Partitions"), "drive-harddisk");
84 connect(parent
, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
85 connect(parent
, SIGNAL(okClicked()), this, SLOT(configAccepted()));
88 void Hdd::configAccepted()
90 KConfigGroup cg
= persistentConfig();
91 QStandardItem
*parentItem
= m_hddModel
.invisibleRootItem();
94 for (int i
= 0; i
< parentItem
->rowCount(); ++i
) {
95 QStandardItem
*item
= parentItem
->child(i
, 0);
97 cg
.writeEntry(item
->data().toString(),
98 parentItem
->child(i
, 1)->text());
99 if (item
->checkState() == Qt::Checked
) {
100 appendItem(item
->data().toString());
104 cg
.writeEntry("uuids", items());
106 uint interval
= ui
.intervalSpinBox
->value();
107 cg
.writeEntry("interval", interval
);
108 interval
*= 60 * 1000;
109 setInterval(interval
);
111 emit
configNeedsSaving();
115 QString
Hdd::title(const QString
& uuid
, const Plasma::DataEngine::Data
&data
)
117 KConfigGroup cg
= persistentConfig();
118 QString label
= cg
.readEntry(uuid
, "");
120 if (label
.isEmpty()) {
121 label
= data
["Label"].toString();
122 if (label
.isEmpty()) {
123 QString path
= data
["File Path"].toString();
125 return i18nc("the root filesystem", "root");
127 label
= fi
.fileName();
128 if (label
.isEmpty()) {
129 label
= data
["Device"].toString();
130 if (label
.isEmpty()) {
131 kDebug() << "Disk: " << uuid
<< " has empty label";
132 label
= i18n("Unknown filesystem");
137 QVariant accessible
= data
["Accessible"];
138 if (accessible
.isValid()) {
139 if (accessible
.canConvert(QVariant::Bool
)) {
140 if (!accessible
.toBool()) {
141 label
= i18nc("hard disk label (not mounted or accessible)",
142 "%1 (not accessible)", label
);
149 bool Hdd::addMeter(const QString
& source
)
152 Plasma::DataEngine
*engine
= dataEngine("soliddevice");
153 Plasma::DataEngine::Data data
;
154 Plasma::Theme
* theme
= Plasma::Theme::defaultTheme();
159 if (!isValidDevice(source
, &data
)) {
160 // do not try to show hard drives and swap partitions.
163 QGraphicsLinearLayout
*layout
= new QGraphicsLinearLayout(Qt::Horizontal
);
164 layout
->setContentsMargins(3, 3, 3, 3);
165 layout
->setSpacing(5);
167 w
= new Plasma::Meter(this);
168 w
->setMeterType(Plasma::Meter::BarMeterHorizontal
);
169 if (mode() != SM::Applet::Panel
) {
170 MonitorIcon
*icon
= new MonitorIcon(this);
171 m_icons
.insert(source
, icon
);
172 icon
->setImage("drive-harddisk");
173 if (data
["Accessible"].toBool()) {
174 QStringList overlays
;
175 overlays
<< QString("emblem-mounted");
176 icon
->setOverlays(overlays
);
178 layout
->addItem(icon
);
180 w
->setSvg("system-monitor/hdd_panel");
182 QColor text
= theme
->color(Plasma::Theme::TextColor
);
183 QColor background
= theme
->color(Plasma::Theme::BackgroundColor
);
184 QColor
darkerText((text
.red() + background
.red()) / 2,
185 (text
.green() + background
.green()) / 2,
186 (text
.blue() + background
.blue()) / 2,
187 (text
.alpha() + background
.alpha()) / 2);
188 w
->setLabel(0, title(source
, data
));
189 w
->setLabelColor(0, text
);
190 w
->setLabelColor(1, darkerText
);
191 w
->setLabelColor(2, darkerText
);
192 QFont font
= theme
->font(Plasma::Theme::DefaultFont
);
193 font
.setPointSize(9);
194 w
->setLabelFont(0, font
);
195 font
.setPointSizeF(7.5);
196 w
->setLabelFont(1, font
);
197 w
->setLabelFont(2, font
);
198 w
->setLabelAlignment(0, Qt::AlignVCenter
| Qt::AlignLeft
);
199 w
->setLabelAlignment(1, Qt::AlignVCenter
| Qt::AlignRight
);
200 w
->setLabelAlignment(2, Qt::AlignVCenter
| Qt::AlignCenter
);
201 w
->setMaximum(data
["Size"].toULongLong() / (1024 * 1024));
203 appendMeter(source
, w
);
204 mainLayout()->addItem(layout
);
205 dataUpdated(source
, data
);
206 setPreferredItemHeight(layout
->preferredSize().height());
208 QString disk
= data
["Parent UDI"].toString();
210 m_diskMap
[disk
] << w
;
211 if (!connectedSources().contains(disk
)) {
212 data
= engine
->query(disk
);
213 dataUpdated(disk
, data
);
219 void Hdd::themeChanged()
221 Plasma::Theme
* theme
= Plasma::Theme::defaultTheme();
222 foreach (Plasma::Meter
*w
, meters().values()) {
223 QColor text
= theme
->color(Plasma::Theme::TextColor
);
224 QColor background
= theme
->color(Plasma::Theme::BackgroundColor
);
225 QColor
darkerText((text
.red() + background
.red()) / 2,
226 (text
.green() + background
.green()) / 2,
227 (text
.blue() + background
.blue()) / 2,
228 (text
.alpha() + background
.alpha()) / 2);
229 w
->setLabelColor(0, text
);
230 w
->setLabelColor(1, darkerText
);
231 w
->setLabelColor(2, darkerText
);
232 QFont font
= theme
->font(Plasma::Theme::DefaultFont
);
233 font
.setPointSize(9);
234 w
->setLabelFont(0, font
);
235 font
.setPointSizeF(7.5);
236 w
->setLabelFont(1, font
);
237 w
->setLabelFont(2, font
);
241 void Hdd::deleteMeters(QGraphicsLinearLayout
* layout
)
243 Applet::deleteMeters(layout
);
247 bool Hdd::isValidDevice(const QString
& uuid
, Plasma::DataEngine::Data
* data
)
249 Plasma::DataEngine
*engine
= dataEngine("soliddevice");
251 *data
= engine
->query(uuid
);
253 kDebug() << uuid << data->value("Device").toString() <<
254 data->value("Usage").toString() <<
255 data->value("File System Type").toString() <<
256 data->value("Size").toString();
258 if ((data
->value("Usage").toString() != "File System" &&
259 data
->value("Usage").toString() != "Raid") ||
260 data
->value("File System Type").toString() == "swap") {
261 QStringList list
= items();
262 list
.removeAll(uuid
);
271 void Hdd::dataUpdated(const QString
& source
,
272 const Plasma::DataEngine::Data
&data
)
274 if (m_diskMap
.keys().contains(source
) && mode() != SM::Applet::Panel
) {
275 if (data
.keys().contains("Temperature")) {
276 QList
<Plasma::Meter
*> widgets
= m_diskMap
[source
];
277 foreach (Plasma::Meter
*w
, widgets
) {
278 w
->setLabel(2, QString("%1\xb0%2").arg(data
["Temperature"].toString())
279 .arg(data
["Temperature Unit"].toString()));
283 Plasma::Meter
*w
= meters().value(source
);
287 qulonglong size
= qulonglong(data
["Size"].toULongLong() /
289 qlonglong availBytes
= 0;
290 QVariant freeSpace
= data
["Free Space"];
291 if (freeSpace
.isValid()) {
292 if (freeSpace
.canConvert(QVariant::LongLong
)) {
293 availBytes
= qlonglong(freeSpace
.toLongLong());
294 w
->setValue(size
- availBytes
/ (1024 * 1024));
300 if (mode() != SM::Applet::Panel
) {
301 w
->setLabel(1, KGlobal::locale()->formatByteSize(availBytes
));
302 QStringList overlays
;
303 if (data
["Accessible"].toBool()) {
304 overlays
<< "emblem-mounted";
306 m_icons
[source
]->setOverlays(overlays
);