nm-connection-list.c 52.4 KB
Newer Older
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
Rodrigo Moya's avatar
Rodrigo Moya committed
2 3 4
/* NetworkManager Connection editor -- Connection editor for NetworkManager
 *
 * Rodrigo Moya <rodrigo@gnome-db.org>
5
 * Dan Williams <dcbw@redhat.com>
Rodrigo Moya's avatar
Rodrigo Moya committed
6 7 8 9 10 11 12 13 14 15 16
 *
 * 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.
 *
17 18 19
 * 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.
Rodrigo Moya's avatar
Rodrigo Moya committed
20
 *
21
 * (C) Copyright 2007 - 2008 Red Hat, Inc.
Rodrigo Moya's avatar
Rodrigo Moya committed
22 23
 */

24
#include <config.h>
25
#include <string.h>
26 27
#include <sys/types.h>
#include <unistd.h>
28

29
#include <gtk/gtk.h>
30
#include <gconf/gconf-client.h>
31
#include <gdk/gdkx.h>
32 33
#include <glib/gi18n.h>

34
#ifdef NO_POLKIT_GNOME
35
#include "polkit-gnome.h"
36 37 38 39
#else
#include <polkit-gnome/polkit-gnome.h>
#endif

40
#include <nm-setting-connection.h>
41 42
#include <nm-connection.h>
#include <nm-setting.h>
43 44 45 46 47
#include <nm-setting-wired.h>
#include <nm-setting-wireless.h>
#include <nm-setting-vpn.h>
#include <nm-setting-gsm.h>
#include <nm-setting-cdma.h>
Tambet Ingo's avatar
Tambet Ingo committed
48
#include <nm-setting-pppoe.h>
49
#include <nm-setting-ppp.h>
50
#include <nm-setting-serial.h>
51
#include <nm-vpn-plugin-ui-interface.h>
52
#include <nm-utils.h>
53

Rodrigo Moya's avatar
Rodrigo Moya committed
54 55
#include "nm-connection-editor.h"
#include "nm-connection-list.h"
56
#include "gconf-helpers.h"
57
#include "mobile-wizard.h"
58
#include "utils.h"
59
#include "vpn-helpers.h"
Rodrigo Moya's avatar
Rodrigo Moya committed
60 61 62

G_DEFINE_TYPE (NMConnectionList, nm_connection_list, G_TYPE_OBJECT)

63 64 65 66 67 68 69
enum {
	LIST_DONE,
	LIST_LAST_SIGNAL
};

static guint list_signals[LIST_LAST_SIGNAL] = { 0 };

70
#define COL_ID 			0
71 72 73
#define COL_LAST_USED	1
#define COL_TIMESTAMP	2
#define COL_CONNECTION	3
74

75 76 77
typedef struct {
	NMConnectionList *list;
	GtkTreeView *treeview;
78
	GtkWidget *button;
79 80
} ActionInfo;

81
static void
82
show_error_dialog (GtkWindow *parent, const gchar *format, ...)
83 84 85 86 87 88 89 90 91 92 93 94 95
{
	GtkWidget *dialog;
	va_list args;
	char *msg;

	va_start (args, format);
	msg = g_strdup_vprintf (format, args);
	va_end (args);

	dialog = gtk_message_dialog_new (NULL,
							   GTK_DIALOG_DESTROY_WITH_PARENT,
							   GTK_MESSAGE_ERROR,
							   GTK_BUTTONS_CLOSE,
96
							   "%s", msg);
97 98
	g_free (msg);

99 100 101
	if (parent)
		gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);

102 103 104 105
	gtk_dialog_run (GTK_DIALOG (dialog));
	gtk_widget_destroy (dialog);
}

106 107
static NMExportedConnection *
get_active_connection (GtkTreeView *treeview)
108
{
109 110 111 112 113
	GtkTreeSelection *selection;
	GList *selected_rows;
	GtkTreeModel *model = NULL;
	GtkTreeIter iter;
	NMExportedConnection *exported = NULL;
114

115 116 117 118
	selection = gtk_tree_view_get_selection (treeview);
	selected_rows = gtk_tree_selection_get_selected_rows (selection, &model);
	if (!selected_rows)
		return NULL;
119

120 121
	if (gtk_tree_model_get_iter (model, &iter, (GtkTreePath *) selected_rows->data))
		gtk_tree_model_get (model, &iter, COL_CONNECTION, &exported, -1);
122

123 124 125
	/* free memory */
	g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
	g_list_free (selected_rows);
126

127 128
	return exported;
}
129

