File: mrp-property.c

package info (click to toggle)
planner 0.14.6-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 13,872 kB
  • ctags: 6,212
  • sloc: ansic: 67,306; sh: 10,242; xml: 3,495; sql: 886; makefile: 762; python: 3
file content (424 lines) | stat: -rw-r--r-- 10,609 bytes parent folder | download | duplicates (5)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2002 CodeFactory AB
 * Copyright (C) 2002 Richard Hult <richard@imendio.com>
 * Copyright (C) 2002 Mikael Hallendal <micke@imendio.com>
 * Copyright (C) 2002 Alvaro del Castillo <acs@barrapunto.com>
 *
 * This library 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 library 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 library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>

#include "mrp-private.h"
#include <glib/gi18n.h>
#include "mrp-project.h"
#include "mrp-property.h"

/* Quarks */
#define LABEL        "label"
#define PROJECT      "project"
#define DESCRIPTION  "description"
#define TYPE         "type"
#define USER_DEFINED "user_defined"

static void         property_set_type               (MrpProperty     *property,
						     MrpPropertyType  type);
static GParamSpec * property_param_spec_string_list (const gchar     *name,
						     const gchar     *nick,
						     const gchar     *blurb,
						     GParamFlags      flags);


static void
property_set_type (MrpProperty *property, MrpPropertyType type)
{
	g_param_spec_set_qdata (G_PARAM_SPEC (property),
				g_quark_from_static_string (TYPE),
				GINT_TO_POINTER (type));
}

void
imrp_property_set_project (MrpProperty *property, MrpProject *project)
{
	g_param_spec_set_qdata (G_PARAM_SPEC (property),
				g_quark_from_static_string (PROJECT),
				project);
}

/**
 * mrp_property_new:
 * @name: the name of the property
 * @type: an #MrpPropertyType
 * @label: the human readable label
 * @description: a string describing the property
 * @user_defined: a #gboolean
 *
 * Creates a new #MrpProperty. @name must be unique in the application.
 * @user_defined specifies if the property was created by a user or a plugin
 * or Planner itself.
 *
 * Return value: a newly create property
 **/
MrpProperty *
mrp_property_new (const gchar     *name,
		  MrpPropertyType  type,
		  const gchar     *label,
		  const gchar     *description,
		  gboolean         user_defined)
{
	MrpProperty *property = NULL;

	switch (type) {
	case MRP_PROPERTY_TYPE_INT:
		property = MRP_PROPERTY (g_param_spec_int (name,
							   "",
							   "",
							   G_MININT,
							   G_MAXINT,
							   0,
							   G_PARAM_READWRITE));
		break;
	case MRP_PROPERTY_TYPE_FLOAT:
		property = MRP_PROPERTY (g_param_spec_float (name,
							     "",
							     "",
							     -G_MAXFLOAT,
							     G_MAXFLOAT,
							     0.0,
							     G_PARAM_READWRITE));
		break;
	case MRP_PROPERTY_TYPE_DURATION:
		property = MRP_PROPERTY (g_param_spec_int (name,
							     "",
							     "",
							     G_MININT,
							     G_MAXINT,
							     0,
							     G_PARAM_READWRITE));
		break;
	case MRP_PROPERTY_TYPE_STRING:
		property = MRP_PROPERTY (g_param_spec_string (name,
							      "",
							      "",
							      NULL,
							      G_PARAM_READWRITE));
		break;
	case MRP_PROPERTY_TYPE_STRING_LIST:
		property = MRP_PROPERTY (property_param_spec_string_list (name,
									  "",
									  "",
									  G_PARAM_READWRITE));
		break;
	case MRP_PROPERTY_TYPE_DATE:
		property = MRP_PROPERTY (mrp_param_spec_time (name,
							      "",
							      "",
							      G_PARAM_READWRITE));
		break;
	case MRP_PROPERTY_TYPE_COST:
		property = MRP_PROPERTY (g_param_spec_float (name,
							     "",
							     "",
							     -G_MAXFLOAT,
							     G_MAXFLOAT,
							     0.0,
							     G_PARAM_READWRITE));
		break;
	default:
		break;
	};

	if (!property) {
		return NULL;
	}

	property_set_type (property, type);
	g_param_spec_set_qdata_full (G_PARAM_SPEC (property),
				     g_quark_from_static_string (LABEL),
				     g_strdup (label),
				     g_free);
	g_param_spec_set_qdata_full (G_PARAM_SPEC (property),
				     g_quark_from_static_string (DESCRIPTION),
				     g_strdup (description),
				     g_free);
	g_param_spec_set_qdata_full (G_PARAM_SPEC (property),
				     g_quark_from_static_string (USER_DEFINED),
				     GINT_TO_POINTER (user_defined),
				     NULL);
	return property;
}

