/* dbus-helper.c generated by valac 0.11.6, the Vala compiler
 * generated from dbus-helper.vala, do not modify */

/*
 * Copyright (C) 2010 Michal Hruby <michal.mhr@gmail.com>
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
 *
 * Authored by Michal Hruby <michal.mhr@gmail.com>
 *
 */

#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <dbus/dbus-glib.h>


#define DOCK_MANAGER_TYPE_DBUS_WATCHER (dock_manager_dbus_watcher_get_type ())
#define DOCK_MANAGER_DBUS_WATCHER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), DOCK_MANAGER_TYPE_DBUS_WATCHER, DockManagerDBusWatcher))
#define DOCK_MANAGER_DBUS_WATCHER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), DOCK_MANAGER_TYPE_DBUS_WATCHER, DockManagerDBusWatcherClass))
#define DOCK_MANAGER_IS_DBUS_WATCHER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), DOCK_MANAGER_TYPE_DBUS_WATCHER))
#define DOCK_MANAGER_IS_DBUS_WATCHER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DOCK_MANAGER_TYPE_DBUS_WATCHER))
#define DOCK_MANAGER_DBUS_WATCHER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), DOCK_MANAGER_TYPE_DBUS_WATCHER, DockManagerDBusWatcherClass))

typedef struct _DockManagerDBusWatcher DockManagerDBusWatcher;
typedef struct _DockManagerDBusWatcherClass DockManagerDBusWatcherClass;
typedef struct _DockManagerDBusWatcherPrivate DockManagerDBusWatcherPrivate;
#define _g_hash_table_unref0(var) ((var == NULL) ? NULL : (var = (g_hash_table_unref (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _dbus_g_connection_unref0(var) ((var == NULL) ? NULL : (var = (dbus_g_connection_unref (var), NULL)))

struct _DockManagerDBusWatcher {
	GObject parent_instance;
	DockManagerDBusWatcherPrivate * priv;
};

struct _DockManagerDBusWatcherClass {
	GObjectClass parent_class;
};

struct _DockManagerDBusWatcherPrivate {
	GHashTable* owned_names;
	DBusGProxy* bus_object;
	GRegex* unnamed_bus_object;
};


static gpointer dock_manager_dbus_watcher_parent_class = NULL;
static DockManagerDBusWatcher* dock_manager_dbus_watcher_instance;
static DockManagerDBusWatcher* dock_manager_dbus_watcher_instance = NULL;

GType dock_manager_dbus_watcher_get_type (void) G_GNUC_CONST;
#define DOCK_MANAGER_DBUS_WATCHER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), DOCK_MANAGER_TYPE_DBUS_WATCHER, DockManagerDBusWatcherPrivate))
enum  {
	DOCK_MANAGER_DBUS_WATCHER_DUMMY_PROPERTY
};
DockManagerDBusWatcher* dock_manager_dbus_watcher_get_default (void);
static DockManagerDBusWatcher* dock_manager_dbus_watcher_new (void);
static DockManagerDBusWatcher* dock_manager_dbus_watcher_construct (GType object_type);
static void dock_manager_dbus_watcher_name_owner_changed (DockManagerDBusWatcher* self, DBusGProxy* sender, const gchar* name, const gchar* old_owner, const gchar* new_owner);
gboolean dock_manager_dbus_watcher_name_has_owner (DockManagerDBusWatcher* self, const gchar* name);
static GObject * dock_manager_dbus_watcher_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties);
static void _g_free0_ (gpointer var);
static void _dock_manager_dbus_watcher_name_owner_changed_dynamic_NameOwnerChanged0_ (DBusGProxy* _sender, const gchar* name, const gchar* old_owner, const gchar* new_owner, gpointer self);
static void g_cclosure_user_marshal_VOID__STRING_STRING_STRING (GClosure * closure, GValue * return_value, guint n_param_values, const GValue * param_values, gpointer invocation_hint, gpointer marshal_data);
void _dynamic_NameOwnerChanged1_connect (gpointer obj, const char * signal_name, GCallback handler, gpointer data);
static gchar** _dynamic_list_names1 (DBusGProxy* self, int* result_length1, GError** error);
static void dock_manager_dbus_watcher_finalize (GObject* obj);
static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func);