130 131 132 133 134 135 136
static GtkListStore *
get_model_for_connection (NMConnectionList *list, NMExportedConnection *exported)
{
	NMConnection *connection;
	NMSettingConnection *s_con;
	GtkTreeView *treeview;
	GtkTreeModel *model;
137
	const char *connection_type;
138

139 140
	connection = nm_exported_connection_get_connection (exported);
	s_con = NM_SETTING_CONNECTION (nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION));
141 142 143
	connection_type = s_con ? nm_setting_connection_get_connection_type (s_con) : NULL;

	if (!connection_type) {
144 145 146
		g_warning ("Ignoring incomplete connection");
		return NULL;
	}
147

148
	treeview = (GtkTreeView *) g_hash_table_lookup (list->treeviews, connection_type);
149
	if (!treeview) {
150
		g_warning ("No registered treeview for connection type '%s'", connection_type);
151 152 153 154 155 156 157 158
		return NULL;
	}

	model = gtk_tree_view_get_model (treeview);
	if (GTK_IS_TREE_MODEL_SORT (model))
		return GTK_LIST_STORE (gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model)));

	return GTK_LIST_STORE (model);
159 160
}

161 162 163 164
static gboolean
get_iter_for_connection (GtkTreeModel *model,
					NMExportedConnection *exported,
					GtkTreeIter *iter)
165
{
166 167
	GtkTreeIter temp_iter;
	gboolean found = FALSE;
168

169 170
	if (!gtk_tree_model_get_iter_first (model, &temp_iter))
		return FALSE;
171

172 173
	do {
		NMExportedConnection *candidate = NULL;
174

175 176 177 178 179 180 181
		gtk_tree_model_get (model, &temp_iter, COL_CONNECTION, &candidate, -1);
		if (candidate && (candidate == exported)) {
			*iter = temp_iter;
			found = TRUE;
			break;
		}
	} while (gtk_tree_model_iter_next (model, &temp_iter));
182

183
	return found;
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
static char *
format_last_used (guint64 timestamp)
{
	GTimeVal now_tv;
	GDate *now, *last;
	char *last_used = NULL;

	if (!timestamp)
		return g_strdup (_("never"));

	g_get_current_time (&now_tv);
	now = g_date_new ();
	g_date_set_time_val (now, &now_tv);

	last = g_date_new ();
	g_date_set_time_t (last, (time_t) timestamp);

	/* timestamp is now or in the future */
	if (now_tv.tv_sec <= timestamp) {
		last_used = g_strdup (_("now"));
		goto out;
	}

	if (g_date_compare (now, last) <= 0) {
		guint minutes, hours;

		/* Same day */

		minutes = (now_tv.tv_sec - timestamp) / 60;
		if (minutes == 0) {
			last_used = g_strdup (_("now"));
			goto out;
		}

		hours = (now_tv.tv_sec - timestamp) / 3600;
		if (hours == 0) {
			/* less than an hour ago */
			last_used = g_strdup_printf (ngettext ("%d minute ago", "%d minutes ago", minutes), minutes);
			goto out;
		}

		last_used = g_strdup_printf (ngettext ("%d hour ago", "%d hours ago", hours), hours);
	} else {
		guint days, months, years;

		days = g_date_get_julian (now) - g_date_get_julian (last);
		if (days == 0) {
			last_used = g_strdup ("today");
			goto out;
		}

		months = days / 30;
		if (months == 0) {
			last_used = g_strdup_printf (ngettext ("%d day ago", "%d days ago", days), days);
			goto out;
		}

		years = days / 365;
		if (years == 0) {
			last_used = g_strdup_printf (ngettext ("%d month ago", "%d months ago", months), months);
			goto out;
		}

		last_used = g_strdup_printf (ngettext ("%d year ago", "%d years ago", years), years);
	}

out:
	g_date_free (now);
	g_date_free (last);
	return last_used;
}

258
static void
259 260 261
update_connection_row (GtkListStore *store,
				   GtkTreeIter *iter,
				   NMExportedConnection *exported)
262 263
{
	NMConnection *connection;
264 265
	NMSettingConnection *s_con;
	char *last_used;
266

267 268 269
	connection = nm_exported_connection_get_connection (exported);
	s_con = NM_SETTING_CONNECTION (nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION));
	g_assert (s_con);
270

271
	last_used = format_last_used (nm_setting_connection_get_timestamp (s_con));
272
	gtk_list_store_set (store, iter,
273
					COL_ID, nm_setting_connection_get_id (s_con),
274
					COL_LAST_USED, last_used,
275
					COL_TIMESTAMP, nm_setting_connection_get_timestamp (s_con),
276 277 278 279
					COL_CONNECTION, exported,
					-1);
	g_free (last_used);
}
280

