My favorites | Sign in
Project Home Issues Source
Checkout   Browse   Changes    
 
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
/*******************************************************************
This file is part of iDialer.

iDialer 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.

iDialer 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 iDialer. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************/

#include "stdafx.h"
#include "iDialer.h"

#include "FileUtils.h"
#include "GraphicsUtils.h"
#include "PhoneUtils.h"
#include "RegistryUtils.h"
#include "SoundUtils.h"
#include "Titlebar.h"
#include "Version.h"

#include <phone.h>
#include <wingdi.h>
#include <pimstore.h>
#include <astdtapi.h>
#include <strsafe.h>
#include <regext.h>
#include <snapi.h>

#include <htmlctrl.h>

#pragma comment(lib, "phone.lib")
#pragma comment(lib, "cellcore.lib")
#pragma comment(lib, "htmlview.lib")

#include <regext.h>

#include "HTTP.h"

//-----------------------------------------------------------------------------
// Global data
//
HINSTANCE hInst; // Program instance handle
HWND hwndMain = 0;
HWND hwndHTML = 0;
bool bDialImmediately = false;

bool biContactExists = false;
int nHighlightedTab = 3;
bool bKeysDialed = false;

HDC hdcSkin = NULL;
HBITMAP hbmSkin = NULL;
HDC hdcMem = NULL;
HBITMAP hbmMem = NULL;
HDC hdcContent = NULL;
HBITMAP hbmContent = NULL;
HDC hdcTmp = NULL;
HBITMAP hbmTmp = NULL;

HFONT hfNumberFont = NULL;
HFONT hfDigitFont = NULL;
HFONT hfTitlebarFont = NULL;
HFONT hfCaptionFont = NULL;

// UI Element Sizes. These can't be static because
// of different DPI devices
int vga = 1; // 1 = qvga, 2 = vga

RECT rScreen = {0};
RECT rTitlebar = {0};
RECT rMenubar = {0};
RECT rContent = {0};
RECT rDigits = {0};
RECT rect1 = {0};
RECT rect2 = {0};
RECT rect3 = {0};
RECT rect4 = {0};
RECT rect5 = {0};
RECT rect6 = {0};
RECT rect7 = {0};
RECT rect8 = {0};
RECT rect9 = {0};
RECT rect0 = {0};
RECT rects = {0};
RECT rectp = {0};
RECT rAddButton = {0};
RECT rCallButton = {0};
RECT rBackButton = {0};
RECT rClickRegion = {0};
POINT ptMouseDown = { -1, -1 };
bool bMouseDown = false;
bool bKeyDown = false;
TCHAR tcButtonPressed = 0;
TCHAR dialNumber[MAX_DIALSTRING] = {0};
TCHAR dialString[MAX_DIALSTRING] = {0};
TCHAR dialName[TAPIMAXCALLEDPARTYSIZE] = {0};

// Program Settings
TCHAR skinFilename[MAX_PATH] = {0};
TCHAR szCountryCode[MAX_COUNTRY_CODE] = {0};
TCHAR szIntlPrefix[MAX_COUNTRY_CODE] = {0};
TCHAR szMyNumber[MAX_DIALSTRING] = {0};
bool bExitOnMinimize = false;
bool bShowFullScreen = true;
UINT uVolume = 0x2020;

// phone services (Phone, CallingCard, GoogleVoice, JaJah, Command)
int nService = 0;
int cServices = 0;
SERVICE services[5];

// speed dial
SPEEDDIALINFO speedDialInfo[10] = {0};

// keep track of if the settings get updated while the program is running
HREGNOTIFY hNotify;

TCHAR dialPlan[MAX_DIALPLAN];

// Message dispatch table for MainWindowProc
const struct decodeUINT MainMessages[] = {
WM_ACTIVATE, DoActivate,
WM_COMMAND, DoCommand,
WM_CONTEXTMENU, DoContextMenu,
WM_COPYDATA, DoCopyData,
WM_DESTROY, DoDestroyMain,
WM_KEYDOWN, DoKeyDown,
WM_KEYUP, DoKeyUp,
WM_LBUTTONDOWN, DoLButtonDown,
WM_LBUTTONUP, DoLButtonUp,
WM_MOUSEMOVE, DoMouseMove,
WM_NOTIFY, DoNotify,
WM_PAINT, DoPaintMain,
WM_REGISTRY, DoRegistryCallback,
WM_SIZE, DoSize,
WM_TIMER, DoTimer,
WM_TITLEBAR, DoTitlebarCallback,
};

//=============================================================================
//
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow) {
MSG msg;

// Initialize this instance.
hwndMain = InitInstance(hInstance, lpCmdLine, nCmdShow);
if (hwndMain == 0)
return 0x10;

// Due to a command-line option
if (bDialImmediately)
PostMessage(hwndMain, WM_COMMAND, CMD_PLACE_CALL, nService);

// Application message loop
while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
// Instance cleanup
return TermInstance (hInstance, msg.wParam);
}

//-----------------------------------------------------------------------------
// InitInstance - Instance initialization
//
HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow){
WNDCLASS wc;
HWND hWnd;
COPYDATASTRUCT cdData;

// Save program instance handle in global variable.
hInst = hInstance;

#if defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)
// If Windows Mobile, allow only one instance of the application.
hWnd = FindWindow (SZ_APP_NAME, NULL);
if (hWnd) {
SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01));
cdData.dwData = NULL;
cdData.cbData = sizeof(wchar_t) * _tcslen(lpCmdLine);
cdData.lpData = lpCmdLine;
SendMessage(hWnd, WM_COPYDATA, NULL, (LPARAM) (LPVOID) &cdData);
return 0;
}
#endif

// Register application main window class.
wc.style = 0; // Window style
wc.lpfnWndProc = MainWndProc; // Callback function
wc.cbClsExtra = 0; // Extra class data
wc.cbWndExtra = 0; // Extra window data
wc.hInstance = hInstance; // Owner handle
wc.hIcon = NULL; // Application icon
wc.hCursor = NULL; // Default cursor
wc.hbrBackground = NULL; // Don't erase the background
wc.lpszMenuName = NULL; // Menu name
wc.lpszClassName = SZ_APP_NAME; // Window class name
if (RegisterClass (&wc) == 0) return 0;

// Create main window.
hWnd = CreateWindowEx(WS_EX_NODRAG, // Ex Style
SZ_APP_NAME, // Window class
SZ_APP_NAME, // Window title
WS_SYSMENU, // Style flags
// don't use WS_VISIBLE.
// Why not? It causes an "error report"
// Why? I don't know.
CW_USEDEFAULT, // x position
CW_USEDEFAULT, // y position
CW_USEDEFAULT, // Initial width
CW_USEDEFAULT, // Initial height
NULL, // Parent
NULL, // Menu, must be null
hInstance, // Application instance
NULL); // Pointer to create
// parameters

// Return fail code if window not created.
if (!IsWindow (hWnd)) return 0;

// Additional initialization
// **************************
LoadSettings();

// maybe we don't need to go any further than this...
if (0 == ParseCommandLine(lpCmdLine))
return 0;

biContactExists = iContactExists();
LoadSpeedDialInfo(speedDialInfo);

// If the "service" setting is modified while I'm running
// then LoadSettings() again
HRESULT hr = RegistryNotifyWindow(
HKEY_CURRENT_USER,
SZ_IDIALER_REG_KEY,
SERVICE_NUM,
hWnd, WM_REGISTRY, CMD_RELOAD_SETTINGS,
NULL, &hNotify);

FillDialString();

// Perform DPI adjustments
HDC hdc = GetDC(hWnd);
int res = min(::GetDeviceCaps(hdc, HORZRES), ::GetDeviceCaps(hdc, VERTRES));
vga = MulDiv(vga, res, DEFAULT_SCREEN_WIDTH);

// Create fonts
hfNumberFont = BuildFont(NUMBER_FONT_SIZE * vga, false, false);
hfDigitFont = BuildFont(DIGIT_FONT_SIZE * vga, true, false);
hfTitlebarFont = BuildFont(TITLEBAR_FONT_SIZE * vga, false, false);
hfCaptionFont = BuildFont(CAPTION_FONT_SIZE * vga, false, false);

// Initialize titlebar callbacks
if (bShowFullScreen)
InitTitlebar(hWnd);

// Initialize the regions, skin, and bitmaps
InitSurface(hWnd);

// Standard show and update calls
ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);
return hWnd;
}

//-----------------------------------------------------------------------------
// TermInstance - Program cleanup
//
int TermInstance (HINSTANCE hInstance, int nDefRC) {
return nDefRC;
}

//=============================================================================
// Message handling procedures for MainWindow
//

//-----------------------------------------------------------------------------
// MainWndProc - Callback function for application window
//
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
INT i;
//
// Search message list to see if we need to handle this
// message. If in list, call procedure.
//
for (i = 0; i < ARRAYSIZE(MainMessages); i++) {
if (wMsg == MainMessages[i].Code)
return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
}
return DefWindowProc (hWnd, wMsg, wParam, lParam);
}

