[SHELL32] -Move CFSExtractIcon_CreateInstance to CFSFolder.cpp
[reactos.git] / reactos / dll / win32 / shell32 / folders / CFSFolder.cpp
index 54c06b4..393f7bf 100644 (file)
 
 WINE_DEFAULT_DEBUG_CHANNEL (shell);
 
+static HRESULT getIconLocationForFolder(IShellFolder * psf, LPCITEMIDLIST pidl, UINT uFlags,
+                                        LPWSTR szIconFile, UINT cchMax, int *piIndex, UINT *pwFlags)
+{
+    static const WCHAR shellClassInfo[] = { '.', 'S', 'h', 'e', 'l', 'l', 'C', 'l', 'a', 's', 's', 'I', 'n', 'f', 'o', 0 };
+    static const WCHAR iconFile[] = { 'I', 'c', 'o', 'n', 'F', 'i', 'l', 'e', 0 };
+    static const WCHAR clsid[] = { 'C', 'L', 'S', 'I', 'D', 0 };
+    static const WCHAR clsid2[] = { 'C', 'L', 'S', 'I', 'D', '2', 0 };
+    static const WCHAR iconIndex[] = { 'I', 'c', 'o', 'n', 'I', 'n', 'd', 'e', 'x', 0 };
+    static const WCHAR wszDesktopIni[] = { 'd','e','s','k','t','o','p','.','i','n','i',0 };
+    int icon_idx;
+
+    if (!(uFlags & GIL_DEFAULTICON) && (_ILGetFileAttributes(ILFindLastID(pidl), NULL, 0) & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)) != 0 )
+    {
+        WCHAR wszFolderPath[MAX_PATH];
+
+        if (!ILGetDisplayNameExW(psf, pidl, wszFolderPath, 0))
+            return FALSE;
+
+        PathAppendW(wszFolderPath, wszDesktopIni);
+
+        if (PathFileExistsW(wszFolderPath))
+        {
+            WCHAR wszPath[MAX_PATH];
+            WCHAR wszCLSIDValue[CHARS_IN_GUID];
+
+            if (GetPrivateProfileStringW(shellClassInfo, iconFile, NULL, wszPath, MAX_PATH, wszFolderPath))
+            {
+                ExpandEnvironmentStringsW(wszPath, szIconFile, cchMax);
+
+                *piIndex = GetPrivateProfileIntW(shellClassInfo, iconIndex, 0, wszFolderPath);
+                return S_OK;
+            }
+            else if (GetPrivateProfileStringW(shellClassInfo, clsid, NULL, wszCLSIDValue, CHARS_IN_GUID, wszFolderPath) &&
+                HCR_GetIconW(wszCLSIDValue, szIconFile, NULL, cchMax, &icon_idx))
+            {
+                *piIndex = icon_idx;
+                return S_OK;
+            }
+            else if (GetPrivateProfileStringW(shellClassInfo, clsid2, NULL, wszCLSIDValue, CHARS_IN_GUID, wszFolderPath) &&
+                HCR_GetIconW(wszCLSIDValue, szIconFile, NULL, cchMax, &icon_idx))
+            {
+                *piIndex = icon_idx;
+                return S_OK;
+            }
+        }
+    }
+
+    static const WCHAR folder[] = { 'F', 'o', 'l', 'd', 'e', 'r', 0 };
+
+    if (!HCR_GetIconW(folder, szIconFile, NULL, cchMax, &icon_idx))
+    {
+        lstrcpynW(szIconFile, swShell32Name, cchMax);
+        icon_idx = -IDI_SHELL_FOLDER;
+    }
+
+    if (uFlags & GIL_OPENICON)
+        *piIndex = icon_idx < 0 ? icon_idx - 1 : icon_idx + 1;
+    else
+        *piIndex = icon_idx;
+
+    return S_OK;
+}
+
+HRESULT CFSExtractIcon_CreateInstance(IShellFolder * psf, LPCITEMIDLIST pidl, REFIID iid, LPVOID * ppvOut)
+{
+    CComPtr<IDefaultExtractIconInit> initIcon;
+    HRESULT hr;
+    int icon_idx = 0;
+    UINT flags = 0; // FIXME: Use it!
+    CHAR sTemp[MAX_PATH] = "";
+    WCHAR wTemp[MAX_PATH] = L"";
+
+    hr = SHCreateDefaultExtractIcon(IID_PPV_ARG(IDefaultExtractIconInit,&initIcon));
+    if (FAILED(hr))
+        return hr;
+
+    if (_ILIsFolder (pidl))
+    {
+        if (SUCCEEDED(getIconLocationForFolder(psf, 
+                          pidl, 0, wTemp, _countof(wTemp),
+                          &icon_idx,
+                          &flags)))
+        {
+            initIcon->SetNormalIcon(wTemp, icon_idx);
+            // FIXME: if/when getIconLocationForFolder does something for 
+            //        GIL_FORSHORTCUT, code below should be uncommented. and
+            //        the following line removed.
+            initIcon->SetShortcutIcon(wTemp, icon_idx);
+        }
+        if (SUCCEEDED(getIconLocationForFolder(psf, 
+                          pidl, GIL_DEFAULTICON, wTemp, _countof(wTemp),
+                          &icon_idx,
+                          &flags)))
+        {
+            initIcon->SetDefaultIcon(wTemp, icon_idx);
+        }
+        // if (SUCCEEDED(getIconLocationForFolder(psf, 
+        //                   pidl, GIL_FORSHORTCUT, wTemp, _countof(wTemp),
+        //                   &icon_idx,
+        //                   &flags)))
+        // {
+        //     initIcon->SetShortcutIcon(wTemp, icon_idx);
+        // }
+        if (SUCCEEDED(getIconLocationForFolder(psf, 
+                          pidl, GIL_OPENICON, wTemp, _countof(wTemp),
+                          &icon_idx,
+                          &flags)))
+        {
+            initIcon->SetOpenIcon(wTemp, icon_idx);
+        }
+    }
+    else
+    {
+        BOOL found = FALSE;
+
+        if (_ILGetExtension(pidl, sTemp, _countof(sTemp)))
+        {
+            if (HCR_MapTypeToValueA(sTemp, sTemp, _countof(sTemp), TRUE)
+                    && HCR_GetIconA(sTemp, sTemp, NULL, _countof(sTemp), &icon_idx))
+            {
+                if (!lstrcmpA("%1", sTemp)) /* icon is in the file */
+                {
+                    ILGetDisplayNameExW(psf, pidl, wTemp, 0);
+                    icon_idx = 0;
+                }
+                else
+                {
+                    MultiByteToWideChar(CP_ACP, 0, sTemp, -1, wTemp, _countof(wTemp));
+                }
+
+                found = TRUE;
+            }
+            else if (!lstrcmpiA(sTemp, "lnkfile"))
+            {
+                /* extract icon from shell shortcut */
+                CComPtr<IShellLinkW> psl;
+                CComPtr<IExtractIconW> pei;
+
+                HRESULT hr = psf->GetUIObjectOf(NULL, 1, &pidl, IID_NULL_PPV_ARG(IShellLinkW, &psl));
+                if (SUCCEEDED(hr))
+                {
+                    hr = psl->GetIconLocation(wTemp, _countof(wTemp), &icon_idx);
+                    if (FAILED(hr) || !*wTemp)
+                    {
+                        /* The icon was not found directly, try to retrieve it from the shell link target */
+                        hr = psl->QueryInterface(IID_PPV_ARG(IExtractIconW, &pei));
+                        if (FAILED(hr) || !pei)
+                            TRACE("No IExtractIconW interface!\n");
+                        else
+                            hr = pei->GetIconLocation(GIL_FORSHELL, wTemp, _countof(wTemp), &icon_idx, &flags);
+                    }
+
+                    if (SUCCEEDED(hr) && *wTemp)
+                        found = TRUE;
+                }
+            }
+        }
+
+        /* FIXME: We should normally use the correct icon format according to 'flags' */
+        if (!found)
+            /* default icon */
+            initIcon->SetNormalIcon(swShell32Name, 0);
+        else
+            initIcon->SetNormalIcon(wTemp, icon_idx);
+    }
+
+    return initIcon->QueryInterface(iid, ppvOut);
+}
+
 /*
 CFileSysEnum should do an initial FindFirstFile and do a FindNextFile as each file is
 returned by Next. When the enumerator is created, it can do numerous additional operations