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

/*
* Copyright (c) 2010 Michal Hruby <michal.mhr@gmail.com>
*               2017 elementary LLC.
*
* 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
*
* Authored by: Michal Hruby <michal.mhr@gmail.com>
*/

#include "slingshot.h"
#include <glib-object.h>
#include <glib.h>
#include <glib/gi18n-lib.h>
#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>
#include <gio/gdesktopappinfo.h>
#include <gdk/gdk.h>

#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

#define SYNAPSE_TYPE_TERMINAL_RUNNER_ACTION (synapse_terminal_runner_action_get_type ())
#define SYNAPSE_TERMINAL_RUNNER_ACTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SYNAPSE_TYPE_TERMINAL_RUNNER_ACTION, SynapseTerminalRunnerAction))
#define SYNAPSE_TERMINAL_RUNNER_ACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), SYNAPSE_TYPE_TERMINAL_RUNNER_ACTION, SynapseTerminalRunnerActionClass))
#define SYNAPSE_IS_TERMINAL_RUNNER_ACTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SYNAPSE_TYPE_TERMINAL_RUNNER_ACTION))
#define SYNAPSE_IS_TERMINAL_RUNNER_ACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SYNAPSE_TYPE_TERMINAL_RUNNER_ACTION))
#define SYNAPSE_TERMINAL_RUNNER_ACTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), SYNAPSE_TYPE_TERMINAL_RUNNER_ACTION, SynapseTerminalRunnerActionClass))

typedef struct _SynapseTerminalRunnerAction SynapseTerminalRunnerAction;
typedef struct _SynapseTerminalRunnerActionClass SynapseTerminalRunnerActionClass;
typedef struct _SynapseTerminalRunnerActionPrivate SynapseTerminalRunnerActionPrivate;
enum  {
	SYNAPSE_TERMINAL_RUNNER_ACTION_0_PROPERTY,
	SYNAPSE_TERMINAL_RUNNER_ACTION_NUM_PROPERTIES
};
static GParamSpec* synapse_terminal_runner_action_properties[SYNAPSE_TERMINAL_RUNNER_ACTION_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)))

struct _SynapseTerminalRunnerAction {
	SynapseBaseAction parent_instance;
	SynapseTerminalRunnerActionPrivate * priv;
};

struct _SynapseTerminalRunnerActionClass {
	SynapseBaseActionClass parent_class;
};

static gpointer synapse_terminal_runner_action_parent_class = NULL;
static GType synapse_terminal_runner_action_type_id = 0;

GType synapse_terminal_runner_action_register_type (GTypeModule * module);
VALA_EXTERN GType synapse_terminal_runner_action_get_type (void) G_GNUC_CONST ;
VALA_EXTERN SynapseTerminalRunnerAction* synapse_terminal_runner_action_new (void);
VALA_EXTERN SynapseTerminalRunnerAction* synapse_terminal_runner_action_construct (GType object_type);
static void synapse_terminal_runner_action_real_do_execute (SynapseBaseAction* base,
                                                     SynapseMatch* match,
                                                     SynapseMatch* target);
static gboolean synapse_terminal_runner_action_real_valid_for_match (SynapseBaseAction* base,
                                                              SynapseMatch* match);

SynapseTerminalRunnerAction*
synapse_terminal_runner_action_construct (GType object_type)
{
	SynapseTerminalRunnerAction * self = NULL;
	self = (SynapseTerminalRunnerAction*) g_object_new (object_type, "title", _ ("Run in Terminal"), "description", _ ("Run application or command in terminal"), "icon-name", "terminal", "has-thumbnail", FALSE, "match-type", SYNAPSE_MATCH_TYPE_ACTION, "default-relevancy", SYNAPSE_MATCH_SCORE_BELOW_AVERAGE, NULL);
	return self;
}

SynapseTerminalRunnerAction*
synapse_terminal_runner_action_new (void)
{
	return synapse_terminal_runner_action_construct (SYNAPSE_TYPE_TERMINAL_RUNNER_ACTION);
}

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

