summaryrefslogtreecommitdiffstats
path: root/knetworkmanager-0.8/src/knetworkmanager-connection_setting_wireless_widget.cpp
blob: e6bd9c2751c8284ca819ee1b4ed7546399aa799d (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
/***************************************************************************
 *
 * knetworkmanager-devicestore_dbus.cpp - A NetworkManager frontend for KDE
 *
 * Copyright (C) 2005, 2006 Novell, Inc.
 *
 * Author: Timo Hoenig        <[email protected]>, <[email protected]>
 *         Valentine Sinitsyn <[email protected]>
 *
 * 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 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 General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 **************************************************************************/

#include <tqwidget.h>
#include <tqlineedit.h>
#include <tqlayout.h>
#include <tqlistview.h>

#include <klistview.h>
#include <klineedit.h>
#include <klistviewsearchline.h>
#include <kiconloader.h>
#include <kglobal.h>
#include <kpushbutton.h>

#include "knetworkmanager-connection.h"
#include "knetworkmanager-connection_setting_widget_interface.h"
#include "knetworkmanager-connection_setting_wireless_widget.h"
#include "knetworkmanager-connection_setting_wireless.h"
#include "knetworkmanager-connection_setting_wireless_security.h"
#include "knetworkmanager-connection_setting_info.h"
#include "knetworkmanager-wireless_network.h"
#include "knetworkmanager-wireless_manager.h"

using namespace ConnectionSettings;

class NetworkListViewItem : public KListViewItem
{
	public:

	NetworkListViewItem(TQListView* parent, WirelessNetwork& net)
		: KListViewItem(parent, TQString::fromUtf8(net.getDisplaySsid()), TQString("%1\%").arg(net.getStrength()))
		, _net(net)
	{
		TQ_UINT8 strength = net.getStrength();
		if (strength > 80)
			setPixmap(1, KGlobal::iconLoader()->loadIcon("nm_signal_100", KIcon::Small));
		else if (strength > 55)
			setPixmap(1, KGlobal::iconLoader()->loadIcon("nm_signal_75", KIcon::Small));
		else if (strength > 30)
			setPixmap(1, KGlobal::iconLoader()->loadIcon("nm_signal_50", KIcon::Small));
		else if (strength > 5)
			setPixmap(1, KGlobal::iconLoader()->loadIcon("nm_signal_25", KIcon::Small));
		else
			setPixmap(1, KGlobal::iconLoader()->loadIcon("nm_signal_00", KIcon::Small));

		if (net.isEncrypted())
			setPixmap(2, KGlobal::iconLoader()->loadIcon("lock", KIcon::Small));
	}

	WirelessNetwork _net;
};

WirelessWidgetImpl::WirelessWidgetImpl(Connection* conn, bool new_conn, TQWidget* parent, const char* name, WFlags fl)
	: WidgetInterface(parent, name, fl)
{
	_wireless_setting = dynamic_cast<Wireless*> (conn->getSetting(NM_SETTING_WIRELESS_SETTING_NAME));
	_info_setting = dynamic_cast<Info*> (conn->getSetting(NM_SETTING_CONNECTION_SETTING_NAME));
	_security_setting = dynamic_cast<WirelessSecurity*> (conn->getSetting(NM_SETTING_WIRELESS_SECURITY_SETTING_NAME));
	_hasName = !_info_setting->getName().isEmpty();
	_new_conn = new_conn;

	TQVBoxLayout* layout = new TQVBoxLayout(this, 1, 1);
	_mainWid = new ConnectionSettingWirelessWidget(this);
	layout->addWidget(_mainWid);

	// FIXME hide this button until it is implemented
	_mainWid->pbExpert->hide();

	Init();
}

void
WirelessWidgetImpl::Init()
{
	TQVBoxLayout* layout = new TQVBoxLayout(_mainWid->framePlaceholder, 1, 1);
	_searchLine = new KListViewSearchLineWidget(_mainWid->lvEssids, _mainWid->framePlaceholder);
	layout->addWidget(_searchLine);

	connect(_mainWid->txtEssid, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(slotEssidChanged(const TQString&)));
	connect(_mainWid->lvEssids, TQT_SIGNAL(doubleClicked(TQListViewItem*, const TQPoint&, int)), this, TQT_SLOT(slotEssidDoubleClicked(TQListViewItem*, const TQPoint&, int)) );
	connect(_mainWid->lvEssids, TQT_SIGNAL(selectionChanged(TQListViewItem*)), this, TQT_SLOT(slotEssidChanged(TQListViewItem*)));

	if (!_new_conn)
	{
		_mainWid->chkAutoRefresh->hide();
		_mainWid->lvEssids->hide();
		_searchLine->hide();
	}
}

void
WirelessWidgetImpl::Activate()
{
	// Fill in all available networks from all devices
	// get all aps from all devices grouped together using the ssid
	TQValueList<WirelessNetwork> nets = WirelessManager::getWirelessNetworks(0, WirelessNetwork::MATCH_SSID);

	_mainWid->lvEssids->clear();

	for (TQValueList<WirelessNetwork>::Iterator it = nets.begin(); it != nets.end(); ++it)
	{
		KListViewItem* item = new NetworkListViewItem(_mainWid->lvEssids, (*it));

		_mainWid->lvEssids->insertItem(item);
	}

	// FIXME, if essid contains unprintable characters show the essid in hex
	_mainWid->txtEssid->setText(_wireless_setting->getEssid());
}

void WirelessWidgetImpl::slotEssidChanged(TQListViewItem* item)
{
	NetworkListViewItem* net_item = dynamic_cast<NetworkListViewItem*>(item);
	if (net_item)
	{
		// update the settingsobject
		updateEssid(net_item->_net.getSsid());

		// update the textbox to match the selected essid
		_mainWid->txtEssid->setText(net_item->_net.getDisplaySsid());
	}
}

void
WirelessWidgetImpl::slotEssidDoubleClicked(TQListViewItem* item, const TQPoint& /*p*/, int /*i*/)
{
	NetworkListViewItem* net_item = dynamic_cast<NetworkListViewItem*>(item);
	if (net_item)
	{
		updateEssid(net_item->_net.getSsid());
		// essid selected with double click -> goto the next setting
		emit next();
	}
}


void
WirelessWidgetImpl::slotEssidChanged(const TQString& new_essid)
{
	/* set the newly entered essid */
	/* FIXME perhaps local8Bit is better? */
	/* FIXME allow entering essid in hex */
	/* FIXME select the appropriate essid in the list too */

	TQByteArray essid(new_essid.utf8());

	// remove trailing \0
	essid.resize(essid.size() - 1);

	updateEssid(essid);
}

void
WirelessWidgetImpl::updateEssid(const TQByteArray& essid)
{
	_wireless_setting->setEssid(essid);

	if (!_hasName)
	{
		// the connection has no name yet -> just take the essid for it
		_info_setting->setName(essid);
	}
}

TQByteArray WirelessWidgetImpl::byteArrayFromHexString(const TQCString& str)
{
	char c[2];
	TQByteArray arr(str.length() - 1);
	TQTextStream stream(str, IO_ReadOnly);

	stream.setf(TQTextStream::hex);
	stream.setf(TQTextStream::left);
	stream.width(2);
	stream.fill('0');

	// 0x
	stream.readRawBytes(c, 2);

	for (uint i = 0; i < (str.length()-1)/2; ++i)
	{
		stream.readRawBytes(c, 2);
		TQString hex;
		hex += "0x";
		hex += c[0];
		hex += c[1];
		arr[i] = (unsigned char)hex.toShort();
	}
	return arr;
}

TQString WirelessWidgetImpl::hexStringFromByteArray(const TQByteArray& bytes)
{
	TQString tmp;
	TQTextStream stream(&tmp, IO_ReadWrite);
	stream.setf(TQTextStream::hex);
	stream.setf(TQTextStream::left);
	stream.width(2);
	stream.fill('0');

	stream << "0x" ;
	for (uint i = 0; i < bytes.size(); ++i)
	{
		stream << static_cast<unsigned char>(bytes[i]);
	}
	return tmp;
}

#include "knetworkmanager-connection_setting_wireless_widget.moc"