https://git.reactos.org/?p=reactos.git;a=commitdiff;h=281f7c4e38cef875077d4…
commit 281f7c4e38cef875077d4aa620745c5cdb5239dd
Author: Katayama Hirofumi MZ <katayama.hirofumi.mz(a)gmail.com>
AuthorDate: Tue Feb 27 19:55:57 2024 +0900
Commit: GitHub <noreply(a)github.com>
CommitDate: Tue Feb 27 19:55:57 2024 +0900
[BROWSEUI] Implement 'Add to Favorites' (#6543)
Implement Explorer's "Add to Favorites" feature.
JIRA issue: CORE-16857
- Add CreateShortcut helper function.
- Add CShellBrowser::OnAddToFavorites.
---
dll/win32/browseui/shellbrowser.cpp | 84 +++++++++++++++++++++++++++++++++----
1 file changed, 76 insertions(+), 8 deletions(-)
diff --git a/dll/win32/browseui/shellbrowser.cpp b/dll/win32/browseui/shellbrowser.cpp
index d24c203c915..0126cf30d41 100644
--- a/dll/win32/browseui/shellbrowser.cpp
+++ b/dll/win32/browseui/shellbrowser.cpp
@@ -612,6 +612,7 @@ public:
LRESULT OnGoUpLevel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL &bHandled);
LRESULT OnBackspace(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL &bHandled);
LRESULT OnGoHome(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL &bHandled);
+ LRESULT OnAddToFavorites(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL
&bHandled);
LRESULT OnOrganizeFavorites(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL
&bHandled);
LRESULT OnToggleStatusBarVisible(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL
&bHandled);
LRESULT OnToggleToolbarLock(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL
&bHandled);
@@ -660,6 +661,7 @@ public:
COMMAND_ID_HANDLER(IDM_GOTO_FORWARD, OnGoForward)
COMMAND_ID_HANDLER(IDM_GOTO_UPONELEVEL, OnGoUpLevel)
COMMAND_ID_HANDLER(IDM_GOTO_HOMEPAGE, OnGoHome)
+ COMMAND_ID_HANDLER(IDM_FAVORITES_ADDTOFAVORITES, OnAddToFavorites)
COMMAND_ID_HANDLER(IDM_FAVORITES_ORGANIZEFAVORITES, OnOrganizeFavorites)
COMMAND_ID_HANDLER(IDM_VIEW_STATUSBAR, OnToggleStatusBarVisible)
COMMAND_ID_HANDLER(IDM_VIEW_REFRESH, OnRefresh)
@@ -3656,24 +3658,90 @@ LRESULT CShellBrowser::OnBackspace(WORD wNotifyCode, WORD wID,
HWND hWndCtl, BOO
return 0;
}
-LRESULT CShellBrowser::OnOrganizeFavorites(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL
&bHandled)
+static BOOL
+CreateShortcut(
+ IN LPCWSTR pszLnkFileName,
+ IN LPCITEMIDLIST pidl,
+ IN LPCWSTR pszDescription OPTIONAL)
{
- CComPtr<IShellFolder> psfDesktop;
- LPITEMIDLIST pidlFavs;
- HRESULT hr;
- hr = SHGetSpecialFolderLocation(m_hWnd, CSIDL_FAVORITES, &pidlFavs);
+ IPersistFile *pPF;
+ IShellLinkW *pSL;
+ HRESULT hr = CoInitialize(NULL);
if (FAILED(hr))
+ return hr;
+
+ hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
+ IID_IShellLinkW, (LPVOID*)&pSL);
+ if (SUCCEEDED(hr))
{
- hr = SHGetSpecialFolderLocation(m_hWnd, CSIDL_COMMON_FAVORITES, &pidlFavs);
- if (FAILED(hr))
- return 0;
+ pSL->SetIDList(pidl);
+
+ if (pszDescription)
+ pSL->SetDescription(pszDescription);
+
+ hr = pSL->QueryInterface(IID_IPersistFile, (LPVOID*)&pPF);
+ if (SUCCEEDED(hr))
+ {
+ hr = pPF->Save(pszLnkFileName, TRUE);
+ pPF->Release();
+ }
+ pSL->Release();
}
+ CoUninitialize();
+
+ return SUCCEEDED(hr);
+}
+
+HRESULT GetFavsLocation(HWND hWnd, LPITEMIDLIST *pPidl)
+{
+ HRESULT hr = SHGetSpecialFolderLocation(hWnd, CSIDL_FAVORITES, pPidl);
+ if (FAILED(hr))
+ hr = SHGetSpecialFolderLocation(hWnd, CSIDL_COMMON_FAVORITES, pPidl);
+
+ return hr;
+}
+
+LRESULT CShellBrowser::OnAddToFavorites(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL
&bHandled)
+{
+ LPITEMIDLIST pidlFavs;
+ HRESULT hr = GetFavsLocation(m_hWnd, &pidlFavs);
+ if (FAILED_UNEXPECTEDLY(hr))
+ return 0;
+
+ SHFILEINFOW fileInfo = { NULL };
+ if (!SHGetFileInfoW((LPCWSTR)fCurrentDirectoryPIDL, 0, &fileInfo,
sizeof(fileInfo),
+ SHGFI_PIDL | SHGFI_DISPLAYNAME))
+ {
+ return 0;
+ }
+
+ WCHAR szPath[MAX_PATH];
+ SHGetPathFromIDListW(pidlFavs, szPath);
+ PathAppendW(szPath, fileInfo.szDisplayName);
+ PathAddExtensionW(szPath, L".lnk");
+
+ CreateShortcut(szPath, fCurrentDirectoryPIDL, NULL);
+ return 0;
+}
+
+LRESULT CShellBrowser::OnOrganizeFavorites(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL
&bHandled)
+{
+ CComPtr<IShellFolder> psfDesktop;
+ LPITEMIDLIST pidlFavs;
+ HRESULT hr = GetFavsLocation(m_hWnd, &pidlFavs);
+ if (FAILED_UNEXPECTEDLY(hr))
+ return 0;
+
hr = SHGetDesktopFolder(&psfDesktop);
if (FAILED_UNEXPECTEDLY(hr))
+ {
+ ILFree(pidlFavs);
return 0;
+ }
hr = SHInvokeDefaultCommand(m_hWnd, psfDesktop, pidlFavs);
+ ILFree(pidlFavs);
if (FAILED_UNEXPECTEDLY(hr))
return 0;