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

/*
 * This file is part of budgie-desktop
 *
 * Copyright © 2015-2022 Budgie Desktop Developers
 *
 * 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.
 */

#include "raven.h"
#include <gio/gio.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib/gi18n-lib.h>
#include <pango/pango.h>

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

struct _BudgieStartListeningPrivate {
	GAppInfo* music_app;
	gboolean has_music_player;
	GtkButton* start_listening;
};

static gint BudgieStartListening_private_offset;
static gpointer budgie_start_listening_parent_class = NULL;

static void budgie_start_listening_check_music_support (BudgieStartListening* self);
static void _budgie_start_listening_check_music_support_g_app_info_monitor_changed (GAppInfoMonitor* _sender,
                                                                             gpointer self);
static void budgie_start_listening_launch_music_player (BudgieStartListening* self);
static void _budgie_start_listening_launch_music_player_gtk_button_clicked (GtkButton* _sender,
                                                                     gpointer self);
static void budgie_start_listening_finalize (GObject * obj);
static GType budgie_start_listening_get_type_once (void);

static inline gpointer
budgie_start_listening_get_instance_private (BudgieStartListening* self)
{
	return G_STRUCT_MEMBER_P (self, BudgieStartListening_private_offset);
}

static void
_budgie_start_listening_check_music_support_g_app_info_monitor_changed (GAppInfoMonitor* _sender,
                                                                        gpointer self)
{
	budgie_start_listening_check_music_support ((BudgieStartListening*) self);
}

static void
_budgie_start_listening_launch_music_player_gtk_button_clicked (GtkButton* _sender,
                                                                gpointer self)
{
	budgie_start_listening_launch_music_player ((BudgieStartListening*) self);
}

BudgieStartListening*
budgie_start_listening_construct (GType object_type)
{
	BudgieStartListening * self = NULL;
	GtkLabel* label = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	GtkLabel* _tmp2_;
	GtkLabel* _tmp3_;
	GtkButton* _tmp4_;
	GtkButton* _tmp5_;
	GtkButton* _tmp6_;
	GAppInfoMonitor* monitor = NULL;
	GAppInfoMonitor* _tmp7_;
	GtkButton* _tmp8_;
	self = (BudgieStartListening*) g_object_new (object_type, "orientation", GTK_ORIENTATION_VERTICAL, "margin", 10, NULL);
	_tmp0_ = g_strdup_printf ("<big>%s</big>", _ ("No apps are currently playing audio."));
	_tmp1_ = _tmp0_;
	_tmp2_ = (GtkLabel*) gtk_label_new (_tmp1_);
	g_object_ref_sink (_tmp2_);
	_tmp3_ = _tmp2_;
	_g_free0 (_tmp1_);
	label = _tmp3_;
	gtk_label_set_justify (label, GTK_JUSTIFY_CENTER);
	gtk_widget_set_halign ((GtkWidget*) label, GTK_ALIGN_CENTER);
	gtk_label_set_max_width_chars (label, 36);
	gtk_label_set_use_markup (label, TRUE);
	gtk_widget_set_valign ((GtkWidget*) label, GTK_ALIGN_CENTER);
	g_object_set (label, "wrap", TRUE, NULL);
	g_object_set (label, "wrap-mode", PANGO_WRAP_WORD, NULL);
	_tmp4_ = (GtkButton*) gtk_button_new_with_label (_ ("Play some music"));
	g_object_ref_sink (_tmp4_);
	_g_object_unref0 (self->priv->start_listening);
	self->priv->start_listening = _tmp4_;
	_tmp5_ = self->priv->start_listening;
	gtk_widget_set_hexpand ((GtkWidget*) _tmp5_, FALSE);
	gtk_box_pack_start ((GtkBox*) self, (GtkWidget*) label, TRUE, TRUE, (guint) 10);
	_tmp6_ = self->priv->start_listening;
	gtk_box_pack_start ((GtkBox*) self, (GtkWidget*) _tmp6_, FALSE, FALSE, (guint) 0);
	_tmp7_ = g_app_info_monitor_get ();
	monitor = _tmp7_;
	g_signal_connect_object (monitor, "changed", (GCallback) _budgie_start_listening_check_music_support_g_app_info_monitor_changed, self, 0);
	_tmp8_ = self->priv->start_listening;
	g_signal_connect_object (_tmp8_, "clicked", (GCallback) _budgie_start_listening_launch_music_player_gtk_button_clicked, self, 0);
	budgie_start_listening_check_music_support (self);
	_g_object_unref0 (monitor);
	_g_object_unref0 (label);
	return self;
}

