~ubuntu-branches/ubuntu/lucid/brasero/lucid-updates

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
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
 * Libbrasero-burn
 * Copyright (C) Philippe Rouquier 2005-2009 <bonfire-app@wanadoo.fr>
 *
 * Libbrasero-burn 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.
 *
 * The Libbrasero-burn authors hereby grant permission for non-GPL compatible
 * GStreamer plugins to be used and distributed together with GStreamer
 * and Libbrasero-burn. This permission is above and beyond the permissions granted
 * by the GPL license by which Libbrasero-burn is covered. If you modify this code
 * you may extend this exception to your version of the code, but you are not
 * obligated to do so. If you do not wish to do so, delete this exception
 * statement from your version.
 * 
 * Libbrasero-burn 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 General Public License
 * along with this program; if not, write to:
 * 	The Free Software Foundation, Inc.,
 * 	51 Franklin Street, Fifth Floor
 * 	Boston, MA  02110-1301, USA.
 */

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

#include <string.h>

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

#include "burn-caps.h"
#include "burn-debug.h"
#include "brasero-plugin.h"
#include "brasero-plugin-private.h"
#include "brasero-plugin-information.h"
#include "brasero-session-helper.h"

#define BRASERO_BURN_CAPS_SHOULD_BLANK(media_MACRO, flags_MACRO)		\
	((media_MACRO & BRASERO_MEDIUM_UNFORMATTED) ||				\
	((media_MACRO & (BRASERO_MEDIUM_HAS_AUDIO|BRASERO_MEDIUM_HAS_DATA)) &&	\
	 (flags_MACRO & (BRASERO_BURN_FLAG_MERGE|BRASERO_BURN_FLAG_APPEND)) == FALSE))

#define BRASERO_BURN_CAPS_NOT_SUPPORTED_LOG_RES(session)			\
{										\
	brasero_burn_session_log (session, "Unsupported type of task operation"); \
	BRASERO_BURN_LOG ("Unsupported type of task operation");		\
	return BRASERO_BURN_NOT_SUPPORTED;					\
}

static BraseroBurnResult
brasero_burn_caps_get_blanking_flags_real (BraseroBurnCaps *caps,
                                           gboolean ignore_errors,
					   BraseroMedia media,
					   BraseroBurnFlag session_flags,
					   BraseroBurnFlag *supported,
					   BraseroBurnFlag *compulsory)
{
	GSList *iter;
	gboolean supported_media;
	BraseroBurnFlag supported_flags = BRASERO_BURN_FLAG_NONE;
	BraseroBurnFlag compulsory_flags = BRASERO_BURN_FLAG_ALL;

	BRASERO_BURN_LOG_DISC_TYPE (media, "Getting blanking flags for");
	if (media == BRASERO_MEDIUM_NONE) {
		BRASERO_BURN_LOG ("Blanking not possible: no media");
		if (supported)
			*supported = BRASERO_BURN_FLAG_NONE;
		if (compulsory)
			*compulsory = BRASERO_BURN_FLAG_NONE;
		return BRASERO_BURN_NOT_SUPPORTED;
	}

	supported_media = FALSE;
	for (iter = caps->priv->caps_list; iter; iter = iter->next) {
		BraseroMedia caps_media;
		BraseroCaps *caps;
		GSList *links;

		caps = iter->data;
		if (!brasero_track_type_get_has_medium (&caps->type))
			continue;

		caps_media = brasero_track_type_get_medium_type (&caps->type);
		if ((media & caps_media) != media)
			continue;

		for (links = caps->links; links; links = links->next) {
			GSList *plugins;
			BraseroCapsLink *link;

			link = links->data;

			if (link->caps != NULL)
				continue;

			supported_media = TRUE;
			/* don't need the plugins to be sorted since we go
			 * through all the plugin list to get all blanking flags
			 * available. */
			for (plugins = link->plugins; plugins; plugins = plugins->next) {
				BraseroPlugin *plugin;
				BraseroBurnFlag supported_plugin;
				BraseroBurnFlag compulsory_plugin;

				plugin = plugins->data;
				if (!brasero_plugin_get_active (plugin, ignore_errors))
					continue;

				if (!brasero_plugin_get_blank_flags (plugin,
								     media,
								     session_flags,
								     &supported_plugin,
								     &compulsory_plugin))
					continue;

				supported_flags |= supported_plugin;
				compulsory_flags &= compulsory_plugin;
			}
		}
	}

	if (!supported_media) {
		BRASERO_BURN_LOG ("media blanking not supported");
		return BRASERO_BURN_NOT_SUPPORTED;
	}

	/* This is a special case that is in MMC specs:
	 * DVD-RW sequential must be fully blanked
	 * if we really want multisession support. */
	if (BRASERO_MEDIUM_IS (media, BRASERO_MEDIUM_DVDRW)
	&& (session_flags & BRASERO_BURN_FLAG_MULTI)) {
		if (compulsory_flags & BRASERO_BURN_FLAG_FAST_BLANK) {
			BRASERO_BURN_LOG ("fast media blanking only supported but multisession required for DVDRW");
			return BRASERO_BURN_NOT_SUPPORTED;
		}

		supported_flags &= ~BRASERO_BURN_FLAG_FAST_BLANK;

		BRASERO_BURN_LOG ("removed fast blank for a DVDRW with multisession");
	}

	if (supported)
		*supported = supported_flags;
	if (compulsory)
		*compulsory = compulsory_flags;

	return BRASERO_BURN_OK;
}

/**
 * brasero_burn_session_get_blank_flags:
 * @session: a #BraseroBurnSession
 * @supported: a #BraseroBurnFlag
 * @compulsory: a #BraseroBurnFlag
 *
 * Given the various parameters stored in @session,
 * stores in @supported and @compulsory, the flags
 * that can be used (@supported) and must be used
 * (@compulsory) when blanking the medium in the
 * #BraseroDrive (set with brasero_burn_session_set_burner ()).
 *
 * Return value: a #BraseroBurnResult.
 * BRASERO_BURN_OK if the retrieval was successful.
 * BRASERO_BURN_ERR otherwise.
 **/

BraseroBurnResult
brasero_burn_session_get_blank_flags (BraseroBurnSession *session,
				      BraseroBurnFlag *supported,
				      BraseroBurnFlag *compulsory)
{
	BraseroMedia media;
	BraseroBurnCaps *self;
	BraseroBurnResult result;
	BraseroBurnFlag session_flags;

	media = brasero_burn_session_get_dest_media (session);
	BRASERO_BURN_LOG_DISC_TYPE (media, "Getting blanking flags for");

	if (media == BRASERO_MEDIUM_NONE) {
		BRASERO_BURN_LOG ("Blanking not possible: no media");
		if (supported)
			*supported = BRASERO_BURN_FLAG_NONE;
		if (compulsory)
			*compulsory = BRASERO_BURN_FLAG_NONE;
		return BRASERO_BURN_NOT_SUPPORTED;
	}

	session_flags = brasero_burn_session_get_flags (session);

	self = brasero_burn_caps_get_default ();
	result = brasero_burn_caps_get_blanking_flags_real (self,
	                                                    brasero_burn_session_get_strict_support (session) == FALSE,
							    media,
							    session_flags,
							    supported,
							    compulsory);
	g_object_unref (self);

	return result;
}

static BraseroBurnResult
brasero_burn_caps_can_blank_real (BraseroBurnCaps *self,
                                  gboolean ignore_plugin_errors,
                                  BraseroMedia media,
				  BraseroBurnFlag flags)
{
	GSList *iter;

	BRASERO_BURN_LOG_DISC_TYPE (media, "Testing blanking caps for");
	if (media == BRASERO_MEDIUM_NONE) {
		BRASERO_BURN_LOG ("no media => no blanking possible");
		return BRASERO_BURN_NOT_SUPPORTED;
	}

	/* This is a special case from MMC: DVD-RW sequential
	 * can only be multisession is they were fully blanked
	 * so if there are the two flags, abort. */
	if (BRASERO_MEDIUM_IS (media, BRASERO_MEDIUM_DVDRW)
	&&  (flags & BRASERO_BURN_FLAG_MULTI)
	&&  (flags & BRASERO_BURN_FLAG_FAST_BLANK)) {
		BRASERO_BURN_LOG ("fast media blanking only supported but multisession required for DVD-RW");
		return BRASERO_BURN_NOT_SUPPORTED;
	}

	for (iter = self->priv->caps_list; iter; iter = iter->next) {
		BraseroCaps *caps;
		GSList *links;

		caps = iter->data;
		if (caps->type.type != BRASERO_TRACK_TYPE_DISC)
			continue;

		if ((media & brasero_track_type_get_medium_type (&caps->type)) != media)
			continue;

		BRASERO_BURN_LOG_TYPE (&caps->type, "Searching links for caps");

		for (links = caps->links; links; links = links->next) {
			GSList *plugins;
			BraseroCapsLink *link;

			link = links->data;

			if (link->caps != NULL)
				continue;

			BRASERO_BURN_LOG ("Searching plugins");

			/* Go through all plugins for the link and stop if we 
			 * find at least one active plugin that accepts the
			 * flags. No need for plugins to be sorted */
			for (plugins = link->plugins; plugins; plugins = plugins->next) {
				BraseroPlugin *plugin;

				plugin = plugins->data;
				if (!brasero_plugin_get_active (plugin, ignore_plugin_errors))
					continue;

				if (brasero_plugin_check_blank_flags (plugin, media, flags)) {
					BRASERO_BURN_LOG_DISC_TYPE (media, "Can blank");
					return BRASERO_BURN_OK;
				}
			}
		}
	}

	BRASERO_BURN_LOG_DISC_TYPE (media, "No blanking capabilities for");

	return BRASERO_BURN_NOT_SUPPORTED;
}

