[SHELL32]
[reactos.git] / reactos / dll / win32 / shell32 / folders.cpp
1 /*
2 * Copyright 1997 Marcus Meissner
3 * Copyright 1998 Juergen Schmied
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20 #include "precomp.h"
21
22 WCHAR swShell32Name[MAX_PATH];
23
24 DWORD NumIconOverlayHandlers = 0;
25 IShellIconOverlayIdentifier ** Handlers = NULL;
26
27 static HRESULT getIconLocationForFolder(IShellFolder * psf, LPCITEMIDLIST pidl, UINT uFlags,
28 LPWSTR szIconFile, UINT cchMax, int *piIndex, UINT *pwFlags)
29 {
30 static const WCHAR shellClassInfo[] = { '.', 'S', 'h', 'e', 'l', 'l', 'C', 'l', 'a', 's', 's', 'I', 'n', 'f', 'o', 0 };
31 static const WCHAR iconFile[] = { 'I', 'c', 'o', 'n', 'F', 'i', 'l', 'e', 0 };
32 static const WCHAR clsid[] = { 'C', 'L', 'S', 'I', 'D', 0 };
33 static const WCHAR clsid2[] = { 'C', 'L', 'S', 'I', 'D', '2', 0 };
34 static const WCHAR iconIndex[] = { 'I', 'c', 'o', 'n', 'I', 'n', 'd', 'e', 'x', 0 };
35 static const WCHAR wszDesktopIni[] = { 'd','e','s','k','t','o','p','.','i','n','i',0 };
36 int icon_idx;
37
38 if (!(uFlags & GIL_DEFAULTICON) && (_ILGetFileAttributes(ILFindLastID(pidl), NULL, 0) & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)) != 0 )
39 {
40 WCHAR wszFolderPath[MAX_PATH];
41
42 if (!ILGetDisplayNameExW(psf, pidl, wszFolderPath, 0))
43 return FALSE;
44
45 PathAppendW(wszFolderPath, wszDesktopIni);
46
47 if (PathFileExistsW(wszFolderPath))
48 {
49 WCHAR wszPath[MAX_PATH];
50 WCHAR wszCLSIDValue[CHARS_IN_GUID];
51
52 if (GetPrivateProfileStringW(shellClassInfo, iconFile, NULL, wszPath, MAX_PATH, wszFolderPath))
53 {
54 ExpandEnvironmentStringsW(wszPath, szIconFile, cchMax);
55
56 *piIndex = GetPrivateProfileIntW(shellClassInfo, iconIndex, 0, wszFolderPath);
57 return S_OK;
58 }
59 else if (GetPrivateProfileStringW(shellClassInfo, clsid, NULL, wszCLSIDValue, CHARS_IN_GUID, wszFolderPath) &&
60 HCR_GetIconW(wszCLSIDValue, szIconFile, NULL, cchMax, &icon_idx))
61 {
62 *piIndex = icon_idx;
63 return S_OK;
64 }
65 else if (GetPrivateProfileStringW(shellClassInfo, clsid2, NULL, wszCLSIDValue, CHARS_IN_GUID, wszFolderPath) &&
66 HCR_GetIconW(wszCLSIDValue, szIconFile, NULL, cchMax, &icon_idx))
67 {
68 *piIndex = icon_idx;
69 return S_OK;
70 }
71 }
72 }
73
74 static const WCHAR folder[] = { 'F', 'o', 'l', 'd', 'e', 'r', 0 };
75
76 if (!HCR_GetIconW(folder, szIconFile, NULL, cchMax, &icon_idx))
77 {
78 lstrcpynW(szIconFile, swShell32Name, cchMax);
79 icon_idx = -IDI_SHELL_FOLDER;
80 }
81
82 if (uFlags & GIL_OPENICON)
83 *piIndex = icon_idx < 0 ? icon_idx - 1 : icon_idx + 1;
84 else
85 *piIndex = icon_idx;
86
87 return S_OK;
88 }
89
90 static void InitIconOverlays(void)
91 {
92 HKEY hKey;
93 DWORD dwIndex, dwResult, dwSize;
94 WCHAR szName[MAX_PATH];
95 WCHAR szValue[100];
96 CLSID clsid;
97
98 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ShellIconOverlayIdentifiers", 0, KEY_READ, &hKey) != ERROR_SUCCESS)
99 return;
100
101 if (RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &dwResult, NULL, NULL, NULL, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
102 {
103 RegCloseKey(hKey);
104 return;
105 }
106
107 Handlers = (IShellIconOverlayIdentifier **)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwResult * sizeof(IShellIconOverlayIdentifier*));
108 if (!Handlers)
109 {
110 RegCloseKey(hKey);
111 return;
112 }
113
114 dwIndex = 0;
115
116 CoInitialize(0);
117
118 do
119 {
120 dwSize = sizeof(szName) / sizeof(WCHAR);
121 dwResult = RegEnumKeyExW(hKey, dwIndex, szName, &dwSize, NULL, NULL, NULL, NULL);
122
123 if (dwResult == ERROR_NO_MORE_ITEMS)
124 break;
125
126 if (dwResult == ERROR_SUCCESS)
127 {
128 dwSize = sizeof(szValue) / sizeof(WCHAR);
129 if (RegGetValueW(hKey, szName, NULL, RRF_RT_REG_SZ, NULL, szValue, &dwSize) == ERROR_SUCCESS)
130 {
131 CComPtr<IShellIconOverlayIdentifier> Overlay;
132
133 CLSIDFromString(szValue, &clsid);
134 dwResult = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARG(IShellIconOverlayIdentifier, &Overlay));
135 if (dwResult == S_OK)
136 {
137 Handlers[NumIconOverlayHandlers] = Overlay.Detach();
138 NumIconOverlayHandlers++;
139 }
140 }
141 }
142
143 dwIndex++;
144
145 } while(1);
146
147 RegCloseKey(hKey);
148 }
149
150 BOOL
151 GetIconOverlay(LPCITEMIDLIST pidl, WCHAR * wTemp, int* pIndex)
152 {
153 DWORD Index;
154 HRESULT hResult;
155 int Priority;
156 int HighestPriority;
157 ULONG IconIndex;
158 ULONG Flags;
159 WCHAR szPath[MAX_PATH];
160
161 if(!SHGetPathFromIDListW(pidl, szPath))
162 return FALSE;
163
164 if (!Handlers)
165 InitIconOverlays();
166
167 HighestPriority = 101;
168 IconIndex = NumIconOverlayHandlers;
169 for(Index = 0; Index < NumIconOverlayHandlers; Index++)
170 {
171 hResult = Handlers[Index]->IsMemberOf(szPath, SFGAO_FILESYSTEM);
172 if (hResult == S_OK)
173 {
174 hResult = Handlers[Index]->GetPriority(&Priority);
175 if (hResult == S_OK)
176 {
177 if (Priority < HighestPriority)
178 {
179 HighestPriority = Priority;
180 IconIndex = Index;
181 }
182 }
183 }
184 }
185
186 if (IconIndex == NumIconOverlayHandlers)
187 return FALSE;
188
189 hResult = Handlers[IconIndex]->GetOverlayInfo(wTemp, MAX_PATH, pIndex, &Flags);
190
191 if (hResult == S_OK)
192 return TRUE;
193 else
194 return FALSE;
195 }
196
197 HRESULT CFSExtractIcon_CreateInstance(IShellFolder * psf, LPCITEMIDLIST pidl, REFIID iid, LPVOID * ppvOut)
198 {
199 CComPtr<IDefaultExtractIconInit> initIcon;
200 HRESULT hr;
201 int icon_idx = 0;
202 UINT flags = 0; // FIXME: Use it!
203 CHAR sTemp[MAX_PATH] = "";
204 WCHAR wTemp[MAX_PATH] = L"";
205
206 hr = SHCreateDefaultExtractIcon(IID_PPV_ARG(IDefaultExtractIconInit,&initIcon));
207 if (FAILED(hr))
208 return hr;
209
210 if (_ILIsFolder (pidl))
211 {
212 if (SUCCEEDED(getIconLocationForFolder(psf,
213 pidl, 0, wTemp, _countof(wTemp),
214 &icon_idx,
215 &flags)))
216 {
217 initIcon->SetNormalIcon(wTemp, icon_idx);
218 // FIXME: if/when getIconLocationForFolder does something for
219 // GIL_FORSHORTCUT, code below should be uncommented. and
220 // the following line removed.
221 initIcon->SetShortcutIcon(wTemp, icon_idx);
222 }
223 if (SUCCEEDED(getIconLocationForFolder(psf,
224 pidl, GIL_DEFAULTICON, wTemp, _countof(wTemp),
225 &icon_idx,
226 &flags)))
227 {
228 initIcon->SetDefaultIcon(wTemp, icon_idx);
229 }
230 // if (SUCCEEDED(getIconLocationForFolder(psf,
231 // pidl, GIL_FORSHORTCUT, wTemp, _countof(wTemp),
232 // &icon_idx,
233 // &flags)))
234 // {
235 // initIcon->SetShortcutIcon(wTemp, icon_idx);
236 // }
237 if (SUCCEEDED(getIconLocationForFolder(psf,
238 pidl, GIL_OPENICON, wTemp, _countof(wTemp),
239 &icon_idx,
240 &flags)))
241 {
242 initIcon->SetOpenIcon(wTemp, icon_idx);
243 }
244 }
245 else
246 {
247 BOOL found = FALSE;
248
249 if (_ILGetExtension(pidl, sTemp, _countof(sTemp)))
250 {
251 if (HCR_MapTypeToValueA(sTemp, sTemp, _countof(sTemp), TRUE)
252 && HCR_GetIconA(sTemp, sTemp, NULL, _countof(sTemp), &icon_idx))
253 {
254 if (!lstrcmpA("%1", sTemp)) /* icon is in the file */
255 {
256 ILGetDisplayNameExW(psf, pidl, wTemp, 0);
257 icon_idx = 0;
258 }
259 else
260 {
261 MultiByteToWideChar(CP_ACP, 0, sTemp, -1, wTemp, _countof(wTemp));
262 }
263
264 found = TRUE;
265 }
266 else if (!lstrcmpiA(sTemp, "lnkfile"))
267 {
268 /* extract icon from shell shortcut */
269 CComPtr<IShellLinkW> psl;
270
271 HRESULT hr = psf->GetUIObjectOf(NULL, 1, &pidl, IID_NULL_PPV_ARG(IShellLinkW, &psl));
272 if (SUCCEEDED(hr))
273 {
274 hr = psl->GetIconLocation(wTemp, _countof(wTemp), &icon_idx);
275
276 if (SUCCEEDED(hr) && *wTemp)
277 found = TRUE;
278 }
279 }
280 }
281
282 /* FIXME: We should normally use the correct icon format according to 'flags' */
283 if (!found)
284 /* default icon */
285 initIcon->SetNormalIcon(swShell32Name, 0);
286 else
287 initIcon->SetNormalIcon(wTemp, icon_idx);
288 }
289
290 return initIcon->QueryInterface(iid, ppvOut);
291 }