//-----------------------------------------------------------------------------
// DoPaintMain - Process WM_PAINT message for window.
//
LRESULT DoPaintMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) {

PAINTSTRUCT ps;
RECT rect;
HDC hdc;
HFONT hfOld;

int rScreenWidth = rScreen.right - rScreen.left;
int rScreenHeight = rScreen.bottom - rScreen.top;

hdc = BeginPaint (hWnd, &ps);

// rect is the region that needs to be painted
rect = ps.rcPaint;

// Draw the main content
BitBlt(hdcMem, rContent.left, rContent.top,
rScreenWidth, rContent.bottom - rContent.top,
hdcContent, rContent.left, rContent.top, SRCCOPY);

int dialStringLen = _tcslen(dialString);

// Draw "About"
if (dialStringLen == 0) {
hfOld = (HFONT)SelectObject(hdcMem, hfCaptionFont);
SetTextAlign(hdcMem, TA_LEFT);
SetTextColor(hdcMem, GetSkinRGB(SKIN_COLOR_TEXT_DIM));
ExtTextOut(hdcMem, rDigits.left + 2,
rDigits.bottom - CAPTION_FONT_SIZE * vga - 2,
NULL, NULL, SZ_ABOUT, _tcslen(SZ_ABOUT), 0);
SelectObject(hdcMem, hfOld);
}

// Draw the dialed number
// (or the last dialed number in gray)
else {
SetTextColor(hdcMem, GetSkinRGB(
bKeysDialed ? SKIN_COLOR_TEXT : SKIN_COLOR_TEXT_DIM));
SetTextAlign(hdcMem, TA_CENTER);
SetBkMode(hdcMem, TRANSPARENT);

hfOld = (HFONT)SelectObject(hdcMem, hfNumberFont);

// Make sure the number will fit on the screen,
// and decrease the font size until it will fit
RECT rc = {0,0,0,0};
int fontsize = NUMBER_FONT_SIZE * vga;
DrawText(hdcMem, dialString, -1, &rc, DT_CALCRECT);
while (rc.right > rContent.right && fontsize > 4) {
fontsize -= 2;
rc.right = rc.bottom = 0;
SelectObject(hdcMem, BuildFont(fontsize, false, false));
DrawText(hdcMem, dialString, -1, &rc, DT_CALCRECT);
}

ExtTextOut(hdcMem, (rDigits.right + rDigits.left) / 2,
(rDigits.bottom + rDigits.top - fontsize) / 2,
NULL, NULL, dialString, dialStringLen, 0);
SelectObject(hdcMem, hfOld);
}

// draw the menu bar
if (biContactExists && rect.bottom > rMenubar.top) {
int rMenubarWidth = rMenubar.right - rMenubar.left;

// this is only necessary if in landscape mode
if (rMenubarWidth > DEFAULT_SCREEN_WIDTH * vga) {
// Copy the first (1 * vga) columns of the
// menu bar from skin to memory
BitBlt(hdcMem, 0, rMenubar.top, vga, SKIN_MENU_BAR_Y_OFFSET * vga,
hdcSkin, 0, SKIN_MENU_BAR_HEIGHT * vga, SRCCOPY);

// This will copy the first (1 * vga) columns of
// the menu bar fully across the screen
for (int x = vga; x < rMenubarWidth; x *= 2) {
int w = 2 * x > rMenubarWidth ? rMenubarWidth - x : x;
BitBlt(hdcMem, x, rMenubar.top, w, SKIN_MENU_BAR_HEIGHT * vga,
hdcMem, 0, rMenubar.top, SRCCOPY);
}
}

// draw buttons
for (int i = 0; i < 5; i++) {
int xdest = rMenubar.left
+ rMenubarWidth / 10 * (2 * i + 1) - MENU_BAR_ICON_WIDTH * vga / 2;
int ydest = rMenubar.top;
int xsrc = i * SKIN_MENU_BAR_ICON_WIDTH * vga;
int ysrc = i == nHighlightedTab
? SKIN_MENU_BAR_SEL_Y_OFFSET * vga
: SKIN_MENU_BAR_Y_OFFSET * vga;
BitBlt(hdcMem, xdest, ydest, MENU_BAR_ICON_WIDTH * vga, SKIN_MENU_BAR_HEIGHT * vga,
hdcSkin, xsrc, ysrc, SRCCOPY);
}
}

// Titlebar
if (rect.top < rTitlebar.bottom) {
DrawTitlebarOn(hdcMem, rTitlebar, hdcSkin, hfTitlebarFont, SZ_APP_NAME);
}

// Name of service, under "call" button
if (cServices > 1) {
SelectObject(hdcMem, hfCaptionFont);
SetTextAlign(hdcMem, TA_LEFT);
SetTextColor(hdcMem, GetSkinRGB(SKIN_COLOR_TEXT));

DrawText(hdcMem,
nService == -1 ? TEXT_ASK : services[nService].title,
-1, &rCallButton, DT_CENTER | DT_BOTTOM | DT_END_ELLIPSIS);
}

// Highlight the region being clicked right now
if ( bKeyDown || (bMouseDown && PtInRect(&rContent, ptMouseDown)) ) {
FillRect(hdcTmp, &rClickRegion,
CreateSolidBrush(GetSkinRGB(SKIN_COLOR_HIGHLIGHT)));

BltAlpha(hdcMem, rClickRegion.left, rClickRegion.top,
rClickRegion.right - rClickRegion.left,
rClickRegion.bottom - rClickRegion.top,
hdcTmp, rClickRegion.left, rClickRegion.top,
rClickRegion.right - rClickRegion.left,
rClickRegion.bottom - rClickRegion.top,
128);
}

// Transfer everything to the actual screen
BitBlt(hdc, rect.left, rect.top, rect.right - rect.left,
rect.bottom - rect.top, hdcMem, rect.left, rect.top, SRCCOPY);

SelectObject(hdcMem, hfOld);
EndPaint (hWnd, &ps);

return 0;
}

//-----------------------------------------------------------------------------
// DoActivate - Process WM_ACTIVATE message for window
//
LRESULT DoActivate (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) {

DWORD dwState;
RECT rc;

if (wParam == WA_CLICKACTIVE || wParam == WA_ACTIVE) {
// To switch to full screen mode, first hide all of the shell parts.
// Do not enable SHFS_HIDESTARTICON;
// it breaks the volume control on Diamond

if (bShowFullScreen) {
dwState = SHFS_HIDETASKBAR | SHFS_HIDESIPBUTTON;

// Resize the main window to the size of the screen.
SetRect(&rc, 0, 0, GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN));
}
else {
dwState = SHFS_SHOWTASKBAR | SHFS_HIDESIPBUTTON;

// Resize the main window to the size of the work area.
SystemParametersInfo(SPI_GETWORKAREA, 0, &rc, FALSE);
}

SHFullScreen(hWnd, dwState);

MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left,
rc.bottom-rc.top, TRUE);

// Hide SIP button displayed by iContactAE
SipShowIM(SIPF_OFF);
ShowWindow(FindWindow(L"MS_SIPBUTTON", NULL), SW_HIDE);

nHighlightedTab = 3;
bMouseDown = false;
bKeyDown = false;

ShowWindow(hWnd, SW_SHOWNORMAL);
}


// The window is being deactivated and another program
// is being run... so minimize
// lParam == NULL when the window being activated is not this app.
else if (lParam == NULL) {
if (bExitOnMinimize) {
DestroyWindow(hWnd);
}
}

// The window is being deactivated
// (maybe for "manage contact", or "edit contact")...
// restore it to non-fullscreen
else {
// To switch to normal mode, first show all of the shell parts.
dwState = (SHFS_SHOWTASKBAR
| SHFS_SHOWSTARTICON
| SHFS_SHOWSIPBUTTON);

SHFullScreen(hWnd, dwState);

// Next resize the main window to the size of the work area.
SystemParametersInfo(SPI_GETWORKAREA, 0, &rc, FALSE);

MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left,
rc.bottom-rc.top, TRUE);
}

return DefWindowProc (hWnd, wMsg, wParam, lParam);
}

//-----------------------------------------------------------------------------
// DoCommand - Process WM_COMMAND message for window
//
LRESULT DoCommand (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) {
int len, index;
TCHAR szBuffer[MAX_PATH] = {0};
PROCESS_INFORMATION pi;
HRESULT hr = E_FAIL;

switch (wParam) {
case CMD_GOTO_FAVORITES:
if (!GetIContactFilename(szBuffer))
break;
nHighlightedTab = 0;
CreateProcess(szBuffer, ICONTACT_FAVORITES,
NULL, NULL, FALSE, NULL, NULL, NULL, NULL, &pi);
break;

case CMD_GOTO_RECENTS:
if (!GetIContactFilename(szBuffer))
break;
nHighlightedTab = 1;
CreateProcess(szBuffer, ICONTACT_RECENTS,
NULL, NULL, FALSE, NULL, NULL, NULL, NULL, &pi);
break;

case CMD_GOTO_CONTACTS:
if (!GetIContactFilename(szBuffer))
break;
nHighlightedTab = 2;
CreateProcess(szBuffer, ICONTACT_CONTACTS,
NULL, NULL, FALSE, NULL, NULL, NULL, NULL, &pi);
break;

case CMD_GOTO_SEARCH:
if (!GetIContactFilename(szBuffer))
break;
nHighlightedTab = 4;
CreateProcess(szBuffer, ICONTACT_SEARCH,
NULL, NULL, FALSE, NULL, NULL, NULL, NULL, &pi);
break;

case CMD_DIAL_DIGIT:

len = _tcslen(dialNumber);
if (lParam == VK_BACK) {
// this erases the last dialed number
if (!bKeysDialed) {
bKeysDialed = true;
dialNumber[0] = 0;
dialName[0] = 0;
}
else if (len > 0) {
dialNumber[len-1] = 0;
}
FillDialString();
}
else if (lParam == VK_TSOFT1) {
AddContact();
}
else if (lParam == VK_RETURN) {
PostMessage(hWnd, WM_COMMAND, CMD_PLACE_CALL, nService);
}
else {
// this erases the last dialed number
if (!bKeysDialed) {
bKeysDialed = true;
len = 0;
dialName[0] = 0;
}
if (len < MAX_DIALSTRING - 1) {
dialNumber[len] = (TCHAR)lParam;
dialNumber[len+1] = 0;
FillDialString();
}
}
break;

case CMD_CLEAR_DIGITS:
dialNumber[0] = 0;
dialName[0] = 0;
FillDialString();
break;

case CMD_PLACE_CALL:
// Store the dialed number for next use
SaveSetting(SZ_IDIALER_REG_KEY, dialNumber, LAST_DIALED_NUMBER);

if (0 == _tcslen(dialName)) {
StringCchCopy(dialName, MAX_DIALSTRING, dialString);
}

// implementation of "ask"
if (lParam == -1) {
HMENU hPopupMenu = CreatePopupMenu();
for (int i = 0; i < cServices; i++) {
StringCchPrintf(szBuffer, MAX_PATH, TEXT("%s %s"),
TEXT_CALL_USING, services[i].title);
AppendMenu(hPopupMenu, MF_STRING, i+1, szBuffer);
}

SetForegroundWindow(hWnd);
int result = TrackPopupMenu(hPopupMenu,
TPM_CENTERALIGN | TPM_VCENTERALIGN | TPM_RETURNCMD,
rContent.right / 2, rContent.bottom / 2, 0, hWnd, NULL);

// result may be "0", or 1 <= result <= cServices
if (!result)
break;

lParam = result - 1;
}

if (lParam >= cServices || lParam < 0)
break;

hr = Dial(dialNumber, dialName, lParam);
break;

case CMD_RELOAD_SETTINGS:
LoadSettings();
break;

case CMD_DIAL_VOICEMAIL:
StringCchCopy(dialNumber, MAX_DIALSTRING, szMyNumber);
StringCchCopy(dialName, TAPIMAXCALLEDPARTYSIZE, TEXT_VOICEMAIL);
PostMessage(hWnd, WM_COMMAND, CMD_PLACE_CALL, nService);
break;

case CMD_SPEED_DIAL:
if (lParam == '1') {
PostMessage(hWnd, CMD_DIAL_VOICEMAIL, 0, 0);
break;
}

index = lParam - '0';
if (index < 2 || index > 9)
break;

if (0 == _tcslen(speedDialInfo[index].number))
break;

bKeysDialed = false;
StringCchCopy(dialNumber, MAX_DIALSTRING, speedDialInfo[index].number);
StringCchCopy(dialName, MAX_DIALSTRING, speedDialInfo[index].name);
PostMessage(hWnd, WM_COMMAND, CMD_PLACE_CALL, nService);
break;

case CMD_PLACE_CALL_COMPLETE:
bKeysDialed = false;
ShowWindow(hWnd, SW_MINIMIZE);
break;

case CMD_CHANGE_SERVICE:
nService++;
if (nService >= cServices || nService < -1)
nService = -1;
StringCchPrintf(szBuffer, MAX_PATH, TEXT("%d"), nService);
SaveSetting(SZ_IDIALER_REG_KEY, szBuffer, SERVICE_NUM);
break;

case CMD_CHANGE_SERVICE_TO:
nService = lParam;
if (nService >= cServices || nService < -1)
nService = -1;
StringCchPrintf(szBuffer, MAX_PATH, TEXT("%d"), nService);
SaveSetting(SZ_IDIALER_REG_KEY, szBuffer, SERVICE_NUM);
break;

case CMD_CUT:
SendMessage(hWnd, WM_COMMAND, CMD_COPY, NULL);
dialNumber[0] = 0;
FillDialString();
dialName[0] = 0;
break;

case CMD_COPY:
if (OpenClipboard(NULL)) {
HLOCAL clipbuffer;
EmptyClipboard();
clipbuffer = LocalAlloc(0, (_tcslen(dialString) + 1) * sizeof TCHAR);
StringCchCopy((TCHAR *)clipbuffer, MAX_PATH, dialString);
// Set UNICODE clipboard data
SetClipboardData(CF_UNICODETEXT, clipbuffer);
CloseClipboard();
LocalFree(clipbuffer);
}
break;

case CMD_PASTE:
if (OpenClipboard(NULL)) {
HANDLE hData = GetClipboardData(CF_UNICODETEXT);
StripNonDigits(szBuffer, (TCHAR *)hData, MAX_PATH);
if (_tcslen(szBuffer) > 0) {
StringCchCopy(dialNumber, MAX_DIALSTRING, szBuffer);
FillDialString();
}
CloseClipboard();
}
break;

case CMD_GREEN_BUTTON:
PostMessage(hWnd, WM_COMMAND, CMD_PLACE_CALL, nService);
break;

case CMD_RED_BUTTON:
ShowWindow(hWnd, SW_MINIMIZE);
break;




}

InvalidateRect(hWnd, &rScreen, FALSE);
UpdateWindow(hWnd);

return DefWindowProc (hWnd, wMsg, wParam, lParam);
}

