Source code of Windows XP (NT5)
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.

372 lines
9.8 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1999 - 1999
  6. //
  7. // File: queryui.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "pch.h"
  11. #include <SnapBase.h>
  12. #include <shlobj.h>
  13. #include <dsclient.h> // Browse for container dialog
  14. #include <cmnquery.h> // IPersistQuery
  15. #include <dsquery.h> // DSFind dialog
  16. #include "resource.h"
  17. #include "editor.h"
  18. #include "queryui.h"
  19. #ifdef DEBUG_ALLOCATOR
  20. #ifdef _DEBUG
  21. #define new DEBUG_NEW
  22. #undef THIS_FILE
  23. static char THIS_FILE[] = __FILE__;
  24. #endif
  25. #endif
  26. BEGIN_MESSAGE_MAP(CADSIEditQueryDialog, CDialog)
  27. //{{AFX_MSG_MAP(CADsObjectDialog)
  28. ON_EN_CHANGE(IDC_EDIT_QUERY_STRING, OnEditQueryString)
  29. ON_EN_CHANGE(IDC_EDIT_QUERY_NAME, OnEditNameString)
  30. ON_BN_CLICKED(IDC_RADIO_ONELEVEL, OnOneLevel)
  31. ON_BN_CLICKED(IDC_RADIO_SUBTREE, OnSubtree)
  32. ON_BN_CLICKED(IDC_BUTTON_BROWSE_CONTAINER, OnBrowse)
  33. ON_BN_CLICKED(IDC_BUTTON_EDIT_QUERY, OnEditQuery)
  34. //}}AFX_MSG_MAP
  35. END_MESSAGE_MAP()
  36. CADSIEditQueryDialog::CADSIEditQueryDialog(CString& sName,
  37. CString& sFilter,
  38. CString& sPath,
  39. CString& sConnectPath,
  40. BOOL bOneLevel,
  41. CCredentialObject* pCredObject)
  42. : CDialog(IDD_QUERY_DIALOG)
  43. {
  44. m_bOneLevel = bOneLevel;
  45. m_sFilter = sFilter;
  46. m_sName = sName;
  47. m_sRootPath = sPath;
  48. m_sConnectPath = sConnectPath;
  49. m_pCredObject = pCredObject;
  50. }
  51. CADSIEditQueryDialog::CADSIEditQueryDialog(CString& sConnectPath, CCredentialObject* pCredObject)
  52. : CDialog(IDD_QUERY_DIALOG)
  53. {
  54. m_bOneLevel = FALSE;
  55. m_sConnectPath = sConnectPath;
  56. m_pCredObject = pCredObject;
  57. }
  58. CADSIEditQueryDialog::~CADSIEditQueryDialog()
  59. {
  60. }
  61. BOOL CADSIEditQueryDialog::OnInitDialog()
  62. {
  63. CDialog::OnInitDialog();
  64. CEdit* pEditName = (CEdit*)GetDlgItem(IDC_EDIT_QUERY_NAME);
  65. CEdit* pEditQueryString = (CEdit*)GetDlgItem(IDC_EDIT_QUERY_STRING);
  66. CEdit* pEditPath = (CEdit*)GetDlgItem(IDC_EDIT_ROOT_PATH);
  67. CButton* pOneLevelButton = (CButton*)GetDlgItem(IDC_RADIO_ONELEVEL);
  68. CButton* pSubtreeButton = (CButton*)GetDlgItem(IDC_RADIO_SUBTREE);
  69. CButton* pOkButton = (CButton*)GetDlgItem(IDOK);
  70. if (m_sRootPath != L"")
  71. {
  72. CString sDisplayPath;
  73. GetDisplayPath(sDisplayPath);
  74. pEditPath->SetWindowText(sDisplayPath);
  75. }
  76. pEditName->SetWindowText(m_sName);
  77. pEditName->SetLimitText(256); // REVEIW_JEFFJON : Hardcoded length
  78. pEditQueryString->SetWindowText(m_sFilter);
  79. pEditQueryString->FmtLines(FALSE);
  80. pEditQueryString->SetLimitText(256); // REVIEW_JEFFJON : Hardcoded length
  81. if (pEditQueryString->GetWindowTextLength() > 0 && pEditName->GetWindowTextLength() > 0)
  82. {
  83. pOkButton->EnableWindow(TRUE);
  84. }
  85. else
  86. {
  87. pOkButton->EnableWindow(FALSE);
  88. }
  89. pOneLevelButton->SetCheck(m_bOneLevel);
  90. pSubtreeButton->SetCheck(!m_bOneLevel);
  91. return TRUE;
  92. }
  93. void CADSIEditQueryDialog::OnEditQueryString()
  94. {
  95. CEdit* pEditName = (CEdit*)GetDlgItem(IDC_EDIT_QUERY_NAME);
  96. CEdit* pEditQueryString = (CEdit*)GetDlgItem(IDC_EDIT_QUERY_STRING);
  97. CEdit* pEditPath = (CEdit*)GetDlgItem(IDC_EDIT_ROOT_PATH);
  98. CButton* pOkButton = (CButton*)GetDlgItem(IDOK);
  99. if (pEditQueryString->GetWindowTextLength() > 0 &&
  100. pEditName->GetWindowTextLength() > 0 &&
  101. pEditPath->GetWindowTextLength() > 0)
  102. {
  103. pEditQueryString->GetWindowText(m_sFilter);
  104. pOkButton->EnableWindow(TRUE);
  105. }
  106. else
  107. {
  108. pEditQueryString->GetWindowText(m_sFilter);
  109. pOkButton->EnableWindow(FALSE);
  110. }
  111. }
  112. void CADSIEditQueryDialog::OnEditNameString()
  113. {
  114. CEdit* pEditName = (CEdit*)GetDlgItem(IDC_EDIT_QUERY_NAME);
  115. CEdit* pEditQueryString = (CEdit*)GetDlgItem(IDC_EDIT_QUERY_STRING);
  116. CEdit* pEditPath = (CEdit*)GetDlgItem(IDC_EDIT_ROOT_PATH);
  117. CButton* pOkButton = (CButton*)GetDlgItem(IDOK);
  118. if (pEditQueryString->GetWindowTextLength() > 0 &&
  119. pEditName->GetWindowTextLength() > 0 &&
  120. pEditPath->GetWindowTextLength() > 0)
  121. {
  122. pEditName->GetWindowText(m_sName);
  123. pOkButton->EnableWindow(TRUE);
  124. }
  125. else
  126. {
  127. pEditName->GetWindowText(m_sName);
  128. pOkButton->EnableWindow(FALSE);
  129. }
  130. }
  131. void CADSIEditQueryDialog::OnOneLevel()
  132. {
  133. CButton* pOneLevelButton = (CButton*)GetDlgItem(IDC_RADIO_ONELEVEL);
  134. m_bOneLevel = pOneLevelButton->GetCheck();
  135. }
  136. void CADSIEditQueryDialog::OnSubtree()
  137. {
  138. CButton* pOneLevelButton = (CButton*)GetDlgItem(IDC_RADIO_ONELEVEL);
  139. m_bOneLevel = pOneLevelButton->GetCheck();
  140. }
  141. void CADSIEditQueryDialog::OnBrowse()
  142. {
  143. HRESULT hr = S_OK, hCredResult;
  144. DWORD result;
  145. CString strTitle;
  146. strTitle.LoadString (IDS_QUERY_BROWSE_TITLE);
  147. DSBROWSEINFO dsbi;
  148. ::ZeroMemory( &dsbi, sizeof(dsbi) );
  149. TCHAR szPath[2 * MAX_PATH+1];
  150. CString str;
  151. str.LoadString(IDS_MOVE_TARGET);
  152. dsbi.hwndOwner = GetSafeHwnd();
  153. dsbi.cbStruct = sizeof (DSBROWSEINFO);
  154. dsbi.pszCaption = (LPWSTR)((LPCWSTR)strTitle); // this is actually the caption
  155. dsbi.pszTitle = (LPWSTR)((LPCWSTR)str);
  156. dsbi.pszRoot = m_sConnectPath;
  157. dsbi.pszPath = szPath;
  158. dsbi.cchPath = ((2 * MAX_PATH + 1) / sizeof(TCHAR));
  159. dsbi.dwFlags = DSBI_INCLUDEHIDDEN | DSBI_RETURN_FORMAT;
  160. dsbi.pfnCallback = NULL;
  161. dsbi.lParam = 0;
  162. dsbi.dwReturnFormat = ADS_FORMAT_X500;
  163. // Specify credentials
  164. CString sUserName;
  165. WCHAR szPassword[MAX_PASSWORD_LENGTH + 1];
  166. if (m_pCredObject->UseCredentials())
  167. {
  168. m_pCredObject->GetUsername(sUserName);
  169. m_pCredObject->GetPassword(szPassword);
  170. dsbi.dwFlags |= DSBI_HASCREDENTIALS;
  171. dsbi.pUserName = sUserName;
  172. dsbi.pPassword = szPassword;
  173. }
  174. result = DsBrowseForContainer( &dsbi );
  175. if ( result == IDOK )
  176. { // returns -1, 0, IDOK or IDCANCEL
  177. // get path from BROWSEINFO struct, put in text edit field
  178. TRACE(_T("returned from DS Browse successfully with:\n %s\n"),
  179. dsbi.pszPath);
  180. m_sRootPath = dsbi.pszPath;
  181. CEdit* pEditPath = (CEdit*)GetDlgItem(IDC_EDIT_ROOT_PATH);
  182. CString sDisplay;
  183. GetDisplayPath(sDisplay);
  184. pEditPath->SetWindowText(sDisplay);
  185. }
  186. OnEditNameString(); // to check to see if the ok button should be enabled
  187. }
  188. void CADSIEditQueryDialog::GetDisplayPath(CString& sDisplay)
  189. {
  190. CComPtr<IADsPathname> pIADsPathname;
  191. HRESULT hr = ::CoCreateInstance(CLSID_Pathname, NULL, CLSCTX_INPROC_SERVER,
  192. IID_IADsPathname, (PVOID *)&(pIADsPathname));
  193. ASSERT((S_OK == hr) && ((pIADsPathname) != NULL));
  194. hr = pIADsPathname->Set((LPWSTR)(LPCWSTR)m_sRootPath, ADS_SETTYPE_FULL);
  195. if (FAILED(hr))
  196. {
  197. TRACE(_T("Set failed. %s"), hr);
  198. }
  199. // Get the leaf name
  200. CString sDN;
  201. BSTR bstrPath = NULL;
  202. hr = pIADsPathname->Retrieve(ADS_FORMAT_X500_DN, &bstrPath);
  203. if (FAILED(hr))
  204. {
  205. TRACE(_T("Failed to get element. %s"), hr);
  206. sDN = L"";
  207. }
  208. else
  209. {
  210. sDN = bstrPath;
  211. }
  212. sDisplay = sDN;
  213. }
  214. // respone to the Edit Query... button
  215. void CADSIEditQueryDialog::OnEditQuery()
  216. {
  217. CLIPFORMAT cfDsQueryParams = (CLIPFORMAT)::RegisterClipboardFormat(CFSTR_DSQUERYPARAMS);
  218. // create a query object
  219. HRESULT hr;
  220. CComPtr<ICommonQuery> spCommonQuery;
  221. hr = ::CoCreateInstance(CLSID_CommonQuery, NULL, CLSCTX_INPROC_SERVER,
  222. IID_ICommonQuery, (PVOID *)&spCommonQuery);
  223. if (FAILED(hr))
  224. return;
  225. // setup structs to make the query
  226. DSQUERYINITPARAMS dqip;
  227. OPENQUERYWINDOW oqw;
  228. ZeroMemory(&dqip, sizeof(DSQUERYINITPARAMS));
  229. ZeroMemory(&oqw, sizeof(OPENQUERYWINDOW));
  230. // Get the username and password if we are impersonating credentials
  231. if (m_pCredObject->UseCredentials())
  232. {
  233. CString szUsername;
  234. WCHAR szPassword[MAX_PASSWORD_LENGTH + 1];
  235. m_pCredObject->GetUsername(szUsername);
  236. m_pCredObject->GetPassword(szPassword);
  237. dqip.pUserName = (LPWSTR)(LPCWSTR)szUsername;
  238. dqip.pPassword = szPassword;
  239. }
  240. dqip.cbStruct = sizeof(dqip);
  241. dqip.dwFlags = DSQPF_NOSAVE | DSQPF_SHOWHIDDENOBJECTS |
  242. DSQPF_ENABLEADMINFEATURES;
  243. dqip.pDefaultScope = NULL;
  244. oqw.cbStruct = sizeof(oqw);
  245. oqw.dwFlags = OQWF_OKCANCEL | OQWF_DEFAULTFORM | OQWF_REMOVEFORMS |
  246. OQWF_REMOVESCOPES | OQWF_SAVEQUERYONOK | OQWF_HIDEMENUS | OQWF_HIDESEARCHUI;
  247. oqw.clsidHandler = CLSID_DsQuery;
  248. oqw.pHandlerParameters = &dqip;
  249. oqw.clsidDefaultForm = CLSID_DsFindAdvanced;
  250. // Get the HWND of the current dialog
  251. HWND hWnd = GetSafeHwnd();
  252. // make the call to get the query displayed
  253. CComPtr<IDataObject> spQueryResultDataObject;
  254. hr = spCommonQuery->OpenQueryWindow(hWnd, &oqw, &spQueryResultDataObject);
  255. if (spQueryResultDataObject == NULL)
  256. {
  257. if (FAILED(hr))
  258. {
  259. // no query available, reset to no data
  260. m_sFilter = L"";
  261. return;
  262. }
  263. // user hit cancel
  264. return;
  265. }
  266. // retrieve the query string from the data object
  267. FORMATETC fmte = {cfDsQueryParams, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
  268. STGMEDIUM medium = {TYMED_NULL, NULL, NULL};
  269. hr = spQueryResultDataObject->GetData(&fmte, &medium);
  270. if (SUCCEEDED(hr)) // we have data
  271. {
  272. // get the query string
  273. LPDSQUERYPARAMS pDsQueryParams = (LPDSQUERYPARAMS)medium.hGlobal;
  274. LPWSTR pwszFilter = (LPWSTR)ByteOffset(pDsQueryParams, pDsQueryParams->offsetQuery);
  275. m_sFilter = pwszFilter;
  276. ::ReleaseStgMedium(&medium);
  277. // REVIEW_MARCOC: this is a hack waiting for Diz to fix it...
  278. // the query string should be a well formed expression. Period
  279. // the query string is in the form (<foo>)(<bar>)...
  280. // if more of one token, need to wrap as (& (<foo>)(<bar>)...)
  281. WCHAR* pChar = (WCHAR*)(LPCWSTR)m_sFilter;
  282. int nLeftPar = 0;
  283. while (*pChar != NULL)
  284. {
  285. if (*pChar == TEXT('('))
  286. {
  287. nLeftPar++;
  288. if (nLeftPar > 1)
  289. break;
  290. }
  291. pChar++;
  292. }
  293. if (nLeftPar > 1)
  294. {
  295. CString s;
  296. s.Format(_T("(&%s)"), (LPCTSTR)m_sFilter);
  297. m_sFilter = s;
  298. }
  299. TRACE(_T("m_sFilter = %s\n"), (LPCTSTR)m_sFilter);
  300. CEdit* pEditQueryString = (CEdit*)GetDlgItem(IDC_EDIT_QUERY_STRING);
  301. pEditQueryString->SetWindowText(m_sFilter);
  302. OnEditQueryString();
  303. }
  304. return;
  305. }
  306. void CADSIEditQueryDialog::GetResults(CString& sName,
  307. CString& sFilter,
  308. CString& sPath,
  309. BOOL* pOneLevel)
  310. {
  311. sName = m_sName;
  312. sFilter = m_sFilter;
  313. sPath = m_sRootPath;
  314. *pOneLevel = m_bOneLevel;
  315. }