summaryrefslogtreecommitdiff
path: root/mate-volume-control/gvc-mixer-dialog.c
blob: c3f399649c317d6f6ff40655304c40a918cbe048 (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
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
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2008 William Jon McCann
 * Copyright (C) 2014 Michal Ratajsky <michal.ratajsky@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"

#include <glib.h>
#include <glib/gi18n.h>
#include <glib-object.h>
#include <gdk/gdk.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include <libmatemixer/matemixer.h>

#include "gvc-channel-bar.h"
#include "gvc-balance-bar.h"
#include "gvc-combo-box.h"
#include "gvc-mixer-dialog.h"
#include "gvc-sound-theme-chooser.h"
#include "gvc-level-bar.h"
#include "gvc-speaker-test.h"
#include "gvc-utils.h"

struct _GvcMixerDialogPrivate
{
        MateMixerContext *context;
        MateMixerBackendFlags backend_flags;
        GHashTable       *bars;
        GtkWidget        *notebook;
        GtkWidget        *output_bar;
        GtkWidget        *input_bar;
        GtkWidget        *input_level_bar;
        GtkWidget        *effects_bar;
        GtkWidget        *output_stream_box;
        GtkWidget        *hw_box;
        GtkWidget        *hw_treeview;
        GtkWidget        *hw_settings_box;
        GtkWidget        *hw_profile_combo;
        GtkWidget        *input_box;
        GtkWidget        *output_box;
        GtkWidget        *applications_box;
        GtkWidget        *applications_window;
        GtkWidget        *no_apps_label;
        GtkWidget        *output_treeview;
        GtkWidget        *output_settings_frame;
        GtkWidget        *output_settings_box;
        GtkWidget        *output_balance_bar;
        GtkWidget        *output_fade_bar;
        GtkWidget        *output_lfe_bar;
        GtkWidget        *output_port_combo;
        GtkWidget        *input_treeview;
        GtkWidget        *input_port_combo;
        GtkWidget        *input_settings_box;
        GtkSizeGroup     *size_group;
        gdouble           last_input_peak;
        guint             num_apps;
};

enum {
        ICON_COLUMN,
        NAME_COLUMN,
        LABEL_COLUMN,
        ACTIVE_COLUMN,
        SPEAKERS_COLUMN,
        NUM_COLUMNS
};

enum {
        HW_ICON_COLUMN,
        HW_NAME_COLUMN,
        HW_LABEL_COLUMN,
        HW_STATUS_COLUMN,
        HW_PROFILE_COLUMN,
        HW_NUM_COLUMNS
};

enum {
        PAGE_EFFECTS,
        PAGE_HARDWARE,
        PAGE_INPUT,
        PAGE_OUTPUT,
        PAGE_APPLICATIONS
};

enum {
        PROP_0,
        PROP_CONTEXT
};

static const guint tab_accel_keys[] = {
        GDK_KEY_1, GDK_KEY_2, GDK_KEY_3, GDK_KEY_4, GDK_KEY_5
};

static void gvc_mixer_dialog_finalize   (GObject                *object);

static void add_stream                  (GvcMixerDialog         *dialog,
                                         MateMixerStream        *stream);
static void add_application_control     (GvcMixerDialog         *dialog,
                                         MateMixerStreamControl *control);

static void remove_stream               (GvcMixerDialog         *dialog,
                                         const gchar            *name);
static void remove_application_control  (GvcMixerDialog         *dialog,
                                         const gchar            *name);

static void bar_set_stream              (GvcMixerDialog         *dialog,
                                         GtkWidget              *bar,
                                         MateMixerStream        *stream);
static void bar_set_stream_control      (GvcMixerDialog         *dialog,
                                         GtkWidget              *bar,
                                         MateMixerStreamControl *control);

static gboolean dialog_page_scroll_event_cb (GtkWidget          *widget,
                                             GdkEventScroll     *event,
                                             GtkWindow          *window);

G_DEFINE_TYPE_WITH_PRIVATE (GvcMixerDialog, gvc_mixer_dialog, GTK_TYPE_DIALOG)

static MateMixerSwitch *
find_stream_port_switch (MateMixerStream *stream)
{
        const GList *switches;

        switches = mate_mixer_stream_list_switches (stream);
        while (switches != NULL) {
                MateMixerStreamSwitch *swtch = MATE_MIXER_STREAM_SWITCH (switches->data);

                if (!MATE_MIXER_IS_STREAM_TOGGLE (swtch) &&
                    mate_mixer_stream_switch_get_role (swtch) == MATE_MIXER_STREAM_SWITCH_ROLE_PORT)
                    return MATE_MIXER_SWITCH (swtch);

                switches = switches->next;
        }
        return NULL;
}

static MateMixerSwitch *
find_device_profile_switch (MateMixerDevice *device)
{
        const GList *switches;

        switches = mate_mixer_device_list_switches (device);
        while (switches != NULL) {
                MateMixerDeviceSwitch *swtch = MATE_MIXER_DEVICE_SWITCH (switches->data);

                if (mate_mixer_device_switch_get_role (swtch) == MATE_MIXER_DEVICE_SWITCH_ROLE_PROFILE)
                        return MATE_MIXER_SWITCH (swtch);

                switches = switches->next;
        }
        return NULL;
}

static MateMixerStream *
find_device_test_stream (GvcMixerDialog *dialog, MateMixerDevice *device)
{
        const GList *streams;

        streams = mate_mixer_device_list_streams (device);
        while (streams != NULL) {
                MateMixerStream   *stream;
                MateMixerDirection direction;

                stream = MATE_MIXER_STREAM (streams->data);
                direction = mate_mixer_stream_get_direction (stream);

                if (direction == MATE_MIXER_DIRECTION_OUTPUT) {
                    MateMixerStreamControl *control;

                    control = mate_mixer_stream_get_default_control (stream);
                    if (mate_mixer_stream_control_get_num_channels (control) > 0)
                        return stream;
                }
                streams = streams->next;
        }
        return FALSE;
}

static gboolean
find_tree_item_by_name (GtkTreeModel *model,
                        const gchar  *name,
                        guint         column,
                        GtkTreeIter  *iter)
{
        gboolean found = FALSE;

        if (!gtk_tree_model_get_iter_first (model, iter))
                return FALSE;

        do {
                gchar *n;
                gtk_tree_model_get (model, iter, column, &n, -1);

                if (!g_strcmp0 (name, n))
                        found = TRUE;

                g_free (n);
        } while (!found && gtk_tree_model_iter_next (model, iter));

        return found;
}

static void
update_default_tree_item (GvcMixerDialog  *dialog,
                          GtkTreeModel    *model,
                          MateMixerStream *stream)
{
        GtkTreeIter  iter;
        const gchar *name = NULL;

        if (gtk_tree_model_get_iter_first (model, &iter) == FALSE)
                return;

        /* The supplied stream is the default, or the selected item. Traverse
         * the item list and mark each item as being selected or not. Also do not
         * presume some known stream is selected and allow NULL here. */
        if (stream != NULL)
                name = mate_mixer_stream_get_name (stream);

        do {
                gchar *n;
                gtk_tree_model_get (model, &iter,
                                    NAME_COLUMN, &n,
                                    -1);
                gtk_list_store_set (GTK_LIST_STORE (model),
                                    &iter,
                                    ACTIVE_COLUMN, !g_strcmp0 (name, n),
                                    -1);
                g_free (n);
        } while (gtk_tree_model_iter_next (model, &iter));
}

static void
update_output_settings (GvcMixerDialog *dialog)
{
        MateMixerStream            *stream;
        MateMixerStreamControl     *control;
        MateMixerStreamControlFlags flags;
        MateMixerSwitch            *port_switch;
        gboolean                    has_settings = FALSE;

        g_debug ("Updating output settings");

        if (dialog->priv->output_balance_bar != NULL) {
                gtk_container_remove (GTK_CONTAINER (dialog->priv->output_settings_box),
                                      dialog->priv->output_balance_bar);

                dialog->priv->output_balance_bar = NULL;
        }
        if (dialog->priv->output_fade_bar != NULL) {
                gtk_container_remove (GTK_CONTAINER (dialog->priv->output_settings_box),
                                      dialog->priv->output_fade_bar);

                dialog->priv->output_fade_bar = NULL;
        }
        if (dialog->priv->output_lfe_bar != NULL) {
                gtk_container_remove (GTK_CONTAINER (dialog->priv->output_settings_box),
                                      dialog->priv->output_lfe_bar);

                dialog->priv->output_lfe_bar = NULL;
        }
        if (dialog->priv->output_port_combo != NULL) {
                gtk_container_remove (GTK_CONTAINER (dialog->priv->output_settings_box),
                                      dialog->priv->output_port_combo);

                dialog->priv->output_port_combo = NULL;
        }

        /* Get the control currently associated with the output slider */
        control = gvc_channel_bar_get_control (GVC_CHANNEL_BAR (dialog->priv->output_bar));
        if (control == NULL) {
                g_debug ("There is no control for the default output stream");
                gtk_widget_hide (dialog->priv->output_settings_frame);
                return;
        }
        flags = mate_mixer_stream_control_get_flags (control);

        /* Enable balance bar if it is available */
        if (flags & MATE_MIXER_STREAM_CONTROL_CAN_BALANCE) {
                dialog->priv->output_balance_bar =
                        gvc_balance_bar_new (control, BALANCE_TYPE_RL);

                gvc_balance_bar_set_size_group (GVC_BALANCE_BAR (dialog->priv->output_balance_bar),
                                                dialog->priv->size_group,
                                                TRUE);

                gtk_box_pack_start (GTK_BOX (dialog->priv->output_settings_box),
                                    dialog->priv->output_balance_bar,
                                    FALSE, FALSE, 6);

                gtk_widget_show (dialog->priv->output_balance_bar);
                has_settings = TRUE;
        }

        /* Enable fade bar if it is available */
        if (flags & MATE_MIXER_STREAM_CONTROL_CAN_FADE) {
                dialog->priv->output_fade_bar =
                        gvc_balance_bar_new (control, BALANCE_TYPE_FR);

                gvc_balance_bar_set_size_group (GVC_BALANCE_BAR (dialog->priv->output_fade_bar),
                                                dialog->priv->size_group,
                                                TRUE);

                gtk_box_pack_start (GTK_BOX (dialog->priv->output_settings_box),
                                    dialog->priv->output_fade_bar,
                                    FALSE, FALSE, 6);

                gtk_widget_show (dialog->priv->output_fade_bar);
                has_settings = TRUE;
        }

        /* Enable subwoofer volume bar if subwoofer is available */
        if (mate_mixer_stream_control_has_channel_position (control, MATE_MIXER_CHANNEL_LFE)) {
                dialog->priv->output_lfe_bar =
                        gvc_balance_bar_new (control, BALANCE_TYPE_LFE);

                gvc_balance_bar_set_size_group (GVC_BALANCE_BAR (dialog->priv->output_lfe_bar),
                                                dialog->priv->size_group,
                                                TRUE);

                gtk_box_pack_start (GTK_BOX (dialog->priv->output_settings_box),
                                    dialog->priv->output_lfe_bar,
                                    FALSE, FALSE, 6);

                gtk_widget_show (dialog->priv->output_lfe_bar);
                has_settings = TRUE;
        }

        /* Get owning stream of the control */
        stream = mate_mixer_stream_control_get_stream (control);
        if (G_UNLIKELY (stream == NULL))
                return;

        /* Enable the port selector if the stream has one */
        port_switch = find_stream_port_switch (stream);
        if (port_switch != NULL) {
                dialog->priv->output_port_combo =
                        gvc_combo_box_new (port_switch, _("Co_nnector:"));

                gvc_combo_box_set_size_group (GVC_COMBO_BOX (dialog->priv->output_port_combo),
                                              dialog->priv->size_group,
                                              FALSE);

                gtk_box_pack_start (GTK_BOX (dialog->priv->output_settings_box),
                                    dialog->priv->output_port_combo,
                                    TRUE, FALSE, 6);

                gtk_widget_show (dialog->priv->output_port_combo);
                has_settings = TRUE;
        }

        if (has_settings == TRUE)
                gtk_widget_show (dialog->priv->output_settings_frame);
        else
                gtk_widget_hide (dialog->priv->output_settings_frame);
}

static void
set_output_stream (GvcMixerDialog *dialog, MateMixerStream *stream)
{
        GtkTreeModel           *model;
        MateMixerStreamControl *control;

        control = gvc_channel_bar_get_control (GVC_CHANNEL_BAR (dialog->priv->output_bar));
        if (control != NULL) {
                /* Disconnect port switch of the previous stream */
                if (dialog->priv->output_port_combo != NULL) {
                        MateMixerSwitch        *swtch;

                        swtch = g_object_get_data (G_OBJECT (dialog->priv->output_port_combo),
                                                   "switch");
                        if (swtch != NULL)
                                g_signal_handlers_disconnect_by_data (G_OBJECT (swtch),
                                                                      dialog);
                }
        }

        bar_set_stream (dialog, dialog->priv->output_bar, stream);

        if (stream != NULL) {
                const GList *controls;

                controls = mate_mixer_context_list_stored_controls (dialog->priv->context);

                /* Move all stored controls to the newly selected default stream */
                while (controls != NULL) {
                        MateMixerStream        *parent;

                        control = MATE_MIXER_STREAM_CONTROL (controls->data);
                        parent  = mate_mixer_stream_control_get_stream (control);

                        /* Prefer streamless controls to stay the way they are, forcing them to
                         * a particular owning stream would be wrong for eg. event controls */
                        if (parent != NULL && parent != stream) {
                                MateMixerDirection direction =
                                        mate_mixer_stream_get_direction (parent);

                                if (direction == MATE_MIXER_DIRECTION_OUTPUT)
                                        mate_mixer_stream_control_set_stream (control, stream);
                        }
                        controls = controls->next;
                }
        }

        model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->priv->output_treeview));
        update_default_tree_item (dialog, model, stream);

        update_output_settings (dialog);
}