/**
 * brasero_burn_session_can_blank:
 * @session: a #BraseroBurnSession
 *
 * Given the various parameters stored in @session, this
 * function checks whether the medium in the
 * #BraseroDrive (set with brasero_burn_session_set_burner ())
 * can be blanked.
 *
 * Return value: a #BraseroBurnResult.
 * BRASERO_BURN_OK if it is possible.
 * BRASERO_BURN_ERR otherwise.
 **/

BraseroBurnResult
brasero_burn_session_can_blank (BraseroBurnSession *session)
{
	BraseroMedia media;
	BraseroBurnFlag flags;
	BraseroBurnCaps *self;
	BraseroBurnResult result;

	self = brasero_burn_caps_get_default ();

	media = brasero_burn_session_get_dest_media (session);
	BRASERO_BURN_LOG_DISC_TYPE (media, "Testing blanking caps for");

	if (media == BRASERO_MEDIUM_NONE) {
		BRASERO_BURN_LOG ("no media => no blanking possible");
		g_object_unref (self);
		return BRASERO_BURN_NOT_SUPPORTED;
	}

	flags = brasero_burn_session_get_flags (session);
	result = brasero_burn_caps_can_blank_real (self,
	                                           brasero_burn_session_get_strict_support (session) == FALSE,
	                                           media,
	                                           flags);
	g_object_unref (self);

	return result;
}

static void
brasero_caps_link_get_record_flags (BraseroCapsLink *link,
                                    gboolean ignore_plugin_errors,
				    BraseroMedia media,
				    BraseroBurnFlag session_flags,
				    BraseroBurnFlag *supported,
				    BraseroBurnFlag *compulsory_retval)
{
	GSList *iter;
	BraseroBurnFlag compulsory;

	compulsory = BRASERO_BURN_FLAG_ALL;

	/* Go through all plugins to get the supported/... record flags for link */
	for (iter = link->plugins; iter; iter = iter->next) {
		gboolean result;
		BraseroPlugin *plugin;
		BraseroBurnFlag plugin_supported;
		BraseroBurnFlag plugin_compulsory;

		plugin = iter->data;
		if (!brasero_plugin_get_active (plugin, ignore_plugin_errors))
			continue;

		result = brasero_plugin_get_record_flags (plugin,
							  media,
							  session_flags,
							  &plugin_supported,
							  &plugin_compulsory);
		if (!result)
			continue;

		*supported |= plugin_supported;
		compulsory &= plugin_compulsory;
	}

	*compulsory_retval = compulsory;
}

static void
brasero_caps_link_get_data_flags (BraseroCapsLink *link,
                                  gboolean ignore_plugin_errors,
				  BraseroMedia media,
				  BraseroBurnFlag session_flags,
				  BraseroBurnFlag *supported)
{
	GSList *iter;

	/* Go through all plugins the get the supported/... data flags for link */
	for (iter = link->plugins; iter; iter = iter->next) {
		gboolean result;
		BraseroPlugin *plugin;
		BraseroBurnFlag plugin_supported;
		BraseroBurnFlag plugin_compulsory;

		plugin = iter->data;
		if (!brasero_plugin_get_active (plugin, ignore_plugin_errors))
			continue;

		result = brasero_plugin_get_image_flags (plugin,
							 media,
							 session_flags,
							 &plugin_supported,
							 &plugin_compulsory);
		*supported |= plugin_supported;
	}
}

static gboolean
brasero_caps_link_check_data_flags (BraseroCapsLink *link,
                                    gboolean ignore_plugin_errors,
				    BraseroBurnFlag session_flags,
				    BraseroMedia media)
{
	GSList *iter;
	BraseroBurnFlag flags;

	/* here we just make sure that at least one of the plugins in the link
	 * can comply with the flags (APPEND/MERGE) */
	flags = session_flags & (BRASERO_BURN_FLAG_APPEND|BRASERO_BURN_FLAG_MERGE);

	/* If there are no image flags forget it */
	if (flags == BRASERO_BURN_FLAG_NONE)
		return TRUE;

	/* Go through all plugins; at least one must support image flags */
	for (iter = link->plugins; iter; iter = iter->next) {
		gboolean result;
		BraseroPlugin *plugin;

		plugin = iter->data;
		if (!brasero_plugin_get_active (plugin, ignore_plugin_errors))
			continue;

		result = brasero_plugin_check_image_flags (plugin,
							   media,
							   session_flags);
		if (result)
			return TRUE;
	}

	return FALSE;
}

static gboolean
brasero_caps_link_check_record_flags (BraseroCapsLink *link,
                                      gboolean ignore_plugin_errors,
				      BraseroBurnFlag session_flags,
				      BraseroMedia media)
{
	GSList *iter;
	BraseroBurnFlag flags;

	flags = session_flags & BRASERO_PLUGIN_BURN_FLAG_MASK;

	/* If there are no record flags forget it */
	if (flags == BRASERO_BURN_FLAG_NONE)
		return TRUE;
	
	/* Go through all plugins: at least one must support record flags */
	for (iter = link->plugins; iter; iter = iter->next) {
		gboolean result;
		BraseroPlugin *plugin;

		plugin = iter->data;
		if (!brasero_plugin_get_active (plugin, ignore_plugin_errors))
			continue;

		result = brasero_plugin_check_record_flags (plugin,
							    media,
							    session_flags);
		if (result)
			return TRUE;
	}

	return FALSE;
}

static gboolean
brasero_caps_link_check_media_restrictions (BraseroCapsLink *link,
                                            gboolean ignore_plugin_errors,
					    BraseroMedia media)
{
	GSList *iter;

	/* Go through all plugins: at least one must support media */
	for (iter = link->plugins; iter; iter = iter->next) {
		gboolean result;
		BraseroPlugin *plugin;

		plugin = iter->data;
		if (!brasero_plugin_get_active (plugin, ignore_plugin_errors))
			continue;

		result = brasero_plugin_check_media_restrictions (plugin, media);
		if (result)
			return TRUE;
	}

	return FALSE;
}

static BraseroBurnResult
brasero_caps_report_plugin_error (BraseroPlugin *plugin,
                                  BraseroForeachPluginErrorCb callback,
                                  gpointer user_data)
{
	GSList *errors;

	errors = brasero_plugin_get_errors (plugin);
	if (!errors)
		return BRASERO_BURN_ERR;

	do {
		BraseroPluginError *error;
		BraseroBurnResult result;

		error = errors->data;
		result = callback (error->type, error->detail, user_data);
		if (result == BRASERO_BURN_RETRY) {
			/* Something has been done
			 * to fix the error like an install
			 * so reload the errors */
			brasero_plugin_check_plugin_ready (plugin);
			errors = brasero_plugin_get_errors (plugin);
			continue;
		}

		if (result != BRASERO_BURN_OK)
			return result;

		errors = errors->next;
	} while (errors);

	return BRASERO_BURN_OK;
}

struct _BraseroFindLinkCtx {
	BraseroMedia media;
	BraseroTrackType *input;
	BraseroPluginIOFlag io_flags;
	BraseroBurnFlag session_flags;

	BraseroForeachPluginErrorCb callback;
	gpointer user_data;

	guint ignore_plugin_errors:1;
	guint check_session_flags:1;
};
typedef struct _BraseroFindLinkCtx BraseroFindLinkCtx;

static void
brasero_caps_find_link_set_ctx (BraseroBurnSession *session,
                                BraseroFindLinkCtx *ctx,
                                BraseroTrackType *input)
{
	ctx->input = input;

	if (ctx->check_session_flags) {
		ctx->session_flags = brasero_burn_session_get_flags (session);

		if (BRASERO_BURN_SESSION_NO_TMP_FILE (session))
			ctx->io_flags = BRASERO_PLUGIN_IO_ACCEPT_PIPE;
		else
			ctx->io_flags = BRASERO_PLUGIN_IO_ACCEPT_FILE;
	}
	else
		ctx->io_flags = BRASERO_PLUGIN_IO_ACCEPT_FILE|
					BRASERO_PLUGIN_IO_ACCEPT_PIPE;

	if (!ctx->callback)
		ctx->ignore_plugin_errors = (brasero_burn_session_get_strict_support (session) == FALSE);
	else
		ctx->ignore_plugin_errors = TRUE;
}

