~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/dataengines/soliddevice/hddtemp.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright (C) 2007 Petri Damsten <damu@iki.fi>
 
3
 *   Copyright (C) 2007 Christopher Blauvelt <cblauvelt@gmail.com>
 
4
 *
 
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
 
8
 *
 
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
 
13
 *
 
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.
 
18
 */
 
19
 
 
20
#include "hddtemp.h"
 
21
 
 
22
#include <QTcpSocket>
 
23
 
 
24
#include <QTimerEvent>
 
25
 
 
26
#include <KDebug>
 
27
 
 
28
HddTemp::HddTemp(QObject* parent)
 
29
    : QObject(parent),
 
30
      m_failCount(0),
 
31
      m_cacheValid(false)
 
32
{
 
33
    updateData();
 
34
}
 
35
 
 
36
HddTemp::~HddTemp()
 
37
{
 
38
}
 
39
 
 
40
QStringList HddTemp::sources()
 
41
{
 
42
    updateData();
 
43
    return m_data.keys();
 
44
}
 
45
 
 
46
void HddTemp::timerEvent(QTimerEvent *event)
 
47
{
 
48
    killTimer(event->timerId());
 
49
    m_cacheValid = false;
 
50
}
 
51
 
 
52
bool HddTemp::updateData()
 
53
{
 
54
    if (m_cacheValid) {
 
55
        return true;
 
56
    }
 
57
    if (m_failCount > 4) {
 
58
        return false;
 
59
    }
 
60
    QTcpSocket socket;
 
61
    QString data;
 
62
 
 
63
    socket.connectToHost("localhost", 7634);
 
64
    if (socket.waitForConnected(500)) {
 
65
        while (data.length() < 1024) {
 
66
            if (!socket.waitForReadyRead(500)) {
 
67
                if (data.length() > 0) {
 
68
                    break;
 
69
                } else {
 
70
                    //kDebug() << socket.errorString();
 
71
                    return false;
 
72
                }
 
73
            }
 
74
            data += QString(socket.readAll());
 
75
        }
 
76
        socket.disconnectFromHost();
 
77
        //on success retry fail count
 
78
        m_failCount = 0;
 
79
    } else {
 
80
        m_failCount++;
 
81
        //kDebug() << socket.errorString();
 
82
        return false;
 
83
    }
 
84
    const QStringList list = data.split('|');
 
85
    int i = 1;
 
86
    m_data.clear();
 
87
    while (i + 4 < list.size()) {
 
88
        m_data[list[i]].append(list[i + 2]);
 
89
        m_data[list[i]].append(list[i + 3]);
 
90
        i += 5;
 
91
    }
 
92
    m_cacheValid = true;
 
93
    startTimer(0);
 
94
 
 
95
    return true;
 
96
}
 
97
 
 
98
QVariant HddTemp::data(const QString source, const DataType type) const
 
99
{
 
100
    return m_data[source][type];
 
101
}
 
102
 
 
103
#include "hddtemp.moc"