/**
 * mrp_property_get_name:
 * @property: an #MrpProperty
 *
 * Fetches the name of @property
 *
 * Return value: the name of @property
 **/
const gchar *
mrp_property_get_name (MrpProperty *property)
{
	g_return_val_if_fail (property != NULL, NULL);

	return G_PARAM_SPEC (property)->name;
}

/**
 * mrp_property_get_property_type:
 * @property: an #MrpProperty
 *
 * Fetches the type of @property
 *
 * Return value: the type of @property
 **/
MrpPropertyType
mrp_property_get_property_type (MrpProperty *property)
{
	g_return_val_if_fail (property != NULL, MRP_PROPERTY_TYPE_NONE);

	return GPOINTER_TO_INT (
		g_param_spec_get_qdata (G_PARAM_SPEC (property),
					g_quark_from_static_string (TYPE)));
}

/**
 * mrp_property_set_label:
 * @property: an #MrpProperty
 * @label: a string containing the new label
 *
 * Sets the label of @property and signals the "property-changed" signal on
 * the project @property is attached to.
 **/
void
mrp_property_set_label (MrpProperty *property, const gchar *label)
{
	gpointer project;

	g_param_spec_set_qdata_full (G_PARAM_SPEC (property),
				     g_quark_from_static_string (LABEL),
				     g_strdup (label),
				     g_free);

	project = g_param_spec_get_qdata (G_PARAM_SPEC (property),
					  g_quark_from_static_string (PROJECT));
	if (project) {
		imrp_project_property_changed (MRP_PROJECT (project),
					       property);
	}
}

/**
 * mrp_property_get_label:
 * @property: an #MrpProperty
 *
 * Fetches the label of @property
 *
 * Return value: the label of @property
 **/
const gchar *
mrp_property_get_label (MrpProperty *property)
{
	g_return_val_if_fail (property != NULL, NULL);

	return ((const gchar *)
		g_param_spec_get_qdata (G_PARAM_SPEC (property),
					g_quark_from_static_string (LABEL)));
}

/**
 * mrp_property_set_description:
 * @property: an #MrpProperty
 * @description: a string containing the new description
 *
 * Sets the description of @property and signals the "property-changed" signal on the project @property is attached to.
 **/
void
mrp_property_set_description (MrpProperty *property, const gchar *description)
{
	gpointer project;

	g_param_spec_set_qdata_full (G_PARAM_SPEC (property),
				     g_quark_from_static_string (DESCRIPTION),
				     g_strdup (description),
				     g_free);

	project = g_param_spec_get_qdata (G_PARAM_SPEC (property),
					  g_quark_from_static_string (PROJECT));
	if (project) {
		imrp_project_property_changed (MRP_PROJECT (project),
					       property);
	}
}

/**
 * mrp_property_get_description:
 * @property: an #MrpProperty
 *
 * Fetches the description of @property
 *
 * Return value: the description of @property
 **/
