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
|
/* -*- 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., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#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"
struct _MrpApplicationPriv {
GList *file_readers;
GList *file_writers;
GList *modules;
};
static void application_class_init (MrpApplicationClass *klass);
static void application_init (MrpApplication *app);
static void application_finalize (GObject *object);
static void application_init_gettext (void);
static void application_init_file_modules (MrpApplication *app);
static void application_finalize_file_modules
(MrpApplication *app);
static GObjectClass *parent_class;
static guint last_used_id;
static GHashTable *data_hash;
GType
mrp_application_get_type (void)
{
static GType type = 0;
if (!type) {
static const GTypeInfo info = {
sizeof (MrpApplicationClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) application_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (MrpApplication),
0, /* n_preallocs */
(GInstanceInitFunc) application_init,
};
type = g_type_register_static (G_TYPE_OBJECT,
"MrpApplication",
&info, 0);
}
return type;
}
static void
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 = application_finalize;
data_hash = g_hash_table_new (NULL, NULL);
last_used_id = 0;
}
static void
application_init (MrpApplication *app)
{
MrpApplicationPriv *priv;
static gboolean first = TRUE;
if (!first) {
g_error ("You can only create one instance of MrpApplication");
exit (1);
}
priv = g_new0 (MrpApplicationPriv, 1);
priv->file_readers = NULL;
priv->file_writers = NULL;
app->priv = priv;
application_init_gettext ();
application_init_file_modules (app);
first = FALSE;
}
static void
application_finalize (GObject *object)
{
MrpApplication *app = MRP_APPLICATION (object);
application_finalize_file_modules (app);
g_free (app->priv);
app->priv = NULL;
if (parent_class->finalize) {
parent_class->finalize (object);
}
}
static void
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");
imrp_time_init ();
}
static void
application_init_file_modules (MrpApplication *app)
{
app->priv->modules = mrp_file_module_load_all (app);
}
static void
application_finalize_file_modules (MrpApplication *app)
{
g_list_foreach (app->priv->file_readers, (GFunc) g_free, NULL);
g_list_free (app->priv->file_readers);
app->priv->file_readers = NULL;
g_list_foreach (app->priv->file_writers, (GFunc) g_free, NULL);
g_list_free (app->priv->file_writers);
app->priv->file_writers = NULL;
g_list_foreach (app->priv->modules, (GFunc) g_free, NULL);
g_list_free (app->priv->modules);
app->priv->modules = NULL;
}
void
imrp_application_register_reader (MrpApplication *app, MrpFileReader *reader)
{
MrpApplicationPriv *priv;
g_return_if_fail (MRP_IS_APPLICATION (app));
g_return_if_fail (reader != NULL);
priv = app->priv;
priv->file_readers = g_list_prepend (priv->file_readers, reader);
}
void
imrp_application_register_writer (MrpApplication *app, MrpFileWriter *writer)
{
MrpApplicationPriv *priv;
g_return_if_fail (MRP_IS_APPLICATION (app));
g_return_if_fail (writer != NULL);
priv = app->priv;
priv->file_writers = g_list_prepend (priv->file_writers, writer);
}
GList *
imrp_application_get_all_file_readers (MrpApplication *app)
{
MrpApplicationPriv *priv;
g_return_val_if_fail (MRP_IS_APPLICATION (app), NULL);
priv = app->priv;
return priv->file_readers;
}
GList *
imrp_application_get_all_file_writers (MrpApplication *app)
{
MrpApplicationPriv *priv;
g_return_val_if_fail (MRP_IS_APPLICATION (app), NULL);
priv = app->priv;
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
imrp_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:
*
* 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:
*
* Get the object reference in the list of MrpObjects
* using the object_id as locator
*
* Return value: a pointer to the data
**/
gboolean
imrp_application_id_remove_data (guint object_id)
{
return g_hash_table_remove (data_hash, GUINT_TO_POINTER (object_id));
}
|