|
Added
Link Here
|
| 1 |
/* |
| 2 |
This file is part of the KDE3 Fork Project |
| 3 |
Copyright (c) 2013 Serghei Amelian <serghei.amelian@gmail.com> |
| 4 |
|
| 5 |
This library is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU Library General Public |
| 7 |
License version 2 as published by the Free Software Foundation. |
| 8 |
|
| 9 |
This library 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 GNU |
| 12 |
Library General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU Library General Public License |
| 15 |
along with this library; see the file COPYING.LIB. If not, write to |
| 16 |
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 |
Boston, MA 02110-1301, USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <TQTime.h> |
| 21 |
|
| 22 |
#include <dbus/TQDBusConnection.h> |
| 23 |
#include <dbus/qdbuserror.h> |
| 24 |
#include <dbus/TQDBusMessage.h> |
| 25 |
#include <dbus/qdbusproxy.h> |
| 26 |
|
| 27 |
#include <kdebug.h> |
| 28 |
|
| 29 |
#include "kmnetworkmonitor.h" |
| 30 |
|
| 31 |
|
| 32 |
class KMNetworkMonitorPrivate : public TQDBusProxy { |
| 33 |
|
| 34 |
TQ_OBJECT |
| 35 |
|
| 36 |
public: |
| 37 |
KMNetworkMonitorPrivate(KMNetworkMonitor *parent) |
| 38 |
: TQDBusProxy(parent, "KMNetworkMonitorPrivate"), lastStatus(-1) |
| 39 |
{ |
| 40 |
setService("org.freedesktop.NetworkManager"); |
| 41 |
setPath("/org/freedesktop/NetworkManager"); |
| 42 |
setInterface("org.freedesktop.NetworkManager"); |
| 43 |
|
| 44 |
TQTime::singleShot(0, this, TQT_SLOT(initialize())); |
| 45 |
} |
| 46 |
|
| 47 |
protected slots: |
| 48 |
void initialize() |
| 49 |
{ |
| 50 |
// connect to DBUS |
| 51 |
TQDBusConnection dbus = TQDBusConnection::systemBus(); |
| 52 |
if(!dbus.isConnected()) { |
| 53 |
kdDebug() << "Unable to connect to DBus: " << dbus.lastError().message() << endl; |
| 54 |
return; |
| 55 |
} |
| 56 |
setConnection(dbus); |
| 57 |
|
| 58 |
// check for current status |
| 59 |
int rc = sendWithAsyncReply("state", TQValueList<TQDBusData>()); |
| 60 |
if(0 == rc) { |
| 61 |
kdDebug() << "Unable to send \"state\" command to DBus" << endl; |
| 62 |
return; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
void handleDBusSignal(const TQDBusMessage &message) |
| 67 |
{ |
| 68 |
// the message is for us |
| 69 |
if(path() == message.path() && interface() == message.interface() && "StateChanged" == message.member()) |
| 70 |
handleMessage(message); |
| 71 |
} |
| 72 |
|
| 73 |
void handleAsyncReply(const TQDBusMessage &message) |
| 74 |
{ |
| 75 |
handleMessage(message); |
| 76 |
} |
| 77 |
|
| 78 |
void handleMessage(const TQDBusMessage &message) |
| 79 |
{ |
| 80 |
bool ok; |
| 81 |
TQ_UINT32 state = message[0].toUInt32(&ok); |
| 82 |
|
| 83 |
if(!ok) { |
| 84 |
kdDebug() << "KMNetworkMonitor: received unexpected type for state (" << message[0].typeName() << ")" << endl; |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
int currStatus = (50 < state ? 1 : 0); |
| 89 |
|
| 90 |
if(lastStatus != currStatus) { |
| 91 |
emit static_cast<KMNetworkMonitor*>(parent())->stateChanged(1 == currStatus); |
| 92 |
lastStatus = currStatus; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
private: |
| 97 |
// -1 = unitialized, 0 = offline, 1 = online |
| 98 |
int lastStatus; |
| 99 |
}; |
| 100 |
|
| 101 |
|
| 102 |
KMNetworkMonitor::KMNetworkMonitor(TQObject *parent, const char *name) |
| 103 |
: TQObject(parent, name) |
| 104 |
{ |
| 105 |
d = new KMNetworkMonitorPrivate(this); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
KMNetworkMonitor::~KMNetworkMonitor() |
| 110 |
{ |
| 111 |
delete d; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
#include "kmnetworkmonitor.moc" |
| 116 |
#include "kmnetworkmonitor.cpp.moc" |