static BraseroBurnResult
brasero_caps_find_link (BraseroCaps *caps,
                        BraseroFindLinkCtx *ctx)
{
	GSList *iter;

	BRASERO_BURN_LOG_WITH_TYPE (&caps->type, BRASERO_PLUGIN_IO_NONE, "Found link (with %i links):", g_slist_length (caps->links));

	/* Here we only make sure we have at least one link working. For a link
	 * to be followed it must first:
	 * - link to a caps with correct io flags
	 * - have at least a plugin accepting the record flags if caps type is
	 *   a disc (that means that the link is the recording part)
	 *
	 * and either:
	 * - link to a caps equal to the input
	 * - link to a caps (linking itself to another caps, ...) accepting the
	 *   input
	 */

	for (iter = caps->links; iter; iter = iter->next) {
		BraseroCapsLink *link;
		BraseroBurnResult result;

		link = iter->data;

		if (!link->caps)
			continue;
		
		/* check that the link has some active plugin */
		if (!brasero_caps_link_active (link, ctx->ignore_plugin_errors))
			continue;

		/* since this link contains recorders, check that at least one
		 * of them can handle the record flags */
		if (ctx->check_session_flags && brasero_track_type_get_has_medium (&caps->type)) {
			if (!brasero_caps_link_check_record_flags (link, ctx->ignore_plugin_errors, ctx->session_flags, ctx->media))
				continue;

			if (brasero_caps_link_check_recorder_flags_for_input (link, ctx->session_flags) != BRASERO_BURN_OK)
				continue;
		}

		/* first see if that's the perfect fit:
		 * - it must have the same caps (type + subtype)
		 * - it must have the proper IO */
		if (brasero_track_type_get_has_data (&link->caps->type)) {
			if (ctx->check_session_flags
			&& !brasero_caps_link_check_data_flags (link, ctx->ignore_plugin_errors, ctx->session_flags, ctx->media))
				continue;
		}
		else if (!brasero_caps_link_check_media_restrictions (link, ctx->ignore_plugin_errors, ctx->media))
			continue;

		if ((link->caps->flags & BRASERO_PLUGIN_IO_ACCEPT_FILE)
		&&   brasero_caps_is_compatible_type (link->caps, ctx->input)) {
			if (ctx->callback) {
				BraseroPlugin *plugin;

				/* If we are supposed to report/signal that the plugin
				 * could be used but only if some more elements are 
				 * installed */
				plugin = brasero_caps_link_need_download (link);
				if (plugin)
					return brasero_caps_report_plugin_error (plugin, ctx->callback, ctx->user_data);
			}
			return BRASERO_BURN_OK;
		}

		/* we can't go further than a DISC type */
		if (brasero_track_type_get_has_medium (&link->caps->type))
			continue;

		if ((link->caps->flags & ctx->io_flags) == BRASERO_PLUGIN_IO_NONE)
			continue;

		/* try to see where the inputs of this caps leads to */
		result = brasero_caps_find_link (link->caps, ctx);
		if (result == BRASERO_BURN_CANCEL)
			return result;

		if (result == BRASERO_BURN_OK) {
			if (ctx->callback) {
				BraseroPlugin *plugin;

				/* If we are supposed to report/signal that the plugin
				 * could be used but only if some more elements are 
				 * installed */
				plugin = brasero_caps_link_need_download (link);
				if (plugin)
					return brasero_caps_report_plugin_error (plugin, ctx->callback, ctx->user_data);
			}
			return BRASERO_BURN_OK;
		}
	}

	return BRASERO_BURN_NOT_SUPPORTED;
}

static BraseroBurnResult
brasero_caps_try_output (BraseroBurnCaps *self,
                         BraseroFindLinkCtx *ctx,
                         BraseroTrackType *output)
{
	BraseroCaps *caps;

	/* here we search the start caps */
	caps = brasero_burn_caps_find_start_caps (self, output);
	if (!caps) {
		BRASERO_BURN_LOG ("No caps available");
		return BRASERO_BURN_NOT_SUPPORTED;
	}

	/* Here flags don't matter as we don't record anything.
	 * Even the IOFlags since that can be checked later with
	 * brasero_burn_caps_get_flags. */
	if (brasero_track_type_get_has_medium (output))
		ctx->media = brasero_track_type_get_medium_type (output);
	else
		ctx->media = BRASERO_MEDIUM_FILE;

	return brasero_caps_find_link (caps, ctx);
}

static BraseroBurnResult
brasero_caps_try_output_with_blanking (BraseroBurnCaps *self,
                                       BraseroBurnSession *session,
                                       BraseroFindLinkCtx *ctx,
                                       BraseroTrackType *output)
{
	gboolean result;
	BraseroCaps *last_caps;

	result = brasero_caps_try_output (self, ctx, output);
	if (result == BRASERO_BURN_OK
	||  result == BRASERO_BURN_CANCEL)
		return result;

	/* we reached this point in two cases:
	 * - if the disc cannot be handled
	 * - if some flags are not handled
	 * It is helpful only if:
	 * - the disc was closed and no plugin can handle this type of 
	 * disc once closed (CD-R(W))
	 * - there was the flag BLANK_BEFORE_WRITE set and no plugin can
	 * handle this flag (means that the plugin should erase and
	 * then write on its own. Basically that works only with
	 * overwrite formatted discs, DVD+RW, ...) */
	if (!brasero_track_type_get_has_medium (output))
		return BRASERO_BURN_NOT_SUPPORTED;

	/* output is a disc try with initial blanking */
	BRASERO_BURN_LOG ("Support for input/output failed.");

	/* apparently nothing can be done to reach our goal. Maybe that
	 * is because we first have to blank the disc. If so add a blank 
	 * task to the others as a first step */
	if ((ctx->check_session_flags
	&& !(ctx->session_flags & BRASERO_BURN_FLAG_BLANK_BEFORE_WRITE))
	||   brasero_burn_session_can_blank (session) != BRASERO_BURN_OK)
		return BRASERO_BURN_NOT_SUPPORTED;

	BRASERO_BURN_LOG ("Trying with initial blanking");

	/* retry with the same disc type but blank this time */
	ctx->media = brasero_track_type_get_medium_type (output);
	ctx->media &= ~(BRASERO_MEDIUM_CLOSED|
	                BRASERO_MEDIUM_APPENDABLE|
	                BRASERO_MEDIUM_UNFORMATTED|
	                BRASERO_MEDIUM_HAS_DATA|
	                BRASERO_MEDIUM_HAS_AUDIO);
	ctx->media |= BRASERO_MEDIUM_BLANK;
	brasero_track_type_set_medium_type (output, ctx->media);

	last_caps = brasero_burn_caps_find_start_caps (self, output);
	if (!last_caps)
		return BRASERO_BURN_NOT_SUPPORTED;

	return brasero_caps_find_link (last_caps, ctx);
}

/**
 * brasero_burn_session_input_supported:
 * @session: a #BraseroBurnSession
 * @input: a #BraseroTrackType
 * @check_flags: a #gboolean
 *
 * Given the various parameters stored in @session, this
 * function checks whether a session with the data type
 * @type could be burnt to the medium in the #BraseroDrive (set 
 * through brasero_burn_session_set_burner ()).
 * If @check_flags is %TRUE, then flags are taken into account
 * and are not if it is %FALSE.
 *
 * Return value: a #BraseroBurnResult.
 * BRASERO_BURN_OK if it is possible.
 * BRASERO_BURN_ERR otherwise.
 **/

BraseroBurnResult
brasero_burn_session_input_supported (BraseroBurnSession *session,
				      BraseroTrackType *input,
                                      gboolean check_flags)
{
	BraseroBurnCaps *self;
	BraseroBurnResult result;
	BraseroTrackType output;
	BraseroFindLinkCtx ctx = { 0, NULL, 0, };

	result = brasero_burn_session_get_output_type (session, &output);
	if (result != BRASERO_BURN_OK)
		return result;

	BRASERO_BURN_LOG_TYPE (input, "Checking support for input");
	BRASERO_BURN_LOG_TYPE (&output, "and output");

	ctx.check_session_flags = check_flags;
	brasero_caps_find_link_set_ctx (session, &ctx, input);

	if (check_flags) {
		result = brasero_check_flags_for_drive (brasero_burn_session_get_burner (session),
							ctx.session_flags);

		if (!result)
			BRASERO_BURN_CAPS_NOT_SUPPORTED_LOG_RES (session);

		BRASERO_BURN_LOG_FLAGS (ctx.session_flags, "with flags");
	}

	self = brasero_burn_caps_get_default ();
	result = brasero_caps_try_output_with_blanking (self,
							session,
	                                                &ctx,
							&output);
	g_object_unref (self);

	if (result != BRASERO_BURN_OK) {
		BRASERO_BURN_LOG_TYPE (input, "Input not supported");
		return result;
	}

	return BRASERO_BURN_OK;
}

/**
 * brasero_burn_session_output_supported:
 * @session: a #BraseroBurnSession *
 * @output: a #BraseroTrackType *
 *
 * Make sure that the image type or medium type defined in @output can be
 * created/burnt given the parameters and the current data set in @session.
 *
 * Return value: BRASERO_BURN_OK if the medium type or the image type can be used as an output.
 **/
BraseroBurnResult
brasero_burn_session_output_supported (BraseroBurnSession *session,
				       BraseroTrackType *output)
{
	BraseroBurnCaps *self;
	BraseroTrackType input;
	BraseroBurnResult result;
	BraseroFindLinkCtx ctx = { 0, NULL, 0, };

	/* Here, we can't check if the drive supports the flags since the output
	 * is hypothetical. There is no real medium. So forget the following :
	 * if (!brasero_burn_caps_flags_check_for_drive (session))
	 *	BRASERO_BURN_CAPS_NOT_SUPPORTED_LOG_RES (session);
	 * The only thing we could do would be to check some known forbidden 
	 * flags for some media provided the output type is DISC. */

	brasero_burn_session_get_input_type (session, &input);
	brasero_caps_find_link_set_ctx (session, &ctx, &input);

	BRASERO_BURN_LOG_TYPE (output, "Checking support for output");
	BRASERO_BURN_LOG_TYPE (&input, "and input");
	BRASERO_BURN_LOG_FLAGS (brasero_burn_session_get_flags (session), "with flags");
	
	self = brasero_burn_caps_get_default ();
	result = brasero_caps_try_output_with_blanking (self,
							session,
	                                                &ctx,
							output);
	g_object_unref (self);

	if (result != BRASERO_BURN_OK) {
		BRASERO_BURN_LOG_TYPE (output, "Output not supported");
		return result;
	}

	return BRASERO_BURN_OK;
}

/**
 * This is only to be used in case one wants to copy using the same drive.
 * It determines the possible middle image type.
 */

