summaryrefslogtreecommitdiffstats
path: root/src/modules/iograph/libkviiograph.cpp
blob: 745937fc7cab54c90574a2dd65a92feda046abbd (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//
//   File : kvi_iograph.cpp
//   Creation date : Tue Oct 31 2000 00:14:12 CEST by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 1999-2000 Szymon Stefanek (pragma at kvirc dot net)
//
//   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; either version 2
//   of the License, or (at your opinion) 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 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 "libkviiograph.h"
#include "kvi_module.h"

#if 0


#include "kvi_console.h"
#include "kvi_options.h"
#include "kvi_ircsocket.h"
#include "kvi_frame.h"
#include "kvi_locale.h"

#include "kvi_pointerlist.h"
#include <tqtooltip.h>
#include <tqpointarray.h>
#include <tqpainter.h>


extern TQPixmap                 * g_pIccMemBuffer;


static KviPointerList<KviIOGraphDisplay> * g_pIOGraphWidgetList;

KviIOGraphDisplay::KviIOGraphDisplay(KviIrcContextController * pController,bool sentGraph,bool recvGraph)
: KviIrcContextGraphicalApplet(pController,"iograph_display")
{
	g_pIOGraphWidgetList->append(this);
	for(int i=0;i < KVI_IOGRAPH_NUMBER_POINTS;i++)
	{
		m_sendRates[i] = 0;
		m_recvRates[i] = 0;
	}
	m_iNextPoint = 1;
	m_uLastSentBytes = pController->console()->socket()->sentBytes();
	m_uLastRecvBytes = pController->console()->socket()->readBytes();
	m_bShowSentGraph = sentGraph;
	m_bShowRecvGraph = recvGraph;

	KviStr tip;
	if(sentGraph)tip = __tr("Outgoing traffic");
	if(recvGraph)
	{
		if(tip.hasData())tip.append("\n");
		tip.append(__tr("Incoming traffic"));
	}

	TQToolTip::add(this,tip.ptr());

	startTimer(1000);
}

KviIOGraphDisplay::~KviIOGraphDisplay()
{
	g_pIOGraphWidgetList->removeRef(this);
}

void KviIOGraphDisplay::timerEvent(TQTimerEvent *e)
{
	unsigned int sB = console()->socket()->sentBytes();
	unsigned int rB = console()->socket()->readBytes();
	int sDiff = (sB - m_uLastSentBytes) / 8;
	int rDiff = (rB - m_uLastRecvBytes) / 32;
//	debug("s:%d,r:%d",sDiff,rDiff);
	if(sDiff < 0)sDiff = 0;
	else if(sDiff > 30)sDiff = 30;
	if(rDiff < 0)rDiff = 0;
	else if(rDiff > 30)rDiff = 30;
	m_uLastSentBytes = sB;
	m_uLastRecvBytes = rB;
	m_sendRates[m_iNextPoint] = sDiff;
	m_recvRates[m_iNextPoint] = rDiff;
	m_iNextPoint++;
	if(m_iNextPoint >= KVI_IOGRAPH_NUMBER_POINTS)m_iNextPoint = 0;
	update();
}

static TQCOORD horizSegments[6 * 4]=
{
	5 , 3  , KVI_IRCTOOLBARAPPLET_MAXIMUM_WIDTH - 4 , 3  ,
	5 , 8  , KVI_IRCTOOLBARAPPLET_MAXIMUM_WIDTH - 4 , 8  ,
	5 , 13 , KVI_IRCTOOLBARAPPLET_MAXIMUM_WIDTH - 4 , 13 ,
	5 , 18 , KVI_IRCTOOLBARAPPLET_MAXIMUM_WIDTH - 4 , 18 ,
	5 , 23 , KVI_IRCTOOLBARAPPLET_MAXIMUM_WIDTH - 4 , 23 ,
	5 , 28 , KVI_IRCTOOLBARAPPLET_MAXIMUM_WIDTH - 4 , 28
};

static TQCOORD vertSegments[29 * 4]=
{
	 9   , 4  , 9   , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 14  , 4  , 14  , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 19  , 4  , 19  , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 24  , 4  , 24  , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 29  , 4  , 29  , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 34  , 4  , 34  , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 39  , 4  , 39  , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 44  , 4  , 44  , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 49  , 4  , 49  , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 54  , 4  , 54  , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 59  , 4  , 59  , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 64  , 4  , 64  , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 69  , 4  , 69  , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 74  , 4  , 74  , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 79  , 4  , 79  , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 84  , 4  , 84  , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 89  , 4  , 89  , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 94  , 4  , 94  , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 99  , 4  , 99  , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 104 , 4  , 104 , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 109 , 4  , 109 , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 114 , 4  , 114 , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 119 , 4  , 119 , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 124 , 4  , 124 , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 129 , 4  , 129 , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 134 , 4  , 134 , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 139 , 4  , 139 , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 144 , 4  , 144 , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 ,
	 149 , 4  , 149 , KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5 
};

void KviIOGraphDisplay::drawContents(TQPainter * p)
{
	static TQPointArray hp(6 * 4,horizSegments);
	static TQPointArray vp(29 * 4,vertSegments);

	p->setPen(KVI_OPTION_COLOR(KviOption_colorIrcToolBarAppletForegroundLowContrast));
	p->drawLineSegments(hp,0,6);
	p->drawLineSegments(vp,0,29);
	p->setPen(KVI_OPTION_COLOR(KviOption_colorIrcToolBarAppletForegroundMidContrast));

	p->drawLine(4,33,KVI_IRCTOOLBARAPPLET_MAXIMUM_WIDTH - 4,33);
	p->drawLine(4,3,4,KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT - 5);

	p->setPen(isActiveContext() ?
			KVI_OPTION_COLOR(KviOption_colorIrcToolBarAppletForegroundHighContrastActive1) : 
			KVI_OPTION_COLOR(KviOption_colorIrcToolBarAppletForegroundHighContrastInactive1));

	// the first point to draw is "m_iNextPoint"
	// it will be drawn at X = 4

	int leftPart = KVI_IOGRAPH_NUMBER_POINTS - m_iNextPoint;

	int i;

	if(m_bShowSentGraph)
	{
		for(i = 1;i < leftPart;i++)
		{
			p->drawLine(3 + i,33 - m_sendRates[m_iNextPoint + i - 1],4 + i,33 - m_sendRates[m_iNextPoint + i]);
		}
	
		p->drawLine(3 + i,33 - m_sendRates[m_iNextPoint + i - 1],4 + i,33 - m_sendRates[0]);
	
		for(i = 1;i < m_iNextPoint;i++)
		{
			p->drawLine(leftPart + 3 + i,33 - m_sendRates[i - 1],leftPart + 4 + i,33 - m_sendRates[i]);
		}
	}

	if(m_bShowRecvGraph)
	{
		p->setPen(isActiveContext() ?
				KVI_OPTION_COLOR(KviOption_colorIrcToolBarAppletForegroundHighContrastActive2) : 
				KVI_OPTION_COLOR(KviOption_colorIrcToolBarAppletForegroundHighContrastInactive2));
	
		for(i = 1;i < leftPart;i++)
		{
			p->drawLine(3 + i,33 - m_recvRates[m_iNextPoint + i - 1],4 + i,33 - m_recvRates[m_iNextPoint + i]);
		}
	
		p->drawLine(3 + i,33 - m_recvRates[m_iNextPoint + i - 1],4 + i,33 - m_recvRates[0]);

		for(i = 1;i < m_iNextPoint;i++)
		{
			p->drawLine(leftPart + 3 + i,33 - m_recvRates[i - 1],leftPart + 4 + i,33 - m_recvRates[i]);
		}
	}
}

TQSize KviIOGraphDisplay::sizeHint() const
{
	return TQSize(KVI_IRCTOOLBARAPPLET_MAXIMUM_WIDTH,KVI_IRCTOOLBARAPPLET_MAXIMUM_HEIGHT);
}


/*
	@doc: iograph.add
	@type:
		command
	@title:
		iograph.add
	@short:
		Adds an IOGraph applet
	@syntax:
		iograph.add [-i] [-o]
	@description:
		Adds an IOGraph applet to the current irc-context toolbar. It will
		monitor the Incoming and Outgoing socket traffic.[br]
		Since both graphs often are somewhat unreadable,
		the -i switch will cause the IOGraph applet to show only
		the incoming traffic, and the -o switch will cause it to show
		only the outgoing traffic.[br]
		Well...I agree...this is an amazing misuse of resources :)
*/



static bool iograph_module_cmd_add(KviModule *m,KviCommand *c)
{
	ENTER_STACK_FRAME(c,"iograph_module_cmd_add");

	KviStr dummy;
	if(!g_pUserParser->parseCmdFinalPart(c,dummy))return false;

	if(!c->window()->console())return c->noIrcContext();

	bool bSentGraph = !(c->hasSwitch('i'));
	bool bRecvGraph = !(c->hasSwitch('o'));

	if(!(bSentGraph || bRecvGraph))bRecvGraph = true;

	KviIOGraphDisplay * dpy = new KviIOGraphDisplay(c->window()->console()->icController(),bSentGraph,bRecvGraph);
	c->window()->console()->icController()->addApplet(dpy);
	dpy->show();

	return c->leaveStackFrame();
}
#endif

static bool iograph_module_init(KviModule *m)
{
#if 0
	g_pIOGraphWidgetList = new KviPointerList<KviIOGraphDisplay>;
	g_pIOGraphWidgetList->setAutoDelete(false);
	m->registerCommand("add",iograph_module_cmd_add);
#endif
	return true;
}

static bool iograph_module_cleanup(KviModule *m)
{
#if 0
	while(g_pIOGraphWidgetList->first())
	{
		KviIOGraphDisplay * dpy = g_pIOGraphWidgetList->first();
		dpy->controller()->removeApplet(dpy); // deleted path
	}
	delete g_pIOGraphWidgetList;
#endif
	return true;
}

static bool iograph_module_can_unload(KviModule *m)
{
#if 0
	return g_pIOGraphWidgetList->isEmpty();
#else
	return true;
#endif
}

KVIRC_MODULE(
	"IOGraph",                                              // module name
	"1.0.0",                                                // module version
	"Copyright (C) 2000 Szymon Stefanek (pragma at kvirc dot net)", // author & (C)
	"IRC socket traffic monitor",
	iograph_module_init,
	iograph_module_can_unload,
	0,
	iograph_module_cleanup
)

#if 0
#include "libkviiograph.moc"
#endif