BudgieStartListening*
budgie_start_listening_new (void)
{
	return budgie_start_listening_construct (BUDGIE_TYPE_START_LISTENING);
}

static void
budgie_start_listening_check_music_support (BudgieStartListening* self)
{
	GAppInfo* _tmp0_;
	GAppInfo* _tmp1_;
	GtkButton* _tmp2_;
	g_return_if_fail (self != NULL);
	_tmp0_ = g_app_info_get_default_for_type ("audio/vorbis", FALSE);
	_g_object_unref0 (self->priv->music_app);
	self->priv->music_app = _tmp0_;
	_tmp1_ = self->priv->music_app;
	self->priv->has_music_player = _tmp1_ != NULL;
	_tmp2_ = self->priv->start_listening;
	gtk_widget_set_visible ((GtkWidget*) _tmp2_, self->priv->has_music_player);
}

static void
budgie_start_listening_launch_music_player (BudgieStartListening* self)
{
	GAppInfo* _tmp0_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->music_app;
	if (_tmp0_ == NULL) {
		return;
	}
	{
		GAppInfo* _tmp1_;
		_tmp1_ = self->priv->music_app;
		g_app_info_launch (_tmp1_, NULL, NULL, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GAppInfo* _tmp2_;
		const gchar* _tmp3_;
		GError* _tmp4_;
		const gchar* _tmp5_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp2_ = self->priv->music_app;
		_tmp3_ = g_app_info_get_name (_tmp2_);
		_tmp4_ = e;
		_tmp5_ = _tmp4_->message;
		g_warning ("start_listening.vala:60: Unable to launch %s: %s", _tmp3_, _tmp5_);
		_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
budgie_start_listening_class_init (BudgieStartListeningClass * klass,
                                   gpointer klass_data)
{
	budgie_start_listening_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &BudgieStartListening_private_offset);
	G_OBJECT_CLASS (klass)->finalize = budgie_start_listening_finalize;
}

static void
budgie_start_listening_instance_init (BudgieStartListening * self,
                                      gpointer klass)
{
	self->priv = budgie_start_listening_get_instance_private (self);
	self->priv->music_app = NULL;
}

static void
budgie_start_listening_finalize (GObject * obj)
{
	BudgieStartListening * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, BUDGIE_TYPE_START_LISTENING, BudgieStartListening);
	_g_object_unref0 (self->priv->music_app);
	_g_object_unref0 (self->priv->start_listening);
	G_OBJECT_CLASS (budgie_start_listening_parent_class)->finalize (obj);
}

static GType
budgie_start_listening_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (BudgieStartListeningClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) budgie_start_listening_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (BudgieStartListening), 0, (GInstanceInitFunc) budgie_start_listening_instance_init, NULL };
	GType budgie_start_listening_type_id;
	budgie_start_listening_type_id = g_type_register_static (gtk_box_get_type (), "BudgieStartListening", &g_define_type_info, 0);
	BudgieStartListening_private_offset = g_type_add_instance_private (budgie_start_listening_type_id, sizeof (BudgieStartListeningPrivate));
	return budgie_start_listening_type_id;
}

GType
budgie_start_listening_get_type (void)
{
	static volatile gsize budgie_start_listening_type_id__once = 0;
	if (g_once_init_enter (&budgie_start_listening_type_id__once)) {
		GType budgie_start_listening_type_id;
		budgie_start_listening_type_id = budgie_start_listening_get_type_once ();
		g_once_init_leave (&budgie_start_listening_type_id__once, budgie_start_listening_type_id);
	}
	return budgie_start_listening_type_id__once;
}