static BraseroBurnResult
brasero_burn_caps_is_session_supported_same_src_dest (BraseroBurnCaps *self,
						      BraseroBurnSession *session,
                                                      BraseroFindLinkCtx *ctx,
                                                      BraseroTrackType *tmp_type)
{
	GSList *iter;
	BraseroDrive *burner;
	BraseroTrackType input;
	BraseroBurnResult result;
	BraseroTrackType output;
	BraseroImageFormat format;

	BRASERO_BURN_LOG ("Checking disc copy support with same source and destination");

	/* To determine if a CD/DVD can be copied using the same source/dest,
	 * we first determine if can be imaged and then if this image can be 
	 * burnt to whatever medium type. */
	brasero_caps_find_link_set_ctx (session, ctx, &input);
	ctx->io_flags = BRASERO_PLUGIN_IO_ACCEPT_FILE;

	memset (&input, 0, sizeof (BraseroTrackType));
	brasero_burn_session_get_input_type (session, &input);
	BRASERO_BURN_LOG_TYPE (&input, "input");

	if (ctx->check_session_flags) {
		/* NOTE: DAO can be a problem. So just in case remove it. It is
		 * not really useful in this context. What we want here is to
		 * know whether a medium can be used given the input; only 1
		 * flag is important here (MERGE) and can have consequences. */
		ctx->session_flags &= ~BRASERO_BURN_FLAG_DAO;
		BRASERO_BURN_LOG_FLAGS (ctx->session_flags, "flags");
	}

	burner = brasero_burn_session_get_burner (session);

	/* First see if it works with a stream type */
	brasero_track_type_set_has_stream (&output);

	/* FIXME! */
	brasero_track_type_set_stream_format (&output,
	                                      BRASERO_AUDIO_FORMAT_RAW|
	                                      BRASERO_METADATA_INFO);

	BRASERO_BURN_LOG_TYPE (&output, "Testing stream type");
	result = brasero_caps_try_output (self, ctx, &output);
	if (result == BRASERO_BURN_CANCEL)
		return result;

	if (result == BRASERO_BURN_OK) {
		BRASERO_BURN_LOG ("Stream type seems to be supported as output");

		/* This format can be used to create an image. Check if can be
		 * burnt now. Just find at least one medium. */
		for (iter = self->priv->caps_list; iter; iter = iter->next) {
			BraseroBurnResult result;
			BraseroMedia media;
			BraseroCaps *caps;

			caps = iter->data;

			if (!brasero_track_type_get_has_medium (&caps->type))
				continue;

			media = brasero_track_type_get_medium_type (&caps->type);
			/* Audio is only supported by CDs */
			if ((media & BRASERO_MEDIUM_CD) == 0)
				continue;

			/* This type of disc cannot be burnt; skip them */
			if (media & BRASERO_MEDIUM_ROM)
				continue;

			/* Make sure this is supported by the drive */
			if (!brasero_drive_can_write_media (burner, media))
				continue;

			ctx->media = media;
			result = brasero_caps_find_link (caps, ctx);
			BRASERO_BURN_LOG_DISC_TYPE (media,
						    "Tested medium (%s)",
						    result == BRASERO_BURN_OK ? "working":"not working");

			if (result == BRASERO_BURN_OK) {
				if (tmp_type) {
					brasero_track_type_set_has_stream (tmp_type);
					brasero_track_type_set_stream_format (tmp_type, brasero_track_type_get_stream_format (&output));
				}
					
				return BRASERO_BURN_OK;
			}

			if (result == BRASERO_BURN_CANCEL)
				return result;
		}
	}
	else
		BRASERO_BURN_LOG ("Stream format not supported as output");

	/* Find one available output format */
	format = BRASERO_IMAGE_FORMAT_CDRDAO;
	brasero_track_type_set_has_image (&output);

	for (; format > BRASERO_IMAGE_FORMAT_NONE; format >>= 1) {
		brasero_track_type_set_image_format (&output, format);

		BRASERO_BURN_LOG_TYPE (&output, "Testing temporary image format");

		/* Don't need to try blanking here (saves
		 * a few lines of debug) since that is an 
		 * image */
		result = brasero_caps_try_output (self, ctx, &output);
		if (result == BRASERO_BURN_CANCEL)
			return result;

		if (result != BRASERO_BURN_OK)
			continue;

		/* This format can be used to create an image. Check if can be
		 * burnt now. Just find at least one medium. */
		for (iter = self->priv->caps_list; iter; iter = iter->next) {
			BraseroBurnResult result;
			BraseroMedia media;
			BraseroCaps *caps;

			caps = iter->data;

			if (!brasero_track_type_get_has_medium (&caps->type))
				continue;

			media = brasero_track_type_get_medium_type (&caps->type);

			/* This type of disc cannot be burnt; skip them */
			if (media & BRASERO_MEDIUM_ROM)
				continue;

			/* These three types only work with CDs. Skip the rest. */
			if ((format == BRASERO_IMAGE_FORMAT_CDRDAO
			||   format == BRASERO_IMAGE_FORMAT_CLONE
			||   format == BRASERO_IMAGE_FORMAT_CUE)
			&& (media & BRASERO_MEDIUM_CD) == 0)
				continue;

			/* Make sure this is supported by the drive */
			if (!brasero_drive_can_write_media (burner, media))
				continue;

			ctx->media = media;
			result = brasero_caps_find_link (caps, ctx);
			BRASERO_BURN_LOG_DISC_TYPE (media,
						    "Tested medium (%s)",
						    result == BRASERO_BURN_OK ? "working":"not working");

			if (result == BRASERO_BURN_OK) {
				if (tmp_type) {
					brasero_track_type_set_has_image (tmp_type);
					brasero_track_type_set_image_format (tmp_type, brasero_track_type_get_image_format (&output));
				}
					
				return BRASERO_BURN_OK;
			}

			if (result == BRASERO_BURN_CANCEL)
				return result;
		}
	}

	return BRASERO_BURN_NOT_SUPPORTED;
}

BraseroBurnResult
brasero_burn_session_get_tmp_image_type_same_src_dest (BraseroBurnSession *session,
                                                       BraseroTrackType *image_type)
{
	BraseroBurnCaps *self;
	BraseroBurnResult result;
	BraseroFindLinkCtx ctx = { 0, NULL, 0, };

	g_return_val_if_fail (BRASERO_IS_BURN_SESSION (session), BRASERO_BURN_ERR);

	self = brasero_burn_caps_get_default ();
	result = brasero_burn_caps_is_session_supported_same_src_dest (self,
	                                                               session,
	                                                               &ctx,
	                                                               image_type);
	g_object_unref (self);
	return result;
}

static BraseroBurnResult
brasero_burn_session_supported (BraseroBurnSession *session,
                                BraseroFindLinkCtx *ctx)
{
	gboolean result;
	BraseroBurnCaps *self;
	BraseroTrackType input;
	BraseroTrackType output;

	/* Special case */
	if (brasero_burn_session_same_src_dest_drive (session)) {
		BraseroBurnResult res;

		self = brasero_burn_caps_get_default ();
		res = brasero_burn_caps_is_session_supported_same_src_dest (self, session, ctx, NULL);
		g_object_unref (self);

		return res;
	}

	result = brasero_burn_session_get_output_type (session, &output);
	if (result != BRASERO_BURN_OK)
		BRASERO_BURN_CAPS_NOT_SUPPORTED_LOG_RES (session);

	brasero_burn_session_get_input_type (session, &input);
	brasero_caps_find_link_set_ctx (session, ctx, &input);

	BRASERO_BURN_LOG_TYPE (&output, "Checking support for session. Ouput is ");
	BRASERO_BURN_LOG_TYPE (&input, "and input is");

	if (ctx->check_session_flags) {
		result = brasero_check_flags_for_drive (brasero_burn_session_get_burner (session), ctx->session_flags);
		if (!result)
			BRASERO_BURN_CAPS_NOT_SUPPORTED_LOG_RES (session);

		BRASERO_BURN_LOG_FLAGS (ctx->session_flags, "with flags");
	}

	self = brasero_burn_caps_get_default ();
	result = brasero_caps_try_output_with_blanking (self,
							session,
	                                                ctx,
							&output);
	g_object_unref (self);

	if (result != BRASERO_BURN_OK) {
		BRASERO_BURN_LOG_TYPE (&output, "Session not supported");
		return result;
	}

	BRASERO_BURN_LOG_TYPE (&output, "Session supported");
	return BRASERO_BURN_OK;
}

/**
 * brasero_burn_session_can_burn:
 * @session: a #BraseroBurnSession
 * @check_flags: a #gboolean
 *
 * Given the various parameters stored in @session, this
 * function checks whether the data contained in @session
 * can be burnt to the medium in the #BraseroDrive (set 
 * through brasero_burn_session_set_burner ()).
 * If @check_flags determine the behavior of this function.
 *
 * Return value: a #BraseroBurnResult.
 * BRASERO_BURN_OK if it is possible.
 * BRASERO_BURN_ERR otherwise.
 **/

BraseroBurnResult
brasero_burn_session_can_burn (BraseroBurnSession *session,
			       gboolean check_flags)
{
	BraseroFindLinkCtx ctx = { 0, NULL, 0, };

	g_return_val_if_fail (BRASERO_IS_BURN_SESSION (session), BRASERO_BURN_ERR);

	ctx.check_session_flags = check_flags;

	return brasero_burn_session_supported (session, &ctx);
}

/**
 * brasero_session_foreach_plugin_error:
 * @session: a #BraseroBurnSession.
 * @callback: a #BraseroSessionPluginErrorCb.
 * @user_data: a #gpointer. The data passed to @callback.
 *
 * Call @callback for each error in plugins.
 *
 * Return value: a #BraseroBurnResult.
 * BRASERO_BURN_OK if it is possible.
 * BRASERO_BURN_ERR otherwise.
 **/

BraseroBurnResult
brasero_session_foreach_plugin_error (BraseroBurnSession *session,
                                      BraseroForeachPluginErrorCb callback,
                                      gpointer user_data)
{
	BraseroFindLinkCtx ctx = { 0, NULL, 0, };

	g_return_val_if_fail (BRASERO_IS_BURN_SESSION (session), BRASERO_BURN_ERR);

	ctx.callback = callback;
	ctx.user_data = user_data;
	
	return brasero_burn_session_supported (session, &ctx);
}