static void
on_context_default_output_stream_notify (MateMixerContext *context,
                                         GParamSpec       *pspec,
                                         GvcMixerDialog   *dialog)
{
        MateMixerStream *stream;

        stream = mate_mixer_context_get_default_output_stream (context);

        set_output_stream (dialog, stream);
}

#define DECAY_STEP .15

static void
on_stream_control_monitor_value (MateMixerStream *stream,
                                 gdouble          value,
                                 GvcMixerDialog  *dialog)
{
        GtkAdjustment *adj;

        if (dialog->priv->last_input_peak >= DECAY_STEP) {
                if (value < dialog->priv->last_input_peak - DECAY_STEP) {
                        value = dialog->priv->last_input_peak - DECAY_STEP;
                }
        }

        dialog->priv->last_input_peak = value;

        adj = gvc_level_bar_get_peak_adjustment (GVC_LEVEL_BAR (dialog->priv->input_level_bar));
        if (value >= 0)
                gtk_adjustment_set_value (adj, value);
        else
                gtk_adjustment_set_value (adj, 0.0);
}

static void
update_input_settings (GvcMixerDialog *dialog)
{
        MateMixerStream            *stream;
        MateMixerStreamControl     *control;
        MateMixerStreamControlFlags flags;
        MateMixerSwitch            *port_switch;

        g_debug ("Updating input settings");

        if (dialog->priv->input_port_combo != NULL) {
                gtk_container_remove (GTK_CONTAINER (dialog->priv->input_settings_box),
                                      dialog->priv->input_port_combo);

                dialog->priv->input_port_combo = NULL;
        }

        /* Get the control currently associated with the input slider */
        control = gvc_channel_bar_get_control (GVC_CHANNEL_BAR (dialog->priv->input_bar));
        if (control == NULL)
                return;

        flags = mate_mixer_stream_control_get_flags (control);

        /* Enable level bar only if supported by the control */
        if (flags & MATE_MIXER_STREAM_CONTROL_HAS_MONITOR)
                g_signal_connect (G_OBJECT (control),
                                  "monitor-value",
                                  G_CALLBACK (on_stream_control_monitor_value),
                                  dialog);

        /* Get owning stream of the control */
        stream = mate_mixer_stream_control_get_stream (control);
        if (G_UNLIKELY (stream == NULL))
                return;

        /* Enable the port selector if the stream has one */
        port_switch = find_stream_port_switch (stream);
        if (port_switch != NULL) {
                dialog->priv->input_port_combo =
                        gvc_combo_box_new (port_switch, _("Co_nnector:"));

                gvc_combo_box_set_size_group (GVC_COMBO_BOX (dialog->priv->input_port_combo),
                                              dialog->priv->size_group,
                                              FALSE);

                gtk_box_pack_start (GTK_BOX (dialog->priv->input_settings_box),
                                    dialog->priv->input_port_combo,
                                    TRUE, TRUE, 0);

                gtk_widget_show (dialog->priv->input_port_combo);
        }
}

static void
on_stream_control_mute_notify (MateMixerStreamControl *control,
                               GParamSpec             *pspec,
                               GvcMixerDialog         *dialog)
{
        /* Stop monitoring the input stream when it gets muted */
        if (mate_mixer_stream_control_get_mute (control) == TRUE)
                mate_mixer_stream_control_set_monitor_enabled (control, FALSE);
        else
                mate_mixer_stream_control_set_monitor_enabled (control, TRUE);
}

static void
set_input_stream (GvcMixerDialog *dialog, MateMixerStream *stream)
{
        GtkTreeModel           *model;
        MateMixerStreamControl *control;

        control = gvc_channel_bar_get_control (GVC_CHANNEL_BAR (dialog->priv->input_bar));
        if (control != NULL) {
                /* Disconnect port switch of the previous stream */
                if (dialog->priv->input_port_combo != NULL) {
                        MateMixerSwitch        *swtch;

                        swtch = g_object_get_data (G_OBJECT (dialog->priv->input_port_combo),
                                                   "switch");
                        if (swtch != NULL)
                                g_signal_handlers_disconnect_by_data (G_OBJECT (swtch),
                                                                      dialog);
                }

                /* Disable monitoring of the previous control */
                g_signal_handlers_disconnect_by_func (G_OBJECT (control),
                                                      G_CALLBACK (on_stream_control_monitor_value),
                                                      dialog);

                mate_mixer_stream_control_set_monitor_enabled (control, FALSE);
        }

        bar_set_stream (dialog, dialog->priv->input_bar, stream);

        if (stream != NULL) {
                const GList *controls;
                guint page = gtk_notebook_get_current_page (GTK_NOTEBOOK (dialog->priv->notebook));

                controls = mate_mixer_context_list_stored_controls (dialog->priv->context);

                /* Move all stored controls to the newly selected default stream */
                while (controls != NULL) {
                        MateMixerStream *parent;

                        control = MATE_MIXER_STREAM_CONTROL (controls->data);
                        parent  = mate_mixer_stream_control_get_stream (control);

                        /* Prefer streamless controls to stay the way they are, forcing them to
                         * a particular owning stream would be wrong for eg. event controls */
                        if (parent != NULL && parent != stream) {
                                MateMixerDirection direction =
                                        mate_mixer_stream_get_direction (parent);

                                if (direction == MATE_MIXER_DIRECTION_INPUT)
                                        mate_mixer_stream_control_set_stream (control, stream);
                        }
                        controls = controls->next;
                }

                if (page == PAGE_INPUT) {
                        control = gvc_channel_bar_get_control (GVC_CHANNEL_BAR (dialog->priv->input_bar));

                        if (G_LIKELY (control != NULL))
                                mate_mixer_stream_control_set_monitor_enabled (control, TRUE);
                }

                /* Enable/disable the peak level monitor according to mute state */
                g_signal_connect (G_OBJECT (stream),
                                  "notify::mute",
                                  G_CALLBACK (on_stream_control_mute_notify),
                                  dialog);
        }

        model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->priv->input_treeview));
        update_default_tree_item (dialog, model, stream);

        update_input_settings (dialog);
}

