/* Main.c generated by valac 0.56.3, the Vala compiler
 * generated from Main.vala, do not modify */

/*
 * Copyright (c) 2011-2015 Wingpanel Developers (http://launchpad.net/wingpanel)
 *
 * 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 Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 */
/*
 *   This plugin adds a dbus-interface to gala that provides additional information
 *   about windows and workspaces for the panel.
 */

#include "wingpanel-interface.h"
#include <gio/gio.h>
#include <gala.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <meta/meta-plugin.h>
#include <meta/display.h>
#include <glib-object.h>

#define WINGPANEL_INTERFACE_MAIN_DBUS_NAME "org.pantheon.gala.WingpanelInterface"
#define WINGPANEL_INTERFACE_MAIN_DBUS_PATH "/org/pantheon/gala/WingpanelInterface"

enum  {
	WINGPANEL_INTERFACE_MAIN_0_PROPERTY,
	WINGPANEL_INTERFACE_MAIN_NUM_PROPERTIES
};
static GParamSpec* wingpanel_interface_main_properties[WINGPANEL_INTERFACE_MAIN_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

struct _WingpanelInterfaceMainPrivate {
	GDBusConnection* dbus_connection;
};

static gint WingpanelInterfaceMain_private_offset;
static gpointer wingpanel_interface_main_parent_class = NULL;
GalaWindowManager* wingpanel_interface_main_wm = NULL;
MetaDisplay* wingpanel_interface_main_display = NULL;

static void wingpanel_interface_main_real_initialize (GalaPlugin* base,
                                               GalaWindowManager* _wm);
static void wingpanel_interface_main_on_bus_aquired (WingpanelInterfaceMain* self,
                                              GDBusConnection* connection);
static void _wingpanel_interface_main_on_bus_aquired_gbus_acquired_callback (GDBusConnection* connection,
                                                                      const gchar* name,
                                                                      gpointer self);
static void __lambda18_ (WingpanelInterfaceMain* self);
static void ___lambda18__gbus_name_lost_callback (GDBusConnection* connection,
                                           const gchar* name,
                                           gpointer self);
static void wingpanel_interface_main_real_destroy (GalaPlugin* base);
static void wingpanel_interface_main_finalize (GObject * obj);
static GType wingpanel_interface_main_get_type_once (void);

static inline gpointer
wingpanel_interface_main_get_instance_private (WingpanelInterfaceMain* self)
{
	return G_STRUCT_MEMBER_P (self, WingpanelInterfaceMain_private_offset);
}

static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

static void
_wingpanel_interface_main_on_bus_aquired_gbus_acquired_callback (GDBusConnection* connection,
                                                                 const gchar* name,
                                                                 gpointer self)
{
	wingpanel_interface_main_on_bus_aquired ((WingpanelInterfaceMain*) self, connection);
}

static void
__lambda18_ (WingpanelInterfaceMain* self)
{
	g_warning ("Main.vala:47: Aquirering \"%s\" failed.", WINGPANEL_INTERFACE_MAIN_DBUS_NAME);
}

static void
___lambda18__gbus_name_lost_callback (GDBusConnection* connection,
                                      const gchar* name,
                                      gpointer self)
{
	__lambda18_ ((WingpanelInterfaceMain*) self);
}

static void
wingpanel_interface_main_real_initialize (GalaPlugin* base,
                                          GalaWindowManager* _wm)
{
	WingpanelInterfaceMain * self;
	GalaWindowManager* _tmp0_;
	GalaWindowManager* _tmp1_;
	MetaDisplay* _tmp2_;
	MetaDisplay* _tmp3_;
	self = (WingpanelInterfaceMain*) base;
	g_return_if_fail (_wm != NULL);
	if (_wm == NULL) {
		return;
	}
	_tmp0_ = _g_object_ref0 (_wm);
	_g_object_unref0 (wingpanel_interface_main_wm);
	wingpanel_interface_main_wm = _tmp0_;
	_tmp1_ = wingpanel_interface_main_wm;
	_tmp2_ = meta_plugin_get_display ((MetaPlugin*) _tmp1_);
	_tmp3_ = _g_object_ref0 (_tmp2_);
	_g_object_unref0 (wingpanel_interface_main_display);
	wingpanel_interface_main_display = _tmp3_;
	g_bus_own_name_with_closures (G_BUS_TYPE_SESSION, WINGPANEL_INTERFACE_MAIN_DBUS_NAME, G_BUS_NAME_OWNER_FLAGS_NONE, (GClosure*) ((_wingpanel_interface_main_on_bus_aquired_gbus_acquired_callback == NULL) ? NULL : g_cclosure_new ((GCallback) _wingpanel_interface_main_on_bus_aquired_gbus_acquired_callback, g_object_ref (self), (GClosureNotify) g_object_unref)), (GClosure*) ((NULL == NULL) ? NULL : g_cclosure_new ((GCallback) NULL, NULL, (GClosureNotify) NULL)), (GClosure*) ((___lambda18__gbus_name_lost_callback == NULL) ? NULL : g_cclosure_new ((GCallback) ___lambda18__gbus_name_lost_callback, g_object_ref (self), (GClosureNotify) g_object_unref)));
}

static void
wingpanel_interface_main_real_destroy (GalaPlugin* base)
{
	WingpanelInterfaceMain * self;
	GError* _inner_error0_ = NULL;
	self = (WingpanelInterfaceMain*) base;
	{
		GDBusConnection* _tmp0_;
		_tmp0_ = self->priv->dbus_connection;
		if (_tmp0_ != NULL) {
			GDBusConnection* _tmp1_;
			_tmp1_ = self->priv->dbus_connection;
			g_dbus_connection_close_sync (_tmp1_, NULL, &_inner_error0_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch0_g_error;
			}
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp2_;
		const gchar* _tmp3_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp2_ = e;
		_tmp3_ = _tmp2_->message;
		g_warning ("Main.vala:56: Closing DBus service failed: %s", _tmp3_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
}

static void
wingpanel_interface_main_on_bus_aquired (WingpanelInterfaceMain* self,
                                         GDBusConnection* connection)
{
	GDBusConnection* _tmp0_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (connection != NULL);
	_tmp0_ = _g_object_ref0 (connection);
	_g_object_unref0 (self->priv->dbus_connection);
	self->priv->dbus_connection = _tmp0_;
	{
		WingpanelInterfaceDBusServer* server = NULL;
		WingpanelInterfaceDBusServer* _tmp1_;
		GDBusConnection* _tmp2_;
		_tmp1_ = wingpanel_interface_dbus_server_new ();
		server = _tmp1_;
		_tmp2_ = self->priv->dbus_connection;
		wingpanel_interface_dbus_server_register_object (server, _tmp2_, WINGPANEL_INTERFACE_MAIN_DBUS_PATH, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_object_unref0 (server);
			goto __catch0_g_error;
		}
		g_debug ("Main.vala:68: DBus service registered.");
		_g_object_unref0 (server);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp3_;
		const gchar* _tmp4_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp3_ = e;
		_tmp4_ = _tmp3_->message;
		g_warning ("Main.vala:70: Registering DBus service failed: %s", _tmp4_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
}

WingpanelInterfaceMain*
wingpanel_interface_main_construct (GType object_type)
{
	WingpanelInterfaceMain * self = NULL;
	self = (WingpanelInterfaceMain*) gala_plugin_construct (object_type);
	return self;
}

WingpanelInterfaceMain*
wingpanel_interface_main_new (void)
{
	return wingpanel_interface_main_construct (WINGPANEL_INTERFACE_TYPE_MAIN);
}

static void
wingpanel_interface_main_class_init (WingpanelInterfaceMainClass * klass,
                                     gpointer klass_data)
{
	wingpanel_interface_main_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &WingpanelInterfaceMain_private_offset);
	((GalaPluginClass *) klass)->initialize = (void (*) (GalaPlugin*, GalaWindowManager*)) wingpanel_interface_main_real_initialize;
	((GalaPluginClass *) klass)->destroy = (void (*) (GalaPlugin*)) wingpanel_interface_main_real_destroy;
	G_OBJECT_CLASS (klass)->finalize = wingpanel_interface_main_finalize;
}

static void
wingpanel_interface_main_instance_init (WingpanelInterfaceMain * self,
                                        gpointer klass)
{
	self->priv = wingpanel_interface_main_get_instance_private (self);
	self->priv->dbus_connection = NULL;
}

static void
wingpanel_interface_main_finalize (GObject * obj)
{
	WingpanelInterfaceMain * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, WINGPANEL_INTERFACE_TYPE_MAIN, WingpanelInterfaceMain);
	_g_object_unref0 (self->priv->dbus_connection);
	G_OBJECT_CLASS (wingpanel_interface_main_parent_class)->finalize (obj);
}

static GType
wingpanel_interface_main_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (WingpanelInterfaceMainClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) wingpanel_interface_main_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (WingpanelInterfaceMain), 0, (GInstanceInitFunc) wingpanel_interface_main_instance_init, NULL };
	GType wingpanel_interface_main_type_id;
	wingpanel_interface_main_type_id = g_type_register_static (GALA_TYPE_PLUGIN, "WingpanelInterfaceMain", &g_define_type_info, 0);
	WingpanelInterfaceMain_private_offset = g_type_add_instance_private (wingpanel_interface_main_type_id, sizeof (WingpanelInterfaceMainPrivate));
	return wingpanel_interface_main_type_id;
}

GType
wingpanel_interface_main_get_type (void)
{
	static volatile gsize wingpanel_interface_main_type_id__once = 0;
	if (g_once_init_enter (&wingpanel_interface_main_type_id__once)) {
		GType wingpanel_interface_main_type_id;
		wingpanel_interface_main_type_id = wingpanel_interface_main_get_type_once ();
		g_once_init_leave (&wingpanel_interface_main_type_id__once, wingpanel_interface_main_type_id);
	}
	return wingpanel_interface_main_type_id__once;
}

void
register_plugin (GalaPluginInfo* result)
{
	gchar* _tmp0_;
	gchar* _tmp1_;
	GalaPluginInfo _tmp2_ = {0};
	_tmp0_ = g_strdup ("wingpanel-interface");
	_tmp1_ = g_strdup ("Wingpanel Developers");
	_g_free0 (_tmp2_.name);
	_tmp2_.name = _tmp0_;
	_g_free0 (_tmp2_.author);
	_tmp2_.author = _tmp1_;
	_tmp2_.plugin_type = WINGPANEL_INTERFACE_TYPE_MAIN;
	_tmp2_.provides = GALA_PLUGIN_FUNCTION_ADDITION;
	_tmp2_.load_priority = GALA_LOAD_PRIORITY_IMMEDIATE;
	*result = _tmp2_;
	return;
}

