00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "hdd.h"
00020 #include "monitoricon.h"
00021 #include <Plasma/Meter>
00022 #include <Plasma/Containment>
00023 #include <Plasma/Theme>
00024 #include <KConfigDialog>
00025 #include <QFileInfo>
00026 #include <QGraphicsLinearLayout>
00027
00028 Hdd::Hdd(QObject *parent, const QVariantList &args)
00029 : SM::Applet(parent, args)
00030 {
00031 setHasConfigurationInterface(true);
00032 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
00033 connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), this, SLOT(themeChanged()));
00034 }
00035
00036 Hdd::~Hdd()
00037 {
00038 }
00039
00040 void Hdd::init()
00041 {
00042 KConfigGroup cg = persistentConfig();
00043 QString predicateString("IS StorageVolume");
00044 setEngine(dataEngine("soliddevice"));
00045 setItems(cg.readEntry("uuids",
00046 engine()->query(predicateString)[predicateString].toStringList()));
00047 setInterval(cg.readEntry("interval", 2) * 60 * 1000);
00048
00049 setTitle(i18n("Disk Space"), true);
00050 connectToEngine();
00051 }
00052
00053 void Hdd::createConfigurationInterface(KConfigDialog *parent)
00054 {
00055 QWidget *widget = new QWidget();
00056 ui.setupUi(widget);
00057 m_hddModel.clear();
00058 m_hddModel.setHorizontalHeaderLabels(QStringList() << i18n("Mount Point")
00059 << i18n("Name"));
00060 QStandardItem *parentItem = m_hddModel.invisibleRootItem();
00061 Plasma::DataEngine::Data data;
00062 QString predicateString("IS StorageVolume");
00063
00064 foreach (const QString& uuid, engine()->query(predicateString)[predicateString].toStringList()) {
00065 if (!isValidDevice(uuid, &data)) {
00066 continue;
00067 }
00068 QStandardItem *item1 = new QStandardItem(data["File Path"].toString());
00069 item1->setEditable(false);
00070 item1->setCheckable(true);
00071 item1->setData(uuid);
00072 if (items().contains(uuid)) {
00073 item1->setCheckState(Qt::Checked);
00074 }
00075 QStandardItem *item2 = new QStandardItem(title(uuid, data));
00076 item2->setEditable(true);
00077 parentItem->appendRow(QList<QStandardItem *>() << item1 << item2);
00078 }
00079 ui.treeView->setModel(&m_hddModel);
00080 ui.treeView->resizeColumnToContents(0);
00081 ui.intervalSpinBox->setValue(interval() / 60 / 1000);
00082
00083 parent->addPage(widget, i18n("Partitions"), "drive-harddisk");
00084 connect(parent, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
00085 connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted()));
00086 }
00087
00088 void Hdd::configAccepted()
00089 {
00090 KConfigGroup cg = persistentConfig();
00091 QStandardItem *parentItem = m_hddModel.invisibleRootItem();
00092
00093 clearItems();
00094 for (int i = 0; i < parentItem->rowCount(); ++i) {
00095 QStandardItem *item = parentItem->child(i, 0);
00096 if (item) {
00097 cg.writeEntry(item->data().toString(),
00098 parentItem->child(i, 1)->text());
00099 if (item->checkState() == Qt::Checked) {
00100 appendItem(item->data().toString());
00101 }
00102 }
00103 }
00104 cg.writeEntry("uuids", items());
00105
00106 uint interval = ui.intervalSpinBox->value();
00107 cg.writeEntry("interval", interval);
00108 interval *= 60 * 1000;
00109 setInterval(interval);
00110
00111 emit configNeedsSaving();
00112 connectToEngine();
00113 }
00114
00115 QString Hdd::title(const QString& uuid, const Plasma::DataEngine::Data &data)
00116 {
00117 KConfigGroup cg = persistentConfig();
00118 QString label = cg.readEntry(uuid, "");
00119
00120 if (label.isEmpty()) {
00121 label = data["Label"].toString();
00122 if (label.isEmpty()) {
00123 QString path = data["File Path"].toString();
00124 if (path == "/")
00125 return i18nc("the root filesystem", "root");
00126 QFileInfo fi(path);
00127 label = fi.fileName();
00128 if (label.isEmpty()) {
00129 label = data["Device"].toString();
00130 if (label.isEmpty()) {
00131 kDebug() << "Disk: " << uuid << " has empty label";
00132 label = i18n("Unknown filesystem");
00133 }
00134 }
00135 }
00136 }
00137 QVariant accessible = data["Accessible"];
00138 if (accessible.isValid()) {
00139 if (accessible.canConvert(QVariant::Bool)) {
00140 if (!accessible.toBool()) {
00141 label = i18nc("hard disk label (not mounted or accessible)",
00142 "%1 (not accessible)", label);
00143 }
00144 }
00145 }
00146 return label;
00147 }
00148
00149 bool Hdd::addMeter(const QString& source)
00150 {
00151 Plasma::Meter *w;
00152 Plasma::DataEngine *engine = dataEngine("soliddevice");
00153 Plasma::DataEngine::Data data;
00154 Plasma::Theme* theme = Plasma::Theme::defaultTheme();
00155
00156 if (!engine) {
00157 return false;
00158 }
00159 if (!isValidDevice(source, &data)) {
00160
00161 return false;
00162 }
00163 QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Horizontal);
00164 layout->setContentsMargins(3, 3, 3, 3);
00165 layout->setSpacing(5);
00166
00167 w = new Plasma::Meter(this);
00168 w->setMeterType(Plasma::Meter::BarMeterHorizontal);
00169 if (mode() != SM::Applet::Panel) {
00170 MonitorIcon *icon = new MonitorIcon(this);
00171 m_icons.insert(source, icon);
00172 icon->setImage("drive-harddisk");
00173 if (data["Accessible"].toBool()) {
00174 QStringList overlays;
00175 overlays << QString("emblem-mounted");
00176 icon->setOverlays(overlays);
00177 }
00178 layout->addItem(icon);
00179 } else {
00180 w->setSvg("system-monitor/hdd_panel");
00181 }
00182 QColor text = theme->color(Plasma::Theme::TextColor);
00183 QColor background = theme->color(Plasma::Theme::BackgroundColor);
00184 QColor darkerText((text.red() + background.red()) / 2,
00185 (text.green() + background.green()) / 2,
00186 (text.blue() + background.blue()) / 2,
00187 (text.alpha() + background.alpha()) / 2);
00188 w->setLabel(0, title(source, data));
00189 w->setLabelColor(0, text);
00190 w->setLabelColor(1, darkerText);
00191 w->setLabelColor(2, darkerText);
00192 QFont font = theme->font(Plasma::Theme::DefaultFont);
00193 font.setPointSize(9);
00194 w->setLabelFont(0, font);
00195 font.setPointSizeF(7.5);
00196 w->setLabelFont(1, font);
00197 w->setLabelFont(2, font);
00198 w->setLabelAlignment(0, Qt::AlignVCenter | Qt::AlignLeft);
00199 w->setLabelAlignment(1, Qt::AlignVCenter | Qt::AlignRight);
00200 w->setLabelAlignment(2, Qt::AlignVCenter | Qt::AlignCenter);
00201 w->setMaximum(data["Size"].toULongLong() / (1024 * 1024));
00202 layout->addItem(w);
00203 appendMeter(source, w);
00204 mainLayout()->addItem(layout);
00205 dataUpdated(source, data);
00206 setPreferredItemHeight(layout->preferredSize().height());
00207
00208 QString disk = data["Parent UDI"].toString();
00209
00210 m_diskMap[disk] << w;
00211 if (!connectedSources().contains(disk)) {
00212 data = engine->query(disk);
00213 dataUpdated(disk, data);
00214 connectSource(disk);
00215 }
00216 return true;
00217 }
00218
00219 void Hdd::themeChanged()
00220 {
00221 Plasma::Theme* theme = Plasma::Theme::defaultTheme();
00222 foreach (Plasma::Meter *w, meters().values()) {
00223 QColor text = theme->color(Plasma::Theme::TextColor);
00224 QColor background = theme->color(Plasma::Theme::BackgroundColor);
00225 QColor darkerText((text.red() + background.red()) / 2,
00226 (text.green() + background.green()) / 2,
00227 (text.blue() + background.blue()) / 2,
00228 (text.alpha() + background.alpha()) / 2);
00229 w->setLabelColor(0, text);
00230 w->setLabelColor(1, darkerText);
00231 w->setLabelColor(2, darkerText);
00232 QFont font = theme->font(Plasma::Theme::DefaultFont);
00233 font.setPointSize(9);
00234 w->setLabelFont(0, font);
00235 font.setPointSizeF(7.5);
00236 w->setLabelFont(1, font);
00237 w->setLabelFont(2, font);
00238 }
00239 }
00240
00241 void Hdd::deleteMeters(QGraphicsLinearLayout* layout)
00242 {
00243 Applet::deleteMeters(layout);
00244 m_diskMap.clear();
00245 }
00246
00247 bool Hdd::isValidDevice(const QString& uuid, Plasma::DataEngine::Data* data)
00248 {
00249 Plasma::DataEngine *engine = dataEngine("soliddevice");
00250 if (engine) {
00251 *data = engine->query(uuid);
00252
00253
00254
00255
00256
00257
00258 if ((data->value("Usage").toString() != "File System" &&
00259 data->value("Usage").toString() != "Raid") ||
00260 data->value("File System Type").toString() == "swap") {
00261 QStringList list = items();
00262 list.removeAll(uuid);
00263 setItems(list);
00264 return false;
00265 }
00266 return true;
00267 }
00268 return false;
00269 }
00270
00271 void Hdd::dataUpdated(const QString& source,
00272 const Plasma::DataEngine::Data &data)
00273 {
00274 if (m_diskMap.keys().contains(source) && mode() != SM::Applet::Panel) {
00275 if (data.keys().contains("Temperature")) {
00276 QList<Plasma::Meter *> widgets = m_diskMap[source];
00277 foreach (Plasma::Meter *w, widgets) {
00278 w->setLabel(2, QString("%1\xb0%2").arg(data["Temperature"].toString())
00279 .arg(data["Temperature Unit"].toString()));
00280 }
00281 }
00282 } else {
00283 Plasma::Meter *w = meters().value(source);
00284 if (!w) {
00285 return;
00286 }
00287 qulonglong size = qulonglong(data["Size"].toULongLong() /
00288 (1024 * 1024));
00289 qlonglong availBytes = 0;
00290 QVariant freeSpace = data["Free Space"];
00291 if (freeSpace.isValid()) {
00292 if (freeSpace.canConvert(QVariant::LongLong)) {
00293 availBytes = qlonglong(freeSpace.toLongLong());
00294 w->setValue(size - availBytes / (1024 * 1024));
00295 }
00296 }
00297 else {
00298 w->setValue(0);
00299 }
00300 if (mode() != SM::Applet::Panel) {
00301 w->setLabel(1, KGlobal::locale()->formatByteSize(availBytes));
00302 QStringList overlays;
00303 if (data["Accessible"].toBool()) {
00304 overlays << "emblem-mounted";
00305 }
00306 m_icons[source]->setOverlays(overlays);
00307 }
00308 }
00309 }
00310
00311 #include "hdd.moc"