//-----------------------------------------------------------------------------
// DoCopyData - Process WM_COPYDATA message for window
//
// We're only using WM_COPYDATA to pass a command line from a newly created
// instance of iDialer to an already running instance.
//
LRESULT DoCopyData (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) {
PCOPYDATASTRUCT pcdData;
pcdData = (PCOPYDATASTRUCT) lParam;

int result = ParseCommandLine((TCHAR*)&pcdData->lpData);

// If we want to exit/minimize now, then do it!
if (result == 0)
ShowWindow(hWnd, SW_MINIMIZE);

else if (bDialImmediately)
PostMessage(hWnd, WM_COMMAND, CMD_PLACE_CALL, nService);

return 0;
}

//-----------------------------------------------------------------------------
// DoTitlebarCallback - Process WM_REGISTRY message for window
//
LRESULT DoTitlebarCallback (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
RefreshTitlebar(lParam);
InvalidateRect(hWnd, &rTitlebar, false);
return 0;
}

//-----------------------------------------------------------------------------
// DoRegistryCallback - Process WM_REGISTRY message for window
//
LRESULT DoRegistryCallback (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {

if (lParam == CMD_RELOAD_SETTINGS) {
LoadSettings();
}
InvalidateRect(hWnd, &rTitlebar, false);
return 0;
}

//-----------------------------------------------------------------------------
// DoSize - Process WM_SIZE message for window
//
LRESULT DoSize (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) {

switch (wParam) {
case SIZE_RESTORED:
case SIZE_MAXIMIZED:
case SIZE_MAXSHOW:
InitSurface(hWnd);
return 0;
}

return DefWindowProc (hWnd, wMsg, wParam, lParam);
}

//-----------------------------------------------------------------------------
// DoLButtonDown - Process WM_LBUTTONDOWN message for window
//

LRESULT DoLButtonDown (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) {

ptMouseDown.x = LOWORD(lParam);
ptMouseDown.y = HIWORD(lParam);

bMouseDown = true;
tcButtonPressed = GetDigitFor(ptMouseDown);
CalculateClickRegion(ptMouseDown);

if (PtInRect(&rMenubar, ptMouseDown)) {
nHighlightedTab = ptMouseDown.x * 5 / rMenubar.right;
}

PlayDTMF(tcButtonPressed, uVolume);

SetTimer(hWnd, IDT_TIMER_LONG_PRESS, 100, NULL);

InvalidateRect(hWnd, &rClickRegion, false);

return 0;
}

//-----------------------------------------------------------------------------
// DoMouseMove - Process WM_MOUSEMOVE message for window
//

LRESULT DoMouseMove (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) {

POINT pt;
pt.x = LOWORD(lParam);
pt.y = HIWORD(lParam);

if (PtInRect(&rMenubar, ptMouseDown)) {
nHighlightedTab = PtInRect(&rMenubar, pt)
? pt.x * 5 / rMenubar.right
: 3;
InvalidateRect(hWnd, &rClickRegion, false);
}

return DefWindowProc (hWnd, wMsg, wParam, lParam);
}

//-----------------------------------------------------------------------------
// DoNotify - Process WM_NOTIFY message for window
//

LRESULT DoNotify (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) {

NMHDR * pnmh = (NMHDR *)lParam;
NM_HTMLVIEW * pnmHTMLView = (NM_HTMLVIEW *)lParam;

// TODO: not exactly sure why this is needed:
TCHAR url[512];
mbstowcs(url, (LPCSTR)pnmHTMLView->szTarget, 512);

IDispatch *pDisp;

switch (pnmh->code) {
case NM_BEFORENAVIGATE:
#ifndef DEBUG
ShowWindow(hwndHTML, SW_HIDE);
#endif
SHFullScreen(hWnd, SHFS_HIDESIPBUTTON);
SetCursor(LoadCursor(NULL, IDC_WAIT));
break;

case NM_DOCUMENTCOMPLETE:
// The user needs to log in first. Show the window.
if (_tcsstr(url, TEXT("/accounts/ServiceLogin"))) {
SetCursor(NULL);
ShowWindow(hwndHTML, SW_SHOW);
SHFullScreen(hWnd, SHFS_SHOWSIPBUTTON);
break;
}

// This is the standard GV control panel. Schedule the call!
else if (
_tcsstr(url, TEXT("/voice/m/sms?"))
|| _tcsstr(url, TEXT("/voice/b/0/m/sms"))
) {
// http://blogs.technet.com/suvarnas/archive/2006/01/13/417387.aspx
SendMessage(hwndHTML, DTM_DOCUMENTDISPATCH,
(WPARAM)0, (LPARAM)&pDisp );
if( NULL == pDisp)
break;

// TODO: fix this for "ask"
TCHAR * pszCallback = nService >= 0 && _tcslen(services[nService].data)
? services[nService].data
: szMyNumber;

GoogleVoiceMakeCall(pszCallback, dialNumber, pDisp);
}

// The call has been scheduled, aaand we're done.
else if (
_tcsstr(url, TEXT("/voice/m/sendcall"))
) {
SetCursor(NULL);
#ifndef DEBUG
PostMessage(hwndMain, WM_COMMAND, CMD_PLACE_CALL_COMPLETE, NULL);
#endif
}

// Why would URL be empty? No connectivity? Timeout?
else if (_tcslen(url) == 0) {
MessageBox(hwndMain, TEXT("Cannot connect!"), NULL, 0);
SetCursor(NULL);
}

// Apparently this happens if you have Cookies turned off in PIE
else if (_tcsstr(url, TEXT("/accounts/CheckCookie"))) {
MessageBox(hwndMain, TEXT("Error checking cookie. ")
TEXT("Please make sure you have cookies enabled ")
TEXT("in Internet Explorer."), NULL, 0);
SetCursor(NULL);
}

// How did we get here?
else {
TCHAR buffer[512];
StringCchPrintf(buffer, 512,
TEXT("Unexpected results from Google Voice! ")
TEXT("Please report to support@supware.net:%s"), url);
MessageBox(hwndMain, buffer, NULL, 0);
SetCursor(NULL);
}
break;
}

return DefWindowProc (hWnd, wMsg, wParam, lParam);
}

//-----------------------------------------------------------------------------
// DoLButtonUp - Process WM_LBUTTONUP message for window
//

LRESULT DoLButtonUp (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) {

if (!bMouseDown)
return DefWindowProc (hWnd, wMsg, wParam, lParam);

POINT pt;
pt.x = LOWORD(lParam);
pt.y = HIWORD(lParam);
bMouseDown = false;
KillTimer(hWnd, IDT_TIMER_LONG_PRESS);
InvalidateRect(hWnd, &rClickRegion, FALSE);

StopDTMF();

nHighlightedTab = PtInRect(&rMenubar, pt)
? pt.x * 5 / rMenubar.right
: 3;

// They clicked in the titlebar
if (PtInRect(&rTitlebar, ptMouseDown) && PtInRect(&rTitlebar, pt)) {
ShowWindow(hWnd, SW_MINIMIZE);
}

// They clicked the bottom menubar
else if (PtInRect(&rMenubar, ptMouseDown) && PtInRect(&rMenubar, pt)) {
switch (nHighlightedTab) {
case 0:
PostMessage(hWnd, WM_COMMAND, CMD_GOTO_FAVORITES, NULL);
break;
case 1:
PostMessage(hWnd, WM_COMMAND, CMD_GOTO_RECENTS, NULL);
break;
case 2:
PostMessage(hWnd, WM_COMMAND, CMD_GOTO_CONTACTS, NULL);
break;
case 3:
PostMessage(hWnd, WM_COMMAND, CMD_CHANGE_SERVICE, NULL);
break;
case 4:
PostMessage(hWnd, WM_COMMAND, CMD_GOTO_SEARCH, NULL);
break;
}
}

else if (tcButtonPressed == GetDigitFor(pt) && 0 != tcButtonPressed) {
PostMessage(hWnd, WM_COMMAND, CMD_DIAL_DIGIT, (LPARAM)tcButtonPressed);
}

tcButtonPressed = 0;

return 0;
}

//-----------------------------------------------------------------------------
// DoTimer - Process WM_TIMER message for window
//
LRESULT DoTimer (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) {

SHRGINFO shrg = {0};
KillTimer(hWnd, IDT_TIMER_LONG_PRESS);

if (tcButtonPressed == VK_BACK
|| tcButtonPressed == VK_RETURN
|| tcButtonPressed == '0'
|| tcButtonPressed == '1'
|| tcButtonPressed > '1' && tcButtonPressed <= '9'
&& _tcslen(speedDialInfo[tcButtonPressed - '0'].number)
|| PtInRect(&rDigits, ptMouseDown)) {

shrg.cbSize = sizeof(SHRGINFO);
shrg.hwndClient = hWnd;
shrg.ptDown.x = ptMouseDown.x;
shrg.ptDown.y = ptMouseDown.y;
shrg.dwFlags = SHRG_RETURNCMD;

SHRecognizeGesture(&shrg);
}

return 0;
}

//-----------------------------------------------------------------------------
// DoContextMenu - Process WM_CONTEXTMENU message for window
//
LRESULT DoContextMenu (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) {
HMENU hPopupMenu = 0;
TCHAR szMenuItem[64] = {0};
BOOL result = FALSE;

if (PtInRect(&rDigits, ptMouseDown)) {
UINT cutcopyflag = _tcslen(dialString) > 0 ? MF_ENABLED : MF_GRAYED;
UINT pasteflag = IsClipboardFormatAvailable(CF_UNICODETEXT) ? MF_ENABLED : MF_GRAYED;

hPopupMenu = CreatePopupMenu();
AppendMenu(hPopupMenu, MF_STRING | cutcopyflag, CMD_CUT, TEXT("Cut"));
AppendMenu(hPopupMenu, MF_STRING | cutcopyflag, CMD_COPY, TEXT("Copy"));
AppendMenu(hPopupMenu, MF_STRING | pasteflag, CMD_PASTE, TEXT("Paste"));

result = TrackPopupMenu(hPopupMenu,
TPM_CENTERALIGN | TPM_VCENTERALIGN | TPM_RETURNCMD,
ptMouseDown.x, ptMouseDown.y, 0, hWnd, NULL);

if (result) {
PostMessage(hWnd, WM_COMMAND, result, 0);
}
bMouseDown = false;
return 0;
}

else if (tcButtonPressed == VK_RETURN) {
hPopupMenu = CreatePopupMenu();
for (int i = 0; i < cServices; i++) {
StringCchPrintf(szMenuItem, MAX_PATH, TEXT("%s %s"),
TEXT_CALL_USING, services[i].title);
AppendMenu(hPopupMenu, MF_STRING, i+1, szMenuItem);
}

SetForegroundWindow(hWnd);
result = TrackPopupMenu(hPopupMenu,
TPM_CENTERALIGN | TPM_VCENTERALIGN | TPM_RETURNCMD,
ptMouseDown.x, ptMouseDown.y, 0, hWnd, NULL);

// result may be "0", or 1 <= result <= cServices
if (result) {
PostMessage(hWnd, WM_COMMAND, CMD_PLACE_CALL, result - 1);
}
bMouseDown = false;
tcButtonPressed = 0;
return 0;
}

else if (tcButtonPressed == VK_BACK) {
PostMessage(hWnd, WM_COMMAND, CMD_CLEAR_DIGITS, NULL);
bMouseDown = false;
tcButtonPressed = 0;
return 0;
}

else if (tcButtonPressed == '0') {
StopDTMF();
PostMessage(hWnd, WM_COMMAND, CMD_DIAL_DIGIT, (LPARAM)'+');
bMouseDown = false;
tcButtonPressed = 0;
return 0;
}

else if (tcButtonPressed == '1') {
StopDTMF();
PostMessage(hWnd, WM_COMMAND, CMD_DIAL_VOICEMAIL, NULL);
bMouseDown = false;
tcButtonPressed = 0;
return 0;
}

else if (tcButtonPressed >= '2' && tcButtonPressed <= '9') {
StopDTMF();
PostMessage(hWnd, WM_COMMAND, CMD_SPEED_DIAL, tcButtonPressed);
bMouseDown = false;
tcButtonPressed = 0;
return 0;
}

return DefWindowProc (hWnd, wMsg, wParam, lParam);
}

//-----------------------------------------------------------------------------
// DoKeyDown - Process WM_KEYDOWN message for window
//
LRESULT DoKeyDown (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) {

switch (wParam) {
case VK_LEFT:
PostMessage(hWnd, WM_COMMAND, CMD_GOTO_CONTACTS, NULL);
return 0;

case VK_RIGHT:
PostMessage(hWnd, WM_COMMAND, CMD_GOTO_SEARCH, NULL);
return 0;

case VK_UP:
case VK_DOWN:
PostMessage(hWnd, WM_COMMAND, CMD_CHANGE_SERVICE, NULL);
return 0;

case VK_TTALK:
case VK_TACTION:
PostMessage(hWnd, WM_COMMAND, CMD_GREEN_BUTTON, NULL);
return 0;

case VK_BACK: // "backspace"
case VK_TBACK: // also "backspace"
case VK_RWIN: // also "backspace"
if (bKeyDown) {
// handle key repeat for "back" only
if (tcButtonPressed == VK_BACK)
PostMessage(hWnd, WM_COMMAND, CMD_DIAL_DIGIT, (LPARAM)tcButtonPressed);
return 0;
}
// yes, this is meant to fall through

case VK_TSOFT1:
case VK_TSOFT2:
MessageBox(hWnd, TEXT("soft buttons"), TEXT("iDialer"), 0);
case VK_T0:
case VK_T1:
case VK_T2:
case VK_T3:
case VK_T4:
case VK_T5:
case VK_T6:
case VK_T7:
case VK_T8:
case VK_T9:
case VK_TSTAR:
case VK_TPOUND:
bKeyDown = true;
tcButtonPressed = GetDigitFor(wParam);
CalculateClickRegion(tcButtonPressed);
SetTimer(hWnd, IDT_TIMER_LONG_PRESS, 100, NULL);
InvalidateRect(hWnd, &rClickRegion, false);
PlayDTMF(tcButtonPressed, uVolume);
return 0;
}

return DefWindowProc (hWnd, wMsg, wParam, lParam);
}

//-----------------------------------------------------------------------------
// DoKeyUp - Process WM_KEYUP message for window
//
LRESULT DoKeyUp (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) {

bKeyDown = false;
InvalidateRect(hWnd, &rClickRegion, false);
KillTimer(hWnd, IDT_TIMER_LONG_PRESS);
StopDTMF();

switch (wParam) {
case VK_LEFT:
case VK_RIGHT:
case VK_TTALK:
case VK_TACTION:
return 0;

// It appears that VK_TSOFT1 and VK_TSOFT2 don't trigger an onkeydown event...
case VK_TSOFT1:
PostMessage(hWnd, WM_COMMAND, CMD_DIAL_DIGIT, VK_TSOFT1);
tcButtonPressed = 0;
return 0;

case VK_TSOFT2:
PostMessage(hWnd, WM_COMMAND, CMD_DIAL_DIGIT, VK_BACK);
tcButtonPressed = 0;
return 0;

default:
if (0 != tcButtonPressed) {
PostMessage(hWnd, WM_COMMAND, CMD_DIAL_DIGIT,
(LPARAM)tcButtonPressed);
tcButtonPressed = 0;
}
return 0;
}

return DefWindowProc (hWnd, wMsg, wParam, lParam);
}

//-----------------------------------------------------------------------------
// DoDestroyMain - Process WM_DESTROY message for window
//
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) {

// Destroy child window if it exists
if (hwndHTML) {
DestroyWindow(hwndHTML);
hwndHTML = 0;
}

// Uninitialize the COM classes
CoUninitialize();

// Remove notifications
RegistryCloseNotification(hNotify);

// Clean up titlebar
if (bShowFullScreen)
DestroyTitlebar();

// Quit
PostQuitMessage (0);
return 0;
}

