/* IndicatorSorter.c generated by valac 0.56.18-dirty, the Vala compiler
 * generated from IndicatorSorter.vala, do not modify */

/*
 * Copyright 2011-2020 elementary, Inc. (https://elementary.io)
 *
 * 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.
 */

#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gee.h>
#include <gtk/gtk.h>
#include "wingpanel.h"

#define WINGPANEL_SERVICES_INDICATOR_SORTER_UNKNOWN_INDICATOR "xxx-unknown"
#define WINGPANEL_SERVICES_INDICATOR_SORTER_AYATANA_INDICATOR "xxx-ayatana"
#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define WINGPANEL_SERVICES_TYPE_INDICATOR_SORTER (wingpanel_services_indicator_sorter_get_type ())
#define WINGPANEL_SERVICES_INDICATOR_SORTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), WINGPANEL_SERVICES_TYPE_INDICATOR_SORTER, WingpanelServicesIndicatorSorter))
#define WINGPANEL_SERVICES_INDICATOR_SORTER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), WINGPANEL_SERVICES_TYPE_INDICATOR_SORTER, WingpanelServicesIndicatorSorterClass))
#define WINGPANEL_SERVICES_IS_INDICATOR_SORTER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), WINGPANEL_SERVICES_TYPE_INDICATOR_SORTER))
#define WINGPANEL_SERVICES_IS_INDICATOR_SORTER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), WINGPANEL_SERVICES_TYPE_INDICATOR_SORTER))
#define WINGPANEL_SERVICES_INDICATOR_SORTER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), WINGPANEL_SERVICES_TYPE_INDICATOR_SORTER, WingpanelServicesIndicatorSorterClass))

typedef struct _WingpanelServicesIndicatorSorter WingpanelServicesIndicatorSorter;
typedef struct _WingpanelServicesIndicatorSorterClass WingpanelServicesIndicatorSorterClass;
typedef struct _WingpanelServicesIndicatorSorterPrivate WingpanelServicesIndicatorSorterPrivate;
enum  {
	WINGPANEL_SERVICES_INDICATOR_SORTER_0_PROPERTY,
	WINGPANEL_SERVICES_INDICATOR_SORTER_NUM_PROPERTIES
};
static GParamSpec* wingpanel_services_indicator_sorter_properties[WINGPANEL_SERVICES_INDICATOR_SORTER_NUM_PROPERTIES];

#define WINGPANEL_WIDGETS_TYPE_INDICATOR_ENTRY (wingpanel_widgets_indicator_entry_get_type ())
#define WINGPANEL_WIDGETS_INDICATOR_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), WINGPANEL_WIDGETS_TYPE_INDICATOR_ENTRY, WingpanelWidgetsIndicatorEntry))
#define WINGPANEL_WIDGETS_INDICATOR_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), WINGPANEL_WIDGETS_TYPE_INDICATOR_ENTRY, WingpanelWidgetsIndicatorEntryClass))
#define WINGPANEL_WIDGETS_IS_INDICATOR_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), WINGPANEL_WIDGETS_TYPE_INDICATOR_ENTRY))
#define WINGPANEL_WIDGETS_IS_INDICATOR_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), WINGPANEL_WIDGETS_TYPE_INDICATOR_ENTRY))
#define WINGPANEL_WIDGETS_INDICATOR_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), WINGPANEL_WIDGETS_TYPE_INDICATOR_ENTRY, WingpanelWidgetsIndicatorEntryClass))

typedef struct _WingpanelWidgetsIndicatorEntry WingpanelWidgetsIndicatorEntry;
typedef struct _WingpanelWidgetsIndicatorEntryClass WingpanelWidgetsIndicatorEntryClass;
#define _g_free0(var) (var = (g_free (var), NULL))

struct _WingpanelServicesIndicatorSorter {
	GObject parent_instance;
	WingpanelServicesIndicatorSorterPrivate * priv;
};

struct _WingpanelServicesIndicatorSorterClass {
	GObjectClass parent_class;
};

static gpointer wingpanel_services_indicator_sorter_parent_class = NULL;
static GeeHashMap* wingpanel_services_indicator_sorter_indicator_order;
static GeeHashMap* wingpanel_services_indicator_sorter_indicator_order = NULL;

VALA_EXTERN GType wingpanel_services_indicator_sorter_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (WingpanelServicesIndicatorSorter, g_object_unref)
VALA_EXTERN GType wingpanel_widgets_indicator_entry_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (WingpanelWidgetsIndicatorEntry, g_object_unref)
VALA_EXTERN gint wingpanel_services_indicator_sorter_compare_func (WingpanelServicesIndicatorSorter* self,
                                                       WingpanelWidgetsIndicatorEntry* a,
                                                       WingpanelWidgetsIndicatorEntry* b);
