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

/*
 * This file is part of budgie-desktop
 *
 * Copyright © 2017-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 "budgiemenuapplet.h"
#include <gtk/gtk.h>
#include <glib.h>
#include <glib/gi18n-lib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>

enum  {
	ICON_CHOOSER_0_PROPERTY,
	ICON_CHOOSER_NUM_PROPERTIES
};
static GParamSpec* icon_chooser_properties[ICON_CHOOSER_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))

static gpointer icon_chooser_parent_class = NULL;
static GType icon_chooser_type_id = 0;

/**
 * Construct a new modal IconChooser with the given parent
 */
static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

IconChooser*
icon_chooser_construct (GType object_type,
                        GtkWindow* parent)
{
	IconChooser * self = NULL;
	GtkFileFilter* filter = NULL;
	GtkFileFilter* _tmp0_;
	GtkFileFilter* _tmp1_;
	GtkFileFilter* _tmp2_;
	GtkFileFilter* _tmp3_;
	GtkFileFilter* _tmp4_;
	GtkFileFilter* _tmp5_;
	GtkFileFilter* _tmp6_;
	GtkFileFilter* _tmp7_;
	GtkFileFilter* _tmp8_;
	GtkFileFilter* _tmp9_;
	gchar* picture_dir = NULL;
	const gchar* _tmp10_;
	gchar* _tmp11_;
	const gchar* _tmp12_;
	GtkWidget* _tmp14_;
	GtkStyleContext* _tmp15_;
	g_return_val_if_fail (parent != NULL, NULL);
	self = (IconChooser*) g_object_new (object_type, "transient-for", parent, "use-header-bar", 1, "title", _ ("Set menu icon from file"), "action", GTK_FILE_CHOOSER_ACTION_OPEN, "modal", TRUE, NULL);
	gtk_file_chooser_set_select_multiple ((GtkFileChooser*) self, FALSE);
	gtk_file_chooser_set_show_hidden ((GtkFileChooser*) self, FALSE);
	_tmp0_ = gtk_file_filter_new ();
	g_object_ref_sink (_tmp0_);
	filter = _tmp0_;
	_tmp1_ = filter;
	gtk_file_filter_add_pixbuf_formats (_tmp1_);
	_tmp2_ = filter;
	gtk_buildable_set_name ((GtkBuildable*) _tmp2_, _ ("Image files"));
	_tmp3_ = filter;
	_tmp4_ = _g_object_ref0 (_tmp3_);
	gtk_file_chooser_add_filter ((GtkFileChooser*) self, _tmp4_);
	_tmp5_ = gtk_file_filter_new ();
	g_object_ref_sink (_tmp5_);
	_g_object_unref0 (filter);
	filter = _tmp5_;
	_tmp6_ = filter;
	gtk_file_filter_add_pattern (_tmp6_, "*");
	_tmp7_ = filter;
	gtk_buildable_set_name ((GtkBuildable*) _tmp7_, _ ("Any file"));
	_tmp8_ = filter;
	_tmp9_ = _g_object_ref0 (_tmp8_);
	gtk_file_chooser_add_filter ((GtkFileChooser*) self, _tmp9_);
	gtk_file_chooser_set_local_only ((GtkFileChooser*) self, TRUE);
	_tmp10_ = g_get_user_special_dir (G_USER_DIRECTORY_PICTURES);
	_tmp11_ = g_strdup (_tmp10_);
	picture_dir = _tmp11_;
	_tmp12_ = picture_dir;
	if (_tmp12_ != NULL) {
		const gchar* _tmp13_;
		_tmp13_ = picture_dir;
		gtk_file_chooser_set_current_folder ((GtkFileChooser*) self, _tmp13_);
	}
	gtk_dialog_add_button ((GtkDialog*) self, _ ("Cancel"), (gint) GTK_RESPONSE_CANCEL);
	_tmp14_ = gtk_dialog_add_button ((GtkDialog*) self, _ ("Set icon"), (gint) GTK_RESPONSE_ACCEPT);
	_tmp15_ = gtk_widget_get_style_context (_tmp14_);
	gtk_style_context_add_class (_tmp15_, GTK_STYLE_CLASS_SUGGESTED_ACTION);
	_g_free0 (picture_dir);
	_g_object_unref0 (filter);
	return self;
}

IconChooser*
icon_chooser_new (GtkWindow* parent)
{
	return icon_chooser_construct (TYPE_ICON_CHOOSER, parent);
}

/**
 * Utility method to modally run the dialog and return a consumable response
 */
gchar*
icon_chooser_run (IconChooser* self)
{
	gint resp = 0;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	GTK_WIDGET_CLASS (icon_chooser_parent_class)->show_all ((GtkWidget*) G_TYPE_CHECK_INSTANCE_CAST (self, gtk_file_chooser_dialog_get_type (), GtkFileChooserDialog));
	resp = gtk_dialog_run ((GtkDialog*) G_TYPE_CHECK_INSTANCE_CAST (self, gtk_file_chooser_dialog_get_type (), GtkFileChooserDialog));
	if (resp == ((gint) GTK_RESPONSE_ACCEPT)) {
		gchar* _tmp0_;
		_tmp0_ = gtk_file_chooser_get_filename ((GtkFileChooser*) self);
		result = _tmp0_;
		return result;
	}
	result = NULL;
	return result;
}

static void
icon_chooser_class_init (IconChooserClass * klass,
                         gpointer klass_data)
{
	icon_chooser_parent_class = g_type_class_peek_parent (klass);
}

static void
icon_chooser_instance_init (IconChooser * self,
                            gpointer klass)
{
}

/**
 * IconChooser is a simple GtkFileChooserDialog wrapper. We tried simple
 * icon selection with IconView + TreeView, and whatever way you do it,
 * it's a complete CPU grinder.
 */
GType
icon_chooser_get_type (void)
{
	return icon_chooser_type_id;
}

GType
icon_chooser_register_type (GTypeModule * module)
{
	static const GTypeInfo g_define_type_info = { sizeof (IconChooserClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) icon_chooser_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (IconChooser), 0, (GInstanceInitFunc) icon_chooser_instance_init, NULL };
	icon_chooser_type_id = g_type_module_register_type (module, gtk_file_chooser_dialog_get_type (), "IconChooser", &g_define_type_info, 0);
	return icon_chooser_type_id;
}