//=============================================================================
// Utility functions
//

void InitSurface(HWND hWnd) {
HRESULT hr = S_OK;
HDC hdc;
hdc = GetDC(hWnd);

// Update the RECTs for the individual sections
GetClientRect(hWnd, &rScreen);

// Title bar, with date, carrier, battery, signal strength, etc.
CopyRect(&rTitlebar, &rScreen);

// Do not display titlebar if we're not full screen (even if we want to be)
if (!bShowFullScreen
|| GetSystemMetrics(SM_CYSCREEN) > rScreen.bottom - rScreen.top) {

// collapse custom titlebar so it is not active
rTitlebar.bottom = rTitlebar.top;
}
else {
rTitlebar.bottom = rTitlebar.top + TITLE_BAR_HEIGHT * vga;
}

// Menu at the bottom of the screen
// Don't bother with the bottom menus if iContact doesn't exist
CopyRect(&rMenubar, &rScreen);
rMenubar.top = rMenubar.bottom
- (biContactExists ? SKIN_MENU_BAR_HEIGHT * vga : 0);

// Between the titlebar and the menubar
CopyRect(&rContent, &rScreen);
rContent.top = rTitlebar.bottom;
rContent.bottom = rMenubar.top;

// The region where the digits dialed show up
CopyRect(&rDigits, &rContent);
rDigits.bottom = rDigits.top
+ max(rScreen.right - rScreen.left,
rScreen.bottom - rScreen.top) / 8;

// All the buttons
// portrait (or square)
if (rScreen.bottom >= rScreen.right) {
rect1.top = rect2.top = rect3.top = rDigits.bottom;
rect1.bottom = rect2.bottom = rect3.bottom
= rect4.top = rect5.top = rect6.top
= (rContent.bottom - rDigits.bottom) / 5 + rect1.top;
rect4.bottom = rect5.bottom = rect6.bottom
= rect7.top = rect8.top = rect9.top
= (rContent.bottom - rDigits.bottom) / 5 + rect4.top;
rect7.bottom = rect8.bottom = rect9.bottom
= rects.top = rect0.top = rectp.top
= (rContent.bottom - rDigits.bottom) / 5 + rect7.top;
rects.bottom = rect0.bottom = rectp.bottom
= rAddButton.top = rCallButton.top = rBackButton.top
= (rContent.bottom - rDigits.bottom) / 5 + rects.top;
rAddButton.bottom = rCallButton.bottom = rBackButton.bottom
= rContent.bottom;

rect1.left = rect4.left = rect7.left = rects.left = rAddButton.left
= rContent.left;
rect1.right = rect4.right = rect7.right = rects.right = rAddButton.right
= rect2.left = rect5.left = rect8.left = rect0.left = rCallButton.left
= (rContent.right - rContent.left) / 3 + rContent.left;
rect2.right = rect5.right = rect8.right = rect0.right = rCallButton.right
= rect3.left = rect6.left = rect9.left = rectp.left = rBackButton.left
= rContent.right - (rContent.right - rContent.left) / 3;
rect3.right = rect6.right = rect9.right = rectp.right = rBackButton.right
= rContent.right;
}
//landscape
else {
rect1.top = rect2.top = rect3.top = rBackButton.top = rDigits.bottom;
rect1.bottom = rect2.bottom = rect3.bottom
= rect4.top = rect5.top = rect6.top
= (rContent.bottom - rDigits.bottom) / 4 + rect1.top;
rect4.bottom = rect5.bottom = rect6.bottom
= rect7.top = rect8.top = rect9.top
= (rContent.bottom - rDigits.bottom) / 4 + rect4.top;
rect7.bottom = rect8.bottom = rect9.bottom
= rects.top = rect0.top = rectp.top
= (rContent.bottom - rDigits.bottom) / 4 + rect7.top;
rects.bottom = rect0.bottom = rectp.bottom = rAddButton.bottom
= rContent.bottom;
rBackButton.bottom = rCallButton.top
= rBackButton.top + (rContent.bottom - rDigits.bottom) / 3;
rAddButton.top = rCallButton.bottom
= rContent.bottom - (rContent.bottom - rDigits.bottom) / 3;


rect1.left = rect4.left = rect7.left = rects.left
= rContent.left;
rect1.right = rect4.right = rect7.right = rects.right
= rect2.left = rect5.left = rect8.left = rect0.left
= (rContent.right - rContent.left) / 4 + rContent.left;
rect2.right = rect5.right = rect8.right = rect0.right
= rect3.left = rect6.left = rect9.left = rectp.left
= (rContent.right - rContent.left) / 4 + rect2.left;
rect3.right = rect6.right = rect9.right = rectp.right
= rBackButton.left = rCallButton.left = rAddButton.left
= (rContent.right - rContent.left) / 4 + rect3.left;
rBackButton.right = rCallButton.right = rAddButton.right
= rContent.right;
}

// Initialize the DCs and Bitmaps
if (hdcMem)
CBR(DeleteDC(hdcMem));
hdcMem = CreateCompatibleDC(hdc);
if (hbmMem)
CBR(DeleteObject(hbmMem));
hbmMem = CreateCompatibleBitmap(hdc, rScreen.right - rScreen.left,
rScreen.bottom - rScreen.top);
SelectObject(hdcMem, hbmMem);

if (hdcContent)
CBR(DeleteDC(hdcContent));
hdcContent = CreateCompatibleDC(hdc);
if (hbmContent)
CBR(DeleteObject(hbmContent));
hbmContent = CreateCompatibleBitmap(hdc, rScreen.right - rScreen.left,
rScreen.bottom - rScreen.top);
SelectObject(hdcContent, hbmContent);

if (hdcTmp)
CBR(DeleteDC(hdcTmp));
hdcTmp = CreateCompatibleDC(hdc);
if (hbmTmp)
CBR(DeleteObject(hbmTmp));
hbmTmp = CreateCompatibleBitmap(hdc, rScreen.right - rScreen.left,
rScreen.bottom - rScreen.top);
SelectObject(hdcTmp, hbmTmp);

// Initialize skin
if (!hbmSkin) {
HBITMAP hbmSkinFile = SHLoadImageFile(skinFilename);
BITMAP bmp;
GetObject(hbmSkinFile, sizeof(bmp), &bmp);

// Load skin
HGDIOBJ hTmpOld = SelectObject(hdcTmp, hbmSkinFile);

// Create skin bitmap
hdcSkin = CreateCompatibleDC(hdc);
hbmSkin = CreateCompatibleBitmap(hdc, DEFAULT_SCREEN_WIDTH * vga,
DEFAULT_SKIN_HEIGHT * vga);
SelectObject(hdcSkin, hbmSkin);

// Stretch skin to properly fit the screen
StretchBlt(hdcSkin, 0, 0, DEFAULT_SCREEN_WIDTH * vga,
DEFAULT_SKIN_HEIGHT * vga,
hdcTmp, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);

// Restore the original hdcTmp bitmap
SelectObject(hdcTmp, hTmpOld);
}

// Cache main content in off-screen bitmap, for speed
DrawContentOn(hdcContent);

CBR(ReleaseDC(hWnd, hdc));

Error:
ASSERT(SUCCEEDED(hr));
}