static gint wingpanel_services_indicator_sorter_get_order (WingpanelWidgetsIndicatorEntry* node);
static gint wingpanel_services_indicator_sorter_compare_entries_by_name (WingpanelWidgetsIndicatorEntry* a,
                                                                  WingpanelWidgetsIndicatorEntry* b);
VALA_EXTERN WingpanelIndicator* wingpanel_widgets_indicator_entry_get_base_indicator (WingpanelWidgetsIndicatorEntry* self);
VALA_EXTERN WingpanelServicesIndicatorSorter* wingpanel_services_indicator_sorter_new (void);
VALA_EXTERN WingpanelServicesIndicatorSorter* wingpanel_services_indicator_sorter_construct (GType object_type);
static void wingpanel_services_indicator_sorter_finalize (GObject * obj);
static GType wingpanel_services_indicator_sorter_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

gint
wingpanel_services_indicator_sorter_compare_func (WingpanelServicesIndicatorSorter* self,
                                                  WingpanelWidgetsIndicatorEntry* a,
                                                  WingpanelWidgetsIndicatorEntry* b)
{
	gint order = 0;
	gint result;
	g_return_val_if_fail (self != NULL, 0);
	if (a == NULL) {
		gint _tmp0_ = 0;
		if (b == NULL) {
			_tmp0_ = 0;
		} else {
			_tmp0_ = -1;
		}
		result = _tmp0_;
		return result;
	}
	if (b == NULL) {
		result = 1;
		return result;
	}
	order = wingpanel_services_indicator_sorter_get_order (a) - wingpanel_services_indicator_sorter_get_order (b);
	if (order == 0) {
		order = wingpanel_services_indicator_sorter_compare_entries_by_name (a, b);
	}
	result = CLAMP (order, -1, 1);
	return result;
}

static gint
wingpanel_services_indicator_sorter_compare_entries_by_name (WingpanelWidgetsIndicatorEntry* a,
                                                             WingpanelWidgetsIndicatorEntry* b)
{
	GCompareFunc _tmp0_;
	WingpanelIndicator* _tmp1_;
	WingpanelIndicator* _tmp2_;
	const gchar* _tmp3_;
	const gchar* _tmp4_;
	gchar* _tmp5_;
	gchar* _tmp6_;
	WingpanelIndicator* _tmp7_;
	WingpanelIndicator* _tmp8_;
	const gchar* _tmp9_;
	const gchar* _tmp10_;
	gchar* _tmp11_;
	gchar* _tmp12_;
	gint _tmp13_;
	gint result;
	g_return_val_if_fail (a != NULL, 0);
	g_return_val_if_fail (b != NULL, 0);
	_tmp0_ = ((GCompareFunc) g_strcmp0);
	_tmp1_ = wingpanel_widgets_indicator_entry_get_base_indicator (a);
	_tmp2_ = _tmp1_;
	_tmp3_ = wingpanel_indicator_get_code_name (_tmp2_);
	_tmp4_ = _tmp3_;
	_tmp5_ = g_utf8_strdown (_tmp4_, (gssize) -1);
	_tmp6_ = _tmp5_;
	_tmp7_ = wingpanel_widgets_indicator_entry_get_base_indicator (b);
	_tmp8_ = _tmp7_;
	_tmp9_ = wingpanel_indicator_get_code_name (_tmp8_);
	_tmp10_ = _tmp9_;
	_tmp11_ = g_utf8_strdown (_tmp10_, (gssize) -1);
	_tmp12_ = _tmp11_;
	_tmp13_ = _tmp0_ (_tmp6_, _tmp12_);
	_g_free0 (_tmp12_);
	_g_free0 (_tmp6_);
	result = _tmp13_;
	return result;
}

static gint
wingpanel_services_indicator_sorter_get_order (WingpanelWidgetsIndicatorEntry* node)
{
	WingpanelIndicator* _tmp0_;
	WingpanelIndicator* _tmp1_;
	const gchar* _tmp2_;
	const gchar* _tmp3_;
	GeeHashMap* _tmp6_;
	WingpanelIndicator* _tmp7_;
	WingpanelIndicator* _tmp8_;
	const gchar* _tmp9_;
	const gchar* _tmp10_;
	GeeHashMap* _tmp17_;
	gpointer _tmp18_;
	gint result;
	g_return_val_if_fail (node != NULL, 0);
	_tmp0_ = wingpanel_widgets_indicator_entry_get_base_indicator (node);
	_tmp1_ = _tmp0_;
	_tmp2_ = wingpanel_indicator_get_code_name (_tmp1_);
	_tmp3_ = _tmp2_;
	if (g_str_has_prefix (_tmp3_, "ayatana-")) {
		GeeHashMap* _tmp4_;
		gpointer _tmp5_;
		_tmp4_ = wingpanel_services_indicator_sorter_indicator_order;
		_tmp5_ = gee_abstract_map_get ((GeeAbstractMap*) _tmp4_, WINGPANEL_SERVICES_INDICATOR_SORTER_AYATANA_INDICATOR);
		result = (gint) ((gintptr) _tmp5_);
		return result;
	}
	_tmp6_ = wingpanel_services_indicator_sorter_indicator_order;
	_tmp7_ = wingpanel_widgets_indicator_entry_get_base_indicator (node);
	_tmp8_ = _tmp7_;
	_tmp9_ = wingpanel_indicator_get_code_name (_tmp8_);
	_tmp10_ = _tmp9_;
	if (gee_abstract_map_has_key ((GeeAbstractMap*) _tmp6_, _tmp10_)) {
		GeeHashMap* _tmp11_;
		WingpanelIndicator* _tmp12_;
		WingpanelIndicator* _tmp13_;
		const gchar* _tmp14_;
		const gchar* _tmp15_;
		gpointer _tmp16_;
		_tmp11_ = wingpanel_services_indicator_sorter_indicator_order;
		_tmp12_ = wingpanel_widgets_indicator_entry_get_base_indicator (node);
		_tmp13_ = _tmp12_;
		_tmp14_ = wingpanel_indicator_get_code_name (_tmp13_);
		_tmp15_ = _tmp14_;
		_tmp16_ = gee_abstract_map_get ((GeeAbstractMap*) _tmp11_, _tmp15_);
		result = (gint) ((gintptr) _tmp16_);
		return result;
	}
	_tmp17_ = wingpanel_services_indicator_sorter_indicator_order;
	_tmp18_ = gee_abstract_map_get ((GeeAbstractMap*) _tmp17_, WINGPANEL_SERVICES_INDICATOR_SORTER_UNKNOWN_INDICATOR);
	result = (gint) ((gintptr) _tmp18_);
	return result;
}

WingpanelServicesIndicatorSorter*
wingpanel_services_indicator_sorter_construct (GType object_type)
{
	WingpanelServicesIndicatorSorter * self = NULL;
	self = (WingpanelServicesIndicatorSorter*) g_object_new (object_type, NULL);
	return self;
}

WingpanelServicesIndicatorSorter*
wingpanel_services_indicator_sorter_new (void)
{
	return wingpanel_services_indicator_sorter_construct (WINGPANEL_SERVICES_TYPE_INDICATOR_SORTER);
}

