• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

superkaramba

  • sources
  • kde-4.12
  • kdeutils
  • superkaramba
  • src
  • sensors
program.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2003 by Hans Karlsson *
3  * karlsson.h@home.se *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  ***************************************************************************/
10 
11 #include "program.h"
12 #include "karamba.h"
13 
14 
15 ProgramSensor::ProgramSensor(Karamba* k, const QString &progName, int interval, const QString &encoding)
16  : Sensor(interval),
17  m_karamba(k)
18 {
19  if (!encoding.isEmpty()) {
20  codec = QTextCodec::codecForName(encoding.toAscii().constData());
21  if (codec == 0)
22  codec = QTextCodec::codecForLocale();
23  } else
24  codec = QTextCodec::codecForLocale();
25 
26 
27  programName = progName;
28  //update();
29  connect(&ksp, SIGNAL(receivedStdout(K3Process*,char*,int)),
30  this, SLOT(receivedStdout(K3Process*,char*,int)));
31  connect(&ksp, SIGNAL(processExited(K3Process*)),
32  this, SLOT(processExited(K3Process*)));
33 }
34 
35 ProgramSensor::~ProgramSensor()
36 {}
37 
38 void ProgramSensor::receivedStdout(K3Process *, char *buffer, int len)
39 {
40  buffer[len] = 0;
41  sensorResult += codec->toUnicode(buffer);
42 }
43 
44 void ProgramSensor::replaceLine(QString &format, const QString &line)
45 {
46  const QStringList tokens = line.split(QRegExp("\\s+"), QString::SkipEmptyParts);
47  QRegExp dblDigit("%(\\d\\d)");
48  replaceArgs(dblDigit, format, tokens);
49  QRegExp digit("%(\\d)");
50  replaceArgs(digit, format, tokens);
51 }
52 
53 void ProgramSensor::replaceArgs(QRegExp& regEx, QString& format, const QStringList& tokens)
54 {
55  int pos = 0;
56  while (pos >= 0) {
57  pos = regEx.indexIn(format, pos);
58  if (pos > -1) {
59  QString matched = regEx.cap(1);
60  int tokenIndex = matched.toInt() - 1;
61  QString replacement = "";
62  if (tokenIndex < tokens.size()) {
63  replacement = tokens.at(tokenIndex);
64  }
65  format.replace(QRegExp('%' + matched), replacement);
66  pos += regEx.matchedLength();
67  }
68  }
69 }
70 
71 
72 void ProgramSensor::processExited(K3Process *)
73 {
74  int lineNbr;
75  SensorParams *sp;
76  Meter *meter;
77  QString value;
78  QVector<QString> lines;
79  QStringList stringList = sensorResult.split('\n');
80  QStringList::ConstIterator end(stringList.constEnd());
81  for (QStringList::ConstIterator it = stringList.constBegin(); it != end; ++it) {
82  lines.push_back(*it);
83  }
84 
85  int count = (int) lines.size();
86  QObject *object;
87  foreach(object, *objList) {
88  sp = (SensorParams*)(object);
89  meter = sp->getMeter();
90  if (meter != 0) {
91  lineNbr = (sp->getParam("LINE")).toInt();
92  if (lineNbr >= 1 && lineNbr <= (int) count) {
93  value = lines[lineNbr-1];
94  } else if (-lineNbr >= 1 && -lineNbr <= (int) count) {
95  value = lines[count+lineNbr];
96  } else if (lineNbr != 0) {
97  value.clear();
98  } else {
99  value = sensorResult;
100  }
101 
102  QString format = sp->getParam("FORMAT");
103  if (!format.isEmpty()) {
104  QString returnValue;
105  QStringList lineList = value.split('\n');
106  QStringList::ConstIterator lineListEnd(lineList.constEnd());
107  for (QStringList::ConstIterator line = lineList.constBegin(); line != lineListEnd; ++line) {
108  QString formatCopy = format;
109  replaceLine(formatCopy, *line);
110  returnValue += formatCopy;
111  if ( lineList.size() > 1) {
112  returnValue += '\n';
113  }
114  }
115  value = returnValue;
116  }
117 
118  meter->setValue(value);
119  }
120  }
121 
122  sensorResult = "";
123 }
124 
125 void ProgramSensor::update()
126 {
127  QString prog = programName;
128  m_karamba->replaceNamedValues(&prog);
129  if ( prog.isEmpty() || prog.startsWith("%echo ")) {
130  sensorResult += prog.mid(6);
131  processExited(NULL);
132  } else {
133  ksp.clearArguments();
134  ksp << prog;
135 
136  ksp.start(K3ProcIO::NotifyOnExit, K3ProcIO::Stdout);
137  }
138 }
139 
140 #include "program.moc"
ProgramSensor::processExited
void processExited(K3Process *proc)
Definition: program.cpp:72
ProgramSensor::~ProgramSensor
~ProgramSensor()
Definition: program.cpp:35
SensorParams::getMeter
Meter * getMeter() const
Definition: sensorparams.cpp:31
QObject
Sensor
Definition: sensor.h:17
Karamba
Definition: karamba.h:52
program.h
Karamba::replaceNamedValues
void replaceNamedValues(QString *source)
Definition: karamba.cpp:2288
SensorParams
Hans Karlsson.
Definition: sensorparams.h:30
ProgramSensor::update
void update()
Definition: program.cpp:125
SensorParams::getParam
QString getParam(const QString &) const
Definition: sensorparams.cpp:26
Meter
Definition: meters/meter.h:23
Meter::setValue
virtual void setValue(int)
Definition: meters/meter.cpp:136
ProgramSensor::receivedStdout
void receivedStdout(K3Process *proc, char *buffer, int buflen)
Definition: program.cpp:38
ProgramSensor::ProgramSensor
ProgramSensor(Karamba *k, const QString &programName, int msec=1000, const QString &encoding=QString())
Definition: program.cpp:15
Sensor::objList
QList< QObject * > * objList
Definition: sensor.h:39
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:07:20 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

superkaramba

Skip menu "superkaramba"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • kremotecontrol
  • ktimer
  • kwallet
  • superkaramba
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal