File: mhgui

package info (click to toggle)
congruity 18-1.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 816 kB
  • ctags: 1,136
  • sloc: python: 8,425; makefile: 52
file content (2819 lines) | stat: -rwxr-xr-x 112,164 bytes parent folder | download | duplicates (2)
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
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
#!/usr/bin/env python

# Copyright 2012-2014 Scott Talbert
#
# This file is part of congruity.
#
# congruity 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 3 of the License, or
# (at your option) any later version.
#
# congruity 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 congruity.  If not, see <http://www.gnu.org/licenses/>.

from ctypes import *
import os
import os.path
import sys
import thread
import time
import traceback

import wxversion
wxversion.ensureMinimal('2.8')
import wx
import wx.animate
import wx.grid
from tempfile import NamedTemporaryFile
sys.path.append('/usr/share/congruity')
from mhmanager import MHManager
from mhmanager import MHAccountDetails
from mhmanager import SaveActivityTemplate

version = "18"

HARMONY_LINK_SKIN_ID = 82
WATCH_TV_BUTTON_SKIN_IDS = [78, 79, 80, 81]

use_local_wsdl = False
if '--use-local-wsdl' in sys.argv:
    use_local_wsdl = True
suds_debug = False
if '--suds-debug' in sys.argv:
    suds_debug = True
mhMgr = MHManager(use_local_wsdl, suds_debug)

try:
    import libconcord
except:
    str = traceback.format_exc()
    app = wx.App(False)
    dlg = wx.MessageDialog(
        None,
        "Could not load libconcord; please ensure it, and the Python "
        "bindings, are installed and in the relevant search paths.\n\n" + str,
        "congruity: Dependency Error",
        wx.OK | wx.ICON_ERROR
    )
    dlg.ShowModal()
    os._exit(1)

ALIGN_LTA = wx.ALIGN_LEFT  | wx.ALIGN_TOP             | wx.ALL
ALIGN_XTA = wx.EXPAND      | wx.ALIGN_TOP             | wx.ALL
ALIGN_LCA = wx.ALIGN_LEFT  | wx.ALIGN_CENTER_VERTICAL | wx.ALL
ALIGN_RCA = wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL | wx.ALL
ALIGN_XCA = wx.EXPAND      | wx.ALIGN_CENTER_VERTICAL | wx.ALL
ALIGN_LBA = wx.ALIGN_LEFT  | wx.ALIGN_BOTTOM          | wx.ALL
ALIGN_XBA = wx.EXPAND      | wx.ALIGN_BOTTOM          | wx.ALL

def dummy_callback_imp(stage_id, count, current, total, type, context, stages):
    pass
dummy_cb = libconcord.callback_type(dummy_callback_imp)

class ThrobberDialog(wx.Dialog):
    TITLE_REMOTE = "Please wait, contacting remote..."
    TITLE_WEBSITE = "Please wait, contacting website..."
    def __init__(self, title):
        if title is None:
            self.title = self.TITLE_WEBSITE
        else:
            self.title = title
        wx.Dialog.__init__(self, None, -1, title=self.title)
        self.SetClientSize((256, 256))
        self.SetBackgroundColour("white")
        self.gif = wx.animate.GIFAnimationCtrl(self, -1,
                                               self.FindGif("throbber.gif"))
        self.gif.GetPlayer().UseBackgroundColour(True)
        self.gif.Play()
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.gif, 1, wx.EXPAND|wx.ALL, 64)
        self.SetSizer(self.sizer)
    def FindGif(self, filename):
        appdir = os.path.abspath(os.path.dirname(sys.argv[0]))
        dirs = ['/usr/share/congruity', appdir, '.']
        for dir in dirs:
            fpath = os.path.join(dir, filename)
            if not os.path.isfile(fpath):
                continue
            return fpath
        raise Exception("Can't load " + filename)

class BackgroundTask:
    def __init__(self, backgroundFunctionSpec, onDoneFunctionSpec,
                 modalThrobber = True, throbberTitle = None):
        self.backgroundFunction = backgroundFunctionSpec[0]
        self.backgroundFunctionArgs = backgroundFunctionSpec[1:]
        self.onDoneFunction = onDoneFunctionSpec[0]
        self.onDoneFunctionArgs = onDoneFunctionSpec[1:]
        thread.start_new_thread(self.ThreadFunction, ())
        self.throbber = None
        if modalThrobber is not None:
            self.throbber = ThrobberDialog(throbberTitle)
            if modalThrobber is True:
                self.throbber.ShowModal()
                self.throbber.Destroy()
            else:
                self.throbber.Show()
    def ThreadFunction(self):
        result = self.backgroundFunction(*self.backgroundFunctionArgs)
        wx.CallAfter(self.ThreadDoneFunction, result)
    def ThreadDoneFunction(self, bgFuncResult):
        if self.throbber is not None:
            if self.throbber.IsModal():
                self.throbber.EndModal(0)
            else:
                self.throbber.Destroy()
        self.onDoneFunction(bgFuncResult, *self.onDoneFunctionArgs)

class WrappedStaticText(wx.StaticText):
    def __init__(self, parent):
        self.parent = parent
        wx.StaticText.__init__(self, parent, -1, "")

    def UpdateText(self, new_label):
        cur_width = self.parent.GetSize().GetWidth()
        self.SetLabel(new_label)
        self.Wrap(cur_width)
        self.Layout()
        self.parent.Layout()

class WizardPanelBase(wx.Panel):
    def __init__(self, parent, resources):
        self.parent = parent
        self.resources = resources

        wx.Panel.__init__(self, parent)

    def OnActivated(self, prev_page, data):
        return (None, None)

    def OnCancel(self):
        self.parent.OnExit(1)

    def GetTitle(self):
        return "Base"

    def IsTerminal(self):
        return False

    def IsBackInitiallyDisabled(self):
        return True

    def IsNextInitiallyDisabled(self):
        return True

    def IsCloseInitiallyDisabled(self):
        return True

    def IsCancelInitiallyDisabled(self):
        return True

    def GetExitCode(self):
        return 0

    def GetBack(self):
        return (None, None)

    def GetNext(self):
        return (None, None)

class WelcomePanel(WizardPanelBase):
    _msg_welcome = (
        "Welcome to MHGUI: an application for accessing " +
        "Logitech's MyHarmony website.\n\n" +
        "Please enter your MyHarmony.com username and " +
        "password below.\n"
    )

    def __init__(self, parent, resources):
        self.parent = parent
        self.resources = resources

        WizardPanelBase.__init__(self, parent, resources)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.textMessage = WrappedStaticText(self)
        self.sizer.Add(self.textMessage, 0, ALIGN_XTA, 5)
        self.usernameLabel = wx.StaticText(self, -1, "Username:")
        self.usernameCtrl = wx.TextCtrl(self, -1, "")
        self.usernameCtrl.SetMinSize((200, 31))
        self.usernameSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.usernameSizer.Add(self.usernameLabel, 0, ALIGN_LCA, 0)
        self.usernameSizer.Add(self.usernameCtrl, 0, ALIGN_RCA, 0)
        self.sizer.Add(self.usernameSizer, 0, wx.EXPAND, 0)
        self.passwordText = wx.StaticText(self, -1, "Password:")
        self.passwordCtrl = wx.TextCtrl(self, -1, "", style=wx.TE_PASSWORD)
        self.passwordCtrl.SetMinSize((200, 31))
        self.passwordSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.passwordSizer.Add(self.passwordText, 0, ALIGN_LCA, 0)
        self.passwordSizer.Add(self.passwordCtrl, 0, ALIGN_RCA, 0)
        self.sizer.Add(self.passwordSizer, 0, wx.EXPAND, 0)
        self.sizer.AddSpacer(25)
        self.createAccountButton = wx.Button(self, label="Create Account")
        self.createAccountButton.Bind(wx.EVT_BUTTON, self.OnCreateAccount)
        self.sizer.Add(self.createAccountButton)

        self.SetSizerAndFit(self.sizer)

        self.next = None

    def _WorkerFunction(self):
        wx.CallAfter(
            self.textMessage.UpdateText,
            self._msg_welcome
        )

        self.next = self.resources.page_remote_select
        self.parent.ReenableNext()

    def OnActivated(self, prev_page, data):
        thread.start_new_thread(self._WorkerFunction, ())
        return (None, None)

    def GetTitle(self):
        return "Welcome"

    def IsCancelInitiallyDisabled(self):
        return False

    def OnNext(self):
        username = self.usernameCtrl.GetValue()
        password = self.passwordCtrl.GetValue()
        BackgroundTask((self.DoLogin, username, password), (self.FinishLogin,))
        return False

    def DoLogin(self, username, password):
        return mhMgr.Login(username, password)

    def FinishLogin(self, loginResult):
        if loginResult is True:
            self.parent._SetPage(self.next, True, True)
        else:
            wx.MessageBox('Login failed.', 'Login failed.', wx.OK |
                          wx.ICON_WARNING)
            self.usernameCtrl.Clear()
            self.passwordCtrl.Clear()

    def GetNext(self):
        return (self.next, True, True)

    def OnCreateAccount(self, event):
        self.parent._SetPage(self.resources.page_create_account, None, True)

class RemoteSelectPanel(WizardPanelBase):
    _msg_welcome = (
        "Please select a remote control below.\n\n" +
        "Remotes (maximum of 6 per account):"
    )

    def __init__(self, parent, resources):
        self.parent = parent
        self.resources = resources

        WizardPanelBase.__init__(self, parent, resources)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.textMessage = WrappedStaticText(self)
        self.sizer.Add(self.textMessage, 0, ALIGN_XTA, 5)
        self.remotesListBox = wx.ListBox(self, style=wx.LB_SINGLE,
                                         size=(134, 75))
        self.sizer.Add(self.remotesListBox)
        self.sizer.AddSpacer(20)
        self.addButton = wx.Button(self, label="Add New Remote",
                                   size=(127, 33))
        self.addButton.Bind(wx.EVT_BUTTON, self.OnAddRemote)
        self.sizer.Add(self.addButton)
        self.sizer.AddSpacer(10)
        self.deleteButton = wx.Button(self, label="Delete Remote",
                                      size=(127, 33))
        self.deleteButton.Bind(wx.EVT_BUTTON, self.OnDeleteRemote)
        self.deleteButton.SetToolTip(wx.ToolTip(
                "Remove a Remote from your Account"
        ))
        self.sizer.Add(self.deleteButton)
        self.sizer.AddSpacer(10)
        self.updateAccountButton = wx.Button(self, label="Update Account",
                                             size=(127,33))
        self.updateAccountButton.Bind(wx.EVT_BUTTON, self.OnUpdateAccount)
        self.updateAccountButton.SetToolTip(wx.ToolTip(
                "Update User Account Details (password, etc.)"
        ))
        self.sizer.Add(self.updateAccountButton)
        self.SetSizerAndFit(self.sizer)

        self.next = None

    def LoadData(self):
        self.remotes = mhMgr.GetRemotes()
        self.remoteDisplayNames = []
        for remote in self.remotes:
            product = mhMgr.GetProduct(remote.SkinId)
            self.remoteDisplayNames.append(product.DisplayName)

    def AddRemote(self, serialNumber, skinId, usbPid, usbVid):
        result = mhMgr.AddRemote(serialNumber, skinId, usbPid, usbVid)
        if result is not None:
            self.LoadData()
        return result

    def DeleteRemote(self, remoteId):
        mhMgr.DeleteRemote(remoteId)
        self.LoadData()

    def LoadAddRemoteData(self):
        self.supportedRemotes = mhMgr.GetSupportedRemoteNames()
        self.supportedSkins = mhMgr.GetSupportedRemoteSkinIds()

    def LoadDataUI(self, loadDataResult):
        self.remotesListBox.Clear()
        self.remotesListBox.Set(self.remoteDisplayNames)
        self.Layout()
        self.parent.Show()

    def OnActivated(self, prev_page, loadData):
        self.textMessage.UpdateText(self._msg_welcome)
        self.parent.ReenableNext()
        if loadData:
            BackgroundTask((self.LoadData,), (self.LoadDataUI,), False)
        return (None, None)

    def GetTitle(self):
        return "Remote Selection"

    def IsCancelInitiallyDisabled(self):
        return False

    def OnAddRemote(self, event):
        BackgroundTask((self.LoadAddRemoteData,), (self.DoAddRemote,))

    def DoAddRemote(self, loadResult):
        if len(self.remotes) >= 6:
            wx.MessageBox('Each account can support up to 6 remotes.',
                          'Maximum Number of Remotes Reached',
                          wx.OK | wx.ICON_WARNING)
            return            
        msg = 'Please ensure your remote control is connected.'
        msg += '\n\nThe web service advertises support for these models:'
        for name in self.supportedRemotes:
            msg += '\n' + name
        msg += "\n\nNOTE: MHGUI has only been tested with Harmony 200, 300,"
        msg += "\n650, and Link."
        wx.MessageBox(msg, 'Connect Remote', wx.OK)

        try:
            libconcord.init_concord()
        except:
            msg = '%s\n    (libconcord function %s error %d)\n\n' % (
                sys.exc_value.result_str,
                sys.exc_value.func,
                sys.exc_value.result
            )
            wx.MessageBox('Could not detect remote: ' + msg, 'Error',
                          wx.OK | wx.ICON_WARNING)
            return

        cb = libconcord.callback_type(dummy_callback_imp)
        try:
            libconcord.get_identity(cb, None)
        except:
            msg = '%s\n    (libconcord function %s error %d)\n\n' % (
                sys.exc_value.result_str,
                sys.exc_value.func,
                sys.exc_value.result
            )
            wx.MessageBox('Could not identify remote: ' + msg, 'Error',
                          wx.OK | wx.ICON_WARNING)
            libconcord.deinit_concord()
            return

        ser_1 = libconcord.get_serial(libconcord.SERIAL_COMPONENT_1)
        ser_2 = libconcord.get_serial(libconcord.SERIAL_COMPONENT_2)
        ser_3 = libconcord.get_serial(libconcord.SERIAL_COMPONENT_3)
        serialNumber = ser_1 + ser_2 + ser_3
        skinId = libconcord.get_skin()
        usbPid = hex(libconcord.get_usb_pid())
        usbVid = hex(libconcord.get_usb_vid())
        model = libconcord.get_mfg() + ' ' + libconcord.get_model()
        libconcord.deinit_concord()
        # Check whether this is a supported remote.
        if skinId not in self.supportedSkins:
            wx.MessageBox('Sorry, this remote model (' + model + ') is '
                          + 'not supported by this software.  Please '
                          + 'use members.harmonyremote.com.',
                          'Unsupported Remote', wx.OK | wx.ICON_WARNING)
            return
        # Check whether this remote is already on this account.
        for remote in self.remotes:
            if remote.SerialNumber == serialNumber:
                wx.MessageBox('Sorry, this remote already exists in '
                              + 'your account.', 'Existing Remote',
                              wx.OK | wx.ICON_WARNING)
                return
        # Actually add the remote!
        BackgroundTask((self.AddRemote, serialNumber, skinId, usbPid, usbVid),
                       (self.FinishAddRemote, skinId))

    def FinishAddRemote(self, result, skinId):
        if result is not None:
            if skinId == HARMONY_LINK_SKIN_ID:
                wx.MessageBox('Remote successfully added.  Make sure to select'
                              + ' your Harmony Link, select "Forward", and'
                              + ' then select "Configure Harmony Link" to'
                              + ' complete setup.', 'Success', wx.OK)
            else:
                wx.MessageBox('Remote successfully added.', 'Success',
                              wx.OK)
            self.LoadDataUI(None)
            return
        else:
            wx.MessageBox('Remote addition failed.', 'Failure',
                          wx.OK, wx.ICON_WARNING)
            return

    def OnDeleteRemote(self, event):
        if self.remotesListBox.GetSelection() != -1:
            dlg = wx.MessageDialog(self.parent,
                                   'Are you sure you want to delete '
                                   + self.remotesListBox.GetStringSelection()
                                   + ' from your account?',
                                   'Delete Confirmation',
                                   wx.YES_NO | wx.ICON_QUESTION)
            result = dlg.ShowModal() == wx.ID_YES
            dlg.Destroy()
            if result:
                remoteId = self.remotes[self.remotesListBox.GetSelection()].Id
                BackgroundTask((self.DeleteRemote, remoteId),
                               (self.LoadDataUI,))
        else:
            wx.MessageBox('Please make a selection.', 'No selection made.',
                          wx.OK | wx.ICON_WARNING)

    def OnUpdateAccount(self, event):
        self.parent._SetPage(self.resources.page_create_account, True, True)

    def OnNext(self):
        if self.remotesListBox.GetSelection() != -1:
            self.next = self.resources.page_remote_configuration
            return True
        else:
            wx.MessageBox('Please make a selection.', 'No selection made.',
                          wx.OK | wx.ICON_WARNING)
            return False

    def GetNext(self):
        return (self.next, self.remotes[self.remotesListBox.GetSelection()],
                True)

