summaryrefslogtreecommitdiff
path: root/thunar/thunar-thumbnail-generator.c
blob: a2b59e09ef3c22450c859d9805cbabfbdb84c3f1 (plain)
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
/* $Id$ */
/*-
 * Copyright (c) 2005 Benedikt Meurer <benny@xfce.org>
 *
 * 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
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <thunar/thunar-thumbnail-generator.h>



typedef struct _ThunarThumbnailInfo ThunarThumbnailInfo;



static void     thunar_thumbnail_generator_class_init (ThunarThumbnailGeneratorClass *klass);
static void     thunar_thumbnail_generator_init       (ThunarThumbnailGenerator      *generator);
static void     thunar_thumbnail_generator_finalize   (GObject                       *object);
static gboolean thunar_thumbnail_generator_idle       (gpointer                       user_data);
static gpointer thunar_thumbnail_generator_thread     (gpointer                       user_data);
static void     thunar_thumbnail_info_free            (ThunarThumbnailInfo           *info);



struct _ThunarThumbnailGeneratorClass
{
  GObjectClass __parent__;
};

struct _ThunarThumbnailGenerator
{
  GObject __parent__;

  ThunarVfsThumbFactory *factory;
  volatile GThread      *thread;
  GMutex                *lock;
  GCond                 *cond;
  gint                   idle_id;

  GList                 *requests;
  GList                 *replies;
};

struct _ThunarThumbnailInfo
{
  ThunarFile    *file;
  ThunarVfsInfo *info;
};



static GObjectClass *thunar_thumbnail_generator_parent_class;



GType
thunar_thumbnail_generator_get_type (void)
{
  static GType type = G_TYPE_INVALID;

  if (G_UNLIKELY (type == G_TYPE_INVALID))
    {
      static const GTypeInfo info =
      {
        sizeof (ThunarThumbnailGeneratorClass),
        NULL,
        NULL,
        (GClassInitFunc) thunar_thumbnail_generator_class_init,
        NULL,
        NULL,
        sizeof (ThunarThumbnailGenerator),
        0,
        (GInstanceInitFunc) thunar_thumbnail_generator_init,
        NULL,
      };

      type = g_type_register_static (G_TYPE_OBJECT, I_("ThunarThumbnailGenerator"), &info, 0);
    }

  return type;
}



static void
thunar_thumbnail_generator_class_init (ThunarThumbnailGeneratorClass *klass)
{
  GObjectClass *gobject_class;

  /* determine the parent type class */
  thunar_thumbnail_generator_parent_class = g_type_class_peek_parent (klass);

  gobject_class = G_OBJECT_CLASS (klass);
  gobject_class->finalize = thunar_thumbnail_generator_finalize;
}



static void
thunar_thumbnail_generator_init (ThunarThumbnailGenerator *generator)
{
  generator->lock = g_mutex_new ();
  generator->cond = g_cond_new ();
  generator->idle_id = -1;
}



static void
thunar_thumbnail_generator_finalize (GObject *object)
{
  ThunarThumbnailGenerator *generator = THUNAR_THUMBNAIL_GENERATOR (object);

  /* acquire the generator lock */
  g_mutex_lock (generator->lock);

  /* release all requests */
  g_list_foreach (generator->requests, (GFunc) thunar_thumbnail_info_free, NULL);
  g_list_free (generator->requests);
  generator->requests = NULL;

  /* wait for the generator thread to terminate */
  while (G_UNLIKELY (generator->thread != NULL))
    g_cond_wait (generator->cond, generator->lock);

  /* release all replies */
  g_list_foreach (generator->replies, (GFunc) thunar_thumbnail_info_free, NULL);
  g_list_free (generator->replies);
  generator->replies = NULL;

  /* drop the idle source (if any) */
  if (G_UNLIKELY (generator->idle_id >= 0))
    g_source_remove (generator->idle_id);

  /* release the thumbnail factory */
  g_object_unref (G_OBJECT (generator->factory));

  /* release the generator lock */
  g_mutex_unlock (generator->lock);

  /* release the cond and mutex */
  g_mutex_free (generator->lock);
  g_cond_free (generator->cond);

  (*G_OBJECT_CLASS (thunar_thumbnail_generator_parent_class)->finalize) (object);
}



static gboolean
thunar_thumbnail_generator_idle (gpointer user_data)
{
  ThunarThumbnailGenerator *generator = THUNAR_THUMBNAIL_GENERATOR (user_data);
  ThunarThumbnailInfo      *info;
  GList                    *infos;
  GList                    *lp;

  /* acquire the lock on the generator */
  g_mutex_lock (generator->lock);

  /* grab the infos returned from the thumbnailer thread */
  infos = generator->replies;
  generator->replies = NULL;

  /* reset the process idle id */
  generator->idle_id = -1;

  /* release the lock on the generator */
  g_mutex_unlock (generator->lock);

  /* acquire the GDK thread lock */
  GDK_THREADS_ENTER ();

  /* invoke "changed" signals on all files */
  for (lp = infos; lp != NULL; lp = lp->next)
    {
      /* invoke "changed" and release the info */
      info = (ThunarThumbnailInfo *) lp->data;
      thunar_file_changed (THUNAR_FILE (info->file));
      thunar_thumbnail_info_free (info);
    }
  g_list_free (infos);

  /* release the GDK thread lock */
  GDK_THREADS_LEAVE ();

  return FALSE;
}