DockManagerDBusWatcher* dock_manager_dbus_watcher_get_default (void) {
	DockManagerDBusWatcher* result = NULL;
	if (dock_manager_dbus_watcher_instance == NULL) {
		DockManagerDBusWatcher* _tmp0_ = NULL;
		DockManagerDBusWatcher* _tmp1_;
		_tmp0_ = dock_manager_dbus_watcher_new ();
		_tmp1_ = _tmp0_;
		_g_object_unref0 (dock_manager_dbus_watcher_instance);
		dock_manager_dbus_watcher_instance = _tmp1_;
	}
	result = dock_manager_dbus_watcher_instance;
	return result;
}


static DockManagerDBusWatcher* dock_manager_dbus_watcher_construct (GType object_type) {
	DockManagerDBusWatcher * self = NULL;
	self = (DockManagerDBusWatcher*) g_object_new (object_type, NULL);
	return self;
}


static DockManagerDBusWatcher* dock_manager_dbus_watcher_new (void) {
	return dock_manager_dbus_watcher_construct (DOCK_MANAGER_TYPE_DBUS_WATCHER);
}


static void dock_manager_dbus_watcher_name_owner_changed (DockManagerDBusWatcher* self, DBusGProxy* sender, const gchar* name, const gchar* old_owner, const gchar* new_owner) {
	gboolean _tmp0_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (sender != NULL);
	g_return_if_fail (name != NULL);
	g_return_if_fail (old_owner != NULL);
	g_return_if_fail (new_owner != NULL);
	_tmp0_ = g_regex_match (self->priv->unnamed_bus_object, name, 0, NULL);
	if (_tmp0_ == FALSE) {
		if (g_strcmp0 (new_owner, "") == 0) {
			gchar* _tmp1_;
			GQuark _tmp2_;
			_tmp1_ = g_strdup (name);
			g_hash_table_replace (self->priv->owned_names, _tmp1_, GINT_TO_POINTER (0));
			_tmp2_ = g_quark_try_string (name);
			if (_tmp2_ != 0) {
				gchar* _tmp3_ = NULL;
				gchar* _tmp4_;
				_tmp3_ = g_strdup_printf ("name-disappeared::%s", name);
				_tmp4_ = _tmp3_;
				g_signal_emit_by_name (self, _tmp4_, name, NULL);
				_g_free0 (_tmp4_);
			} else {
				g_signal_emit_by_name (self, "name-disappeared", name);
			}
		} else {
			if (g_strcmp0 (old_owner, "") == 0) {
				gchar* _tmp5_;
				GQuark _tmp6_;
				_tmp5_ = g_strdup (name);
				g_hash_table_replace (self->priv->owned_names, _tmp5_, GINT_TO_POINTER (1));
				_tmp6_ = g_quark_try_string (name);
				if (_tmp6_ != 0) {
					gchar* _tmp7_ = NULL;
					gchar* _tmp8_;
					_tmp7_ = g_strdup_printf ("name-appeared::%s", name);
					_tmp8_ = _tmp7_;
					g_signal_emit_by_name (self, _tmp8_, name, NULL);
					_g_free0 (_tmp8_);
				} else {
					g_signal_emit_by_name (self, "name-appeared", name);
				}
			}
		}
	}
}


gboolean dock_manager_dbus_watcher_name_has_owner (DockManagerDBusWatcher* self, const gchar* name) {
	gboolean result = FALSE;
	gconstpointer _tmp0_ = NULL;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (name != NULL, FALSE);
	_tmp0_ = g_hash_table_lookup (self->priv->owned_names, name);
	result = GPOINTER_TO_INT (_tmp0_) != 0;
	return result;
}


static void _g_free0_ (gpointer var) {
	var = (g_free (var), NULL);
}


static void _dock_manager_dbus_watcher_name_owner_changed_dynamic_NameOwnerChanged0_ (DBusGProxy* _sender, const gchar* name, const gchar* old_owner, const gchar* new_owner, gpointer self) {
	dock_manager_dbus_watcher_name_owner_changed (self, _sender, name, old_owner, new_owner);
}


