-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathicmpengine.cpp
More file actions
139 lines (119 loc) · 3.41 KB
/
Copy pathicmpengine.cpp
File metadata and controls
139 lines (119 loc) · 3.41 KB
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
139
// the following header is required by the LGPL because
// we are using the actual time engine code
/*
* Copyright 2010 Shining <shining@freaknet.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <pthread.h>
#include <unistd.h>
/*
#include <algorithm>
#include <QDate>
#include <QTime>
#include <KSystemTimeZones>
#include <KDateTime>
#include <Plasma/DataContainer>
*/
#include "icmpengine.h"
int Thread::Start(void * arg)
{
Arg(arg); // store user data
int code = pthread_create(&ThreadId, NULL, EntryPoint, this);
return code;
}
void Thread::Run(void * arg)
{
arg=NULL;
Setup();
Execute( Arg() );
}
/*static */
void *EntryPoint(void * pthis)
{
Thread * pt = (Thread*)pthis;
pt->Run( pt->Arg() );
return NULL;
}
void cSource::Setup()
{
oCommand = "ping -c 1 ";
oCommand+=mTarget;
oCommand.append(" >/dev/null 2>&1");
Arg(strdup(oCommand.toLatin1()));
}
void cSource::Execute(void *target)
{
FILE *fTarget;
while (true) {
if ((fTarget = popen((const char *)target, "w")) == NULL)
currentStatus = unknown;
else
currentStatus = !pclose(fTarget);
sleep (1);
}
}
using namespace std;
IcmpEngine::IcmpEngine(QObject* parent, const QVariantList& args)
: Plasma::DataEngine(parent, args)
{
// We ignore any arguments - data engines do not have much use for them
Q_UNUSED(args)
status[unknown] = "Unknown";
status[offline] = "Offline";
status[online] = "Online";
key = "status";
statusLatest = statusPrevious = unknown;
// This prevents applets from setting an unnecessarily high
// update interval and using too much CPU.
setMinimumPollingInterval(5000);
}
list <cSource *>::iterator IcmpEngine::findSource(const QString &name)
{
list <cSource *>::iterator cSourceIterator;
for (cSourceIterator=sources.begin(); cSourceIterator!=sources.end(); cSourceIterator++)
if ((*cSourceIterator)->i_am(name))
break;
return cSourceIterator;
}
bool IcmpEngine::sourceRequestEvent(const QString &name)
{
int result;
cSource *pSource = new cSource(name);
list <cSource *>::iterator cSourceIterator = findSource(name);
if (cSourceIterator != sources.end()) {
delete (pSource);
result = (*(cSourceIterator))->getStatus();
}
else {
pSource->Start();
sources.push_back(pSource);
result = pSource->getStatus();
}
setData(name, I18N_NOOP(key), status[result]);
return true;
}
bool IcmpEngine::updateSourceEvent(const QString &name)
{
setData(name, I18N_NOOP(key), status[(*(findSource(name)))->getStatus()]);
return true;
}
// This does the magic that allows Plasma to load
// this plugin. The first argument must match
// the X-Plasma-EngineName in the .desktop file.
K_EXPORT_PLASMA_DATAENGINE(icmp, IcmpEngine)
// this is needed since IcmpEngine is a QObject
#include "icmpengine.moc"