File: mrp-application.c

package info (click to toggle)
planner 0.14.92-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,676 kB
  • sloc: ansic: 68,177; xml: 6,112; sql: 890; makefile: 43; sh: 42; python: 26
file content (266 lines) | stat: -rw-r--r-- 6,694 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
/* -*- 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) 2004 Alvaro del Castillo <acs@barrapunto.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 St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/**
 * SECTION:mrp-application
 * @Short_description: the main project application object.
 * @Title: MrpApplication
 * @include: libplanner/mrp-application.h
 *
 * #MrpApplication is the libplanner infrastructure.
 * It loads #GModule plug-ins, registers file readers and writers.
 *
 * One must instantiate an #MrpApplication to use libplanner.
 * A sole one can be instantiated.
 *
 * #MrpApplication also features a unique identifier generator.
 * mrp_application_get_unique_id() generates a unique id within the application.
 *
 * #MrpApplication is able to register pointers against an id.
 * mrp_application_id_get_data() retrieves the pointer given the id.
 * Every #MrpObject registers itself this way.
 */

#include <config.h>

#include <stdlib.h>

#include <glib/gi18n.h>
#include "mrp-file-module.h"
#include "mrp-private.h"
#include "mrp-application.h"
#include "mrp-paths.h"

typedef struct {
	GList *file_readers;
	GList *file_writers;
	GList *modules;
} MrpApplicationPrivate;

G_DEFINE_TYPE_WITH_PRIVATE (MrpApplication, mrp_application, G_TYPE_OBJECT)

static GObjectClass *parent_class;
static guint         last_used_id;
static GHashTable   *data_hash;

static void
mrp_application_finalize_file_modules (MrpApplication *app)
{
	MrpApplicationPrivate *priv = mrp_application_get_instance_private (app);
	g_list_foreach (priv->file_readers, (GFunc) g_free, NULL);
	g_list_free (priv->file_readers);
	priv->file_readers = NULL;

	g_list_foreach (priv->file_writers, (GFunc) g_free, NULL);
	g_list_free (priv->file_writers);
	priv->file_writers = NULL;

	g_list_foreach (priv->modules, (GFunc) g_free, NULL);
	g_list_free (priv->modules);
	priv->modules = NULL;
}

static void
mrp_application_finalize (GObject *object)
{
	MrpApplication *app = MRP_APPLICATION (object);

	mrp_application_finalize_file_modules (app);

	if (parent_class->finalize) {
		parent_class->finalize (object);
	}
}

static void
mrp_application_class_init (MrpApplicationClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));

	object_class->finalize = mrp_application_finalize;

	data_hash = g_hash_table_new (NULL, NULL);

	last_used_id = 0;

}

static void
mrp_application_init_gettext (void)
{
	gchar *locale_dir;

	locale_dir = mrp_paths_get_locale_dir ();
	bindtextdomain (GETTEXT_PACKAGE, locale_dir);
	g_free(locale_dir);

	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
}

static void
mrp_application_init_file_modules (MrpApplication *app)
{
	MrpApplicationPrivate *priv = mrp_application_get_instance_private (app);
	priv->modules = mrp_file_module_load_all (app);
}

static void
mrp_application_init (MrpApplication *app)
{
	MrpApplicationPrivate *priv;
	static gboolean     first = TRUE;

	if (!first) {
		g_error ("You can only create one instance of MrpApplication");
		exit (1);
	}

	priv = mrp_application_get_instance_private (app);
	priv->file_readers = NULL;
	priv->file_writers = NULL;

	mrp_application_init_gettext ();
	mrp_application_init_file_modules (app);

	first = FALSE;
}

void
mrp_application_register_reader (MrpApplication *app, MrpFileReader *reader)
{
	MrpApplicationPrivate *priv = mrp_application_get_instance_private (app);

	g_return_if_fail (MRP_IS_APPLICATION (app));
	g_return_if_fail (reader != NULL);

	priv->file_readers = g_list_prepend (priv->file_readers, reader);
}

void
mrp_application_register_writer (MrpApplication *app, MrpFileWriter *writer)
{
	MrpApplicationPrivate *priv = mrp_application_get_instance_private (app);

	g_return_if_fail (MRP_IS_APPLICATION (app));
	g_return_if_fail (writer != NULL);

	priv->file_writers = g_list_prepend (priv->file_writers, writer);
}

GList *
mrp_application_get_all_file_readers (MrpApplication *app)
{
	MrpApplicationPrivate *priv = mrp_application_get_instance_private (app);

	g_return_val_if_fail (MRP_IS_APPLICATION (app), NULL);

	return priv->file_readers;
}

GList *
mrp_application_get_all_file_writers (MrpApplication *app)
{
	MrpApplicationPrivate *priv = mrp_application_get_instance_private (app);

	g_return_val_if_fail (MRP_IS_APPLICATION (app), NULL);

	return priv->file_writers;
}

/**
 * mrp_application_new:
 *
 * Creates a new #MrpApplication.
 *
 * Return value: the newly created application
 **/
MrpApplication *
mrp_application_new (void)
{
	return g_object_new (MRP_TYPE_APPLICATION, NULL);
}

/**
 * mrp_application_get_unique_id:
 *
 * Returns a unique identifier in the #MrpApplication namespace.
 *
 * Return value: the unique id
 **/
guint
mrp_application_get_unique_id (void)
{
	return ++last_used_id;
}

/*
 * imrp_application_id_set_data:
 *
 * Set the data unique identifier for a data
 *
 * Return value: TRUE if the change has been done
 **/
gboolean
mrp_application_id_set_data (gpointer data,
			      guint    data_id)
{
	g_assert (g_hash_table_lookup (data_hash, GUINT_TO_POINTER (data_id)) == NULL);

	g_hash_table_insert (data_hash, GUINT_TO_POINTER (data_id), data);

	return TRUE;
}



/**
 * mrp_application_id_get_data:
 * @object_id: an object id
 *
 * Get the object reference in the list of MrpObjects
 * using the object_id as locator.
 *
 * Return value: a pointer to the data
 **/
gpointer
mrp_application_id_get_data (guint object_id)
{
	return g_hash_table_lookup (data_hash, GUINT_TO_POINTER (object_id));
}

/*
 * mrp_application_id_get_data:
 * @object_id: an object id
 *
 * Get the object reference in the list of MrpObjects
 * using the object_id as locator
 *
 * Return value: a pointer to the data
 **/
gboolean
mrp_application_id_remove_data (guint object_id)
{
	return g_hash_table_remove (data_hash, GUINT_TO_POINTER (object_id));
}