static void
synapse_terminal_runner_action_real_do_execute (SynapseBaseAction* base,
                                                SynapseMatch* match,
                                                SynapseMatch* target)
{
	SynapseTerminalRunnerAction * self;
	SynapseMatchType _tmp0_;
	SynapseMatchType _tmp1_;
	GError* _inner_error0_ = NULL;
	self = (SynapseTerminalRunnerAction*) base;
	_tmp0_ = synapse_match_get_match_type (match);
	_tmp1_ = _tmp0_;
	if (_tmp1_ == SYNAPSE_MATCH_TYPE_APPLICATION) {
		SynapseApplicationMatch* app_match = NULL;
		SynapseApplicationMatch* _tmp2_;
		GAppInfo* _tmp3_ = NULL;
		SynapseApplicationMatch* _tmp4_;
		GAppInfo* _tmp5_;
		GAppInfo* _tmp6_;
		GAppInfo* _tmp7_;
		GAppInfo* original = NULL;
		GAppInfo* _tmp12_;
		app_match = SYNAPSE_IS_APPLICATION_MATCH (match) ? ((SynapseApplicationMatch*) match) : NULL;
		_tmp2_ = app_match;
		g_return_if_fail (_tmp2_ != NULL);
		_tmp4_ = app_match;
		_tmp5_ = synapse_application_match_get_app_info (_tmp4_);
		_tmp6_ = _tmp5_;
		_tmp7_ = _g_object_ref0 (_tmp6_);
		_tmp3_ = _tmp7_;
		if (_tmp3_ == NULL) {
			SynapseApplicationMatch* _tmp8_;
			const gchar* _tmp9_;
			const gchar* _tmp10_;
			GDesktopAppInfo* _tmp11_;
			_tmp8_ = app_match;
			_tmp9_ = synapse_application_match_get_filename (_tmp8_);
			_tmp10_ = _tmp9_;
			_tmp11_ = g_desktop_app_info_new_from_filename (_tmp10_);
			_g_object_unref0 (_tmp3_);
			_tmp3_ = (GAppInfo*) _tmp11_;
		}
		_tmp12_ = _tmp3_;
		_tmp3_ = NULL;
		original = _tmp12_;
		{
			GAppInfo* app = NULL;
			GAppInfo* _tmp13_;
			const gchar* _tmp14_;
			GAppInfo* _tmp15_;
			const gchar* _tmp16_;
			GAppInfo* _tmp17_;
			GdkDisplay* display = NULL;
			GdkDisplay* _tmp18_;
			GAppInfo* _tmp19_;
			GdkDisplay* _tmp20_;
			GdkAppLaunchContext* _tmp21_;
			GdkAppLaunchContext* _tmp22_;
			_tmp13_ = original;
			_tmp14_ = g_app_info_get_commandline (_tmp13_);
			_tmp15_ = original;
			_tmp16_ = g_app_info_get_name (_tmp15_);
			_tmp17_ = g_app_info_create_from_commandline (_tmp14_, _tmp16_, G_APP_INFO_CREATE_NEEDS_TERMINAL, &_inner_error0_);
			app = _tmp17_;
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch0_g_error;
			}
			_tmp18_ = gdk_display_get_default ();
			display = _tmp18_;
			_tmp19_ = app;
			_tmp20_ = display;
			_tmp21_ = gdk_display_get_app_launch_context (_tmp20_);
			_tmp22_ = _tmp21_;
			g_app_info_launch (_tmp19_, NULL, (GAppLaunchContext*) _tmp22_, &_inner_error0_);
			_g_object_unref0 (_tmp22_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				_g_object_unref0 (app);
				goto __catch0_g_error;
			}
			_g_object_unref0 (app);
		}
		goto __finally0;
		__catch0_g_error:
		{
			GError* err = NULL;
			GError* _tmp23_;
			const gchar* _tmp24_;
			err = _inner_error0_;
			_inner_error0_ = NULL;
			_tmp23_ = err;
			_tmp24_ = _tmp23_->message;
			g_critical ("TerminalRunnerAction.vala:47: %s", _tmp24_);
			_g_error_free0 (err);
		}
		__finally0:
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_object_unref0 (original);
			_g_object_unref0 (_tmp3_);
			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;
		}
		_g_object_unref0 (original);
		_g_object_unref0 (_tmp3_);
	}
}

static gboolean
synapse_terminal_runner_action_real_valid_for_match (SynapseBaseAction* base,
                                                     SynapseMatch* match)
{
	SynapseTerminalRunnerAction * self;
	SynapseMatchType _tmp0_;
	SynapseMatchType _tmp1_;
	gboolean result;
	self = (SynapseTerminalRunnerAction*) base;
	g_return_val_if_fail (match != NULL, FALSE);
	_tmp0_ = synapse_match_get_match_type (match);
	_tmp1_ = _tmp0_;
	switch (_tmp1_) {
		case SYNAPSE_MATCH_TYPE_APPLICATION:
		{
			SynapseApplicationMatch* am = NULL;
			SynapseApplicationMatch* _tmp2_;
			am = SYNAPSE_IS_APPLICATION_MATCH (match) ? ((SynapseApplicationMatch*) match) : NULL;
			_tmp2_ = am;
			result = _tmp2_ != NULL;
			return result;
		}
		default:
		{
			result = FALSE;
			return result;
		}
	}
}

static void
synapse_terminal_runner_action_class_init (SynapseTerminalRunnerActionClass * klass,
                                           gpointer klass_data)
{
	synapse_terminal_runner_action_parent_class = g_type_class_peek_parent (klass);
	((SynapseBaseActionClass *) klass)->do_execute = (void (*) (SynapseBaseAction*, SynapseMatch*, SynapseMatch*)) synapse_terminal_runner_action_real_do_execute;
	((SynapseBaseActionClass *) klass)->valid_for_match = (gboolean (*) (SynapseBaseAction*, SynapseMatch*)) synapse_terminal_runner_action_real_valid_for_match;
}

static void
synapse_terminal_runner_action_instance_init (SynapseTerminalRunnerAction * self,
                                              gpointer klass)
{
}

GType
synapse_terminal_runner_action_get_type (void)
{
	return synapse_terminal_runner_action_type_id;
}

GType
synapse_terminal_runner_action_register_type (GTypeModule * module)
{
	static const GTypeInfo g_define_type_info = { sizeof (SynapseTerminalRunnerActionClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) synapse_terminal_runner_action_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (SynapseTerminalRunnerAction), 0, (GInstanceInitFunc) synapse_terminal_runner_action_instance_init, NULL };
	synapse_terminal_runner_action_type_id = g_type_module_register_type (module, SYNAPSE_TYPE_BASE_ACTION, "SynapseTerminalRunnerAction", &g_define_type_info, 0);
	return synapse_terminal_runner_action_type_id;
}

