File: gedit-message.c

package info (click to toggle)
gedit 3.30.2-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 35,836 kB
  • sloc: ansic: 48,529; python: 6,973; xml: 5,967; sh: 5,043; makefile: 1,240; perl: 30
file content (323 lines) | stat: -rw-r--r-- 7,655 bytes parent folder | download | duplicates (2)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 * gedit-message.c
 * This file is part of gedit
 *
 * Copyright (C) 2008, 2010 - Jesse van den Kieboom
 *
 * gedit 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.
 *
 * gedit 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 gedit; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

#include "gedit-message.h"

#include <string.h>

/**
 * SECTION:gedit-message
 * @short_description: message bus message object
 * @include: gedit/gedit-message.h
 *
 * Communication on a #GeditMessageBus is done through messages. Messages are
 * sent over the bus and received by connecting callbacks on the message bus.
 * A #GeditMessage is an instantiation of a #GeditMessageType, containing
 * values for the arguments as specified in the message type.
 *
 * A message can be seen as a method call, or signal emission depending on
 * who is the sender and who is the receiver. There is no explicit distinction
 * between methods and signals.
 *
 * Since: 2.26
 */

struct _GeditMessagePrivate
{
	gchar *object_path;
	gchar *method;
};

enum
{
	PROP_0,
	PROP_OBJECT_PATH,
	PROP_METHOD,
	LAST_PROP
};

static GParamSpec *properties[LAST_PROP];

G_DEFINE_TYPE_WITH_PRIVATE (GeditMessage, gedit_message, G_TYPE_OBJECT)

static void
gedit_message_finalize (GObject *object)
{
	GeditMessage *message = GEDIT_MESSAGE (object);

	g_free (message->priv->object_path);
	g_free (message->priv->method);

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

static void
gedit_message_get_property (GObject    *object,
                            guint       prop_id,
                            GValue     *value,
                            GParamSpec *pspec)
{
	GeditMessage *msg = GEDIT_MESSAGE (object);

	switch (prop_id)
	{
		case PROP_OBJECT_PATH:
			g_value_set_string (value,
			                    msg->priv->object_path);
			break;
		case PROP_METHOD:
			g_value_set_string (value,
			                    msg->priv->method);
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
			break;
	}
}

static void
gedit_message_set_property (GObject      *object,
                            guint         prop_id,
                            const GValue *value,
                            GParamSpec   *pspec)
{
	GeditMessage *msg = GEDIT_MESSAGE (object);

	switch (prop_id)
	{
		case PROP_OBJECT_PATH:
			g_free (msg->priv->object_path);
			msg->priv->object_path = g_value_dup_string (value);
			break;
		case PROP_METHOD:
			g_free (msg->priv->method);
			msg->priv->method = g_value_dup_string (value);
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
			break;
	}
}

static void
gedit_message_class_init (GeditMessageClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS(klass);

	object_class->finalize = gedit_message_finalize;

	object_class->get_property = gedit_message_get_property;
	object_class->set_property = gedit_message_set_property;

	/**
	 * GeditMessage:object_path:
	 *
	 * The messages object path (e.g. /gedit/object/path).
	 *
	 */
	properties[PROP_OBJECT_PATH] =
		g_param_spec_string ("object-path",
		                     "OBJECT_PATH",
		                     "The message object path",
		                     NULL,
		                     G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);

	/**
	 * GeditMessage:method:
	 *
	 * The messages method.
	 *
	 */
	properties[PROP_METHOD] =
		g_param_spec_string ("method",
		                     "METHOD",
		                     "The message method",
		                     NULL,
		                     G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties (object_class, LAST_PROP, properties);
}

static void
gedit_message_init (GeditMessage *self)
{
	self->priv = gedit_message_get_instance_private (self);
}

/**
 * gedit_message_get_method:
 * @message: the #GeditMessage
 *
 * Get the message method.
 *
 * Return value: the message method
 *
 */
const gchar *
gedit_message_get_method (GeditMessage *message)
{
	g_return_val_if_fail (GEDIT_IS_MESSAGE (message), NULL);

	return message->priv->method;
}

/**
 * gedit_message_get_object_path:
 * @message: the #GeditMessage
 *
 * Get the message object path.
 *
 * Return value: the message object path
 *
 */
const gchar *
gedit_message_get_object_path (GeditMessage *message)
{
	g_return_val_if_fail (GEDIT_IS_MESSAGE (message), NULL);

	return message->priv->object_path;
}

/**
 * gedit_message_is_valid_object_path:
 * @object_path: (allow-none): the object path
 *
 * Returns whether @object_path is a valid object path
 *
 * Return value: %TRUE if @object_path is a valid object path
 *
 */
gboolean
gedit_message_is_valid_object_path (const gchar *object_path)
{
	if (!object_path)
	{
		return FALSE;
	}

	/* needs to start with / */
	if (*object_path != '/')
	{
		return FALSE;
	}

	while (*object_path)
	{
		if (*object_path == '/')
		{
			++object_path;

			if (!*object_path || !(g_ascii_isalpha (*object_path) || *object_path == '_'))
			{
				return FALSE;
			}
		}
		else if (!(g_ascii_isalnum (*object_path) || *object_path == '_'))
		{
			return FALSE;
		}

		++object_path;
	}

	return TRUE;
}

/**
 * gedit_message_type_identifier:
 * @object_path: (allow-none): the object path
 * @method: (allow-none): the method
 *
 * Get the string identifier for @method at @object_path.
 *
 * Return value: the identifier for @method at @object_path
 *
 */
gchar *
gedit_message_type_identifier (const gchar *object_path,
                               const gchar *method)
{
	return g_strconcat (object_path, ".", method, NULL);
}

/**
 * gedit_message_has:
 * @message: the #GeditMessage
 * @propname: the property name
 *
 * Check if a message has a certain property.
 *
 * Return Value: %TRUE if message has @propname, %FALSE otherwise
 *
 */
gboolean
gedit_message_has (GeditMessage *message,
                   const gchar  *propname)
{
	GObjectClass *klass;

	g_return_val_if_fail (GEDIT_IS_MESSAGE (message), FALSE);
	g_return_val_if_fail (propname != NULL, FALSE);

	klass = G_OBJECT_GET_CLASS (G_OBJECT (message));

	return g_object_class_find_property (klass, propname) != NULL;
}

gboolean
gedit_message_type_has (GType         gtype,
                        const gchar  *propname)
{
	GObjectClass *klass;
	gboolean ret;

	g_return_val_if_fail (g_type_is_a (gtype, GEDIT_TYPE_MESSAGE), FALSE);
	g_return_val_if_fail (propname != NULL, FALSE);

	klass = g_type_class_ref (gtype);
	ret = g_object_class_find_property (klass, propname) != NULL;
	g_type_class_unref (klass);

	return ret;
}

gboolean
gedit_message_type_check (GType         gtype,
                          const gchar  *propname,
                          GType         value_type)
{
	GObjectClass *klass;
	gboolean ret;
	GParamSpec *spec;

	g_return_val_if_fail (g_type_is_a (gtype, GEDIT_TYPE_MESSAGE), FALSE);
	g_return_val_if_fail (propname != NULL, FALSE);

	klass = g_type_class_ref (gtype);
	spec = g_object_class_find_property (klass, propname);
	ret = spec != NULL && spec->value_type == value_type;
	g_type_class_unref (klass);

	return ret;
}

/* ex:set ts=8 noet: */