static void
on_context_default_input_stream_notify (MateMixerContext *context,
                                        GParamSpec       *pspec,
                                        GvcMixerDialog   *dialog)
{
        MateMixerStream *stream;

        g_debug ("Default input stream has changed");

        stream = mate_mixer_context_get_default_input_stream (context);

        set_input_stream (dialog, stream);
}

static GtkWidget *
create_bar (GvcMixerDialog *dialog, gboolean use_size_group, gboolean symmetric)
{
        GtkWidget *bar;

        bar = gvc_channel_bar_new (NULL);

        if (use_size_group == TRUE)
                gvc_channel_bar_set_size_group (GVC_CHANNEL_BAR (bar),
                                                dialog->priv->size_group,
                                                symmetric);

        g_object_set (G_OBJECT (bar),
                      "orientation", GTK_ORIENTATION_HORIZONTAL,
                      "show-mute",   TRUE,
                      "show-icons",  TRUE,
                      "show-marks",  TRUE,
                      "extended",    TRUE, NULL);
        return bar;
}

static void
bar_set_stream (GvcMixerDialog  *dialog,
                GtkWidget       *bar,
                MateMixerStream *stream)
{
        MateMixerStreamControl *control = NULL;

        if (stream != NULL)
                control = mate_mixer_stream_get_default_control (stream);

        bar_set_stream_control (dialog, bar, control);
}

static void
bar_set_stream_control (GvcMixerDialog         *dialog,
                        GtkWidget              *bar,
                        MateMixerStreamControl *control)
{
        const gchar            *name;
        MateMixerStreamControl *previous;

        previous = gvc_channel_bar_get_control (GVC_CHANNEL_BAR (bar));
        if (previous == control)
                return;

        if (previous != NULL) {
                name = mate_mixer_stream_control_get_name (previous);

                g_debug ("Removing stream control %s from bar %s",
                         name,
                         gvc_channel_bar_get_name (GVC_CHANNEL_BAR (bar)));

                g_signal_handlers_disconnect_by_data (G_OBJECT (previous), dialog);

                /* This may not do anything because we no longer have the information
                 * about the owning stream, in case it was an input stream, make
                 * sure to disconnected from the peak level monitor */
                mate_mixer_stream_control_set_monitor_enabled (previous, FALSE);

                g_hash_table_remove (dialog->priv->bars, name);
        }

        gvc_channel_bar_set_control (GVC_CHANNEL_BAR (bar), control);

        if (control != NULL) {
                name = mate_mixer_stream_control_get_name (control);

                g_debug ("Setting stream control %s for bar %s",
                         name,
                         gvc_channel_bar_get_name (GVC_CHANNEL_BAR (bar)));

                g_hash_table_insert (dialog->priv->bars,
                                     (gpointer) name,
                                     bar);

                gtk_widget_set_sensitive (GTK_WIDGET (bar), TRUE);
        } else
                gtk_widget_set_sensitive (GTK_WIDGET (bar), TRUE);
}

static void
add_application_control (GvcMixerDialog *dialog, MateMixerStreamControl *control)
{
        MateMixerStream                *stream;
        MateMixerStreamControlMediaRole media_role;
        MateMixerAppInfo               *info;
        MateMixerDirection              direction = MATE_MIXER_DIRECTION_UNKNOWN;
        GtkWidget                      *bar;
        const gchar                    *app_id;
        const gchar                    *app_name;
        const gchar                    *app_icon;

        media_role = mate_mixer_stream_control_get_media_role (control);

        /* Add stream to the applications page, but make sure the stream qualifies
         * for the inclusion */
        info = mate_mixer_stream_control_get_app_info (control);
        if (info == NULL)
                return;

        /* Skip streams with roles we don't care about */
        if (media_role == MATE_MIXER_STREAM_CONTROL_MEDIA_ROLE_EVENT ||
            media_role == MATE_MIXER_STREAM_CONTROL_MEDIA_ROLE_TEST ||
            media_role == MATE_MIXER_STREAM_CONTROL_MEDIA_ROLE_ABSTRACT ||
            media_role == MATE_MIXER_STREAM_CONTROL_MEDIA_ROLE_FILTER)
                return;

        app_id = mate_mixer_app_info_get_id (info);

        /* These applications may have associated streams because they do peak
         * level monitoring, skip these too */
        if (!g_strcmp0 (app_id, "org.mate.VolumeControl") ||
            !g_strcmp0 (app_id, "org.gnome.VolumeControl") ||
            !g_strcmp0 (app_id, "org.PulseAudio.pavucontrol"))
                return;

        app_name = mate_mixer_app_info_get_name (info);
        if (app_name == NULL)
                app_name = mate_mixer_stream_control_get_label (control);
        if (app_name == NULL)
                app_name = mate_mixer_stream_control_get_name (control);
        if (G_UNLIKELY (app_name == NULL))
                return;

        bar = create_bar (dialog, FALSE, FALSE);

        g_object_set (G_OBJECT (bar),
                      "show-marks", FALSE,
                      "extended", FALSE,
                      NULL);

        /* By default channel bars use speaker icons, use microphone icons
         * instead for recording applications */
        stream = mate_mixer_stream_control_get_stream (control);
        if (stream != NULL)
                direction = mate_mixer_stream_get_direction (stream);

        if (direction == MATE_MIXER_DIRECTION_INPUT)
                g_object_set (G_OBJECT (bar),
                              "low-icon-name", "audio-input-microphone-low",
                              "high-icon-name", "audio-input-microphone-high",
                              NULL);

        app_icon = mate_mixer_app_info_get_icon (info);
        if (app_icon == NULL) {
                if (direction == MATE_MIXER_DIRECTION_INPUT)
                        app_icon = "audio-input-microphone";
                else
                        app_icon = "applications-multimedia";
        }

        gvc_channel_bar_set_name (GVC_CHANNEL_BAR (bar), app_name);
        gvc_channel_bar_set_icon_name (GVC_CHANNEL_BAR (bar), app_icon);

        gtk_box_pack_start (GTK_BOX (dialog->priv->applications_box),
                            bar,
                            FALSE, FALSE, 12);

        bar_set_stream_control (dialog, bar, control);
        dialog->priv->num_apps++;

        gtk_widget_hide (dialog->priv->no_apps_label);
        gtk_widget_show (bar);
}

static void
on_stream_control_added (MateMixerStream *stream,
                         const gchar     *name,
                         GvcMixerDialog  *dialog)
{
        MateMixerStreamControl    *control;
        MateMixerStreamControlRole role;

        control = mate_mixer_stream_get_control (stream, name);
        if (G_UNLIKELY (control == NULL))
                return;

        role = mate_mixer_stream_control_get_role (control);

        if (role == MATE_MIXER_STREAM_CONTROL_ROLE_APPLICATION)
                add_application_control (dialog, control);
}

static void
on_stream_control_removed (MateMixerStream *stream,
                           const gchar     *name,
                           GvcMixerDialog  *dialog)
{
        MateMixerStreamControl *control;

        control = gvc_channel_bar_get_control (GVC_CHANNEL_BAR (dialog->priv->input_bar));
        if (control != NULL) {
                const gchar *input_name = mate_mixer_stream_control_get_name (control);

                if (strcmp (name, input_name) == 0) {
                        // XXX probably can't even happen, but handle it somehow
                        return;
                }
        }

        control = gvc_channel_bar_get_control (GVC_CHANNEL_BAR (dialog->priv->output_bar));
        if (control != NULL) {
                const gchar *input_name = mate_mixer_stream_control_get_name (control);

                if (strcmp (name, input_name) == 0) {
                        // XXX probably can't even happen, but handle it somehow
                        return;
                }
        }

        /* No way to be sure that it is an application control, but we don't have
         * any other than application bars that could match the name */
        remove_application_control (dialog, name);
}

static void
add_stream (GvcMixerDialog *dialog, MateMixerStream *stream)
{
        GtkTreeModel      *model = NULL;
        GtkTreeIter        iter;
        const gchar       *speakers = NULL;
        const GList       *controls;
        gboolean           is_default = FALSE;
        MateMixerDirection direction;

        direction = mate_mixer_stream_get_direction (stream);

        if (direction == MATE_MIXER_DIRECTION_INPUT) {
                MateMixerStream *input;

                input = mate_mixer_context_get_default_input_stream (dialog->priv->context);
                if (stream == input) {
                        bar_set_stream (dialog, dialog->priv->input_bar, stream);

                        update_input_settings (dialog);
                        is_default = TRUE;
                }
                model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->priv->input_treeview));
        }
        else if (direction == MATE_MIXER_DIRECTION_OUTPUT) {
                MateMixerStream        *output;
                MateMixerStreamControl *control;

                output = mate_mixer_context_get_default_output_stream (dialog->priv->context);
                if (stream == output) {
                        bar_set_stream (dialog, dialog->priv->output_bar, stream);

                        update_output_settings (dialog);
                        is_default = TRUE;
                }
                model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->priv->output_treeview));

                control = mate_mixer_stream_get_default_control (stream);
                if (G_LIKELY (control != NULL))
                        speakers = gvc_channel_map_to_pretty_string (control);
        }

        controls = mate_mixer_stream_list_controls (stream);
        while (controls != NULL) {
                MateMixerStreamControl    *control = MATE_MIXER_STREAM_CONTROL (controls->data);
                MateMixerStreamControlRole role;

                role = mate_mixer_stream_control_get_role (control);

                if (role == MATE_MIXER_STREAM_CONTROL_ROLE_APPLICATION)
                        add_application_control (dialog, control);

                controls = controls->next;
        }

        if (model != NULL) {
                const gchar *name;
                const gchar *label;

                name  = mate_mixer_stream_get_name (stream);
                label = mate_mixer_stream_get_label (stream);

                gtk_list_store_append (GTK_LIST_STORE (model), &iter);
                gtk_list_store_set (GTK_LIST_STORE (model),
                                    &iter,
                                    NAME_COLUMN, name,
                                    LABEL_COLUMN, label,
                                    ACTIVE_COLUMN, is_default,
                                    SPEAKERS_COLUMN, speakers,
                                    -1);
        }

        // XXX find a way to disconnect when removed
        g_signal_connect (G_OBJECT (stream),
                          "control-added",
                          G_CALLBACK (on_stream_control_added),
                          dialog);
        g_signal_connect (G_OBJECT (stream),
                          "control-removed",
                          G_CALLBACK (on_stream_control_removed),
                          dialog);
}