static void g_cclosure_user_marshal_VOID__STRING_STRING_STRING (GClosure * closure, GValue * return_value, guint n_param_values, const GValue * param_values, gpointer invocation_hint, gpointer marshal_data) {
	typedef void (*GMarshalFunc_VOID__STRING_STRING_STRING) (gpointer data1, const char* arg_1, const char* arg_2, const char* arg_3, gpointer data2);
	register GMarshalFunc_VOID__STRING_STRING_STRING callback;
	register GCClosure * cc;
	register gpointer data1, data2;
	cc = (GCClosure *) closure;
	g_return_if_fail (n_param_values == 4);
	if (G_CCLOSURE_SWAP_DATA (closure)) {
		data1 = closure->data;
		data2 = param_values->data[0].v_pointer;
	} else {
		data1 = param_values->data[0].v_pointer;
		data2 = closure->data;
	}
	callback = (GMarshalFunc_VOID__STRING_STRING_STRING) (marshal_data ? marshal_data : cc->callback);
	callback (data1, g_value_get_string (param_values + 1), g_value_get_string (param_values + 2), g_value_get_string (param_values + 3), data2);
}


void _dynamic_NameOwnerChanged1_connect (gpointer obj, const char * signal_name, GCallback handler, gpointer data) {
	dbus_g_object_register_marshaller (g_cclosure_user_marshal_VOID__STRING_STRING_STRING, G_TYPE_NONE, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID);
	dbus_g_proxy_add_signal (obj, "NameOwnerChanged", G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID);
	dbus_g_proxy_connect_signal (obj, "NameOwnerChanged", handler, data, NULL);
}


static gchar** _dynamic_list_names1 (DBusGProxy* self, int* result_length1, GError** error) {
	gchar** result;
	dbus_g_proxy_call (self, "ListNames", error, G_TYPE_INVALID, G_TYPE_STRV, &result, G_TYPE_INVALID);
	if (*error) {
		return NULL;
	}
	*result_length1 = g_strv_length (result);
	return result;
}


static GObject * dock_manager_dbus_watcher_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) {
	GObject * obj;
	GObjectClass * parent_class;
	DockManagerDBusWatcher * self;
	GHashTable* _tmp0_ = NULL;
	GHashTable* _tmp1_;
	DBusGConnection* _tmp2_ = NULL;
	DBusGConnection* connection;
	DBusGProxy* _tmp3_ = NULL;
	DBusGProxy* _tmp4_;
	GRegex* _tmp5_ = NULL;
	GRegex* _tmp6_;
	GRegex* _tmp7_;
	gint _tmp8_;
	gchar** _tmp9_ = NULL;
	gchar** all_names;
	gint all_names_length1;
	gint _all_names_size_;
	GError * _inner_error_ = NULL;
	parent_class = G_OBJECT_CLASS (dock_manager_dbus_watcher_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = DOCK_MANAGER_DBUS_WATCHER (obj);
	_tmp0_ = g_hash_table_new_full (g_str_hash, g_str_equal, _g_free0_, NULL);
	_tmp1_ = _tmp0_;
	_g_hash_table_unref0 (self->priv->owned_names);
	self->priv->owned_names = _tmp1_;
	_tmp2_ = dbus_g_bus_get (DBUS_BUS_SESSION, &_inner_error_);
	connection = _tmp2_;
	if (_inner_error_ != NULL) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
	}
	_tmp3_ = dbus_g_proxy_new_for_name (connection, "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus");
	_tmp4_ = _tmp3_;
	_g_object_unref0 (self->priv->bus_object);
	self->priv->bus_object = _tmp4_;
	_dynamic_NameOwnerChanged1_connect (self->priv->bus_object, "NameOwnerChanged", (GCallback) _dock_manager_dbus_watcher_name_owner_changed_dynamic_NameOwnerChanged0_, self);
	_tmp5_ = g_regex_new ("^:\\d+.\\d+$", 0, 0, &_inner_error_);
	_tmp6_ = _tmp5_;
	if (_inner_error_ != NULL) {
		_dbus_g_connection_unref0 (connection);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
	}
	_tmp7_ = _tmp6_;
	_g_regex_unref0 (self->priv->unnamed_bus_object);
	self->priv->unnamed_bus_object = _tmp7_;
	_tmp9_ = _dynamic_list_names1 (self->priv->bus_object, &_tmp8_, &_inner_error_);
	all_names = _tmp9_;
	all_names_length1 = _tmp8_;
	_all_names_size_ = _tmp8_;
	if (_inner_error_ != NULL) {
		_dbus_g_connection_unref0 (connection);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
	}
	{
		gchar** s_collection;
		int s_collection_length1;
		int s_it;
		s_collection = all_names;
		s_collection_length1 = all_names_length1;
		for (s_it = 0; s_it < all_names_length1; s_it = s_it + 1) {
			gchar* _tmp10_;
			gchar* s;
			_tmp10_ = g_strdup (s_collection[s_it]);
			s = _tmp10_;
			{
				gboolean _tmp11_;
				_tmp11_ = g_regex_match (self->priv->unnamed_bus_object, s, 0, NULL);
				if (_tmp11_ == FALSE) {
					gchar* _tmp12_;
					_tmp12_ = g_strdup (s);
					g_hash_table_insert (self->priv->owned_names, _tmp12_, GINT_TO_POINTER (1));
				}
				_g_free0 (s);
			}
		}
	}
	all_names = (_vala_array_free (all_names, all_names_length1, (GDestroyNotify) g_free), NULL);
	_dbus_g_connection_unref0 (connection);
	return obj;
}