class RemoteConfigurationPanel(WizardPanelBase):
    _msg_welcome = (
        "Please make a selection below.\n"
    )

    def __init__(self, parent, resources):
        self.parent = parent
        self.resources = resources

        WizardPanelBase.__init__(self, parent, resources)

        self.buttonSize = (170, 33) # Size of largest button
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.textMessage = wx.StaticText(self)
        self.sizer.Add(self.textMessage, 0, ALIGN_XTA, 5)
        self.devicesListBox = wx.ListBox(self, style=wx.LB_SINGLE)
        self.sizer.Add(self.devicesListBox, 0, 0, 0)
        self.sizer.AddSpacer(20)
        self.buttonSizer = wx.BoxSizer(wx.HORIZONTAL)

        self.leftButtonSizer = wx.BoxSizer(wx.VERTICAL)
        self.addButton = wx.Button(self, label="Add Device",
                                   size=self.buttonSize)
        self.addButton.Bind(wx.EVT_BUTTON, self.OnAdd)
        self.addButton.SetToolTip(wx.ToolTip(
                "Add a device (TV, cable box, etc) to this remote"
        ))
        self.leftButtonSizer.Add(self.addButton, 0, 0, 0)
        self.deleteButton = wx.Button(self, label="Delete Device",
                                      size=self.buttonSize)
        self.deleteButton.Bind(wx.EVT_BUTTON, self.OnDelete)
        self.leftButtonSizer.Add(self.deleteButton, 0, 0, 0)
        self.renameButton = wx.Button(self, label="Rename Device",
                                      size=self.buttonSize)
        self.renameButton.Bind(wx.EVT_BUTTON, self.OnRename)
        self.leftButtonSizer.Add(self.renameButton, 0, 0, 0)
        self.configureButton = wx.Button(self, label="Configure Device",
                                         size=self.buttonSize)
        self.configureButton.Bind(wx.EVT_BUTTON, self.OnConfigure)
        self.configureButton.SetToolTip(wx.ToolTip(
                "Adjust/fix button functionality for a device"
        ))
        self.leftButtonSizer.Add(self.configureButton, 0, 0, 0)
        self.adjustPowerSettingsButton = wx.Button(
            self, label="Adjust Power Settings", size=self.buttonSize)
        self.adjustPowerSettingsButton.Bind(wx.EVT_BUTTON,
                                            self.OnAdjustPowerSettings)
        self.adjustPowerSettingsButton.SetToolTip(wx.ToolTip(
                "Adjust power on/off settings for a device"
        ))
        self.leftButtonSizer.Add(self.adjustPowerSettingsButton)
        self.buttonSizer.Add(self.leftButtonSizer, 0, 0, 0)

        self.rightButtonSizer = wx.BoxSizer(wx.VERTICAL)
        self.buttonSizer.Add(self.rightButtonSizer, 0, 0, 0)

        self.sizer.Add(self.buttonSizer, 0, 0, 0)
        self.SetSizerAndFit(self.sizer)

        self.next = None

    def AddFavoriteChannelsButton(self):
        self.favoriteChannelsButton = wx.Button(self,
                                                label="Edit Favorite Channels",
                                                size=self.buttonSize)
        self.favoriteChannelsButton.Bind(wx.EVT_BUTTON, self.OnFavoriteChannels)
        self.rightButtonSizer.Add(self.favoriteChannelsButton, 0, 0, 0)

    def AddConfigButtons(self):
        self.syncButton = wx.Button(self, label="Sync Remote",
                                    size=self.buttonSize)
        self.syncButton.Bind(wx.EVT_BUTTON, self.OnSync)
        self.syncButton.SetToolTip(wx.ToolTip(
                "Update the configuration on your remote"
        ))
        self.rightButtonSizer.Add(self.syncButton, 0, 0, 0)
        self.downloadConfigButton = wx.Button(self, label="Download Config",
                                              size=self.buttonSize)
        self.downloadConfigButton.Bind(wx.EVT_BUTTON, self.OnDownloadConfig)
        self.downloadConfigButton.SetToolTip(wx.ToolTip(
                "Download the config for your remote and save it to a file"
        ))
        self.rightButtonSizer.Add(self.downloadConfigButton, 0, 0, 0)

    def AddSetupWatchTVButton(self):
        self.setupWatchTVButton = wx.Button(self, label="Setup Watch TV Button",
                                            size=self.buttonSize)
        self.setupWatchTVButton.Bind(wx.EVT_BUTTON, self.OnSetupWatchTV)
        self.setupWatchTVButton.SetToolTip(wx.ToolTip(
                "Configure what happens when you press your Watch TV button"
        ))
        self.rightButtonSizer.Add(self.setupWatchTVButton, 0, 0, 0)

    def AddConfigureHarmonyLinkButton(self):
        self.configureHarmonyLinkButton = wx.Button(
            self, label="Configure Harmony Link", size=self.buttonSize)
        self.configureHarmonyLinkButton.Bind(wx.EVT_BUTTON,
                                             self.OnConfigureHarmonyLink)
        self.configureHarmonyLinkButton.SetToolTip(wx.ToolTip(
                "Configure Harmony Link Settings (Room Name, Wifi, etc.)"
        ))
        self.rightButtonSizer.Add(self.configureHarmonyLinkButton, 0, 0, 0)

    def AddSetupActivitiesButton(self):
        self.setupActivitiesButton = wx.Button(self, label="Setup Activities",
                                               size=self.buttonSize)
        self.setupActivitiesButton.Bind(wx.EVT_BUTTON, self.OnSetupActivities)
        self.setupActivitiesButton.SetToolTip(wx.ToolTip(
                "Configure one-button activities (e.g., Watch TV)"
        ))
        self.rightButtonSizer.Add(self.setupActivitiesButton, 0, 0, 0)

    def LoadData(self):
        self.product = mhMgr.GetProduct(self.remote.SkinId)
        self.devices = mhMgr.GetDevices(self.remote.Id)

    def LoadDataUI(self, loadDataResult):
        self._msg_welcome = "Remote Configuration for " \
            + self.product.DisplayName + "\n\nDevices (maximum of " \
            + str(self.product.MaxDevicesPerAccount) + "):"
        self.textMessage.SetLabel(self._msg_welcome)
        self.PopulateDevicesList()

        # Populate remote-specific buttons in rightButtonSizer
        self.rightButtonSizer.Clear(deleteWindows=True)
        capabilities = self.product.SupportedCapabilities.SupportedCapability
        if "FavoriteChannels" in capabilities:
            self.AddFavoriteChannelsButton()
        if "CompiledRemoteButtonMapping" in capabilities or \
                "ActivityCompiledRemoteButtonMapping" in capabilities:
            self.AddConfigButtons()
        if int(self.remote.SkinId) in WATCH_TV_BUTTON_SKIN_IDS:
            self.AddSetupWatchTVButton()
        if "Wifi" in capabilities:
            self.AddConfigureHarmonyLinkButton()
        if "Activities" in capabilities:
            self.AddSetupActivitiesButton()

        self.Layout()
        self.parent.Show()

    def PopulateDevicesList(self):
        self.devicesList = []
        if self.devices is not None:
            for device in self.devices:
                self.devicesList.append(device.Name)
            self.devicesListBox.Set(self.devicesList)
        else:
            self.devicesListBox.Clear()

    def OnAdd(self, event):
        if len(self.devicesList) < self.product.MaxDevicesPerAccount:
            self.parent._SetPage(self.resources.page_add_device, self.remote)
        else:
            wx.MessageBox('Remote cannot support additional devices.',
                          'Cannot Add Device.', wx.OK | wx.ICON_WARNING)

    def OnDelete(self, event):
        deviceToDelete = self.devicesListBox.GetSelections()
        if deviceToDelete != ():
            dlg = wx.MessageDialog(self.parent,
                                   'Are you sure you want to delete '
                                   + self.devicesListBox.GetStringSelection()
                                   + '?', 'Delete Confirmation',
                                   wx.YES_NO | wx.ICON_QUESTION)
            result = dlg.ShowModal() == wx.ID_YES
            dlg.Destroy()
            if result:
                BackgroundTask((self.DoDelete, deviceToDelete),
                               (self.FinishDelete,))
        else:
            wx.MessageBox('Please select a device to delete.',
                          'No selection made.', wx.OK | wx.ICON_WARNING)

    def DoDelete(self, deviceToDelete):
        mhMgr.DeleteDevice(self.devices[deviceToDelete[0]].Id)
        self.devices = mhMgr.GetDevices(self.remote.Id)

    def FinishDelete(self, result):
        self.PopulateDevicesList()
        self.Layout()

    def OnRename(self, event):
        deviceToRename = self.devicesListBox.GetSelections()
        if deviceToRename != ():
            dlg = wx.TextEntryDialog(self.parent,
                                     'Enter new name for device "'
                                     + self.devicesListBox.GetStringSelection()
                                     + '":', 'Rename Device')
            result = dlg.ShowModal() == wx.ID_OK
            newName = dlg.GetValue()
            dlg.Destroy()
            if result:
                BackgroundTask((self.DoRename, deviceToRename, newName),
                               (self.FinishRename,))
        else:
            wx.MessageBox('Please select a device to rename.',
                          'No selection made.', wx.OK | wx.ICON_WARNING)

    def DoRename(self, deviceToRename, newName):
        mhMgr.RenameDevice(self.devices[deviceToRename[0]].Id, newName)
        self.devices = mhMgr.GetDevices(self.remote.Id)

    def FinishRename(self, result):
        self.PopulateDevicesList()
        self.Layout()

    def OnConfigure(self, event):
        deviceToConfigure = self.devicesListBox.GetSelections()
        if deviceToConfigure != ():
            params = (
                self.remote.SkinId,
                self.product,
                self.devices[deviceToConfigure[0]].Id,
                self.devices[deviceToConfigure[0]].Name
            )
            self.parent._SetPage(self.resources.page_configure_device, params,
                                 True)
        else:
            wx.MessageBox('Please select a device to configure.',
                          'No selection made.', wx.OK | wx.ICON_WARNING)

    def OnFavoriteChannels(self, event):
        deviceToConfigure = self.devicesListBox.GetSelections()
        if deviceToConfigure != ():
            params = (
                self.remote.SkinId,
                self.devices[deviceToConfigure[0]].Id,
                self.devices[deviceToConfigure[0]].Name
            )
            self.parent._SetPage(self.resources.page_favorite_channels, params,
                                 True)
        else:
            wx.MessageBox('Please select a device for which to configure the '
                          + "favorite channels.", 'No selection made.',
                          wx.OK | wx.ICON_WARNING)

    def OnSync(self, event):
        tempFile = NamedTemporaryFile(delete=False)
        tempFile.close()
        BackgroundTask((mhMgr.GetConfig, self.remote, tempFile.name),
                       (self.FinishSync, tempFile.name))

    def FinishSync(self, getConfigResult, tempFileName):
        self.parent.Hide()
        BackgroundTask((self.DoCongruity, tempFileName),
                       (self.FinishCongruity,), None)

    def DoCongruity(self, tempFileName):
        myDir = os.path.abspath(self.resources.appdir)
        congruityPath = os.path.join(myDir, "congruity")
        os.system(congruityPath + " --no-web " + tempFileName)
        os.unlink(tempFileName)

    def FinishCongruity(self, result):
        self.parent.Show()

    def OnDownloadConfig(self, event):
        dialog = wx.FileDialog(
            self,
            message = "Save configuration as...", 
            defaultFile = "config.zip",
            style = wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT
        )
        if dialog.ShowModal() == wx.ID_OK:
            filename = dialog.GetPath()
            dialog.Destroy()
            BackgroundTask((mhMgr.GetConfig, self.remote, filename),
                           (self.FinishDownloadConfig, filename))
        else:
            dialog.Destroy()

    def FinishDownloadConfig(self, getConfigResult, filename):
        wx.MessageBox('Config file saved to ' + filename,
                      'Download complete.', wx.OK)

    def OnSetupWatchTV(self, event):
        self.parent._SetPage(self.resources.page_setup_watch_tv, self.remote,
                             True)

    def OnConfigureHarmonyLink(self, event):
        self.parent._SetPage(self.resources.page_harmony_link, self.remote)

    def OnSetupActivities(self, event):
        self.parent._SetPage(self.resources.page_setup_activities, self.remote,
                             True)

    def OnAdjustPowerSettings(self, event):
        deviceToConfigure = self.devicesListBox.GetSelections()
        if deviceToConfigure != ():
            self.parent._SetPage(self.resources.page_adjust_power_settings,
                                 self.devices[deviceToConfigure[0]], True)
        else:
            wx.MessageBox('Please select a device to adjust.',
                          'No selection made.', wx.OK | wx.ICON_WARNING)

    def OnActivated(self, prev_page, remote):
        self.parent.ReenableBack()
        if remote is not None:
            self.remote = remote
            BackgroundTask((self.LoadData,), (self.LoadDataUI,), False)
        return (None, None)

    def GetTitle(self):
        return "Remote Configuration"

    def IsCancelInitiallyDisabled(self):
        return False

    def OnNext(self):
        pass

    def GetNext(self):
        return (self.next, None)

    def GetBack(self):
        return (self.resources.page_remote_select, False)

