not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / dataengines / soliddevice / hddtemp.cpp
blob1fd23ff80fa184bf27600da8aebe436d660bf07d
1 /*
2 * Copyright (C) 2007 Petri Damsten <damu@iki.fi>
3 * Copyright (C) 2007 Christopher Blauvelt <cblauvelt@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Library General Public License version 2 as
7 * published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "hddtemp.h"
22 #include <QTcpSocket>
24 #include <KDebug>
26 HddTemp::HddTemp(QObject* parent)
27 : QObject(parent),
28 m_failCount(0),
29 m_timer(0)
31 updateData();
32 m_timer = new QTimer(this);
33 connect(m_timer, SIGNAL(timeout()), this, SLOT(updateData()));
34 m_timer->start(10000);
37 HddTemp::~HddTemp()
41 QStringList HddTemp::sources() const
43 return m_data.keys();
46 bool HddTemp::updateData()
48 if (m_failCount > 4) {
49 return false;
51 QTcpSocket socket;
52 QString data;
54 socket.connectToHost("localhost", 7634);
55 if (socket.waitForConnected(500)) {
56 while (data.length() < 1024) {
57 if (!socket.waitForReadyRead(500)) {
58 if (data.length() > 0) {
59 break;
60 } else {
61 //kDebug() << socket.errorString();
62 return false;
65 data += QString(socket.readAll());
67 socket.disconnectFromHost();
68 //on success retry fail count
69 m_failCount = 0;
70 } else {
71 m_failCount++;
72 //kDebug() << socket.errorString();
73 return false;
75 QStringList list = data.split('|');
76 int i = 1;
77 m_data.clear();
78 while (i + 4 < list.size()) {
79 m_data[list[i]].append(list[i + 2]);
80 m_data[list[i]].append(list[i + 3]);
81 i += 5;
83 return true;
86 QVariant HddTemp::data(const QString source, const DataType type) const
88 return m_data[source][type];
91 #include "hddtemp.moc"