File: unitmodel.cpp

package info (click to toggle)
kde-config-systemd 1.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,560 kB
  • ctags: 688
  • sloc: cpp: 3,306; makefile: 9; sh: 2
file content (295 lines) | stat: -rw-r--r-- 9,363 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*******************************************************************************
 * Copyright (C) 2013-2015 Ragnar Thomsen <rthomsen6@gmail.com>                *
 *                                                                             *
 * 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 <http://www.gnu.org/licenses/>.              *
 *******************************************************************************/

#include "unitmodel.h"

#include <QtDBus/QtDBus>
#include <QColor>
#include <KLocalizedString>
#include <KColorScheme>

#include <systemd/sd-journal.h>

UnitModel::UnitModel(QObject *parent)
 : QAbstractTableModel(parent)
{
}

UnitModel::UnitModel(QObject *parent, const QList<SystemdUnit> *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("<FONT COLOR=white>");
    toolTipText.append("<b>" + selUnit + "</b><hr>");

    // 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("<b>Description: </b>"));
        toolTipText.append(iface->property("Description").toString());
        toolTipText.append(i18n("<br><b>Unit file: </b>"));
        toolTipText.append(iface->property("FragmentPath").toString());
        toolTipText.append(i18n("<br><b>Unit file state: </b>"));
        toolTipText.append(iface->property("UnitFileState").toString());

        qulonglong ActiveEnterTimestamp = iface->property("ActiveEnterTimestamp").toULongLong();
        toolTipText.append(i18n("<br><b>Activated: </b>"));
        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("<br><b>Deactivated: </b>"));
        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<QVariant> args;
      args << selUnit;

      toolTipText.append(i18n("<b>Unit file: </b>"));
      if (!selUnitFile.isEmpty())
        toolTipText.append(selUnitFile);

      toolTipText.append(i18n("<br><b>Unit file state: </b>"));
      if (!selUnitFile.isEmpty())
        toolTipText.append(iface->callWithArgumentList(QDBus::AutoDetect, "GetUnitFileState", args).arguments().at(0).toString());

      delete iface;
    }

    // Journal entries for units
    toolTipText.append(i18n("<hr><b>Last log entries:</b>"));
    QStringList log = getLastJrnlEntries(selUnit);
    if (log.isEmpty())
      toolTipText.append(i18n("<br><i>No log entries found for this unit.</i>"));
    else
    {
      for(int i = log.count()-1; i >= 0; --i)
      {
        if (!log.at(i).isEmpty())
          toolTipText.append(QString("<br>" + log.at(i)));
      }
    }

    toolTipText.append("</FONT");

    return toolTipText;
  }

  return QVariant();
}

QStringList UnitModel::getLastJrnlEntries(QString unit) const
{
  QString match1, match2;
  int r, jflags;
  QStringList reply;
  const void *data;
  size_t length;
  uint64_t time;
  sd_journal *journal;

  if (!userBus.isEmpty())
  {
    match1 = QString("USER_UNIT=" + unit);
    jflags = (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_CURRENT_USER);
  }
  else
  {
    match1 = QString("_SYSTEMD_UNIT=" + unit);
    match2 = QString("UNIT=" + unit);
    jflags = (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_SYSTEM);
  }

  r = sd_journal_open(&journal, jflags);
  if (r != 0)
  {
    qDebug() << "Failed to open journal";
    return reply;
  }

  sd_journal_flush_matches(journal);

  r = sd_journal_add_match(journal, match1.toUtf8(), 0);
  if (r != 0)
    return reply;

  if (!match2.isEmpty())
  {
    sd_journal_add_disjunction(journal);
    r = sd_journal_add_match(journal, match2.toUtf8(), 0);
    if (r != 0)
      return reply;
  }


  r = sd_journal_seek_tail(journal);
  if (r != 0)
    return reply;

  // Fetch the last 5 entries
  for (int i = 0; i < 5; ++i)
  {
    r = sd_journal_previous(journal);
    if (r == 1)
    {
      QString line;

      // Get the date and time
      r = sd_journal_get_realtime_usec(journal, &time);
      if (r == 0)
      {
        QDateTime date;
        date.setMSecsSinceEpoch(time/1000);
        line.append(date.toString("yyyy.MM.dd hh:mm"));
      }

      // Color messages according to priority
      r = sd_journal_get_data(journal, "PRIORITY", &data, &length);
      if (r == 0)
      {
        int prio = QString::fromUtf8((const char *)data, length).section('=',1).toInt();
        if (prio <= 3)
          line.append("<span style='color:tomato;'>");
        else if (prio == 4)
          line.append("<span style='color:khaki;'>");
        else
          line.append("<span style='color:palegreen;'>");
      }

      // 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) + "</span>");
        if (line.length() > 195)
          line = QString(line.left(195) + "..." + "</span>");
        reply << line;
      }
    }
    else // previous failed, no more entries
      return reply;
  }

  sd_journal_close(journal);

  return reply;
}