static void
wingpanel_services_indicator_sorter_class_init (WingpanelServicesIndicatorSorterClass * klass,
                                                gpointer klass_data)
{
	GeeHashMap* _tmp0_;
	GeeHashMap* _tmp1_;
	GeeHashMap* _tmp2_;
	GeeHashMap* _tmp3_;
	GeeHashMap* _tmp4_;
	GeeHashMap* _tmp5_;
	GeeHashMap* _tmp6_;
	GeeHashMap* _tmp7_;
	GeeHashMap* _tmp8_;
	GeeHashMap* _tmp9_;
	GeeHashMap* _tmp10_;
	GeeHashMap* _tmp11_;
	GeeHashMap* _tmp12_;
	GeeHashMap* _tmp13_;
	GeeHashMap* _tmp14_;
	GeeHashMap* _tmp15_;
	wingpanel_services_indicator_sorter_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->finalize = wingpanel_services_indicator_sorter_finalize;
	_tmp0_ = gee_hash_map_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) g_free, G_TYPE_INT, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
	wingpanel_services_indicator_sorter_indicator_order = _tmp0_;
	_tmp1_ = wingpanel_services_indicator_sorter_indicator_order;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp1_, WINGPANEL_SERVICES_INDICATOR_SORTER_AYATANA_INDICATOR, (gpointer) ((gintptr) 0));
	_tmp2_ = wingpanel_services_indicator_sorter_indicator_order;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp2_, WINGPANEL_SERVICES_INDICATOR_SORTER_UNKNOWN_INDICATOR, (gpointer) ((gintptr) 1));
	_tmp3_ = wingpanel_services_indicator_sorter_indicator_order;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp3_, WINGPANEL_INDICATOR_ACCESSIBILITY, (gpointer) ((gintptr) 2));
	_tmp4_ = wingpanel_services_indicator_sorter_indicator_order;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp4_, WINGPANEL_INDICATOR_NIGHT_LIGHT, (gpointer) ((gintptr) 3));
	_tmp5_ = wingpanel_services_indicator_sorter_indicator_order;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp5_, WINGPANEL_INDICATOR_PRIVACY, (gpointer) ((gintptr) 4));
	_tmp6_ = wingpanel_services_indicator_sorter_indicator_order;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp6_, WINGPANEL_INDICATOR_KEYBOARD, (gpointer) ((gintptr) 5));
	_tmp7_ = wingpanel_services_indicator_sorter_indicator_order;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp7_, WINGPANEL_INDICATOR_SOUND, (gpointer) ((gintptr) 6));
	_tmp8_ = wingpanel_services_indicator_sorter_indicator_order;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp8_, WINGPANEL_INDICATOR_NETWORK, (gpointer) ((gintptr) 7));
	_tmp9_ = wingpanel_services_indicator_sorter_indicator_order;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp9_, WINGPANEL_INDICATOR_BLUETOOTH, (gpointer) ((gintptr) 8));
	_tmp10_ = wingpanel_services_indicator_sorter_indicator_order;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp10_, WINGPANEL_INDICATOR_PRINTER, (gpointer) ((gintptr) 9));
	_tmp11_ = wingpanel_services_indicator_sorter_indicator_order;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp11_, WINGPANEL_INDICATOR_SYNC, (gpointer) ((gintptr) 10));
	_tmp12_ = wingpanel_services_indicator_sorter_indicator_order;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp12_, WINGPANEL_INDICATOR_POWER, (gpointer) ((gintptr) 11));
	_tmp13_ = wingpanel_services_indicator_sorter_indicator_order;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp13_, WINGPANEL_INDICATOR_MESSAGES, (gpointer) ((gintptr) 12));
	_tmp14_ = wingpanel_services_indicator_sorter_indicator_order;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp14_, WINGPANEL_INDICATOR_QUICKSETTINGS, (gpointer) ((gintptr) 13));
	_tmp15_ = wingpanel_services_indicator_sorter_indicator_order;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp15_, WINGPANEL_INDICATOR_SESSION, (gpointer) ((gintptr) 14));
}

static void
wingpanel_services_indicator_sorter_instance_init (WingpanelServicesIndicatorSorter * self,
                                                   gpointer klass)
{
}

static void
wingpanel_services_indicator_sorter_finalize (GObject * obj)
{
	WingpanelServicesIndicatorSorter * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, WINGPANEL_SERVICES_TYPE_INDICATOR_SORTER, WingpanelServicesIndicatorSorter);
	G_OBJECT_CLASS (wingpanel_services_indicator_sorter_parent_class)->finalize (obj);
}

/**
 * Indicator sorter class.
 *
 * This class is composed of static methods because compare_func() needs
 * to be static in order to work properly, since instance methods cannot
 * be passed as CompareFuncs.
 */
static GType
wingpanel_services_indicator_sorter_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (WingpanelServicesIndicatorSorterClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) wingpanel_services_indicator_sorter_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (WingpanelServicesIndicatorSorter), 0, (GInstanceInitFunc) wingpanel_services_indicator_sorter_instance_init, NULL };
	GType wingpanel_services_indicator_sorter_type_id;
	wingpanel_services_indicator_sorter_type_id = g_type_register_static (G_TYPE_OBJECT, "WingpanelServicesIndicatorSorter", &g_define_type_info, 0);
	return wingpanel_services_indicator_sorter_type_id;
}

GType
wingpanel_services_indicator_sorter_get_type (void)
{
	static volatile gsize wingpanel_services_indicator_sorter_type_id__once = 0;
	if (g_once_init_enter (&wingpanel_services_indicator_sorter_type_id__once)) {
		GType wingpanel_services_indicator_sorter_type_id;
		wingpanel_services_indicator_sorter_type_id = wingpanel_services_indicator_sorter_get_type_once ();
		g_once_init_leave (&wingpanel_services_indicator_sorter_type_id__once, wingpanel_services_indicator_sorter_type_id);
	}
	return wingpanel_services_indicator_sorter_type_id__once;
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