static void
update_device_test_visibility (GvcMixerDialog *dialog)
{
        MateMixerDevice *device;
        MateMixerStream *stream;

        device = g_object_get_data (G_OBJECT (dialog->priv->hw_profile_combo), "device");
        if (G_UNLIKELY (device == NULL)) {
                return;
        }

        stream = find_device_test_stream (dialog, device);

        g_object_set (G_OBJECT (dialog->priv->hw_profile_combo),
                      "show-button", (stream != NULL),
                      NULL);
}

static void
on_context_stream_added (MateMixerContext *context,
                         const gchar      *name,
                         GvcMixerDialog   *dialog)
{
        MateMixerStream   *stream;
        MateMixerDirection direction;
        GtkWidget         *bar;

        stream = mate_mixer_context_get_stream (context, name);
        if (G_UNLIKELY (stream == NULL))
                return;

        direction = mate_mixer_stream_get_direction (stream);

        /* If the newly added stream belongs to the currently selected device and
         * the test button is hidden, this stream may be the one to allow the
         * sound test and therefore we may need to enable the button */
        if (dialog->priv->hw_profile_combo != NULL && direction == MATE_MIXER_DIRECTION_OUTPUT) {
                MateMixerDevice *device1;
                MateMixerDevice *device2;

                device1 = mate_mixer_stream_get_device (stream);
                device2 = g_object_get_data (G_OBJECT (dialog->priv->hw_profile_combo),
                                             "device");

                if (device1 == device2) {
                        gboolean show_button;

                        g_object_get (G_OBJECT (dialog->priv->hw_profile_combo),
                                      "show-button", &show_button,
                                      NULL);

                        if (show_button == FALSE)
                                update_device_test_visibility (dialog);
                }
        }

        bar = g_hash_table_lookup (dialog->priv->bars, name);
        if (G_UNLIKELY (bar != NULL))
                return;

        add_stream (dialog, stream);
}

static void
remove_stream (GvcMixerDialog *dialog, const gchar *name)
{
        GtkWidget    *bar;
        GtkTreeIter   iter;
        GtkTreeModel *model;

        bar = g_hash_table_lookup (dialog->priv->bars, name);

        if (bar != NULL) {
                g_debug ("Removing stream %s from bar %s",
                         name,
                         gvc_channel_bar_get_name (GVC_CHANNEL_BAR (bar)));

                bar_set_stream (dialog, bar, NULL);
        }

        /* Remove from any models */
        model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->priv->output_treeview));
        if (find_tree_item_by_name (GTK_TREE_MODEL (model),
                                    name,
                                    NAME_COLUMN,
                                    &iter) == TRUE)
                gtk_list_store_remove (GTK_LIST_STORE (model), &iter);

        model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->priv->input_treeview));
        if (find_tree_item_by_name (GTK_TREE_MODEL (model),
                                    name,
                                    NAME_COLUMN,
                                    &iter) == TRUE)
                gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
}

static void
remove_application_control (GvcMixerDialog *dialog, const gchar *name)
{
        GtkWidget *bar;

        bar = g_hash_table_lookup (dialog->priv->bars, name);
        if (G_UNLIKELY (bar == NULL))
                return;

        g_debug ("Removing application stream %s", name);

        /* We could call bar_set_stream_control here, but that would pointlessly
         * invalidate the channel bar, so just remove it ourselves */
        g_hash_table_remove (dialog->priv->bars, name);

        gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (bar)), bar);

        if (G_UNLIKELY (dialog->priv->num_apps <= 0)) {
                g_warn_if_reached ();
                dialog->priv->num_apps = 1;
        }

        dialog->priv->num_apps--;

        if (dialog->priv->num_apps == 0)
                gtk_widget_show (dialog->priv->no_apps_label);
}

static void
on_context_stream_removed (MateMixerContext *context,
                           const gchar      *name,
                           GvcMixerDialog   *dialog)
{
        if (dialog->priv->hw_profile_combo != NULL) {
                gboolean show_button;

                g_object_get (G_OBJECT (dialog->priv->hw_profile_combo),
                              "show-button", &show_button,
                              NULL);

                if (show_button == TRUE)
                        update_device_test_visibility (dialog);
        }

        remove_stream (dialog, name);
}

static void
on_context_stored_control_added (MateMixerContext *context,
                                 const gchar      *name,
                                 GvcMixerDialog   *dialog)
{
        MateMixerStreamControl         *control;
        MateMixerStreamControlMediaRole media_role;

        control = MATE_MIXER_STREAM_CONTROL (mate_mixer_context_get_stored_control (context, name));
        if (G_UNLIKELY (control == NULL))
                return;

        media_role = mate_mixer_stream_control_get_media_role (control);

        if (media_role == MATE_MIXER_STREAM_CONTROL_MEDIA_ROLE_EVENT)
                bar_set_stream_control (dialog, dialog->priv->effects_bar, control);
}

static void
on_context_stored_control_removed (MateMixerContext *context,
                                   const gchar      *name,
                                   GvcMixerDialog   *dialog)
{
        GtkWidget *bar;

        bar = g_hash_table_lookup (dialog->priv->bars, name);

        if (bar != NULL) {
                /* We only use a stored control in the effects bar */
                if (G_UNLIKELY (bar != dialog->priv->effects_bar)) {
                        g_warn_if_reached ();
                        return;
                }

                bar_set_stream (dialog, bar, NULL);
        }
}

static gchar *
device_status (MateMixerDevice *device)
{
        guint        inputs = 0;
        guint        outputs = 0;
        gchar       *inputs_str = NULL;
        gchar       *outputs_str = NULL;
        const GList *streams;

        /* Get number of input and output streams in the device */
        streams = mate_mixer_device_list_streams (device);
        while (streams != NULL) {
                MateMixerStream   *stream = MATE_MIXER_STREAM (streams->data);
                MateMixerDirection direction;

                direction = mate_mixer_stream_get_direction (stream);

                if (direction == MATE_MIXER_DIRECTION_INPUT)
                        inputs++;
                else if (direction == MATE_MIXER_DIRECTION_OUTPUT)
                        outputs++;

                streams = streams->next;
        }

        if (inputs == 0 && outputs == 0) {
                /* translators:
                 * The device has been disabled */
                return g_strdup (_("Disabled"));
        }

        if (outputs > 0) {
                /* translators:
                 * The number of sound outputs on a particular device */
                outputs_str = g_strdup_printf (ngettext ("%u Output",
                                                         "%u Outputs",
                                                          outputs),
                                               outputs);
        }

        if (inputs > 0) {
                /* translators:
                 * The number of sound inputs on a particular device */
                inputs_str = g_strdup_printf (ngettext ("%u Input",
                                                        "%u Inputs",
                                                        inputs),
                                              inputs);
        }

        if (inputs_str != NULL && outputs_str != NULL) {
                gchar *ret = g_strdup_printf ("%s / %s",
                                              outputs_str,
                                              inputs_str);
                g_free (outputs_str);
                g_free (inputs_str);
                return ret;
        }

        if (inputs_str != NULL)
                return inputs_str;

        return outputs_str;
}

static void
update_device_info (GvcMixerDialog *dialog, MateMixerDevice *device)
{
        GtkTreeModel    *model = NULL;
        GtkTreeIter      iter;
        const gchar     *label;
        const gchar     *profile_label = NULL;
        gchar           *status;
        MateMixerSwitch *profile_switch;

        model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->priv->hw_treeview));

        if (find_tree_item_by_name (model,
                                    mate_mixer_device_get_name (device),
                                    HW_NAME_COLUMN,
                                    &iter) == FALSE)
                return;

        label = mate_mixer_device_get_label (device);

        profile_switch = find_device_profile_switch (device);
        if (profile_switch != NULL) {
                MateMixerSwitchOption *active;

                active = mate_mixer_switch_get_active_option (profile_switch);
                if (G_LIKELY (active != NULL))
                        profile_label = mate_mixer_switch_option_get_label (active);
        }

        status = device_status (device);

        gtk_list_store_set (GTK_LIST_STORE (model),
                            &iter,
                            HW_LABEL_COLUMN, label,
                            HW_PROFILE_COLUMN, profile_label,
                            HW_STATUS_COLUMN, status,
                            -1);
        g_free (status);
}

static void
on_device_profile_active_option_notify (MateMixerDeviceSwitch *swtch,
                                        GParamSpec            *pspec,
                                        GvcMixerDialog        *dialog)
{
        MateMixerDevice *device;

        device = mate_mixer_device_switch_get_device (swtch);

        update_device_info (dialog, device);
}

static void
add_device (GvcMixerDialog *dialog, MateMixerDevice *device)
{
        GtkTreeModel    *model;
        GtkTreeIter      iter;
        GIcon           *icon;
        const gchar     *name;
        const gchar     *label;
        gchar           *status;
        const gchar     *profile_label = NULL;
        MateMixerSwitch *profile_switch;

        model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->priv->hw_treeview));

        name  = mate_mixer_device_get_name (device);
        label = mate_mixer_device_get_label (device);

        if (find_tree_item_by_name (GTK_TREE_MODEL (model),
                                    name,
                                    HW_NAME_COLUMN,
                                    &iter) == FALSE)
                gtk_list_store_append (GTK_LIST_STORE (model), &iter);

        icon = g_themed_icon_new_with_default_fallbacks (mate_mixer_device_get_icon (device));

        profile_switch = find_device_profile_switch (device);
        if (profile_switch != NULL) {
                MateMixerSwitchOption *active;

                active = mate_mixer_switch_get_active_option (profile_switch);
                if (G_LIKELY (active != NULL))
                        profile_label = mate_mixer_switch_option_get_label (active);

                g_signal_connect (G_OBJECT (profile_switch),
                                  "notify::active-option",
                                  G_CALLBACK (on_device_profile_active_option_notify),
                                  dialog);
        }

        status = device_status (device);

        gtk_list_store_set (GTK_LIST_STORE (model),
                            &iter,
                            HW_NAME_COLUMN, name,
                            HW_LABEL_COLUMN, label,
                            HW_ICON_COLUMN, icon,
                            HW_PROFILE_COLUMN, profile_label,
                            HW_STATUS_COLUMN, status,
                            -1);
        g_free (status);

}