/**
 * brasero_burn_session_get_required_media_type:
 * @session: a #BraseroBurnSession
 *
 * Return the medium types that could be used to burn
 * @session.
 *
 * Return value: a #BraseroMedia
 **/

BraseroMedia
brasero_burn_session_get_required_media_type (BraseroBurnSession *session)
{
	BraseroMedia required_media = BRASERO_MEDIUM_NONE;
	BraseroFindLinkCtx ctx = { 0, NULL, 0, };
	BraseroTrackType input;
	BraseroBurnCaps *self;
	GSList *iter;

	if (brasero_burn_session_is_dest_file (session))
		return BRASERO_MEDIUM_FILE;

	/* we try to determine here what type of medium is allowed to be burnt
	 * to whether a CD or a DVD. Appendable, blank are not properties being
	 * determined here. We just want it to be writable in a broad sense. */
	ctx.check_session_flags = TRUE;
	brasero_burn_session_get_input_type (session, &input);
	brasero_caps_find_link_set_ctx (session, &ctx, &input);
	BRASERO_BURN_LOG_TYPE (&input, "Determining required media type for input");

	/* NOTE: BRASERO_BURN_FLAG_BLANK_BEFORE_WRITE is a problem here since it
	 * is only used if needed. Likewise DAO can be a problem. So just in
	 * case remove them. They are not really useful in this context. What we
	 * want here is to know which media can be used given the input; only 1
	 * flag is important here (MERGE) and can have consequences. */
	ctx.session_flags &= ~BRASERO_BURN_FLAG_DAO;
	BRASERO_BURN_LOG_FLAGS (ctx.session_flags, "and flags");

	self = brasero_burn_caps_get_default ();
	for (iter = self->priv->caps_list; iter; iter = iter->next) {
		BraseroCaps *caps;
		gboolean result;

		caps = iter->data;

		if (!brasero_track_type_get_has_medium (&caps->type))
			continue;

		/* Put BRASERO_MEDIUM_NONE so we can always succeed */
		result = brasero_caps_find_link (caps, &ctx);
		BRASERO_BURN_LOG_DISC_TYPE (caps->type.subtype.media,
					    "Tested (%s)",
					    result == BRASERO_BURN_OK ? "working":"not working");

		if (result == BRASERO_BURN_CANCEL) {
			g_object_unref (self);
			return result;
		}

		if (result != BRASERO_BURN_OK)
			continue;

		/* This caps work, add its subtype */
		required_media |= brasero_track_type_get_medium_type (&caps->type);
	}

	/* filter as we are only interested in these */
	required_media &= BRASERO_MEDIUM_WRITABLE|
			  BRASERO_MEDIUM_CD|
			  BRASERO_MEDIUM_DVD;

	g_object_unref (self);
	return required_media;
}

/**
 * brasero_burn_session_get_possible_output_formats:
 * @session: a #BraseroBurnSession
 * @formats: a #BraseroImageFormat
 *
 * Returns the disc image types that could be set to create
 * an image given the current state of @session.
 *
 * Return value: a #guint. The number of formats available.
 **/

guint
brasero_burn_session_get_possible_output_formats (BraseroBurnSession *session,
						  BraseroImageFormat *formats)
{
	guint num = 0;
	BraseroImageFormat format;
	BraseroTrackType *output = NULL;

	g_return_val_if_fail (BRASERO_IS_BURN_SESSION (session), 0);

	/* see how many output format are available */
	format = BRASERO_IMAGE_FORMAT_CDRDAO;
	(*formats) = BRASERO_IMAGE_FORMAT_NONE;

	output = brasero_track_type_new ();
	brasero_track_type_set_has_image (output);

	for (; format > BRASERO_IMAGE_FORMAT_NONE; format >>= 1) {
		BraseroBurnResult result;

		brasero_track_type_set_image_format (output, format);
		result = brasero_burn_session_output_supported (session, output);
		if (result == BRASERO_BURN_OK) {
			(*formats) |= format;
			num ++;
		}
	}

	brasero_track_type_free (output);

	return num;
}

/**
 * brasero_burn_session_get_default_output_format:
 * @session: a #BraseroBurnSession
 *
 * Returns the default disc image type that should be set to create
 * an image given the current state of @session.
 *
 * Return value: a #BraseroImageFormat
 **/

BraseroImageFormat
brasero_burn_session_get_default_output_format (BraseroBurnSession *session)
{
	BraseroBurnCaps *self;
	BraseroBurnResult result;
	BraseroTrackType source = { BRASERO_TRACK_TYPE_NONE, { 0, }};
	BraseroTrackType output = { BRASERO_TRACK_TYPE_NONE, { 0, }};

	self = brasero_burn_caps_get_default ();

	if (!brasero_burn_session_is_dest_file (session)) {
		g_object_unref (self);
		return BRASERO_IMAGE_FORMAT_NONE;
	}

	brasero_burn_session_get_input_type (session, &source);
	if (brasero_track_type_is_empty (&source)) {
		g_object_unref (self);
		return BRASERO_IMAGE_FORMAT_NONE;
	}

	if (brasero_track_type_get_has_image (&source)) {
		g_object_unref (self);
		return brasero_track_type_get_image_format (&source);
	}

	brasero_track_type_set_has_image (&output);
	brasero_track_type_set_image_format (&output, BRASERO_IMAGE_FORMAT_NONE);

	if (brasero_track_type_get_has_stream (&source)) {
		/* Otherwise try all possible image types */
		output.subtype.img_format = BRASERO_IMAGE_FORMAT_CDRDAO;
		for (; output.subtype.img_format != BRASERO_IMAGE_FORMAT_NONE;
		       output.subtype.img_format >>= 1) {
		
			result = brasero_burn_session_output_supported (session,
									&output);
			if (result == BRASERO_BURN_OK) {
				g_object_unref (self);
				return brasero_track_type_get_image_format (&output);
			}
		}

		g_object_unref (self);
		return BRASERO_IMAGE_FORMAT_NONE;
	}

	if (brasero_track_type_get_has_data (&source)
	|| (brasero_track_type_get_has_medium (&source)
	&& (brasero_track_type_get_medium_type (&source) & BRASERO_MEDIUM_DVD))) {
		brasero_track_type_set_image_format (&output, BRASERO_IMAGE_FORMAT_BIN);
		result = brasero_burn_session_output_supported (session, &output);
		g_object_unref (self);

		if (result != BRASERO_BURN_OK)
			return BRASERO_IMAGE_FORMAT_NONE;

		return BRASERO_IMAGE_FORMAT_BIN;
	}

	/* for the input which are CDs there are lots of possible formats */
	output.subtype.img_format = BRASERO_IMAGE_FORMAT_CDRDAO;
	for (; output.subtype.img_format != BRASERO_IMAGE_FORMAT_NONE;
	       output.subtype.img_format >>= 1) {
	
		result = brasero_burn_session_output_supported (session, &output);
		if (result == BRASERO_BURN_OK) {
			g_object_unref (self);
			return brasero_track_type_get_image_format (&output);
		}
	}

	g_object_unref (self);
	return BRASERO_IMAGE_FORMAT_NONE;
}

static BraseroBurnResult
brasero_caps_set_flags_from_recorder_input (BraseroTrackType *input,
                                            BraseroBurnFlag *supported,
                                            BraseroBurnFlag *compulsory)
{
	if (brasero_track_type_get_has_image (input)) {
		BraseroImageFormat format;

		format = brasero_track_type_get_image_format (input);
		if (format == BRASERO_IMAGE_FORMAT_CUE
		||  format == BRASERO_IMAGE_FORMAT_CDRDAO) {
			if ((*supported) & BRASERO_BURN_FLAG_DAO)
				(*compulsory) |= BRASERO_BURN_FLAG_DAO;
			else
				return BRASERO_BURN_NOT_SUPPORTED;
		}
		else if (format == BRASERO_IMAGE_FORMAT_CLONE) {
			/* RAW write mode should (must) only be used in this case */
			if ((*supported) & BRASERO_BURN_FLAG_RAW) {
				(*supported) &= ~BRASERO_BURN_FLAG_DAO;
				(*compulsory) &= ~BRASERO_BURN_FLAG_DAO;
				(*compulsory) |= BRASERO_BURN_FLAG_RAW;
			}
			else
				return BRASERO_BURN_NOT_SUPPORTED;
		}
		else
			(*supported) &= ~BRASERO_BURN_FLAG_RAW;
	}
	
	return BRASERO_BURN_OK;
}

