summaryrefslogtreecommitdiff
path: root/plparse/totem-disc.c
blob: 74ea5040a40e7037699a670353cf23ce7bcf9c5d (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
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
/* Totem Disc Content Detection
 * Copyright (C) 2004 Ronald Bultje <rbultje@ronald.bitfreak.net>
 * Copyright (C) 2004-2007 Bastien Nocera <hadess@hadess.net>
 *
 * 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.
 *
 * The Totem project hereby grant permission for non-gpl compatible GStreamer
 * plugins to be used and distributed together with GStreamer and Totem. This
 * permission are above and beyond the permissions granted by the GPL license
 * Totem is covered by.
 *
 * Monday 7th February 2005: Christian Schaller: Add exception clause.
 * See license_change file for details.
 *
 */

/**
 * SECTION:totem-disc
 * @short_description: disc utility functions
 * @stability: Stable
 * @include: totem-disc.h
 *
 * This file has various different disc utility functions for getting
 * the media types and labels of discs.
 *
 * The functions in this file refer to MRLs, which are a special form
 * of URIs used by xine to refer to things such as DVDs. An example of
 * an MRL would be <literal>dvd:///dev/scd0</literal>, which is not a
 * valid URI as far as, for example, GIO is concerned.
 *
 * The rest of the totem-pl-parser API exclusively uses URIs.
 **/

#include "config.h"

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include <sys/stat.h>

#include <glib.h>
#include <glib/gi18n.h>
#include <gio/gio.h>

#ifdef HAVE_LIBARCHIVE
#include <archive.h>
#include <archive_entry.h>
#endif /* HAVE_ARCHIVE */

#include "totem-disc.h"
#include "totem-pl-parser.h"

typedef struct _CdCache {
  /* device node and mountpoint */
  char *device, *mountpoint;
  GVolume *volume;

  char **content_types;

  GFile *iso_file;

  /* Whether we have a medium */
  guint has_medium : 1;
  /* if we're checking a media, or a dir */
  guint is_media : 1;

  /* indicates if we mounted this mountpoint ourselves or if it
   * was already mounted. */
  guint self_mounted : 1;
  guint mounted : 1;

  /* Whether it's a local ISO file */
  guint is_iso : 1;
} CdCache;

typedef struct _CdCacheCallbackData {
  CdCache *cache;
  gboolean called;
  gboolean result;
  GError *error;
} CdCacheCallbackData;

static void cd_cache_free (CdCache *cache);

static char *
totem_resolve_symlink (const char *device, GError **error)
{
  char *dir, *_link;
  char *f;
  char *f1;

  f = g_strdup (device);
  while (g_file_test (f, G_FILE_TEST_IS_SYMLINK)) {
    _link = g_file_read_link (f, error);
    if (_link == NULL) {
      g_free (f);
      return NULL;
    }

    dir = g_path_get_dirname (f);
    f1 = g_build_filename (dir, _link, NULL);
    g_free (dir);
    g_free (f);
    f = f1;
  }

  if (f != NULL) {
    GFile *file;

    file = g_file_new_for_path (f);
    f1 = g_file_get_path (file);
    g_object_unref (file);
    g_free (f);
    f = f1;
  }
  return f;
}

static gboolean
cd_cache_get_best_mount_for_drive (GDrive *drive,
				   char **mountpoint,
				   GVolume **volume)
{
  GList *list, *l;
  int rank;

  rank = 0;
  list = g_drive_get_volumes (drive);
  for (l = list; l != NULL; l = l->next) {
    GVolume *v;
    GMount *m;
    GFile *file;
    int new_rank;

    v = l->data;

    m = g_volume_get_mount (v);
    if (m == NULL)
      continue;

    file = g_mount_get_root (m);
    if (g_file_has_uri_scheme (file, "cdda"))
      new_rank = 100;
    else
      new_rank = 50;

    if (new_rank > rank) {
      if (*mountpoint)
	g_free (*mountpoint);
      if (*volume)
	g_object_unref (volume);

      *volume = g_object_ref (v);
      *mountpoint = g_file_get_path (file);
      rank = new_rank;
    }

    g_object_unref (file);
    g_object_unref (m);
  }

  g_list_foreach (list, (GFunc) g_object_unref, NULL);
  g_list_free (list);

  return rank > 0;
}

static gboolean
cd_cache_get_dev_from_volumes (GVolumeMonitor *mon, const char *device,
			      char **mountpoint, GVolume **volume)
{
  GList *list, *l;
  gboolean found;

  found = FALSE;
  list = g_volume_monitor_get_connected_drives (mon);
  for (l = list; l != NULL; l = l->next) {
    GDrive *drive;
    char *ddev, *resolved;

    drive = l->data;
    ddev = g_drive_get_identifier (drive, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
    if (ddev == NULL)
      continue;
    resolved = totem_resolve_symlink (ddev, NULL);
    g_free (ddev);
    if (resolved == NULL)
      continue;

    if (strcmp (resolved, device) == 0) {
      found = cd_cache_get_best_mount_for_drive (drive, mountpoint, volume);
    }

    g_free (resolved);
    if (found != FALSE)
      break;
  }

  g_list_foreach (list, (GFunc) g_object_unref, NULL);
  g_list_free (list);

  if (found != FALSE)
    return found;

  /* Not in the drives? Look in the volumes themselves */
  found = FALSE;
  list = g_volume_monitor_get_volumes (mon);
  for (l = list; l != NULL; l = l->next) {
    GVolume *vol;
    char *ddev, *resolved;

    vol = l->data;
    ddev = g_volume_get_identifier (vol, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
    if (ddev == NULL)
      continue;
    resolved = totem_resolve_symlink (ddev, NULL);
    g_free (ddev);
    if (resolved == NULL)
      continue;

    if (strcmp (resolved, device) == 0) {
      found = TRUE;
      *volume = g_object_ref (vol);
    }

    g_free (resolved);
    if (found != FALSE)
      break;
  }

  g_list_foreach (list, (GFunc) g_object_unref, NULL);
  g_list_free (list);

  return found;
}

static gboolean
cd_cache_has_content_type (CdCache *cache, const char *content_type)
{
  guint i;

  if (cache->content_types == NULL) {
    return FALSE;
  }

  for (i = 0; cache->content_types[i] != NULL; i++) {
    if (g_str_equal (cache->content_types[i], content_type) != FALSE)
      return TRUE;
  }
  return FALSE;
}

static gboolean
cd_cache_check_archive (CdCache *cache,
			const char *filename,
			GError **error)
{
#ifndef HAVE_LIBARCHIVE
  g_set_error (error, TOTEM_PL_PARSER_ERROR, TOTEM_PL_PARSER_ERROR_MOUNT_FAILED,
	       _("Failed to mount %s."), filename);
  return FALSE;
#else
  struct archive *a;
  struct archive_entry *entry;
  const char * content_types[] = { NULL, NULL };
  int r;

  a = archive_read_new();
  archive_read_support_format_iso9660(a);
  r = archive_read_open_filename(a, filename, 10240);
  if (r != ARCHIVE_OK) {
    g_set_error (error, TOTEM_PL_PARSER_ERROR, TOTEM_PL_PARSER_ERROR_MOUNT_FAILED,
		 _("Failed to mount %s."), filename);
    return FALSE;
  }
  while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
    const char *name;

    name = archive_entry_pathname (entry);
    if (g_ascii_strcasecmp (name, "VIDEO_TS/VIDEO_TS.IFO") == 0) {
      content_types[0] = "x-content/video-dvd";
      cache->content_types = g_strdupv ((gchar**) content_types);
      break;
    } else if (g_ascii_strcasecmp (name, "mpegav/AVSEQ01.DAT") == 0) {
      content_types[0] = "x-content/video-vcd";
      cache->content_types = g_strdupv ((gchar**) content_types);
      break;
    } else if (g_ascii_strcasecmp (name, "MPEG2/AVSEQ01.MPG") == 0) {
      content_types[0] = "x-content/video-svcd";
      cache->content_types = g_strdupv ((gchar**) content_types);
      break;
    } else if (g_ascii_strcasecmp (name, "BDAV") == 0 ||
               g_ascii_strcasecmp (name, "BDMV") == 0) {
      content_types[0] = "x-content/video-bluray";
      cache->content_types = g_strdupv ((gchar**) content_types);
      break;
    }
    archive_read_data_skip(a);
  }
  archive_read_free(a);
  return TRUE;
#endif
}

static char *
unescape_archive_name (const char *orig_uri)
{
  char *uri;
  guint len;
  char *escape1, *escape2;

  uri = g_strdup (orig_uri);

  /* Remove trailing slash */
  len = strlen (uri);
  if (uri[len - 1] == '/')
    uri[len - 1] = '\0';

  /* Unescape the path */
  escape1 = g_uri_unescape_string (uri + strlen ("archive://"), NULL);
  escape2 = g_uri_unescape_string (escape1, NULL);
  g_free (escape1);
  g_free (uri);

  return escape2;
}

static CdCache *
cd_cache_new (const char *dev,
	      GError     **error)
{
  CdCache *cache;
  char *mountpoint = NULL, *device, *local;
  GVolumeMonitor *mon;
  GVolume *volume = NULL;
  GFile *file;
  gboolean found, self_mounted;

  if (dev[0] == '/') {
    local = g_strdup (dev);
    file = g_file_new_for_path (dev);
  } else {
    if (g_str_has_prefix (dev, "archive://")) {
      char *orig_uri;
      orig_uri = unescape_archive_name (dev);
      file = g_file_new_for_uri (orig_uri);
      g_free (orig_uri);
      local = g_file_get_path (file);
    } else {
      file = g_file_new_for_commandline_arg (dev);
      local = g_file_get_path (file);
    }
  }

  if (local == NULL) {
    /* No error, just no cache */
    g_object_unref (file);
    return NULL;
  }

  self_mounted = FALSE;

  if (g_file_test (local, G_FILE_TEST_IS_DIR) != FALSE) {
    cache = g_new0 (CdCache, 1);
    cache->mountpoint = local;
    cache->is_media = FALSE;
    cache->content_types = g_content_type_guess_for_tree (file);
    g_object_unref (file);

    return cache;
  } else if (g_file_test (local, G_FILE_TEST_IS_REGULAR)) {
    cache = g_new0 (CdCache, 1);
    cache->is_iso = TRUE;
    cache->is_media = FALSE;

    g_object_unref (file);

    if (cd_cache_check_archive (cache, local, error) == FALSE) {
      cd_cache_free (cache);
      return FALSE;
    }

    cache->device = local;
    cache->self_mounted = FALSE;
    cache->mounted = FALSE;

    return cache;
  }

  g_object_unref (file);

  /* We have a local device
   * retrieve mountpoint and volume from gio volumes */
  device = totem_resolve_symlink (local, error);
  g_free (local);
  if (!device)
    return NULL;
  mon = g_volume_monitor_get ();
  found = cd_cache_get_dev_from_volumes (mon, device, &mountpoint, &volume);
  if (!found) {
    g_set_error (error, TOTEM_PL_PARSER_ERROR, TOTEM_PL_PARSER_ERROR_NO_DISC,
	_("No media in drive for device ā€œ%sā€."),
	device);
    g_free (device);
    return NULL;
  }

  cache = g_new0 (CdCache, 1);
  cache->device = device;
  cache->mountpoint = mountpoint;
  cache->self_mounted = self_mounted;
  cache->volume = volume;
  cache->is_media = TRUE;

  {
    GMount *mount;

    mount = g_volume_get_mount (volume);
    if (mount) {
      cache->content_types = g_mount_guess_content_type_sync (mount, FALSE, NULL, NULL);
      g_object_unref (mount);
    }
  }

  return cache;
}

static gboolean
cd_cache_has_medium (CdCache *cache)
{
  GDrive *drive;
  gboolean retval;

  if (cache->volume == NULL)
    return FALSE;

  drive = g_volume_get_drive (cache->volume);
  if (drive == NULL) {
    /* No drive, so not something that can have a medium */
    return TRUE;
  }
  retval = g_drive_has_media (drive);
  g_object_unref (drive);

  return retval;
}

static gboolean
cd_cache_open_device (CdCache *cache,
		      GError **error)
{
  /* not a medium? */
  if (cache->is_media == FALSE || cache->has_medium != FALSE) {
    return TRUE;
  }

  if (cd_cache_has_medium (cache) == FALSE) {
    g_set_error (error, TOTEM_PL_PARSER_ERROR, TOTEM_PL_PARSER_ERROR_NO_DISC,
	_("Please check that a disc is present in the drive."));
    return FALSE;
  }
  cache->has_medium = TRUE;

  return TRUE;
}

static void
cd_cache_mount_callback (GObject *source_object,
			 GAsyncResult *res,
			 CdCacheCallbackData *data)
{
  data->result = g_volume_mount_finish (data->cache->volume, res, &data->error);
  data->called = TRUE;
}

static gboolean
cd_cache_open_mountpoint (CdCache *cache,
			  GError **error)
{
  GMount *mount;
  GFile *root;

  /* already opened? */
  if (cache->mounted || cache->is_media == FALSE || cache->is_iso)
    return TRUE;

  /* check for mounting - assume we'll mount ourselves */
  if (cache->volume == NULL)
    return TRUE;

  mount = g_volume_get_mount (cache->volume);
  cache->self_mounted = (mount == NULL);

  /* mount if we have to */
  if (cache->self_mounted) {
    CdCacheCallbackData data;

    memset (&data, 0, sizeof(data));
    data.cache = cache;

    /* mount - wait for callback */
    g_volume_mount (cache->volume,
		    G_MOUNT_MOUNT_NONE,
		    NULL,
		    NULL,
		    (GAsyncReadyCallback) cd_cache_mount_callback,
		    &data);
    /* FIXME wait until it's done, any better way? */
    while (!data.called) g_main_context_iteration (NULL, TRUE);

    if (!data.result) {
      if (data.error) {
	g_propagate_error (error, data.error);
      } else {
	g_set_error (error, TOTEM_PL_PARSER_ERROR, TOTEM_PL_PARSER_ERROR_MOUNT_FAILED,
		     _("Failed to mount %s."), cache->device);
      }
      return FALSE;
    } else {
      cache->mounted = TRUE;
      mount = g_volume_get_mount (cache->volume);
    }
  }

  if (!cache->mountpoint) {
    root = g_mount_get_root (mount);
    cache->mountpoint = g_file_get_path (root);
    g_object_unref (root);
  }

  return TRUE;
}

static void
cd_cache_unmount_callback (GObject *source_object,
			   GAsyncResult *res,
			   CdCacheCallbackData *data)
{
  data->result = g_mount_unmount_with_operation_finish (G_MOUNT (source_object),
                                                        res, NULL);
  data->called = TRUE;
}

static void
cd_cache_free (CdCache *cache)
{
  GMount *mount;

  g_strfreev (cache->content_types);

  if (cache->iso_file && cache->self_mounted) {
    mount = g_file_find_enclosing_mount (cache->iso_file,
					 NULL, NULL);
    if (mount) {
      CdCacheCallbackData data;

      memset (&data, 0, sizeof(data));

      g_mount_unmount_with_operation (mount,
                                      G_MOUNT_UNMOUNT_NONE,
                                      NULL,
                                      NULL,
                                      (GAsyncReadyCallback) cd_cache_unmount_callback,
                                      &data);

      while (!data.called) g_main_context_iteration (NULL, TRUE);
      g_object_unref (mount);
    }
    g_object_unref (cache->iso_file);
  }

  /* free mem */
  if (cache->volume)
    g_object_unref (cache->volume);
  g_free (cache->mountpoint);
  g_free (cache->device);
  g_free (cache);
}

static TotemDiscMediaType
cd_cache_disc_is_cdda (CdCache *cache,
		       GError **error)
{
  /* We can't have audio CDs on disc, yet */
  if (cache->is_media == FALSE)
    return MEDIA_TYPE_DATA;
  if (!cd_cache_open_device (cache, error))
    return MEDIA_TYPE_ERROR;
  if (cd_cache_has_content_type (cache, "x-content/audio-cdda") != FALSE)
    return MEDIA_TYPE_CDDA;

  return MEDIA_TYPE_DATA;
}

static TotemDiscMediaType
cd_cache_disc_is_vcd (CdCache *cache,
                      GError **error)
{
  /* open disc and open mount */
  if (!cd_cache_open_device (cache, error))
    return MEDIA_TYPE_ERROR;
  if (!cd_cache_open_mountpoint (cache, error))
    return MEDIA_TYPE_ERROR;

  if (cd_cache_has_content_type (cache, "x-content/video-vcd") != FALSE)
    return MEDIA_TYPE_VCD;
  if (cd_cache_has_content_type (cache, "x-content/video-svcd") != FALSE)
    return MEDIA_TYPE_VCD;

  return MEDIA_TYPE_DATA;
}

static TotemDiscMediaType
cd_cache_disc_is_dvd (CdCache *cache,
		      GError **error)
{
  /* open disc, check capabilities and open mount */
  if (!cd_cache_open_device (cache, error))
    return MEDIA_TYPE_ERROR;
  if (!cd_cache_open_mountpoint (cache, error))
    return MEDIA_TYPE_ERROR;

  if (cd_cache_has_content_type (cache, "x-content/video-dvd") != FALSE)
    return MEDIA_TYPE_DVD;

  return MEDIA_TYPE_DATA;
}

static TotemDiscMediaType
cd_cache_disc_is_bd (CdCache *cache,
		     GError **error)
{
  /* open disc, check capabilities and open mount */
  if (!cd_cache_open_device (cache, error))
    return MEDIA_TYPE_ERROR;
  if (!cd_cache_open_mountpoint (cache, error))
    return MEDIA_TYPE_ERROR;

  if (cd_cache_has_content_type (cache, "x-content/video-bluray") != FALSE)
    return MEDIA_TYPE_BD;

  return MEDIA_TYPE_DATA;
}

/**
 * totem_cd_mrl_from_type:
 * @scheme: a scheme (e.g. "dvd")
 * @dir: a directory URI
 *
 * Builds an MRL using the scheme @scheme and the given URI @dir,
 * taking the filename from the URI if it's a <filename>file://</filename> and just
 * using the whole URI otherwise.
 *
 * Return value: a newly-allocated string containing the MRL
 **/
char *
totem_cd_mrl_from_type (const char *scheme, const char *dir)
{
  char *retval;

  if (g_str_has_prefix (dir, "file://") != FALSE) {
    char *local;
    local = g_filename_from_uri (dir, NULL, NULL);
    retval = g_strdup_printf ("%s://%s", scheme, local);
    g_free (local);
  } else {
    retval = g_strdup_printf ("%s://%s", scheme, dir);
  }
  return retval;
}

static char *
totem_cd_dir_get_parent (const char *dir)
{
  GFile *file, *parent_file;
  char *parent;

  file = g_file_new_for_path (dir);
  parent_file = g_file_get_parent (file);
  g_object_unref (file);

  parent = g_file_get_path (parent_file);
  g_object_unref (parent_file);

  return parent;
}

/**
 * totem_cd_detect_type_from_dir:
 * @dir: a directory URI
 * @mrl: (out) (transfer full) (allow-none): return location for the disc's MRL, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * Detects the disc's type, given its mount directory URI. If
 * a string pointer is passed to @mrl, it will return the disc's
 * MRL as from totem_cd_mrl_from_type().
 *
 * Note that this function does synchronous I/O.
 *
 * If no disc is present in the drive, a #TOTEM_PL_PARSER_ERROR_NO_DISC
 * error will be returned. On unknown mounting errors, a
 * #TOTEM_PL_PARSER_ERROR_MOUNT_FAILED error will be returned. On other
 * I/O errors, or if resolution of symlinked mount paths failed, a code from
 * #GIOErrorEnum will be returned.
 *
 * Return value: #TotemDiscMediaType corresponding to the disc's type, or #MEDIA_TYPE_ERROR on failure
 **/
TotemDiscMediaType
totem_cd_detect_type_from_dir (const char *dir, char **mrl, GError **error)
{
  CdCache *cache;
  TotemDiscMediaType type;

  g_return_val_if_fail (dir != NULL, MEDIA_TYPE_ERROR);

  if (!(cache = cd_cache_new (dir, error)))
    return MEDIA_TYPE_ERROR;
  if ((type = cd_cache_disc_is_vcd (cache, error)) == MEDIA_TYPE_DATA &&
      (type = cd_cache_disc_is_dvd (cache, error)) == MEDIA_TYPE_DATA &&
      (type = cd_cache_disc_is_bd (cache, error)) == MEDIA_TYPE_DATA) {
    /* is it the directory itself? */
    char *parent;

    cd_cache_free (cache);
    parent = totem_cd_dir_get_parent (dir);
    if (!parent)
      return type;

    cache = cd_cache_new (parent, error);
    g_free (parent);
    if (!cache)
      return MEDIA_TYPE_ERROR;
    if ((type = cd_cache_disc_is_vcd (cache, error)) == MEDIA_TYPE_DATA &&
	(type = cd_cache_disc_is_dvd (cache, error)) == MEDIA_TYPE_DATA &&
	(type = cd_cache_disc_is_bd (cache, error)) == MEDIA_TYPE_DATA) {
      /* crap, nothing found */
      cd_cache_free (cache);
      return type;
    }
  }

  if (mrl == NULL) {
    cd_cache_free (cache);
    return type;
  }

  if (type == MEDIA_TYPE_DVD) {
    *mrl = totem_cd_mrl_from_type ("dvd", cache->mountpoint ? cache->mountpoint : cache->device);
  } else if (type == MEDIA_TYPE_VCD) {
    *mrl = totem_cd_mrl_from_type ("vcd", cache->mountpoint);
  } else if (type == MEDIA_TYPE_BD) {
    *mrl = totem_cd_mrl_from_type ("bluray", cache->mountpoint);
  }

  cd_cache_free (cache);

  return type;
}

/**
 * totem_cd_detect_type_with_url:
 * @device: a device node path
 * @mrl: (out) (transfer full) (allow-none): return location for the disc's MRL, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * Detects the disc's type, given its device node path. If
 * a string pointer is passed to @mrl, it will return the disc's
 * MRL as from totem_cd_mrl_from_type().
 *
 * Note that this function does synchronous I/O.
 *
 * Possible error codes are as per totem_cd_detect_type_from_dir().
 *
 * Return value: #TotemDiscMediaType corresponding to the disc's type, or #MEDIA_TYPE_ERROR on failure
 **/
TotemDiscMediaType
totem_cd_detect_type_with_url (const char *device,
    			       char      **mrl,
			       GError     **error)
{
  CdCache *cache;
  TotemDiscMediaType type;

  if (mrl != NULL)
    *mrl = NULL;

  if (!(cache = cd_cache_new (device, error)))
    return MEDIA_TYPE_ERROR;

  type = cd_cache_disc_is_cdda (cache, error);
  if (type == MEDIA_TYPE_ERROR && *error != NULL) {
    cd_cache_free (cache);
    return type;
  }

  if ((type == MEDIA_TYPE_DATA || type == MEDIA_TYPE_ERROR) &&
      (type = cd_cache_disc_is_vcd (cache, error)) == MEDIA_TYPE_DATA &&
      (type = cd_cache_disc_is_dvd (cache, error)) == MEDIA_TYPE_DATA &&
      (type = cd_cache_disc_is_bd (cache, error)) == MEDIA_TYPE_DATA) {
    /* crap, nothing found */
  }

  if (mrl == NULL) {
    cd_cache_free (cache);
    return type;
  }

  switch (type) {
  case MEDIA_TYPE_DVD:
    {
      const char *str;

      if (!cache->is_iso)
	str = cache->mountpoint ? cache->mountpoint : device;
      else
	str = cache->device;
      *mrl = totem_cd_mrl_from_type ("dvd", str);
    }
    break;
  case MEDIA_TYPE_VCD:
    {
      const char *str;

      if (!cache->is_iso)
	str = cache->mountpoint ? cache->mountpoint : device;
      else
	str = cache->device;
      *mrl = totem_cd_mrl_from_type ("vcd", str);
    }
    break;
  case MEDIA_TYPE_CDDA:
    {
      const char *dev;

      dev = cache->device ? cache->device : device;
      if (g_str_has_prefix (dev, "/dev/") != FALSE)
	*mrl = totem_cd_mrl_from_type ("cdda", dev + 5);
      else
	*mrl = totem_cd_mrl_from_type ("cdda", dev);
    }
    break;
  case MEDIA_TYPE_BD:
    {
      const char *str;

      if (!cache->is_iso)
	str = cache->mountpoint ? cache->mountpoint : device;
      else
	str = cache->device;
      *mrl = totem_cd_mrl_from_type ("bluray", str);
    }
    break;
  case MEDIA_TYPE_DATA:
    if (cache->is_iso) {
      type = MEDIA_TYPE_ERROR;
      /* No error, it's just not usable */
    } else {
      *mrl = g_filename_to_uri (cache->mountpoint, NULL, NULL);
      if (*mrl == NULL)
	*mrl = g_strdup (cache->mountpoint);
    }
    break;
  case MEDIA_TYPE_ERROR:
  case MEDIA_TYPE_DVB:
  default:
    break;
  }

  cd_cache_free (cache);

  return type;
}

/**
 * totem_cd_detect_type:
 * @device: a device node path
 * @error: return location for a #GError, or %NULL
 *
 * Detects the disc's type, given its device node path.
 *
 * Possible error codes are as per totem_cd_detect_type_with_url().
 *
 * Return value: #TotemDiscMediaType corresponding to the disc's type, or #MEDIA_TYPE_ERROR on failure
 **/
TotemDiscMediaType
totem_cd_detect_type (const char  *device,
		      GError     **error)
{
  return totem_cd_detect_type_with_url (device, NULL, error);
}

/**
 * totem_cd_has_medium:
 * @device: a device node path
 *
 * Returns whether the disc has a physical medium.
 *
 * Return value: %TRUE if the disc physically exists
 **/
gboolean
totem_cd_has_medium (const char *device)
{
  CdCache *cache;
  gboolean retval = TRUE;

  if (!(cache = cd_cache_new (device, NULL)))
    return TRUE;

  retval = cd_cache_has_medium (cache);
  cd_cache_free (cache);

  return retval;
}

/**
 * totem_cd_get_human_readable_name:
 * @type: a #TotemDiscMediaType
 *
 * Returns the human-readable name for the given
 * #TotemDiscMediaType.
 *
 * Return value: the disc media type's readable name, which must not be freed, or %NULL for unhandled media types
 **/
const char *
totem_cd_get_human_readable_name (TotemDiscMediaType type)
{
  switch (type)
  {
  case MEDIA_TYPE_CDDA:
    return N_("Audio CD");
  case MEDIA_TYPE_VCD:
    return N_("Video CD");
  case MEDIA_TYPE_DVD:
    return N_("DVD");
  case MEDIA_TYPE_DVB:
    return N_("Digital Television");
  case MEDIA_TYPE_BD:
    return N_("Blu-ray");
  case MEDIA_TYPE_ERROR:
  case MEDIA_TYPE_DATA:
  default:
    g_assert_not_reached ();
  }

  return NULL;
}

GQuark
totem_disc_media_type_quark (void)
{
  static GQuark quark = 0;
  if (!quark)
    quark = g_quark_from_static_string ("totem_disc_media_type");

  return quark;
}

/*
 * vim: sw=2 ts=8 cindent noai bs=2
 */