summaryrefslogtreecommitdiffstats
path: root/knetworkmanager-0.8/src/knetworkmanager-connection.cpp
blob: 4f5b9db58f7d48390a9c2753fa1bda41b1d45aca (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
/***************************************************************************
 *
 * knetworkmanager-connection.cpp - A NetworkManager frontend for KDE
 *
 * Copyright (C) 2005, 2006 Novell, Inc.
 *
 * Author: Helmut Schaa <hschaa@suse.de>, <helmut.schaa@gmx.de>
 *
 * 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
 *
 **************************************************************************/

/* qt headers */
#include <tqvaluelist.h>

/* kde headers */
#include <kdebug.h>
#include <tdelocale.h>

/* TQDbus headers */
#include <tqdbusconnection.h>
#include <tqdbusobjectpath.h>
#include <tqdbusdata.h>
#include <tqdbusdatamap.h>
#include <tqdbusvariant.h>

/* NM headers */
#include <NetworkManager.h>

/* knetworkmanager headers */
#include "knetworkmanager.h"
#include "knetworkmanager-connection.h"
#include "knetworkmanager-connection_dbus.h"
#include "knetworkmanager-connection_secrets_dbus.h"
#include "knetworkmanager-connection_setting.h"
#include "knetworkmanager-nmsettings.h"
#include <stdio.h>

extern unsigned char vpn_new_credentials_needed;

using namespace ConnectionSettings;

namespace ConnectionSettings
{

class ConnectionPrivate
{
	public:
		ConnectionPrivate(Connection* parent)
    {
			conn_dbus = new ConnectionDBus(parent);
			conn_secrets_dbus = new ConnectionSecretsDBus(parent);
			secrets_requested = false;
		}
		~ConnectionPrivate() {}

		TQT_DBusObjectPath        obj_path;
		ConnectionDBus*        conn_dbus;
		ConnectionSecretsDBus* conn_secrets_dbus;
		TQValueList<ConnectionSetting*> settings;
		TQString                specific_object;
		bool                   secrets_requested;
};

}

/*
	class Connection
*/
Connection::Connection()
{
	d = new ConnectionPrivate(this);

	NMSettings* nmSettings = NMSettings::getInstance();
	d->obj_path = nmSettings->getObjPathForConnection();

	TQT_DBusConnection conn = TQT_DBusConnection::systemBus();

	if (!registerObject(conn, objectPath()))
		kdError() << "registerobjectpath failed" << endl;

	// get notified whenever NM needs a secret
	connect(d->conn_secrets_dbus, TQT_SIGNAL(SecretsNeeded(const TQString&, const TQStringList&, bool)), this, TQT_SLOT(slotSecretsNeeded(const TQString&, const TQStringList&, bool)));
}

Connection::~Connection()
{
	for (TQValueList<ConnectionSetting*>::Iterator it= d->settings.begin(); it != d->settings.end(); ++it)
	{
		delete (*it);
		*it = NULL;
	}
	delete d;
}

ConnectionSetting*
Connection::getSetting(const TQString& type) const
{
	// find a setting by its type
	for (TQValueList<ConnectionSetting*>::ConstIterator it = d->settings.begin(); it != d->settings.end(); ++it)
	{
		if ((*it)->getType() == type)
			return (*it);
	}
	return NULL;
}

TQValueList<ConnectionSetting*>
Connection::getSettings() const
{
	return d->settings;
}

void
Connection::appendSetting(ConnectionSetting* setting)
{
	// that's our setting now :)
	d->settings.append(setting);
	connect(setting, TQT_SIGNAL(validityChanged()), this, TQT_SLOT(slotSettingValidityChanged()));
}

void
Connection::setSpecificObject(const TQString& obj_path)
{
	d->specific_object = obj_path;
}

TQString
Connection::getSpecificObject() const
{
	return d->specific_object;
}

TQT_DBusObjectPath
Connection::getObjectPath() const
{
	return d->obj_path;
}

TQString
Connection::objectPath() const
{
	return d->obj_path;
}

bool
Connection::isValid() const
{
	bool retval = true;
	// check if every enabled setting is valid
	for (TQValueList<ConnectionSetting*>::ConstIterator it = d->settings.begin(); it != d->settings.end(); ++it)
	{
		if ((*it)->getEnabled())
			retval &= (*it)->isValid();
	}
	return retval;
}

void
Connection::slotSecretsNeeded(const TQString& setting_name, const TQStringList& hints, bool request_new)
{
	printf("Connection::slotSecretsNeeded %s, new: %s\n", setting_name.ascii(), (request_new ? "yes" : "no"));
	kdDebug() << "Connection::slotSecretsNeeded " << setting_name.ascii() << ", new: " << (request_new ? "yes" : "no") << endl;
	ConnectionSetting* setting = getSetting(setting_name);

	// If needed, request new VPN credentials
	if (strcmp("vpn", setting_name.ascii()) == 0) {
		if (vpn_new_credentials_needed == 1) {
			vpn_new_credentials_needed = 0;
			request_new = 1;
		}
	}

	if (!setting)
	{
		// send an error to NM
		d->conn_secrets_dbus->SendGetSecretsReply(NULL);
	}
	else
	{
		if (!request_new && setting->hasSecrets())
		{
			// if the setting has secrets just send them
			d->conn_secrets_dbus->SendGetSecretsReply(setting);
		}
		else
		{
			// NetworkManager asks for new secrets, ask user for new secrets/retry
			d->secrets_requested = true;
			emit SecretsNeeded(this, setting, hints, request_new);
		}
	}
}

void
Connection::slotSecretsProvided(ConnectionSetting* setting)
{
	if (!d->secrets_requested)
		return;

	if (!setting)
	{
		// send all settings to NM
		d->conn_secrets_dbus->SendGetSecretsReply(NULL);
	}
	else
	{
		// if we have the secrets already send them to NM
		d->conn_secrets_dbus->SendGetSecretsReply(setting);
	}
	d->secrets_requested = false;
}

void
Connection::slotSecretsError()
{
	if (!d->secrets_requested)
		return;

	d->conn_secrets_dbus->SendGetSecretsError();
	d->secrets_requested = false;
}

TQT_DBusObjectBase*
Connection::createInterface(const TQString& interfaceName)
{
	// the interfaces are already created, just return the right one
	if (interfaceName == NM_DBUS_IFACE_SETTINGS_CONNECTION)
		return d->conn_dbus;

	if (interfaceName == NM_DBUS_IFACE_SETTINGS_CONNECTION_SECRETS)
		return d->conn_secrets_dbus;

	return NULL;
}

TQString
Connection::getType()
{
	return TQString();
}

void
Connection::slotSettingValidityChanged()
{
	emit validityChanged();
}

void
Connection::slotAboutToBeRemoved()
{
	d->conn_dbus->slotAboutToBeRemoved();
}
void
Connection::slotUpdated()
{
	d->conn_dbus->slotUpdated();
}

void
Connection::updateSettings(Connection* conn)
{
	TQValueList<ConnectionSetting*> settings = conn->getSettings();
	// copy all settings over to the new connection
	for (TQValueList<ConnectionSetting*>::Iterator it = settings.begin(); it != settings.end(); ++it)
	{
		ConnectionSetting* other_setting = *it;
		ConnectionSetting* my_setting = getSetting(other_setting->getType());
		if (my_setting)
		{
			my_setting->fromMap(other_setting->toMap());
			my_setting->fromSecretsMap(other_setting->toSecretsMap(false));
		}
		else
		{
			// should not happen
		}
	}
}

bool
Connection::awaitingSecrets()
{
	return d->secrets_requested;
}

#include "knetworkmanager-connection.moc"