Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

392 lines
12 KiB

  1. // tapiloc.cpp : Implementation of CTapiLocationInfo
  2. #include "stdafx.h"
  3. #include "icwhelp.h"
  4. #include "tapiloc.h"
  5. /////////////////////////////////////////////////////////////////////////////
  6. // CTapiLocationInfo
  7. HRESULT CTapiLocationInfo::OnDraw(ATL_DRAWINFO& di)
  8. {
  9. return S_OK;
  10. }
  11. STDMETHODIMP CTapiLocationInfo::get_wNumberOfLocations(short * psVal, long *pCurrLoc)
  12. {
  13. if ((psVal == NULL) || (pCurrLoc == NULL))
  14. return E_POINTER;
  15. *psVal = m_wNumTapiLocations;
  16. *pCurrLoc = m_pTC ? (long) m_dwCurrLoc : 0;
  17. return S_OK;
  18. }
  19. STDMETHODIMP CTapiLocationInfo::get_bstrAreaCode(BSTR * pbstrAreaCode)
  20. {
  21. USES_CONVERSION;
  22. if (pbstrAreaCode == NULL)
  23. return E_POINTER;
  24. *pbstrAreaCode = m_bstrAreaCode.Copy();
  25. return S_OK;
  26. }
  27. STDMETHODIMP CTapiLocationInfo::put_bstrAreaCode(BSTR bstrAreaCode)
  28. {
  29. USES_CONVERSION;
  30. m_bstrAreaCode = bstrAreaCode;
  31. return S_OK;
  32. }
  33. STDMETHODIMP CTapiLocationInfo::get_lCountryCode(long * plVal)
  34. {
  35. *plVal = m_dwCountry;
  36. return S_OK;
  37. }
  38. STDMETHODIMP CTapiLocationInfo::get_NumCountries(long *pNumOfCountry)
  39. {
  40. LPLINECOUNTRYLIST pLineCountryTemp = NULL;
  41. LPLINECOUNTRYENTRY pLCETemp;
  42. DWORD idx;
  43. DWORD dwCurLID = 0;
  44. //LPIDLOOKUPELEMENT m_rgIDLookUp;
  45. // Get TAPI country list
  46. if (m_pLineCountryList)
  47. GlobalFree(m_pLineCountryList);
  48. m_pLineCountryList = (LPLINECOUNTRYLIST)GlobalAlloc(GPTR,sizeof(LINECOUNTRYLIST));
  49. if (!m_pLineCountryList)
  50. return S_FALSE;
  51. m_pLineCountryList->dwTotalSize = sizeof(LINECOUNTRYLIST);
  52. idx = lineGetCountry(0,0x10003,m_pLineCountryList);
  53. if (idx && idx != LINEERR_STRUCTURETOOSMALL)
  54. return S_FALSE;
  55. Assert(m_pLineCountryList->dwNeededSize);
  56. pLineCountryTemp = (LPLINECOUNTRYLIST)GlobalAlloc(GPTR,
  57. (size_t)m_pLineCountryList->dwNeededSize);
  58. if (!pLineCountryTemp)
  59. return S_FALSE;
  60. pLineCountryTemp->dwTotalSize = m_pLineCountryList->dwNeededSize;
  61. GlobalFree(m_pLineCountryList);
  62. m_pLineCountryList = pLineCountryTemp;
  63. pLineCountryTemp = NULL;
  64. if (lineGetCountry(0,0x10003,m_pLineCountryList))
  65. return S_FALSE;
  66. // look up array
  67. pLCETemp = (LPLINECOUNTRYENTRY)((DWORD_PTR)m_pLineCountryList +
  68. m_pLineCountryList->dwCountryListOffset);
  69. if(m_rgNameLookUp)
  70. GlobalFree(m_rgNameLookUp);
  71. m_rgNameLookUp = (LPCNTRYNAMELOOKUPELEMENT)GlobalAlloc(GPTR,
  72. (int)(sizeof(CNTRYNAMELOOKUPELEMENT) * m_pLineCountryList->dwNumCountries));
  73. if (!m_rgNameLookUp) return S_FALSE;
  74. DWORD dwCID = atol((const char *)m_szCountryCode);
  75. for (idx=0;idx<m_pLineCountryList->dwNumCountries;idx++)
  76. {
  77. m_rgNameLookUp[idx].psCountryName = (LPTSTR)((LPBYTE)m_pLineCountryList + (DWORD)pLCETemp[idx].dwCountryNameOffset);
  78. m_rgNameLookUp[idx].pLCE = &pLCETemp[idx];
  79. if (m_rgNameLookUp[idx].pLCE->dwCountryCode == dwCID)
  80. {
  81. if (m_rgNameLookUp[idx].psCountryName)
  82. m_bstrDefaultCountry = A2BSTR(m_rgNameLookUp[idx].psCountryName);
  83. }
  84. }
  85. *pNumOfCountry = m_pLineCountryList->dwNumCountries;
  86. return S_OK;
  87. }
  88. STDMETHODIMP CTapiLocationInfo::get_CountryName(long lCountryIndex, BSTR* pszCountryName, long* pCountryCode)
  89. {
  90. *pszCountryName = A2BSTR(m_rgNameLookUp[lCountryIndex].psCountryName);
  91. if (m_rgNameLookUp[lCountryIndex].pLCE)
  92. {
  93. *pCountryCode = m_rgNameLookUp[lCountryIndex].pLCE->dwCountryCode;
  94. }
  95. return S_OK;
  96. }
  97. STDMETHODIMP CTapiLocationInfo::get_DefaultCountry(BSTR * pszCountryName)
  98. {
  99. USES_CONVERSION;
  100. if (pszCountryName == NULL)
  101. return E_POINTER;
  102. *pszCountryName = m_bstrDefaultCountry.Copy();
  103. return S_OK;
  104. }
  105. STDMETHODIMP CTapiLocationInfo::GetTapiLocationInfo(BOOL * pbRetVal)
  106. {
  107. HRESULT hr = ERROR_SUCCESS;
  108. TCHAR szAreaCode[MAX_AREACODE+1];
  109. DWORD cDevices=0;
  110. DWORD dwCurDev = 0;
  111. DWORD dwAPI = 0;
  112. LONG lrc = 0;
  113. LINEEXTENSIONID leid;
  114. LPVOID pv = NULL;
  115. DWORD dwCurLoc = 0;
  116. USES_CONVERSION;
  117. m_hLineApp=NULL;
  118. // Assume Failure
  119. *pbRetVal = FALSE;
  120. if (m_pTC)
  121. {
  122. m_dwCountry = 0; // Reset country ID, re-read TAPI info
  123. GlobalFree(m_pTC);
  124. m_pTC = NULL;
  125. }
  126. // Get area code from TAPI
  127. if (!m_bstrAreaCode)
  128. {
  129. hr = tapiGetLocationInfo(m_szCountryCode,szAreaCode);
  130. if (hr)
  131. {
  132. TraceMsg(TF_TAPIINFO, TEXT("ICWHELP:tapiGetLocationInfo failed. RUN FOR YOUR LIVES!!\n"));
  133. #ifdef UNICODE
  134. // There is no lineInitializeW verion in TAPI. So use A version lineInitialize.
  135. hr = lineInitialize(&m_hLineApp,_Module.GetModuleInstance(),LineCallback,GetSzA(IDS_TITLE),&cDevices);
  136. #else
  137. hr = lineInitialize(&m_hLineApp,_Module.GetModuleInstance(),LineCallback,GetSz(IDS_TITLE),&cDevices);
  138. #endif
  139. if (hr == ERROR_SUCCESS)
  140. {
  141. lineTranslateDialog(m_hLineApp,0,0x10004,GetActiveWindow(),NULL);
  142. lineShutdown(m_hLineApp);
  143. }
  144. hr = tapiGetLocationInfo(m_szCountryCode,szAreaCode);
  145. }
  146. if (hr)
  147. {
  148. goto GetTapiInfoExit;
  149. }
  150. m_bstrAreaCode = A2BSTR(szAreaCode);
  151. }
  152. // Get the numeric Country code from TAPI for the current location
  153. if (m_dwCountry == 0)
  154. {
  155. // Get CountryID from TAPI
  156. m_hLineApp = NULL;
  157. // Get the handle to the line app
  158. #ifdef UNICODE
  159. // There is no lineInitializeW verion in TAPI. So use A version lineInitialize.
  160. lineInitialize(&m_hLineApp,_Module.GetModuleInstance(),LineCallback,GetSzA(IDS_TITLE),&cDevices);
  161. #else
  162. lineInitialize(&m_hLineApp,_Module.GetModuleInstance(),LineCallback,GetSz(IDS_TITLE),&cDevices);
  163. #endif
  164. if (!m_hLineApp)
  165. {
  166. // if we can't figure it out because TAPI is messed up
  167. // just default to the US and bail out of here.
  168. // The user will still have the chance to pick the right answer.
  169. m_dwCountry = 1;
  170. goto GetTapiInfoExit;
  171. }
  172. if (cDevices)
  173. {
  174. // Get the TAPI API version
  175. //
  176. dwCurDev = 0;
  177. dwAPI = 0;
  178. lrc = -1;
  179. while (lrc && dwCurDev < cDevices)
  180. {
  181. // NOTE: device ID's are 0 based
  182. ZeroMemory(&leid,sizeof(leid));
  183. lrc = lineNegotiateAPIVersion(m_hLineApp,dwCurDev,0x00010004,0x00010004,&dwAPI,&leid);
  184. dwCurDev++;
  185. }
  186. if (lrc)
  187. {
  188. // TAPI and us can't agree on anything so nevermind...
  189. goto GetTapiInfoExit;
  190. }
  191. // Find the CountryID in the translate cap structure
  192. m_pTC = (LINETRANSLATECAPS *)GlobalAlloc(GPTR,sizeof(LINETRANSLATECAPS));
  193. if (!m_pTC)
  194. {
  195. // we are in real trouble here, get out!
  196. hr = ERROR_NOT_ENOUGH_MEMORY;
  197. goto GetTapiInfoExit;
  198. }
  199. // Get the needed size
  200. m_pTC->dwTotalSize = sizeof(LINETRANSLATECAPS);
  201. lrc = lineGetTranslateCaps(m_hLineApp,dwAPI,m_pTC);
  202. if(lrc)
  203. {
  204. goto GetTapiInfoExit;
  205. }
  206. pv = GlobalAlloc(GPTR, ((size_t)m_pTC->dwNeededSize));
  207. if (!pv)
  208. {
  209. hr = ERROR_NOT_ENOUGH_MEMORY;
  210. goto GetTapiInfoExit;
  211. }
  212. ((LINETRANSLATECAPS*)pv)->dwTotalSize = m_pTC->dwNeededSize;
  213. m_pTC = (LINETRANSLATECAPS*)pv;
  214. pv = NULL;
  215. lrc = lineGetTranslateCaps(m_hLineApp,dwAPI,m_pTC);
  216. if(lrc)
  217. {
  218. goto GetTapiInfoExit;
  219. }
  220. // sanity check
  221. Assert(m_pTC->dwLocationListOffset);
  222. // We have the Number of TAPI locations, so save it now
  223. m_wNumTapiLocations = (WORD)m_pTC->dwNumLocations;
  224. // Loop through the locations to find the correct country code
  225. m_plle = LPLINELOCATIONENTRY (LPSTR(m_pTC) + m_pTC->dwLocationListOffset);
  226. for (dwCurLoc = 0; dwCurLoc < m_pTC->dwNumLocations; dwCurLoc++)
  227. {
  228. if (m_pTC->dwCurrentLocationID == m_plle->dwPermanentLocationID)
  229. {
  230. m_dwCountry = m_plle->dwCountryID;
  231. m_dwCurrLoc = dwCurLoc;
  232. break; // for loop
  233. }
  234. m_plle++;
  235. }
  236. // If we could not find it in the above loop, default to US
  237. if (!m_dwCountry)
  238. {
  239. m_dwCountry = 1;
  240. goto GetTapiInfoExit;
  241. }
  242. }
  243. }
  244. *pbRetVal = TRUE; // Getting here means everything worked
  245. GetTapiInfoExit:
  246. // Give the user an Error Message, and the wizard will bail.
  247. if (!*pbRetVal)
  248. {
  249. if( m_hLineApp )
  250. {
  251. lineShutdown(m_hLineApp);
  252. m_hLineApp = NULL;
  253. }
  254. MsgBox(IDS_CONFIGAPIFAILED,MB_MYERROR);
  255. }
  256. return S_OK;
  257. }
  258. STDMETHODIMP CTapiLocationInfo::get_LocationName(long lLocationIndex, BSTR* pszLocationName)
  259. {
  260. if (m_pTC == NULL)
  261. return E_POINTER;
  262. m_plle = LPLINELOCATIONENTRY (LPSTR(m_pTC) + m_pTC->dwLocationListOffset);
  263. m_plle += lLocationIndex;
  264. *pszLocationName = A2BSTR( ((LPSTR) m_pTC) + m_plle->dwLocationNameOffset );
  265. return S_OK;
  266. }
  267. STDMETHODIMP CTapiLocationInfo::get_LocationInfo(long lLocationIndex, long *pLocationID, BSTR* pszCountryName, long *pCountryCode, BSTR* pszAreaCode)
  268. {
  269. DWORD idx;
  270. LPLINECOUNTRYLIST pLineCountryTemp = NULL;
  271. DWORD dwCurLID = 0;
  272. // Loop through the locations to find the correct country code
  273. m_plle = LPLINELOCATIONENTRY (LPSTR(m_pTC) + m_pTC->dwLocationListOffset);
  274. m_plle += lLocationIndex;
  275. // Assign country code and area code
  276. *pCountryCode = m_plle->dwCountryID;
  277. *pszAreaCode = A2BSTR( ((LPSTR) m_pTC) + m_plle->dwCityCodeOffset );
  278. // Assign location ID
  279. *pLocationID = m_plle->dwPermanentLocationID;
  280. if (m_pLineCountryList)
  281. {
  282. GlobalFree(m_pLineCountryList);
  283. m_pLineCountryList = NULL;
  284. }
  285. // Get TAPI country name from country ID
  286. m_pLineCountryList = (LPLINECOUNTRYLIST)GlobalAlloc(GPTR,sizeof(LINECOUNTRYLIST));
  287. if (!m_pLineCountryList)
  288. return E_POINTER;
  289. m_pLineCountryList->dwTotalSize = sizeof(LINECOUNTRYLIST);
  290. idx = lineGetCountry(m_plle->dwCountryID,0x10003,m_pLineCountryList);
  291. if (idx && idx != LINEERR_STRUCTURETOOSMALL)
  292. return E_POINTER;
  293. Assert(m_pLineCountryList->dwNeededSize);
  294. pLineCountryTemp = (LPLINECOUNTRYLIST)GlobalAlloc(GPTR,
  295. (size_t)m_pLineCountryList->dwNeededSize);
  296. if (!pLineCountryTemp)
  297. return E_POINTER;
  298. pLineCountryTemp->dwTotalSize = m_pLineCountryList->dwNeededSize;
  299. GlobalFree(m_pLineCountryList);
  300. m_pLineCountryList = pLineCountryTemp;
  301. if (lineGetCountry(m_plle->dwCountryID,0x10003,m_pLineCountryList))
  302. return E_POINTER;
  303. LPLINECOUNTRYENTRY pLCETemp = (LPLINECOUNTRYENTRY)((DWORD_PTR)m_pLineCountryList + m_pLineCountryList->dwCountryListOffset);
  304. LPTSTR psCountryName = (LPTSTR)((LPBYTE)m_pLineCountryList + (DWORD)pLCETemp[0].dwCountryNameOffset);
  305. *pszCountryName = A2BSTR(psCountryName);
  306. return S_OK;
  307. }
  308. STDMETHODIMP CTapiLocationInfo::put_LocationId(long lLocationID)
  309. {
  310. ASSERT(m_hLineApp);
  311. // Must call GetTapiLocationInfo to get the Tapi handle first
  312. if (m_hLineApp)
  313. {
  314. lineSetCurrentLocation(m_hLineApp, lLocationID);
  315. return S_OK;
  316. }
  317. else
  318. {
  319. return E_FAIL;
  320. }
  321. }