/******************************************************************************* * Copyright (C) 2013-2015 Ragnar Thomsen * * * * This program is free software: you can redistribute it and/or modify it * * under the terms of the GNU General Public License as published by the Free * * Software Foundation, either version 2 of the License, or (at your option) * * any later version. * * * * This program is distributed in the hope that it will be useful, but WITHOUT * * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * * more details. * * * * You should have received a copy of the GNU General Public License along * * with this program. If not, see . * *******************************************************************************/ #include "unitmodel.h" #include #include #include #include #include UnitModel::UnitModel(QObject *parent) : QAbstractTableModel(parent) { } UnitModel::UnitModel(QObject *parent, const QList *list, QString userBusPath) : QAbstractTableModel(parent) { unitList = list; userBus = userBusPath; } int UnitModel::rowCount(const QModelIndex &) const { return unitList->size(); } int UnitModel::columnCount(const QModelIndex &) const { return 4; } QVariant UnitModel::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation == Qt::Horizontal && role == Qt::DisplayRole && section == 0) return i18n("Load State"); if (orientation == Qt::Horizontal && role == Qt::DisplayRole && section == 1) return i18n("Active State"); if (orientation == Qt::Horizontal && role == Qt::DisplayRole && section == 2) return i18n("Unit State"); if (orientation == Qt::Horizontal && role == Qt::DisplayRole && section == 3) return i18n("Unit"); return QVariant(); } QVariant UnitModel::data(const QModelIndex & index, int role) const { if (!index.isValid()) return QVariant(); if (role == Qt::DisplayRole) { if (index.column() == 0) return unitList->at(index.row()).load_state; else if (index.column() == 1) return unitList->at(index.row()).active_state; else if (index.column() == 2) return unitList->at(index.row()).sub_state; else if (index.column() == 3) return unitList->at(index.row()).id; } else if (role == Qt::ForegroundRole) { const KColorScheme scheme(QPalette::Normal); if (unitList->at(index.row()).active_state == "active") return scheme.foreground(KColorScheme::PositiveText); else if (unitList->at(index.row()).active_state == "failed") return scheme.foreground(KColorScheme::NegativeText); else if (unitList->at(index.row()).active_state == "-") return scheme.foreground(KColorScheme::InactiveText); else return QVariant(); } else if (role == Qt::ToolTipRole) { QString selUnit = unitList->at(index.row()).id; QString selUnitPath = unitList->at(index.row()).unit_path.path(); QString selUnitFile = unitList->at(index.row()).unit_file; QString toolTipText; toolTipText.append(""); toolTipText.append("" + selUnit + "
"); // Create a DBus interface QDBusConnection bus(""); if (!userBus.isEmpty()) bus = QDBusConnection::connectToBus(userBus, "org.freedesktop.systemd1"); else bus = QDBusConnection::systemBus(); QDBusInterface *iface; // Use the DBus interface to get unit properties if (!selUnitPath.isEmpty()) { // Unit has a valid path iface = new QDBusInterface ("org.freedesktop.systemd1", selUnitPath, "org.freedesktop.systemd1.Unit", bus); if (iface->isValid()) { // Unit has a valid unit DBus object toolTipText.append(i18n("Description: ")); toolTipText.append(iface->property("Description").toString()); toolTipText.append(i18n("
Unit file: ")); toolTipText.append(iface->property("FragmentPath").toString()); toolTipText.append(i18n("
Unit file state: ")); toolTipText.append(iface->property("UnitFileState").toString()); qulonglong ActiveEnterTimestamp = iface->property("ActiveEnterTimestamp").toULongLong(); toolTipText.append(i18n("
Activated: ")); if (ActiveEnterTimestamp == 0) toolTipText.append("n/a"); else { QDateTime timeActivated; timeActivated.setMSecsSinceEpoch(ActiveEnterTimestamp/1000); toolTipText.append(timeActivated.toString()); } qulonglong InactiveEnterTimestamp = iface->property("InactiveEnterTimestamp").toULongLong(); toolTipText.append(i18n("
Deactivated: ")); if (InactiveEnterTimestamp == 0) toolTipText.append("n/a"); else { QDateTime timeDeactivated; timeDeactivated.setMSecsSinceEpoch(InactiveEnterTimestamp/1000); toolTipText.append(timeDeactivated.toString()); } } delete iface; } else { // Unit does not have a valid unit DBus object // Retrieve UnitFileState from Manager object iface = new QDBusInterface ("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", bus); QList args; args << selUnit; toolTipText.append(i18n("Unit file: ")); if (!selUnitFile.isEmpty()) toolTipText.append(selUnitFile); toolTipText.append(i18n("
Unit file state: ")); if (!selUnitFile.isEmpty()) toolTipText.append(iface->callWithArgumentList(QDBus::AutoDetect, "GetUnitFileState", args).arguments().at(0).toString()); delete iface; } // Journal entries for units toolTipText.append(i18n("
Last log entries:")); QStringList log = getLastJrnlEntries(selUnit); if (log.isEmpty()) toolTipText.append(i18n("
No log entries found for this unit.")); else { for(int i = log.count()-1; i >= 0; --i) { if (!log.at(i).isEmpty()) toolTipText.append(QString("
" + log.at(i))); } } toolTipText.append(""); else if (prio == 4) line.append(""); else line.append(""); } // Get the message itself r = sd_journal_get_data(journal, "MESSAGE", &data, &length); if (r == 0) { line.append(": " + QString::fromUtf8((const char *)data, length).section('=',1) + ""); if (line.length() > 195) line = QString(line.left(195) + "..." + ""); reply << line; } } else // previous failed, no more entries return reply; } sd_journal_close(journal); return reply; }