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
|
/* vi:set et ai sw=2 sts=2 ts=2: */
/*-
* Copyright (c) 2005-206 Benedikt Meurer <benny@xfce.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library 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.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <gdk/gdk.h>
#include <thunarx/thunarx-private.h>
#include <thunarx/thunarx-provider-factory.h>
#include <thunarx/thunarx-provider-module.h>
#include <thunarx/thunarx-provider-plugin.h>
/* "provider cache" cleanup interval (in seconds) */
#define THUNARX_PROVIDER_FACTORY_INTERVAL (45)
static void thunarx_provider_factory_finalize (GObject *object);
static void thunarx_provider_factory_add (ThunarxProviderFactory *factory,
ThunarxProviderModule *module);
static GList *thunarx_provider_factory_load_modules (ThunarxProviderFactory *factory);
static gboolean thunarx_provider_factory_timer (gpointer user_data);
static void thunarx_provider_factory_timer_destroy (gpointer user_data);
typedef struct
{
GObject *provider; /* cached provider reference or %NULL */
GType type; /* provider GType */
} ThunarxProviderInfo;
struct _ThunarxProviderFactoryClass
{
GObjectClass __parent__;
};
struct _ThunarxProviderFactory
{
GObject __parent__;
ThunarxProviderInfo *infos; /* provider types and cached provider references */
gint n_infos; /* number of items in the infos array */
guint timer_id; /* GSource timer to cleanup cached providers */
};
static GList *thunarx_provider_modules = NULL; /* list of active provider modules */
G_DEFINE_TYPE (ThunarxProviderFactory, thunarx_provider_factory, G_TYPE_OBJECT)
static void
thunarx_provider_factory_class_init (ThunarxProviderFactoryClass *klass)
{
GObjectClass *gobject_class;
gobject_class = G_OBJECT_CLASS (klass);
gobject_class->finalize = thunarx_provider_factory_finalize;
}
static void
thunarx_provider_factory_init (ThunarxProviderFactory *factory)
{
}
static void
thunarx_provider_factory_finalize (GObject *object)
{
ThunarxProviderFactory *factory = THUNARX_PROVIDER_FACTORY (object);
gint n;
/* stop the "provider cache" cleanup timer */
if (G_LIKELY (factory->timer_id != 0))
g_source_remove (factory->timer_id);
/* release provider infos */
for (n = 0; n < factory->n_infos; ++n)
if (factory->infos[n].provider != NULL)
g_object_unref (factory->infos[n].provider);
g_free (factory->infos);
(*G_OBJECT_CLASS (thunarx_provider_factory_parent_class)->finalize) (object);
}
static void
thunarx_provider_factory_add (ThunarxProviderFactory *factory,
ThunarxProviderModule *module)
{
const GType *types;
gint n_types;
/* determines the types provided by the module */
thunarx_provider_module_list_types (module, &types, &n_types);
/* add the types provided by the extension */
factory->infos = g_renew (ThunarxProviderInfo, factory->infos, factory->n_infos + n_types);
for (; n_types-- > 0; ++types)
{
factory->infos[factory->n_infos].provider = NULL;
factory->infos[factory->n_infos].type = *types;
++factory->n_infos;
}
}
static GList*
thunarx_provider_factory_load_modules (ThunarxProviderFactory *factory)
{
ThunarxProviderModule *module;
const gchar *name;
GList *modules = NULL;
GList *lp;
GDir *dp;
gsize i;
const gchar *thunarx_dirs[] =
{
THUNARX_DIRECTORY,
"/usr/lib/thunarx-2"
};
/* Search both multiarch and non-multiarch THUNARX_DIRECTORYs */
for (i = 0; i < G_N_ELEMENTS (thunarx_dirs); ++i)
{
dp = g_dir_open (thunarx_dirs[i], 0, NULL);
if (G_LIKELY (dp != NULL))
{
/* determine the types for all existing plugins */
for (;;)
{
/* read the next entry from the directory */
name = g_dir_read_name (dp);
if (G_UNLIKELY (name == NULL))
break;
/* check if this is a valid plugin file */
if (g_str_has_suffix (name, "." G_MODULE_SUFFIX))
{
/* check if we already have that module */
for (lp = thunarx_provider_modules; lp != NULL; lp = lp->next)
if (g_str_equal (G_TYPE_MODULE (lp->data)->name, name))
break;
/* use or allocate a new module for the file */
if (G_UNLIKELY (lp != NULL))
{
/* just use the existing module */
module = THUNARX_PROVIDER_MODULE (lp->data);
}
else
{
/* allocate the new module and add it to our list */
module = thunarx_provider_module_new (name);
thunarx_provider_modules = g_list_prepend (thunarx_provider_modules, module);
}
/* try to load the module */
if (g_type_module_use (G_TYPE_MODULE (module)))
{
/* add the types provided by the module */
thunarx_provider_factory_add (factory, module);
/* add the module to our list */
modules = g_list_prepend (modules, module);
}
}
}
g_dir_close (dp);
}
}
return modules;
}
static gboolean
thunarx_provider_factory_timer (gpointer user_data)
{
ThunarxProviderFactory *factory = THUNARX_PROVIDER_FACTORY (user_data);
ThunarxProviderInfo *info;
gint n;
GDK_THREADS_ENTER ();
/* drop all providers for which only we keep a reference */
for (n = factory->n_infos; --n >= 0; )
{
info = factory->infos + n;
if (info->provider != NULL && info->provider->ref_count == 1)
{
g_object_unref (info->provider);
info->provider = NULL;
}
}
GDK_THREADS_LEAVE ();
return TRUE;
}
static void
thunarx_provider_factory_timer_destroy (gpointer user_data)
{
THUNARX_PROVIDER_FACTORY (user_data)->timer_id = 0;
}
/**
* thunarx_provider_factory_get_default:
*
* Returns a reference to the default #ThunarxProviderFactory
* instance.
*
* The caller is responsible to free the returned object
* using g_object_unref() when no longer needed.
*
* Return value: a reference to the default
* #ThunarxProviderFactory instance.
**/
ThunarxProviderFactory*
thunarx_provider_factory_get_default (void)
{
static ThunarxProviderFactory *factory = NULL;
/* allocate the default factory instance on-demand */
if (G_UNLIKELY (factory == NULL))
{
factory = g_object_new (THUNARX_TYPE_PROVIDER_FACTORY, NULL);
g_object_add_weak_pointer (G_OBJECT (factory), (gpointer) &factory);
}
else
{
/* take a reference on the default factory for the caller */
g_object_ref (G_OBJECT (factory));
}
return factory;
}
/**
* thunarx_provider_factory_list_providers:
* @factory : a #ThunarxProviderFactory instance.
* @type : the provider #GType.
*
* Returns all providers of the given @type.
*
* The caller is responsible to release the returned
* list of providers using code like this:
* <informalexample><programlisting>
* g_list_free_full (list, g_object_unref);
* </programlisting></informalexample>
*
* Return value: the of providers for @type.
**/
GList*
thunarx_provider_factory_list_providers (ThunarxProviderFactory *factory,
GType type)
{
ThunarxProviderInfo *info;
GList *providers = NULL;
GList *modules = NULL;
GList *lp;
gint n;
/* check if the cleanup timer is running (and thereby the factory is initialized) */
if (G_UNLIKELY (factory->timer_id == 0))
{
/* load all available modules (and thereby initialize the factory) */
modules = thunarx_provider_factory_load_modules (factory);
/* start the "provider cache" cleanup timer */
factory->timer_id = g_timeout_add_seconds_full (G_PRIORITY_LOW, THUNARX_PROVIDER_FACTORY_INTERVAL,
thunarx_provider_factory_timer, factory,
thunarx_provider_factory_timer_destroy);
}
/* determine all available providers for the type */
for (info = factory->infos, n = factory->n_infos; --n >= 0; ++info)
if (G_LIKELY (g_type_is_a (info->type, type)))
{
/* allocate the provider on-demand */
if (G_UNLIKELY (info->provider == NULL))
{
info->provider = g_object_new (info->type, NULL);
if (G_UNLIKELY (info->provider == NULL))
continue;
}
/* take a reference for the caller */
g_object_ref (info->provider);
/* add the provider to the result list */
providers = g_list_append (providers, info->provider);
}
/* check if we were initialized by this method invocation */
if (G_UNLIKELY (modules != NULL))
{
/* unload all non-persistent modules */
for (lp = modules; lp != NULL; lp = lp->next)
if (!thunarx_provider_plugin_get_resident (lp->data))
g_type_module_unuse (G_TYPE_MODULE (lp->data));
g_list_free (modules);
}
return providers;
}
|