COLORREF GetSkinRGB(int index) {
COLORREF c = GetPixel(hdcSkin, index * vga, SKIN_COLORS_Y_OFFSET * vga);
return c;
}

TCHAR GetDigitFor(POINT pt) {

if (PtInRect(&rect1, pt))
return '1';
else if (PtInRect(&rect2, pt))
return '2';
else if (PtInRect(&rect3, pt))
return '3';
else if (PtInRect(&rect4, pt))
return '4';
else if (PtInRect(&rect5, pt))
return '5';
else if (PtInRect(&rect6, pt))
return '6';
else if (PtInRect(&rect7, pt))
return '7';
else if (PtInRect(&rect8, pt))
return '8';
else if (PtInRect(&rect9, pt))
return '9';
else if (PtInRect(&rect0, pt))
return '0';
else if (PtInRect(&rects, pt))
return '*';
else if (PtInRect(&rectp, pt))
return '#';
else if (PtInRect(&rAddButton, pt))
return VK_TSOFT1;
else if (PtInRect(&rCallButton, pt))
return VK_RETURN;
else if (PtInRect(&rBackButton, pt))
return VK_BACK;

return 0;
}

TCHAR GetDigitFor (int key) {
switch (key) {
case VK_RETURN:
case VK_BACK:
case VK_TSOFT1:
return key;
case VK_TBACK:
case VK_TSOFT2:
return VK_BACK;
case VK_T0:
return '0';
case VK_T1:
return '1';
case VK_T2:
return '2';
case VK_T3:
return '3';
case VK_T4:
return '4';
case VK_T5:
return '5';
case VK_T6:
return '6';
case VK_T7:
return '7';
case VK_T8:
return '8';
case VK_T9:
return '9';
case VK_TSTAR:
return '*';
case VK_TPOUND:
return '#';
}
return 0;
}

void CalculateClickRegion(POINT pt) {
// They clicked elsewhere in the titlebar
if (PtInRect(&rTitlebar, pt)) {
rClickRegion = rTitlebar;
}

// They clicked in the bottom menus
else if (PtInRect(&rMenubar, pt)) {
rClickRegion = rMenubar;
}

// They clicked on one of the main buttons
else if (PtInRect(&rect1, pt))
rClickRegion = rect1;
else if (PtInRect(&rect2, pt))
rClickRegion = rect2;
else if (PtInRect(&rect3, pt))
rClickRegion = rect3;
else if (PtInRect(&rect4, pt))
rClickRegion = rect4;
else if (PtInRect(&rect5, pt))
rClickRegion = rect5;
else if (PtInRect(&rect6, pt))
rClickRegion = rect6;
else if (PtInRect(&rect7, pt))
rClickRegion = rect7;
else if (PtInRect(&rect8, pt))
rClickRegion = rect8;
else if (PtInRect(&rect9, pt))
rClickRegion = rect9;
else if (PtInRect(&rect0, pt))
rClickRegion = rect0;
else if (PtInRect(&rects, pt))
rClickRegion = rects;
else if (PtInRect(&rectp, pt))
rClickRegion = rectp;
else if (PtInRect(&rAddButton, pt))
rClickRegion = rAddButton;
else if (PtInRect(&rCallButton, pt))
rClickRegion = rCallButton;
else if (PtInRect(&rBackButton, pt))
rClickRegion = rBackButton;

// Clicked on a non-clickable region
else {
rClickRegion.top = -1;
rClickRegion.bottom = -1;
}
}

void CalculateClickRegion(TCHAR key) {
switch (key) {
case VK_RETURN:
rClickRegion = rCallButton;
break;
case VK_TSOFT1:
rClickRegion = rAddButton;
break;
case VK_BACK:
rClickRegion = rBackButton;
break;
case VK_T0:
rClickRegion = rect0;
break;
case VK_T1:
rClickRegion = rect1;
break;
case VK_T2:
rClickRegion = rect2;
break;
case VK_T3:
rClickRegion = rect3;
break;
case VK_T4:
rClickRegion = rect4;
break;
case VK_T5:
rClickRegion = rect5;
break;
case VK_T6:
rClickRegion = rect6;
break;
case VK_T7:
rClickRegion = rect7;
break;
case VK_T8:
rClickRegion = rect8;
break;
case VK_T9:
rClickRegion = rect9;
break;
case VK_TSTAR:
rClickRegion = rects;
break;
case VK_TPOUND:
rClickRegion = rectp;
break;
default:
rClickRegion.top = -1;
rClickRegion.bottom = -1;
break;
}
}

void StripNonDigits(TCHAR * to, const TCHAR * from, int cchTo) {
int cTo = 0;

for (int i = 0; i < (int)_tcslen(from) && i < cchTo - 1; i++) {
if (_tcschr(VALID_DIGITS, from[i]))
to[cTo++] = from[i];
}
to[cTo] = 0;
}

void ScrubPhoneNumber(TCHAR * szNumber, int cchNumber) {
TCHAR buffer[MAX_DIALSTRING] = {0};

TCHAR * pos = szNumber;
if (*pos == '+') {
StringCchCopy(buffer, MAX_DIALSTRING, szNumber);
}
else if (_tcsstr(szNumber, szCountryCode) == pos) {
StringCchPrintf(buffer, MAX_DIALSTRING, TEXT("+%s"),
szNumber);
}
else {
StringCchPrintf(buffer, MAX_DIALSTRING, TEXT("+%s%s"),
szCountryCode, szNumber);
}

StringCchCopy(szNumber, cchNumber, buffer);
}