281 282 283 284 285 286 287 288 289 290 291 292 293

/**********************************************/
/* PolKit helpers */

static gboolean
is_permission_denied_error (GError *error)
{
	return dbus_g_error_has_name (error, "org.freedesktop.NetworkManagerSettings.Connection.NotPrivileged") ||
		dbus_g_error_has_name (error, "org.freedesktop.NetworkManagerSettings.System.NotPrivileged");
}

static gboolean
obtain_auth (GError *pk_error,
294 295 296 297
             GtkWindow *parent,
             PolKitGnomeAuthCB callback,
             gpointer user_data,
             GError **error)
298 299 300 301
{
	PolKitAction *pk_action;
	char **tokens;
	gboolean success = FALSE;
302
	guint xid = 0;
303 304 305

	tokens = g_strsplit (pk_error->message, " ", 2);
	if (g_strv_length (tokens) != 2) {
306 307
		g_set_error (error, 0, 0, "%s", _("PolicyKit authorization was malformed."));
		goto out;
308 309
	}

310 311 312 313
	pk_action = polkit_action_new_from_string_representation (tokens[0]);
	if (!pk_action) {
		g_set_error (error, 0, 0, "%s", _("PolicyKit authorization could not be created."));
		goto out;
314 315
	}

316 317 318 319 320 321 322
	if (parent)
		xid = gdk_x11_drawable_get_xid (GDK_DRAWABLE (GTK_WIDGET (parent)->window));
	success = polkit_gnome_auth_obtain (pk_action, xid, getpid (), callback, user_data, error);
	polkit_action_unref (pk_action);

out:
	g_strfreev (tokens);
323 324 325 326 327 328 329 330 331 332
	return success;
}

/**********************************************/
/* Connection removing */

typedef void (*ConnectionRemovedFn) (NMExportedConnection *exported,
							  gboolean success,
							  gpointer user_data);

333
typedef struct {
334
	NMExportedConnection *exported;
335
	GtkWindow *parent;
336 337 338 339 340
	ConnectionRemovedFn callback;
	gpointer user_data;
} ConnectionRemoveInfo;

static void remove_connection (NMExportedConnection *exported,
341 342 343
                               GtkWindow *parent,
                               ConnectionRemovedFn callback,
                               gpointer user_data);
344 345

static void
346 347 348 349
remove_connection_cb (PolKitAction *action,
				  gboolean gained_privilege,
				  GError *error,
				  gpointer user_data)
350
{
351
	ConnectionRemoveInfo *info = (ConnectionRemoveInfo *) user_data;
352
	gboolean done = TRUE;
353

354
	if (gained_privilege) {
355
		remove_connection (info->exported, info->parent, info->callback, info->user_data);
356 357
		done = FALSE;
	} else if (error) {
358
		show_error_dialog (info->parent, _("Could not obtain required privileges: %s."), error->message);
359 360
		g_error_free (error);
	} else
361
		show_error_dialog (info->parent, _("Could not remove system connection: permission denied."));
362 363

	if (done && info->callback)
364
		info->callback (info->exported, FALSE, info->user_data);
365

366
	g_object_unref (info->exported);
367
	g_slice_free (ConnectionRemoveInfo, info);
368 369 370
}

static void
371
remove_connection (NMExportedConnection *exported,
372
                   GtkWindow *parent,
373 374
                   ConnectionRemovedFn callback,
                   gpointer user_data)