static void dock_manager_dbus_watcher_class_init (DockManagerDBusWatcherClass * klass) {
	dock_manager_dbus_watcher_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (DockManagerDBusWatcherPrivate));
	G_OBJECT_CLASS (klass)->constructor = dock_manager_dbus_watcher_constructor;
	G_OBJECT_CLASS (klass)->finalize = dock_manager_dbus_watcher_finalize;
	g_signal_new ("name_appeared", DOCK_MANAGER_TYPE_DBUS_WATCHER, G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED, 0, NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE, 1, G_TYPE_STRING);
	g_signal_new ("name_disappeared", DOCK_MANAGER_TYPE_DBUS_WATCHER, G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED, 0, NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE, 1, G_TYPE_STRING);
}


static void dock_manager_dbus_watcher_instance_init (DockManagerDBusWatcher * self) {
	self->priv = DOCK_MANAGER_DBUS_WATCHER_GET_PRIVATE (self);
}


static void dock_manager_dbus_watcher_finalize (GObject* obj) {
	DockManagerDBusWatcher * self;
	self = DOCK_MANAGER_DBUS_WATCHER (obj);
	_g_hash_table_unref0 (self->priv->owned_names);
	_g_object_unref0 (self->priv->bus_object);
	_g_regex_unref0 (self->priv->unnamed_bus_object);
	G_OBJECT_CLASS (dock_manager_dbus_watcher_parent_class)->finalize (obj);
}


GType dock_manager_dbus_watcher_get_type (void) {
	static volatile gsize dock_manager_dbus_watcher_type_id__volatile = 0;
	if (g_once_init_enter (&dock_manager_dbus_watcher_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (DockManagerDBusWatcherClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) dock_manager_dbus_watcher_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (DockManagerDBusWatcher), 0, (GInstanceInitFunc) dock_manager_dbus_watcher_instance_init, NULL };
		GType dock_manager_dbus_watcher_type_id;
		dock_manager_dbus_watcher_type_id = g_type_register_static (G_TYPE_OBJECT, "DockManagerDBusWatcher", &g_define_type_info, 0);
		g_once_init_leave (&dock_manager_dbus_watcher_type_id__volatile, dock_manager_dbus_watcher_type_id);
	}
	return dock_manager_dbus_watcher_type_id__volatile;
}


static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	if ((array != NULL) && (destroy_func != NULL)) {
		int i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}


static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}