void FillDialString() {
int lNumber = _tcslen(dialNumber);
int lDialPlan = _tcslen(dialPlan);

int cNumber = 0;
int cDialString = 0;
bool match = true;

// Try to match up the DialNumber
// with one of the lines in DialPlan.
// The DialPlan will be stored as a [\n\r;]+ seperated list

for (int cDialPlan = 0; cDialPlan < lDialPlan; cDialPlan++) {

switch (dialPlan[cDialPlan]) {
case '\r':
case '\n':
case ';':
if (match && cNumber == lNumber) {
// SUCCESS!
goto MATCHED;
}

cDialString = 0;
cNumber = 0;
match = true;
break;

case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
case '+':
case '#':
case '*':
if (cNumber < lNumber
&& dialPlan[cDialPlan] == dialNumber[cNumber])
dialString[cDialString] = dialNumber[cNumber];
else
match = false;

cDialString++;
cNumber++;
break;

case 'N':
if (cNumber < lNumber
&& dialNumber[cNumber] >= '0'
&& dialNumber[cNumber] <= '9') {
dialString[cDialString] = dialNumber[cNumber];
cNumber++;
}
else if (cNumber == lNumber) {
dialString[cDialString] = ' ';
}
else {
match = false;
}
cDialString++;
break;

case '_':
dialString[cDialString++] = ' ';
break;

default:
dialString[cDialString] = dialPlan[cDialPlan];
cDialString++;
break;
}
}

if (cNumber != lNumber) {
// no matches, fall back to just copying the number
StringCchCopy(dialString, MAX_DIALSTRING, dialNumber);
return;
}

MATCHED:
dialString[cDialString] = 0;

// post-process the string:

// parenthesis-space means there are no more numbers...
TCHAR * paren = wcsstr(dialString, TEXT("( "));
if (paren)
*paren = 0;

// space or "-" should be removed from the end
while (_tcslen(dialString) > 0
&& (
dialString[_tcslen(dialString) - 1] == ' '
|| dialString[_tcslen(dialString) - 1] == '-'
)
) {
dialString[_tcslen(dialString) - 1] = 0;
}

//done!
}

void AddContact() {
HRESULT hr = S_OK;
IContact * pContact = NULL;
IItem * pItem = NULL;
IPOutlookApp2 * polApp;
BSTR bstrNumber;
TCHAR * pNumber = &dialString[0];
HWND hWnd = 0;

// Initialize COM libraries (for POOM)
hr = CoInitializeEx(NULL, 0);
CHR(hr);

hr = CoCreateInstance(__uuidof(Application), NULL, CLSCTX_INPROC_SERVER,
__uuidof(IPOutlookApp2), (LPVOID*) &polApp);
CHR(hr);
hWnd = FindWindow (SZ_APP_NAME, NULL);
hr = polApp->Logon((long)hWnd);
CHR(hr);

hr = polApp->CreateItem(olContactItem, (IDispatch**)&pContact);
CHR(hr);

bstrNumber = ::SysAllocString(pNumber);
pContact->put_MobileTelephoneNumber(bstrNumber);

hr = ((IDispatch*)pContact)->QueryInterface(__uuidof(IItem),
(LPVOID*)&pItem);

CHR(hr);

hr = pItem->Edit(hWnd);

Error:
// cleanup
RELEASE_OBJ(pContact);
RELEASE_OBJ(polApp);
::SysFreeString(bstrNumber);

return;
}

HRESULT Dial(const TCHAR * szNumber, const TCHAR * szName, int which) {
HRESULT hr = E_FAIL;

// TODO: better number validation
CBR(_tcslen(szNumber) >= 1);

if (!_tcscmp(szNumber, TEXT("911"))) {
hr = DialStandard(szNumber, szName);
CHR(hr);
PostMessage(hwndMain, WM_COMMAND, CMD_PLACE_CALL_COMPLETE, NULL);
}

switch (services[which].type) {
case Phone:
hr = DialStandard(szNumber, szName);
CHR(hr);
PostMessage(hwndMain, WM_COMMAND, CMD_PLACE_CALL_COMPLETE, NULL);
break;

case CallingCard:
hr = DialCallingCard(szNumber, szName, &services[which]);
CHR(hr);
PostMessage(hwndMain, WM_COMMAND, CMD_PLACE_CALL_COMPLETE, NULL);
break;

case GoogleVoice:
hr = DialGoogleVoice(szNumber, szName, &services[which]);
CHR(hr);
break;

case JaJah:
hr = DialJaJah(szNumber, szName, &services[which]);
CHR(hr);
PostMessage(hwndMain, WM_COMMAND, CMD_PLACE_CALL_COMPLETE, NULL);
break;

case Command:
hr = DialCommand(szNumber, szName, &services[which]);
CHR(hr);
PostMessage(hwndMain, WM_COMMAND, CMD_PLACE_CALL_COMPLETE, NULL);
break;

default:
CHR(E_FAIL);
break;
}

hr = S_OK;

Error:
return hr;
}

// Splits a string into multiple strings
// This will modify szData, by converting "sep" into "\0"
// Sets s1, s2, s3 to the parts of the string
void SplitString(TCHAR * pszOutput, size_t cchOutput, const TCHAR * pszInput,
TCHAR sep, TCHAR ** s1, TCHAR ** s2, TCHAR ** s3) {

StringCchCopy(pszOutput, cchOutput, pszInput);
*s1 = pszOutput;

if (!s2)
return;

*s2 = _tcschr(*s1, sep);
if (*s2 == 0) {
*s2 = *s1 + _tcslen(*s1);
}
else {
**s2 = '\0';
(*s2)++;
}

if (!s3)
return;

*s3 = _tcschr(*s2, sep);
if (*s3 == 0) {
*s3 = *s2 + _tcslen(*s2);
}
else {
**s3 = '\0';
(*s3)++;
}

return;
}

// Replaces szSearch in pszInput with pszSubstitute
void ExpandString(TCHAR * pszOutput, size_t cchOutput,
const TCHAR * pszSearch, const TCHAR * pszSubstitute) {

TCHAR buffer[MAX_PATH];
TCHAR * src = &buffer[0];
TCHAR * search;
int len = _tcslen(pszSearch);

// initialize pszOutput
StringCchCopy(buffer, MAX_PATH, pszOutput);
*pszOutput = 0;

// substitute all occurrences of pszSearch into pszOutput
while (search = _tcsstr(src, pszSearch)) {
StringCchCatN(pszOutput, cchOutput, src, search - src);
StringCchCat(pszOutput, cchOutput, pszSubstitute);
src = search + len;
}

// copy the remaining bytes
StringCchCat(pszOutput, cchOutput, src);
}

HRESULT DialStandard(const TCHAR * szNumber, const TCHAR * szName) {

#if DEBUG
MessageBox(hwndMain, szNumber, TEXT("dialing..."), 0);
#endif

LRESULT lResult = tapiRequestMakeCall(szNumber, NULL, szName, NULL);
return lResult == 0 ? S_OK : E_FAIL;
}

HRESULT DialCallingCard(const TCHAR * szNumber, const TCHAR * szName,
SERVICE * pService) {

TCHAR szFullNumber[MAX_DIALSTRING] = {0};

TCHAR szData[128] = {0};
TCHAR * pszPrefix;
TCHAR * pszSuffix;
SplitString(szData, 128, pService->data, ';', &pszPrefix, &pszSuffix);

// do not prepend if the number already begins with the same prefix
if (pszPrefix > 0 && _tcsstr(szNumber, pszPrefix) != szNumber)
StringCchCat(szFullNumber, MAX_DIALSTRING, pszPrefix);

// replace "+" at beginning of the string with "011"
if (szNumber[0] == '+') {
// (only prepend international prefix if dialing internationally)
if (_tcsstr(szNumber, szCountryCode) != &szNumber[1]) {
StringCchCat(szFullNumber, MAX_DIALSTRING, szIntlPrefix);
}
StringCchCat(szFullNumber, MAX_DIALSTRING, &szNumber[1]);
}
else {
StringCchCat(szFullNumber, MAX_DIALSTRING, szNumber);
}

// do not append if the number already ends with the same suffix
int offset = max(0, _tcslen(szNumber) - _tcslen(pszSuffix));
if (_tcsstr(szNumber, pszSuffix) != szNumber + offset)
StringCchCat(szFullNumber, MAX_DIALSTRING, pszSuffix);

return DialStandard(szFullNumber, szName);
}

HRESULT DialGoogleVoice(const TCHAR * szNumber, const TCHAR * szName,
SERVICE * pService) {

WNDCLASS wc = { 0 };
HRESULT hr = S_OK;
HWND hwndParent = GetForegroundWindow();

SetCursor(LoadCursor(NULL, IDC_WAIT));

// create web browser child window
if (hwndHTML == 0) {
VERIFY(InitHTMLControl(hInst));

int padding = 10 * vga;
int taskbar = 25 * vga;
hwndHTML = CreateWindow(
WC_HTML, NULL,
WS_CHILD | WS_BORDER | WS_VISIBLE,
padding, padding,
rScreen.right - 2 * padding,
rScreen.bottom - 2 * padding - taskbar,
hwndParent, NULL, hInst, NULL);
}
CBR(hwndHTML != NULL);

// Navigate that web browser child to google voice.
// WM_NOTIFY handling will take care of the rest.
// why /m/sms ? Because it's a short page that
// also contains the _rnr_se
BSTR bstrURL = SysAllocString(
TEXT("https://www.google.com/voice/m/sms?"));
hr = SendMessage(hwndHTML, DTM_NAVIGATE, 0, (LPARAM)bstrURL);
SysFreeString(bstrURL);
CHR(hr);

hr = S_OK;
Error:
if (FAILED(hr))
SetCursor(NULL);
return hr;
}

HRESULT DialJaJah(const TCHAR * szNumber, const TCHAR * szName,
SERVICE * pService) {

HRESULT hr = E_FAIL;

SetCursor(LoadCursor(NULL, IDC_WAIT));

TCHAR szData[128] = {0};
TCHAR * pszUsername;
TCHAR * pszPassword;
TCHAR * pszCallback;
SplitString(szData, 128, pService->data, ';',
&pszUsername, &pszPassword, &pszCallback);

if (_tcslen(pszCallback) == 0)
pszCallback = szMyNumber;

// Get scrubbed "to" number
TCHAR szToNumber[MAX_DIALSTRING] = {0};
StringCchCopy(szToNumber, MAX_DIALSTRING, szNumber);
ScrubPhoneNumber(szToNumber, MAX_DIALSTRING);

// &pszCallback[1] because JaJah doesn't want the "+" in the request
hr = HttpJaJahDial(&pszCallback[1], &szToNumber[1],
pszUsername, pszPassword);

if (FAILED(hr)) {
TCHAR szErrorMessage[MAX_PATH];
GetHttpErrorMessage(szErrorMessage, MAX_PATH);
MessageBox(NULL, szErrorMessage, SZ_APP_NAME, 0);

StringCchPrintf(szErrorMessage, MAX_PATH, TEXT("from: %s\nto: %s"),
&pszCallback[1], szToNumber);
MessageBox(NULL, szErrorMessage, SZ_APP_NAME, 0);

goto Error;
}

hr = S_OK;

Error:
SetCursor(NULL);
return hr;
}

