summaryrefslogtreecommitdiff
path: root/plugins/cover-thumbnailer/cover-thumbnailer.c
blob: 34302e9c7d1b224f21eb861ee9dd9b86990a693c (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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
/* vi:set et ai sw=2 sts=2 ts=2: */
/*-
 * Copyright (c) 2012 Nick Schermer <nick@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., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * Parts are based on the gsf-office-thumbnailer in libgsf, which is
 * written by Federico Mena-Quintero <federico@novell.com>.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif

#include <math.h>

#include <glib.h>
#include <glib/gi18n.h>
#include <glib-object.h>

#include <gdk-pixbuf/gdk-pixbuf.h>
#include <tumbler/tumbler.h>
#include <curl/curl.h>

#include <cover-thumbnailer/cover-thumbnailer.h>



#define SERIES_PATTERN    "\\b((?:s\\d{1,2}e\\d{1,2}|\\d{1,2}x\\d{1,2}))\\b"
#define ABBREV_PATTERN    "\\b(\\w{1,}(?:rip|scr)|r5|hdtv|(?:480|720|1080)p|\\w{3}$)\\b"
#define YEAR_PATTERN      "\\b(\\d{4})\\b"

#define OMDBAPI_QUERY_URL "http://www.omdbapi.com/?t="

#define TMDB_BASE_URL     "http://cf2.imgobject.com/t/p/"
#define TMDB_QUERY_URL    "http://api.themoviedb.org/3/search/movie?api_key="



static void cover_thumbnailer_finalize     (GObject                    *object);
static void cover_thumbnailer_set_property (GObject                    *object,
                                            guint                       prop_id,
                                            const GValue               *value,
                                            GParamSpec                 *pspec);
static void cover_thumbnailer_create       (TumblerAbstractThumbnailer *thumbnailer,
                                            GCancellable               *cancellable,
                                            TumblerFileInfo            *info);



struct _CoverThumbnailerClass
{
  TumblerAbstractThumbnailerClass __parent__;
};

struct _CoverThumbnailer
{
  TumblerAbstractThumbnailer __parent__;

  /* themoviedb api key */
  gchar  *api_key;

  /* whitelisted locations */
  GSList *locations;

  /* precompiled */
  GRegex *series_regex;
  GRegex *abbrev_regex;
  GRegex *year_regex;

  /* multi session for curl */
  CURLM *curl_multi;
};

enum
{
  PROP_0,
  PROP_LOCATIONS,
  PROP_API_KEY
};



G_DEFINE_DYNAMIC_TYPE (CoverThumbnailer,
                       cover_thumbnailer,
                       TUMBLER_TYPE_ABSTRACT_THUMBNAILER);



void
cover_thumbnailer_register (TumblerProviderPlugin *plugin)
{
  cover_thumbnailer_register_type (G_TYPE_MODULE (plugin));
}



static void
cover_thumbnailer_class_init (CoverThumbnailerClass *klass)
{
  TumblerAbstractThumbnailerClass *abstractthumbnailer_class;
  GObjectClass                    *gobject_class;

  abstractthumbnailer_class = TUMBLER_ABSTRACT_THUMBNAILER_CLASS (klass);
  abstractthumbnailer_class->create = cover_thumbnailer_create;

  gobject_class = G_OBJECT_CLASS (klass);
  gobject_class->finalize = cover_thumbnailer_finalize;
  gobject_class->set_property = cover_thumbnailer_set_property;

  g_object_class_install_property (gobject_class,
                                   PROP_LOCATIONS,
                                   g_param_spec_boxed ("locations",
                                                       "locations",
                                                       "locations",
                                                       G_TYPE_STRV,
                                                       G_PARAM_CONSTRUCT_ONLY |
                                                       G_PARAM_WRITABLE));

  g_object_class_install_property (gobject_class,
                                   PROP_API_KEY,
                                   g_param_spec_string ("api-key",
                                                        "api-key",
                                                        "api-key",
                                                        NULL,
                                                        G_PARAM_CONSTRUCT_ONLY |
                                                        G_PARAM_WRITABLE));
}



