medit/moo/mooedit/moooutputfilter.c

398 lines
10 KiB
C
Raw Normal View History

2006-09-01 01:48:52 -07:00
/*
* moooutputfilter.c
*
2007-04-07 01:21:52 -07:00
* Copyright (C) 2004-2007 by Yevgen Muntyan <muntyan@math.tamu.edu>
2006-09-01 01:48:52 -07:00
*
* 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.
*
* See COPYING file that comes with this distribution.
*/
#include "mooedit/moooutputfilter.h"
2006-09-02 02:01:26 -07:00
#include "mooedit/mooeditor.h"
2006-09-01 10:37:47 -07:00
#include "mooutils/moomarshals.h"
2006-11-01 22:38:00 -08:00
#include "mooutils/mooutils-misc.h"
2006-09-01 10:37:47 -07:00
#include <string.h>
2006-09-01 01:48:52 -07:00
2006-09-02 02:01:26 -07:00
struct _MooOutputFilterPrivate {
MooLineView *view;
2007-02-27 20:27:49 -08:00
char *active_dir;
char *active_file;
2006-09-02 02:01:26 -07:00
MooEditWindow *window;
};
2006-09-01 01:48:52 -07:00
G_DEFINE_TYPE (MooOutputFilter, moo_output_filter, G_TYPE_OBJECT)
static void moo_output_filter_open_file_line (MooOutputFilter *filter,
MooFileLineData *data);
2006-09-01 10:37:47 -07:00
enum {
STDOUT_LINE,
STDERR_LINE,
CMD_START,
CMD_EXIT,
N_SIGNALS
};
static guint signals[N_SIGNALS];
2006-09-01 01:48:52 -07:00
2006-09-02 02:01:26 -07:00
static void
moo_output_filter_finalize (GObject *object)
{
MooOutputFilter *filter = MOO_OUTPUT_FILTER (object);
2007-02-27 20:27:49 -08:00
g_free (filter->priv->active_dir);
g_free (filter->priv->active_file);
2006-09-02 02:01:26 -07:00
G_OBJECT_CLASS (moo_output_filter_parent_class)->finalize (object);
}
static void
moo_output_filter_activate (MooOutputFilter *filter,
int line)
{
MooFileLineData *data;
data = moo_line_view_get_boxed (filter->priv->view, line, MOO_TYPE_FILE_LINE_DATA);
if (data)
{
moo_output_filter_open_file_line (filter, data);
2006-09-02 02:01:26 -07:00
moo_file_line_data_free (data);
}
}
2006-09-01 01:48:52 -07:00
static void
2006-09-01 10:37:47 -07:00
moo_output_filter_class_init (MooOutputFilterClass *klass)
2006-09-01 01:48:52 -07:00
{
2006-09-02 02:01:26 -07:00
G_OBJECT_CLASS (klass)->finalize = moo_output_filter_finalize;
klass->activate = moo_output_filter_activate;
g_type_class_add_private (klass, sizeof (MooOutputFilterPrivate));
2006-09-01 10:37:47 -07:00
signals[STDOUT_LINE] =
g_signal_new ("stdout-line",
G_OBJECT_CLASS_TYPE (klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (MooOutputFilterClass, stdout_line),
g_signal_accumulator_true_handled, NULL,
_moo_marshal_BOOL__STRING,
G_TYPE_BOOLEAN, 1,
G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
signals[STDERR_LINE] =
g_signal_new ("stderr-line",
G_OBJECT_CLASS_TYPE (klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (MooOutputFilterClass, stderr_line),
g_signal_accumulator_true_handled, NULL,
_moo_marshal_BOOL__STRING,
G_TYPE_BOOLEAN, 1,
G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
signals[CMD_START] =
g_signal_new ("cmd-start",
G_OBJECT_CLASS_TYPE (klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (MooOutputFilterClass, cmd_start),
2006-09-02 00:34:01 -07:00
NULL, NULL,
_moo_marshal_VOID__VOID,
G_TYPE_NONE, 0);
2006-09-01 10:37:47 -07:00
signals[CMD_EXIT] =
g_signal_new ("cmd-exit",
G_OBJECT_CLASS_TYPE (klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (MooOutputFilterClass, cmd_exit),
g_signal_accumulator_true_handled, NULL,
_moo_marshal_BOOL__INT,
G_TYPE_BOOLEAN, 1,
G_TYPE_INT);
2006-09-01 01:48:52 -07:00
}
static void
2006-09-02 02:01:26 -07:00
moo_output_filter_init (MooOutputFilter *filter)
2006-09-01 01:48:52 -07:00
{
2006-09-02 02:01:26 -07:00
filter->priv = G_TYPE_INSTANCE_GET_PRIVATE (filter, MOO_TYPE_OUTPUT_FILTER, MooOutputFilterPrivate);
}
static void
view_activate (MooLineView *view,
int line,
MooOutputFilter *filter)
{
g_return_if_fail (MOO_IS_LINE_VIEW (view));
g_return_if_fail (MOO_IS_OUTPUT_FILTER (filter));
if (MOO_OUTPUT_FILTER_GET_CLASS (filter)->activate)
MOO_OUTPUT_FILTER_GET_CLASS (filter)->activate (filter, line);
2006-09-01 01:48:52 -07:00
}
2006-09-01 10:37:47 -07:00
void
moo_output_filter_set_view (MooOutputFilter *filter,
MooLineView *view)
{
g_return_if_fail (MOO_IS_OUTPUT_FILTER (filter));
g_return_if_fail (!view || MOO_IS_LINE_VIEW (view));
2006-09-02 02:01:26 -07:00
if (filter->priv->view == view)
2006-09-01 10:37:47 -07:00
return;
2006-09-02 02:01:26 -07:00
if (filter->priv->view)
2006-09-01 10:37:47 -07:00
{
2006-09-02 02:01:26 -07:00
g_signal_handlers_disconnect_by_func (filter->priv->view,
(gpointer) view_activate,
filter);
2006-09-01 10:37:47 -07:00
if (MOO_OUTPUT_FILTER_GET_CLASS (filter)->detach)
MOO_OUTPUT_FILTER_GET_CLASS (filter)->detach (filter);
}
2006-09-02 02:01:26 -07:00
filter->priv->view = view;
2006-09-01 10:37:47 -07:00
if (view)
{
2006-09-02 02:01:26 -07:00
g_signal_connect (view, "activate", G_CALLBACK (view_activate), filter);
2006-09-01 10:37:47 -07:00
if (MOO_OUTPUT_FILTER_GET_CLASS (filter)->attach)
MOO_OUTPUT_FILTER_GET_CLASS (filter)->attach (filter);
}
}
MooLineView *
moo_output_filter_get_view (MooOutputFilter *filter)
{
g_return_val_if_fail (MOO_IS_OUTPUT_FILTER (filter), NULL);
2006-09-02 02:01:26 -07:00
return filter->priv->view;
2006-09-01 10:37:47 -07:00
}
static gboolean
moo_output_filter_output_line (MooOutputFilter *filter,
const char *line,
guint sig)
{
gboolean result = FALSE;
2007-06-11 22:42:20 -07:00
if (line[0] && line[1] && line[strlen(line) - 1] == '\n')
2006-09-01 10:37:47 -07:00
g_warning ("%s: oops", G_STRLOC);
g_signal_emit (filter, signals[sig], 0, line, &result);
return result;
}
gboolean
moo_output_filter_stdout_line (MooOutputFilter *filter,
const char *line)
{
g_return_val_if_fail (MOO_IS_OUTPUT_FILTER (filter), FALSE);
g_return_val_if_fail (line != NULL, FALSE);
return moo_output_filter_output_line (filter, line, STDOUT_LINE);
}
gboolean
moo_output_filter_stderr_line (MooOutputFilter *filter,
const char *line)
{
g_return_val_if_fail (MOO_IS_OUTPUT_FILTER (filter), FALSE);
g_return_val_if_fail (line != NULL, FALSE);
return moo_output_filter_output_line (filter, line, STDERR_LINE);
}
void
2006-09-02 02:01:26 -07:00
moo_output_filter_cmd_start (MooOutputFilter *filter,
2007-02-27 20:27:49 -08:00
const char *active_dir)
2006-09-01 10:37:47 -07:00
{
2006-09-02 02:01:26 -07:00
char *tmp;
2006-09-01 10:37:47 -07:00
g_return_if_fail (MOO_IS_OUTPUT_FILTER (filter));
2006-09-02 02:01:26 -07:00
2007-02-27 20:27:49 -08:00
tmp = filter->priv->active_dir;
filter->priv->active_dir = g_strdup (active_dir);
2006-09-02 02:01:26 -07:00
g_free (tmp);
2006-09-01 10:37:47 -07:00
g_signal_emit (filter, signals[CMD_START], 0);
}
gboolean
moo_output_filter_cmd_exit (MooOutputFilter *filter,
int status)
{
gboolean result = FALSE;
g_return_val_if_fail (MOO_IS_OUTPUT_FILTER (filter), FALSE);
g_signal_emit (filter, signals[CMD_EXIT], 0, status, &result);
return result;
}
2006-09-02 00:34:01 -07:00
MooFileLineData *
moo_file_line_data_new (const char *file,
int line,
int character)
{
MooFileLineData *data;
data = g_new0 (MooFileLineData, 1);
data->file = file && file[0] ? g_strdup (file) : NULL;
2006-09-02 00:34:01 -07:00
data->line = line;
data->character = character;
return data;
}
static MooFileLineData *
2006-09-02 00:34:01 -07:00
moo_file_line_data_copy (MooFileLineData *data)
{
MooFileLineData *copy = NULL;
if (data)
{
copy = g_memdup (data, sizeof (MooFileLineData));
copy->file = g_strdup (data->file);
}
return copy;
}
void
moo_file_line_data_free (MooFileLineData *data)
{
if (data)
{
g_free (data->file);
g_free (data);
}
}
2006-09-02 02:01:26 -07:00
GType
moo_file_line_data_get_type (void)
{
static GType type = 0;
if (G_UNLIKELY (!type))
type = g_boxed_type_register_static ("MooFileLineData",
(GBoxedCopyFunc) moo_file_line_data_copy,
(GBoxedFreeFunc) moo_file_line_data_free);
return type;
}
const char *
moo_output_filter_get_active_file (MooOutputFilter *filter)
{
g_return_val_if_fail (MOO_IS_OUTPUT_FILTER (filter), NULL);
2007-02-27 20:27:49 -08:00
return filter->priv->active_file;
}
void
moo_output_filter_set_active_file (MooOutputFilter *filter,
const char *filename)
{
char *tmp;
g_return_if_fail (MOO_IS_OUTPUT_FILTER (filter));
2007-02-27 20:27:49 -08:00
tmp = filter->priv->active_file;
filter->priv->active_file = g_strdup (filename);
g_free (tmp);
}
2007-02-27 20:27:49 -08:00
const char *
moo_output_filter_get_active_dir (MooOutputFilter *filter)
{
g_return_val_if_fail (MOO_IS_OUTPUT_FILTER (filter), NULL);
return filter->priv->active_dir;
}
2006-09-02 02:01:26 -07:00
void
moo_output_filter_set_window (MooOutputFilter *filter,
gpointer window)
{
g_return_if_fail (MOO_IS_OUTPUT_FILTER (filter));
g_return_if_fail (!window || MOO_IS_EDIT_WINDOW (window));
filter->priv->window = window;
}
#if 0
2006-09-02 02:01:26 -07:00
gpointer
moo_output_filter_get_window (MooOutputFilter *filter)
{
g_return_val_if_fail (MOO_IS_OUTPUT_FILTER (filter), NULL);
return filter->priv->window;
}
#endif
2006-09-02 02:01:26 -07:00
static void
moo_output_filter_open_file_line (MooOutputFilter *filter,
MooFileLineData *data)
2006-09-02 02:01:26 -07:00
{
const char *filename;
2006-09-02 02:01:26 -07:00
const char *path = NULL;
char *freeme = NULL;
g_return_if_fail (MOO_IS_OUTPUT_FILTER (filter));
g_return_if_fail (data != NULL);
2007-02-27 20:27:49 -08:00
filename = data->file ? data->file : filter->priv->active_file;
g_return_if_fail (filename != NULL);
if (g_path_is_absolute (filename))
2006-09-02 02:01:26 -07:00
{
path = filename;
2006-09-02 02:01:26 -07:00
}
2007-02-27 20:27:49 -08:00
else if (filter->priv->active_dir)
2006-09-02 02:01:26 -07:00
{
2007-02-27 20:27:49 -08:00
freeme = g_build_filename (filter->priv->active_dir, filename, NULL);
2006-09-02 02:01:26 -07:00
path = freeme;
}
if (path)
{
if (g_file_test (path, G_FILE_TEST_EXISTS))
{
MooEditor *editor = moo_editor_instance ();
moo_editor_open_file_line (editor, path, data->line, filter->priv->window);
}
else
{
2006-11-01 22:38:00 -08:00
_moo_message ("file '%s' does not exist", path);
2006-09-02 02:01:26 -07:00
}
}
else
{
_moo_message ("could not find file '%s'", filename);
2006-09-02 02:01:26 -07:00
}
g_free (freeme);
}