summaryrefslogtreecommitdiff
path: root/logview/src/logview-filter.c
blob: 7dd8bf297b0681f04ed6abe86d12af7b40553fa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
/* Copyright (C) Johannes Schmid 2009 <jhs@gnome.org>
 * Copyright (C) 2012-2021 MATE Developers
 *
 * This file is part of MATE Utils.
 *
 * MATE Utils 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.
 *
 * MATE Utils 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 MATE Utils.  If not, see <https://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "logview-filter.h"

enum {
  PROP_0,
  PROP_REGEX,
  PROP_NAME,
  PROP_TEXTTAG
};

struct _LogviewFilterPrivate {
  GRegex* regex;
  gchar* name;
  GtkTextTag* tag;
};

G_DEFINE_TYPE_WITH_PRIVATE (LogviewFilter, logview_filter, G_TYPE_OBJECT);

static void
logview_filter_init (LogviewFilter *object)
{
  object->priv = logview_filter_get_instance_private (object);
  object->priv->tag = NULL;
}

static void
logview_filter_finalize (GObject *object)
{
  LogviewFilterPrivate *priv = LOGVIEW_FILTER (object)->priv;

  if (priv->tag) {
    g_object_unref (priv->tag);
  }

  g_regex_unref (priv->regex);
  g_free (priv->name);

  G_OBJECT_CLASS (logview_filter_parent_class)->finalize (object);
}

static void
logview_filter_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
  LogviewFilterPrivate* priv = LOGVIEW_FILTER (object)->priv;

  switch (prop_id) {
    case PROP_NAME:
      priv->name = g_value_dup_string (value);
      break;
    case PROP_REGEX: {
      GError* err;
      const gchar* regex;

      err = NULL;

      regex = g_value_get_string (value);
      priv->regex = g_regex_new (regex, 0, 0, &err);

      if (err) {
        g_regex_unref (priv->regex);
        priv->regex = NULL;
        g_warning ("Couldn't create GRegex object: %s", err->message);
        g_error_free (err);
      }

      break;
    }
    case PROP_TEXTTAG: {
      if (priv->tag) {
        g_object_unref (priv->tag);
      }

      priv->tag = g_value_dup_object (value);
      break;
    }
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
logview_filter_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
  LogviewFilterPrivate* priv = LOGVIEW_FILTER (object)->priv;

  switch (prop_id) {
    case PROP_REGEX:
      g_value_set_string (value, g_regex_get_pattern (priv->regex));
      break;
    case PROP_NAME:
      g_value_set_string (value, priv->name);
      break;
    case PROP_TEXTTAG:
      g_value_set_object (value, priv->tag);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
logview_filter_class_init (LogviewFilterClass *klass)
{
  GObjectClass* object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = logview_filter_finalize;
  object_class->set_property = logview_filter_set_property;
  object_class->get_property = logview_filter_get_property;

  g_object_class_install_property (object_class,
                                   PROP_REGEX,
                                   g_param_spec_string ("regex",
                                                        "regular expression",
                                                        "regular expression",
                                                        "NULL",
                                                        G_PARAM_WRITABLE | G_PARAM_READABLE | G_PARAM_CONSTRUCT_ONLY));

  g_object_class_install_property (object_class,
                                   PROP_NAME,
                                   g_param_spec_string ("name",
                                                        "name",
                                                        "name",
                                                        "NULL",
                                                        G_PARAM_WRITABLE | G_PARAM_READABLE | G_PARAM_CONSTRUCT_ONLY));

  g_object_class_install_property (object_class,
                                   PROP_TEXTTAG,
                                   g_param_spec_object ("texttag",
                                                        "texttag",
                                                        "The text tag to be set on matching lines",
                                                        GTK_TYPE_TEXT_TAG,
                                                        G_PARAM_READABLE | G_PARAM_WRITABLE));
}

/* public methods */

LogviewFilter*
logview_filter_new (const gchar *name, const gchar *regex)
{
  return g_object_new (LOGVIEW_TYPE_FILTER,
                       "name", name,
                       "regex", regex,
                       NULL);
}

gboolean
logview_filter_filter (LogviewFilter *filter, const gchar *line)
{
  GMatchInfo* match_info;
  LogviewFilterPrivate* priv;
  gboolean retval;

  g_return_val_if_fail (LOGVIEW_IS_FILTER (filter), FALSE);
  g_return_val_if_fail (line != NULL, FALSE);

  priv = filter->priv;

  g_regex_match (priv->regex, line, 0, &match_info);

  retval = g_match_info_matches (match_info);

  g_match_info_free (match_info);

  return retval;
}

GtkTextTag *
logview_filter_get_tag (LogviewFilter *filter)
{
  g_return_val_if_fail (LOGVIEW_IS_FILTER (filter), NULL);

  return filter->priv->tag;
}