static void
cover_thumbnailer_class_finalize (CoverThumbnailerClass *klass)
{
}



static void
cover_thumbnailer_init (CoverThumbnailer *thumbnailer)
{
  /* prepare the regular expressions */
  thumbnailer->series_regex = g_regex_new (SERIES_PATTERN, G_REGEX_CASELESS, 0, NULL);
  thumbnailer->abbrev_regex = g_regex_new (ABBREV_PATTERN, G_REGEX_CASELESS, 0, NULL);
  thumbnailer->year_regex = g_regex_new (YEAR_PATTERN, 0, 0, NULL);

  /* curl dns share */
  thumbnailer->curl_multi = curl_multi_init ();
}



static void
cover_thumbnailer_set_property (GObject      *object,
                                guint         prop_id,
                                const GValue *value,
                                GParamSpec   *pspec)
{
  CoverThumbnailer     *cover = COVER_THUMBNAILER (object);
  guint                i;
  const gchar * const *locations;
  GFile               *gfile;

  switch (prop_id)
    {
    case PROP_API_KEY:
      cover->api_key = g_value_dup_string (value);
      break;

    case PROP_LOCATIONS:
      locations = g_value_get_boxed (value);
      g_assert (locations != NULL);
      for (i = 0; locations[i] != NULL; i++)
        {
          gfile = g_file_new_for_commandline_arg (locations[i]);
          cover->locations = g_slist_prepend (cover->locations, gfile);
        }
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}



static void
cover_thumbnailer_finalize (GObject *object)
{
  CoverThumbnailer *cover = COVER_THUMBNAILER (object);

  /* cleanup */
  g_regex_unref (cover->series_regex);
  g_regex_unref (cover->abbrev_regex);
  g_regex_unref (cover->year_regex);

  g_free (cover->api_key);

  g_slist_foreach (cover->locations, (GFunc) g_object_unref, NULL);
  g_slist_free (cover->locations);

  curl_multi_cleanup (cover->curl_multi);

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



static void
cover_thumbnailer_size_prepared (GdkPixbufLoader        *loader,
                                 gint                    source_width,
                                 gint                    source_height,
                                 TumblerThumbnailFlavor *flavor)
{

  gint    dest_width;
  gint    dest_height;
  gdouble hratio;
  gdouble wratio;

  g_return_if_fail (GDK_IS_PIXBUF_LOADER (loader));
  g_return_if_fail (TUMBLER_IS_THUMBNAIL_FLAVOR (flavor));

  /* get the destination size */
  tumbler_thumbnail_flavor_get_size (flavor, &dest_width, &dest_height);

  if (source_width <= dest_width && source_height <= dest_height)
    {
      /* do not scale the image */
      dest_width = source_width;
      dest_height = source_height;
    }
  else
    {
      /* determine which axis needs to be scaled down more */
      wratio = (gdouble) source_width / (gdouble) dest_width;
      hratio = (gdouble) source_height / (gdouble) dest_height;

      /* adjust the other axis */
      if (hratio > wratio)
        dest_width = rint (source_width / hratio);
     else
        dest_height = rint (source_height / wratio);
    }

  gdk_pixbuf_loader_set_size (loader, MAX (dest_width, 1), MAX (dest_height, 1));
}



static size_t
cover_thumbnailer_load_pixbuf_write (gpointer data,
                                     size_t   size,
                                     size_t   nmemb,
                                     gpointer user_data)
{
  GdkPixbufLoader *loader = GDK_PIXBUF_LOADER (user_data);
  size_t           len = size * nmemb;
  GError          *err = NULL;

  g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), 0);

  /* write to the loader */
  if (!gdk_pixbuf_loader_write (loader, data, len, &err))
    {
      g_critical ("Failed to write to pixbuf loader: %s", err->message);
      g_error_free (err);
    }

  return len;
}