static void
on_context_device_added (MateMixerContext *context, const gchar *name, GvcMixerDialog *dialog)
{
        MateMixerDevice *device;

        device = mate_mixer_context_get_device (context, name);
        if (G_UNLIKELY (device == NULL))
                return;

        add_device (dialog, device);
}

static void
on_context_device_removed (MateMixerContext *context,
                           const gchar      *name,
                           GvcMixerDialog   *dialog)
{
        GtkTreeIter   iter;
        GtkTreeModel *model;

        /* Remove from the device model */
        model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->priv->hw_treeview));

        if (find_tree_item_by_name (GTK_TREE_MODEL (model),
                                    name,
                                    HW_NAME_COLUMN,
                                    &iter) == TRUE)
                gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
}

static void
make_label_bold (GtkLabel *label)
{
        PangoFontDescription *font_desc;

        font_desc = pango_font_description_new ();

        pango_font_description_set_weight (font_desc, PANGO_WEIGHT_BOLD);

        /* This will only affect the weight of the font, the rest is
         * from the current state of the widget, which comes from the
         * theme or user prefs, since the font desc only has the
         * weight flag turned on. */
        PangoAttrList *attrs = pango_attr_list_new ();
        PangoAttribute *font_desc_attr = pango_attr_font_desc_new (font_desc);
        pango_attr_list_insert (attrs, font_desc_attr);
        gtk_label_set_attributes (label, attrs);
        pango_attr_list_unref (attrs);

        pango_font_description_free (font_desc);
}

static void
on_input_radio_toggled (GtkCellRendererToggle *renderer,
                        gchar                 *path_str,
                        GvcMixerDialog        *dialog)
{
        GtkTreeModel *model;
        GtkTreeIter   iter;
        GtkTreePath  *path;
        gboolean      toggled = FALSE;
        gchar        *name = NULL;

        model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->priv->input_treeview));
        path  = gtk_tree_path_new_from_string (path_str);

        gtk_tree_model_get_iter (model, &iter, path);
        gtk_tree_path_free (path);

        gtk_tree_model_get (model, &iter,
                            NAME_COLUMN, &name,
                            ACTIVE_COLUMN, &toggled,
                            -1);
        if (toggled ^ 1) {
                MateMixerStream      *stream;
                MateMixerBackendFlags flags;

                stream = mate_mixer_context_get_stream (dialog->priv->context, name);
                if (G_UNLIKELY (stream == NULL)) {
                        g_warn_if_reached ();
                        g_free (name);
                        return;
                }

                g_debug ("Default input stream selection changed to %s", name);

                // XXX cache this
                flags = mate_mixer_context_get_backend_flags (dialog->priv->context);

                if (flags & MATE_MIXER_BACKEND_CAN_SET_DEFAULT_INPUT_STREAM)
                        mate_mixer_context_set_default_input_stream (dialog->priv->context, stream);
                else
                        set_input_stream (dialog, stream);
        }
        g_free (name);
}

static void
on_output_radio_toggled (GtkCellRendererToggle *renderer,
                         gchar                 *path_str,
                         GvcMixerDialog        *dialog)
{
        GtkTreeModel *model;
        GtkTreeIter   iter;
        GtkTreePath  *path;
        gboolean      toggled = FALSE;
        gchar        *name = NULL;

        model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->priv->output_treeview));
        path  = gtk_tree_path_new_from_string (path_str);

        gtk_tree_model_get_iter (model, &iter, path);
        gtk_tree_path_free (path);

        gtk_tree_model_get (model, &iter,
                            NAME_COLUMN, &name,
                            ACTIVE_COLUMN, &toggled,
                            -1);
        if (toggled ^ 1) {
                MateMixerStream      *stream;
                MateMixerBackendFlags flags;

                stream = mate_mixer_context_get_stream (dialog->priv->context, name);
                if (G_UNLIKELY (stream == NULL)) {
                        g_warn_if_reached ();
                        g_free (name);
                        return;
                }

                g_debug ("Default output stream selection changed to %s", name);

                // XXX cache this
                flags = mate_mixer_context_get_backend_flags (dialog->priv->context);

                if (flags & MATE_MIXER_BACKEND_CAN_SET_DEFAULT_OUTPUT_STREAM)
                        mate_mixer_context_set_default_output_stream (dialog->priv->context, stream);
                else
                        set_output_stream (dialog, stream);
        }
        g_free (name);
}

static void
stream_name_to_text (GtkTreeViewColumn *column,
                     GtkCellRenderer   *cell,
                     GtkTreeModel      *model,
                     GtkTreeIter       *iter,
                     gpointer           user_data)
{
        gchar *label;
        gchar *speakers;

        gtk_tree_model_get (model, iter,
                            LABEL_COLUMN, &label,
                            SPEAKERS_COLUMN, &speakers,
                            -1);

        if (speakers != NULL) {
                gchar *str = g_strdup_printf ("%s\n<i>%s</i>",
                                              label,
                                              speakers);

                g_object_set (cell, "markup", str, NULL);
                g_free (str);
                g_free (speakers);
        } else {
                g_object_set (cell, "text", label, NULL);
        }

        g_free (label);
}

static gint
compare_stream_treeview_items (GtkTreeModel *model,
                               GtkTreeIter  *a,
                               GtkTreeIter  *b,
                               gpointer      user_data)
{
        gchar *desc_a = NULL;
        gchar *desc_b = NULL;
        gint   result;

        gtk_tree_model_get (model, a,
                            LABEL_COLUMN, &desc_a,
                            -1);
        gtk_tree_model_get (model, b,
                            LABEL_COLUMN, &desc_b,
                            -1);

        if (desc_a == NULL) {
                g_free (desc_b);
                return -1;
        }
        if (desc_b == NULL) {
                g_free (desc_a);
                return 1;
        }

        result = g_ascii_strcasecmp (desc_a, desc_b);

        g_free (desc_a);
        g_free (desc_b);
        return result;
}

static GtkWidget *
create_stream_treeview (GvcMixerDialog *dialog, GCallback on_toggled)
{
        GtkWidget         *treeview;
        GtkListStore      *store;
        GtkCellRenderer   *renderer;
        GtkTreeViewColumn *column;

        treeview = gtk_tree_view_new ();
        gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (treeview), FALSE);

        store = gtk_list_store_new (NUM_COLUMNS,
                                    G_TYPE_ICON,
                                    G_TYPE_STRING,
                                    G_TYPE_STRING,
                                    G_TYPE_BOOLEAN,
                                    G_TYPE_STRING);

        gtk_tree_view_set_model (GTK_TREE_VIEW (treeview),
                                 GTK_TREE_MODEL (store));

        renderer = gtk_cell_renderer_toggle_new ();
        gtk_cell_renderer_toggle_set_radio (GTK_CELL_RENDERER_TOGGLE (renderer), TRUE);

        column = gtk_tree_view_column_new_with_attributes (NULL,
                                                           renderer,
                                                           "active", ACTIVE_COLUMN,
                                                           NULL);

        gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);

        g_signal_connect (G_OBJECT (renderer),
                          "toggled",
                          G_CALLBACK (on_toggled),
                          dialog);

        gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (treeview), -1,
                                                    _("Name"),
                                                    gtk_cell_renderer_text_new (),
                                                    stream_name_to_text,
                                                    NULL, NULL);

        /* Keep the list of streams sorted by the name */
        gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
                                              LABEL_COLUMN,
                                              GTK_SORT_ASCENDING);

        gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
                                         LABEL_COLUMN,
                                         compare_stream_treeview_items,
                                         NULL, NULL);
        return treeview;
}

static void
on_device_profile_changing (GvcComboBox           *combobox,
                            MateMixerSwitchOption *option,
                            GvcMixerDialog        *dialog)
{
        g_debug ("Changing device profile");
        // TODO
}

static void
on_test_speakers_clicked (GvcComboBox *widget, GvcMixerDialog *dialog)
{
        GtkWidget       *d,
                        *test,
                        *container;
        gchar           *title;
        MateMixerDevice *device;
        MateMixerStream *stream;

        device = g_object_get_data (G_OBJECT (widget), "device");
        if (G_UNLIKELY (device == NULL)) {
                g_warn_if_reached ();
                return;
        }

        stream = find_device_test_stream (dialog, device);
        if (G_UNLIKELY (stream == NULL)) {
                g_warn_if_reached ();
                return;
        }

        title = g_strdup_printf (_("Speaker Testing for %s"),
                                 mate_mixer_device_get_label (device));

        d = gtk_dialog_new_with_buttons (title,
                                         GTK_WINDOW (dialog),
                                         GTK_DIALOG_MODAL |
                                         GTK_DIALOG_DESTROY_WITH_PARENT,
                                         "gtk-close",
                                         GTK_RESPONSE_CLOSE,
                                         NULL);
        g_free (title);

        gtk_window_set_resizable (GTK_WINDOW (d), FALSE);

        test = gvc_speaker_test_new (stream);
        gtk_widget_show (test);

        container = gtk_dialog_get_content_area (GTK_DIALOG (d));
        gtk_container_add (GTK_CONTAINER (container), test);

        gtk_dialog_run (GTK_DIALOG (d));
        gtk_widget_destroy (d);
}

