summaryrefslogtreecommitdiffstats
path: root/ksim/monitors/lm_sensors/ksimsensors.cpp
blob: bef854a03536ff579c14d2127082f0b33f89ca5e (plain)
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
/*  ksim - a system monitor for kde
 *
 *  Copyright (C) 2001  Robbie Ward <linuxphreak@gmx.co.uk>
 *
 *  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, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "ksimsensors.h"
#include "ksimsensors.moc"

#include <kdebug.h>
#include <tdeapplication.h>
#include <klocale.h>
#include <tdeaboutapplication.h>
#include <tdeaboutdata.h>
#include <tdeconfig.h>

#include <tqlayout.h>

#include <label.h>
#include <themetypes.h>
#include "sensorsconfig.h"
#include "sensorbase.h"

KSIM_INIT_PLUGIN(PluginModule)

PluginModule::PluginModule(const char *name)
           : KSim::PluginObject(name)
{
  setConfigFileName(instanceName());
}

PluginModule::~PluginModule()
{
}

KSim::PluginView *PluginModule::createView(const char *className)
{
  return new SensorsView(this, className);
}

KSim::PluginPage *PluginModule::createConfigPage(const char *className)
{
  return new SensorsConfig(this, className);
}

void PluginModule::showAbout()  
{
  TQString version = kapp->aboutData()->version();

  TDEAboutData aboutData(instanceName(),
     I18N_NOOP("KSim Sensors Plugin"), version.latin1(),
     I18N_NOOP("An lm_sensors plugin for KSim"),
     TDEAboutData::License_GPL, "(C) 2001 Robbie Ward");

  aboutData.addAuthor("Robbie Ward", I18N_NOOP("Author"),
     "linuxphreak@gmx.co.uk");

  TDEAboutApplication(&aboutData).exec();
}

SensorsView::SensorsView(KSim::PluginObject *parent, const char *name)
   : DCOPObject("sensors"), KSim::PluginView(parent, name)
{
  config()->setGroup("Sensors");
  (new TQVBoxLayout(this))->setAutoAdd(true);

  connect(SensorBase::self(), TQT_SIGNAL(updateSensors(const SensorList &)),
     this, TQT_SLOT(updateSensors(const SensorList &)));

  insertSensors();
}

SensorsView::~SensorsView()
{
}

void SensorsView::reparseConfig()
{
  config()->setGroup("Sensors");
  bool displayFahrenheit = config()->readBoolEntry("displayFahrenheit", false);
  int updateVal = config()->readNumEntry("sensorUpdateValue", 5);
  SensorBase::self()->setDisplayFahrenheit(displayFahrenheit);
  SensorBase::self()->setUpdateSpeed(updateVal * 1000);

  TQString label;
  TQStringList names;
  SensorItemList sensorItemList;
  const SensorList &list = SensorBase::self()->sensorsList();

  SensorList::ConstIterator it;
  for (it = list.begin(); it != list.end(); ++it) {
    config()->setGroup("Sensors");
    label = (*it).sensorType() + "/" + (*it).sensorName();
    names = TQStringList::split(':', config()->readEntry(label));
    if (names[0] == "1")
      sensorItemList.append(SensorItem((*it).sensorId(), names[1]));
  }

  if (sensorItemList == m_sensorItemList)
    return;

  m_sensorItemList.clear();
  m_sensorItemList = sensorItemList;
  insertSensors(false);
}

void SensorsView::insertSensors(bool createList)
{
  const SensorList &list = SensorBase::self()->sensorsList();

  if (createList) {
    TQString label;
    TQStringList names;

    config()->setGroup("Sensors");
    bool displayFahrenheit = config()->readBoolEntry("displayFahrenheit", false);
    int updateVal = config()->readNumEntry("sensorUpdateValue", 5);
    SensorBase::self()->setDisplayFahrenheit(displayFahrenheit);
    SensorBase::self()->setUpdateSpeed(updateVal * 1000);

    SensorList::ConstIterator it;
    for (it = list.begin(); it != list.end(); ++it) {
      label = (*it).sensorType() + "/" + (*it).sensorName();
      names = TQStringList::split(':', config()->readEntry(label));
      if (names[0] == "1")
        m_sensorItemList.append(SensorItem((*it).sensorId(), names[1]));
    }
  }

  SensorItemList::Iterator item;
  for (item = m_sensorItemList.begin(); item != m_sensorItemList.end(); ++item)
    (*item).setLabel(new KSim::Label(KSim::Types::None, this));

  updateSensors(list);
}

void SensorsView::updateSensors(const SensorList &sensorList)
{
  if (sensorList.isEmpty() || m_sensorItemList.isEmpty())
    return;

  SensorList::ConstIterator it;
  for (it = sensorList.begin(); it != sensorList.end(); ++it) {
    SensorItemList::Iterator item;
    for (item = m_sensorItemList.begin(); item != m_sensorItemList.end(); ++item) {
      if ((*item).id == (*it).sensorId()) {
        if (!(*item).label->isVisible())
          (*item).label->show();

        (*item).label->setText((*item).name + ": " +
           (*it).sensorValue() + (*it).sensorUnit());
      }
    }
  }
}

TQString SensorsView::sensorValue(const TQString &sensor,
    const TQString &label1)
{
  const SensorList &list = SensorBase::self()->sensorsList();
  config()->setGroup("Sensors");
  TQStringList names = TQStringList::split(':',
     config()->readEntry(sensor + "/" + label1));

  if (names[0] == "0" || list.isEmpty())
    return i18n("Sensor specified not found.");

  SensorList::ConstIterator it;
  for (it = list.begin(); it != list.end(); ++it) {
    if (sensor == (*it).sensorType() && label1 == (*it).sensorName()) {
      return names[1] + TQString(": ") + (*it).sensorValue() + (*it).sensorUnit();
    }
  }

  return i18n("Sensor specified not found.");
}