static size_t
cover_thumbnailer_load_contents_write (gpointer data,
                                       size_t   size,
                                       size_t   nmemb,
                                       gpointer user_data)
{
  GString *contents = user_data;
  size_t   len = size * nmemb;

  g_string_append_len (contents, data, len);

  return len;
}



static gint
cover_thumbnailer_check_progress (gpointer user_data,
                                  gdouble  dltotal,
                                  gdouble  dlnow,
                                  gdouble  ultotal,
                                  gdouble  ulnow)
{
  GCancellable *cancellable = G_CANCELLABLE (user_data);

  g_return_val_if_fail (G_IS_CANCELLABLE (cancellable), 1);

  /* return 1 to stop the download, 0 continue */
  return g_cancellable_is_cancelled (cancellable);
}



static CURL *
cover_thumbnailer_load_prepare (CoverThumbnailer *cover,
                                const gchar      *url,
                                GCancellable     *cancellable)
{
  CURL *curl_handle;

  g_return_val_if_fail (g_str_has_prefix (url, "http://"), NULL);
  g_return_val_if_fail (G_IS_CANCELLABLE (cancellable), NULL);
  g_return_val_if_fail (IS_COVER_THUMBNAILER (cover), NULL);

  /* curl downloader */
  curl_handle = curl_easy_init ();
  curl_multi_add_handle (cover->curl_multi, curl_handle);
  /* curl_easy_setopt (curl_handle, CURLOPT_VERBOSE, TRUE); */
  curl_easy_setopt (curl_handle, CURLOPT_TCP_KEEPALIVE, TRUE);
  curl_easy_setopt (curl_handle, CURLOPT_URL, url);
  curl_easy_setopt (curl_handle, CURLOPT_USERAGENT, PACKAGE_NAME "/" PACKAGE_VERSION);

  /* cancellable check */
  curl_easy_setopt (curl_handle, CURLOPT_PROGRESSFUNCTION, cover_thumbnailer_check_progress);
  curl_easy_setopt (curl_handle, CURLOPT_PROGRESSDATA, cancellable);
  curl_easy_setopt (curl_handle, CURLOPT_NOPROGRESS, FALSE);

  return curl_handle;
}



static CURLcode
cover_thumbnailer_load_perform (CoverThumbnailer  *cover,
                                CURL              *curl_handle)
{
  gint            still_running;
  struct timeval  timeout;
  fd_set          fdread;
  fd_set          fdwrite;
  fd_set          fdexcep;
  gint            rc = 0;
  gint            maxfd;
  CURLcode        code = CURLE_OK;
  CURLMsg        *msg;

  do
    {
      /* start the action */
      while (curl_multi_perform (cover->curl_multi, &still_running)
             == CURLM_CALL_MULTI_PERFORM);

      if (!still_running)
        break;

      /* timeout once per second */
      timeout.tv_sec = 1;
      timeout.tv_usec = 0;

      FD_ZERO (&fdread);
      FD_ZERO (&fdwrite);
      FD_ZERO (&fdexcep);

      /* get file descriptors from the transfers */
      curl_multi_fdset (cover->curl_multi, &fdread, &fdwrite, &fdexcep, &maxfd);
      rc = select (maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);

      /* select error */
      if (rc == -1)
        break;
    }
  while (still_running);

  /* get return value */
  msg = curl_multi_info_read (cover->curl_multi, &rc);
  if (msg != NULL)
    code = msg->data.result;

  /* cleanup */
  curl_multi_remove_handle (cover->curl_multi, curl_handle);
  curl_easy_cleanup (curl_handle);

  return code;
}



