[rshell]
[reactos.git] / dll / win32 / shell32 / openwithmenu.cpp
1 /*
2 * Open With Context Menu extension
3 *
4 * Copyright 2007 Johannes Anderwald <johannes.anderwald@reactos.org>
5 * Copyright 2009 Andrew Hill
6 * Copyright 2012 Rafal Harabien
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 #include "precomp.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(shell);
26
27 //
28 // [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\system]
29 // "NoInternetOpenWith"=dword:00000001
30 //
31
32 BOOL PathIsExeW(LPCWSTR lpszPath);
33
34 class COpenWithList
35 {
36 public:
37 struct SApp
38 {
39 WCHAR wszFilename[MAX_PATH];
40 WCHAR wszCmd[MAX_PATH];
41 //WCHAR wszManufacturer[256];
42 WCHAR wszName[256];
43 BOOL bHidden;
44 BOOL bRecommended;
45 BOOL bMRUList;
46 HICON hIcon;
47 };
48
49 COpenWithList();
50 ~COpenWithList();
51
52 BOOL Load();
53 SApp *Add(LPCWSTR pwszPath);
54 static BOOL SaveApp(SApp *pApp);
55 SApp *Find(LPCWSTR pwszFilename);
56 static LPCWSTR GetName(SApp *pApp);
57 static HICON GetIcon(SApp *pApp);
58 static BOOL Execute(SApp *pApp, LPCWSTR pwszFilePath);
59 static BOOL IsHidden(SApp *pApp);
60 BOOL LoadRecommended(LPCWSTR pwszFilePath);
61 BOOL SetDefaultHandler(SApp *pApp, LPCWSTR pwszFilename);
62
63 inline SApp *GetList() { return m_pApp; }
64 inline UINT GetCount() { return m_cApp; }
65 inline UINT GetRecommendedCount() { return m_cRecommended; }
66
67 private:
68 typedef struct _LANGANDCODEPAGE
69 {
70 WORD lang;
71 WORD code;
72 } LANGANDCODEPAGE, *LPLANGANDCODEPAGE;
73
74 SApp *m_pApp;
75 UINT m_cApp, m_cRecommended;
76 BOOL m_bNoOpen;
77
78 SApp *AddInternal(LPCWSTR pwszFilename);
79 static BOOL LoadInfo(SApp *pApp);
80 static VOID GetPathFromCmd(LPWSTR pwszAppPath, LPCWSTR pwszCmd);
81 BOOL LoadProgIdList(HKEY hKey, LPCWSTR pwszExt);
82 static HANDLE OpenMRUList(HKEY hKey);
83 BOOL LoadMRUList(HKEY hKey);
84 BOOL LoadAppList(HKEY hKey);
85 VOID LoadFromProgIdKey(HKEY hKey, LPCWSTR pwszExt);
86 VOID LoadRecommendedFromHKCR(LPCWSTR pwszExt);
87 VOID LoadRecommendedFromHKCU(LPCWSTR pwszExt);
88 static BOOL AddAppToMRUList(SApp *pApp, LPCWSTR pwszFilename);
89
90 inline VOID SetRecommended(SApp *pApp)
91 {
92 if (!pApp->bRecommended)
93 ++m_cRecommended;
94 pApp->bRecommended = TRUE;
95 }
96 };
97
98 COpenWithList::COpenWithList():
99 m_pApp(NULL), m_cApp(0), m_cRecommended(0), m_bNoOpen(FALSE) {}
100
101 COpenWithList::~COpenWithList()
102 {
103 for (UINT i = 0; i < m_cApp; ++i)
104 if (m_pApp[i].hIcon)
105 DestroyIcon(m_pApp[i].hIcon);
106
107 HeapFree(GetProcessHeap(), 0, m_pApp);
108 }
109
110 BOOL COpenWithList::Load()
111 {
112 HKEY hKey;
113 WCHAR wszName[256], wszBuf[100];;
114 DWORD i = 0, cchName, dwSize;
115 SApp *pApp;
116
117 if (RegOpenKeyEx(HKEY_CLASSES_ROOT, L"Applications", 0, KEY_READ, &hKey) != ERROR_SUCCESS)
118 {
119 ERR("RegOpenKeyEx HKCR\\Applications failed!\n");
120 return FALSE;
121 }
122
123 while (TRUE)
124 {
125 cchName = _countof(wszName);
126 if (RegEnumKeyEx(hKey, i++, wszName, &cchName, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
127 break;
128
129 pApp = AddInternal(wszName);
130
131 if (pApp)
132 {
133 StringCbPrintfW(wszBuf, sizeof(wszBuf), L"%s\\shell\\open\\command", wszName);
134 dwSize = sizeof(pApp->wszCmd);
135 if (RegGetValueW(hKey, wszBuf, L"", RRF_RT_REG_SZ, NULL, pApp->wszCmd, &dwSize) != ERROR_SUCCESS)
136 {
137 ERR("Failed to add app %ls\n", wszName);
138 pApp->bHidden = TRUE;
139 }
140 else
141 TRACE("App added %ls\n", pApp->wszCmd);
142 }
143 else
144 ERR("AddInternal failed\n");
145 }
146
147 RegCloseKey(hKey);
148 return TRUE;
149 }
150
151 COpenWithList::SApp *COpenWithList::Add(LPCWSTR pwszPath)
152 {
153 SApp *pApp = AddInternal(PathFindFileNameW(pwszPath));
154
155 if (pApp)
156 {
157 StringCbPrintfW(pApp->wszCmd, sizeof(pApp->wszCmd), L"\"%s\" %%1", pwszPath);
158 SaveApp(pApp);
159 }
160
161 return pApp;
162 }
163
164 BOOL COpenWithList::SaveApp(SApp *pApp)
165 {
166 WCHAR wszBuf[256];
167 HKEY hKey;
168
169 StringCbPrintfW(wszBuf, sizeof(wszBuf), L"Applications\\%s\\shell\\open\\command", pApp->wszFilename);
170 if (RegCreateKeyEx(HKEY_CLASSES_ROOT, wszBuf, 0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) != ERROR_SUCCESS)
171 {
172 ERR("RegOpenKeyEx failed\n");
173 return FALSE;
174 }
175
176 if (RegSetValueEx(hKey, L"", 0, REG_SZ, (PBYTE)pApp->wszCmd, (wcslen(pApp->wszCmd)+1)*sizeof(WCHAR)) != ERROR_SUCCESS)
177 ERR("Cannot add app to registry\n");
178
179 RegCloseKey(hKey);
180 return TRUE;
181 }
182
183 COpenWithList::SApp *COpenWithList::Find(LPCWSTR pwszFilename)
184 {
185 for (UINT i = 0; i < m_cApp; ++i)
186 if (wcsicmp(m_pApp[i].wszFilename, pwszFilename) == 0)
187 return &m_pApp[i];
188 return NULL;
189 }
190
191 LPCWSTR COpenWithList::GetName(SApp *pApp)
192 {
193 if (!pApp->wszName[0])
194 {
195 if (!LoadInfo(pApp))
196 {
197 WARN("Failed to load %ls info\n", pApp->wszFilename);
198 StringCbCopyW(pApp->wszName, sizeof(pApp->wszName), pApp->wszFilename);
199 }
200 }
201
202 TRACE("%ls name: %ls\n", pApp->wszFilename, pApp->wszName);
203 return pApp->wszName;
204 }
205
206 HICON COpenWithList::GetIcon(SApp *pApp)
207 {
208 if (!pApp->hIcon)
209 {
210 WCHAR wszPath[MAX_PATH];
211
212 GetPathFromCmd(wszPath, pApp->wszCmd);
213 pApp->hIcon = ExtractIconW(shell32_hInstance, wszPath, 0);
214 }
215
216 TRACE("%ls icon: %p\n", pApp->wszFilename, pApp->hIcon);
217
218 return pApp->hIcon;
219 }
220
221 BOOL COpenWithList::Execute(COpenWithList::SApp *pApp, LPCWSTR pwszFilePath)
222 {
223 STARTUPINFOW si;
224 PROCESS_INFORMATION pi;
225 WCHAR wszBuf[MAX_PATH * 2 + 8], *pszEnd = wszBuf;
226 size_t cchRemaining = _countof(wszBuf);
227
228 /* setup path with argument */
229 ZeroMemory(&si, sizeof(STARTUPINFOW));
230 si.cb = sizeof(STARTUPINFOW);
231
232 /* Build the command line */
233 for (UINT i = 0; pApp->wszCmd[i] && cchRemaining > 1; ++i)
234 {
235 if (pApp->wszCmd[i] != '%')
236 {
237 *(pszEnd++) = pApp->wszCmd[i];
238 --cchRemaining;
239 }
240 else if (pApp->wszCmd[++i] == '1')
241 {
242 if (StrChrW(pwszFilePath, L' ') && cchRemaining > 3)
243 StringCchPrintfExW(pszEnd, cchRemaining, &pszEnd, &cchRemaining, 0, L"\"%ls\"", pwszFilePath);
244 else
245 StringCchCopyExW(pszEnd, cchRemaining, pwszFilePath, &pszEnd, &cchRemaining, 0);
246 }
247 }
248 /* NULL-terminate the command string */
249 if (cchRemaining > 0)
250 *pszEnd = L'\0';
251
252 /* Start the application now */
253 TRACE("Starting process %ls\n", wszBuf);
254 if (!CreateProcessW(NULL, wszBuf, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
255 {
256 ERR("CreateProcessW %ls failed\n", wszBuf);
257 return FALSE;
258 }
259
260 /* Add app to registry if it wasnt there before */
261 SaveApp(pApp);
262 if (!pApp->bMRUList)
263 AddAppToMRUList(pApp, pwszFilePath);
264
265 CloseHandle(pi.hThread);
266 CloseHandle(pi.hProcess);
267 return TRUE;
268 }
269
270 BOOL COpenWithList::IsHidden(SApp *pApp)
271 {
272 WCHAR wszBuf[100];
273 DWORD dwSize = 0;
274
275 if (pApp->bHidden)
276 return pApp->bHidden;
277
278 if (FAILED(StringCbPrintfW(wszBuf, sizeof(wszBuf), L"Applications\\%s", pApp->wszFilename)))
279 {
280 ERR("insufficient buffer\n");
281 return FALSE;
282 }
283
284 if (RegGetValueW(HKEY_CLASSES_ROOT, wszBuf, L"NoOpenWith", RRF_RT_REG_SZ, NULL, NULL, &dwSize) != ERROR_SUCCESS)
285 return FALSE;
286
287 pApp->bHidden = TRUE;
288 return TRUE;
289 }
290
291 COpenWithList::SApp *COpenWithList::AddInternal(LPCWSTR pwszFilename)
292 {
293 /* Check for duplicate */
294 SApp *pApp = Find(pwszFilename);
295 if (pApp)
296 return pApp;
297
298 /* Create new item */
299 if (!m_pApp)
300 m_pApp = (SApp*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(m_pApp[0]));
301 else
302 m_pApp = (SApp*)HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, m_pApp, (m_cApp + 1)*sizeof(m_pApp[0]));
303 if (!m_pApp)
304 {
305 ERR("Allocation failed\n");
306 return NULL;
307 }
308
309 pApp = &m_pApp[m_cApp++];
310 wcscpy(pApp->wszFilename, pwszFilename);
311 return pApp;
312 }
313
314 BOOL COpenWithList::LoadInfo(COpenWithList::SApp *pApp)
315 {
316 UINT cbSize, cchLen;
317 LPVOID pBuf;
318 WORD wLang = 0, wCode = 0;
319 LPLANGANDCODEPAGE lpLangCode;
320 WCHAR wszBuf[100];
321 WCHAR *pResult;
322 WCHAR wszPath[MAX_PATH];
323
324 GetPathFromCmd(wszPath, pApp->wszCmd);
325 TRACE("LoadInfo %ls\n", wszPath);
326
327 /* query version info size */
328 cbSize = GetFileVersionInfoSizeW(wszPath, NULL);
329 if (!cbSize)
330 {
331 ERR("GetFileVersionInfoSizeW %ls failed: %lu\n", wszPath, GetLastError());
332 return FALSE;
333 }
334
335 /* allocate buffer */
336 pBuf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cbSize + 200);
337 if (!pBuf)
338 {
339 ERR("HeapAlloc failed\n");
340 return FALSE;
341 }
342
343 /* query version info */
344 if (!GetFileVersionInfoW(wszPath, 0, cbSize, pBuf))
345 {
346 ERR("GetFileVersionInfoW %ls failed: %lu\n", wszPath, GetLastError());
347 HeapFree(GetProcessHeap(), 0, pBuf);
348 return FALSE;
349 }
350
351 /* query lang code */
352 if (VerQueryValueW(pBuf, L"VarFileInfo\\Translation", (LPVOID*)&lpLangCode, &cbSize))
353 {
354 /* FIXME: find language from current locale / if not available,
355 * default to english
356 * for now default to first available language
357 */
358 wLang = lpLangCode->lang;
359 wCode = lpLangCode->code;
360 }
361
362 /* Query name */
363 swprintf(wszBuf, L"\\StringFileInfo\\%04x%04x\\FileDescription", wLang, wCode);
364 if (VerQueryValueW(pBuf, wszBuf, (LPVOID *)&pResult, &cchLen))
365 StringCchCopyNW(pApp->wszName, _countof(pApp->wszName), pResult, cchLen);
366 else
367 ERR("Cannot get app name\n");
368
369 /* Query manufacturer */
370 /*swprintf(wszBuf, L"\\StringFileInfo\\%04x%04x\\CompanyName", wLang, wCode);
371
372 if (VerQueryValueW(pBuf, wszBuf, (LPVOID *)&pResult, &cchLen))
373 StringCchCopyNW(pApp->wszManufacturer, _countof(pApp->wszManufacturer), pResult, cchLen);*/
374 HeapFree(GetProcessHeap(), 0, pBuf);
375 return TRUE;
376 }
377
378 VOID COpenWithList::GetPathFromCmd(LPWSTR pwszAppPath, LPCWSTR pwszCmd)
379 {
380 WCHAR wszBuf[MAX_PATH], *pwszDest = wszBuf;
381
382 /* Remove arguments */
383 if (pwszCmd[0] == '"')
384 {
385 for(LPCWSTR pwszSrc = pwszCmd + 1; *pwszSrc && *pwszSrc != '"'; ++pwszSrc)
386 *(pwszDest++) = *pwszSrc;
387 }
388 else
389 {
390 for(LPCWSTR pwszSrc = pwszCmd; *pwszSrc && *pwszSrc != ' '; ++pwszSrc)
391 *(pwszDest++) = *pwszSrc;
392 }
393
394 *pwszDest = 0;
395
396 /* Expand evn vers and optionally search for path */
397 ExpandEnvironmentStrings(wszBuf, pwszAppPath, MAX_PATH);
398 if (!PathFileExists(pwszAppPath))
399 SearchPath(NULL, pwszAppPath, NULL, MAX_PATH, pwszAppPath, NULL);
400 }
401
402 BOOL COpenWithList::LoadRecommended(LPCWSTR pwszFilePath)
403 {
404 LPCWSTR pwszExt;
405
406 pwszExt = PathFindExtensionW(pwszFilePath);
407 if (!pwszExt[0])
408 return FALSE;
409
410 /* load programs directly associated from HKCU */
411 LoadRecommendedFromHKCU(pwszExt);
412
413 /* load programs associated from HKCR\Extension */
414 LoadRecommendedFromHKCR(pwszExt);
415
416 return TRUE;
417 }
418
419 BOOL COpenWithList::LoadProgIdList(HKEY hKey, LPCWSTR pwszExt)
420 {
421 HKEY hSubkey, hSubkey2;
422 WCHAR wszProgId[256];
423 DWORD i = 0, cchProgId;
424
425 if (RegOpenKeyExW(hKey, L"OpenWithProgIDs", 0, KEY_READ, &hSubkey) != ERROR_SUCCESS)
426 return FALSE;
427
428 while (TRUE)
429 {
430 /* Enumerate values - value name is ProgId */
431 cchProgId = _countof(wszProgId);
432 if (RegEnumValue(hSubkey, i++, wszProgId, &cchProgId, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
433 break;
434
435 /* If ProgId exists load it */
436 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, wszProgId, 0, KEY_READ, &hSubkey2) == ERROR_SUCCESS)
437 {
438 LoadFromProgIdKey(hSubkey2, pwszExt);
439 RegCloseKey(hSubkey2);
440 }
441 }
442
443 RegCloseKey(hSubkey);
444 return TRUE;
445 }
446
447 HANDLE COpenWithList::OpenMRUList(HKEY hKey)
448 {
449 CREATEMRULISTW Info;
450
451 /* Initialize mru list info */
452 Info.cbSize = sizeof(Info);
453 Info.nMaxItems = 32;
454 Info.dwFlags = MRU_STRING;
455 Info.hKey = hKey;
456 Info.lpszSubKey = L"OpenWithList";
457 Info.lpfnCompare = NULL;
458
459 return CreateMRUListW(&Info);
460 }
461
462 BOOL COpenWithList::LoadMRUList(HKEY hKey)
463 {
464 HANDLE hList;
465 int nItem, nCount, nResult;
466 WCHAR wszAppFilename[MAX_PATH];
467
468 /* Open MRU list */
469 hList = OpenMRUList(hKey);
470 if (!hList)
471 {
472 TRACE("OpenMRUList failed\n");
473 return FALSE;
474 }
475
476 /* Get list count */
477 nCount = EnumMRUListW(hList, -1, NULL, 0);
478
479 for(nItem = 0; nItem < nCount; nItem++)
480 {
481 nResult = EnumMRUListW(hList, nItem, wszAppFilename, _countof(wszAppFilename));
482 if (nResult <= 0)
483 continue;
484
485 /* Insert item */
486 SApp *pApp = Find(wszAppFilename);
487
488 TRACE("Recommended app %ls: %p\n", wszAppFilename, pApp);
489 if (pApp)
490 {
491 pApp->bMRUList = TRUE;
492 SetRecommended(pApp);
493 }
494 }
495
496 /* Free the MRU list */
497 FreeMRUList(hList);
498 return TRUE;
499 }
500
501 BOOL COpenWithList::LoadAppList(HKEY hKey)
502 {
503 WCHAR wszAppFilename[MAX_PATH];
504 HKEY hSubkey;
505 DWORD i = 0, cchAppFilename;
506
507 if (RegOpenKeyExW(hKey, L"OpenWithList", 0, KEY_READ, &hSubkey) != ERROR_SUCCESS)
508 return FALSE;
509
510 while (TRUE)
511 {
512 /* Enum registry keys - each of them is app name */
513 cchAppFilename = _countof(wszAppFilename);
514 if (RegEnumKeyExW(hSubkey, i++, wszAppFilename, &cchAppFilename, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
515 break;
516
517 /* Set application as recommended */
518 SApp *pApp = Find(wszAppFilename);
519
520 TRACE("Recommended app %ls: %p\n", wszAppFilename, pApp);
521 if (pApp)
522 SetRecommended(pApp);
523 }
524
525 RegCloseKey(hSubkey);
526 return TRUE;
527 }
528
529 VOID COpenWithList::LoadFromProgIdKey(HKEY hKey, LPCWSTR pwszExt)
530 {
531 WCHAR wszCmd[MAX_PATH], wszPath[MAX_PATH];
532 DWORD dwSize = 0;
533
534 /* Check if NoOpen value exists */
535 if (RegGetValueW(hKey, NULL, L"NoOpen", RRF_RT_REG_SZ, NULL, NULL, &dwSize) == ERROR_SUCCESS)
536 {
537 /* Display warning dialog */
538 m_bNoOpen = TRUE;
539 }
540
541 /* Check if there is a directly available execute key */
542 dwSize = sizeof(wszCmd);
543 if (RegGetValueW(hKey, L"shell\\open\\command", NULL, RRF_RT_REG_SZ, NULL, (PVOID)wszCmd, &dwSize) == ERROR_SUCCESS)
544 {
545 /* Erase extra arguments */
546 GetPathFromCmd(wszPath, wszCmd);
547
548 /* Add application */
549 SApp *pApp = AddInternal(PathFindFileNameW(wszPath));
550 TRACE("Add app %ls: %p\n", wszPath, pApp);
551
552 if (pApp)
553 {
554 StringCbCopyW(pApp->wszCmd, sizeof(pApp->wszCmd), wszCmd);
555 SetRecommended(pApp);
556 if (!pApp->bMRUList)
557 AddAppToMRUList(pApp, pwszExt);
558 }
559 }
560 }
561
562 VOID COpenWithList::LoadRecommendedFromHKCR(LPCWSTR pwszExt)
563 {
564 HKEY hKey, hSubkey;
565 WCHAR wszBuf[MAX_PATH], wszBuf2[MAX_PATH];
566 DWORD dwSize;
567
568 /* Check if extension exists */
569 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, pwszExt, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
570 {
571 /* Load items from SystemFileAssociations\Ext key */
572 StringCbPrintfW(wszBuf, sizeof(wszBuf), L"SystemFileAssociations\\%s", pwszExt);
573 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, wszBuf, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
574 return;
575 }
576
577 /* Load programs referenced from HKCR\ProgId */
578 dwSize = sizeof(wszBuf);
579 if (RegGetValueW(hKey, NULL, L"", RRF_RT_REG_SZ, NULL, wszBuf, &dwSize) == ERROR_SUCCESS &&
580 RegOpenKeyExW(HKEY_CLASSES_ROOT, wszBuf, 0, KEY_READ, &hSubkey) == ERROR_SUCCESS)
581 {
582 LoadFromProgIdKey(hSubkey, pwszExt);
583 RegCloseKey(hSubkey);
584 }
585 else
586 LoadFromProgIdKey(hKey, pwszExt);
587
588 /* Load items from HKCR\Ext\OpenWithList */
589 LoadAppList(hKey);
590
591 /* Load items from HKCR\Ext\OpenWithProgIDs */
592 if (RegOpenKeyExW(hKey, L"OpenWithProgIDs", 0, KEY_READ, &hSubkey) == ERROR_SUCCESS)
593 {
594 LoadProgIdList(hSubkey, pwszExt);
595 RegCloseKey(hSubkey);
596 }
597
598 /* Load additional items from referenced PerceivedType */
599 dwSize = sizeof(wszBuf);
600 if (RegGetValueW(hKey, NULL, L"PerceivedType", RRF_RT_REG_SZ, NULL, wszBuf, &dwSize) == ERROR_SUCCESS)
601 {
602 RegCloseKey(hKey);
603
604 StringCbPrintfW(wszBuf2, sizeof(wszBuf2), L"SystemFileAssociations\\%s", wszBuf);
605 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, wszBuf2, 0, KEY_READ | KEY_WRITE, &hSubkey) == ERROR_SUCCESS)
606 {
607 /* Load from OpenWithList key */
608 LoadAppList(hSubkey);
609 RegCloseKey(hSubkey);
610 }
611 }
612
613 /* Close the key */
614 RegCloseKey(hKey);
615 }
616
617 VOID COpenWithList::LoadRecommendedFromHKCU(LPCWSTR pwszExt)
618 {
619 WCHAR wszBuf[MAX_PATH];
620 HKEY hKey;
621
622 StringCbPrintfW(wszBuf, sizeof(wszBuf),
623 L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\%s",
624 pwszExt);
625 if (RegOpenKeyExW(HKEY_CURRENT_USER, wszBuf, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
626 {
627 /* Load MRU and ProgId lists */
628 LoadMRUList(hKey);
629 LoadProgIdList(hKey, pwszExt);
630
631 /* Handle "Aplication" value */
632 DWORD cbBuf = sizeof(wszBuf);
633 if (RegGetValueW(hKey, NULL, L"Application", RRF_RT_REG_SZ, NULL, wszBuf, &cbBuf) == ERROR_SUCCESS)
634 {
635 SApp *pApp = Find(wszBuf);
636 if (pApp)
637 SetRecommended(pApp);
638 }
639
640 /* Close the key */
641 RegCloseKey(hKey);
642 }
643 }
644
645 BOOL COpenWithList::AddAppToMRUList(SApp *pApp, LPCWSTR pwszFilename)
646 {
647 WCHAR wszBuf[100];
648 LPCWSTR pwszExt;
649 HKEY hKey;
650 HANDLE hList;
651
652 /* Get file extension */
653 pwszExt = PathFindExtensionW(pwszFilename);
654 if (!pwszExt[0])
655 return FALSE;
656
657 /* Build registry key */
658 if (FAILED(StringCbPrintfW(wszBuf, sizeof(wszBuf),
659 L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\%s",
660 pwszExt)))
661 {
662 ERR("insufficient buffer\n");
663 return FALSE;
664 }
665
666 /* Open base key for this file extension */
667 if (RegCreateKeyExW(HKEY_CURRENT_USER, wszBuf, 0, NULL, 0, KEY_WRITE | KEY_READ, NULL, &hKey, NULL) != ERROR_SUCCESS)
668 return FALSE;
669
670 /* Open MRU list */
671 hList = OpenMRUList(hKey);
672 if (hList)
673 {
674 /* Insert the entry */
675 AddMRUStringW(hList, pApp->wszFilename);
676
677 /* Close MRU list */
678 FreeMRUList(hList);
679 }
680
681 RegCloseKey(hKey);
682 return TRUE;
683 }
684
685 BOOL COpenWithList::SetDefaultHandler(SApp *pApp, LPCWSTR pwszFilename)
686 {
687 HKEY hKey, hSrcKey, hDestKey;
688 DWORD dwDisposition;
689 WCHAR wszBuf[256];
690
691 TRACE("SetDefaultHandler %ls %ls", pApp->wszFilename, pwszFilename);
692
693 /* Extract file extension */
694 LPCWSTR pwszExt = PathFindExtensionW(pwszFilename);
695 if (!pwszExt[0] || !pwszExt[1])
696 return FALSE;
697
698 /* Create file extension key */
699 if (RegCreateKeyExW(HKEY_CLASSES_ROOT, pwszExt, 0, NULL, 0, KEY_READ|KEY_WRITE, NULL, &hKey, &dwDisposition) != ERROR_SUCCESS)
700 {
701 ERR("Cannot open ext key");
702 return FALSE;
703 }
704
705 if (dwDisposition == REG_CREATED_NEW_KEY)
706 {
707 /* A new entry was created create the prog key id */
708 StringCbPrintfW(wszBuf, sizeof(wszBuf), L"%s_auto_file", pwszExt + 1);
709 if (RegSetValueExW(hKey, L"", 0, REG_SZ, (const BYTE*)wszBuf, (wcslen(wszBuf) + 1) * sizeof(WCHAR)) != ERROR_SUCCESS)
710 {
711 RegCloseKey(hKey);
712 ERR("RegSetValueExW failed\n");
713 return FALSE;
714 }
715 }
716 else
717 {
718 /* Entry already exists fetch prog key id */
719 DWORD dwSize = sizeof(wszBuf);
720 if (RegGetValueW(hKey, NULL, L"", RRF_RT_REG_SZ, NULL, wszBuf, &dwSize) != ERROR_SUCCESS)
721 {
722 ERR("RegGetValueW failed: %lu\n", GetLastError());
723 RegCloseKey(hKey);
724 return FALSE;
725 }
726 }
727
728 /* Close file extension key */
729 RegCloseKey(hKey);
730
731 /* Create prog id key */
732 if (RegCreateKeyExW(HKEY_CLASSES_ROOT, wszBuf, 0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL) != ERROR_SUCCESS)
733 {
734 ERR("RegCreateKeyExW failed\n");
735 return FALSE;
736 }
737
738 /* Check if there already verbs existing for that app */
739 StringCbPrintfW(wszBuf, sizeof(wszBuf), L"Applications\\%s\\shell", pApp->wszFilename);
740 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, wszBuf, 0, KEY_READ, &hSrcKey) != ERROR_SUCCESS)
741 {
742 ERR("RegOpenKeyExW %ls failed\n", wszBuf);
743 RegCloseKey(hKey);
744 return FALSE;
745 }
746
747 /* Open destination key */
748 if (RegCreateKeyExW(hKey, L"shell", 0, NULL, 0, KEY_WRITE, NULL, &hDestKey, NULL) != ERROR_SUCCESS)
749 {
750 ERR("RegCreateKeyExW failed\n");
751 RegCloseKey(hSrcKey);
752 RegCloseKey(hKey);
753 return FALSE;
754 }
755
756 /* Copy static verbs from Classes\Applications key */
757 LONG Result = RegCopyTreeW(hSrcKey, NULL, hDestKey);
758 RegCloseKey(hDestKey);
759 RegCloseKey(hSrcKey);
760 RegCloseKey(hKey);
761
762 if (Result != ERROR_SUCCESS)
763 {
764 ERR("RegCopyTreeW failed\n");
765 return FALSE;
766 }
767
768 return TRUE;
769 }
770
771 class COpenWithDialog
772 {
773 public:
774 COpenWithDialog(const OPENASINFO *pInfo, COpenWithList *pAppList);
775 ~COpenWithDialog();
776 static INT_PTR CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
777
778 private:
779 VOID Init(HWND hwnd);
780 VOID AddApp(COpenWithList::SApp *pApp, BOOL bSelected);
781 VOID Browse();
782 VOID Accept();
783 COpenWithList::SApp *GetCurrentApp();
784
785 const OPENASINFO *m_pInfo;
786 COpenWithList *m_pAppList;
787 BOOL m_bListAllocated;
788 HWND m_hDialog, m_hTreeView;
789 HTREEITEM m_hRecommend;
790 HTREEITEM m_hOther;
791 HIMAGELIST m_hImgList;
792 };
793
794 COpenWithDialog::COpenWithDialog(const OPENASINFO *pInfo, COpenWithList *pAppList = NULL):
795 m_pInfo(pInfo), m_pAppList(pAppList), m_hImgList(NULL)
796 {
797 if (!m_pAppList)
798 {
799 m_pAppList = new COpenWithList;
800 m_bListAllocated = TRUE;
801 }
802 else
803 m_bListAllocated = FALSE;
804 }
805
806 COpenWithDialog::~COpenWithDialog()
807 {
808 if (m_bListAllocated && m_pAppList)
809 delete m_pAppList;
810 if (m_hImgList)
811 ImageList_Destroy(m_hImgList);
812 }
813
814 VOID COpenWithDialog::AddApp(COpenWithList::SApp *pApp, BOOL bSelected)
815 {
816 LPCWSTR pwszName = m_pAppList->GetName(pApp);
817 HICON hIcon = m_pAppList->GetIcon(pApp);
818
819 TRACE("AddApp Cmd %ls Name %ls\n", pApp->wszCmd, pwszName);
820
821 /* Add item to the list */
822 TVINSERTSTRUCT tvins;
823
824 if (pApp->bRecommended)
825 tvins.hParent = tvins.hInsertAfter = m_hRecommend;
826 else
827 tvins.hParent = tvins.hInsertAfter = m_hOther;
828
829 tvins.item.mask = TVIF_TEXT|TVIF_PARAM;
830 tvins.item.pszText = (LPWSTR)pwszName;
831 tvins.item.lParam = (LPARAM)pApp;
832
833 if (hIcon)
834 {
835 tvins.item.mask |= TVIF_IMAGE | TVIF_SELECTEDIMAGE;
836 tvins.item.iImage = tvins.item.iSelectedImage = ImageList_AddIcon(m_hImgList, hIcon);
837 }
838
839 HTREEITEM hItem = TreeView_InsertItem(m_hTreeView, &tvins);
840
841 if (bSelected)
842 TreeView_SelectItem(m_hTreeView, hItem);
843 }
844
845 VOID COpenWithDialog::Browse()
846 {
847 WCHAR wszTitle[64];
848 WCHAR wszFilter[256];
849 WCHAR wszPath[MAX_PATH];
850 OPENFILENAMEW ofn;
851
852 /* Initialize OPENFILENAMEW structure */
853 ZeroMemory(&ofn, sizeof(OPENFILENAMEW));
854 ofn.lStructSize = sizeof(OPENFILENAMEW);
855 ofn.hInstance = shell32_hInstance;
856 ofn.hwndOwner = m_hDialog;
857 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
858 ofn.nMaxFile = (sizeof(wszPath) / sizeof(WCHAR));
859 ofn.lpstrFile = wszPath;
860
861 /* Init title */
862 if (LoadStringW(shell32_hInstance, IDS_OPEN_WITH, wszTitle, sizeof(wszTitle) / sizeof(WCHAR)))
863 {
864 ofn.lpstrTitle = wszTitle;
865 ofn.nMaxFileTitle = wcslen(wszTitle);
866 }
867
868 /* Init the filter string */
869 if (LoadStringW(shell32_hInstance, IDS_OPEN_WITH_FILTER, wszFilter, sizeof(wszFilter) / sizeof(WCHAR)))
870 ofn.lpstrFilter = wszFilter;
871 ZeroMemory(wszPath, sizeof(wszPath));
872
873 /* Create OpenFile dialog */
874 if (!GetOpenFileNameW(&ofn))
875 return;
876
877 /* Setup context for insert proc */
878 COpenWithList::SApp *pApp = m_pAppList->Add(wszPath);
879 AddApp(pApp, TRUE);
880 }
881
882 COpenWithList::SApp *COpenWithDialog::GetCurrentApp()
883 {
884 TVITEM tvi;
885 tvi.hItem = TreeView_GetSelection(m_hTreeView);
886 if (!tvi.hItem)
887 return NULL;
888
889 tvi.mask = TVIF_PARAM;
890 if (!TreeView_GetItem(m_hTreeView, &tvi))
891 return NULL;
892
893 return (COpenWithList::SApp*)tvi.lParam;
894 }
895
896 VOID COpenWithDialog::Init(HWND hwnd)
897 {
898 TRACE("COpenWithDialog::Init hwnd %p\n", hwnd);
899
900 m_hDialog = hwnd;
901 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)this);
902
903 /* Handle register checkbox */
904 HWND hRegisterCheckbox = GetDlgItem(hwnd, 14003);
905 if (!(m_pInfo->oaifInFlags & OAIF_ALLOW_REGISTRATION))
906 EnableWindow(hRegisterCheckbox, FALSE);
907 if (m_pInfo->oaifInFlags & OAIF_FORCE_REGISTRATION)
908 SendMessage(hRegisterCheckbox, BM_SETCHECK, BST_CHECKED, 0);
909 if (m_pInfo->oaifInFlags & OAIF_HIDE_REGISTRATION)
910 ShowWindow(hRegisterCheckbox, SW_HIDE);
911
912 if (m_pInfo->pcszFile)
913 {
914 WCHAR wszBuf[MAX_PATH];
915 UINT cchBuf;
916
917 /* Add filename to label */
918 cchBuf = GetDlgItemTextW(hwnd, 14001, wszBuf, _countof(wszBuf));
919 StringCchCopyW(wszBuf + cchBuf, _countof(wszBuf) - cchBuf, PathFindFileNameW(m_pInfo->pcszFile));
920 SetDlgItemTextW(hwnd, 14001, wszBuf);
921
922 /* Load applications from registry */
923 m_pAppList->Load();
924 m_pAppList->LoadRecommended(m_pInfo->pcszFile);
925
926 /* Init treeview */
927 m_hTreeView = GetDlgItem(hwnd, 14002);
928 m_hImgList = ImageList_Create(16, 16, 0, m_pAppList->GetCount() + 1, m_pAppList->GetCount() + 1);
929 (void)TreeView_SetImageList(m_hTreeView, m_hImgList, TVSIL_NORMAL);
930
931 /* If there are some recommendations add parent nodes: Recommended and Others */
932 UINT cRecommended = m_pAppList->GetRecommendedCount();
933 if (cRecommended > 0)
934 {
935 TVINSERTSTRUCT tvins;
936 HICON hFolderIcon;
937
938 tvins.hParent = tvins.hInsertAfter = TVI_ROOT;
939 tvins.item.mask = TVIF_TEXT|TVIF_STATE|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
940 tvins.item.pszText = (LPWSTR)wszBuf;
941 tvins.item.state = tvins.item.stateMask = TVIS_EXPANDED;
942 hFolderIcon = (HICON)LoadImage(shell32_hInstance, MAKEINTRESOURCE(IDI_SHELL_FOLDER), IMAGE_ICON, 0, 0, 0);
943 tvins.item.iImage = tvins.item.iSelectedImage = ImageList_AddIcon(m_hImgList, hFolderIcon);
944
945 LoadStringW(shell32_hInstance, IDS_OPEN_WITH_RECOMMENDED, wszBuf, _countof(wszBuf));
946 m_hRecommend = TreeView_InsertItem(m_hTreeView, &tvins);
947
948 LoadStringW(shell32_hInstance, IDS_OPEN_WITH_OTHER, wszBuf, _countof(wszBuf));
949 m_hOther = TreeView_InsertItem(m_hTreeView, &tvins);
950 }
951 else
952 m_hRecommend = m_hOther = TVI_ROOT;
953
954 /* Add all applications */
955 BOOL bNoAppSelected = TRUE;
956 COpenWithList::SApp *pAppList = m_pAppList->GetList();
957 for (UINT i = 0; i < m_pAppList->GetCount(); ++i)
958 {
959 if (!COpenWithList::IsHidden(&pAppList[i]))
960 {
961 if (bNoAppSelected && (pAppList[i].bRecommended || !cRecommended))
962 {
963 AddApp(&pAppList[i], TRUE);
964 bNoAppSelected = FALSE;
965 }
966 else
967 AddApp(&pAppList[i], FALSE);
968 }
969 }
970 }
971 }
972
973 VOID COpenWithDialog::Accept()
974 {
975 COpenWithList::SApp *pApp = GetCurrentApp();
976 if (pApp)
977 {
978 /* Set programm as default handler */
979 if (SendDlgItemMessage(m_hDialog, 14003, BM_GETCHECK, 0, 0) == BST_CHECKED)
980 m_pAppList->SetDefaultHandler(pApp, m_pInfo->pcszFile);
981
982 /* Execute program */
983 if (m_pInfo->oaifInFlags & OAIF_EXEC)
984 m_pAppList->Execute(pApp, m_pInfo->pcszFile);
985
986 DestroyWindow(m_hDialog);
987 }
988 }
989
990 INT_PTR CALLBACK COpenWithDialog::DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
991 {
992 COpenWithDialog *pThis = (COpenWithDialog*)GetWindowLongPtr(hwndDlg, DWLP_USER);
993
994 switch(uMsg)
995 {
996 case WM_INITDIALOG:
997 {
998 COpenWithDialog *pThis = (COpenWithDialog*)lParam;
999
1000 pThis->Init(hwndDlg);
1001 return TRUE;
1002 }
1003 case WM_COMMAND:
1004 switch(LOWORD(wParam))
1005 {
1006 case 14004: /* browse */
1007 {
1008 pThis->Browse();
1009 return TRUE;
1010 }
1011 case IDOK: /* ok */
1012 {
1013 pThis->Accept();
1014 return TRUE;
1015 }
1016 case IDCANCEL: /* cancel */
1017 DestroyWindow(hwndDlg);
1018 return TRUE;
1019 default:
1020 break;
1021 }
1022 break;
1023 case WM_NOTIFY:
1024 switch (((LPNMHDR)lParam)->code)
1025 {
1026 case TVN_SELCHANGED:
1027 EnableWindow(GetDlgItem(hwndDlg, IDOK), pThis->GetCurrentApp() ? TRUE : FALSE);
1028 break;
1029 case NM_DBLCLK:
1030 case NM_RETURN:
1031 pThis->Accept();
1032 break;
1033 }
1034 break;
1035 case WM_CLOSE:
1036 DestroyWindow(hwndDlg);
1037 return TRUE;
1038 default:
1039 break;
1040 }
1041 return FALSE;
1042 }
1043
1044 COpenWithMenu::COpenWithMenu()
1045 {
1046 m_idCmdFirst = 0;
1047 m_idCmdLast = 0;
1048 m_pAppList = new COpenWithList;
1049 }
1050
1051 COpenWithMenu::~COpenWithMenu()
1052 {
1053 TRACE("Destroying COpenWithMenu(%p)\n", this);
1054
1055 if (m_hSubMenu)
1056 {
1057 INT Count, Index;
1058 MENUITEMINFOW mii;
1059
1060 /* get item count */
1061 Count = GetMenuItemCount(m_hSubMenu);
1062 if (Count == -1)
1063 return;
1064
1065 /* setup menuitem info */
1066 ZeroMemory(&mii, sizeof(mii));
1067 mii.cbSize = sizeof(mii);
1068 mii.fMask = MIIM_DATA | MIIM_FTYPE | MIIM_CHECKMARKS;
1069
1070 for(Index = 0; Index < Count; Index++)
1071 {
1072 if (GetMenuItemInfoW(m_hSubMenu, Index, TRUE, &mii))
1073 {
1074 if (mii.hbmpChecked)
1075 DeleteObject(mii.hbmpChecked);
1076 }
1077 }
1078 }
1079
1080 if (m_pAppList)
1081 delete m_pAppList;
1082 }
1083
1084 HBITMAP COpenWithMenu::IconToBitmap(HICON hIcon)
1085 {
1086 HDC hdc, hdcScr;
1087 HBITMAP hbm, hbmOld;
1088 RECT rc;
1089
1090 hdcScr = GetDC(NULL);
1091 hdc = CreateCompatibleDC(hdcScr);
1092 SetRect(&rc, 0, 0, GetSystemMetrics(SM_CXMENUCHECK), GetSystemMetrics(SM_CYMENUCHECK));
1093 hbm = CreateCompatibleBitmap(hdcScr, rc.right, rc.bottom);
1094 ReleaseDC(NULL, hdcScr);
1095
1096 hbmOld = (HBITMAP)SelectObject(hdc, hbm);
1097 FillRect(hdc, &rc, (HBRUSH)(COLOR_MENU + 1));
1098 if (!DrawIconEx(hdc, 0, 0, hIcon, rc.right, rc.bottom, 0, NULL, DI_NORMAL))
1099 ERR("DrawIcon failed: %x\n", GetLastError());
1100 SelectObject(hdc, hbmOld);
1101
1102 DeleteDC(hdc);
1103
1104 return hbm;
1105 }
1106
1107 VOID COpenWithMenu::AddChooseProgramItem()
1108 {
1109 MENUITEMINFOW mii;
1110 WCHAR wszBuf[128];
1111
1112 ZeroMemory(&mii, sizeof(mii));
1113 mii.cbSize = sizeof(mii);
1114 mii.fMask = MIIM_TYPE | MIIM_ID;
1115 mii.fType = MFT_SEPARATOR;
1116 mii.wID = -1;
1117 InsertMenuItemW(m_hSubMenu, -1, TRUE, &mii);
1118
1119 if (!LoadStringW(shell32_hInstance, IDS_OPEN_WITH_CHOOSE, wszBuf, _countof(wszBuf)))
1120 {
1121 ERR("Failed to load string\n");
1122 return;
1123 }
1124
1125 mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
1126 mii.fType = MFT_STRING;
1127 mii.fState = MFS_ENABLED;
1128 mii.wID = m_idCmdLast;
1129 mii.dwTypeData = (LPWSTR)wszBuf;
1130 mii.cch = wcslen(wszBuf);
1131
1132 InsertMenuItemW(m_hSubMenu, -1, TRUE, &mii);
1133 }
1134
1135 VOID COpenWithMenu::AddApp(PVOID pApp)
1136 {
1137 MENUITEMINFOW mii;
1138 LPCWSTR pwszName = m_pAppList->GetName((COpenWithList::SApp*)pApp);
1139
1140 ZeroMemory(&mii, sizeof(mii));
1141 mii.cbSize = sizeof(mii);
1142 mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE | MIIM_DATA;
1143 mii.fType = MFT_STRING;
1144 mii.fState = MFS_ENABLED;
1145 mii.wID = m_idCmdLast;
1146 mii.dwTypeData = (LPWSTR)pwszName;
1147 mii.cch = wcslen(mii.dwTypeData);
1148 mii.dwItemData = (ULONG_PTR)pApp;
1149
1150 HICON hIcon = m_pAppList->GetIcon((COpenWithList::SApp*)pApp);
1151 if (hIcon)
1152 {
1153 mii.fMask |= MIIM_CHECKMARKS;
1154 mii.hbmpChecked = mii.hbmpUnchecked = IconToBitmap(hIcon);
1155 }
1156
1157 if (InsertMenuItemW(m_hSubMenu, -1, TRUE, &mii))
1158 m_idCmdLast++;
1159 }
1160
1161 HRESULT WINAPI COpenWithMenu::QueryContextMenu(
1162 HMENU hMenu,
1163 UINT indexMenu,
1164 UINT idCmdFirst,
1165 UINT idCmdLast,
1166 UINT uFlags)
1167 {
1168 TRACE("hMenu %p indexMenu %u idFirst %u idLast %u uFlags %u\n", hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
1169
1170 INT DefaultPos = GetMenuDefaultItem(hMenu, TRUE, 0);
1171
1172 WCHAR wszName[100];
1173 UINT NameId = (DefaultPos == -1 ? IDS_OPEN : IDS_OPEN_WITH);
1174 if (!LoadStringW(shell32_hInstance, NameId, wszName, _countof(wszName)))
1175 {
1176 ERR("Failed to load string\n");
1177 return E_FAIL;
1178 }
1179
1180 /* Init first cmd id and submenu */
1181 m_idCmdFirst = m_idCmdLast = idCmdFirst;
1182 m_hSubMenu = NULL;
1183
1184 /* If we are going to be default item, we shouldn't be submenu */
1185 if (DefaultPos != -1)
1186 {
1187 /* Load applications list */
1188 m_pAppList->Load();
1189 m_pAppList->LoadRecommended(m_wszPath);
1190
1191 /* Create submenu only if there is more than one application and menu has a default item */
1192 if (m_pAppList->GetRecommendedCount() > 1)
1193 {
1194 m_hSubMenu = CreatePopupMenu();
1195
1196 for(UINT i = 0; i < m_pAppList->GetCount(); ++i)
1197 {
1198 COpenWithList::SApp *pApp = m_pAppList->GetList() + i;
1199 if (pApp->bRecommended)
1200 AddApp(pApp);
1201 }
1202
1203 AddChooseProgramItem();
1204 }
1205 }
1206
1207 /* Insert menu item */
1208 MENUITEMINFOW mii;
1209 ZeroMemory(&mii, sizeof(mii));
1210 mii.cbSize = sizeof(mii);
1211 mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
1212 if (m_hSubMenu)
1213 {
1214 mii.fMask |= MIIM_SUBMENU;
1215 mii.hSubMenu = m_hSubMenu;
1216 mii.wID = -1;
1217 }
1218 else
1219 mii.wID = m_idCmdLast;
1220
1221 mii.fType = MFT_STRING;
1222 mii.dwTypeData = (LPWSTR)wszName;
1223 mii.cch = wcslen(wszName);
1224
1225 mii.fState = MFS_ENABLED;
1226 if (DefaultPos == -1)
1227 mii.fState |= MFS_DEFAULT;
1228
1229 if (!InsertMenuItemW(hMenu, DefaultPos + 1, TRUE, &mii))
1230 return E_FAIL;
1231
1232 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, m_idCmdLast - m_idCmdFirst + 1);
1233 }
1234
1235 HRESULT WINAPI
1236 COpenWithMenu::InvokeCommand(LPCMINVOKECOMMANDINFO lpici)
1237 {
1238 HRESULT hr = E_FAIL;
1239
1240 TRACE("This %p idFirst %u idLast %u idCmd %u\n", this, m_idCmdFirst, m_idCmdLast, m_idCmdFirst + LOWORD(lpici->lpVerb));
1241
1242 if (HIWORD(lpici->lpVerb) == 0 && m_idCmdFirst + LOWORD(lpici->lpVerb) <= m_idCmdLast)
1243 {
1244 if (m_idCmdFirst + LOWORD(lpici->lpVerb) == m_idCmdLast)
1245 {
1246 OPENASINFO info;
1247 LPCWSTR pwszExt = PathFindExtensionW(m_wszPath);
1248
1249 info.pcszFile = m_wszPath;
1250 info.oaifInFlags = OAIF_EXEC;
1251 if (pwszExt[0])
1252 info.oaifInFlags |= OAIF_REGISTER_EXT | OAIF_ALLOW_REGISTRATION;
1253 info.pcszClass = NULL;
1254 hr = SHOpenWithDialog(lpici->hwnd, &info);
1255 }
1256 else
1257 {
1258 /* retrieve menu item info */
1259 MENUITEMINFOW mii;
1260 ZeroMemory(&mii, sizeof(mii));
1261 mii.cbSize = sizeof(mii);
1262 mii.fMask = MIIM_DATA | MIIM_FTYPE;
1263
1264 if (GetMenuItemInfoW(m_hSubMenu, LOWORD(lpici->lpVerb), TRUE, &mii) && mii.dwItemData)
1265 {
1266 /* launch item with specified app */
1267 COpenWithList::SApp *pApp = (COpenWithList::SApp*)mii.dwItemData;
1268 COpenWithList::Execute(pApp, m_wszPath);
1269 hr = S_OK;
1270 }
1271 }
1272 }
1273
1274 return hr;
1275 }
1276
1277 HRESULT WINAPI
1278 COpenWithMenu::GetCommandString(UINT_PTR idCmd, UINT uType,
1279 UINT* pwReserved, LPSTR pszName, UINT cchMax )
1280 {
1281 FIXME("%p %lu %u %p %p %u\n", this,
1282 idCmd, uType, pwReserved, pszName, cchMax );
1283
1284 return E_NOTIMPL;
1285 }
1286
1287 HRESULT WINAPI COpenWithMenu::HandleMenuMsg(
1288 UINT uMsg,
1289 WPARAM wParam,
1290 LPARAM lParam)
1291 {
1292 TRACE("This %p uMsg %x\n", this, uMsg);
1293
1294 return E_NOTIMPL;
1295 }
1296
1297 HRESULT WINAPI
1298 COpenWithMenu::Initialize(LPCITEMIDLIST pidlFolder,
1299 IDataObject *pdtobj,
1300 HKEY hkeyProgID)
1301 {
1302 STGMEDIUM medium;
1303 FORMATETC fmt;
1304 HRESULT hr;
1305 LPIDA pida;
1306 LPCITEMIDLIST pidlFolder2;
1307 LPCITEMIDLIST pidlChild;
1308 LPCITEMIDLIST pidl;
1309 LPCWSTR pwszExt;
1310
1311 TRACE("This %p\n", this);
1312
1313 if (pdtobj == NULL)
1314 return E_INVALIDARG;
1315
1316 fmt.cfFormat = RegisterClipboardFormatW(CFSTR_SHELLIDLIST);
1317 fmt.ptd = NULL;
1318 fmt.dwAspect = DVASPECT_CONTENT;
1319 fmt.lindex = -1;
1320 fmt.tymed = TYMED_HGLOBAL;
1321
1322 hr = pdtobj->GetData(&fmt, &medium);
1323
1324 if (FAILED(hr))
1325 {
1326 ERR("pdtobj->GetData failed with 0x%x\n", hr);
1327 return hr;
1328 }
1329
1330 pida = (LPIDA)GlobalLock(medium.hGlobal);
1331 ASSERT(pida->cidl >= 1);
1332
1333 pidlFolder2 = (LPCITEMIDLIST) ((LPBYTE)pida + pida->aoffset[0]);
1334 pidlChild = (LPCITEMIDLIST) ((LPBYTE)pida + pida->aoffset[1]);
1335
1336 pidl = ILCombine(pidlFolder2, pidlChild);
1337
1338 GlobalUnlock(medium.hGlobal);
1339 GlobalFree(medium.hGlobal);
1340
1341 if (!pidl)
1342 {
1343 ERR("no mem\n");
1344 return E_OUTOFMEMORY;
1345 }
1346 if (!_ILIsValue(pidlChild))
1347 {
1348 TRACE("pidl is not a file\n");
1349 SHFree((void*)pidl);
1350 return E_FAIL;
1351 }
1352
1353 if (!SHGetPathFromIDListW(pidl, m_wszPath))
1354 {
1355 SHFree((void*)pidl);
1356 ERR("SHGetPathFromIDListW failed\n");
1357 return E_FAIL;
1358 }
1359
1360 SHFree((void*)pidl);
1361 TRACE("szPath %s\n", debugstr_w(m_wszPath));
1362
1363 pwszExt = PathFindExtensionW(m_wszPath);
1364 if (PathIsExeW(pwszExt) || !_wcsicmp(pwszExt, L".lnk"))
1365 {
1366 TRACE("file is a executable or shortcut\n");
1367 return E_FAIL;
1368 }
1369
1370 return S_OK;
1371 }
1372
1373 HRESULT WINAPI
1374 SHOpenWithDialog(HWND hwndParent, const OPENASINFO *poainfo)
1375 {
1376 MSG msg;
1377 HWND hwnd;
1378 COpenWithDialog *pDialog;
1379
1380 TRACE("SHOpenWithDialog hwndParent %p poainfo %p\n", hwndParent, poainfo);
1381
1382 InitCommonControls();
1383
1384 if (poainfo->pcszClass == NULL && poainfo->pcszFile == NULL)
1385 return E_FAIL;
1386
1387 pDialog = new COpenWithDialog(poainfo);
1388 if (!pDialog)
1389 return E_OUTOFMEMORY;
1390
1391 hwnd = CreateDialogParam(shell32_hInstance, MAKEINTRESOURCE(IDD_OPEN_WITH), hwndParent, COpenWithDialog::DialogProc, (LPARAM)pDialog);
1392 if (hwnd == NULL)
1393 {
1394 ERR("Failed to create dialog\n");
1395 return E_FAIL;
1396 }
1397
1398 ShowWindow(hwnd, SW_SHOWNORMAL);
1399
1400 while (GetMessage(&msg, NULL, 0, 0) && IsWindow(hwnd))
1401 {
1402 if (!IsDialogMessage(hwnd, &msg))
1403 {
1404 TranslateMessage(&msg);
1405 DispatchMessage(&msg);
1406 }
1407 }
1408
1409 delete pDialog;
1410
1411 return S_OK;
1412 }