const gchar *
mrp_property_get_description (MrpProperty *property)
{
	g_return_val_if_fail (property != NULL, NULL);

	return ((const gchar *)
		g_param_spec_get_qdata (G_PARAM_SPEC (property),
					g_quark_from_static_string (DESCRIPTION)));
}

/**
 * mrp_property_set_user_defined:
 * @property: an #MrpProperty
 * @user_defined: if the property is user defined
 *
 * Sets if @property is user-defined or created by a plugin or Planner
 * itself.
 **/
void
mrp_property_set_user_defined (MrpProperty *property, gboolean user_defined)
{
	g_param_spec_set_qdata_full (G_PARAM_SPEC (property),
				     g_quark_from_static_string (USER_DEFINED),
				     GINT_TO_POINTER (user_defined),
				     NULL);
}

/**
 * mrp_property_get_user_defined:
 * @property: an #MrpProperty
 *
 * Fetches if @property is uesr defined or not.
 *
 * Return value: %TRUE if @property is user defined, otherwise %FALSE
 **/
gboolean
mrp_property_get_user_defined (MrpProperty *property)
{
	return ((gboolean)
		GPOINTER_TO_INT (g_param_spec_get_qdata (G_PARAM_SPEC (property),
							 g_quark_from_static_string (USER_DEFINED))));
}

/**
 * mrp_property_ref:
 * @property: an #MrpProperty
 *
 * Add a reference to @property. User should call this when storing a reference
 * to @property.
 *
 * Return value: the property
 **/
MrpProperty *
mrp_property_ref (MrpProperty *property)
{
	g_return_val_if_fail (property != NULL, NULL);

	return MRP_PROPERTY (g_param_spec_ref (G_PARAM_SPEC (property)));
}

/**
 * mrp_property_unref:
 * @property: an #MrpProperty
 *
 * Remove a reference from @property. If the reference count reaches 0 the
 * property will be freed. User should not use it's reference after calling
 * mrp_property_unref().
 **/
void
mrp_property_unref (MrpProperty *property)
{
	g_return_if_fail (property != NULL);

	g_param_spec_unref (G_PARAM_SPEC (property));
}

/**
 * mrp_property_type_as_string:
 * @type: an #MrpPropertyType
 *
 * Transform a #MrpPropertyTYpe into a human readable string.
 *
 * Return value: a string representation of @type
 **/
const gchar *
mrp_property_type_as_string (MrpPropertyType type)
{
	switch (type) {
	case MRP_PROPERTY_TYPE_STRING:
		return _("Text");
	case MRP_PROPERTY_TYPE_STRING_LIST:
		return _("String list");
	case MRP_PROPERTY_TYPE_INT:
		return _("Integer number");
	case MRP_PROPERTY_TYPE_FLOAT:
		return _("Floating-point number");
	case MRP_PROPERTY_TYPE_DATE:
		return _("Date");
	case MRP_PROPERTY_TYPE_DURATION:
		return _("Duration");
	case MRP_PROPERTY_TYPE_COST:
		return _("Cost");
	case MRP_PROPERTY_TYPE_NONE:
		g_warning ("Requested name for type 'none'.");
		return _("None");
	default:
		g_assert_not_reached ();
	}

	return NULL;
}

static GParamSpec *
property_param_spec_string_list (const gchar *name,
				 const gchar *nick,
				 const gchar *blurb,
				 GParamFlags flags)
{
	GParamSpec *spec;

	spec = g_param_spec_string (name,
				    nick,
				    blurb,
				    NULL,
				    flags);

	return g_param_spec_value_array (name,
					 nick,
					 blurb,
					 spec,
					 flags);
}

/* Boxed types. */

GType
mrp_property_get_type (void)
{
	static GType our_type = 0;

	if (our_type == 0) {
		our_type = g_boxed_type_register_static ("MrpProperty",
							 (GBoxedCopyFunc) g_param_spec_ref,
							 (GBoxedFreeFunc) g_param_spec_unref);
	}

	return our_type;
}