class AddDevicePanel(WizardPanelBase):
    _msg_welcome = (
        "Please enter the manufacturer and model number of your device below.\n"
    )

    def __init__(self, parent, resources):
        self.parent = parent
        self.resources = resources

        WizardPanelBase.__init__(self, parent, resources)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.textMessage = WrappedStaticText(self)
        self.sizer.Add(self.textMessage, 0, ALIGN_XTA, 5)
        self.manufacturerLabel = wx.StaticText(self, -1, "Manufacturer:")
        self.manufacturerCtrl = wx.TextCtrl(self, -1, "")
        self.manufacturerCtrl.SetMinSize((200, 31))
        self.manufacturerSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.manufacturerSizer.Add(self.manufacturerLabel, 0, ALIGN_LCA, 0)
        self.manufacturerSizer.Add(self.manufacturerCtrl, 0, ALIGN_RCA, 0)
        self.sizer.Add(self.manufacturerSizer, 0, wx.EXPAND, 0)
        self.modelNumberText = wx.StaticText(self, -1, "Model Number:")
        self.modelNumberCtrl = wx.TextCtrl(self, -1, "")
        self.modelNumberCtrl.SetMinSize((200, 31))
        self.modelNumberSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.modelNumberSizer.Add(self.modelNumberText, 0, ALIGN_LCA, 0)
        self.modelNumberSizer.Add(self.modelNumberCtrl, 0, ALIGN_RCA, 0)
        self.sizer.Add(self.modelNumberSizer, 0, wx.EXPAND, 0)
        self.searchButton = wx.Button(self, label="Search")
        self.searchButton.Bind(wx.EVT_BUTTON, self.OnSearch)
        self.sizer.Add(self.searchButton, 0, 0, 0)

        self.divider = wx.StaticLine(self, -1, None, None, wx.LI_HORIZONTAL)
        self.sizer.Add(self.divider, 0, wx.EXPAND | wx.ALL, 5)

        self.searchResultsText = wx.StaticText(self, -1, "Search Results:")
        self.sizer.Add(self.searchResultsText, 0, 0, 0)
        self.searchResultsListBox = wx.ListBox(self, style=wx.LB_SINGLE, 
                                               size=(300, 150))
        self.sizer.Add(self.searchResultsListBox, 0, 0, 0)
        self.addButton = wx.Button(self, label="Add Device")
        self.addButton.Bind(wx.EVT_BUTTON, self.OnAdd)
        self.sizer.Add(self.addButton, 0, 0, 0)

        self.SetSizerAndFit(self.sizer)

        self.next = None

    def OnActivated(self, prev_page, data):
        self.textMessage.UpdateText(self._msg_welcome)
        self.parent.ReenableBack()
        self.remote = data
        self.ClearPage()
        return (None, None)

    def OnSearch(self, event):
        self.searchResultsList = []
        self.searchResultsListBox.Set(self.searchResultsList)
        if self.manufacturerCtrl.IsEmpty():
            wx.MessageBox('Please enter a manufacturer.',
                          'No manufacturer entered.', wx.OK | wx.ICON_WARNING)
        elif self.modelNumberCtrl.IsEmpty():
            wx.MessageBox('Please enter a model number.',
                          'No model number entered.', wx.OK | wx.ICON_WARNING)
        else:
            manufacturer = self.manufacturerCtrl.GetValue()
            modelNumber = self.modelNumberCtrl.GetValue()
            BackgroundTask((self.DoSearch, manufacturer, modelNumber),
                           (self.FinishSearch,))

    def DoSearch(self, manufacturer, modelNumber):
        return mhMgr.SearchDevices(manufacturer, modelNumber, 5)

    def FinishSearch(self, searchResults):
        if searchResults.Status == "NoMatchFound":
            wx.MessageBox('Sorry, no devices were found.',
                          'No devices found.', wx.OK | wx.ICON_WARNING)
        else:
            self.matches = searchResults.Matches.PublicDeviceSearchMatch
            for match in self.matches:
                self.searchResultsList.append(match.Manufacturer + " "
                                              + match.DeviceModel)
            self.searchResultsListBox.Set(self.searchResultsList)

    def OnAdd(self, event):
        deviceToAdd = self.searchResultsListBox.GetSelections()
        if deviceToAdd != ():
            BackgroundTask((self.DoAdd, deviceToAdd), (self.FinishAdd,))
        else:
            wx.MessageBox('Please select a device to add.',
                          'No selection made.', wx.OK | wx.ICON_WARNING)

    def DoAdd(self, deviceToAdd):
        return mhMgr.AddDevice(self.matches[deviceToAdd[0]], self.remote.Id)

    def FinishAdd(self, result):
        if result is None:
            wx.MessageBox('Device addition failed!',
                          'Failure', wx.OK | wx.ICON_WARNING)
        else:
            self.parent._SetPage(self.resources.page_remote_configuration,
                                 self.remote, True)

    def ClearPage(self):
        self.manufacturerCtrl.Clear()
        self.modelNumberCtrl.Clear()
        self.searchResultsListBox.Clear()

    def GetTitle(self):
        return "Device Addition"

    def IsCancelInitiallyDisabled(self):
        return False

    def OnNext(self):
        pass

    def GetNext(self):
        return (self.next, None)

    def GetBack(self):
        return (self.resources.page_remote_configuration, None)

class CreateAccountPanel(WizardPanelBase):
    _msg_welcome_create = (
        "Please enter the information below in order to create an account.\n"
    )
    _msg_welcome_update = (
        "Please update the information below.\n"
    )

    def __init__(self, parent, resources):
        self.parent = parent
        self.resources = resources

        WizardPanelBase.__init__(self, parent, resources)

        self.securityQuestionChoices = [
            "What was the make of your first car?",
            "What's your mother's maiden name?",
            "What's the name of your favorite sports team?",
            "What's your first pet's name?",
            "Who is your all-time favorite movie character?" ]
        self.securityQuestionTokens = [
            "QuestionCar",
            "QuestionMothersMaidenName",
            "QuestionSportsTeam",
            "QuestionFirstPet",
            "QuestionMovieCharacter" ]

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.textMessage = WrappedStaticText(self)
        self.sizer.Add(self.textMessage, 0, ALIGN_XTA, 5)
    
        self.lineOneSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.firstNameLabel = wx.StaticText(self, -1, "First Name:")
        self.lineOneSizer.Add(self.firstNameLabel, 0, ALIGN_LCA)
        self.firstNameCtrl = wx.TextCtrl(self, size=(120,31))
        self.firstNameCtrl.SetMaxLength(30)
        self.lineOneSizer.Add(self.firstNameCtrl)
        self.lineOneSizer.AddSpacer(25)
        self.lastNameLabel = wx.StaticText(self, -1, "Last Name:")
        self.lineOneSizer.Add(self.lastNameLabel, 0, ALIGN_LCA)
        self.lastNameCtrl = wx.TextCtrl(self, size=(120,31))
        self.lastNameCtrl.SetMaxLength(30)
        self.lineOneSizer.Add(self.lastNameCtrl)
        self.sizer.Add(self.lineOneSizer)
        self.sizer.AddSpacer(12)

        self.lineTwoSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.countryLabel = wx.StaticText(self, -1, "Country:")
        self.lineTwoSizer.Add(self.countryLabel, 0, ALIGN_LCA)
        self.countryChoice = wx.Choice(self, -1)
        self.lineTwoSizer.Add(self.countryChoice)
        self.sizer.Add(self.lineTwoSizer)
        self.sizer.AddSpacer(12)

        self.lineThreeSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.emailAddressLabel = wx.StaticText(self, -1, "Email Address:")
        self.lineThreeSizer.Add(self.emailAddressLabel, 0, ALIGN_LCA)
        self.emailAddressCtrl = wx.TextCtrl(self, size=(170,31))
        self.emailAddressCtrl.SetMaxLength(50)
        self.lineThreeSizer.Add(self.emailAddressCtrl)
        self.sizer.Add(self.lineThreeSizer)
        self.sizer.AddSpacer(12)

        self.lineFourSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.passwordLabel = wx.StaticText(self, -1, "Password:")
        self.lineFourSizer.Add(self.passwordLabel, 0, ALIGN_LCA)
        self.passwordCtrl = wx.TextCtrl(self, -1, "", style=wx.TE_PASSWORD, 
                                        size=(120, 31))
        self.passwordCtrl.SetMaxLength(30)
        self.lineFourSizer.Add(self.passwordCtrl)
        self.lineFourSizer.AddSpacer(25)
        self.retypePasswordLabel = wx.StaticText(self, -1, "Confirm Password:")
        self.lineFourSizer.Add(self.retypePasswordLabel, 0, ALIGN_LCA)
        self.retypePasswordCtrl = wx.TextCtrl(self, -1, "",
                                              style=wx.TE_PASSWORD,
                                              size=(120, 31))
        self.retypePasswordCtrl.SetMaxLength(30)
        self.lineFourSizer.Add(self.retypePasswordCtrl)
        self.sizer.Add(self.lineFourSizer)
        self.sizer.AddSpacer(12)

        self.lineFiveSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.securityQuestionLabel = wx.StaticText(self, -1,
                                                   "Security Question:")
        self.lineFiveSizer.Add(self.securityQuestionLabel, 0, ALIGN_LCA)
        self.securityQuestionChoice = wx.Choice(self, -1,
            choices=self.securityQuestionChoices)
        self.lineFiveSizer.Add(self.securityQuestionChoice)
        self.sizer.Add(self.lineFiveSizer)
        self.sizer.AddSpacer(12)

        self.lineSixSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.securityAnswerLabel = wx.StaticText(self, -1, "Security Answer:")
        self.lineSixSizer.Add(self.securityAnswerLabel, 0, ALIGN_LCA)
        self.securityAnswerCtrl = wx.TextCtrl(self, size=(170,31))
        self.securityAnswerCtrl.SetMaxLength(30)
        self.lineSixSizer.Add(self.securityAnswerCtrl)
        self.sizer.Add(self.lineSixSizer)
        self.sizer.AddSpacer(12)

        self.lineSevenSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.termsCheckBox = wx.CheckBox(self,
            label="I agree to the Logitech Terms of Use and Privacy Policy")
        self.sizer.Add(self.termsCheckBox)
        self.contactCheckBox = wx.CheckBox(self,
            label="Keep me informed about Logitech offers and products.")
        self.sizer.Add(self.contactCheckBox)

        self.lineEightSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.termsOfUseLink = wx.HyperlinkCtrl(self, -1, "Terms of Use",
            "https://files.myharmony.com/Assets/legal/en/termsofuse.html")
        self.lineEightSizer.Add(self.termsOfUseLink)
        self.lineEightSizer.AddSpacer(10)
        self.privacyPolicyLink = wx.HyperlinkCtrl(self, -1, "Privacy Policy",
            "https://files.myharmony.com/Assets/legal/en/privacypolicy.html")
        self.lineEightSizer.Add(self.privacyPolicyLink)
        self.sizer.Add(self.lineEightSizer)

        self.SetSizerAndFit(self.sizer)

        self.next = None

    def OnActivated(self, prev_page, data):
        self.parent.ReenableNext()
        self.parent.ReenableBack()
        if data is not None:
            self.isUpdate = True
            self.parent.title.SetLabel("Account Update")
            self._msg_welcome = self._msg_welcome_update
        else:
            self.isUpdate = False
            self._msg_welcome = self._msg_welcome_create
            self.ClearData()
        self.textMessage.UpdateText(self._msg_welcome)
        BackgroundTask((self.LoadData,), (self.LoadDataUI,), False)
        return (None, None)

    def LoadData(self):
        self.countryLists = mhMgr.GetCountryLists()
        self.countryCodes = self.countryLists[0]
        self.countries = self.countryLists[1]
        if self.isUpdate:
            self.details = mhMgr.GetAccountDetails()

    def LoadDataUI(self, loadDataResult):
        self.countryChoice.SetItems(self.countries)
        if self.isUpdate:
            self.PopulateData()
        self.parent.Show()

    def PopulateData(self):
        details = self.details
        self.firstNameCtrl.SetValue(details.firstName)
        self.lastNameCtrl.SetValue(details.lastName)
        self.countryChoice.SetSelection(
            self.countryCodes.index(details.country)
        )
        self.emailAddressCtrl.SetValue(details.email)
        self.passwordCtrl.SetValue(details.password) 
        self.retypePasswordCtrl.SetValue(details.password)
        self.securityQuestionChoice.SetSelection(
            self.securityQuestionTokens.index(details.securityQuestion)
        )
        self.securityAnswerCtrl.Clear()
        self.termsCheckBox.SetValue(True)
        if str(details.keepMeInformed).lower() == "true":
            self.contactCheckBox.SetValue(True)
        else:
            self.contactCheckBox.SetValue(False)

    def ClearData(self):
        self.firstNameCtrl.Clear()
        self.lastNameCtrl.Clear()
        self.countryChoice.SetSelection(0)
        self.emailAddressCtrl.Clear()
        self.passwordCtrl.Clear()
        self.retypePasswordCtrl.Clear()
        self.securityQuestionChoice.SetSelection(0)
        self.securityAnswerCtrl.Clear()
        self.termsCheckBox.SetValue(False)
        self.contactCheckBox.SetValue(False)

    def GetTitle(self):
        return "Account Creation"

    def IsCancelInitiallyDisabled(self):
        return False

    def OnNext(self):
        if self.firstNameCtrl.IsEmpty():
            wx.MessageBox('First Name Required.',
                          'Error', wx.OK | wx.ICON_WARNING)
            return
        if self.lastNameCtrl.IsEmpty():
            wx.MessageBox('Last Name Required.',
                          'Error', wx.OK | wx.ICON_WARNING)
            return
        if self.countryChoice.GetSelection() == 0:
            wx.MessageBox('Country Selection Required.',
                          'Error', wx.OK | wx.ICON_WARNING)
            return
        if self.emailAddressCtrl.IsEmpty():
            wx.MessageBox('Email Address Required.',
                          'Error', wx.OK | wx.ICON_WARNING)
            return
        if len(self.passwordCtrl.GetValue()) < 4:
            wx.MessageBox('Passwords must be at least 4 characters.',
                          'Error', wx.OK | wx.ICON_WARNING)
            return
        if self.passwordCtrl.GetValue() != self.retypePasswordCtrl.GetValue():
            wx.MessageBox('Passwords do not match.',
                          'Error', wx.OK | wx.ICON_WARNING)
            return
        if (not self.isUpdate) and (self.securityAnswerCtrl.IsEmpty()):
            wx.MessageBox('Security Answer Required.',
                          'Error', wx.OK | wx.ICON_WARNING)
            return
        if self.termsCheckBox.GetValue() is False:
            wx.MessageBox('Must accept terms and conditions.',
                          'Error', wx.OK | wx.ICON_WARNING)
            return

        mhAccount = MHAccountDetails()
        mhAccount.firstName = self.firstNameCtrl.GetValue()
        mhAccount.lastName = self.lastNameCtrl.GetValue()
        mhAccount.country = self.countryCodes[self.countryChoice.GetSelection()]
        mhAccount.email = self.emailAddressCtrl.GetValue()
        mhAccount.password = self.passwordCtrl.GetValue()
        mhAccount.securityQuestion = \
            self.securityQuestionTokens[self.securityQuestionChoice.GetSelection()]
        mhAccount.securityAnswer = self.securityAnswerCtrl.GetValue()
        if self.contactCheckBox.GetValue() is True:
            mhAccount.keepMeInformed = "true"
        else:
            mhAccount.keepMeInformed = "false"

        if not self.isUpdate:
            BackgroundTask((self.DoCreateAccount, mhAccount),
                           (self.FinishCreateAccount,))
        else:
            BackgroundTask((self.DoUpdateAccount, mhAccount),
                           (self.FinishUpdateAccount,))

    def DoCreateAccount(self, mhAccount):
        return mhMgr.CreateAccount(mhAccount)

    def FinishCreateAccount(self, result):
        if result is None:
            wx.MessageBox('Account created successfully.', 'Success', wx.OK)
            self.parent._SetPage(self.resources.page_welcome, None)
        else:
            wx.MessageBox(result, 'Error', wx.OK | wx.ICON_WARNING)

    def DoUpdateAccount(self, mhAccount):
        return mhMgr.UpdateAccountDetails(mhAccount)

    def FinishUpdateAccount(self, result):
        if result:
            wx.MessageBox('Account updated successfully.', 'Success', wx.OK)
            self.parent._SetPage(self.resources.page_remote_select, False)
        else:
            wx.MessageBox('Account update failed.', 'Failure',
                          wx.OK | wx.ICON_WARNING)

    def GetNext(self):
        return (self.next, None)

    def GetBack(self):
        if self.isUpdate:
            return (self.resources.page_remote_select, False)
        else:
            return (self.resources.page_welcome, None)