static BraseroPluginIOFlag
brasero_caps_get_flags (BraseroCaps *caps,
                        gboolean ignore_plugin_errors,
			BraseroBurnFlag session_flags,
			BraseroMedia media,
			BraseroTrackType *input,
			BraseroPluginIOFlag flags,
			BraseroBurnFlag *supported,
			BraseroBurnFlag *compulsory)
{
	GSList *iter;
	BraseroPluginIOFlag retval = BRASERO_PLUGIN_IO_NONE;

	/* First we must know if this link leads somewhere. It must 
	 * accept the already existing flags. If it does, see if it 
	 * accepts the input and if not, if one of its ancestors does */
	for (iter = caps->links; iter; iter = iter->next) {
		BraseroBurnFlag data_supported = BRASERO_BURN_FLAG_NONE;
		BraseroBurnFlag rec_compulsory = BRASERO_BURN_FLAG_ALL;
		BraseroBurnFlag rec_supported = BRASERO_BURN_FLAG_NONE;
		BraseroPluginIOFlag io_flags;
		BraseroCapsLink *link;

		link = iter->data;

		if (!link->caps)
			continue;

		/* check that the link has some active plugin */
		if (!brasero_caps_link_active (link, ignore_plugin_errors))
			continue;

		if (brasero_track_type_get_has_medium (&caps->type)) {
			BraseroBurnFlag tmp;
			BraseroBurnResult result;

			brasero_caps_link_get_record_flags (link,
			                                    ignore_plugin_errors,
							    media,
							    session_flags,
							    &rec_supported,
							    &rec_compulsory);

			/* see if that link can handle the record flags.
			 * NOTE: compulsory are not a failure in this case. */
			tmp = session_flags & BRASERO_PLUGIN_BURN_FLAG_MASK;
			if ((tmp & rec_supported) != tmp)
				continue;

			/* This is the recording plugin, check its input as
			 * some flags depend on it. */
			result = brasero_caps_set_flags_from_recorder_input (&link->caps->type,
			                                                     &rec_supported,
			                                                     &rec_compulsory);
			if (result != BRASERO_BURN_OK)
				continue;
		}

		if (brasero_track_type_get_has_data (&link->caps->type)) {
			BraseroBurnFlag tmp;

			brasero_caps_link_get_data_flags (link,
			                                  ignore_plugin_errors,
							  media,
							  session_flags,
						    	  &data_supported);

			/* see if that link can handle the data flags. 
			 * NOTE: compulsory are not a failure in this case. */
			tmp = session_flags & (BRASERO_BURN_FLAG_APPEND|
					       BRASERO_BURN_FLAG_MERGE);

			if ((tmp & data_supported) != tmp)
				continue;
		}
		else if (!brasero_caps_link_check_media_restrictions (link, ignore_plugin_errors, media))
			continue;

		/* see if that's the perfect fit */
		if ((link->caps->flags & BRASERO_PLUGIN_IO_ACCEPT_FILE)
		&&   brasero_caps_is_compatible_type (link->caps, input)) {
			/* special case for input that handle output/input */
			if (caps->type.type == BRASERO_TRACK_TYPE_DISC)
				retval |= BRASERO_PLUGIN_IO_ACCEPT_PIPE;
			else
				retval |= caps->flags;

			(*compulsory) &= rec_compulsory;
			(*supported) |= data_supported|rec_supported;
			continue;
		}

		if ((link->caps->flags & flags) == BRASERO_PLUGIN_IO_NONE)
			continue;

		/* we can't go further than a DISC type */
		if (link->caps->type.type == BRASERO_TRACK_TYPE_DISC)
			continue;

		/* try to see where the inputs of this caps leads to */
		io_flags = brasero_caps_get_flags (link->caps,
		                                   ignore_plugin_errors,
						   session_flags,
						   media,
						   input,
						   flags,
						   supported,
						   compulsory);
		if (io_flags == BRASERO_PLUGIN_IO_NONE)
			continue;

		(*compulsory) &= rec_compulsory;
		retval |= (io_flags & flags);
		(*supported) |= data_supported|rec_supported;
	}

	return retval;
}

static void
brasero_medium_supported_flags (BraseroMedium *medium,
				BraseroBurnFlag *supported_flags,
                                BraseroBurnFlag *compulsory_flags)
{
	BraseroMedia media;

	media = brasero_medium_get_status (medium);

	/* This is always FALSE */
	if (media & BRASERO_MEDIUM_PLUS)
		(*supported_flags) &= ~BRASERO_BURN_FLAG_DUMMY;

	/* Simulation is only possible according to write modes. This mode is
	 * mostly used by cdrecord/wodim for CLONE images. */
	else if (media & BRASERO_MEDIUM_DVD) {
		if (!brasero_medium_can_use_dummy_for_sao (medium))
			(*supported_flags) &= ~BRASERO_BURN_FLAG_DUMMY;
	}
	else if ((*supported_flags) & BRASERO_BURN_FLAG_DAO) {
		if (!brasero_medium_can_use_dummy_for_sao (medium))
			(*supported_flags) &= ~BRASERO_BURN_FLAG_DUMMY;
	}
	else if (!brasero_medium_can_use_dummy_for_tao (medium))
		(*supported_flags) &= ~BRASERO_BURN_FLAG_DUMMY;

	/* The following is only true if we won't _have_ to blank
	 * the disc since a CLOSED disc is not able for tao/sao.
	 * so if BLANK_BEFORE_RIGHT is TRUE then we leave 
	 * the benefit of the doubt, but flags should be rechecked
	 * after the drive was blanked. */
	if (((*compulsory_flags) & BRASERO_BURN_FLAG_BLANK_BEFORE_WRITE) == 0
	&&  !BRASERO_MEDIUM_RANDOM_WRITABLE (media)
	&&  !brasero_medium_can_use_tao (medium)) {
		(*supported_flags) &= ~BRASERO_BURN_FLAG_MULTI;

		if (brasero_medium_can_use_sao (medium))
			(*compulsory_flags) |= BRASERO_BURN_FLAG_DAO;
		else
			(*supported_flags) &= ~BRASERO_BURN_FLAG_DAO;
	}

	if (!brasero_medium_can_use_burnfree (medium))
		(*supported_flags) &= ~BRASERO_BURN_FLAG_BURNPROOF;
}

static void
brasero_burn_caps_flags_update_for_drive (BraseroBurnSession *session,
                                          BraseroBurnFlag *supported_flags,
                                          BraseroBurnFlag *compulsory_flags)
{
	BraseroDrive *drive;
	BraseroMedium *medium;

	drive = brasero_burn_session_get_burner (session);
	if (!drive)
		return;

	medium = brasero_drive_get_medium (drive);
	if (!medium)
		return;

	brasero_medium_supported_flags (medium,
	                                supported_flags,
	                                compulsory_flags);
}

static BraseroBurnResult
brasero_caps_get_flags_for_disc (BraseroBurnCaps *self,
                                 gboolean ignore_plugin_errors,
				 BraseroBurnFlag session_flags,
				 BraseroMedia media,
				 BraseroTrackType *input,
				 BraseroBurnFlag *supported,
				 BraseroBurnFlag *compulsory)
{
	BraseroBurnFlag supported_flags = BRASERO_BURN_FLAG_NONE;
	BraseroBurnFlag compulsory_flags = BRASERO_BURN_FLAG_ALL;
	BraseroPluginIOFlag io_flags;
	BraseroTrackType output;
	BraseroCaps *caps;

	/* create the output to find first caps */
	brasero_track_type_set_has_medium (&output);
	brasero_track_type_set_medium_type (&output, media);

	caps = brasero_burn_caps_find_start_caps (self, &output);
	if (!caps) {
		BRASERO_BURN_LOG_DISC_TYPE (media, "FLAGS: no caps could be found for");
		return BRASERO_BURN_NOT_SUPPORTED;
	}

	BRASERO_BURN_LOG_WITH_TYPE (&caps->type,
				    caps->flags,
				    "FLAGS: trying caps");

	io_flags = brasero_caps_get_flags (caps,
	                                   ignore_plugin_errors,
					   session_flags,
					   media,
					   input,
					   BRASERO_PLUGIN_IO_ACCEPT_FILE|
					   BRASERO_PLUGIN_IO_ACCEPT_PIPE,
					   &supported_flags,
					   &compulsory_flags);

	if (io_flags == BRASERO_PLUGIN_IO_NONE) {
		BRASERO_BURN_LOG ("FLAGS: not supported");
		return BRASERO_BURN_NOT_SUPPORTED;
	}

	/* NOTE: DO NOT TEST the input image here. What should be tested is the
	 * type of the input right before the burner plugin. See:
	 * brasero_burn_caps_set_flags_from_recorder_input())
	 * For example in the following situation: AUDIO => CUE => BURNER the
	 * DAO flag would not be set otherwise. */

	if (brasero_track_type_get_has_stream (input)) {
		BraseroStreamFormat format;

		format = brasero_track_type_get_stream_format (input);
		if (format & BRASERO_METADATA_INFO) {
			/* In this case, DAO is compulsory if we want to write CD-TEXT */
			if (supported_flags & BRASERO_BURN_FLAG_DAO)
				compulsory_flags |= BRASERO_BURN_FLAG_DAO;
			else
				return BRASERO_BURN_NOT_SUPPORTED;
		}
	}

	if (compulsory_flags & BRASERO_BURN_FLAG_DAO) {
		/* unlikely */
		compulsory_flags &= ~BRASERO_BURN_FLAG_RAW;
		supported_flags &= ~BRASERO_BURN_FLAG_RAW;
	}
	
	if (io_flags & BRASERO_PLUGIN_IO_ACCEPT_PIPE) {
		supported_flags |= BRASERO_BURN_FLAG_NO_TMP_FILES;

		if ((io_flags & BRASERO_PLUGIN_IO_ACCEPT_FILE) == 0)
			compulsory_flags |= BRASERO_BURN_FLAG_NO_TMP_FILES;
	}

	*supported |= supported_flags;
	*compulsory |= compulsory_flags;

	return BRASERO_BURN_OK;
}