375
{
376 377 378 379 380 381 382 383 384
	GError *error = NULL;
	gboolean success;

	success = nm_exported_connection_delete (exported, &error);
	if (!success) {
		gboolean auth_pending = FALSE;

		if (is_permission_denied_error (error)) {
			ConnectionRemoveInfo *info;
385
			GError *auth_error = NULL;
386 387 388

			info = g_slice_new (ConnectionRemoveInfo);
			info->exported = g_object_ref (exported);
389
			info->parent = parent;
390 391 392
			info->callback = callback;
			info->user_data = user_data;

393 394 395 396 397 398 399
			auth_pending = obtain_auth (error, parent, remove_connection_cb, info, &auth_error);
			if (auth_error) {
				show_error_dialog (parent,
				                   _("Removing connection failed: %s."),
				                   auth_error->message);
				g_error_free (auth_error);
			}
400 401 402 403 404 405

			if (!auth_pending) {
				g_object_unref (info->exported);
				g_slice_free (ConnectionRemoveInfo, info);
			}
		} else
406
			show_error_dialog (parent, _("Removing connection failed: %s."), error->message);
407 408 409 410 411

		g_error_free (error);

		if (auth_pending)
			return;
412 413 414 415 416 417 418 419 420 421 422 423 424
	} else {
		NMConnection *connection;
		NMSettingConnection *s_con;
		NMSettingVPN *s_vpn;
		NMVpnPluginUiInterface *plugin;

		connection = nm_exported_connection_get_connection (exported);
		s_con = (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION);
		g_assert (s_con);

		/* FIXME: clean up any left-over connection secrets here */

		/* Clean up VPN secrets and any plugin-specific data */
425
		if (!strcmp (nm_setting_connection_get_connection_type (s_con), NM_SETTING_VPN_SETTING_NAME)) {
426 427
			s_vpn = (NMSettingVPN *) nm_connection_get_setting (connection, NM_TYPE_SETTING_VPN);
			if (s_vpn) {
428
				plugin = vpn_get_plugin_by_service (nm_setting_vpn_get_service_type (s_vpn));
429 430 431 432 433 434 435 436 437
				if (plugin)
					if (!nm_vpn_plugin_ui_interface_delete_connection (plugin, connection, &error)) {
						g_warning ("%s: couldn't clean up VPN connection on delete: (%d) %s",
						           __func__, error ? error->code : -1, error ? error->message : "unknown");
						if (error)
							g_error_free (error);
					}
			}
		}
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
	}

	if (callback)
		callback (exported, success, user_data);
}

/**********************************************/
/* Connection adding */

typedef void (*ConnectionAddedFn) (NMExportedConnection *exported,
							gboolean success,
							gpointer user_data);

typedef struct {
	NMConnectionList *list;
453
	NMConnectionEditor *editor;
454
	NMConnection *connection;
455 456 457 458 459
	ConnectionAddedFn callback;
	gpointer user_data;
} ConnectionAddInfo;

static void add_connection (NMConnectionList *self,
460 461 462 463
                            NMConnectionEditor *editor,
                            NMConnection *connection,
                            ConnectionAddedFn callback,
                            gpointer user_data);
464 465 466 467 468 469 470 471

static void
add_connection_cb (PolKitAction *action,
			    gboolean gained_privilege,
			    GError *error,
			    gpointer user_data)
{
	ConnectionAddInfo *info = (ConnectionAddInfo *) user_data;
472
	GtkWindow *parent = nm_connection_editor_get_window (info->editor);
473 474 475
	gboolean done = TRUE;

	if (gained_privilege) {
476
		add_connection (info->list, info->editor, info->connection, info->callback, info->user_data);
477 478
		done = FALSE;
	} else if (error) {
479
		show_error_dialog (parent, _("Could not obtain required privileges: %s."), error->message);
480 481
		g_error_free (error);
	} else
482
		show_error_dialog (parent, _("Could not add system connection: permission denied."));
483 484 485 486 487 488 489 490 491 492

	if (done && info->callback)
		info->callback (NULL, FALSE, info->user_data);

	g_object_unref (info->connection);
	g_slice_free (ConnectionAddInfo, info);
}

static void
add_connection (NMConnectionList *self,
493 494 495 496
                NMConnectionEditor *editor,
                NMConnection *connection,
                ConnectionAddedFn callback,
                gpointer user_data)
