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

/*      profile.vala*/
/*      */
/*      Copyright 2011 Hong Jen Yee (PCMan) <pcman.tw@pcman.tw@gmail.com>*/
/*      */
/*      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.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <gio/gio.h>
#include <fm-file-info.h>
#include <stdio.h>


#define FM_TYPE_FILE_ACTION_EXEC_MODE (fm_file_action_exec_mode_get_type ())
typedef struct _FmFileActionProfile FmFileActionProfile;
typedef struct _FmFileActionCondition FmFileActionCondition;
#define _g_free0(var) (var = (g_free (var), NULL))
#define _fm_file_action_condition_free0(var) ((var == NULL) ? NULL : (var = (fm_file_action_condition_free (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_string_free0(var) ((var == NULL) ? NULL : (var = (g_string_free (var, TRUE), NULL)))

typedef enum  {
	FM_FILE_ACTION_EXEC_MODE_NORMAL,
	FM_FILE_ACTION_EXEC_MODE_TERMINAL,
	FM_FILE_ACTION_EXEC_MODE_EMBEDDED,
	FM_FILE_ACTION_EXEC_MODE_DISPLAY_OUTPUT
} FmFileActionExecMode;

struct _FmFileActionProfile {
	gchar* id;
	gchar* name;
	gchar* exec;
	gchar* path;
	FmFileActionExecMode exec_mode;
	gboolean startup_notify;
	gchar* startup_wm_class;
	gchar* exec_as;
	FmFileActionCondition* condition;
};



GType fm_file_action_exec_mode_get_type (void) G_GNUC_CONST;
void fm_file_action_profile_free (FmFileActionProfile* self);
void fm_file_action_condition_free (FmFileActionCondition* self);
static void fm_file_action_profile_instance_init (FmFileActionProfile * self);
FmFileActionProfile* fm_file_action_profile_new (GKeyFile* kf, const gchar* profile_name);
FmFileActionProfile* fm_file_action_profile_new (GKeyFile* kf, const gchar* profile_name);
gchar* utils_key_file_get_string (GKeyFile* kf, const gchar* group, const gchar* key, const gchar* def_val);
gboolean utils_key_file_get_bool (GKeyFile* kf, const gchar* group, const gchar* key, gboolean def_val);
FmFileActionCondition* fm_file_action_condition_new (GKeyFile* kf, const gchar* group);
FmFileActionCondition* fm_file_action_condition_new (GKeyFile* kf, const gchar* group);
static gboolean fm_file_action_profile_launch_once (FmFileActionProfile* self, GAppLaunchContext* ctx, FmFileInfo* first_file, GList* files, gchar** output);
gchar* fm_file_action_parameters_expand (const gchar* templ, GList* files, gboolean for_display, FmFileInfo* first_file);
gboolean fm_file_action_profile_launch (FmFileActionProfile* self, GAppLaunchContext* ctx, GList* files, gchar** output);
gboolean fm_file_action_parameters_is_plural (const gchar* exec);
gboolean fm_file_action_profile_match (FmFileActionProfile* self, GList* files);
gboolean fm_file_action_condition_match (FmFileActionCondition* self, GList* files);


GType fm_file_action_exec_mode_get_type (void) {
	static volatile gsize fm_file_action_exec_mode_type_id__volatile = 0;
	if (g_once_init_enter (&fm_file_action_exec_mode_type_id__volatile)) {
		static const GEnumValue values[] = {{FM_FILE_ACTION_EXEC_MODE_NORMAL, "FM_FILE_ACTION_EXEC_MODE_NORMAL", "normal"}, {FM_FILE_ACTION_EXEC_MODE_TERMINAL, "FM_FILE_ACTION_EXEC_MODE_TERMINAL", "terminal"}, {FM_FILE_ACTION_EXEC_MODE_EMBEDDED, "FM_FILE_ACTION_EXEC_MODE_EMBEDDED", "embedded"}, {FM_FILE_ACTION_EXEC_MODE_DISPLAY_OUTPUT, "FM_FILE_ACTION_EXEC_MODE_DISPLAY_OUTPUT", "display-output"}, {0, NULL, NULL}};
		GType fm_file_action_exec_mode_type_id;
		fm_file_action_exec_mode_type_id = g_enum_register_static ("FmFileActionExecMode", values);
		g_once_init_leave (&fm_file_action_exec_mode_type_id__volatile, fm_file_action_exec_mode_type_id);
	}
	return fm_file_action_exec_mode_type_id__volatile;
}


static const gchar* string_to_string (const gchar* self) {
	const gchar* result = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	result = self;
	return result;
}


FmFileActionProfile* fm_file_action_profile_new (GKeyFile* kf, const gchar* profile_name) {
	FmFileActionProfile* self;
	const gchar* _tmp0_;
	gchar* _tmp1_;
	const gchar* _tmp2_;
	const gchar* _tmp3_ = NULL;
	gchar* _tmp4_ = NULL;
	gchar* group_name;
	GKeyFile* _tmp5_;
	const gchar* _tmp6_;
	gchar* _tmp7_ = NULL;
	GKeyFile* _tmp8_;
	const gchar* _tmp9_;
	gchar* _tmp10_ = NULL;
	GKeyFile* _tmp11_;
	const gchar* _tmp12_;
	gchar* _tmp13_ = NULL;
	GKeyFile* _tmp14_;
	const gchar* _tmp15_;
	gchar* _tmp16_ = NULL;
	gchar* s;
	const gchar* _tmp17_;
	GKeyFile* _tmp21_;
	const gchar* _tmp22_;
	gboolean _tmp23_ = FALSE;
	GKeyFile* _tmp24_;
	const gchar* _tmp25_;
	gchar* _tmp26_ = NULL;
	GKeyFile* _tmp27_;
	const gchar* _tmp28_;
	gchar* _tmp29_ = NULL;
	GKeyFile* _tmp30_;
	const gchar* _tmp31_;
	FmFileActionCondition* _tmp32_;
	g_return_val_if_fail (kf != NULL, NULL);
	g_return_val_if_fail (profile_name != NULL, NULL);
	self = g_slice_new0 (FmFileActionProfile);
	fm_file_action_profile_instance_init (self);
	_tmp0_ = profile_name;
	_tmp1_ = g_strdup (_tmp0_);
	_g_free0 (self->id);
	self->id = _tmp1_;
	_tmp2_ = profile_name;
	_tmp3_ = string_to_string (_tmp2_);
	_tmp4_ = g_strconcat ("X-Action-Profile ", _tmp3_, NULL);
	group_name = _tmp4_;
	_tmp5_ = kf;
	_tmp6_ = group_name;
	_tmp7_ = utils_key_file_get_string (_tmp5_, _tmp6_, "Name", NULL);
	_g_free0 (self->name);
	self->name = _tmp7_;
	_tmp8_ = kf;
	_tmp9_ = group_name;
	_tmp10_ = utils_key_file_get_string (_tmp8_, _tmp9_, "Exec", NULL);
	_g_free0 (self->exec);
	self->exec = _tmp10_;
	_tmp11_ = kf;
	_tmp12_ = group_name;
	_tmp13_ = utils_key_file_get_string (_tmp11_, _tmp12_, "Path", NULL);
	_g_free0 (self->path);
	self->path = _tmp13_;
	_tmp14_ = kf;
	_tmp15_ = group_name;
	_tmp16_ = utils_key_file_get_string (_tmp14_, _tmp15_, "ExecutionMode", NULL);
	s = _tmp16_;
	_tmp17_ = s;
	if (g_strcmp0 (_tmp17_, "Normal") == 0) {
		self->exec_mode = FM_FILE_ACTION_EXEC_MODE_NORMAL;
	} else {
		const gchar* _tmp18_;
		_tmp18_ = s;
		if (g_strcmp0 (_tmp18_, "Terminal") == 0) {
			self->exec_mode = FM_FILE_ACTION_EXEC_MODE_TERMINAL;
		} else {
			const gchar* _tmp19_;
			_tmp19_ = s;
			if (g_strcmp0 (_tmp19_, "Embedded") == 0) {
				self->exec_mode = FM_FILE_ACTION_EXEC_MODE_EMBEDDED;
			} else {
				const gchar* _tmp20_;
				_tmp20_ = s;
				if (g_strcmp0 (_tmp20_, "DisplayOutput") == 0) {
					self->exec_mode = FM_FILE_ACTION_EXEC_MODE_DISPLAY_OUTPUT;
				} else {
					self->exec_mode = FM_FILE_ACTION_EXEC_MODE_NORMAL;
				}
			}
		}
	}
	_tmp21_ = kf;
	_tmp22_ = group_name;
	_tmp23_ = utils_key_file_get_bool (_tmp21_, _tmp22_, "StartupNotify", FALSE);
	self->startup_notify = _tmp23_;
	_tmp24_ = kf;
	_tmp25_ = group_name;
	_tmp26_ = utils_key_file_get_string (_tmp24_, _tmp25_, "StartupWMClass", NULL);
	_g_free0 (self->startup_wm_class);
	self->startup_wm_class = _tmp26_;
	_tmp27_ = kf;
	_tmp28_ = group_name;
	_tmp29_ = utils_key_file_get_string (_tmp27_, _tmp28_, "ExecuteAs", NULL);
	_g_free0 (self->exec_as);
	self->exec_as = _tmp29_;
	_tmp30_ = kf;
	_tmp31_ = group_name;
	_tmp32_ = fm_file_action_condition_new (_tmp30_, _tmp31_);
	_fm_file_action_condition_free0 (self->condition);
	self->condition = _tmp32_;
	_g_free0 (s);
	_g_free0 (group_name);
	return self;
}


static gboolean fm_file_action_profile_launch_once (FmFileActionProfile* self, GAppLaunchContext* ctx, FmFileInfo* first_file, GList* files, gchar** output) {
	gchar* _vala_output = NULL;
	gboolean result = FALSE;
	const gchar* _tmp0_;
	const gchar* _tmp1_;
	GList* _tmp2_;
	FmFileInfo* _tmp3_;
	gchar* _tmp4_ = NULL;
	gchar* exec_cmd;
	FILE* _tmp5_;
	const gchar* _tmp6_;
	const gchar* _tmp7_;
	gboolean ret;
	FmFileActionExecMode _tmp8_;
	GError * _inner_error_ = NULL;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (ctx != NULL, FALSE);
	_tmp0_ = self->exec;
	if (_tmp0_ == NULL) {
		result = FALSE;
		if (output) {
			*output = _vala_output;
		} else {
			_g_free0 (_vala_output);
		}
		return result;
	}
	_tmp1_ = self->exec;
	_tmp2_ = files;
	_tmp3_ = first_file;
	_tmp4_ = fm_file_action_parameters_expand (_tmp1_, _tmp2_, FALSE, _tmp3_);
	exec_cmd = _tmp4_;
	_tmp5_ = stdout;
	_tmp6_ = self->id;
	_tmp7_ = exec_cmd;
	fprintf (_tmp5_, "Profile: %s\nlaunch command: %s\n\n", _tmp6_, _tmp7_);
	ret = FALSE;
	_tmp8_ = self->exec_mode;
	if (_tmp8_ == FM_FILE_ACTION_EXEC_MODE_DISPLAY_OUTPUT) {
		{
			gint exit_status = 0;
			const gchar* _tmp9_;
			gchar* _tmp10_ = NULL;
			gint _tmp11_ = 0;
			gboolean _tmp12_ = FALSE;
			gboolean _tmp13_;
			gboolean _tmp14_;
			_tmp9_ = exec_cmd;
			_tmp12_ = g_spawn_command_line_sync (_tmp9_, &_tmp10_, NULL, &_tmp11_, &_inner_error_);
			_g_free0 (_vala_output);
			_vala_output = _tmp10_;
			exit_status = _tmp11_;
			_tmp13_ = _tmp12_;
			if (_inner_error_ != NULL) {
				if (_inner_error_->domain == G_SPAWN_ERROR) {
					goto __catch4_g_spawn_error;
				}
				_g_free0 (exec_cmd);
				g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
				g_clear_error (&_inner_error_);
				return FALSE;
			}
			ret = _tmp13_;
			_tmp14_ = ret;
			if (_tmp14_) {
				gint _tmp15_;
				_tmp15_ = exit_status;
				ret = _tmp15_ == 0;
			}
		}
		goto __finally4;
		__catch4_g_spawn_error:
		{
			GError* err = NULL;
			err = _inner_error_;
			_inner_error_ = NULL;
			_g_error_free0 (err);
		}
		__finally4:
		if (_inner_error_ != NULL) {
			_g_free0 (exec_cmd);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
			return FALSE;
		}
	} else {
		{
			const gchar* _tmp16_;
			gboolean _tmp17_ = FALSE;
			gboolean _tmp18_;
			_tmp16_ = exec_cmd;
			_tmp17_ = g_spawn_command_line_async (_tmp16_, &_inner_error_);
			_tmp18_ = _tmp17_;
			if (_inner_error_ != NULL) {
				if (_inner_error_->domain == G_SPAWN_ERROR) {
					goto __catch5_g_spawn_error;
				}
				_g_free0 (exec_cmd);
				g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
				g_clear_error (&_inner_error_);
				return FALSE;
			}
			ret = _tmp18_;
		}
		goto __finally5;
		__catch5_g_spawn_error:
		{
			GError* err = NULL;
			err = _inner_error_;
			_inner_error_ = NULL;
			_g_error_free0 (err);
		}
		__finally5:
		if (_inner_error_ != NULL) {
			_g_free0 (exec_cmd);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
			return FALSE;
		}
	}
	result = ret;
	_g_free0 (exec_cmd);
	if (output) {
		*output = _vala_output;
	} else {
		_g_free0 (_vala_output);
	}
	return result;
}


gboolean fm_file_action_profile_launch (FmFileActionProfile* self, GAppLaunchContext* ctx, GList* files, gchar** output) {
	gchar* _vala_output = NULL;
	gboolean result = FALSE;
	const gchar* _tmp0_;
	gboolean _tmp1_ = FALSE;
	gboolean plural_form;
	gboolean ret = FALSE;
	gboolean _tmp2_;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (ctx != NULL, FALSE);
	_tmp0_ = self->exec;
	_tmp1_ = fm_file_action_parameters_is_plural (_tmp0_);
	plural_form = _tmp1_;
	_tmp2_ = plural_form;
	if (_tmp2_) {
		GAppLaunchContext* _tmp3_;
		GList* _tmp4_;
		GList* _tmp5_ = NULL;
		gconstpointer _tmp6_;
		GList* _tmp7_;
		gchar* _tmp8_ = NULL;
		gboolean _tmp9_ = FALSE;
		_tmp3_ = ctx;
		_tmp4_ = files;
		_tmp5_ = g_list_first (_tmp4_);
		_tmp6_ = _tmp5_->data;
		_tmp7_ = files;
		_tmp9_ = fm_file_action_profile_launch_once (self, _tmp3_, (FmFileInfo*) _tmp6_, _tmp7_, &_tmp8_);
		_g_free0 (_vala_output);
		_vala_output = _tmp8_;
		ret = _tmp9_;
	} else {
		GString* all_output;
		const gchar* _tmp10_;
		GList* _tmp12_;
		gboolean _tmp24_ = FALSE;
		GString* _tmp25_;
		gboolean _tmp27_;
		all_output = NULL;
		_tmp10_ = _vala_output;
		if (_tmp10_ != NULL) {
			GString* _tmp11_;
			_tmp11_ = g_string_new ("");
			_g_string_free0 (all_output);
			all_output = _tmp11_;
		}
		_tmp12_ = files;
		{
			GList* fi_collection = NULL;
			GList* fi_it = NULL;
			fi_collection = _tmp12_;
			for (fi_it = fi_collection; fi_it != NULL; fi_it = fi_it->next) {
				FmFileInfo* fi = NULL;
				fi = (FmFileInfo*) fi_it->data;
				{
					gchar* one_output = NULL;
					GAppLaunchContext* _tmp13_;
					FmFileInfo* _tmp14_;
					GList* _tmp15_;
					gchar* _tmp16_ = NULL;
					gboolean _tmp17_ = FALSE;
					GString* _tmp18_;
					gboolean _tmp20_;
					_tmp13_ = ctx;
					_tmp14_ = fi;
					_tmp15_ = files;
					fm_file_action_profile_launch_once (self, _tmp13_, _tmp14_, _tmp15_, &_tmp16_);
					_g_free0 (one_output);
					one_output = _tmp16_;
					_tmp18_ = all_output;
					if (_tmp18_ != NULL) {
						const gchar* _tmp19_;
						_tmp19_ = one_output;
						_tmp17_ = _tmp19_ != NULL;
					} else {
						_tmp17_ = FALSE;
					}
					_tmp20_ = _tmp17_;
					if (_tmp20_) {
						GString* _tmp21_;
						const gchar* _tmp22_;
						GString* _tmp23_;
						_tmp21_ = all_output;
						_tmp22_ = one_output;
						g_string_append (_tmp21_, _tmp22_);
						_tmp23_ = all_output;
						g_string_append (_tmp23_, "\n");
					}
					_g_free0 (one_output);
				}
			}
		}
		_tmp25_ = all_output;
		if (_tmp25_ != NULL) {
			const gchar* _tmp26_;
			_tmp26_ = _vala_output;
			_tmp24_ = _tmp26_ != NULL;
		} else {
			_tmp24_ = FALSE;
		}
		_tmp27_ = _tmp24_;
		if (_tmp27_) {
			GString* _tmp28_;
			gchar* _tmp29_;
			_tmp28_ = all_output;
			_tmp29_ = _tmp28_->str;
			_tmp28_->str = NULL;
			_g_free0 (_vala_output);
			_vala_output = _tmp29_;
		}
		ret = TRUE;
		_g_string_free0 (all_output);
	}
	result = ret;
	if (output) {
		*output = _vala_output;
	} else {
		_g_free0 (_vala_output);
	}
	return result;
}


gboolean fm_file_action_profile_match (FmFileActionProfile* self, GList* files) {
	gboolean result = FALSE;
	FILE* _tmp0_;
	const gchar* _tmp1_;
	FmFileActionCondition* _tmp2_;
	GList* _tmp3_;
	gboolean _tmp4_ = FALSE;
	g_return_val_if_fail (self != NULL, FALSE);
	_tmp0_ = stdout;
	_tmp1_ = self->id;
	fprintf (_tmp0_, "  match profile: %s\n", _tmp1_);
	_tmp2_ = self->condition;
	_tmp3_ = files;
	_tmp4_ = fm_file_action_condition_match (_tmp2_, _tmp3_);
	result = _tmp4_;
	return result;
}


static void fm_file_action_profile_instance_init (FmFileActionProfile * self) {
}


void fm_file_action_profile_free (FmFileActionProfile* self) {
	_g_free0 (self->id);
	_g_free0 (self->name);
	_g_free0 (self->exec);
	_g_free0 (self->path);
	_g_free0 (self->startup_wm_class);
	_g_free0 (self->exec_as);
	_fm_file_action_condition_free0 (self->condition);
	g_slice_free (FmFileActionProfile, self);
}