static GdkPixbuf *
cover_thumbnailer_load_pixbuf (CoverThumbnailer        *cover,
                               const gchar             *url,
                               TumblerThumbnailFlavor  *flavor,
                               GCancellable            *cancellable,
                               GError                 **error)
{
  CURL            *curl_handle;
  GdkPixbufLoader *loader;
  GdkPixbuf       *pixbuf = NULL;
  gint             ret;

  g_return_val_if_fail (url != NULL, NULL);
  g_return_val_if_fail (G_IS_CANCELLABLE (cancellable), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
  g_return_val_if_fail (TUMBLER_IS_THUMBNAIL_FLAVOR (flavor), NULL);

  if (g_cancellable_is_cancelled (cancellable))
    return NULL;

  /* create a pixbuf loader */
  loader = gdk_pixbuf_loader_new ();
  g_signal_connect (loader, "size-prepared",
                    G_CALLBACK (cover_thumbnailer_size_prepared), flavor);

  /* download the image into a pixbuf loader */
  curl_handle = cover_thumbnailer_load_prepare (cover, url, cancellable);
  curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, cover_thumbnailer_load_pixbuf_write);
  curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, loader);
  ret = cover_thumbnailer_load_perform (cover, curl_handle);

  /* check if everything went fine */
  if (gdk_pixbuf_loader_close (loader, error)
      && ret == 0
      && !g_cancellable_is_cancelled (cancellable))
    {
      /* take the pixbuf */
      pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
      if (G_LIKELY (pixbuf != NULL))
        g_object_ref (pixbuf);
    }
  else if (ret != 0 && error == NULL)
    {
      /* curl error */
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                   _("Failed to load the poster image \"%s\""), url);
    }

  g_object_unref (loader);

  return pixbuf;
}



static gchar *
cover_thumbnailer_load_contents (CoverThumbnailer  *cover,
                                 const gchar       *uri,
                                 GCancellable      *cancellable,
                                 GError           **error)
{
  GString *contents;
  CURL    *curl_handle;
  gint     ret;

  if (g_cancellable_is_cancelled (cancellable))
    return NULL;

  /* prepare buffer */
  contents = g_string_new (NULL);

  /* download metadata */
  curl_handle = cover_thumbnailer_load_prepare (cover, uri, cancellable);
  curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, cover_thumbnailer_load_contents_write);
  curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, contents);
  ret = cover_thumbnailer_load_perform (cover, curl_handle);

  if (ret != 0)
    {
      /* curl error */
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                   _("Failed to load the metadata from \"%s\""), uri);
    }

  return g_string_free (contents, ret != 0);
}



