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

23 24 25 26 27 28 29 30
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <string.h>
#include <stdlib.h>
#include <signal.h>

31
#include <dbus/dbus.h>
32
#include <gtk/gtk.h>
33
#include <glib/gi18n-lib.h>
34
#include <glib.h>
35
#include <glib-object.h>
36
#include <dbus/dbus-glib.h>
37
#include <dbus/dbus-glib-lowlevel.h>
38

39
#include <nm-setting-wired.h>
40
#include "nm-connection-list.h"
Rodrigo Moya's avatar
Rodrigo Moya committed
41

42 43
static GMainLoop *loop = NULL;

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
#define ARG_TYPE "type"

#define DBUS_TYPE_G_MAP_OF_VARIANT          (dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE))

#define NM_CE_DBUS_SERVICE_NAME "org.freedesktop.NetworkManager.Gnome.ConnectionEditor"

#define NM_TYPE_CE_SERVICE            (nm_ce_service_get_type ())
#define NM_CE_SERVICE(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_CE_SERVICE, NMCEService))
#define NM_CE_SERVICE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_CE_SERVICE, NMCEServiceClass))
#define NM_IS_CE_SERVICE(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_CE_SERVICE))
#define NM_IS_CE_SERVICE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), NM_TYPE_CE_SERVICE))
#define NM_CE_SERVICE_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_CE_SERVICE, NMCEServiceClass))

typedef struct {
	GObject parent;
	NMConnectionList *list;
} NMCEService;

typedef struct {
	GObjectClass parent;
} NMCEServiceClass;

GType nm_ce_service_get_type (void);

G_DEFINE_TYPE (NMCEService, nm_ce_service, G_TYPE_OBJECT)

static gboolean impl_start (NMCEService *self, GHashTable *args, GError **error);

#include "nm-connection-editor-service-glue.h"

static NMCEService *
nm_ce_service_new (DBusGConnection *bus, DBusGProxy *proxy, NMConnectionList *list)
{
	GObject *object;
	DBusConnection *connection;
	GError *err = NULL;
	guint32 result;

	g_return_val_if_fail (bus != NULL, NULL);
	g_return_val_if_fail (proxy != NULL, NULL);

	object = g_object_new (NM_TYPE_CE_SERVICE, NULL);
	if (!object)
		return NULL;

	NM_CE_SERVICE (object)->list = list;

	dbus_connection_set_change_sigpipe (TRUE);
	connection = dbus_g_connection_get_connection (bus);
	dbus_connection_set_exit_on_disconnect (connection, FALSE);

	/* Register our single-instance service.  Don't care if it fails. */
	if (!dbus_g_proxy_call (proxy, "RequestName", &err,
					    G_TYPE_STRING, NM_CE_DBUS_SERVICE_NAME,
					    G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
					    G_TYPE_INVALID,
					    G_TYPE_UINT, &result,
					    G_TYPE_INVALID)) {
		g_warning ("Could not acquire the connection editor service.\n"
		            "  Message: '%s'", err->message);
		g_error_free (err);
	} else {
		if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
			g_warning ("Could not acquire the connection editor service as it is already taken.");
		else {
			/* success */
			dbus_g_connection_register_g_object (bus, "/", object);
		}
	}

	return (NMCEService *) object;
}

static void
nm_ce_service_init (NMCEService *self)
{
}

static void
nm_ce_service_class_init (NMCEServiceClass *config_class)
{
	dbus_g_object_type_install_info (G_TYPE_FROM_CLASS (config_class),
									 &dbus_glib_nm_connection_editor_service_object_info);
}

static gboolean
impl_start (NMCEService *self, GHashTable *table, GError **error)
{
	GValue *value;

	value = g_hash_table_lookup (table, ARG_TYPE);
	if (value && G_VALUE_HOLDS_STRING (value))
		nm_connection_list_set_type (self->list, g_value_get_string (value));
137
	nm_connection_list_present (self->list);
138 139 140 141 142

	return TRUE;
}


143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
static void
signal_handler (int signo)
{
	if (signo == SIGINT || signo == SIGTERM) {
		g_message ("Caught signal %d, shutting down...", signo);
		g_main_loop_quit (loop);
	}
}

static void
setup_signals (void)
{
	struct sigaction action;
	sigset_t mask;

	sigemptyset (&mask);
	action.sa_handler = signal_handler;
	action.sa_mask = mask;
	action.sa_flags = 0;
	sigaction (SIGTERM,  &action, NULL);
	sigaction (SIGINT,  &action, NULL);
}