static void
on_device_selection_changed (GtkTreeSelection *selection, GvcMixerDialog *dialog)
{
        GtkTreeIter          iter;
        gchar               *name;
        MateMixerDevice     *device;
        MateMixerSwitch     *profile_switch;

        g_debug ("Device selection changed");

        if (dialog->priv->hw_profile_combo != NULL) {
                gtk_container_remove (GTK_CONTAINER (dialog->priv->hw_settings_box),
                                      dialog->priv->hw_profile_combo);

                dialog->priv->hw_profile_combo = NULL;
        }

        if (gtk_tree_selection_get_selected (selection, NULL, &iter) == FALSE)
                return;

        gtk_tree_model_get (gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->priv->hw_treeview)),
                            &iter,
                            HW_NAME_COLUMN, &name,
                            -1);

        device = mate_mixer_context_get_device (dialog->priv->context, name);
        if (G_UNLIKELY (device == NULL)) {
                g_warn_if_reached ();
                g_free (name);
                return;
        }
        g_free (name);

        /* Profile/speaker test combo */
        profile_switch = find_device_profile_switch (device);
        if (profile_switch != NULL) {
                dialog->priv->hw_profile_combo =
                        gvc_combo_box_new (profile_switch, _("_Profile:"));

                g_object_set (G_OBJECT (dialog->priv->hw_profile_combo),
                              "button-label", _("Test Speakers"),
                              NULL);

                g_signal_connect (G_OBJECT (dialog->priv->hw_profile_combo),
                                  "changing",
                                  G_CALLBACK (on_device_profile_changing),
                                  dialog);

                g_signal_connect (G_OBJECT (dialog->priv->hw_profile_combo),
                                  "button-clicked",
                                  G_CALLBACK (on_test_speakers_clicked),
                                  dialog);

                g_object_set_data_full (G_OBJECT (dialog->priv->hw_profile_combo),
                                        "device",
                                        g_object_ref (device),
                                        g_object_unref);

                gtk_box_pack_start (GTK_BOX (dialog->priv->hw_settings_box),
                                    dialog->priv->hw_profile_combo,
                                    TRUE, TRUE, 6);

                /* Enable the speaker test button if the selected device
                 * is capable of sound output */
                update_device_test_visibility (dialog);

                gtk_widget_show (dialog->priv->hw_profile_combo);
        }
}

static void
on_notebook_switch_page (GtkNotebook    *notebook,
                         GtkWidget      *page,
                         guint           page_num,
                         GvcMixerDialog *dialog)
{
        MateMixerStreamControl *control;

        // XXX because this is called too early in constructor
        if (G_UNLIKELY (dialog->priv->input_bar == NULL))
                return;

        control = gvc_channel_bar_get_control (GVC_CHANNEL_BAR (dialog->priv->input_bar));
        if (control == NULL)
                return;

        if (page_num == PAGE_INPUT)
                mate_mixer_stream_control_set_monitor_enabled (control, TRUE);
        else
                mate_mixer_stream_control_set_monitor_enabled (control, FALSE);
}

static void
device_name_to_text (GtkTreeViewColumn *column,
                     GtkCellRenderer   *cell,
                     GtkTreeModel      *model,
                     GtkTreeIter       *iter,
                     gpointer           user_data)
{
        gchar *label = NULL;
        gchar *profile = NULL;
        gchar *status = NULL;

        gtk_tree_model_get (model, iter,
                            HW_LABEL_COLUMN, &label,
                            HW_PROFILE_COLUMN, &profile,
                            HW_STATUS_COLUMN, &status,
                            -1);

        if (G_LIKELY (status != NULL)) {
                gchar *str;

                if (profile != NULL)
                        str = g_strdup_printf ("%s\n<i>%s</i>\n<i>%s</i>",
                                               label,
                                               status,
                                               profile);
                else
                        str = g_strdup_printf ("%s\n<i>%s</i>",
                                               label,
                                               status);

                g_object_set (cell, "markup", str, NULL);
                g_free (str);
                g_free (profile);
                g_free (status);
        } else
                g_object_set (cell, "text", label, NULL);

        g_free (label);
}

static gint
compare_device_treeview_items (GtkTreeModel *model,
                               GtkTreeIter  *a,
                               GtkTreeIter  *b,
                               gpointer      user_data)
{
        gchar *desc_a = NULL;
        gchar *desc_b = NULL;
        gint   result;

        gtk_tree_model_get (model, a,
                            HW_LABEL_COLUMN, &desc_a,
                            -1);
        gtk_tree_model_get (model, b,
                            HW_LABEL_COLUMN, &desc_b,
                            -1);

        result = g_ascii_strcasecmp (desc_a, desc_b);

        g_free (desc_a);
        g_free (desc_b);
        return result;
}

static GtkWidget *
create_device_treeview (GvcMixerDialog *dialog, GCallback on_changed)
{
        GtkWidget         *treeview;
        GtkListStore      *store;
        GtkCellRenderer   *renderer;
        GtkTreeViewColumn *column;
        GtkTreeSelection  *selection;

        treeview = gtk_tree_view_new ();
        gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (treeview), FALSE);

        selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview));
        g_signal_connect (G_OBJECT (selection),
                          "changed",
                          on_changed,
                          dialog);

        store = gtk_list_store_new (HW_NUM_COLUMNS,
                                    G_TYPE_ICON,
                                    G_TYPE_STRING,
                                    G_TYPE_STRING,
                                    G_TYPE_STRING,
                                    G_TYPE_STRING);

        gtk_tree_view_set_model (GTK_TREE_VIEW (treeview),
                                 GTK_TREE_MODEL (store));

        renderer = gtk_cell_renderer_pixbuf_new ();
        g_object_set (G_OBJECT (renderer),
                      "stock-size",
                      GTK_ICON_SIZE_DIALOG,
                      NULL);

        column = gtk_tree_view_column_new_with_attributes (NULL,
                                                           renderer,
                                                           "gicon", HW_ICON_COLUMN,
                                                           NULL);

        gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
        gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (treeview), -1,
                                                    _("Name"),
                                                    gtk_cell_renderer_text_new (),
                                                    device_name_to_text,
                                                    NULL, NULL);

        /* Keep the list of streams sorted by the name */
        gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
                                              HW_LABEL_COLUMN,
                                              GTK_SORT_ASCENDING);

        gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
                                         HW_LABEL_COLUMN,
                                         compare_device_treeview_items,
                                         NULL, NULL);
        return treeview;
}

static void
dialog_accel_cb (GtkAccelGroup    *accelgroup,
                 GObject          *object,
                 guint             key,
                 GdkModifierType   mod,
                 GvcMixerDialog   *self)
{
        gint num = -1;
        gint i;

        for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
                if (tab_accel_keys[i] == key) {
                        num = i;
                        break;
                }
        }

        if (num != -1)
                gtk_notebook_set_current_page (GTK_NOTEBOOK (self->priv->notebook), num);
}

static void
create_page_effects (GvcMixerDialog *self)
{
        GtkWidget *box;
        GtkWidget *label;
        GtkWidget *chooser;

        box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
        gtk_container_set_border_width (GTK_CONTAINER (box), 12);

        label = gtk_label_new (_("Sound Effects"));
        gtk_notebook_append_page (GTK_NOTEBOOK (self->priv->notebook),
                                  box,
                                  label);

        /*
         * Create a volume slider for the sound effect sounds.
         *
         * Only look for a stored control because regular controls only exist
         * for short time periods when an event sound is played.
         */
        if (self->priv->backend_flags & MATE_MIXER_BACKEND_HAS_STORED_CONTROLS) {
                GvcChannelBar *bar;
                const GList   *list;

                bar = GVC_CHANNEL_BAR (create_bar (self, TRUE, TRUE));

                gtk_box_pack_start (GTK_BOX (box),
                                    GTK_WIDGET (bar),
                                    FALSE, FALSE, 0);

                gvc_channel_bar_set_show_marks (bar, FALSE);
                gvc_channel_bar_set_extended (bar, FALSE);
                gvc_channel_bar_set_name (bar, _("_Alert volume: "));

                /* Find an event role stored control */
                list = mate_mixer_context_list_stored_controls (self->priv->context);
                while (list != NULL) {
                        MateMixerStreamControl *control = MATE_MIXER_STREAM_CONTROL (list->data);
                        MateMixerStreamControlMediaRole media_role;

                        media_role = mate_mixer_stream_control_get_media_role (control);

                        if (media_role == MATE_MIXER_STREAM_CONTROL_MEDIA_ROLE_EVENT) {
                                bar_set_stream_control (self, GTK_WIDGET (bar), control);
                                break;
                        }

                        list = list->next;
                }

                self->priv->effects_bar = GTK_WIDGET (bar);
        }

        chooser = gvc_sound_theme_chooser_new ();
        gtk_box_pack_start (GTK_BOX (box),
                            chooser,
                            TRUE, TRUE, 6);
}