class ConfigureDevicePanel(WizardPanelBase):
    _msg_welcome = (
        "Please select a remote control button to modify on the left.\n" +
        "Then, select a device command to assign to that button on the right.\n"
        + "If there is an existing device command assigned to a button, it\n"
        + "will be selected when you select the remote control button."
    )

    def __init__(self, parent, resources):
        self.parent = parent
        self.resources = resources

        WizardPanelBase.__init__(self, parent, resources)

        self.LC_WIDTH = 225
        self.LC_HEIGHT = 225
        self.LC_SIZE = (self.LC_WIDTH, self.LC_HEIGHT)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.textMessage = WrappedStaticText(self)
        self.sizer.Add(self.textMessage, 0, ALIGN_XTA, 5)

        self.sizer.AddSpacer(10)
        self.hSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.leftSizer = wx.BoxSizer(wx.VERTICAL)
        self.remoteButtonsLabel = wx.StaticText(self, -1, "Remote Buttons:")
        self.leftSizer.Add(self.remoteButtonsLabel, 0, 0, 0)
        self.remoteButtonsListCtrl = wx.ListCtrl(self, size=self.LC_SIZE,
            style=wx.LC_REPORT | wx.LC_SINGLE_SEL | wx.LC_NO_HEADER)
        self.remoteButtonsListCtrl.InsertColumn(0, "")
        self.remoteButtonsListCtrl.SetColumnWidth(0, self.LC_WIDTH - 1)
        self.remoteButtonsListCtrl.Bind(wx.EVT_LIST_ITEM_SELECTED,
                                        self.OnRemoteButtonSelection)
        self.leftSizer.Add(self.remoteButtonsListCtrl)
        self.hSizer.Add(self.leftSizer, 0, 0, 0)
        self.hSizer.AddSpacer(20)
        self.rightSizer = wx.BoxSizer(wx.VERTICAL)
        self.deviceCommandsLabel = wx.StaticText(self, -1, "Device Commands:")
        self.rightSizer.Add(self.deviceCommandsLabel, 0, 0, 0)
        self.deviceCommandsListCtrl = wx.ListCtrl(self, size=self.LC_SIZE,
            style=wx.LC_REPORT | wx.LC_SINGLE_SEL | wx.LC_NO_HEADER)
        self.deviceCommandsListCtrl.InsertColumn(0, "")
        self.deviceCommandsListCtrl.SetColumnWidth(0, self.LC_WIDTH - 1)
        self.rightSizer.Add(self.deviceCommandsListCtrl)
        self.hSizer.Add(self.rightSizer, 0, 0, 0)
        self.sizer.Add(self.hSizer, 0, 0, 0)
        self.sizer.AddSpacer(10)

        self.bottomSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.updateButton = wx.Button(self, label="Update Button")
        self.updateButton.Bind(wx.EVT_BUTTON, self.OnUpdate)
        self.updateButton.SetToolTip(wx.ToolTip(
                "Update selected button with the selected device command"
        ))
        self.bottomSizer.Add(self.updateButton, 0, 0, 0)
        self.overrideButton = wx.Button(self, label="Override Command")
        self.overrideButton.Bind(wx.EVT_BUTTON, self.OnOverride)
        self.overrideButton.SetToolTip(wx.ToolTip(
                "Learn IR command from existing remote to replace a command"
        ))
        self.bottomSizer.Add(self.overrideButton, 0, 0, 0)
        self.addButton = wx.Button(self, label="Add Command")
        self.addButton.Bind(wx.EVT_BUTTON, self.OnAdd)
        self.addButton.SetToolTip(wx.ToolTip(
                "Learn IR command from existing remote as a new command"
        ))
        self.bottomSizer.Add(self.addButton, 0, 0, 0)
        self.restoreButton = wx.Button(self, label="Restore Command")
        self.restoreButton.Bind(wx.EVT_BUTTON, self.OnRestore)
        self.restoreButton.SetToolTip(wx.ToolTip(
                "Remove overriden IR command/restore to official command"
        ))
        self.bottomSizer.Add(self.restoreButton, 0, 0, 0)
        self.sizer.Add(self.bottomSizer, 0, 0, 0)

        self.SetSizerAndFit(self.sizer)

        self.next = None

    def OnActivated(self, prev_page, data):
        self.textMessage.UpdateText(self._msg_welcome)
        self.parent.ReenableBack()
        self.skinId, self.product, self.deviceId, self.deviceName = data
        self.parent.title.SetLabel(self.deviceName)
        BackgroundTask((self.LoadData,), (self.LoadDataUI,), False)
        return (None, None)

    def LoadData(self):
        # Only populate the buttons list if this remote has buttons; if the
        # 'remote' is a Harmony Link, it has no ProductButtonList.
        self.remoteButtons = None
        capabilities = self.product.SupportedCapabilities.SupportedCapability
        if "CompiledRemoteButtonMapping" in capabilities:
            self.remoteButtons = mhMgr.GetProductButtonList(self.skinId)
        self.deviceCommands = mhMgr.GetCommands(self.deviceId)
        self.buttonMap = mhMgr.GetButtonMap(self.deviceId)

    def LoadDataUI(self, loadDataResult):
        self.remoteButtonsListCtrl.DeleteAllItems()
        if self.remoteButtons is not None:
            for i in range(len(self.remoteButtons)):
                buttonKey = self.remoteButtons[i].ButtonKey
                self.remoteButtonsListCtrl.InsertStringItem(i, buttonKey)
            
        self.deviceCommandsListCtrl.DeleteAllItems()
        if self.deviceCommands is not None:
            for i in range(len(self.deviceCommands)):
                command = self.deviceCommands[i]
                name = command.Name
                if command.IsLearned == "true":
                    name += "*"
                self.deviceCommandsListCtrl.InsertStringItem(i, name)
                if self.IsCommandMapped(command):
                    color = wx.LIGHT_GREY
                    self.deviceCommandsListCtrl.SetItemTextColour(i, color)

        self.parent.Show()

    def OnRemoteButtonSelection(self, event):
        buttonSelection = self.remoteButtonsListCtrl.GetFirstSelected()
        key = self.remoteButtons[buttonSelection].ButtonKey
        foundCommand = self.FindCommand(key)
        if foundCommand is not None:
            for i in range(len(self.deviceCommands)):
                # ButtonAssignment might not be a CommandButtonAssignment, but
                # ignore the error if it isn't.
                try:
                    if foundCommand.ButtonAssignment.CommandId.Value == \
                            self.deviceCommands[i].Id.Value:
                        self.deviceCommandsListCtrl.Select(i)
                        self.deviceCommandsListCtrl.EnsureVisible(i)
                        return
                except AttributeError:
                    pass
        commandSelection = self.deviceCommandsListCtrl.GetFirstSelected()
        if commandSelection != -1:
            self.deviceCommandsListCtrl.Select(commandSelection, 0)

    def FindCommand(self, buttonKey):
        for button in self.buttonMap.Buttons.AbstractButton:
            if buttonKey == button.ButtonKey:
                return button
        return None

    def IsCommandMapped(self, command):
        for button in self.buttonMap.Buttons.AbstractButton:
            # ButtonAssignment might not be a CommandButtonAssignment, but
            # ignore the error if it isn't.
            try:
                if button.ButtonAssignment.CommandId.Value == \
                   command.Id.Value:
                    return True
            except AttributeError:
                pass
        return False

    def OnUpdate(self, event):
        buttonSelection = self.remoteButtonsListCtrl.GetFirstSelected()
        commandSelection = self.deviceCommandsListCtrl.GetFirstSelected()
        if (buttonSelection == -1) or (commandSelection == -1):
            wx.MessageBox('Please select a button and command to assign.',
                          'No selection(s) made.', wx.OK | wx.ICON_WARNING)
            return
        button = self.remoteButtons[buttonSelection]
        command = self.deviceCommands[commandSelection]
        BackgroundTask((self.DoUpdate, button, command), (self.LoadDataUI,))

    def DoUpdate(self, button, command):
        mhMgr.UpdateButtonMap(self.buttonMap, button, command)
        self.LoadData()

    def OnOverride(self, event):
        commandSelection = self.deviceCommandsListCtrl.GetFirstSelected()
        if commandSelection == -1:
            wx.MessageBox('Please select a command to override.',
                          'No command selected.', wx.OK | wx.ICON_WARNING)
            return
        command = self.deviceCommands[commandSelection].Name
        self.UpdateIR(command)

    def OnAdd(self, event):
        dlg = wx.TextEntryDialog(None, "Enter the name of the new command:",
                                 "Add Command")
        if dlg.ShowModal() != wx.ID_OK:
            return
        command = dlg.GetValue()
        self.UpdateIR(command)

    def OnRestore(self, event):
        commandSelection = self.deviceCommandsListCtrl.GetFirstSelected()
        if commandSelection == -1:
            wx.MessageBox('Please select a command to restore.',
                          'No command selected.', wx.OK | wx.ICON_WARNING)
            return
        command = self.deviceCommands[commandSelection]
        if command.IsLearned != "true":
            wx.MessageBox('Selected command is not a learned command.',
                          'Error', wx.OK | wx.ICON_WARNING)
            return
        BackgroundTask((self.DoRestore, command), (self.FinishRestore,))

    def DoRestore(self, command):
        result = mhMgr.DeleteIRCommand(command.Id, self.deviceId)
        self.LoadData()
        return result

    def FinishRestore(self, result):
        if result is not None:
            wx.MessageBox('IR command deletion failed: ' + result, 'Error',
                          wx.OK | wx.ICON_WARNING)
        self.LoadDataUI(None)

    def UpdateIR(self, commandName):
        msg = 'Please ensure your remote control is connected.'
        wx.MessageBox(msg, 'Connect Remote', wx.OK)
        try:
            libconcord.init_concord()
        except:
            msg = '%s\n    (libconcord function %s error %d)\n\n' % (
                sys.exc_value.result_str,
                sys.exc_value.func,
                sys.exc_value.result
            )
            wx.MessageBox('Could not detect remote: ' + msg, 'Error',
                          wx.OK | wx.ICON_WARNING)
            return

        msg = 'Please place your two remotes 3 inches (8 cm) apart.  After ' \
            + 'pressing OK, press the button on your non-Harmony remote that ' \
            + 'you wish to be learned.'
        wx.MessageBox(msg, 'Position Remotes', wx.OK)
        self.commandName = commandName
        self.carrierClock = c_uint()
        self.signal = POINTER(c_uint)()
        self.signalLength = c_uint()
        BackgroundTask((self.DoLearnIR,), (self.FinishLearnIR,), 
                       throbberTitle=ThrobberDialog.TITLE_REMOTE)

    def DoLearnIR(self):
        msg = None
        try:
            libconcord.learn_from_remote(
                byref(self.carrierClock),
                byref(self.signal),
                byref(self.signalLength),
                libconcord.callback_type(dummy_callback_imp),
                None
            )
        except:
            msg = '%s\n    (libconcord function %s error %d)\n\n' % (
                sys.exc_value.result_str,
                sys.exc_value.func,
                sys.exc_value.result
            )
            libconcord.deinit_concord()

    def FinishLearnIR(self, msg):
        if msg is not None:
            wx.MessageBox('IR learning failed: ' + msg + '\nPerhaps you did '
                          + 'not press a key?', 'Error',
                          wx.OK | wx.ICON_WARNING)
            return

        self.rawSequence = c_char_p()
        libconcord.encode_for_posting(self.carrierClock, self.signal,
                                      self.signalLength, self.rawSequence)
        BackgroundTask((self.DoUpdateIR,), (self.FinishUpdateIR,))

    def DoUpdateIR(self):
        result = mhMgr.UpdateIRCommand(self.commandName, self.rawSequence.value,
                                       self.deviceId)
        self.LoadData()
        return result

    def FinishUpdateIR(self, result):
        if result is not None:
            wx.MessageBox('IR learning update failed: ' + result, 'Error',
                          wx.OK | wx.ICON_WARNING)
        self.LoadDataUI(None)
        libconcord.delete_ir_signal(self.signal)
        libconcord.delete_encoded_signal(self.rawSequence)
        libconcord.deinit_concord()

    def GetTitle(self):
        return "Device Configuration"

    def IsCancelInitiallyDisabled(self):
        return False

    def OnNext(self):
        pass

    def GetNext(self):
        return (self.next, None)

    def GetBack(self):
        return (self.resources.page_remote_configuration, None)