static gpointer
thunar_thumbnail_generator_thread (gpointer user_data)
{
  ThunarThumbnailGenerator *generator = THUNAR_THUMBNAIL_GENERATOR (user_data);
  ThunarThumbnailInfo      *info;
  GdkPixbuf                *icon;

  for (;;)
    {
      /* lock the factory */
      g_mutex_lock (generator->lock);

      /* grab the first thumbnail from the request list */
      if (G_LIKELY (generator->requests != NULL))
        {
          info = generator->requests->data;
          generator->requests = g_list_remove (generator->requests, info);
        }
      else
        {
          info = NULL;
        }

      /* check if we have something to generate */
      if (G_UNLIKELY (info == NULL))
        {
          /* reset the thread pointer */
          generator->thread = NULL;
          g_cond_signal (generator->cond);
          g_mutex_unlock (generator->lock);
          break;
        }

      /* release the lock */
      g_mutex_unlock (generator->lock);

      /* try to generate the thumbnail */
      icon = thunar_vfs_thumb_factory_generate_thumbnail (generator->factory, info->info);

      /* store the thumbnail (or the failed notice) */
      thunar_vfs_thumb_factory_store_thumbnail (generator->factory, icon, info->info, NULL);

      /* place the thumbnail on the reply list and schedule the idle source */
      g_mutex_lock (generator->lock);
      generator->replies = g_list_prepend (generator->replies, info);
      if (G_UNLIKELY (generator->idle_id < 0))
        generator->idle_id = g_idle_add_full (G_PRIORITY_LOW, thunar_thumbnail_generator_idle, generator, NULL);
      g_mutex_unlock (generator->lock);

      /* release the icon (if any) */
      if (G_LIKELY (icon != NULL))
        g_object_unref (G_OBJECT (icon));
    }

  return NULL;
}



static void
thunar_thumbnail_info_free (ThunarThumbnailInfo *info)
{
  g_object_unref (G_OBJECT (info->file));
  thunar_vfs_info_unref (info->info);
  g_free (info);
}



/**
 * thunar_thumbnail_generator_new:
 * @factory : a #ThunarVfsThumbFactory.
 *
 * Allocates a new #ThunarThumbnailGenerator object,
 * which can be used to generate and store thumbnails
 * for files.
 *
 * The caller is responsible to free the returned
 * object using g_object_unref() when no longer needed.
 *
 * Return value: a newly allocated #ThunarThumbnailGenerator.
 **/
ThunarThumbnailGenerator*
thunar_thumbnail_generator_new (ThunarVfsThumbFactory *factory)
{
  ThunarThumbnailGenerator *generator;

  g_return_val_if_fail (THUNAR_VFS_IS_THUMB_FACTORY (factory), NULL);

  /* allocate the generator object */
  generator = g_object_new (THUNAR_TYPE_THUMBNAIL_GENERATOR, NULL);
  generator->factory = g_object_ref (G_OBJECT (factory));

  return generator;
}



/**
 * thunar_thumbnail_generator_enqueue:
 * @generator
 * @file
 **/
void
thunar_thumbnail_generator_enqueue (ThunarThumbnailGenerator *generator,
                                    ThunarFile               *file)
{
  ThunarThumbnailInfo *info;
  GError              *error = NULL;
  GList               *lp;

  g_return_if_fail (THUNAR_IS_THUMBNAIL_GENERATOR (generator));
  g_return_if_fail (THUNAR_IS_FILE (file));

  /* acquire the generator lock */
  g_mutex_lock (generator->lock);

  /* check if the file is already on the request list */
  for (lp = generator->requests; lp != NULL; lp = lp->next)
    if (((ThunarThumbnailInfo *) lp->data)->file == file)
      break;

  /* schedule a request for the file if it's not already done */
  if (G_LIKELY (lp == NULL))
    {
      /* allocate a thumbnail info for the file */
      info = g_new (ThunarThumbnailInfo, 1);
      info->file = g_object_ref (G_OBJECT (file));
      info->info = thunar_vfs_info_copy (thunar_file_get_info (file));

      /* schedule the request */
      generator->requests = g_list_append (generator->requests, info);

      /* start the generator thread on-demand */
      if (G_UNLIKELY (generator->thread == NULL))
        {
          generator->thread = g_thread_create_full (thunar_thumbnail_generator_thread, generator, 0,
                                                    FALSE, FALSE, G_THREAD_PRIORITY_LOW, &error);
          if (G_UNLIKELY (generator->thread == NULL))
            {
              g_warning ("Failed to launch thumbnail generator thread: %s", error->message);
              g_error_free (error);
            }
        }
    }

  /* release the generator lock */
  g_mutex_unlock (generator->lock);
}