summaryrefslogtreecommitdiffstats
path: root/src/kvirc/ui/kvi_ipeditor.cpp
blob: df15e0275bae1456137432f1a91b474e56e8d33e (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//
//   File : kvi_ipeditor.cpp
//   Creation date : Wed Jun 12 2000 14:16:49 by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 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.
//
#define __KVIRC__
#include "kvi_ipeditor.h"
#include "kvi_qcstring.h"

#include <tqapplication.h>
#include <tqlineedit.h>
#include <tqlabel.h>
#include <tqframe.h>
#include <ctype.h>
#include <tqevent.h>


// FIXME: #warning "THIS COULD GO INTO libkvioptions ?"

KviIpEditor::KviIpEditor(TQWidget * parent,AddressType addrType,const TQString &ipAddr,const char *name)
:TQFrame(parent,name)
{

	for(int i=0;i<7;i++)
	{
		m_pEdit[i]  = 0;
		m_pLabel[i] = 0;
	}
	m_pEdit[7] = 0;
	setFrameStyle(TQFrame::Sunken|TQFrame::StyledPanel);

#ifdef COMPILE_USE_QT4
	setBackgroundRole(TQPalette::Base);
#else
	setBackgroundMode(TQWidget::PaletteBase);
#endif

	setAddressType(addrType);

	setAddress(ipAddr);
}

KviIpEditor::~KviIpEditor()
{
	
}

void KviIpEditor::setEnabled(bool bEnabled)
{
	TQFrame::setEnabled(bEnabled);
	for(int i=0;i<8;i++)
	{
		if(m_pEdit[i])m_pEdit[i]->setEnabled(bEnabled);
		if(i<7)if(m_pLabel[i])
		{
			// Is this the right way ?
#ifdef COMPILE_USE_QT4
			m_pLabel[i]->setBackgroundRole(isEnabled() ? TQPalette::Base : TQPalette::Background);
#else
			m_pLabel[i]->setBackgroundMode(isEnabled() ? TQWidget::PaletteBase : TQWidget::PaletteBackground);
#endif
			m_pLabel[i]->setEnabled(bEnabled);
		}
	}
#ifdef COMPILE_USE_QT4
	setBackgroundRole(isEnabled() ? TQPalette::Base : TQPalette::Background);
#else
	setBackgroundMode(isEnabled() ? TQWidget::PaletteBase : TQWidget::PaletteBackground);
#endif
}

void KviIpEditor::setAddressType(AddressType addrType)
{
	if((addrType != IpV4) && (addrType != IpV6))m_addrType = IpV4;
	else m_addrType = addrType;
	recreateChildren();
}

KviIpEditor::AddressType KviIpEditor::addressType() const
{
	return m_addrType;
}

bool KviIpEditor::hasEmptyFields() const
{
	bool bHasEF = false;
	for(int i=0;i<8;i++)
	{
		if(m_pEdit[i])
		{
			if(m_pEdit[i]->text().isEmpty())bHasEF = true;
		}
	}
	return bHasEF;
}

void KviIpEditor::clear()
{
	if(!m_pEdit[0])return;
	int maxW = (m_addrType == IpV4) ? 4 : 8;
	for(int i=0;i< maxW ;i++)
	{
		m_pEdit[i]->setText("");
	}
}

bool KviIpEditor::setAddress(const TQString &ipAddr)
{
	// FIXME We could check if the addres
    //       is valid before effectively setting the fields
	clear();

	KviTQCString ip = ipAddr.ascii(); // ip addresses are digits & latin letters abcdef (IpV6)

	ip = ip.stripWhiteSpace();
	const char * c = ip.data();

	if(!c)return false; // huh ?....(should never happen at this point)

	if(m_addrType == IpV4)
	{
		for(int i=0;i<4;i++)
		{
			const char *anchor = c;
			while(isdigit(*c))c++;
			if(c == anchor)return false; // Invalid empty field
			KviTQCString str(anchor,(c - anchor) + 1);
			bool bOk;
			int num = str.toInt(&bOk);
			if(!bOk)return false; // should never happen , but just to be sure
			if((num < 0) || (num > 255))return false; // Invalid field
			m_pEdit[i]->setText(str.data());
			if(i < 3){
				if(*c == '.')c++;
				else return false; // missing separator
			}
		}
	} else {
		for(int i=0;i<8;i++)
		{
			const char *anchor = c;
			while(isdigit(*c) || ((tolower(*c) >= 'a') && (tolower(*c) <= 'f')) || ((tolower(*c) >= 'A') && (tolower(*c) <= 'F')))c++;
			KviTQCString str(anchor,(c - anchor) + 1);
			if(str.length() > 4)return false; // Too long
			m_pEdit[i]->setText(str.data());
			if(i < 7){
				if(*c == ':')c++;
				else return false; // missing separator
			}
		}
	}
	if(*c)return false; // trailing garbage (we could avoid this)
	return true;
}

TQString KviIpEditor::address() const
{
	TQString ret;

	if(m_addrType == IpV6)
	{
		for(int i=0;i<8;i++)
		{
			ret.append(m_pEdit[i]->text());
			if(i < 7)ret.append(":");
		}
	} else {
		for(int i=0;i<4;i++)
		{
			TQString tmp = m_pEdit[i]->text();
			bool bOk;
			int num = tmp.toInt(&bOk);
			if(!bOk)num = 0;
			tmp.setNum(num);
			ret.append(tmp);
			if(i < 3)ret.append(".");
		}
	}
	return ret;
}

void KviIpEditor::recreateChildren()
{
	// A bit slow , but compact
	bool bIpV4 = (m_addrType == IpV4);
	int max = bIpV4 ? 4 : 8;
	TQFontMetrics fm(font());
	//int minWidth = fm.width(bIpV4 ? "000" : "AAAA") + 4;
	for(int i=0;i<max;i++)
	{
		if(!m_pEdit[i]){
			m_pEdit[i] = new TQLineEdit(this);
			m_pEdit[i]->installEventFilter(this);
			m_pEdit[i]->setFrame(false);
			m_pEdit[i]->setAlignment(TQt::AlignCenter);
		}
		//m_pEdit[i]->setMinimumWidth(minWidth);
		m_pEdit[i]->setMaxLength(bIpV4 ? 3 : 4);
		m_pEdit[i]->show();
		if(i < (max - 1))
		{
			if(!m_pLabel[i])m_pLabel[i] = new TQLabel(this);
			m_pLabel[i]->setText(bIpV4 ? "." : ":");
			m_pLabel[i]->show();
			// Is this the right way ? setBackgroundMode seems to not work well
#ifdef COMPILE_USE_QT4
			m_pLabel[i]->setBackgroundRole(isEnabled() ? TQPalette::Base : TQPalette::Background);
#else
			m_pLabel[i]->setBackgroundMode(isEnabled() ? TQWidget::PaletteBase : TQWidget::PaletteBackground);
#endif
		}
	}
	// Kill the unused widgets , if any
	if(bIpV4)
	{
		for(int i=4;i<8;i++)
		{
			if(m_pEdit[i])
			{
				delete m_pEdit[i];
				m_pEdit[i] = 0;
			}
			if(m_pLabel[i - 1])
			{
				delete m_pLabel[i - 1];
				m_pLabel[i - 1] = 0;
			}
		}
	}
	//setMinimumWidth(4 + (max * minWidth) + ((max - 1) * m_pLabel[0]->sizeHint().width()));
	setMinimumHeight(m_pLabel[0]->sizeHint().height() + 4);
	resizeEvent(0);
}

bool KviIpEditor::eventFilter(TQObject * o,TQEvent *e)
{
	if(o->inherits(TQLINEEDIT_OBJECT_NAME_STRING))
	{
		if(e->type() == TQEvent::KeyPress)
		{
			TQString s;
			// find the editor
			int edIdx = -1;
			for(int i=0;i<8;i++)
			{
				if(TQT_BASE_OBJECT(m_pEdit[i]) == TQT_BASE_OBJECT(o))
				{
					edIdx = i;
					break;
				}
			}
			if(edIdx == -1)return TQFrame::eventFilter(o,e); // user added TQLineEdit child ?
			int edMax = (m_addrType == IpV4) ? 3 : 7;
			int cursorPos = ((TQLineEdit *)o)->cursorPosition();
			switch(((TQKeyEvent *)e)->key())
			{
				case TQt::Key_Right:
					s = ((TQLineEdit *)o)->text();
					if(((unsigned int)cursorPos) == s.length())
					{
						if(edIdx < edMax)
						{
							m_pEdit[++edIdx]->setCursorPosition(0);
							m_pEdit[edIdx]->setFocus();
							return true;
						}
					}
				break;
				case TQt::Key_Left:
				case TQt::Key_Backspace:
					if(cursorPos == 0)
					{
						if(edIdx > 0)
						{
							s = m_pEdit[--edIdx]->text();
							m_pEdit[edIdx]->setCursorPosition(s.length());
							m_pEdit[edIdx]->setFocus();
							return true;
						}
					}
					return TQFrame::eventFilter(o,e);
				break;
				case TQt::Key_End:
				case TQt::Key_Home:
				case TQt::Key_Delete:
				case TQt::Key_Tab:
					return TQFrame::eventFilter(o,e);
				break;
				default:
					// a normal key (this part substitutes a TQValidator)
					const char c = tolower(((TQKeyEvent *)e)->ascii());
					if(m_addrType == IpV4)
					{
						if((c >= '0') && (c <= '9'))
						{
							if(m_pEdit[edIdx]->hasSelectedText())m_pEdit[edIdx]->cut();
							cursorPos = m_pEdit[edIdx]->cursorPosition();
							s = m_pEdit[edIdx]->text();
							s.insert(cursorPos,c);
							bool bOk = false;
							int num = s.toInt(&bOk);
							if(!bOk)return true; //should never happen , but just to be sure
							if((num < 0) || (num > 255))return true; //invalid field
							m_pEdit[edIdx]->setText(s);
							if(num > 25)
							{
								// The focus goes to the next editor
								if(edIdx < edMax)
								{
									m_pEdit[++edIdx]->setFocus();
									m_pEdit[edIdx]->selectAll();
									//m_pEdit[edIdx]->setCursorPosition(0);
									return true;
								}
							}
							m_pEdit[edIdx]->cursorForward(false);
						} else {
							if((c == '.') && (edIdx < edMax))
							{
								if(!m_pEdit[edIdx]->hasSelectedText())
								{
									m_pEdit[++edIdx]->setFocus();
									m_pEdit[edIdx]->selectAll();
								}
							}
						}
					} else {
						if(  ((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'f')) )
						{
							if(m_pEdit[edIdx]->hasSelectedText())m_pEdit[edIdx]->cut();
							cursorPos = m_pEdit[edIdx]->cursorPosition();
							s = m_pEdit[edIdx]->text();

							if(s.length() == 4)
							{
								if((cursorPos == 4) && (edIdx < edMax))
								{
									// the char goes in the next editor
									s = c;
									m_pEdit[++edIdx]->setText(s);
									m_pEdit[edIdx]->end(false);
									m_pEdit[edIdx]->setFocus();
								} // else either no space or invalid place in the string
							} else {
								// ok .. can insert
								s.insert(cursorPos,c);
								m_pEdit[edIdx]->setText(s);
								if((s.length() == 4) && (edIdx < edMax))
								{
									// the focus now goes to the next editor
									m_pEdit[++edIdx]->setFocus();
									m_pEdit[edIdx]->selectAll();
									//m_pEdit[edIdx]->setCursorPosition(0);
								} else {
									m_pEdit[edIdx]->cursorForward(false);
								}
							}
						} else {
							if((c == ':') && (edIdx < edMax))
							{
								if(!m_pEdit[edIdx]->hasSelectedText())
								{
									m_pEdit[++edIdx]->setFocus();
									m_pEdit[edIdx]->selectAll();
								}
							}
						}
					}
					return true;
				break;
			}
		}
	}
	return TQFrame::eventFilter(o,e);
}

void KviIpEditor::resizeEvent(TQResizeEvent *e)
{
	if(m_pEdit[0])
	{
		int maxW = (m_addrType == IpV4) ? 4 : 8;
		int labHint = m_pLabel[0]->sizeHint().width();
		int hghHint = height() - 4;
		int ediWdth = ((width() - 4) - ((maxW - 1) * labHint)) / maxW;
		int curX = 2;
		for(int i=0;i<maxW;i++)
		{
			if(i > 0)
			{
				m_pLabel[i - 1]->setGeometry(curX,2,labHint,hghHint);
				curX += labHint;
			}
			m_pEdit[i]->setGeometry(curX,2,ediWdth,hghHint);
			curX += ediWdth;
		}
	}
	if(e)TQFrame::resizeEvent(e);
}

TQSize KviIpEditor::sizeHint()
{
	if(m_pEdit[0])
	{
		int labHint = m_pLabel[0]->sizeHint().width();
		int hghHint = m_pEdit[0]->sizeHint().height();
		int ediHint = m_pEdit[0]->sizeHint().width();
		if(m_addrType == IpV4)return TQSize((labHint * 3) + (ediHint * 4) + 4,hghHint + 4);
		else return TQSize((labHint * 7) + (ediHint * 8) + 4,hghHint + 4);
	} else return TQFrame::sizeHint();
}


#include "kvi_ipeditor.moc"