class FavoriteChannelsPanel(WizardPanelBase):
    _msg_welcome = (
        "Please select a favorite channel button to modify on the left.\n" +
        "Then, type the channel on the right as you would enter it on your " +
        "remote.\n"
        + "If there is an existing channel assigned to a button, it\n"
        + "will be displayed when you select the favorite control button."
    )

    def __init__(self, parent, resources):
        self.parent = parent
        self.resources = resources

        WizardPanelBase.__init__(self, parent, resources)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.textMessage = WrappedStaticText(self)
        self.sizer.Add(self.textMessage, 0, ALIGN_XTA, 5)

        self.hSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.leftSizer = wx.BoxSizer(wx.VERTICAL)
        self.remoteButtonsLabel = wx.StaticText(self, -1, "Buttons:")
        self.leftSizer.Add(self.remoteButtonsLabel, 0, 0, 0)
        self.remoteButtonsListBox = wx.ListBox(self, style=wx.LB_SINGLE)
        self.remoteButtonsListBox.Bind(wx.EVT_LISTBOX,
                                       self.OnRemoteButtonSelection)
        self.leftSizer.Add(self.remoteButtonsListBox, 0, 0, 0)
        self.hSizer.Add(self.leftSizer, 0, 0, 0)
        self.hSizer.AddSpacer(20)
        self.rightSizer = wx.BoxSizer(wx.VERTICAL)
        self.channelLabel = wx.StaticText(self, -1, "Channel:")
        self.rightSizer.Add(self.channelLabel, 0, 0, 0)
        self.channelCtrl = wx.TextCtrl(self, -1, "")
        self.channelCtrl.SetMinSize((70, 31))
        self.rightSizer.Add(self.channelCtrl, 0, 0, 0)
        self.hSizer.Add(self.rightSizer, 0, 0, 0)
        self.sizer.Add(self.hSizer, 0, 0, 0)
        self.sizer.AddSpacer(20)

        self.updateButton = wx.Button(self, label="Update Button")
        self.updateButton.Bind(wx.EVT_BUTTON, self.OnUpdate)
        self.updateButton.SetToolTip(wx.ToolTip(
                "Save selected button assignment"
        ))
        self.sizer.Add(self.updateButton, 0, 0, 0)

        self.SetSizerAndFit(self.sizer)

        self.next = None

    def OnActivated(self, prev_page, data):
        self.textMessage.UpdateText(self._msg_welcome)
        self.parent.ReenableBack()
        self.skinId, self.deviceId, self.deviceName = data
        self.parent.title.SetLabel(self.deviceName)
        BackgroundTask((self.LoadData,), (self.LoadDataUI,), False)
        return (None, None)

    def LoadData(self):
        self.remoteButtons = mhMgr.GetProductButtonList(self.skinId)
        self.buttonMap = mhMgr.GetButtonMap(self.deviceId)

    def LoadDataUI(self, loadDataResult):
        self.remoteButtonsList = []
        if self.remoteButtons is not None:
            for button in self.remoteButtons:
                if button.ButtonType == "FavoriteChannelButton":
                    self.remoteButtonsList.append(button.ButtonKey)
            self.remoteButtonsListBox.Set(self.remoteButtonsList)
        self.Fit()
        self.parent.Show()

    def OnRemoteButtonSelection(self, event):
        key = self.remoteButtons[self.remoteButtonsListBox.GetSelection()] \
            .ButtonKey
        foundCommand = self.FindCommand(key)
        if foundCommand is not None:
            # ButtonAssignment might not be a ChannelButtonAssignment, but
            # ignore the error if it isn't.
            try:
                self.channelCtrl.SetValue(foundCommand.ButtonAssignment.Channel)
                return
            except AttributeError:
                pass
        self.channelCtrl.Clear()

    def FindCommand(self, buttonKey):
        for button in self.buttonMap.Buttons.AbstractButton:
            if buttonKey == button.ButtonKey:
                return button
        return None

    def OnUpdate(self, event):
        buttonSelection = self.remoteButtonsListBox.GetSelection()
        channel = self.channelCtrl.GetValue()
        if (buttonSelection == -1) or (not channel.isdigit()):
            wx.MessageBox('Please select a button and channel to assign.',
                          'No selection(s) made.', wx.OK | wx.ICON_WARNING)
            return
        button = self.remoteButtons[buttonSelection]
        BackgroundTask((self.DoUpdate, button, channel), (self.LoadDataUI,))

    def DoUpdate(self, button, channel):
        mhMgr.UpdateButtonMap(self.buttonMap, button, channel,
                              isChannelButton = True)
        self.LoadData()

    def GetTitle(self):
        return "Favorite Channels"

    def IsCancelInitiallyDisabled(self):
        return False

    def OnNext(self):
        pass

    def GetNext(self):
        return (self.next, None)

    def GetBack(self):
        return (self.resources.page_remote_configuration, None)

class SetupWatchTVPanel(WizardPanelBase):
    _msg_welcome = (
        "Please select the devices to be turned on when you press the\n" +
        "Watch TV button.  Then, select the proper input for each device, if " +
        "applicable.  Re-order the devices to change the power-on order.\n"
    )

    def __init__(self, parent, resources):
        self.parent = parent
        self.resources = resources

        WizardPanelBase.__init__(self, parent, resources)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.textMessage = WrappedStaticText(self)
        self.sizer.Add(self.textMessage, 0, ALIGN_XTA, 5)

        self.listBoxLabel = wx.StaticText(self, -1, "Selected Devices:" +
                                          "\t\t\t  Unselected Devices:")
        self.sizer.Add(self.listBoxLabel)

        self.buttonSize = (40, 40)
        self.listBoxSize = (160, 160)
        self.deviceSelectionSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.selectedDevicesListBox = wx.ListBox(self, style=wx.LB_SINGLE,
                                                 size=self.listBoxSize)
        self.deviceSelectionSizer.Add(self.selectedDevicesListBox)
        self.deviceSelectionButtonSizer = wx.BoxSizer(wx.VERTICAL)
        self.selectDeviceButton = wx.Button(self, label=u'\u2190',
                                            size=self.buttonSize)
        self.selectDeviceButton.SetToolTip(wx.ToolTip(
                "Add device to Watch TV configuration"
        ))
        self.selectDeviceButton.Bind(wx.EVT_BUTTON, self.OnSelect)
        self.deviceSelectionButtonSizer.Add(self.selectDeviceButton)
        self.removeDeviceButton = wx.Button(self, label=u'\u2192',
                                            size=self.buttonSize)
        self.removeDeviceButton.SetToolTip(wx.ToolTip(
                "Remove device from Watch TV configuration"
        ))
        self.removeDeviceButton.Bind(wx.EVT_BUTTON, self.OnRemove)
        self.deviceSelectionButtonSizer.Add(self.removeDeviceButton)
        self.raiseDeviceButton = wx.Button(self, label=u'\u2191',
                                           size=self.buttonSize)
        self.raiseDeviceButton.SetToolTip(wx.ToolTip(
                "Move device earlier in power-on order"
        ))
        self.raiseDeviceButton.Bind(wx.EVT_BUTTON, self.OnRaise)
        self.deviceSelectionButtonSizer.Add(self.raiseDeviceButton)
        self.lowerDeviceButton = wx.Button(self, label=u'\u2193',
                                           size=self.buttonSize)
        self.lowerDeviceButton.SetToolTip(wx.ToolTip(
                "Move device later in power-on order"
        ))
        self.lowerDeviceButton.Bind(wx.EVT_BUTTON, self.OnLower)
        self.deviceSelectionButtonSizer.Add(self.lowerDeviceButton)
        self.deviceSelectionSizer.Add(self.deviceSelectionButtonSizer)
        self.unselectedDevicesListBox = wx.ListBox(self, style=wx.LB_SINGLE,
                                                   size=self.listBoxSize)
        self.deviceSelectionSizer.Add(self.unselectedDevicesListBox)
        self.sizer.Add(self.deviceSelectionSizer)

        # Placeholder sizer for input selection widgets
        self.inputSelectionSizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.inputSelectionSizer)

        self.saveChangesButton = wx.Button(self, label="Save Changes")
        self.saveChangesButton.Bind(wx.EVT_BUTTON, self.OnSave)
        self.sizer.Add(self.saveChangesButton)

        self.SetSizerAndFit(self.sizer)

        self.next = None

    def FindRole(self, device, activity):
        if activity:
            for role in activity.Roles.AbstractActivityRole:
                if role.DeviceId.Value == device.Id.Value and \
                        role.DeviceId.IsPersisted == device.Id.IsPersisted:
                    return role
        return None

    def GetDevice(self, role):
        for device in self.devices:
            if device.Id.Value == role.DeviceId.Value:
                return (device.Name, device.Id)

    def LoadData(self):
        self.devices = mhMgr.GetDevices(self.remote.Id)
        self.activity = mhMgr.GetActivity(self.remote.Id, "WatchTV")
        self.inputNames = {}
        for device in self.devices:
            self.inputNames[device.Id] = mhMgr.GetDeviceInputNames(device.Id)

    def LoadDataUI(self, loadDataResult):
        self.selectedDevicesListBox.Clear()
        self.unselectedDevicesListBox.Clear()
        self.inputSelectionSizer.Clear(deleteWindows=True)
        self.inputSelectionWidgets = {}

        # Add devices from the roles first since they need to be added in role
        # order
        if self.activity:
            for role in self.activity.Roles.AbstractActivityRole:
                deviceName, deviceId = self.GetDevice(role)
                self.selectedDevicesListBox.Append(deviceName, deviceId)

        # Now add the other devices.
        for device in self.devices:
            role = self.FindRole(device, self.activity)
            if not role:
                self.unselectedDevicesListBox.Append(device.Name, device.Id)
            inputNames = self.inputNames[device.Id]
            if inputNames:
                label = wx.StaticText(self, -1, "Input for " + device.Name
                                      + ":")
                choice = wx.Choice(self, -1, choices=inputNames)
                try:
                    choice.SetSelection(choice.FindString(
                            role.SelectedInput.Name))
                except:
                    pass
                sizer = wx.BoxSizer(wx.HORIZONTAL)
                sizer.Add(label, flag=wx.ALIGN_CENTER_VERTICAL)
                sizer.Add(choice)
                self.inputSelectionSizer.Add(sizer)
                self.inputSelectionWidgets[device.Id] = choice
        self.Layout()
        self.parent.Show()

    def OnActivated(self, prev_page, remote):
        self.textMessage.UpdateText(self._msg_welcome)
        self.parent.ReenableBack()
        self.remote = remote
        BackgroundTask((self.LoadData,), (self.LoadDataUI,), False)
        return (None, None)

    def OnSelect(self, event):
        selection = self.unselectedDevicesListBox.GetSelection()
        if selection != wx.NOT_FOUND:
            self.selectedDevicesListBox.Append(
                self.unselectedDevicesListBox.GetStringSelection(),
                self.unselectedDevicesListBox.GetClientData(selection))
            self.unselectedDevicesListBox.Delete(selection)

    def OnRemove(self, event):
        selection = self.selectedDevicesListBox.GetSelection()
        if selection != wx.NOT_FOUND:
            self.unselectedDevicesListBox.Append(
                self.selectedDevicesListBox.GetStringSelection(),
                self.selectedDevicesListBox.GetClientData(selection))
            self.selectedDevicesListBox.Delete(selection)

    def OnRaise(self, event):
        selection = self.selectedDevicesListBox.GetSelection()
        if selection != wx.NOT_FOUND and selection != 0:
            selectionString = self.selectedDevicesListBox.GetStringSelection()
            clientData = self.selectedDevicesListBox.GetClientData(selection)
            self.selectedDevicesListBox.Delete(selection)
            self.selectedDevicesListBox.Insert(selectionString, selection - 1,
                                               clientData)
            self.selectedDevicesListBox.SetSelection(selection - 1)

    def OnLower(self, event):
        selection = self.selectedDevicesListBox.GetSelection()
        lastItem = self.selectedDevicesListBox.GetCount() - 1
        if selection != wx.NOT_FOUND and selection != lastItem:
            selectionString = self.selectedDevicesListBox.GetStringSelection()
            clientData = self.selectedDevicesListBox.GetClientData(selection)
            self.selectedDevicesListBox.Delete(selection)
            self.selectedDevicesListBox.Insert(selectionString, selection + 1,
                                               clientData)
            self.selectedDevicesListBox.SetSelection(selection + 1)

    def OnSave(self, event):
        if self.selectedDevicesListBox.GetCount() == 0:
            if self.activity is not None:
                BackgroundTask((self.DoDelete,), (self.LoadDataUI,))
        else:
            deviceInfo = []
            for index in range(self.selectedDevicesListBox.GetCount()):
                deviceId = self.selectedDevicesListBox.GetClientData(index)
                try:
                    inputName = self.inputSelectionWidgets[deviceId].\
                        GetStringSelection()
                except KeyError:
                    inputName = None
                deviceInfo.append((deviceId, inputName))
            BackgroundTask((self.DoSave, deviceInfo), (self.LoadDataUI,))

    def DoDelete(self):
        mhMgr.DeleteActivity(self.activity)
        self.LoadData()

    def DoSave(self, deviceInfo):
        mhMgr.SaveWatchTVActivity(self.remote.Id, deviceInfo, self.activity)
        self.LoadData()

    def GetTitle(self):
        return "Setup Watch TV Button"

    def IsCancelInitiallyDisabled(self):
        return False

    def OnNext(self):
        pass

    def GetNext(self):
        return (self.next, None)

    def GetBack(self):
        return (self.resources.page_remote_configuration, None)

