File: thunarx-provider-factory.c

package info (click to toggle)
thunar 4.20.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,636 kB
  • sloc: ansic: 91,676; sh: 5,036; makefile: 1,033; xml: 951; python: 41; sed: 16
file content (376 lines) | stat: -rw-r--r-- 11,445 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
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
/* 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 "thunarx/thunarx-private.h"
#include "thunarx/thunarx-provider-factory.h"
#include "thunarx/thunarx-provider-module.h"
#include "thunarx/thunarx-provider-plugin.h"

#include <gdk/gdk.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 void
thunarx_provider_factory_create_modules (ThunarxProviderFactory *factory);
static gboolean
thunarx_provider_factory_timer (gpointer user_data);
static void
thunarx_provider_factory_timer_destroy (gpointer user_data);

/**
 * SECTION: thunarx-provider-factory
 * @short_description: The provider factory support for applications
 * @title: ThunarxProviderFactory
 * @include: thunarx/thunarx.h
 *
 * The #ThunarxProviderFactory class allows applications to use Thunar plugins. It handles
 * the loading of the installed extensions and instantiates providers for the application.
 * For example, Thunar uses this class to access the installed extensions.
 */

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 */

  gint initialized;
};

static gboolean thunarx_provider_modules_created = FALSE;
static GList   *thunarx_provider_modules = NULL;            /* list of all active provider modules */
static GList   *thunarx_persistent_provider_modules = NULL; /* list of active persistent provider modules */
static GList   *thunarx_volatile_provider_modules = NULL;   /* list of active volatile 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)
{
  factory->initialized = FALSE;
}



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 = NULL;
  gint         n_types = 0;


  /* determines the types provided by the module */
  thunarx_provider_module_list_types (module, &types, &n_types);

  if (types != NULL && n_types != 0)
    {
      /* 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 void
thunarx_provider_factory_create_modules (ThunarxProviderFactory *factory)
{
  ThunarxProviderModule *module;
  const gchar           *name;
  GList                 *lp;
  GDir                  *dp;
  gchar                 *dirs_string = NULL;
  gchar                **dirs;

  if (g_strcmp0 (THUNARX_ENABLE_CUSTOM_DIRS, "TRUE") == 0)
    dirs_string = (gchar *) g_getenv ("THUNARX_DIRS");

  dirs_string = THUNARX_DIRECTORY":/usr/lib/thunarx-3";

  dirs = g_strsplit (dirs_string, G_SEARCHPATH_SEPARATOR_S, 0);

  for (int i = 0; dirs[i] != NULL; i++)
    {
      dp = g_dir_open (dirs[i], 0, NULL);

      if (G_LIKELY (dp != NULL))
        {
          /* determine the types for all existing plugins */
          for (name = g_dir_read_name (dp); name != NULL; name = g_dir_read_name (dp))
            {
              /* check if this is a valid plugin file */
              if (g_str_has_suffix (name, "." G_MODULE_SUFFIX))
                {
                  gboolean module_already_loaded = FALSE;

                  /* 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))
                        {
                          module_already_loaded = TRUE;
                          break;
                        }
                    }

                  if (G_UNLIKELY (module_already_loaded == TRUE))
                    continue;

                  /* allocate the new module and add it to our lists */
                  module = thunarx_provider_module_new (name);
                  thunarx_provider_modules = g_list_prepend (thunarx_provider_modules, module);
                }
            }

          g_dir_close (dp);
        }
    }

  g_strfreev (dirs);
}



static gboolean
thunarx_provider_factory_timer (gpointer user_data)
{
  ThunarxProviderFactory *factory = THUNARX_PROVIDER_FACTORY (user_data);
  ThunarxProviderInfo    *info;
  gint                    n;

  THUNAR_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;
        }
    }

  THUNAR_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.
 *
 * Returns: (transfer full): 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>
 *
 * Returns: (transfer full) (element-type GObject): the of providers for @type.
 **/
GList *
thunarx_provider_factory_list_providers (ThunarxProviderFactory *factory,
                                         GType                   type)
{
  ThunarxProviderInfo *info;
  GList               *providers = NULL;
  GList               *lp;
  gint                 n;

  /* create all available modules */
  if (thunarx_provider_modules_created == FALSE)
    {
      thunarx_provider_factory_create_modules (factory);

      /* On the first call, we need to load all modules once, since only when loaded, we can tell if they are persistent or volatile */
      for (lp = thunarx_provider_modules; lp != NULL; lp = lp->next)
        {
          g_type_module_use (G_TYPE_MODULE (lp->data));
          if (thunarx_provider_plugin_get_resident (THUNARX_PROVIDER_PLUGIN (lp->data)))
            thunarx_persistent_provider_modules = g_list_prepend (thunarx_persistent_provider_modules, lp->data);
          else
            thunarx_volatile_provider_modules = g_list_prepend (thunarx_volatile_provider_modules, lp->data);
        }

      thunarx_provider_modules_created = TRUE;
    }
  else
    {
      /* volatile modules are reloaded on each call */
      for (lp = thunarx_volatile_provider_modules; lp != NULL; lp = lp->next)
        g_type_module_use (G_TYPE_MODULE (lp->data));
    }

  if (factory->initialized == FALSE)
    {
      for (lp = thunarx_provider_modules; lp != NULL; lp = lp->next)
        thunarx_provider_factory_add (factory, THUNARX_PROVIDER_MODULE (lp->data));

      if (G_UNLIKELY (factory->timer_id == 0))
        {
          /* 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);
        }
      factory->initialized = TRUE;
    }

  /* 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);
      }

  /* unload all volatile modules */
  for (lp = thunarx_volatile_provider_modules; lp != NULL; lp = lp->next)
    thunarx_provider_module_unuse (THUNARX_PROVIDER_MODULE (lp->data));

  return providers;
}