166 167
static gboolean
try_existing_instance (DBusGConnection *bus, DBusGProxy *proxy, const char *type)
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
	gboolean has_owner = FALSE;
	DBusGProxy *instance;
	GHashTable *args;
	GValue type_value = { 0, };
	gboolean success = FALSE;

	if (!dbus_g_proxy_call (proxy, "NameHasOwner", NULL,
	                        G_TYPE_STRING, NM_CE_DBUS_SERVICE_NAME, G_TYPE_INVALID,
	                        G_TYPE_BOOLEAN, &has_owner, G_TYPE_INVALID))
		return FALSE;

	if (!has_owner)
		return FALSE;

	/* Send arguments to existing process */
	instance = dbus_g_proxy_new_for_name (bus,
	                                      NM_CE_DBUS_SERVICE_NAME,
	                                      "/",
	                                      NM_CE_DBUS_SERVICE_NAME);
	if (!instance)
		return FALSE;

	args = g_hash_table_new (g_str_hash, g_str_equal);
	if (type) {
		g_value_init (&type_value, G_TYPE_STRING);
		g_value_set_static_string (&type_value, type);
		g_hash_table_insert (args, "type", &type_value);
	}

	if (dbus_g_proxy_call (instance, "Start", NULL,
	                       DBUS_TYPE_G_MAP_OF_VARIANT, args, G_TYPE_INVALID,
	                       G_TYPE_INVALID))
		success = TRUE;

	g_hash_table_destroy (args);
	g_object_unref (instance);
	return success;
206 207
}

Rodrigo Moya's avatar
Rodrigo Moya committed
208 209 210
int
main (int argc, char *argv[])
{
211 212
	GOptionContext *opt_ctx;
	GError *error = NULL;
213
	NMConnectionList *list;
214
	DBusGConnection *bus;
215
	char *type = NULL;
216 217
	NMCEService *service = NULL;
	DBusGProxy *proxy = NULL;
218 219

	GOptionEntry entries[] = {
220
		{ ARG_TYPE, 0, 0, G_OPTION_ARG_STRING, &type, "Type of connection to show at launch", NM_SETTING_WIRED_SETTING_NAME },
221 222
		{ NULL }
	};
Rodrigo Moya's avatar
Rodrigo Moya committed
223

224 225
	bindtextdomain (GETTEXT_PACKAGE, NMALOCALEDIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
Rodrigo Moya's avatar
Rodrigo Moya committed
226
	gtk_init (&argc, &argv);
227
	textdomain (GETTEXT_PACKAGE);
Rodrigo Moya's avatar
Rodrigo Moya committed
228 229 230 231

	/* parse arguments: an idea is to use gconf://$setting_name / system://$setting_name to
	   allow this program to work with both GConf and system-wide settings */

232 233 234 235 236 237 238 239 240 241 242 243
	opt_ctx = g_option_context_new (NULL);
	g_option_context_set_summary (opt_ctx, "Allows users to view and edit network connection settings");
	g_option_context_add_main_entries (opt_ctx, entries, NULL);

	if (!g_option_context_parse (opt_ctx, &argc, &argv, &error)) {
		g_warning ("%s\n", error->message);
		g_error_free (error);
		return 1;
	}

	g_option_context_free (opt_ctx);

244 245 246 247 248 249 250 251 252 253 254 255
	/* Inits the dbus-glib type system too */
	bus = dbus_g_bus_get (DBUS_BUS_SESSION, NULL);
	proxy = dbus_g_proxy_new_for_name (bus,
									 "org.freedesktop.DBus",
									 "/org/freedesktop/DBus",
									 "org.freedesktop.DBus");

	/* Check for an existing instance on the bus */
	if (proxy) {
		if (try_existing_instance (bus, proxy, type))
			goto exit;
	}
Rodrigo Moya's avatar
Rodrigo Moya committed
256

257 258
	loop = g_main_loop_new (NULL, FALSE);

259
	list = nm_connection_list_new (type);
260 261 262 263 264
	if (!list) {
		g_warning ("Failed to initialize the UI, exiting...");
		return 1;
	}

265 266 267 268 269
	/* Create our single-instance-app service if we can */
	if (proxy)
		service = nm_ce_service_new (bus, proxy, list);

	g_signal_connect_swapped (G_OBJECT (list), "done", G_CALLBACK (g_main_loop_quit), loop);
270
	nm_connection_list_run (list);
Rodrigo Moya's avatar
Rodrigo Moya committed
271

272 273
	setup_signals ();
	g_main_loop_run (loop);
274

275
	g_object_unref (list);
276 277
	if (service)
		g_object_unref (service);
278

279 280 281 282
exit:
	if (proxy)
		g_object_unref (proxy);
	dbus_g_connection_unref (bus);
Rodrigo Moya's avatar
Rodrigo Moya committed
283 284
	return 0;
}
285