class ConfigureHarmonyLinkSettingsPanel(WizardPanelBase):
    _msg_welcome = (
        "On this page, you can configure the Room and WiFi settings for\n" +
        "your Harmony Link.\n"
    )

    def __init__(self, parent, resources):
        self.parent = parent
        self.resources = resources

        self.roomNames = [ "", "Basement", "Bedroom", "Den", "Dorm",
                           "Family Room", "Hallway", "Kitchen", "Living Room",
                           "Media Room", "Office", "Rec Room" ]
        self.serviceLink = "https://svcs.myharmony.com/Discovery"
        self.encryptionTypes = [ "OPEN", "WEP", "WPA-PSK", "WPA2-PSK" ]
        self.labelSize = (133, 21)

        WizardPanelBase.__init__(self, parent, resources)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.textMessage = WrappedStaticText(self)
        self.sizer.Add(self.textMessage, 0, ALIGN_XTA, 5)

        self.roomNameSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.roomNameLabel = wx.StaticText(self, -1, "Room Name:")
        self.roomNameLabel.SetMinSize(self.labelSize)
        self.roomNameSizer.Add(self.roomNameLabel,
                               flag=wx.ALIGN_CENTER_VERTICAL)
        self.roomNameChoice = wx.Choice(self, -1, choices=self.roomNames)
        self.roomNameSizer.Add(self.roomNameChoice)
        self.sizer.Add(self.roomNameSizer)

        self.sizer.AddSpacer(25)

        self.wifiNetworksSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.wifiNetworksLabel = wx.StaticText(self, -1, "Available Networks:")
        self.wifiNetworksSizer.Add(self.wifiNetworksLabel,
                                   flag=wx.ALIGN_CENTER_VERTICAL)
        self.wifiNetworksListBox = wx.ListBox(self, style=wx.LB_SINGLE,
                                         size=(200, 62))
        self.wifiNetworksListBox.Bind(wx.EVT_LISTBOX, self.OnSelectNetwork)
        self.wifiNetworksSizer.Add(self.wifiNetworksListBox)
        self.sizer.Add(self.wifiNetworksSizer)

        self.ssidSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.ssidLabel = wx.StaticText(self, -1, "SSID:")
        self.ssidLabel.SetMinSize(self.labelSize)
        self.ssidSizer.Add(self.ssidLabel, flag=wx.ALIGN_CENTER_VERTICAL)
        self.ssidCtrl = wx.TextCtrl(self, -1, "")
        self.ssidCtrl.SetMinSize((200, 31))
        self.ssidSizer.Add(self.ssidCtrl)
        self.sizer.Add(self.ssidSizer)

        self.passwordSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.passwordLabel = wx.StaticText(self, -1, "Password:")
        self.passwordLabel.SetMinSize(self.labelSize)
        self.passwordSizer.Add(self.passwordLabel,
                               flag=wx.ALIGN_CENTER_VERTICAL)
        self.passwordCtrl = wx.TextCtrl(self, -1, "")
        self.passwordCtrl.SetMinSize((200, 31))
        self.passwordSizer.Add(self.passwordCtrl)
        self.sizer.Add(self.passwordSizer)

        self.encryptionSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.encryptionLabel = wx.StaticText(self, -1, "Encryption:")
        self.encryptionLabel.SetMinSize(self.labelSize)
        self.encryptionSizer.Add(self.encryptionLabel,
                                 flag=wx.ALIGN_CENTER_VERTICAL)
        self.encryptionChoice = wx.Choice(self, -1,
                                          choices=self.encryptionTypes)
        self.encryptionSizer.Add(self.encryptionChoice)
        self.sizer.Add(self.encryptionSizer)

        self.wifiStatusSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.wifiStatusLabel = wx.StaticText(self, -1, "WiFi Status:")
        self.wifiStatusLabel.SetMinSize(self.labelSize)
        self.wifiStatusSizer.Add(self.wifiStatusLabel,
                                 flag = wx.ALIGN_CENTER_VERTICAL)
        self.wifiStatusCtrl = wx.TextCtrl(self, -1, "")
        self.wifiStatusCtrl.SetEditable(False)
        self.wifiStatusCtrl.SetMinSize((300, 31))
        self.wifiStatusSizer.Add(self.wifiStatusCtrl)
        self.sizer.Add(self.wifiStatusSizer)

        self.sizer.AddSpacer(25)

        self.saveChangesButton = wx.Button(self, label="Save Changes")
        self.saveChangesButton.Bind(wx.EVT_BUTTON, self.OnSaveChanges)
        self.sizer.Add(self.saveChangesButton)

        self.SetSizerAndFit(self.sizer)

        self.next = None

    def OnActivated(self, prev_page, data):
        self.textMessage.UpdateText(self._msg_welcome)
        self.parent.ReenableBack()
        self.remote = data
        BackgroundTask((self.LoadData,), (self.LoadDataUI,), 
                       throbberTitle=ThrobberDialog.TITLE_REMOTE)
        return (None, None)

    def LoadData(self):
        try:
            libconcord.init_concord()
        except:
            msg = '%s\n    (libconcord function %s error %d)\n\n' % (
                sys.exc_value.result_str,
                sys.exc_value.func,
                sys.exc_value.result
            )
            return msg

        # We don't really care about the identity, but for the Harmony Link,
        # this seems to be the more reliable way of starting off a conversation
        # with it.
        try:
            libconcord.get_identity(dummy_cb, None)
        except:
            # If get_identity fails, try again.  The Harmony Link seems to have
            # a weird bug where it will fail once, but if you try again, it
            # will succeed.
            libconcord.get_identity(dummy_cb, None)

        self.LoadConfigProperties()
        self.LoadWifiConfig()
        self.LoadWifiNetworks()
        return None

    def LoadDataUI(self, msg):
        if msg is not None:
            wx.MessageBox('Could not detect Harmony Link: ' + msg, 'Error',
                          wx.OK | wx.ICON_WARNING)
            self.parent._SetPage(self.resources.page_remote_configuration, None)
            return
        self.LoadConfigPropertiesUI(None)
        self.LoadWifiConfigUI(None)
        self.LoadWifiNetworksUI(None)

    def LoadConfigProperties(self):
        self.remoteName = mhMgr.GetRemoteName(self.remote.Id)
        self.cfg_prop = libconcord.mh_cfg_properties()
        libconcord.mh_get_cfg_properties(byref(self.cfg_prop))

    def LoadConfigPropertiesUI(self, result):
        if self.remoteName is None:
            choiceItem = wx.NOT_FOUND
        else:
            choiceItem = self.roomNameChoice.FindString(self.remoteName)
        if choiceItem != wx.NOT_FOUND:
            self.roomNameChoice.SetSelection(choiceItem)
        else:
            self.roomNameChoice.SetSelection(0)

    def LoadWifiConfig(self):
        self.wifi_cfg = libconcord.mh_wifi_config()
        libconcord.mh_get_wifi_config(byref(self.wifi_cfg))

    def LoadWifiConfigUI(self, result):
        self.ssidCtrl.SetValue(self.wifi_cfg.ssid)
        self.passwordCtrl.SetValue(self.wifi_cfg.password)
        encNum = self.encryptionChoice.FindString(self.wifi_cfg.encryption)
        if encNum != wx.NOT_FOUND:
            self.encryptionChoice.SetSelection(encNum)
        else:
            self.encryptionChoice.SetSelection(0)
        if self.wifi_cfg.connect_status == "connected":
            self.wifiStatusCtrl.SetValue("connected")
        else:
            self.wifiStatusCtrl.SetValue(self.wifi_cfg.connect_status + ": " +
                                         self.wifi_cfg.error_code)

    def LoadWifiNetworks(self):
        self.wifi_networks = libconcord.mh_wifi_networks()
        libconcord.mh_get_wifi_networks(byref(self.wifi_networks))

    def LoadWifiNetworksUI(self, result):
        self.wifiNetworksListBox.DeselectAll()
        self.wifiNetworksListBox.Clear()
        for network in self.wifi_networks.network:
            if network.ssid == "":
                break
            self.wifiNetworksListBox.Append(network.ssid, network.encryption)

    def OnSelectNetwork(self, event):
        self.ssidCtrl.SetValue(self.wifiNetworksListBox.GetStringSelection())
        self.passwordCtrl.Clear()
        self.encryptionChoice.SetSelection(
            self.encryptionChoice.FindString(
                self.wifiNetworksListBox.GetClientData(
                    self.wifiNetworksListBox.GetSelection()
                    )
                )
            )

    def OnSaveChanges(self, event):
        # Validate Selections
        if self.roomNameChoice.GetSelection() == 0:
            wx.MessageBox('Please select a room name', 'Error',
                          wx.OK | wx.ICON_WARNING)
            return
        if self.ssidCtrl.IsEmpty():
            wx.MessageBox('Please enter an SSID', 'Error',
                          wx.OK | wx.ICON_WARNING)
            return

        # Determine which item(s) needs to be saved
        self.saveCfgProp = False
        roomName = self.roomNameChoice.GetStringSelection()
        if self.cfg_prop.host_name != roomName or self.cfg_prop.email != \
                mhMgr.email or self.remoteName != roomName:
            self.saveCfgProp = True
        self.saveWifi = False
        ssid = self.ssidCtrl.GetValue()
        password = self.passwordCtrl.GetValue()
        encryption = self.encryptionChoice.GetStringSelection()
        if encryption == "OPEN":
            encryption = ""
        if self.wifi_cfg.ssid != ssid or self.wifi_cfg.password != password or \
                self.wifi_cfg.encryption != encryption:
            self.saveWifi = True

        # Save what needs to be saved
        if self.saveWifi:
            self.wifi_cfg.ssid = ssid
            self.wifi_cfg.encryption = encryption
            self.wifi_cfg.password = password
        if self.saveCfgProp:
            self.cfg_prop.host_name = roomName
            self.cfg_prop.email = mhMgr.email
            self.cfg_prop.service_link = self.serviceLink
        if self.saveWifi or self.saveCfgProp:
            BackgroundTask((self.DoSaveChanges,), (self.FinishSaveChanges,), 
                           throbberTitle=ThrobberDialog.TITLE_REMOTE)

    def DoSaveChanges(self):
        if self.saveWifi:
            libconcord.mh_set_wifi_config(byref(self.wifi_cfg))
            self.LoadWifiConfig()
        if self.saveCfgProp:
            mhMgr.SetRemoteName(self.remote.Id, self.cfg_prop.host_name)
            libconcord.mh_set_cfg_properties(byref(self.cfg_prop))
            time.sleep(10)
            # Do not self.LoadConfigProperties() here.  The Link doesn't
            # respond very well if you do.  Just assume everything worked.

    def FinishSaveChanges(self, result):
        if self.saveWifi:
            self.LoadWifiConfigUI(None)

    def GetTitle(self):
        return "Configure Harmony Link Settings"

    def IsCancelInitiallyDisabled(self):
        return False

    def OnNext(self):
        pass

    def GetNext(self):
        return (self.next, None)

    def GetBack(self):
        libconcord.deinit_concord()
        return (self.resources.page_remote_configuration, None)

    def OnCancel(self):
        libconcord.deinit_concord()
        self.parent.OnExit(0)