static gboolean
cover_thumbnailer_get_title (CoverThumbnailer  *cover,
                             GFile            *gfile,
                             gchar           **ret_title,
                             gchar           **ret_year)
{
  gchar       *basename;
  gboolean     is_series;
  GMatchInfo  *match_info;
  gint         start_pos;
  gint         end_pos;
  gchar       *year = NULL;
  GString     *title;
  const gchar *p;
  gboolean     append_space;
  gunichar     uchar;
  gboolean     succeed;
  gchar       *temp;

  g_return_val_if_fail (G_IS_FILE (gfile), FALSE);
  g_return_val_if_fail (ret_title != NULL, FALSE);
  g_return_val_if_fail (ret_year != NULL, FALSE);

  /* get the basename */
  basename = g_file_get_basename (gfile);

  /* check if the title looks like a serie */
  is_series = g_regex_match (cover->series_regex, basename, 0, &match_info);

  /* if this is not a serie, look for other filename crap */
  if (is_series
      || g_regex_match (cover->abbrev_regex, basename, 0, &match_info))
    {
      /* remove series or abbrev suffix from the filename */
      if (g_match_info_fetch_pos (match_info, 0, &start_pos, NULL)
          && start_pos > 0)
        basename[start_pos] = '\0';
      g_match_info_free (match_info);

      if (!is_series)
        {
          /* for non-series, look for a year in the title */
          if (g_regex_match (cover->year_regex, basename, 0, &match_info))
            {
              /* store year and remove the suffix from the title */
              if (g_match_info_fetch_pos (match_info, 0, &start_pos, &end_pos)
                  && start_pos >= 0
                  && end_pos > start_pos)
                {
                  year = g_strndup (basename + start_pos, end_pos - start_pos);

                  if (start_pos == 0)
                    {
                      temp = g_strdup (basename + end_pos);
                      g_free (basename);
                      basename = temp;
                    }
                  else
                    {
                      basename[start_pos] = '\0';
                    }
                }
              g_match_info_free (match_info);
            }
        }
    }

  /* append the possible title part of the filename */
  title = g_string_sized_new (strlen (basename));
  for (p = basename, append_space = FALSE; *p != '\0'; p = g_utf8_next_char (p))
    {
      uchar = g_utf8_get_char (p);
      if (g_unichar_isalnum (uchar)
          || uchar == '\'' || uchar == '!')
        {
          if (append_space)
            {
              g_string_append_c (title, '+');
              append_space = FALSE;
            }

          /* append the char */
          g_string_append_unichar (title, uchar);
        }
      else if (title->len > 0)
        {
          /* start with a space next time we append a char */
          append_space = TRUE;
        }
    }

  /* finalize */
  g_free (basename);
  succeed = title->len > 1;
  *ret_title = g_string_free (title, !succeed);
  *ret_year = year;

  return succeed;
}



static gchar *
cover_thumbnailer_poster_url (CoverThumbnailer        *cover,
                              const gchar             *title,
                              const gchar             *year,
                              TumblerThumbnailFlavor  *flavor,
                              GCancellable            *cancellable,
                              GError                 **error)
{
  gchar       *query;
  const gchar *needle;
  const gchar *p;
  const gchar *k = NULL;
  gchar       *url_part;
  gchar       *url = NULL;
  gchar       *data;
  gint         dest_size;

  g_return_val_if_fail (TUMBLER_IS_THUMBNAIL_FLAVOR (flavor), NULL);
  g_return_val_if_fail (IS_COVER_THUMBNAILER (cover), NULL);

  if (G_LIKELY (cover->api_key == NULL))
    {
      needle = "\"Poster\":\"http://";
      query = g_strconcat (OMDBAPI_QUERY_URL, title,
                           year != NULL ? "&y=" : NULL, year,
                           NULL);
    }
  else
    {
      needle = "\"poster_path\":\"/";
      query = g_strconcat (TMDB_QUERY_URL, cover->api_key,
                           "&query=", title,
                           year != NULL ? "&year=" : NULL, year,
                           NULL);
    }

  data = cover_thumbnailer_load_contents (cover, query, cancellable, error);
  g_free (query);

  if (data != NULL)
    {
      p = strstr (data, needle);
      if (p != NULL)
        {
          p += strlen (needle);
          k = strstr (p, ".jpg\"");
        }

      if (p != NULL && k != NULL)
        {
          /* extract poster data from the contents */
          url_part = g_strndup (p, k - p);

          /* get destination size */
          tumbler_thumbnail_flavor_get_size (flavor, &dest_size, NULL);

          if (cover->api_key == NULL)
            {
              if (g_str_has_suffix (url_part, "_V1_SX300"))
                {
                  /* imdb supports output sizes, above means image X is 300px
                   * so set something that avoids scaling. Y is most of the time
                   * higher with posters, so set Y<thumbsize> */
                  url_part[strlen (url_part) - 4] = '\0';
                  url = g_strdup_printf ("http://%sY%d.jpg", url_part, dest_size);
                }
              else
                {
                  /* fallback that always works */
                  url = g_strconcat ("http://", url_part, ".jpg", NULL);
                }
            }
          else
            {
              /* see http://api.themoviedb.org/3/configuration?api_key= for the values */
              url = g_strconcat (TMDB_BASE_URL,
                                 dest_size <= 154 ? "w154" : "w342", /* optimize for 128 or 256 */
                                 "/", url_part, ".jpg", NULL);
            }
          g_free (url_part);
        }
      else
        {
          /* check for api key problems */
          if (cover->api_key != NULL
              && strstr (data, "Invalid API key") != NULL)
            {
              g_printerr ("\n%s.\n\n",
                          _("Invalid API key, you must be granted a valid "
                            "key. The Movie DB backend will be disabled."));

              g_free (cover->api_key);
              cover->api_key = NULL;
            }

          g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                               _("No poster key found in metadata"));
        }

      g_free (data);
    }

  return url;
}