static GObject *
gvc_mixer_dialog_constructor (GType                  type,
                              guint                  n_construct_properties,
                              GObjectConstructParam *construct_params)
{
        GObject          *object;
        GvcMixerDialog   *self;
        GtkWidget        *main_vbox;
        GtkWidget        *label;
        GtkWidget        *box;
        GtkWidget        *scroll_box;
        GtkWidget        *sbox;
        GtkWidget        *ebox;
        GtkTreeSelection *selection;
        GtkAccelGroup    *accel_group;
        GtkTreeIter       iter;
        gint              i;
        const GList      *list;
        GClosure         *closure = NULL;

        object = G_OBJECT_CLASS (gvc_mixer_dialog_parent_class)->constructor (type,
                                                                              n_construct_properties,
                                                                              construct_params);

        self = GVC_MIXER_DIALOG (object);

        gtk_dialog_add_button (GTK_DIALOG (self), "gtk-close", GTK_RESPONSE_OK);

        main_vbox = gtk_dialog_get_content_area (GTK_DIALOG (self));
        gtk_box_set_spacing (GTK_BOX (main_vbox), 2);

        gtk_container_set_border_width (GTK_CONTAINER (self), 6);

        self->priv->output_stream_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
        gtk_widget_set_margin_top (self->priv->output_stream_box, 12);

        gtk_box_pack_start (GTK_BOX (main_vbox),
                            self->priv->output_stream_box,
                            FALSE, FALSE, 0);

        self->priv->output_bar = create_bar (self, TRUE, TRUE);
        gvc_channel_bar_set_name (GVC_CHANNEL_BAR (self->priv->output_bar),
                                  _("_Output volume: "));

        gtk_widget_show (self->priv->output_bar);
        gtk_widget_set_sensitive (self->priv->output_bar, FALSE);

        gtk_box_pack_start (GTK_BOX (self->priv->output_stream_box),
                            self->priv->output_bar, TRUE, TRUE, 12);

        self->priv->notebook = gtk_notebook_new ();

        gtk_widget_add_events (self->priv->notebook, GDK_SCROLL_MASK);
        g_signal_connect (self->priv->notebook,
                          "scroll-event",
                          G_CALLBACK (dialog_page_scroll_event_cb),
                          self);

        gtk_box_pack_start (GTK_BOX (main_vbox),
                            self->priv->notebook,
                            TRUE, TRUE, 0);

        g_signal_connect (G_OBJECT (self->priv->notebook),
                          "switch-page",
                          G_CALLBACK (on_notebook_switch_page),
                          self);

        gtk_container_set_border_width (GTK_CONTAINER (self->priv->notebook), 5);

        /* Set up accels (borrowed from Empathy) */
        accel_group = gtk_accel_group_new ();
        gtk_window_add_accel_group (GTK_WINDOW (self), accel_group);

        for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
                closure = g_cclosure_new (G_CALLBACK (dialog_accel_cb),
                                          self,
                                          NULL);
                gtk_accel_group_connect (accel_group,
                                         tab_accel_keys[i],
                                         GDK_MOD1_MASK,
                                         0,
                                         closure);
        }

        g_object_unref (accel_group);

        /* Create notebook pages */
        create_page_effects (self);

        self->priv->hw_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
        gtk_container_set_border_width (GTK_CONTAINER (self->priv->hw_box), 12);

        label = gtk_label_new (_("Hardware"));
        gtk_notebook_append_page (GTK_NOTEBOOK (self->priv->notebook),
                                  self->priv->hw_box,
                                  label);

        box = gtk_frame_new (_("C_hoose a device to configure:"));
        label = gtk_frame_get_label_widget (GTK_FRAME (box));
        make_label_bold (GTK_LABEL (label));
        gtk_label_set_use_underline (GTK_LABEL (label), TRUE);
        gtk_frame_set_shadow_type (GTK_FRAME (box), GTK_SHADOW_NONE);
        gtk_box_pack_start (GTK_BOX (self->priv->hw_box), box, TRUE, TRUE, 0);

        self->priv->hw_treeview = create_device_treeview (self,
                                                         G_CALLBACK (on_device_selection_changed));
        gtk_label_set_mnemonic_widget (GTK_LABEL (label), self->priv->hw_treeview);

        scroll_box = gtk_scrolled_window_new (NULL, NULL);
        gtk_widget_set_margin_top (scroll_box, 6);
        gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll_box),
                                        GTK_POLICY_NEVER,
                                        GTK_POLICY_AUTOMATIC);
        gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scroll_box),
                                             GTK_SHADOW_IN);
        gtk_container_add (GTK_CONTAINER (scroll_box), self->priv->hw_treeview);
        gtk_container_add (GTK_CONTAINER (box), scroll_box);

        selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self->priv->hw_treeview));
        gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);

        box = gtk_frame_new (_("Settings for the selected device:"));
        label = gtk_frame_get_label_widget (GTK_FRAME (box));
        make_label_bold (GTK_LABEL (label));
        gtk_frame_set_shadow_type (GTK_FRAME (box), GTK_SHADOW_NONE);
        gtk_box_pack_start (GTK_BOX (self->priv->hw_box), box, FALSE, TRUE, 12);

        self->priv->hw_settings_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);

        gtk_container_add (GTK_CONTAINER (box), self->priv->hw_settings_box);

        self->priv->input_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);

        gtk_container_set_border_width (GTK_CONTAINER (self->priv->input_box), 12);

        label = gtk_label_new (_("Input"));
        gtk_notebook_append_page (GTK_NOTEBOOK (self->priv->notebook),
                                  self->priv->input_box,
                                  label);

        self->priv->input_bar = create_bar (self, TRUE, TRUE);
        gtk_widget_set_margin_top (self->priv->input_bar, 6);

        gvc_channel_bar_set_name (GVC_CHANNEL_BAR (self->priv->input_bar),
                                  _("_Input volume: "));

        gvc_channel_bar_set_low_icon_name (GVC_CHANNEL_BAR (self->priv->input_bar),
                                           "audio-input-microphone-low");
        gvc_channel_bar_set_high_icon_name (GVC_CHANNEL_BAR (self->priv->input_bar),
                                            "audio-input-microphone-high");

        gtk_widget_show (self->priv->input_bar);
        gtk_widget_set_sensitive (self->priv->input_bar, FALSE);

        gtk_box_pack_start (GTK_BOX (self->priv->input_box),
                            self->priv->input_bar,
                            FALSE, FALSE, 0);

        box  = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
        sbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
        ebox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);

        gtk_box_pack_start (GTK_BOX (self->priv->input_box),
                            box,
                            FALSE, FALSE, 6);
        gtk_box_pack_start (GTK_BOX (box),
                            sbox,
                            FALSE, FALSE, 0);

        label = gtk_label_new (_("Input level:"));
        gtk_box_pack_start (GTK_BOX (sbox),
                            label,
                            FALSE, FALSE, 0);
        gtk_size_group_add_widget (self->priv->size_group, sbox);

        self->priv->input_level_bar = gvc_level_bar_new ();
        gvc_level_bar_set_orientation (GVC_LEVEL_BAR (self->priv->input_level_bar),
                                       GTK_ORIENTATION_HORIZONTAL);
        gvc_level_bar_set_scale (GVC_LEVEL_BAR (self->priv->input_level_bar),
                                 GVC_LEVEL_SCALE_LINEAR);
        gtk_box_pack_start (GTK_BOX (box),
                            self->priv->input_level_bar,
                            TRUE, TRUE, 6);

        gtk_box_pack_start (GTK_BOX (box),
                            ebox,
                            FALSE, FALSE, 0);
        gtk_size_group_add_widget (self->priv->size_group, ebox);

        self->priv->input_settings_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
        gtk_box_pack_start (GTK_BOX (self->priv->input_box),
                            self->priv->input_settings_box,
                            FALSE, FALSE, 0);

        box = gtk_frame_new (_("C_hoose a device for sound input:"));
        label = gtk_frame_get_label_widget (GTK_FRAME (box));
        make_label_bold (GTK_LABEL (label));
        gtk_label_set_use_underline (GTK_LABEL (label), TRUE);
        gtk_frame_set_shadow_type (GTK_FRAME (box), GTK_SHADOW_NONE);
        gtk_box_pack_start (GTK_BOX (self->priv->input_box), box, TRUE, TRUE, 0);

        self->priv->input_treeview =
                create_stream_treeview (self, G_CALLBACK (on_input_radio_toggled));

        gtk_label_set_mnemonic_widget (GTK_LABEL (label), self->priv->input_treeview);

        scroll_box = gtk_scrolled_window_new (NULL, NULL);
        gtk_widget_set_margin_top (scroll_box, 6);
        gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll_box),
                                        GTK_POLICY_NEVER,
                                        GTK_POLICY_AUTOMATIC);
        gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scroll_box),
                                             GTK_SHADOW_IN);
        gtk_container_add (GTK_CONTAINER (scroll_box), self->priv->input_treeview);
        gtk_container_add (GTK_CONTAINER (box), scroll_box);

        selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self->priv->input_treeview));
        gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);

        /* Output page */
        self->priv->output_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
        gtk_container_set_border_width (GTK_CONTAINER (self->priv->output_box), 12);
        label = gtk_label_new (_("Output"));
        gtk_notebook_append_page (GTK_NOTEBOOK (self->priv->notebook),
                                  self->priv->output_box,
                                  label);

        box = gtk_frame_new (_("C_hoose a device for sound output:"));
        label = gtk_frame_get_label_widget (GTK_FRAME (box));
        make_label_bold (GTK_LABEL (label));
        gtk_label_set_use_underline (GTK_LABEL (label), TRUE);
        gtk_frame_set_shadow_type (GTK_FRAME (box), GTK_SHADOW_NONE);
        gtk_box_pack_start (GTK_BOX (self->priv->output_box), box, TRUE, TRUE, 0);

        self->priv->output_treeview = create_stream_treeview (self,
                                                              G_CALLBACK (on_output_radio_toggled));
        gtk_label_set_mnemonic_widget (GTK_LABEL (label), self->priv->output_treeview);

        scroll_box = gtk_scrolled_window_new (NULL, NULL);
        gtk_widget_set_margin_top (scroll_box, 6);
        gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll_box),
                                        GTK_POLICY_NEVER,
                                        GTK_POLICY_AUTOMATIC);
        gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scroll_box),
                                             GTK_SHADOW_IN);
        gtk_container_add (GTK_CONTAINER (scroll_box), self->priv->output_treeview);
        gtk_container_add (GTK_CONTAINER (box), scroll_box);

        selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self->priv->output_treeview));
        gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);

        box = gtk_frame_new (_("Settings for the selected device:"));
        label = gtk_frame_get_label_widget (GTK_FRAME (box));
        make_label_bold (GTK_LABEL (label));
        gtk_frame_set_shadow_type (GTK_FRAME (box), GTK_SHADOW_NONE);
        gtk_box_pack_start (GTK_BOX (self->priv->output_box), box, FALSE, FALSE, 12);
        self->priv->output_settings_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
        gtk_container_add (GTK_CONTAINER (box), self->priv->output_settings_box);

        self->priv->output_settings_frame = box;

        /* Applications */
        self->priv->applications_window = gtk_scrolled_window_new (NULL, NULL);
        gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (self->priv->applications_window),
                                        GTK_POLICY_NEVER,
                                        GTK_POLICY_AUTOMATIC);

        gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (self->priv->applications_window),
                                             GTK_SHADOW_NONE);

        self->priv->applications_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
        gtk_container_set_border_width (GTK_CONTAINER (self->priv->applications_box), 12);

        gtk_container_add (GTK_CONTAINER (self->priv->applications_window),
                           self->priv->applications_box);

        label = gtk_label_new (_("Applications"));
        gtk_notebook_append_page (GTK_NOTEBOOK (self->priv->notebook),
                                  self->priv->applications_window,
                                  label);

        self->priv->no_apps_label = gtk_label_new (_("No application is currently playing or recording audio."));
        gtk_box_pack_start (GTK_BOX (self->priv->applications_box),
                            self->priv->no_apps_label,
                            TRUE, TRUE, 0);

        gtk_widget_show_all (main_vbox);

        list = mate_mixer_context_list_streams (self->priv->context);
        while (list != NULL) {
                add_stream (self, MATE_MIXER_STREAM (list->data));
                list = list->next;
        }

        list = mate_mixer_context_list_devices (self->priv->context);
        while (list != NULL) {
                add_device (self, MATE_MIXER_DEVICE (list->data));
                list = list->next;
        }

        selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self->priv->hw_treeview));

        /* Select the first device in the list */
        // XXX handle no devices
        if (gtk_tree_selection_get_selected (selection, NULL, NULL) == FALSE) {
                GtkTreeModel *model =
                        gtk_tree_view_get_model (GTK_TREE_VIEW (self->priv->hw_treeview));

                if (gtk_tree_model_get_iter_first (model, &iter))
                        gtk_tree_selection_select_iter (selection, &iter);
        }

        return object;
}