497 498
{
	NMExportedConnection *exported = NULL;
499 500
	NMConnectionScope scope;
	gboolean success = FALSE;
501

502 503
	scope = nm_connection_get_scope (connection);
	if (scope == NM_CONNECTION_SCOPE_SYSTEM) {
504
		GError *error = NULL;
505

506
		utils_fill_connection_certs (connection);
507
		success = nm_dbus_settings_system_add_connection (self->system_settings, connection, &error);
508 509
		utils_clear_filled_connection_certs (connection);

510 511
		if (!success) {
			gboolean pending_auth = FALSE;
512
			GtkWindow *parent;
513

514
			parent = nm_connection_editor_get_window (editor);
515 516
			if (is_permission_denied_error (error)) {
				ConnectionAddInfo *info;
517
				GError *auth_error = NULL;
518 519 520

				info = g_slice_new (ConnectionAddInfo);
				info->list = self;
521
				info->editor = editor;
522 523 524 525
				info->connection = g_object_ref (connection);
				info->callback = callback;
				info->user_data = user_data;

526 527 528 529 530 531 532
				pending_auth = obtain_auth (error, parent, add_connection_cb, info, &auth_error);
				if (auth_error) {
					show_error_dialog (parent,
					                   _("Adding connection failed: %s."),
					                   auth_error->message);
					g_error_free (auth_error);
				}
533 534 535 536 537

				if (!pending_auth) {
					g_object_unref (info->connection);
					g_slice_free (ConnectionAddInfo, info);
				}
538
			} else
539
				show_error_dialog (parent, _("Adding connection failed: %s."), error->message);
540 541 542 543 544

			g_error_free (error);

			if (pending_auth)
				return;
545
		}
546
	} else if (scope == NM_CONNECTION_SCOPE_USER) {
547 548
		exported = (NMExportedConnection *) nma_gconf_settings_add_connection (self->gconf_settings, connection);
		success = exported != NULL;
549 550 551 552
		if (success && editor)
			nm_connection_editor_save_vpn_secrets (editor);
	} else
		g_warning ("%s: unhandled connection scope %d!", __func__, scope);
553

554 555 556 557 558 559
	if (callback)
		callback (exported, success, user_data);

	if (exported)
		g_object_unref (exported);
}
560

561 562
/**********************************************/
/* Connection updating */
563

564 565 566
typedef void (*ConnectionUpdatedFn) (NMConnectionList *list,
							  gboolean success,
							  gpointer user_data);
567

568 569
typedef struct {
	NMConnectionList *list;
570
	NMConnectionEditor *editor;
571 572 573 574 575 576 577 578 579
	NMExportedConnection *original;
	NMConnection *modified;
	ConnectionUpdatedFn callback;
	gpointer user_data;

	NMExportedConnection *added_connection;
} ConnectionUpdateInfo;

static void update_connection (NMConnectionList *list,
580 581 582 583 584
                               NMConnectionEditor *editor,
                               NMExportedConnection *original,
                               NMConnection *modified,
                               ConnectionUpdatedFn callback,
                               gpointer user_data);
585 586


587 588 589 590 591
static void
connection_update_done (ConnectionUpdateInfo *info, gboolean success)
{
	if (info->callback)
		info->callback (info->list, success, info->user_data);
592

593 594 595 596
	g_object_unref (info->original);
	g_object_unref (info->modified);
	if (info->added_connection)
		g_object_unref (info->added_connection);
597

598 599
	g_slice_free (ConnectionUpdateInfo, info);
}
600

601 602 603 604 605 606 607 608 609 610
static void
connection_update_remove_done (NMExportedConnection *exported,
						 gboolean success,
						 gpointer user_data)
{
	ConnectionUpdateInfo *info = (ConnectionUpdateInfo *) user_data;

	if (success)
		connection_update_done (info, success);
	else if (info->added_connection) {
611 612
		GtkWindow *parent;

613 614
		/* Revert the scope of the original connection and remove the connection we just successfully added */
		/* FIXME: loops forever on error */
615 616
		parent = nm_connection_editor_get_window (info->editor);
		remove_connection (info->added_connection, parent, connection_update_remove_done, info);
617
	}
618
}
619

620 621 622 623 624 625 626 627 628 629
static void
connection_update_add_done (NMExportedConnection *exported,
					   gboolean success,
					   gpointer user_data)
{
	ConnectionUpdateInfo *info = (ConnectionUpdateInfo *) user_data;

	if (success) {
		/* Adding the connection with different scope succeeded, now try to remove the original */
		info->added_connection = exported ? g_object_ref (exported) : NULL;
630
		remove_connection (info->original, GTK_WINDOW (info->editor), connection_update_remove_done, info);
631 632 633 634 635 636 637 638 639 640 641 642
	} else
		connection_update_done (info, success);
}

static void
update_connection_cb (PolKitAction *action,
				  gboolean gained_privilege,
				  GError *error,
				  gpointer user_data)
{
	ConnectionUpdateInfo *info = (ConnectionUpdateInfo *) user_data;
	gboolean done = TRUE;
643
	GtkWindow *parent;
644

645
	parent = nm_connection_editor_get_window (info->editor);
646
	if (gained_privilege) {
647
		update_connection (info->list, info->editor, info->original, info->modified, info->callback, info->user_data);
648 649
		done = FALSE;
	} else if (error) {
650
		show_error_dialog (parent, _("Could not obtain required privileges: %s."), error->message);
651 652
		g_error_free (error);
	} else
653
		show_error_dialog (parent, _("Could not update system connection: permission denied."));
654 655 656 657 658 659 660

	if (done)
		connection_update_done (info, FALSE);
	else {
		g_object_unref (info->original);
		g_object_unref (info->modified);
		g_slice_free (ConnectionUpdateInfo, info);
661
	}
662 663 664 665
}