static BraseroBurnResult
brasero_burn_caps_get_flags_for_medium (BraseroBurnCaps *self,
                                        BraseroBurnSession *session,
					BraseroMedia media,
					BraseroBurnFlag session_flags,
					BraseroTrackType *input,
					BraseroBurnFlag *supported_flags,
					BraseroBurnFlag *compulsory_flags)
{
	BraseroBurnResult result;
	gboolean can_blank = FALSE;

	/* See if medium is supported out of the box */
	result = brasero_caps_get_flags_for_disc (self,
	                                          brasero_burn_session_get_strict_support (session) == FALSE,
						  session_flags,
						  media,
						  input,
						  supported_flags,
						  compulsory_flags);

	/* see if we can add BRASERO_BURN_FLAG_BLANK_BEFORE_WRITE. Add it when:
	 * - media can be blanked, it has audio or data and we're not merging
	 * - media is not formatted and it can be blanked/formatted */
	if (brasero_burn_caps_can_blank_real (self, brasero_burn_session_get_strict_support (session) == FALSE, media, session_flags) == BRASERO_BURN_OK)
		can_blank = TRUE;
	else if (session_flags & BRASERO_BURN_FLAG_BLANK_BEFORE_WRITE)
		return BRASERO_BURN_NOT_SUPPORTED;

	if (can_blank) {
		gboolean first_success;
		BraseroBurnFlag blank_compulsory = BRASERO_BURN_FLAG_NONE;
		BraseroBurnFlag blank_supported = BRASERO_BURN_FLAG_NONE;

		/* we reached this point in two cases:
		 * - if the disc cannot be handled
		 * - if some flags are not handled
		 * It is helpful only if:
		 * - the disc was closed and no plugin can handle this type of 
		 * disc once closed (CD-R(W))
		 * - there was the flag BLANK_BEFORE_WRITE set and no plugin can
		 * handle this flag (means that the plugin should erase and
		 * then write on its own. Basically that works only with
		 * overwrite formatted discs, DVD+RW, ...) */

		/* What's above is not entirely true. In fact we always need to
		 * check even if we first succeeded. There are some cases like
		 * CDRW where it's useful.
		 * Ex: a CDRW with data appendable can be either appended (then
		 * no DAO possible) or blanked and written (DAO possible). */

		/* result here is the result of the first operation, so if it
		 * failed, BLANK before becomes compulsory. */
		first_success = (result == BRASERO_BURN_OK);

		/* pretends it is blank and formatted to see if it would work.
		 * If it works then that means that the BLANK_BEFORE_WRITE flag
		 * is compulsory. */
		media &= ~(BRASERO_MEDIUM_CLOSED|
			   BRASERO_MEDIUM_APPENDABLE|
			   BRASERO_MEDIUM_UNFORMATTED|
			   BRASERO_MEDIUM_HAS_DATA|
			   BRASERO_MEDIUM_HAS_AUDIO);
		media |= BRASERO_MEDIUM_BLANK;
		result = brasero_caps_get_flags_for_disc (self,
		                                          brasero_burn_session_get_strict_support (session) == FALSE,
							  session_flags,
							  media,
							  input,
							  supported_flags,
							  compulsory_flags);

		/* if both attempts failed, drop it */
		if (result != BRASERO_BURN_OK) {
			/* See if we entirely failed */
			if (!first_success)
				return result;

			/* we tried with a blank medium but did not 
			 * succeed. So that means the flags BLANK.
			 * is not supported */
		}
		else {
			(*supported_flags) |= BRASERO_BURN_FLAG_BLANK_BEFORE_WRITE;

			if (!first_success)
				(*compulsory_flags) |= BRASERO_BURN_FLAG_BLANK_BEFORE_WRITE;

			/* If BLANK flag is supported then MERGE/APPEND can't be compulsory */
			(*compulsory_flags) &= ~(BRASERO_BURN_FLAG_MERGE|BRASERO_BURN_FLAG_APPEND);

			/* need to add blanking flags */
			brasero_burn_caps_get_blanking_flags_real (self,
			                                           brasero_burn_session_get_strict_support (session) == FALSE,
								   media,
								   session_flags,
								   &blank_supported,
								   &blank_compulsory);
			(*supported_flags) |= blank_supported;
			(*compulsory_flags) |= blank_compulsory;
		}
		
	}
	else if (result != BRASERO_BURN_OK)
		return result;

	/* These are a special case for DVDRW sequential */
	if (BRASERO_MEDIUM_IS (media, BRASERO_MEDIUM_DVDRW)) {
		/* That's a way to give priority to MULTI over FAST
		 * and leave the possibility to always use MULTI. */
		if (session_flags & BRASERO_BURN_FLAG_MULTI)
			(*supported_flags) &= ~BRASERO_BURN_FLAG_FAST_BLANK;
		else if ((session_flags & BRASERO_BURN_FLAG_FAST_BLANK)
		         &&  (session_flags & BRASERO_BURN_FLAG_BLANK_BEFORE_WRITE)) {
			/* We should be able to handle this case differently but unfortunately
			 * there are buggy firmwares that won't report properly the supported
			 * mode writes */
			if (!((*supported_flags) & BRASERO_BURN_FLAG_DAO))
					 return BRASERO_BURN_NOT_SUPPORTED;

			(*compulsory_flags) |= BRASERO_BURN_FLAG_DAO;
		}
	}

	if (session_flags & BRASERO_BURN_FLAG_BLANK_BEFORE_WRITE) {
		/* make sure we remove MERGE/APPEND from supported and
		 * compulsory since that's not possible anymore */
		(*supported_flags) &= ~(BRASERO_BURN_FLAG_MERGE|BRASERO_BURN_FLAG_APPEND);
		(*compulsory_flags) &= ~(BRASERO_BURN_FLAG_MERGE|BRASERO_BURN_FLAG_APPEND);
	}

	/* FIXME! we should restart the whole process if
	 * ((session_flags & compulsory_flags) != compulsory_flags) since that
	 * means that some supported files could be excluded but were not */

	return BRASERO_BURN_OK;
}

static BraseroBurnResult
brasero_burn_caps_get_flags_same_src_dest_for_types (BraseroBurnCaps *self,
                                                     BraseroBurnSession *session,
                                                     BraseroTrackType *input,
                                                     BraseroTrackType *output,
                                                     BraseroBurnFlag *supported_ret,
                                                     BraseroBurnFlag *compulsory_ret)
{
	GSList *iter;
	gboolean type_supported;
	BraseroBurnResult result;
	BraseroBurnFlag session_flags;
	BraseroFindLinkCtx ctx = { 0, NULL, 0, };
	BraseroBurnFlag supported_final = BRASERO_BURN_FLAG_NONE;
	BraseroBurnFlag compulsory_final = BRASERO_BURN_FLAG_ALL;

	/* NOTE: there is no need to get the flags here since there are
	 * no specific DISC => IMAGE flags. We just want to know if that
	 * is possible. */
	BRASERO_BURN_LOG_TYPE (output, "Testing temporary image format");

	brasero_caps_find_link_set_ctx (session, &ctx, input);
	ctx.io_flags = BRASERO_PLUGIN_IO_ACCEPT_FILE;

	/* Here there is no need to try blanking as there
	 * is no disc (saves a few debug lines) */
	result = brasero_caps_try_output (self, &ctx, output);
	if (result != BRASERO_BURN_OK) {
		BRASERO_BURN_LOG_TYPE (output, "Format not supported");
		return result;
	}

	session_flags = brasero_burn_session_get_flags (session);

	/* This format can be used to create an image. Check if can be
	 * burnt now. Just find at least one medium. */
	type_supported = FALSE;
	for (iter = self->priv->caps_list; iter; iter = iter->next) {
		BraseroBurnFlag compulsory;
		BraseroBurnFlag supported;
		BraseroBurnResult result;
		BraseroMedia media;
		BraseroCaps *caps;

		caps = iter->data;
		if (!brasero_track_type_get_has_medium (&caps->type))
			continue;

		media = brasero_track_type_get_medium_type (&caps->type);

		/* This type of disc cannot be burnt; skip them */
		if (media & BRASERO_MEDIUM_ROM)
			continue;

		if ((media & BRASERO_MEDIUM_CD) == 0) {
			if (brasero_track_type_get_has_image (output)) {
				BraseroImageFormat format;

				format = brasero_track_type_get_image_format (output);
				/* These three types only work with CDs. */
				if (format == BRASERO_IMAGE_FORMAT_CDRDAO
				||   format == BRASERO_IMAGE_FORMAT_CLONE
				||   format == BRASERO_IMAGE_FORMAT_CUE)
					continue;
			}
			else if (brasero_track_type_get_has_stream (output))
				continue;
		}

		/* Merge all available flags for each possible medium type */
		supported = BRASERO_BURN_FLAG_NONE;
		compulsory = BRASERO_BURN_FLAG_NONE;

		result = brasero_caps_get_flags_for_disc (self,
		                                          brasero_burn_session_get_strict_support (session) == FALSE,
		                                          session_flags,
		                                          media,
							  output,
							  &supported,
							  &compulsory);

		if (result != BRASERO_BURN_OK)
			continue;

		type_supported = TRUE;
		supported_final |= supported;
		compulsory_final &= compulsory;
	}

	BRASERO_BURN_LOG_TYPE (output, "Format supported %i", type_supported);
	if (!type_supported)
		return BRASERO_BURN_NOT_SUPPORTED;

	*supported_ret = supported_final;
	*compulsory_ret = compulsory_final;
	return BRASERO_BURN_OK;
}