static void
cover_thumbnailer_create (TumblerAbstractThumbnailer *thumbnailer,
                          GCancellable               *cancellable,
                          TumblerFileInfo            *info)
{
  CoverThumbnailer       *cover = COVER_THUMBNAILER (thumbnailer);
  const gchar            *uri;
  TumblerThumbnail       *thumbnail;
  gchar                  *year;
  gchar                  *title;
  GdkPixbuf              *pixbuf = NULL;
  gchar                  *poster_url;
  GError                 *error = NULL;
  TumblerImageData        data;
  TumblerThumbnailFlavor *flavor;
  GFile                  *gfile;
  GSList                 *lp;

  /* source file */
  uri = tumbler_file_info_get_uri (info);
  gfile = g_file_new_for_uri (uri);

  /* check if file is in allowed destinations */
  for (lp = cover->locations; lp != NULL; lp = lp->next)
    if (g_file_has_prefix (gfile, lp->data))
      break;

  if (lp == NULL)
    {
      /* location not white-listed */
      g_object_unref (gfile);
      g_signal_emit_by_name (thumbnailer, "error", uri,
                             TUMBLER_ERROR_UNSUPPORTED,
                             _("Location is not whitelisted in rc file"));
      return;
    }

  /* target data */
  thumbnail = tumbler_file_info_get_thumbnail (info);
  flavor = tumbler_thumbnail_get_flavor (thumbnail);

  /* extract title from filename */
  if (cover_thumbnailer_get_title (cover, gfile, &title, &year))
    {
      /* request online metadata and return the poster url */
      poster_url = cover_thumbnailer_poster_url (cover, title, year, flavor, cancellable, &error);

      g_free (title);
      g_free (year);

      if (poster_url != NULL)
        {
          /* download poster and load it in a pixbuf */
          pixbuf = cover_thumbnailer_load_pixbuf (cover, poster_url, flavor, cancellable, &error);
          g_free (poster_url);
        }
    }
  else
    {
      g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_INVALID_FILENAME,
                           _("Movie title is too short"));
    }

  if (pixbuf != NULL)
    {
      /* prepare the image data */
      data.data = gdk_pixbuf_get_pixels (pixbuf);
      data.has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
      data.bits_per_sample = gdk_pixbuf_get_bits_per_sample (pixbuf);
      data.width = gdk_pixbuf_get_width (pixbuf);
      data.height = gdk_pixbuf_get_height (pixbuf);
      data.rowstride = gdk_pixbuf_get_rowstride (pixbuf);
      data.colorspace = (TumblerColorspace) gdk_pixbuf_get_colorspace (pixbuf);

      tumbler_thumbnail_save_image_data (thumbnail, &data,
                                         tumbler_file_info_get_mtime (info),
                                         cancellable, &error);

      g_object_unref (pixbuf);
    }

  /* return the status */
  if (error != NULL)
    {
      g_signal_emit_by_name (thumbnailer, "error", uri,
                             error->code, error->message);
      g_error_free (error);
    }
  else
    {
      g_signal_emit_by_name (thumbnailer, "ready", uri);
    }

  g_object_unref (thumbnail);
  g_object_unref (flavor);
  g_object_unref (gfile);
}