static void
update_connection (NMConnectionList *list,
666 667 668 669 670
                   NMConnectionEditor *editor,
                   NMExportedConnection *original,
                   NMConnection *modified,
                   ConnectionUpdatedFn callback,
                   gpointer user_data)
671 672 673
{
	NMConnectionScope original_scope;
	ConnectionUpdateInfo *info;
674

675 676
	info = g_slice_new0 (ConnectionUpdateInfo);
	info->list = list;
677
	info->editor = editor;
678 679 680 681 682 683 684 685 686 687 688 689
	info->original = g_object_ref (original);
	info->modified = g_object_ref (modified);
	info->callback = callback;
	info->user_data = user_data;

	original_scope = nm_connection_get_scope (nm_exported_connection_get_connection (original));
	if (nm_connection_get_scope (modified) == original_scope) {
		/* The easy part: Connection is updated */
		GHashTable *new_settings;
		GError *error = NULL;
		gboolean success;
		gboolean pending_auth = FALSE;
690
		GtkWindow *parent;
691

692
		utils_fill_connection_certs (modified);
693 694
		new_settings = nm_connection_to_hash (modified);

695 696 697 698
		/* Hack; make sure that gconf private values are copied */
		nm_gconf_copy_private_connection_values (nm_exported_connection_get_connection (original),
		                                         modified);

699 700 701 702
		success = nm_exported_connection_update (original, new_settings, &error);
		g_hash_table_destroy (new_settings);
		utils_clear_filled_connection_certs (modified);

703
		parent = nm_connection_editor_get_window (editor);
704
		if (!success) {
705 706 707 708 709 710 711 712 713 714 715 716
			if (is_permission_denied_error (error)) {
				GError *auth_error = NULL;

				pending_auth = obtain_auth (error, parent, update_connection_cb, info, &auth_error);
				if (auth_error) {
					show_error_dialog (parent,
					                   _("Updating connection failed: %s."),
					                   auth_error->message);
					g_error_free (auth_error);
				}
			} else
				show_error_dialog (parent, _("Updating connection failed: %s."), error->message);
717 718

			g_error_free (error);
719 720 721 722
		} else {
			/* Save user-connection vpn secrets */
			if (editor && (original_scope == NM_CONNECTION_SCOPE_USER))
				nm_connection_editor_save_vpn_secrets (editor);
723 724 725 726 727 728 729 730
		}

		if (!pending_auth)
			connection_update_done (info, success);
	} else {
		/* The hard part: Connection scope changed:
		   Add the exported connection,
		   if it succeeds, remove the old one. */
731
		add_connection (list, editor, modified, connection_update_add_done, info);
732
	}
733 734
}

735 736 737 738
static void
add_done_cb (NMConnectionEditor *editor, gint response, gpointer user_data)
{
	ActionInfo *info = (ActionInfo *) user_data;
739
	NMConnection *connection;
740

741
	connection = nm_connection_editor_get_connection (editor);
742
	if (response == GTK_RESPONSE_OK)
743
		add_connection (info->list, editor, connection, NULL, NULL);
744

745
	g_hash_table_remove (info->list->editors, connection);
746 747
}

748
static void
749
add_one_name (gpointer data, gpointer user_data)
750
{
751 752
	NMExportedConnection *exported = NM_EXPORTED_CONNECTION (data);
	NMConnection *connection;
753
	NMSettingConnection *s_con;
754
	const char *id;
755 756
	GSList **list = (GSList **) user_data;

757
	connection = nm_exported_connection_get_connection (exported);
758
	s_con = NM_SETTING_CONNECTION (nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION));
759 760 761
	id = nm_setting_connection_get_id (s_con);
	g_assert (id);
	*list = g_slist_append (*list, (gpointer) id);
762 763 764 765 766
}