HRESULT DialCommand(const TCHAR * szNumber, const TCHAR * szName, SERVICE * pService) {
TCHAR szCommand[MAX_PATH] = {0};
TCHAR szArgs[MAX_PATH] = {0};
PROCESS_INFORMATION pi;
HRESULT hr = S_OK;

// Data will be of the format:
// Path\Program.exe;-arg1=%number% -arg2=%name%
// where:
// Path, if relative, to where iDialer is installed, up 2 (i.e. \Program Files)
// if absolute, can be any path
// %number% will be replaced with number to be dialed
// %name% will be replaced with name to be dialed
TCHAR szData[MAX_PATH] = {0};
StringCchCopy(szData, MAX_PATH, pService->data);
ExpandString(szData, MAX_PATH, TEXT("%number%"), szNumber);
ExpandString(szData, MAX_PATH, TEXT("%name%"), szName);

// the part after the semicolon is the arguments
TCHAR * args = _tcschr(szData, ';');
CBR(args);
*(args++) = '\0';

// the part before the semicolon is the path info
if (szData[0] == '\\') {
// absolute directory specified
StringCchCopy(szCommand, MAX_PATH, szData);
}
else {
// relative filename specified, look up2 directories
::GetModuleFileName(NULL, szCommand, MAX_PATH);
TCHAR * pstr = _tcsrchr(szCommand, '\\');
if (pstr) *pstr = '\0';
pstr = _tcsrchr(szCommand, '\\');
if (pstr) *(++pstr) = '\0';
StringCchCat(szCommand, MAX_PATH, szData);
}

#if DEBUG
TCHAR szDebugBuffer[MAX_PATH];
StringCchPrintf(szDebugBuffer, MAX_PATH, TEXT("%s %s"), szCommand, args);
MessageBox(hwndMain, szDebugBuffer, TEXT("running..."), 0);
#endif

CreateProcess(szCommand, args,
NULL, NULL, FALSE, NULL, NULL, NULL, NULL, &pi);

Error:
return hr;
}

void DrawContentOn(HDC hdc) {

// Draw the digits region background
DrawNumberRegion(hdc, rDigits);

// Draw the digit buttons
DrawDigitButton(hdc, rect1, '1', TEXT(""));
DrawDigitButton(hdc, rect2, '2', TEXT("ABC"));
DrawDigitButton(hdc, rect3, '3', TEXT("DEF"));
DrawDigitButton(hdc, rect4, '4', TEXT("GHI"));
DrawDigitButton(hdc, rect5, '5', TEXT("JKL"));
DrawDigitButton(hdc, rect6, '6', TEXT("MNO"));
DrawDigitButton(hdc, rect7, '7', TEXT("PQRS"));
DrawDigitButton(hdc, rect8, '8', TEXT("TUV"));
DrawDigitButton(hdc, rect9, '9', TEXT("WXYZ"));
DrawDigitButton(hdc, rects, '*', TEXT(""));
DrawDigitButton(hdc, rect0, '0', TEXT("+"));
DrawDigitButton(hdc, rectp, '#', TEXT(""));

// Draw the function buttons

DrawAddButton(hdc, rAddButton);
DrawCallButton(hdc, rCallButton);
DrawBackButton(hdc, rBackButton);
}

void DrawGlassySurface(HDC hdc, RECT rect, UINT index1, UINT index2,
UINT index3, UINT index4) {

unsigned int Shift = 8;
COLORREF rgb0 = GetSkinRGB(index1);
COLORREF rgb1 = GetSkinRGB(index2);
COLORREF rgb2 = GetSkinRGB(index3);
COLORREF rgb3 = GetSkinRGB(index4);

TRIVERTEX vert[6];
GRADIENT_RECT gRect[3];

vert [0] .x = rect.left;
vert [0] .y = rect.top;
vert [0] .Red = GetRValue(rgb0) << Shift;
vert [0] .Green = GetGValue(rgb0) << Shift;
vert [0] .Blue = GetBValue(rgb0) << Shift;
vert [0] .Alpha = 0x0000;

vert [1] .x = rect.right;
vert [1] .y = (rect.bottom - rect.top) / 2 - 2 + rect.top;
vert [1] .Red = GetRValue(rgb1) << Shift;
vert [1] .Green = GetGValue(rgb1) << Shift;
vert [1] .Blue = GetBValue(rgb1) << Shift;
vert [1] .Alpha = 0x0000;

vert [2] .x = rect.left;
vert [2] .y = vert[1].y;
vert [2] .Red = vert[1].Red;
vert [2] .Green = vert[1].Green;
vert [2] .Blue = vert[1].Blue;
vert [2] .Alpha = vert[1].Alpha;

vert [3] .x = rect.right;
vert [3] .y = (rect.bottom - rect.top) / 2 + 2 + rect.top;
vert [3] .Red = GetRValue(rgb2) << Shift;
vert [3] .Green = GetGValue(rgb2) << Shift;
vert [3] .Blue = GetBValue(rgb2) << Shift;
vert [3] .Alpha = 0x0000;

vert [4] .x = rect.left;
vert [4] .y = vert[3].y;
vert [4] .Red = vert[3].Red;
vert [4] .Green = vert[3].Green;
vert [4] .Blue = vert[3].Blue;
vert [4] .Alpha = vert[3].Alpha;

vert [5] .x = rect.right;
vert [5] .y = rect.bottom;
vert [5] .Red = GetRValue(rgb3) << Shift;
vert [5] .Green = GetGValue(rgb3) << Shift;
vert [5] .Blue = GetBValue(rgb3) << Shift;
vert [5] .Alpha = 0x0000;

gRect [0] .UpperLeft = 0;
gRect [0] .LowerRight = 1;
gRect [1] .UpperLeft = 2;
gRect [1] .LowerRight = 3;
gRect [2] .UpperLeft = 4;
gRect [2] .LowerRight = 5;

GradientFill(hdc, vert, 6, gRect, 3, GRADIENT_FILL_RECT_V);
}

void DrawDigitButton(HDC hdc, RECT rect, TCHAR digit, TCHAR * label) {
unsigned int Shift = 8;

TRIVERTEX vert[2];
GRADIENT_RECT gRect;
COLORREF rgb0 = GetSkinRGB(SKIN_COLOR_DIGIT_BUTTON_RGB1);
COLORREF rgb1 = GetSkinRGB(SKIN_COLOR_DIGIT_BUTTON_RGB2);
HFONT hfOld;

vert [0] .x = rect.left;
vert [0] .y = rect.top;
vert [0] .Red = GetRValue(rgb0) << Shift;
vert [0] .Green = GetGValue(rgb0) << Shift;
vert [0] .Blue = GetBValue(rgb0) << Shift;
vert [0] .Alpha = 0x0000;

vert [1] .x = rect.right;
vert [1] .y = rect.bottom;
vert [1] .Red = GetRValue(rgb1) << Shift;
vert [1] .Green = GetGValue(rgb1) << Shift;
vert [1] .Blue = GetBValue(rgb1) << Shift;
vert [1] .Alpha = 0x0000;

gRect.UpperLeft = 0;
gRect.LowerRight = 1;

GradientFill(hdc, vert, 2, &gRect, 1, GRADIENT_FILL_RECT_V);

// draw the shadow
HPEN hpen = CreatePen(PS_SOLID, 1, GetSkinRGB(SKIN_COLOR_BUTTON_SHADOW));
HGDIOBJ hold = SelectObject(hdc, hpen);
MoveToEx(hdc, rect.left, rect.bottom - 1, NULL);
LineTo(hdc, rect.right - 1, rect.bottom - 1);
LineTo(hdc, rect.right - 1, rect.top);
SelectObject(hdc, hold);
DeleteObject(hpen);

// draw the highlight
hpen = CreatePen(PS_SOLID, 1, GetSkinRGB(SKIN_COLOR_BUTTON_HIGHLIGHT));
hold = SelectObject(hdc, hpen);
LineTo(hdc, rect.left, rect.top);
LineTo(hdc, rect.left, rect.bottom - 1);
SelectObject(hdc, hold);
DeleteObject(hpen);

// draw the text
hfOld = (HFONT)SelectObject(hdc, hfDigitFont);
SetTextAlign(hdc, TA_CENTER);
SetTextColor(hdc, GetSkinRGB(SKIN_COLOR_TEXT));
SetBkMode(hdc, TRANSPARENT);
int x = (rect.right - rect.left) / 2 + rect.left;
int y = (rect.bottom - rect.top - DIGIT_FONT_SIZE * vga - CAPTION_FONT_SIZE * vga) / 2
+ rect.top;
ExtTextOut(hdc, x, y, NULL, NULL, &digit, 1, 0);

// draw the caption "ABC", etc.
int len = _tcslen(label);
if (len > 0) {
SelectObject(hdc, hfCaptionFont);
SetTextColor(hdc, GetSkinRGB(SKIN_COLOR_TEXT_DIM));
SetBkMode(hdc, TRANSPARENT);
ExtTextOut(hdc, x, y + DIGIT_FONT_SIZE * vga / 2 + CAPTION_FONT_SIZE * vga * 2/3,
NULL, NULL, label, len, 0);
}

// restore original font
SelectObject(hdcMem, hfOld);
}

void DrawNumberRegion(HDC hdc, RECT rect) {
DrawGlassySurface(hdc, rect, SKIN_COLOR_NUMBER_REGION_RGB1,
SKIN_COLOR_NUMBER_REGION_RGB2, SKIN_COLOR_NUMBER_REGION_RGB3,
SKIN_COLOR_NUMBER_REGION_RGB4);
}