class SetupActivitiesPanel(WizardPanelBase):
    _msg_welcome = (
        "Existing Activities:"
    )

    def __init__(self, parent, resources):
        self.parent = parent
        self.resources = resources

        WizardPanelBase.__init__(self, parent, resources)

        self.buttonSize = (127, 33)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.textMessage = WrappedStaticText(self)
        self.sizer.Add(self.textMessage, 0, ALIGN_XTA, 5)
        self.activitiesListBox = wx.ListBox(self, style=wx.LB_SINGLE,
                                            size=(200, 100))
        self.sizer.Add(self.activitiesListBox)
        self.sizer.AddSpacer(20)
        self.addButton = wx.Button(self, label="Add Activity",
                                   size=self.buttonSize)
        self.addButton.Bind(wx.EVT_BUTTON, self.OnAddActivity)
        self.sizer.Add(self.addButton)
        self.sizer.AddSpacer(10)
        self.editButton = wx.Button(self, label="Edit Activity",
                                    size=self.buttonSize)
        self.editButton.Bind(wx.EVT_BUTTON, self.OnEditActivity)
        self.sizer.Add(self.editButton)
        self.sizer.AddSpacer(10)
        self.deleteButton = wx.Button(self, label="Delete Activity",
                                      size=self.buttonSize)
        self.deleteButton.Bind(wx.EVT_BUTTON, self.OnDeleteActivity)
        self.sizer.Add(self.deleteButton)
        self.SetSizerAndFit(self.sizer)

        self.next = None

    def OnActivated(self, prev_page, data):
        self.textMessage.UpdateText(self._msg_welcome)
        self.parent.ReenableBack()
        if data is not None:
            self.remote = data
            BackgroundTask((self.LoadData,), (self.LoadDataUI,), False)
        return (None, None)

    def LoadData(self):
        self.activities = mhMgr.GetActivities(self.remote.Id)
        self.recommendedActivities = mhMgr.GetRecommendedActivities(
            self.remote.Id)

    def LoadDataUI(self, loadDataResult):
        self.activitiesListBox.Clear()
        if self.activities:
            for activity in self.activities:
                self.activitiesListBox.Append(activity.Name, activity)
        self.parent.Show()

    def OnAddActivity(self, event):
        activityTypeStrings = []
        for activity in self.recommendedActivities:
            activityTypeStrings.append(mhMgr.GetActivityTypeString(activity))
        dlg = wx.SingleChoiceDialog(
            self.parent,
            "Please select an activity type to add:",
            "Activity Selection",
            activityTypeStrings
        )
        if dlg.ShowModal() != wx.ID_OK:
            return
        activityType = self.recommendedActivities[dlg.GetSelection()]
        params = (self.remote, activityType, None)
        self.parent._SetPage(self.resources.page_edit_activity, params, True)

    def OnEditActivity(self, event):
        activity = self.GetSelectedActivity()
        if not activity:
            return
        params = (self.remote, activity.Type, activity)
        self.parent._SetPage(self.resources.page_edit_activity, params, True)

    def OnDeleteActivity(self, event):
        activity = self.GetSelectedActivity()
        if not activity:
            return
        BackgroundTask((self.DoDelete, activity), (self.LoadDataUI,))

    def GetSelectedActivity(self):
        activityNum = self.activitiesListBox.GetSelection()
        if activityNum == wx.NOT_FOUND:
            wx.MessageBox('Please select an activity', 'Error',
                          wx.OK | wx.ICON_WARNING)
            return None
        return self.activitiesListBox.GetClientData(activityNum)

    def DoDelete(self, activity):
        mhMgr.DeleteActivity(activity)
        self.activities = mhMgr.GetActivities(self.remote.Id)

    def GetTitle(self):
        return "Activity Setup"

    def IsCancelInitiallyDisabled(self):
        return False

    def GetNext(self):
        return (self.next, None)

    def GetBack(self):
        return (self.resources.page_remote_configuration, None)

class EditActivityPanel(WizardPanelBase):
    def __init__(self, parent, resources):
        self.parent = parent
        self.resources = resources

        WizardPanelBase.__init__(self, parent, resources)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizerAndFit(self.sizer)

        self.next = None

    def OnActivated(self, prev_page, data):
        self.parent.ReenableBack()
        self.remote, self.activityType, self.activity = data
        BackgroundTask((self.LoadData,), (self.LoadDataUI,), False)
        return (None, None)

    def LoadData(self):
        if self.activity:
            self.activityNameDefault = self.activity.Name
        else:
            self.activityNameDefault = mhMgr.GetActivityTypeString(
                self.activityType)
        self.activityTemplate = mhMgr.GetActivityTemplate(self.remote.Id,
                                                          self.activityType)

    def LoadDataUI(self, loadDataResult):
        self.sizer.Clear(deleteWindows=True)

        self.activityNameSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.activityNameLabel = wx.StaticText(self, -1, "Activity Name:")
        self.activityNameSizer.Add(self.activityNameLabel,
                                   flag=wx.ALIGN_CENTER_VERTICAL)
        self.activityNameCtrl = wx.TextCtrl(self, -1, self.activityNameDefault)
        self.activityNameCtrl.SetMinSize((150, 31))
        self.activityNameSizer.Add(self.activityNameCtrl)
        self.sizer.Add(self.activityNameSizer)
        self.sizer.AddSpacer(10)

        self.roles = []
        self.roleChoices = {}
        for role in self.activityTemplate.roles:
            roleType, devices = role
            self.roles.append(roleType)
            if roleType == "DisplayActivityRole": # Don't add widget for Display
                self.displayDeviceId = devices[0][1]
                continue
            roleString = mhMgr.GetRoleString(roleType)
            label = "What device do you use to " + roleString + "?"
            labelText = wx.StaticText(self, -1, label)
            self.sizer.Add(labelText)
            choice = wx.Choice(self, -1)
            self.sizer.Add(choice)
            self.sizer.AddSpacer(10)
            self.roleChoices[roleType] = choice
            for deviceName, deviceId in devices:
                choice.Append(deviceName, deviceId)
            choice.SetSelection(0)
            if self.activity:
                device = mhMgr.GetRoleDeviceId(self.activity, roleType)
                for i in range(choice.GetCount()):
                    if choice.GetClientData(i).Value == device.Value:
                        choice.SetSelection(i)

        self.inputSelectionChoices = {}
        for deviceName, deviceId, inputs in \
            self.activityTemplate.devicesWithInputs:
            label = "What input should " + deviceName + " be set to?"
            inputSelectionLabel = wx.StaticText(self, -1, label)
            self.sizer.Add(inputSelectionLabel)
            inputSelectionChoice = wx.Choice(self, -1, choices=inputs)
            self.sizer.Add(inputSelectionChoice)
            self.inputSelectionChoices[deviceId.Value] = inputSelectionChoice
            self.sizer.AddSpacer(10)
            if self.activity:
                input = mhMgr.GetDeviceInput(self.activity, deviceId)
                if input:
                    inputNum = inputSelectionChoice.FindString(input)
                    inputSelectionChoice.SetSelection(inputNum)

        self.sizer.AddSpacer(20)
        self.saveChangesButton = wx.Button(self, label="Save Changes")
        self.saveChangesButton.Bind(wx.EVT_BUTTON, self.OnSaveChanges)
        self.sizer.Add(self.saveChangesButton)
        self.Layout()
        self.parent.Show()

    def OnSaveChanges(self, event):
        if self.activityNameCtrl.IsEmpty():
            wx.MessageBox('Please enter an activity name', 'Error',
                          wx.OK | wx.ICON_WARNING)
            return

        template = SaveActivityTemplate()
        template.activityName = self.activityNameCtrl.GetValue()
        template.activityType = self.activityType
        template.roles = []

        for role in self.roles:
            if role == "DisplayActivityRole":
                deviceId = self.displayDeviceId
            else:
                deviceNum = self.roleChoices[role].GetSelection()
                deviceId = self.roleChoices[role].GetClientData(deviceNum)
            try:
                inputChoice = self.inputSelectionChoices[deviceId.Value]
                inputName = inputChoice.GetStringSelection()
            except KeyError:
                inputName = None
            template.roles.append((role, deviceId, inputName))

        self.parent.Hide()
        BackgroundTask((self.DoSave, template), (self.FinishSave,), False)

    def DoSave(self, template):
        mhMgr.SaveActivityByTemplate(self.remote.Id, template, self.activity)

    def FinishSave(self, saveResult):
        self.parent._SetPage(self.resources.page_setup_activities, self.remote,
                             True)

    def GetTitle(self):
        return "Edit Activity"

    def IsCancelInitiallyDisabled(self):
        return False

    def GetNext(self):
        return (self.next, None)

    def GetBack(self):
        return (self.resources.page_setup_activities, None)

