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
|
/*
connectionstatusplugin.cpp
Copyright (c) 2002-2003 by Chris Howells <howells@kde.org>
Copyright (c) 2003 by Martijn Klingens <klingens@kde.org>
Kopete (c) 2002-2003 by the Kopete developers <kopete-devel@kde.org>
*************************************************************************
* *
* 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; version 2 of the License. *
* *
*************************************************************************
*/
#include "connectionstatusplugin.h"
#include <tqtimer.h>
#include <kdebug.h>
#include <kgenericfactory.h>
#include <tdeprocess.h>
#include "kopeteaccountmanager.h"
typedef KGenericFactory<ConnectionStatusPlugin> ConnectionStatusPluginFactory;
K_EXPORT_COMPONENT_FACTORY( kopete_connectionstatus, ConnectionStatusPluginFactory( "kopete_connectionstatus" ) )
ConnectionStatusPlugin::ConnectionStatusPlugin( TQObject *parent, const char *name, const TQStringList& /* args */ )
: Kopete::Plugin( ConnectionStatusPluginFactory::instance(), parent, name )
{
kdDebug( 14301 ) << k_funcinfo << endl;
m_process = 0L;
m_timer = new TQTimer();
connect( m_timer, TQ_SIGNAL( timeout() ), this, TQ_SLOT( slotCheckStatus() ) );
m_timer->start( 60000 );
m_pluginConnected = false;
}
ConnectionStatusPlugin::~ConnectionStatusPlugin()
{
kdDebug( 14301 ) << k_funcinfo << endl;
delete m_timer;
delete m_process;
}
void ConnectionStatusPlugin::slotCheckStatus()
{
kdDebug( 14301 ) << k_funcinfo << endl;
if ( m_process )
{
kdWarning( 14301 ) << k_funcinfo << "Previous netstat process is still running!" << endl
<< "Not starting new netstat. Perhaps your system is under heavy load?" << endl;
return;
}
m_buffer = TQString();
// Use TDEProcess to run netstat -rn. We'll then parse the output of
// netstat -rn in slotProcessStdout() to see if it mentions the
// default gateway. If so, we're connected, if not, we're offline
m_process = new TDEProcess;
#if defined(__FreeBSD__)
*m_process << "netstat" << "-rfinet";
#else
*m_process << "netstat" << "-r";
#endif
connect( m_process, TQ_SIGNAL( receivedStdout( TDEProcess *, char *, int ) ), this, TQ_SLOT( slotProcessStdout( TDEProcess *, char *, int ) ) );
connect( m_process, TQ_SIGNAL( processExited( TDEProcess * ) ), this, TQ_SLOT( slotProcessExited( TDEProcess * ) ) );
if ( !m_process->start( TDEProcess::NotifyOnExit, TDEProcess::Stdout ) )
{
kdWarning( 14301 ) << k_funcinfo << "Unable to start netstat process!" << endl;
delete m_process;
m_process = 0L;
}
}
void ConnectionStatusPlugin::slotProcessExited( TDEProcess *process )
{
kdDebug( 14301 ) << m_buffer << endl;
if ( process == m_process )
{
setConnectedStatus( m_buffer.contains( "default" ) );
m_buffer = TQString();
delete m_process;
m_process = 0L;
}
}
void ConnectionStatusPlugin::slotProcessStdout( TDEProcess *, char *buffer, int buflen )
{
// Look for a default gateway
//kdDebug( 14301 ) << k_funcinfo << endl;
m_buffer += TQString::fromLatin1( buffer, buflen );
//kdDebug( 14301 ) << qsBuffer << endl;
}
void ConnectionStatusPlugin::setConnectedStatus( bool connected )
{
//kdDebug( 14301 ) << k_funcinfo << endl;
// We have to handle a few cases here. First is the machine is connected, and the plugin thinks
// we're connected. Then we don't do anything. Next, we can have machine connected, but plugin thinks
// we're disconnected. Also, machine disconnected, plugin disconnected -- we
// don't do anything. Finally, we can have the machine disconnected, and the plugin thinks we're
// connected. This mechanism is required so that we don't keep calling the connect/disconnect functions
// constantly.
if ( connected && !m_pluginConnected )
{
// The machine is connected and plugin thinks we're disconnected
kdDebug( 14301 ) << k_funcinfo << "Setting m_pluginConnected to true" << endl;
m_pluginConnected = true;
Kopete::AccountManager::self()->connectAll();
kdDebug( 14301 ) << k_funcinfo << "We're connected" << endl;
}
else if ( !connected && m_pluginConnected )
{
// The machine isn't connected and plugin thinks we're connected
kdDebug( 14301 ) << k_funcinfo << "Setting m_pluginConnected to false" << endl;
m_pluginConnected = false;
Kopete::AccountManager::self()->disconnectAll();
kdDebug( 14301 ) << k_funcinfo << "We're offline" << endl;
}
}
#include "connectionstatusplugin.moc"
|