static char *
get_next_available_name (NMConnectionList *list, const char *format)
{
767
	GSList *connections;
768 769 770 771
	GSList *names = NULL, *iter;
	char *cname = NULL;
	int i = 0;

772 773 774 775 776 777
	connections = nm_settings_list_connections (NM_SETTINGS (list->system_settings));
	connections = g_slist_concat (connections, nm_settings_list_connections (NM_SETTINGS (list->gconf_settings)));

	g_slist_foreach (connections, add_one_name, &names);
	g_slist_free (connections);

778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
	if (g_slist_length (names) == 0)
		return g_strdup_printf (format, 1);

	/* Find the next available unique connection name */
	while (!cname && (i++ < 10000)) {
		char *temp;
		gboolean found = FALSE;

		temp = g_strdup_printf (format, i);
		for (iter = names; iter; iter = g_slist_next (iter)) {
			if (!strcmp (iter->data, temp)) {
				found = TRUE;
				break;
			}
		}
		if (!found)
			cname = temp;
		else
			g_free (temp);
	}

	g_slist_free (names);
	return cname;
}

803 804 805 806 807 808
static void
add_default_serial_setting (NMConnection *connection)
{
	NMSettingSerial *s_serial;

	s_serial = NM_SETTING_SERIAL (nm_setting_serial_new ());
809 810 811 812 813 814 815
	g_object_set (s_serial,
	              NM_SETTING_SERIAL_BAUD, 115200,
	              NM_SETTING_SERIAL_BITS, 8,
	              NM_SETTING_SERIAL_PARITY, 'n',
	              NM_SETTING_SERIAL_STOPBITS, 1,
	              NULL);

816 817 818
	nm_connection_add_setting (connection, NM_SETTING (s_serial));
}