class AdjustPowerSettingsPanel(WizardPanelBase):
    _msg_welcome = (
        "On this page, you can adjust the commands that are sent when your " +
        "device\nis powered on and off."
    )

    def __init__(self, parent, resources):
        self.parent = parent
        self.resources = resources

        WizardPanelBase.__init__(self, parent, resources)

        self.buttonSize = (127, 33)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.textMessage = WrappedStaticText(self)
        self.sizer.Add(self.textMessage, 0, ALIGN_XTA, 5)

        self.buttonSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.addCommandButton = wx.Button(self, label="Add Command",
                                          size=self.buttonSize)
        self.addCommandButton.Bind(wx.EVT_BUTTON, self.OnAddCommand)
        self.buttonSizer.Add(self.addCommandButton)
        self.addDelayButton = wx.Button(self, label="Add Delay",
                                        size=self.buttonSize)
        self.addDelayButton.Bind(wx.EVT_BUTTON, self.OnAddDelay)
        self.buttonSizer.Add(self.addDelayButton)
        self.editCommandButton = wx.Button(self, label="Edit Command",
                                           size=self.buttonSize)
        self.editCommandButton.Bind(wx.EVT_BUTTON, self.OnEditCommand)
        self.buttonSizer.Add(self.editCommandButton)
        self.deleteCommandButton = wx.Button(self, label="Delete Command",
                                             size=self.buttonSize)
        self.deleteCommandButton.Bind(wx.EVT_BUTTON, self.OnDeleteCommand)
        self.buttonSizer.Add(self.deleteCommandButton)
        self.sizer.Add(self.buttonSizer)
        self.sizer.AddSpacer(10)

        self.commandGrid = wx.grid.Grid(self)
        self.commandGrid.CreateGrid(1, 2)
        self.commandGrid.EnableEditing(False)
        self.commandGrid.SetColLabelValue(0, "Command")
        self.commandGrid.SetColLabelValue(1, "Duration")
        self.commandGrid.SetSelectionMode(wx.grid.Grid.wxGridSelectRows)
        self.commandGrid.Bind(wx.grid.EVT_GRID_SELECT_CELL, self.OnSelectCell)
        self.sizer.Add(self.commandGrid)

        self.sizer.AddSpacer(10)
        self.saveButton = wx.Button(self, label="Save Changes",
                                    size=self.buttonSize)
        self.saveButton.Bind(wx.EVT_BUTTON, self.OnSaveChanges)
        self.sizer.Add(self.saveButton)

        self.SetSizerAndFit(self.sizer)

        self.next = None

    def OnActivated(self, prev_page, data):
        self.textMessage.UpdateText(self._msg_welcome)
        self.parent.ReenableBack()
        self.device = data
        BackgroundTask((self.LoadData,), (self.LoadDataUI,), False)
        return (None, None)

    def LoadData(self):
        self.powerFeature = mhMgr.GetPowerFeature(self.device.Id)
        self.deviceCommands = mhMgr.GetCommands(self.device.Id)

    def LoadDataUI(self, loadDataResult):
        self.deviceCommandsList = []
        if self.deviceCommands:
            for command in self.deviceCommands:
                self.deviceCommandsList.append(command.Name)
        if self.commandGrid.GetNumberRows() > 0:
            self.commandGrid.DeleteRows(0, self.commandGrid.GetNumberRows())

        self.actions = mhMgr.GetPowerFeatureActions(
            self.powerFeature.PowerToggleActions)
        if self.actions is None:
            self.actions = mhMgr.GetPowerFeatureActions(
                self.powerFeature.PowerOnActions)
            if self.actions is not None:
                self.actionType = "PowerOn"
        else:
            self.actionType = "PowerToggle"

        if self.actions:
            for action in self.actions:
                if action[0] == "IRPressAction":
                    rowNum = self.AddRow()
                    self.commandGrid.SetCellValue(rowNum, 0, action[1])
                    self.commandGrid.SetCellValue(rowNum, 1, action[2])
                elif action[0] == "IRDelayAction":
                    rowNum = self.AddRow()
                    self.commandGrid.SetCellValue(rowNum, 0, "Delay")
                    self.commandGrid.SetCellValue(rowNum, 1, action[2])
            self.commandGrid.AutoSize()
            self.commandGrid.ClearSelection()
        else:
            self.actions = []

        self.parent.Show()

    def AddRow(self):
        self.commandGrid.AppendRows(1)
        return self.commandGrid.GetNumberRows() - 1

    def EditCommandRow(self, commandName, duration, rowNum=None):
        if rowNum is None:
            rowNum = self.AddRow()
            self.actions.append(("IRPressAction", commandName, duration))
        else:
            self.actions[rowNum] = ("IRPressAction", commandName, duration)
        self.commandGrid.SetCellValue(rowNum, 0, commandName)
        self.commandGrid.SetCellValue(rowNum, 1, duration)

    def EditDelayRow(self, delayAmount, rowNum=None):
        if rowNum is None:
            rowNum = self.AddRow()
            self.actions.append(("IRDelayAction", None, delayAmount))
        else:
            self.actions[rowNum] = ("IRDelayAction", None, delayAmount)
        self.commandGrid.SetCellValue(rowNum, 0, "Delay")
        self.commandGrid.SetCellValue(rowNum, 1, delayAmount)

    def ResizeGrid(self):
        self.commandGrid.AutoSize()
        self.Layout()

    def GetSelectedRow(self):
        try:
            return self.commandGrid.GetSelectedRows()[0]
        except:
            return None

    def DoEditCommand(self, rowNum=None):
        if rowNum is not None:
            commandName = self.commandGrid.GetCellValue(rowNum, 0)
            duration = int(self.commandGrid.GetCellValue(rowNum, 1))
        else:
            commandName = None
            duration = 0
        dlg = wx.SingleChoiceDialog(self.parent,
                                    'Select command:',
                                    'Command Selection',
                                    self.deviceCommandsList)
        if commandName:
            dlg.SetSelection(self.deviceCommandsList.index(commandName))
        if dlg.ShowModal() == wx.ID_OK:
            commandName = dlg.GetStringSelection()
            dlg.Destroy()
            dlg = wx.NumberEntryDialog(self.parent,
                                       'Enter duration in seconds:',
                                       '(0-60 s)', 'Duration Entry',
                                       value=duration, min=0, max=60)
            if dlg.ShowModal() == wx.ID_OK:
                self.EditCommandRow(commandName, str(dlg.GetValue()), rowNum)
                self.ResizeGrid()
        dlg.Destroy()

    def DoEditDelay(self, rowNum=None):
        if rowNum is not None:
            delayAmount = int(self.commandGrid.GetCellValue(rowNum, 1))
        else:
            delayAmount = 0
        dlg = wx.NumberEntryDialog(self.parent,
                                   'Enter delay amount in milliseconds:',
                                   '(0-20000 ms)', 'Delay Entry',
                                   value=delayAmount, min=0, max=20000)
        if dlg.ShowModal() == wx.ID_OK:
            self.EditDelayRow(str(dlg.GetValue()), rowNum)
            self.ResizeGrid()
        dlg.Destroy()

    def OnSelectCell(self, event):
        self.commandGrid.SelectRow(event.GetRow())
        event.Skip()

    def OnAddCommand(self, event):
        self.DoEditCommand()

    def OnAddDelay(self, event):
        self.DoEditDelay()

    def OnEditCommand(self, event):
        rowNum = self.GetSelectedRow()
        if rowNum is None:
            wx.MessageBox('Please select a command', 'Error',
                          wx.OK | wx.ICON_WARNING)
        else:
            if self.actions[rowNum][0] == "IRDelayAction":
                self.DoEditDelay(rowNum)
            else:
                self.DoEditCommand(rowNum)

    def OnDeleteCommand(self, event):
        rowNum = self.GetSelectedRow()
        if rowNum is None:
            wx.MessageBox('Please select a command', 'Error',
                          wx.OK | wx.ICON_WARNING)
        else:
            self.commandGrid.DeleteRows(rowNum, 1)
            self.ResizeGrid()
            del self.actions[rowNum]

    def OnSaveChanges(self, event):
        BackgroundTask((self.DoSaveChanges,), (self.FinishSaveChanges,))

    def DoSaveChanges(self):
        mhMgr.SavePowerFeature(self.powerFeature, self.actions, self.actionType)

    def FinishSaveChanges(self, saveChangesResult):
        pass

    def GetTitle(self):
        return "Power Setting Adjustment"

    def IsCancelInitiallyDisabled(self):
        return False

    def GetNext(self):
        return (self.next, None)

    def GetBack(self):
        return (self.resources.page_remote_configuration, None)

class Wizard(wx.Dialog):
    def __init__(
        self,
        resources,
        app_finalizer,
        min_page_width = 658,
        min_page_height = None
    ):
        self.app_finalizer = app_finalizer

        self.min_page_width = min_page_width
        self.min_page_height = min_page_height

        wx.Dialog.__init__(self, None, -1, 'MHGUI version ' + version)

        sizer_main = wx.BoxSizer(wx.VERTICAL)

        sizer_top = wx.BoxSizer(wx.HORIZONTAL)
        bitmap = wx.StaticBitmap(self, -1, resources.img_remote)
        sizer_top.Add(bitmap, 0, wx.EXPAND | wx.ALL, 5)

        self.sizer_top_right = wx.BoxSizer(wx.VERTICAL)
        self.title = wx.StaticText(self, -1, "Title")
        font = wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD)
        self.title.SetFont(font)
        self.sizer_top_right.Add(self.title, 0, wx.EXPAND)
        divider_top_right = wx.StaticLine(self, -1, None, None, wx.LI_HORIZONTAL)
        self.sizer_top_right.Add(divider_top_right, 0, wx.EXPAND)
        spacer = wx.StaticText(self, -1, "")
        self.sizer_top_right.Add(spacer, 0, wx.EXPAND)

        sizer_top.Add(self.sizer_top_right, 1, wx.EXPAND | wx.ALL, 5)
        sizer_main.Add(sizer_top, 1, wx.EXPAND | wx.ALL, 5)

        divider_main = wx.StaticLine(self, -1, None, None, wx.LI_HORIZONTAL)
        sizer_main.Add(divider_main, 0, wx.EXPAND | wx.ALL, 5)

        sizer_buttons = wx.BoxSizer(wx.HORIZONTAL)

        sizer_buttons.AddStretchSpacer()

        self.btn_back = wx.Button(self, wx.ID_BACKWARD)
        self.Bind(wx.EVT_BUTTON, self._OnBack, self.btn_back)
        sizer_buttons.Add(self.btn_back, 0, wx.EXPAND | wx.ALL, 5)

        self.btn_next = wx.Button(self, wx.ID_FORWARD)
        self.Bind(wx.EVT_BUTTON, self._OnNext, self.btn_next)
        sizer_buttons.Add(self.btn_next, 0, wx.EXPAND | wx.ALL, 5)

        self.btn_close = wx.Button(self, wx.ID_CLOSE)
        self.Bind(wx.EVT_BUTTON, self._OnNext, self.btn_close)
        sizer_buttons.Add(self.btn_close, 0, wx.EXPAND | wx.ALL, 5)

        self.btn_cancel = wx.Button(self, wx.ID_CLOSE)
        self.Bind(wx.EVT_BUTTON, self._OnCancel, self.btn_cancel)
        self.Bind(wx.EVT_CLOSE, self._OnCancel)
        sizer_buttons.Add(self.btn_cancel, 0, wx.EXPAND | wx.ALL, 5)

        sizer_main.Add(sizer_buttons, 0, wx.EXPAND | wx.ALL, 5)

        self.SetSizerAndFit(sizer_main)

        self.cur_page = None

    def SetPages(self, pages):
        def tuple_max(a, b):
            return (max(a[0], b[0]), max(a[1], b[1]))

        self.pages = pages

        for page in self.pages:
            page.Hide()

        size_wiz = self.GetSizeTuple()
        for page in self.pages:
            page.Show()
            self.sizer_top_right.Add(page, 1, wx.EXPAND)
            self.Fit()
            size_page = self.GetSizeTuple()
            size_wiz = tuple_max(size_wiz, size_page)
            page.Hide()
            self.sizer_top_right.Detach(page)

        if self.min_page_width and (size_wiz[0] < self.min_page_width):
            size_wiz = (self.min_page_width, size_wiz[1])

        if self.min_page_height and (size_wiz[1] < self.min_page_height):
            size_wiz = (size_wiz[0], self.min_page_height )

        self.SetClientSize(size_wiz)

    def SetInitialPage(self, page):
        if self.cur_page:
            raise Exception("Current page already set")
        self._SetPage(page, None)

    def OnExit(self, retcode):
        if self.app_finalizer:
            self.app_finalizer()
        os._exit(retcode)

    def _ReenableButton(self, button):
        button.Enable(True)
        button.Hide()
        button.Show()
        button.SetFocus()

    def ReenableBack(self):
        self._ReenableButton(self.btn_back)

    def ReenableNext(self):
        self._ReenableButton(self.btn_next)

    def ReenableClose(self):
        self._ReenableButton(self.btn_close)

    def ReenableCancel(self):
        self._ReenableButton(self.btn_cancel)

    def _OnBack(self, event):
        try:
            (page, data, hide) = self.cur_page.GetBack()
        except:
            (page, data) = self.cur_page.GetBack()
            hide = False
        self._SetPage(page, data, hide)

    def _OnNext(self, event):
        if self.cur_page.IsTerminal():
            self.OnExit(self.cur_page.GetExitCode())
        if self.cur_page.OnNext():
            try:
                (page, data, hide) = self.cur_page.GetNext()
            except:
                (page, data) = self.cur_page.GetNext()
                hide = False
            self._SetPage(page, data, hide)

    def _OnCancel(self, event):
        self.cur_page.OnCancel()

    def _SetPage(self, page, data, hide=False):
        if hide:
            self.Hide()
        while page:
            if not page in self.pages:
                raise Exception("Invalid page")

            prev_page = self.cur_page
            if prev_page:
                prev_page.Hide()
                self.sizer_top_right.Detach(prev_page)

            self.cur_page = page

            self.cur_page.Show()
            self.sizer_top_right.Add(self.cur_page, 1, wx.EXPAND)

            self.title.SetLabel(self.cur_page.GetTitle())

            self.Layout()

            is_terminal = self.cur_page.IsTerminal()
            if is_terminal:
                self.btn_next.Hide()
                self.btn_close.Show()
            else:
                self.btn_next.Show()
                self.btn_close.Hide()

            self.btn_back.Enable(not self.cur_page.IsBackInitiallyDisabled())
            self.btn_next.Enable(not self.cur_page.IsNextInitiallyDisabled())
            self.btn_close.Enable(not self.cur_page.IsCloseInitiallyDisabled())
            self.btn_cancel.Enable(
                (not is_terminal)
                and
                (not self.cur_page.IsCancelInitiallyDisabled())
            )

            if self.btn_next.IsEnabled():
                self.btn_next.SetFocus()
            elif self.btn_close.IsEnabled():
                self.btn_close.SetFocus()

            (page, data) = self.cur_page.OnActivated(prev_page, data)

class Resources(object):
    def __init__(self, appdir, no_web):
        self.appdir = appdir
        self.no_web = no_web

    def LoadImages(self):
        def load(filename, appdir = self.appdir):
            dirs = ['/usr/share/congruity', appdir, '.']
            for dir in dirs:
                fpath = os.path.join(dir, filename)
                if not os.path.isfile(fpath):
                    continue
                return wx.Image(fpath, wx.BITMAP_TYPE_PNG).ConvertToBitmap()
            raise Exception("Can't load " + filename)

        self.img_remote       = load("remote.png")

    def CreatePages(self, wizard):
        self.page_welcome = WelcomePanel(wizard, self)
        self.page_remote_select = RemoteSelectPanel(wizard, self)
        self.page_remote_configuration = RemoteConfigurationPanel(wizard, self)
        self.page_add_device = AddDevicePanel(wizard, self)
        self.page_create_account = CreateAccountPanel(wizard, self)
        self.page_configure_device = ConfigureDevicePanel(wizard, self)
        self.page_favorite_channels = FavoriteChannelsPanel(wizard, self)
        self.page_setup_watch_tv = SetupWatchTVPanel(wizard, self)
        self.page_harmony_link = ConfigureHarmonyLinkSettingsPanel(wizard, self)
        self.page_setup_activities = SetupActivitiesPanel(wizard, self)
        self.page_edit_activity = EditActivityPanel(wizard, self)
        self.page_adjust_power_settings = AdjustPowerSettingsPanel(wizard, self)
        self.pages = [
            self.page_welcome,
            self.page_remote_select,
            self.page_remote_configuration,
            self.page_add_device,
            self.page_create_account,
            self.page_configure_device,
            self.page_favorite_channels,
            self.page_setup_watch_tv,
            self.page_harmony_link,
            self.page_setup_activities,
            self.page_edit_activity,
            self.page_adjust_power_settings,
        ]

class Finalizer(object):
    def __init__(self, resources):
        self.resources = resources

    def __call__(self):
        pass


def main(argv):
    appdir = os.path.dirname(argv[0])

    app = wx.App(False)

    resources = Resources(appdir, True)
    resources.LoadImages()

    wizard = Wizard(resources, Finalizer(resources))

    resources.CreatePages(wizard)
    wizard.SetPages(resources.pages)
    wizard.SetInitialPage(resources.page_welcome)

    wizard.Show()

    app.MainLoop()

if __name__ == "__main__":
    main(sys.argv)