static MateMixerContext *
gvc_mixer_dialog_get_context (GvcMixerDialog *dialog)
{
        return dialog->priv->context;
}

static void
gvc_mixer_dialog_set_context (GvcMixerDialog *dialog, MateMixerContext *context)
{
        dialog->priv->context = g_object_ref (context);

        g_signal_connect (G_OBJECT (dialog->priv->context),
                          "stream-added",
                          G_CALLBACK (on_context_stream_added),
                          dialog);
        g_signal_connect (G_OBJECT (dialog->priv->context),
                          "stream-removed",
                          G_CALLBACK (on_context_stream_removed),
                          dialog);

        g_signal_connect (G_OBJECT (dialog->priv->context),
                          "device-added",
                          G_CALLBACK (on_context_device_added),
                          dialog);
        g_signal_connect (G_OBJECT (dialog->priv->context),
                          "device-removed",
                          G_CALLBACK (on_context_device_removed),
                          dialog);

        g_signal_connect (G_OBJECT (dialog->priv->context),
                          "notify::default-input-stream",
                          G_CALLBACK (on_context_default_input_stream_notify),
                          dialog);
        g_signal_connect (G_OBJECT (dialog->priv->context),
                          "notify::default-output-stream",
                          G_CALLBACK (on_context_default_output_stream_notify),
                          dialog);

        g_signal_connect (G_OBJECT (dialog->priv->context),
                          "stored-control-added",
                          G_CALLBACK (on_context_stored_control_added),
                          dialog);
        g_signal_connect (G_OBJECT (dialog->priv->context),
                          "stored-control-removed",
                          G_CALLBACK (on_context_stored_control_removed),
                          dialog);

        dialog->priv->backend_flags = mate_mixer_context_get_backend_flags (context);

        g_object_notify (G_OBJECT (dialog), "context");
}

static void
gvc_mixer_dialog_set_property (GObject       *object,
                               guint          prop_id,
                               const GValue  *value,
                               GParamSpec    *pspec)
{
        GvcMixerDialog *self = GVC_MIXER_DIALOG (object);

        switch (prop_id) {
        case PROP_CONTEXT:
                gvc_mixer_dialog_set_context (self, g_value_get_object (value));
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
        }
}

static void
gvc_mixer_dialog_get_property (GObject     *object,
                               guint        prop_id,
                               GValue      *value,
                               GParamSpec  *pspec)
{
        GvcMixerDialog *self = GVC_MIXER_DIALOG (object);

        switch (prop_id) {
        case PROP_CONTEXT:
                g_value_set_object (value, gvc_mixer_dialog_get_context (self));
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
        }
}

static void
gvc_mixer_dialog_dispose (GObject *object)
{
        GvcMixerDialog *dialog = GVC_MIXER_DIALOG (object);

        if (dialog->priv->context != NULL) {
                g_signal_handlers_disconnect_by_data (G_OBJECT (dialog->priv->context),
                                                      dialog);

                g_clear_object (&dialog->priv->context);
        }

        G_OBJECT_CLASS (gvc_mixer_dialog_parent_class)->dispose (object);
}

static void
gvc_mixer_dialog_class_init (GvcMixerDialogClass *klass)
{
        GObjectClass *object_class = G_OBJECT_CLASS (klass);

        object_class->constructor = gvc_mixer_dialog_constructor;
        object_class->dispose = gvc_mixer_dialog_dispose;
        object_class->finalize = gvc_mixer_dialog_finalize;
        object_class->set_property = gvc_mixer_dialog_set_property;
        object_class->get_property = gvc_mixer_dialog_get_property;

        g_object_class_install_property (object_class,
                                         PROP_CONTEXT,
                                         g_param_spec_object ("context",
                                                              "Context",
                                                              "MateMixer context",
                                                              MATE_MIXER_TYPE_CONTEXT,
                                                              G_PARAM_READWRITE |
                                                              G_PARAM_CONSTRUCT_ONLY |
                                                              G_PARAM_STATIC_STRINGS));

        GtkWidgetClass *widget_class  = GTK_WIDGET_CLASS (klass);
        gtk_widget_class_set_css_name (widget_class, "GvcMixerDialog");
}

static void
gvc_mixer_dialog_init (GvcMixerDialog *dialog)
{
        dialog->priv = gvc_mixer_dialog_get_instance_private (dialog);

        dialog->priv->bars = g_hash_table_new (g_str_hash, g_str_equal);
        dialog->priv->size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
}

static void
gvc_mixer_dialog_finalize (GObject *object)
{
        GvcMixerDialog *dialog;

        dialog = GVC_MIXER_DIALOG (object);

        g_hash_table_destroy (dialog->priv->bars);

        G_OBJECT_CLASS (gvc_mixer_dialog_parent_class)->finalize (object);
}

GvcMixerDialog *
gvc_mixer_dialog_new (MateMixerContext *context)
{
        return g_object_new (GVC_TYPE_MIXER_DIALOG,
                             "icon-name", "multimedia-volume-control",
                             "title", _("Sound Preferences"),
                             "context", context,
                             NULL);
}

gboolean
gvc_mixer_dialog_set_page (GvcMixerDialog *self, const gchar *page)
{
        guint num = 0;

        g_return_val_if_fail (GVC_IS_MIXER_DIALOG (self), FALSE);

        if (page != NULL) {
                if (g_str_equal (page, "effects"))
                        num = PAGE_EFFECTS;
                else if (g_str_equal (page, "hardware"))
                        num = PAGE_HARDWARE;
                else if (g_str_equal (page, "input"))
                        num = PAGE_INPUT;
                else if (g_str_equal (page, "output"))
                        num = PAGE_OUTPUT;
                else if (g_str_equal (page, "applications"))
                        num = PAGE_APPLICATIONS;
        }

        gtk_notebook_set_current_page (GTK_NOTEBOOK (self->priv->notebook), num);

        return TRUE;
}

static gboolean
dialog_page_scroll_event_cb (GtkWidget      *widget,
                             GdkEventScroll *event,
                             GtkWindow      *window)
{
        GtkNotebook *notebook = GTK_NOTEBOOK (widget);
        GtkWidget *child, *event_widget, *action_widget;

        child = gtk_notebook_get_nth_page (notebook, gtk_notebook_get_current_page (notebook));
        if (child == NULL)
                return FALSE;

        event_widget = gtk_get_event_widget ((GdkEvent *) event);

        /* Ignore scroll events from the content of the page */
        if (event_widget == NULL ||
            event_widget == child ||
            gtk_widget_is_ancestor (event_widget, child))
                return FALSE;

        /* And also from the action widgets */
        action_widget = gtk_notebook_get_action_widget (notebook, GTK_PACK_START);
        if (event_widget == action_widget ||
            (action_widget != NULL && gtk_widget_is_ancestor (event_widget, action_widget)))
                return FALSE;
        action_widget = gtk_notebook_get_action_widget (notebook, GTK_PACK_END);
        if (event_widget == action_widget ||
            (action_widget != NULL && gtk_widget_is_ancestor (event_widget, action_widget)))
                return FALSE;

        switch (event->direction) {

                case GDK_SCROLL_RIGHT:
                case GDK_SCROLL_DOWN:
                        gtk_notebook_next_page (notebook);
                        break;
                case GDK_SCROLL_LEFT:
                case GDK_SCROLL_UP:
                        gtk_notebook_prev_page (notebook);
                        break;

                case GDK_SCROLL_SMOOTH:
                        switch (gtk_notebook_get_tab_pos (notebook)) {
                            case GTK_POS_LEFT:
                            case GTK_POS_RIGHT:
                                    if (event->delta_y > 0)
                                        gtk_notebook_next_page (notebook);
                                    else if (event->delta_y < 0)
                                        gtk_notebook_prev_page (notebook);
                                    break;
                            case GTK_POS_TOP:
                            case GTK_POS_BOTTOM:
                                    if (event->delta_x > 0)
                                        gtk_notebook_next_page (notebook);
                                    else if (event->delta_x < 0)
                                        gtk_notebook_prev_page (notebook);
                                    break;
                        }
                        break;
        }

        return TRUE;
}