Plasma5Support

hddtemp.cpp
1/*
2 SPDX-FileCopyrightText: 2007 Petri Damsten <damu@iki.fi>
3 SPDX-FileCopyrightText: 2007 Christopher Blauvelt <cblauvelt@gmail.com>
4
5 SPDX-License-Identifier: LGPL-2.0-only
6*/
7
8#include "hddtemp.h"
9
10#include <QTcpSocket>
11
12#include <QTimerEvent>
13
14#include <QDebug>
15
16HddTemp::HddTemp(QObject *parent)
17 : QObject(parent)
18{
19 updateData();
20}
21
22HddTemp::~HddTemp()
23{
24}
25
26QStringList HddTemp::sources()
27{
28 updateData();
29 return m_data.keys();
30}
31
32void HddTemp::timerEvent(QTimerEvent *event)
33{
34 killTimer(event->timerId());
35 m_cacheValid = false;
36}
37
38bool HddTemp::updateData()
39{
40 if (m_cacheValid) {
41 return true;
42 }
43
44 if (m_failCount > 4) {
45 return false;
46 }
47
48 QTcpSocket socket;
49 QString data;
50
51 socket.connectToHost(QStringLiteral("localhost"), 7634);
52 if (socket.waitForConnected(500)) {
53 while (data.length() < 1024) {
54 if (!socket.waitForReadyRead(500)) {
55 if (data.length() > 0) {
56 break;
57 } else {
58 // qDebug() << socket.errorString();
59 return false;
60 }
61 }
62 data += QString(socket.readAll());
63 }
64 socket.disconnectFromHost();
65 // on success retry fail count
66 m_failCount = 0;
67 } else {
68 m_failCount++;
69 // qDebug() << socket.errorString();
70 return false;
71 }
72 const QStringList list = data.split('|');
73 int i = 1;
74 m_data.clear();
75 while (i + 4 < list.size()) {
76 m_data[list[i]].append(list[i + 2]);
77 m_data[list[i]].append(list[i + 3]);
78 i += 5;
79 }
80 m_cacheValid = true;
81 startTimer(0);
82
83 return true;
84}
85
86QVariant HddTemp::data(const QString source, const DataType type) const
87{
88 return m_data[source][type];
89}
Type type(const QSqlDatabase &db)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
void connectToHost(const QHostAddress &address, quint16 port, OpenMode openMode)
virtual void disconnectFromHost()
virtual bool waitForConnected(int msecs)
virtual bool waitForReadyRead(int msecs) override
QByteArray readAll()
qsizetype size() const const
void clear()
QList< Key > keys() const const
virtual bool event(QEvent *e)
void killTimer(int id)
int startTimer(int interval, Qt::TimerType timerType)
qsizetype length() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:38 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.