void DrawCallButton(HDC hdc, RECT rect) {
HFONT hfOld;

DrawGlassySurface(hdc, rect, SKIN_COLOR_CALL_BUTTON_RGB1,
SKIN_COLOR_CALL_BUTTON_RGB2, SKIN_COLOR_CALL_BUTTON_RGB3,
SKIN_COLOR_CALL_BUTTON_RGB4);

// draw phone icon
int yoffset = (rAddButton.bottom - rAddButton.top - 16 * vga) / 2 + vga;
BitBlt(hdc, rCallButton.left + 8 * vga, rCallButton.top + yoffset,
21 * vga, 16 * vga, hdcSkin, 86 * vga, 20 * vga, SRCCOPY);

// draw the word "call"
hfOld = (HFONT)SelectObject(hdc, hfDigitFont);
SetTextAlign(hdcMem, TA_CENTER);
SetTextColor(hdcMem, GetSkinRGB(SKIN_COLOR_TEXT));
SetBkMode(hdcMem, TRANSPARENT);
int x = (rect.right - rect.left + 21 * vga) / 2 + rect.left;
int y = (rect.bottom - rect.top - DIGIT_FONT_SIZE * vga) / 2 + rect.top;
ExtTextOut(hdc, x, y, NULL, NULL, TEXT_CALL, 4, 0);

// restore original font
SelectObject(hdc, hfOld);
}

void DrawAddButton(HDC hdc, RECT rect) {
DrawGlassySurface(hdc, rect, SKIN_COLOR_ADD_BUTTON_RGB1,
SKIN_COLOR_ADD_BUTTON_RGB2, SKIN_COLOR_ADD_BUTTON_RGB3,
SKIN_COLOR_ADD_BUTTON_RGB4);

int yoffset = (rAddButton.bottom - rAddButton.top - 16 * vga) / 2 + vga;
BitBlt(hdc, rAddButton.left + 25 * vga, rAddButton.top + yoffset,
27 * vga, 16 * vga, hdcSkin, 25 * vga, 21 * vga, SRCCOPY);
}

void DrawBackButton(HDC hdc, RECT rect) {
DrawGlassySurface(hdc, rect, SKIN_COLOR_BACK_BUTTON_RGB1,
SKIN_COLOR_BACK_BUTTON_RGB2, SKIN_COLOR_BACK_BUTTON_RGB3,
SKIN_COLOR_BACK_BUTTON_RGB4);

// draw "back" icon
int yoffset = (rAddButton.bottom - rAddButton.top - 16 * vga) / 2 + vga;
BitBlt(hdc, rBackButton.left + 27 * vga, rBackButton.top + yoffset,
29 * vga, 16 * vga, hdcSkin, 186 * vga, 21 * vga, SRCCOPY);
}

void LoadSettings() {
TCHAR key[32] = {0};
TCHAR value[128] = {0};

// dial plan
LoadSetting(dialPlan, MAX_DIALPLAN, SZ_IDIALER_REG_KEY, DIALPLAN,
DIALPLAN_DEFAULT);

// skin
// can be "default" or "iphone" or "\Storare Card\my_skin.png"
TCHAR szSkin[MAX_PATH];
LoadSetting(szSkin, MAX_PATH, SZ_IDIALER_REG_KEY, SKIN, SKIN_DEFAULT);
if (szSkin[0] == '\\')
StringCchCopy(skinFilename, MAX_PATH, szSkin);
else
GetCurDirFilename(skinFilename, szSkin, TEXT("png"));

// use the default skin anyway, if the specified skin can't be found
if (!FileExists(skinFilename)) {
GetCurDirFilename(skinFilename, SKIN_DEFAULT, TEXT("png"));

// die if the skin _still_ can't be found
if (!FileExists(skinFilename)) {
HWND hWnd = FindWindow (SZ_APP_NAME, NULL);
MessageBox(hWnd, TEXT("No skin found"), SZ_APP_NAME, 0);
PostMessage(hWnd, WM_CLOSE, 0, 0);
}
}

// country code
LoadSetting(szCountryCode, MAX_COUNTRY_CODE, SZ_IDIALER_REG_KEY,
COUNTRY_CODE, COUNTRY_CODE_DEFAULT);

// international dialing prefix
LoadSetting(szIntlPrefix, MAX_COUNTRY_CODE, SZ_IDIALER_REG_KEY,
INTL_PREFIX, INTL_PREFIX_DEFAULT);

// exit on minimize
LoadSetting(value, 128, SZ_IDIALER_REG_KEY, EXIT_ON_MINIMIZE,
EXIT_ON_MINIMIZE_DEFAULT);
bExitOnMinimize = value[0] == '1';

#ifdef DEBUG
bExitOnMinimize = false;
#endif

// full screen (1 = custom titlebar, 0 = standard titlebar)
LoadSetting(value, 128, SZ_IDIALER_REG_KEY, SHOW_FULL_SCREEN,
SHOW_FULL_SCREEN_DEFAULT);
bShowFullScreen = value[0] == '1';

// volume
LoadSetting(value, 128, SZ_IDIALER_REG_KEY, VOLUME_KEYPAD,
VOLUME_KEYPAD_DEFAULT);
uVolume = min(100, max(0, _ttol(value)));
uVolume = MulDiv(uVolume, 0xFF, 100);
uVolume |= uVolume << 8;

// my number -- user may want to override own phone number for voicemail
LoadSetting(szMyNumber, MAX_DIALSTRING, SZ_IDIALER_REG_KEY, MY_NUMBER);
if (_tcslen(szMyNumber) == 0) {
SHGetPhoneNumber(szMyNumber, MAX_DIALSTRING, 1);
SaveSetting(SZ_IDIALER_REG_KEY, szMyNumber, MY_NUMBER);
}
ScrubPhoneNumber(szMyNumber, MAX_DIALSTRING);

// services
for (cServices = 1; cServices <= ARRAYSIZE(services); cServices++) {

// service type
StringCchPrintf(key, 32, SERVICE_TYPE_FORMAT, cServices);
LoadSetting(value, 128, SZ_IDIALER_REG_KEY, key);
if (0 == value[0])
break;

SERVICE * pService = &services[cServices-1];

if (0 == _tcscmp(value, SERVICE_TYPE_PHONE)) {
pService->type = Phone;
}
else if (0 == _tcscmp(value, SERVICE_TYPE_CALLINGCARD)) {
pService->type = CallingCard;
}
else if (0 == _tcscmp(value, SERVICE_TYPE_GOOGLEVOICE)) {
pService->type = GoogleVoice;
}
else if (0 == _tcscmp(value, SERVICE_TYPE_JAJAH)) {
pService->type = JaJah;
}
else if (0 == _tcscmp(value, SERVICE_TYPE_COMMAND)) {
pService->type = Command;
}
else {
break;
}

// service title
StringCchPrintf(key, 64, SERVICE_TITLE_FORMAT, cServices);
LoadSetting(value, 64, SZ_IDIALER_REG_KEY, key);
StringCchCopy(pService->title, 64, value);
// Fill in with <name of carrier> if blank
if (_tcslen(pService->title) == 0) {
RegistryGetString(
SN_PHONEOPERATORNAME_ROOT,
SN_PHONEOPERATORNAME_PATH,
SN_PHONEOPERATORNAME_VALUE,
pService->title, 64);
}

// service data
StringCchPrintf(key, 128, SERVICE_DATA_FORMAT, cServices);
LoadSetting(value, 128, SZ_IDIALER_REG_KEY, key);
StringCchCopy(pService->data, 128, value);
}

// we need at least one service... make it just the phone
cServices--;
if (0 == cServices) {
cServices = 1;
services[0].type = Phone;
services[0].title[0] = 0;
services[0].data[0] = 0;
}

// which service is currently selected?
LoadSetting(value, 128, SZ_IDIALER_REG_KEY, SERVICE_NUM, SERVICE_NUM_DEFAULT);
nService = _wtoi(value) % cServices;

if (0 == _tcslen(dialNumber)) {
// fill the number with the most recently dialed number
// but obvi don't dial it
TCHAR buffer[MAX_DIALSTRING];
LoadSetting(buffer, MAX_DIALSTRING, SZ_IDIALER_REG_KEY, LAST_DIALED_NUMBER);
StripNonDigits(dialNumber, buffer, MAX_DIALSTRING);
}
}

// A return value of "0" means to immediately exit iDialer
int ParseCommandLine(const TCHAR * tcszCmdLine) {
TCHAR szBuffer[MAX_PATH];
TCHAR szPhoneNumber[MAX_PATH] = {0};
bDialImmediately = false;

StringCchCopy(szBuffer, MAX_PATH, tcszCmdLine);

// iDialer can be used as a command-line utility
// with this argument: -choosecontact
// In this case, fill dialName and number
if (_tcsstr(szBuffer, COMMAND_LINE_CHOOSECONTACT) != NULL) {
if (FAILED(ChoosePhoneNumber(dialName, szBuffer)))
return 0;
}

// iDialer can also be used as a command-line utility
// with these arguments: mynumber [-name=myname]
TCHAR * pname = _tcsstr(szBuffer, COMMAND_LINE_NAME);
if (pname != NULL) {
int nameLen = wcslen(pname) - wcslen(COMMAND_LINE_NAME);
if (nameLen > 0) {
StringCchCopyN(dialName, TAPIMAXCALLEDPARTYSIZE,
pname + wcslen(COMMAND_LINE_NAME), nameLen);
}
// erase the -name=... from the command line
*pname = 0;
}

// first load into szPhoneNumber here so as to not override
// dialNumber with an empty number
StripNonDigits(szPhoneNumber, szBuffer, MAX_DIALSTRING);

// This means there was no number to dial, so return,
// but don't dial immediately.
if (_tcslen(szPhoneNumber) == 0)
return 1;

StringCchCopy(dialNumber, MAX_DIALSTRING, szPhoneNumber);
FillDialString();

// Special case: we can dial immediately without loading the
// main window as long as we are using the default dialer
// or calling card.
if (nService == -1) {}
else if (services[nService].type == Phone) {
SaveSetting(SZ_IDIALER_REG_KEY, dialNumber, LAST_DIALED_NUMBER);
DialStandard(dialNumber, dialName);
return 0;
}
else if (services[nService].type == CallingCard) {
SaveSetting(SZ_IDIALER_REG_KEY, dialNumber, LAST_DIALED_NUMBER);
DialCallingCard(dialNumber, dialName, &services[nService]);
return 0;
}

bDialImmediately = true;
return 1;
}

Change log

r6 by supbro on Nov 25, 2010   Diff
Added new URL scheme for Google Voice
Go to: 
Project members, sign in to write a code review

Older revisions

r5 by supbro on Nov 24, 2010   Diff
Added new URL scheme for Google Voice
r2 by supbro on Apr 25, 2010   Diff
Initial GPL3 commit
All revisions of this file

File info

Size: 78086 bytes, 2463 lines
Powered by Google Project Hosting