OCContextMenu.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * Copyright (c) 2015 Daniel Molkentin <danimo@owncloud.com>. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. #include "stdafx.h"
  15. #include "OCContextMenu.h"
  16. #include "OCClientInterface.h"
  17. #include <shobjidl.h>
  18. #include <shlwapi.h>
  19. #include <shellapi.h>
  20. #include <StringUtil.h>
  21. extern long g_cDllRef;
  22. OCContextMenu::OCContextMenu(void)
  23. : m_cRef(1)
  24. {
  25. InterlockedIncrement(&g_cDllRef);
  26. }
  27. OCContextMenu::~OCContextMenu(void)
  28. {
  29. InterlockedDecrement(&g_cDllRef);
  30. }
  31. #pragma region IUnknown
  32. // Query to the interface the component supported.
  33. IFACEMETHODIMP OCContextMenu::QueryInterface(REFIID riid, void **ppv)
  34. {
  35. static const QITAB qit[] =
  36. {
  37. QITABENT(OCContextMenu, IContextMenu),
  38. QITABENT(OCContextMenu, IShellExtInit),
  39. { 0 },
  40. };
  41. return QISearch(this, qit, riid, ppv);
  42. }
  43. // Increase the reference count for an interface on an object.
  44. IFACEMETHODIMP_(ULONG) OCContextMenu::AddRef()
  45. {
  46. return InterlockedIncrement(&m_cRef);
  47. }
  48. // Decrease the reference count for an interface on an object.
  49. IFACEMETHODIMP_(ULONG) OCContextMenu::Release()
  50. {
  51. ULONG cRef = InterlockedDecrement(&m_cRef);
  52. if (0 == cRef) {
  53. delete this;
  54. }
  55. return cRef;
  56. }
  57. #pragma endregion
  58. #pragma region IShellExtInit
  59. // Initialize the context menu handler.
  60. IFACEMETHODIMP OCContextMenu::Initialize(
  61. LPCITEMIDLIST pidlFolder, LPDATAOBJECT pDataObj, HKEY hKeyProgID)
  62. {
  63. m_selectedFiles.clear();
  64. if (!pDataObj) {
  65. return E_INVALIDARG;
  66. }
  67. FORMATETC fe = { CF_HDROP, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  68. STGMEDIUM stm;
  69. if (SUCCEEDED(pDataObj->GetData(&fe, &stm))) {
  70. // Get an HDROP handle.
  71. HDROP hDrop = static_cast<HDROP>(GlobalLock(stm.hGlobal));
  72. if (hDrop) {
  73. UINT nFiles = DragQueryFile(hDrop, 0xFFFFFFFF, nullptr, 0);
  74. for (UINT i = 0; i < nFiles; ++i) {
  75. // Get the path of the file.
  76. wchar_t buffer[MAX_PATH];
  77. if (!DragQueryFile(hDrop, i, buffer, ARRAYSIZE(buffer))) {
  78. m_selectedFiles.clear();
  79. break;
  80. }
  81. if (i)
  82. m_selectedFiles += L'\x1e';
  83. m_selectedFiles += buffer;
  84. }
  85. GlobalUnlock(stm.hGlobal);
  86. }
  87. ReleaseStgMedium(&stm);
  88. }
  89. // If any value other than S_OK is returned from the method, the context
  90. // menu item is not displayed.
  91. return m_selectedFiles.empty() ? E_FAIL : S_OK;
  92. }
  93. #pragma endregion
  94. #pragma region IContextMenu
  95. void InsertSeperator(HMENU hMenu, UINT indexMenu)
  96. {
  97. // Add a separator.
  98. MENUITEMINFO sep = { sizeof(sep) };
  99. sep.fMask = MIIM_TYPE;
  100. sep.fType = MFT_SEPARATOR;
  101. InsertMenuItem(hMenu, indexMenu, TRUE, &sep);
  102. }
  103. IFACEMETHODIMP OCContextMenu::QueryContextMenu(HMENU hMenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags)
  104. {
  105. // If uFlags include CMF_DEFAULTONLY then we should not do anything.
  106. if (CMF_DEFAULTONLY & uFlags)
  107. {
  108. return MAKE_HRESULT(SEVERITY_SUCCESS, 0, USHORT(0));
  109. }
  110. m_info = OCClientInterface::FetchInfo(m_selectedFiles);
  111. if (m_info.menuItems.empty()) {
  112. return MAKE_HRESULT(SEVERITY_SUCCESS, 0, USHORT(0));
  113. }
  114. InsertSeperator(hMenu, indexMenu++);
  115. HMENU hSubmenu = CreateMenu();
  116. {
  117. MENUITEMINFO mii = { sizeof(mii) };
  118. mii.fMask = MIIM_SUBMENU | MIIM_FTYPE | MIIM_STRING;
  119. mii.hSubMenu = hSubmenu;
  120. mii.fType = MFT_STRING;
  121. mii.dwTypeData = &m_info.contextMenuTitle[0];
  122. if (!InsertMenuItem(hMenu, indexMenu++, TRUE, &mii))
  123. return HRESULT_FROM_WIN32(GetLastError());
  124. }
  125. InsertSeperator(hMenu, indexMenu++);
  126. UINT indexSubMenu = 0;
  127. for (auto &item : m_info.menuItems) {
  128. bool disabled = item.flags.find(L'd') != std::string::npos;
  129. MENUITEMINFO mii = { sizeof(mii) };
  130. mii.fMask = MIIM_ID | MIIM_FTYPE | MIIM_STRING | MIIM_STATE;
  131. mii.wID = idCmdFirst + indexSubMenu;
  132. mii.fType = MFT_STRING;
  133. mii.dwTypeData = &item.title[0];
  134. mii.fState = disabled ? MFS_DISABLED : MFS_ENABLED;
  135. if (!InsertMenuItem(hSubmenu, indexSubMenu, true, &mii))
  136. return HRESULT_FROM_WIN32(GetLastError());
  137. indexSubMenu++;
  138. }
  139. // Return an HRESULT value with the severity set to SEVERITY_SUCCESS.
  140. // Set the code value to the offset of the largest command identifier
  141. // that was assigned, plus one (1).
  142. return MAKE_HRESULT(SEVERITY_SUCCESS, 0, USHORT(indexSubMenu));
  143. }
  144. IFACEMETHODIMP OCContextMenu::InvokeCommand(LPCMINVOKECOMMANDINFO pici)
  145. {
  146. std::wstring command;
  147. CMINVOKECOMMANDINFOEX *piciEx = nullptr;
  148. if (pici->cbSize == sizeof(CMINVOKECOMMANDINFOEX))
  149. piciEx = (CMINVOKECOMMANDINFOEX*)pici;
  150. // For the Unicode case, if the high-order word is not zero, the
  151. // command's verb string is in lpcmi->lpVerbW.
  152. if (piciEx
  153. && (piciEx->fMask & CMIC_MASK_UNICODE)
  154. && HIWORD(((CMINVOKECOMMANDINFOEX*)pici)->lpVerbW)) {
  155. command = piciEx->lpVerbW;
  156. // Verify that we handle the verb
  157. bool handled = false;
  158. for (auto &item : m_info.menuItems) {
  159. if (item.command == command) {
  160. handled = true;
  161. break;
  162. }
  163. }
  164. if (!handled)
  165. return E_FAIL;
  166. } else if (IS_INTRESOURCE(pici->lpVerb)) {
  167. // If the command cannot be identified through the verb string, then
  168. // check the identifier offset.
  169. auto offset = LOWORD(pici->lpVerb);
  170. if (offset >= m_info.menuItems.size())
  171. return E_FAIL;
  172. command = m_info.menuItems[offset].command;
  173. } else {
  174. return E_FAIL;
  175. }
  176. OCClientInterface::SendRequest(command.data(), m_selectedFiles);
  177. return S_OK;
  178. }
  179. IFACEMETHODIMP OCContextMenu::GetCommandString(UINT_PTR idCommand,
  180. UINT uFlags, UINT *pwReserved, LPSTR pszName, UINT cchMax)
  181. {
  182. if (idCommand < m_info.menuItems.size() && uFlags == GCS_VERBW) {
  183. return StringCchCopyW(reinterpret_cast<PWSTR>(pszName), cchMax,
  184. m_info.menuItems[idCommand].command.data());
  185. }
  186. return E_INVALIDARG;
  187. }
  188. #pragma endregion