static BraseroBurnResult
brasero_burn_caps_get_flags_same_src_dest (BraseroBurnCaps *self,
					   BraseroBurnSession *session,
					   BraseroBurnFlag *supported_ret,
					   BraseroBurnFlag *compulsory_ret)
{
	BraseroTrackType input;
	BraseroBurnResult result;
	gboolean copy_supported;
	BraseroTrackType output;
	BraseroImageFormat format;
	BraseroBurnFlag session_flags;
	BraseroBurnFlag supported_final = BRASERO_BURN_FLAG_NONE;
	BraseroBurnFlag compulsory_final = BRASERO_BURN_FLAG_ALL;

	BRASERO_BURN_LOG ("Retrieving disc copy flags with same source and destination");

	/* To determine if a CD/DVD can be copied using the same source/dest,
	 * we first determine if can be imaged and then what are the flags when
	 * we can burn it to a particular medium type. */
	memset (&input, 0, sizeof (BraseroTrackType));
	brasero_burn_session_get_input_type (session, &input);
	BRASERO_BURN_LOG_TYPE (&input, "input");

	session_flags = brasero_burn_session_get_flags (session);
	BRASERO_BURN_LOG_FLAGS (session_flags, "(FLAGS) Session flags");

	/* Check the current flags are possible */
	if (session_flags & (BRASERO_BURN_FLAG_MERGE|BRASERO_BURN_FLAG_NO_TMP_FILES))
		return BRASERO_BURN_NOT_SUPPORTED;

	/* Check for stream type */
	brasero_track_type_set_has_stream (&output);
	/* FIXME! */
	brasero_track_type_set_stream_format (&output,
	                                      BRASERO_AUDIO_FORMAT_RAW|
	                                      BRASERO_METADATA_INFO);

	result = brasero_burn_caps_get_flags_same_src_dest_for_types (self,
	                                                              session,
	                                                              &input,
	                                                              &output,
	                                                              &supported_final,
	                                                              &compulsory_final);
	if (result == BRASERO_BURN_CANCEL)
		return result;

	copy_supported = (result == BRASERO_BURN_OK);

	/* Check flags for all available format */
	format = BRASERO_IMAGE_FORMAT_CDRDAO;
	brasero_track_type_set_has_image (&output);
	for (; format > BRASERO_IMAGE_FORMAT_NONE; format >>= 1) {
		BraseroBurnFlag supported;
		BraseroBurnFlag compulsory;

		/* check if this image type is possible given the current flags */
		if (format != BRASERO_IMAGE_FORMAT_CLONE
		&& (session_flags & BRASERO_BURN_FLAG_RAW))
			continue;

		brasero_track_type_set_image_format (&output, format);

		supported = BRASERO_BURN_FLAG_NONE;
		compulsory = BRASERO_BURN_FLAG_NONE;
		result = brasero_burn_caps_get_flags_same_src_dest_for_types (self,
		                                                              session,
		                                                              &input,
		                                                              &output,
		                                                              &supported,
		                                                              &compulsory);
		if (result == BRASERO_BURN_CANCEL)
			return result;

		if (result != BRASERO_BURN_OK)
			continue;

		copy_supported = TRUE;
		supported_final |= supported;
		compulsory_final &= compulsory;
	}

	if (!copy_supported)
		return BRASERO_BURN_NOT_SUPPORTED;

	*supported_ret |= supported_final;
	*compulsory_ret |= compulsory_final;

	/* we also add these two flags as being supported
	 * since they could be useful and can't be tested
	 * until the disc is inserted which it is not at the
	 * moment */
	(*supported_ret) |= (BRASERO_BURN_FLAG_BLANK_BEFORE_WRITE|
			     BRASERO_BURN_FLAG_FAST_BLANK);

	if (brasero_track_type_get_medium_type (&input) & BRASERO_MEDIUM_HAS_AUDIO) {
		/* This is a special case for audio discs.
		 * Since they may contain CD-TEXT and
		 * since CD-TEXT can only be written with
		 * DAO then we must make sure the user
		 * can't use MULTI since then DAO is
		 * impossible. */
		(*compulsory_ret) |= BRASERO_BURN_FLAG_DAO;

		/* This is just to make sure */
		(*supported_ret) &= (~BRASERO_BURN_FLAG_MULTI);
		(*compulsory_ret) &= (~BRASERO_BURN_FLAG_MULTI);
	}

	return BRASERO_BURN_OK;
}

/**
 * This is meant to use as internal API
 */
BraseroBurnResult
brasero_caps_session_get_image_flags (BraseroTrackType *input,
                                     BraseroTrackType *output,
                                     BraseroBurnFlag *supported,
                                     BraseroBurnFlag *compulsory)
{
	BraseroBurnFlag compulsory_flags = BRASERO_BURN_FLAG_NONE;
	BraseroBurnFlag supported_flags = BRASERO_BURN_FLAG_CHECK_SIZE|BRASERO_BURN_FLAG_NOGRACE;

	BRASERO_BURN_LOG ("FLAGS: image required");

	/* In this case no APPEND/MERGE is possible */
	if (brasero_track_type_get_has_medium (input))
		supported_flags |= BRASERO_BURN_FLAG_EJECT;

	*supported = supported_flags;
	*compulsory = compulsory_flags;

	BRASERO_BURN_LOG_FLAGS (supported_flags, "FLAGS: supported");
	BRASERO_BURN_LOG_FLAGS (compulsory_flags, "FLAGS: compulsory");

	return BRASERO_BURN_OK;
}

/**
 * brasero_burn_session_get_burn_flags:
 * @session: a #BraseroBurnSession
 * @supported: a #BraseroBurnFlag or NULL
 * @compulsory: a #BraseroBurnFlag or NULL
 *
 * Given the various parameters stored in @session, this function
 * stores:
 * - the flags that can be used (@supported)
 * - the flags that must be used (@compulsory)
 * when writing @session to a disc.
 *
 * Return value: a #BraseroBurnResult.
 * BRASERO_BURN_OK if the retrieval was successful.
 * BRASERO_BURN_ERR otherwise.
 **/

BraseroBurnResult
brasero_burn_session_get_burn_flags (BraseroBurnSession *session,
				     BraseroBurnFlag *supported,
				     BraseroBurnFlag *compulsory)
{
	gboolean res;
	BraseroMedia media;
	BraseroBurnCaps *self;
	BraseroTrackType *input;
	BraseroBurnResult result;

	BraseroBurnFlag session_flags;
	/* FIXME: what's the meaning of NOGRACE when outputting ? */
	BraseroBurnFlag compulsory_flags = BRASERO_BURN_FLAG_NONE;
	BraseroBurnFlag supported_flags = BRASERO_BURN_FLAG_CHECK_SIZE|
					  BRASERO_BURN_FLAG_NOGRACE;

	self = brasero_burn_caps_get_default ();

	input = brasero_track_type_new ();
	brasero_burn_session_get_input_type (session, input);
	BRASERO_BURN_LOG_WITH_TYPE (input,
				    BRASERO_PLUGIN_IO_NONE,
				    "FLAGS: searching available flags for input");

	if (brasero_burn_session_is_dest_file (session)) {
		BraseroTrackType *output;

		BRASERO_BURN_LOG ("FLAGS: image required");

		output = brasero_track_type_new ();
		brasero_burn_session_get_output_type (session, output);
		brasero_caps_session_get_image_flags (input, output, supported, compulsory);
		brasero_track_type_free (output);

		brasero_track_type_free (input);
		g_object_unref (self);
		return BRASERO_BURN_OK;
	}

	supported_flags |= BRASERO_BURN_FLAG_EJECT;

	/* special case */
	if (brasero_burn_session_same_src_dest_drive (session)) {
		BRASERO_BURN_LOG ("Same source and destination");
		result = brasero_burn_caps_get_flags_same_src_dest (self,
								    session,
								    &supported_flags,
								    &compulsory_flags);

		/* These flags are of course never possible */
		supported_flags &= ~(BRASERO_BURN_FLAG_NO_TMP_FILES|
				     BRASERO_BURN_FLAG_MERGE);
		compulsory_flags &= ~(BRASERO_BURN_FLAG_NO_TMP_FILES|
				      BRASERO_BURN_FLAG_MERGE);

		if (result == BRASERO_BURN_OK) {
			BRASERO_BURN_LOG_FLAGS (supported_flags, "FLAGS: supported");
			BRASERO_BURN_LOG_FLAGS (compulsory_flags, "FLAGS: compulsory");

			*supported = supported_flags;
			*compulsory = compulsory_flags;
		}
		else
			BRASERO_BURN_LOG ("No available flags for copy");

		brasero_track_type_free (input);
		g_object_unref (self);
		return result;
	}

	session_flags = brasero_burn_session_get_flags (session);
	BRASERO_BURN_LOG_FLAGS (session_flags, "FLAGS (session):");

	/* sanity check:
	 * - drive must support flags
	 * - MERGE and BLANK are not possible together.
	 * - APPEND and MERGE are compatible. MERGE wins
	 * - APPEND and BLANK are incompatible */
	res = brasero_check_flags_for_drive (brasero_burn_session_get_burner (session), session_flags);
	if (!res) {
		BRASERO_BURN_LOG ("Session flags not supported by drive");
		brasero_track_type_free (input);
		g_object_unref (self);
		return BRASERO_BURN_ERR;
	}

	if ((session_flags & (BRASERO_BURN_FLAG_MERGE|BRASERO_BURN_FLAG_APPEND))
	&&  (session_flags & BRASERO_BURN_FLAG_BLANK_BEFORE_WRITE)) {
		brasero_track_type_free (input);
		g_object_unref (self);
		return BRASERO_BURN_NOT_SUPPORTED;
	}
	
	/* Let's get flags for recording */
	media = brasero_burn_session_get_dest_media (session);
	result = brasero_burn_caps_get_flags_for_medium (self,
	                                                 session,
							 media,
							 session_flags,
							 input,
							 &supported_flags,
							 &compulsory_flags);

	brasero_track_type_free (input);
	g_object_unref (self);

	if (result != BRASERO_BURN_OK)
		return result;

	brasero_burn_caps_flags_update_for_drive (session,
	                                          &supported_flags,
	                                          &compulsory_flags);

	if (supported)
		*supported = supported_flags;

	if (compulsory)
		*compulsory = compulsory_flags;

	BRASERO_BURN_LOG_FLAGS (supported_flags, "FLAGS: supported");
	BRASERO_BURN_LOG_FLAGS (compulsory_flags, "FLAGS: compulsory");
	return BRASERO_BURN_OK;
}