819
static NMConnection *
820
create_new_connection_for_type (NMConnectionList *list, const char *connection_type)
821
{
822
	GType ctype;
823 824 825
	NMConnection *connection = NULL;
	NMSettingConnection *s_con;
	NMSetting *type_setting = NULL;
826
	char *id, *uuid;
827
	GType mb_type;
828

829 830
	ctype = nm_connection_lookup_setting_type (connection_type);

831
	connection = nm_connection_new ();
832
	nm_connection_set_scope (connection, NM_CONNECTION_SCOPE_USER);
833
	s_con = NM_SETTING_CONNECTION (nm_setting_connection_new ());
834 835 836
	uuid = nm_utils_uuid_generate ();
	g_object_set (s_con, NM_SETTING_CONNECTION_UUID, uuid, NULL);
	g_free (uuid);
837 838 839
	nm_connection_add_setting (connection, NM_SETTING (s_con));

	if (ctype == NM_TYPE_SETTING_WIRED) {
840 841 842 843 844 845 846
		id = get_next_available_name (list, _("Wired connection %d"));
		g_object_set (s_con,
		              NM_SETTING_CONNECTION_ID, id,
		              NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRED_SETTING_NAME,
		              NM_SETTING_CONNECTION_AUTOCONNECT, TRUE,
		              NULL);
		g_free (id);
847 848 849

		type_setting = nm_setting_wired_new ();
	} else if (ctype == NM_TYPE_SETTING_WIRELESS) {
850 851
		NMSettingWireless *s_wireless;

852 853 854 855 856 857 858
		id = get_next_available_name (list, _("Wireless connection %d"));
		g_object_set (s_con,
		              NM_SETTING_CONNECTION_ID, id,
		              NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRELESS_SETTING_NAME,
		              NM_SETTING_CONNECTION_AUTOCONNECT, TRUE,
		              NULL);
		g_free (id);
859 860

		type_setting = nm_setting_wireless_new ();
861
		s_wireless = NM_SETTING_WIRELESS (type_setting);
862
		g_object_set (s_wireless, NM_SETTING_WIRELESS_MODE, "infrastructure", NULL);
863
	} else if ((ctype == NM_TYPE_SETTING_GSM) || (ctype == NM_TYPE_SETTING_CDMA)) {
864
		/* Since GSM is a placeholder for both GSM and CDMA; ask the user which
865 866
		 * one they really want.
		 */
867 868 869 870
		mb_type = mobile_wizard_ask_connection_type ();
		if (mb_type == NM_TYPE_SETTING_GSM) {
			NMSettingGsm *s_gsm;

871 872 873 874 875 876 877
			id = get_next_available_name (list, _("GSM connection %d"));
			g_object_set (s_con,
					    NM_SETTING_CONNECTION_ID, id,
					    NM_SETTING_CONNECTION_TYPE, NM_SETTING_GSM_SETTING_NAME,
					    NM_SETTING_CONNECTION_AUTOCONNECT, FALSE,
					    NULL);
			g_free (id);
878 879 880 881 882

			add_default_serial_setting (connection);

			type_setting = nm_setting_gsm_new ();
			s_gsm = NM_SETTING_GSM (type_setting);
883
			/* De-facto standard for GSM */
884
			g_object_set (s_gsm, NM_SETTING_GSM_NUMBER, "*99#", NULL);
885 886

			nm_connection_add_setting (connection, nm_setting_ppp_new ());
887 888 889
		} else if (mb_type == NM_TYPE_SETTING_CDMA) {
			NMSettingCdma *s_cdma;

890 891 892 893 894 895 896
			id = get_next_available_name (list, _("CDMA connection %d"));
			g_object_set (s_con,
					    NM_SETTING_CONNECTION_ID, id,
					    NM_SETTING_CONNECTION_TYPE, NM_SETTING_CDMA_SETTING_NAME,
					    NM_SETTING_CONNECTION_AUTOCONNECT, FALSE,
					    NULL);
			g_free (id);
897 898 899 900 901

			add_default_serial_setting (connection);

			type_setting = nm_setting_cdma_new ();
			s_cdma = NM_SETTING_CDMA (type_setting);
902 903 904

			/* De-facto standard for CDMA */
			g_object_set (s_cdma, NM_SETTING_CDMA_NUMBER, "#777", NULL);
905 906

			nm_connection_add_setting (connection, nm_setting_ppp_new ());
907 908 909
		} else {
			/* user canceled; do nothing */
		}
910
	} else if (ctype == NM_TYPE_SETTING_VPN) {
911 912 913 914 915
		char *service = NULL;

		service = vpn_ask_connection_type ();
		if (service) {
			NMSettingVPN *s_vpn;
916

917 918 919 920 921 922 923
			id = get_next_available_name (list, _("VPN connection %d"));
			g_object_set (s_con,
					    NM_SETTING_CONNECTION_ID, id,
					    NM_SETTING_CONNECTION_TYPE, NM_SETTING_VPN_SETTING_NAME,
					    NM_SETTING_CONNECTION_AUTOCONNECT, FALSE,
					    NULL);
			g_free (id);
924 925 926

			type_setting = nm_setting_vpn_new ();
			s_vpn = NM_SETTING_VPN (type_setting);
927 928
			g_object_set (s_vpn, NM_SETTING_VPN_SERVICE_TYPE, service, NULL);
			g_free (service);
929
		}		
930
	} else if (ctype == NM_TYPE_SETTING_PPPOE) {
931 932 933 934 935 936 937
		id = get_next_available_name (list, _("DSL connection %d"));
		g_object_set (s_con,
				    NM_SETTING_CONNECTION_ID, id,
				    NM_SETTING_CONNECTION_TYPE, NM_SETTING_PPPOE_SETTING_NAME,
				    NM_SETTING_CONNECTION_AUTOCONNECT, FALSE,
				    NULL);
		g_free (id);
938 939

		type_setting = nm_setting_pppoe_new ();
940 941 942

		nm_connection_add_setting (connection, nm_setting_wired_new ());
		nm_connection_add_setting (connection, nm_setting_ppp_new ());
943 944 945 946 947 948 949 950 951 952 953 954 955 956
	} else {
		g_warning ("%s: unhandled connection type '%s'", __func__, g_type_name (ctype)); 
	}

	if (type_setting) {
		nm_connection_add_setting (connection, type_setting);
	} else {
		g_object_unref (connection);
		connection = NULL;
	}

	return connection;
}

957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
typedef struct {
	const char *type;
	GtkTreeView *treeview;
} LookupTreeViewInfo;

static void
lookup_treeview (gpointer key, gpointer value, gpointer user_data)
{
	LookupTreeViewInfo *info = (LookupTreeViewInfo *) user_data;

	if (!info->type && info->treeview == value)
		info->type = (const char *) key;
}

static const char *
get_connection_type_from_treeview (NMConnectionList *self,
							GtkTreeView *treeview)
{
	LookupTreeViewInfo info;

	info.type = NULL;
	info.treeview = treeview;

	g_hash_table_foreach (self->treeviews, lookup_treeview, &info);

	return info.type;
}

Rodrigo Moya's avatar
Rodrigo Moya committed
985
static void
986
add_connection_clicked (GtkButton *button, gpointer user_data)
Rodrigo Moya's avatar
Rodrigo Moya committed
987
{
988 989
	ActionInfo *info = (ActionInfo *) user_data;
	const char *connection_type;
Dan Williams's avatar