2 * Helper program to build unix menu entries
4 * Copyright 1997 Marcus Meissner
5 * Copyright 1998 Juergen Schmied
6 * Copyright 2003 Mike McCormack for CodeWeavers
7 * Copyright 2004 Dmitry Timoshkov
8 * Copyright 2005 Bill Medland
9 * Copyright 2008 Damjan Jovanovic
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * This program is used to replicate the Windows desktop and start menu
27 * into the native desktop's copies. Desktop entries are merged directly
28 * into the native desktop. The Windows Start Menu corresponds to a Wine
29 * entry within the native "start" menu and replicates the whole tree
30 * structure of the Windows Start Menu. Currently it does not differentiate
31 * between the user's desktop/start menu and the "All Users" copies.
33 * This program will read a Windows shortcut file using the IShellLink
34 * interface, then create a KDE/GNOME menu entry for the shortcut.
36 * winemenubuilder [ -w ] <shortcut.lnk>
38 * If the -w parameter is passed, and the shortcut cannot be created,
39 * this program will wait for the parent process to finish and then try
40 * again. This covers the case when a ShortCut is created before the
41 * executable containing its icon.
44 * Handle data lnk files. There is no icon in the file; the icon is in
45 * the handler for the file type (or pointed to by the lnk file). Also it
46 * might be better to use a native handler (e.g. a native acroread for pdf
48 * Differentiate between the user's entries and the "All Users" entries.
49 * If it is possible to add the desktop files to the native system's
50 * shared location for an "All Users" entry then do so. As a suggestion the
51 * shared menu Wine base could be writable to the wine group, or a wineadm
53 * Clean up fd.o menu icons and .directory files when the menu is deleted
55 * Associate applications under HKCR\Applications to open any MIME type
56 * (by associating with application/octet-stream, or how?).
57 * Clean up fd.o MIME types when they are deleted in Windows, their icons
58 * too. Very hard - once we associate them with fd.o, we can't tell whether
59 * they are ours or not, and the extension <-> MIME type mapping isn't
61 * Wine's HKCR is broken - it doesn't merge HKCU\Software\Classes, so apps
62 * that write associations there won't associate (#17019).
66 #include "wine/port.h"
81 #define NONAMELESSUNION
94 #include "wine/unicode.h"
95 #include "wine/debug.h"
96 #include "wine/library.h"
97 #include "wine/list.h"
98 #include "wine/rbtree.h"
100 WINE_DEFAULT_DEBUG_CHANNEL(menubuilder);
102 #define in_desktop_dir(csidl) ((csidl)==CSIDL_DESKTOPDIRECTORY || \
103 (csidl)==CSIDL_COMMON_DESKTOPDIRECTORY)
104 #define in_startmenu(csidl) ((csidl)==CSIDL_STARTMENU || \
105 (csidl)==CSIDL_COMMON_STARTMENU)
107 /* link file formats */
109 #include "pshpack1.h"
128 GRPICONDIRENTRY idEntries[1];
167 #define NE_RSCTYPE_ICON 0x8003
168 #define NE_RSCTYPE_GROUP_ICON 0x800e
186 struct rb_string_entry
189 struct wine_rb_entry entry;
192 DEFINE_GUID(CLSID_WICIcnsEncoder, 0x312fb6f1,0xb767,0x409d,0x8a,0x6d,0x0f,0xc1,0x54,0xd4,0xf0,0x5c);
194 static char *xdg_config_dir;
195 static char *xdg_data_dir;
196 static char *xdg_desktop_dir;
198 static WCHAR* assoc_query(ASSOCSTR assocStr, LPCWSTR name, LPCWSTR extra);
199 static HRESULT open_icon(LPCWSTR filename, int index, BOOL bWait, IStream **ppStream);
201 /* Utility routines */
202 static unsigned short crc16(const char* string)
204 unsigned short crc = 0;
207 for (i = 0; string[i] != 0; i++)
210 for (j = 0; j < 8; c >>= 1, j++)
212 xor_poly = (c ^ crc) & 1;
221 static char *strdupA( const char *str )
225 if (!str) return NULL;
226 if ((ret = HeapAlloc( GetProcessHeap(), 0, strlen(str) + 1 ))) strcpy( ret, str );
230 static char* heap_printf(const char *format, ...)
239 buffer = HeapAlloc(GetProcessHeap(), 0, size);
242 va_start(args, format);
243 n = vsnprintf(buffer, size, format, args);
251 HeapFree(GetProcessHeap(), 0, buffer);
254 if (!buffer) return NULL;
255 ret = HeapReAlloc(GetProcessHeap(), 0, buffer, strlen(buffer) + 1 );
256 if (!ret) ret = buffer;
260 static int winemenubuilder_rb_string_compare(const void *key, const struct wine_rb_entry *entry)
262 const struct rb_string_entry *t = WINE_RB_ENTRY_VALUE(entry, const struct rb_string_entry, entry);
264 return strcmp((char*)key, t->string);
267 static void *winemenubuilder_rb_alloc(size_t size)
269 return HeapAlloc(GetProcessHeap(), 0, size);
272 static void *winemenubuilder_rb_realloc(void *ptr, size_t size)
274 return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
277 static void winemenubuilder_rb_free(void *ptr)
279 HeapFree(GetProcessHeap(), 0, ptr);
282 static void winemenubuilder_rb_destroy(struct wine_rb_entry *entry, void *context)
284 struct rb_string_entry *t = WINE_RB_ENTRY_VALUE(entry, struct rb_string_entry, entry);
285 HeapFree(GetProcessHeap(), 0, t->string);
286 HeapFree(GetProcessHeap(), 0, t);
289 static const struct wine_rb_functions winemenubuilder_rb_functions =
291 winemenubuilder_rb_alloc,
292 winemenubuilder_rb_realloc,
293 winemenubuilder_rb_free,
294 winemenubuilder_rb_string_compare,
297 static void write_xml_text(FILE *file, const char *text)
300 for (i = 0; text[i]; i++)
303 fputs("&", file);
304 else if (text[i] == '<')
306 else if (text[i] == '>')
308 else if (text[i] == '\'')
309 fputs("'", file);
310 else if (text[i] == '"')
311 fputs(""", file);
313 fputc(text[i], file);
317 static BOOL create_directories(char *directory)
322 for (i = 0; directory[i]; i++)
324 if (i > 0 && directory[i] == '/')
327 mkdir(directory, 0777);
331 if (mkdir(directory, 0777) && errno != EEXIST)
337 static char* wchars_to_utf8_chars(LPCWSTR string)
340 INT size = WideCharToMultiByte(CP_UTF8, 0, string, -1, NULL, 0, NULL, NULL);
341 ret = HeapAlloc(GetProcessHeap(), 0, size);
343 WideCharToMultiByte(CP_UTF8, 0, string, -1, ret, size, NULL, NULL);
347 static char* wchars_to_unix_chars(LPCWSTR string)
350 INT size = WideCharToMultiByte(CP_UNIXCP, 0, string, -1, NULL, 0, NULL, NULL);
351 ret = HeapAlloc(GetProcessHeap(), 0, size);
353 WideCharToMultiByte(CP_UNIXCP, 0, string, -1, ret, size, NULL, NULL);
357 static WCHAR* utf8_chars_to_wchars(LPCSTR string)
360 INT size = MultiByteToWideChar(CP_UTF8, 0, string, -1, NULL, 0);
361 ret = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
363 MultiByteToWideChar(CP_UTF8, 0, string, -1, ret, size);
367 /* Icon extraction routines
369 * FIXME: should use PrivateExtractIcons and friends
370 * FIXME: should not use stdio
373 static HRESULT convert_to_native_icon(IStream *icoFile, int *indices, int numIndices,
374 const CLSID *outputFormat, const char *outputFileName, LPCWSTR commentW)
376 WCHAR *dosOutputFileName = NULL;
377 IWICImagingFactory *factory = NULL;
378 IWICBitmapDecoder *decoder = NULL;
379 IWICBitmapEncoder *encoder = NULL;
380 IWICBitmapScaler *scaler = NULL;
381 IStream *outputFile = NULL;
385 dosOutputFileName = wine_get_dos_file_name(outputFileName);
386 if (dosOutputFileName == NULL)
388 WINE_ERR("error converting %s to DOS file name\n", outputFileName);
391 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
392 &IID_IWICImagingFactory, (void**)&factory);
395 WINE_ERR("error 0x%08X creating IWICImagingFactory\n", hr);
398 hr = IWICImagingFactory_CreateDecoderFromStream(factory, icoFile, NULL,
399 WICDecodeMetadataCacheOnDemand, &decoder);
402 WINE_ERR("error 0x%08X creating IWICBitmapDecoder\n", hr);
405 if (IsEqualCLSID(outputFormat,&CLSID_WICIcnsEncoder))
407 hr = IWICImagingFactory_CreateBitmapScaler(factory, &scaler);
410 WINE_WARN("error 0x%08X creating IWICBitmapScaler\n", hr);
413 hr = CoCreateInstance(outputFormat, NULL, CLSCTX_INPROC_SERVER,
414 &IID_IWICBitmapEncoder, (void**)&encoder);
417 WINE_ERR("error 0x%08X creating bitmap encoder\n", hr);
420 hr = SHCreateStreamOnFileW(dosOutputFileName, STGM_CREATE | STGM_WRITE, &outputFile);
423 WINE_ERR("error 0x%08X creating output file %s\n", hr, wine_dbgstr_w(dosOutputFileName));
426 hr = IWICBitmapEncoder_Initialize(encoder, outputFile, GENERIC_WRITE);
429 WINE_ERR("error 0x%08X initializing encoder\n", hr);
433 for (i = 0; i < numIndices; i++)
435 IWICBitmapFrameDecode *sourceFrame = NULL;
436 IWICBitmapSource *sourceBitmap = NULL;
437 IWICBitmapFrameEncode *dstFrame = NULL;
438 IPropertyBag2 *options = NULL;
441 hr = IWICBitmapDecoder_GetFrame(decoder, indices[i], &sourceFrame);
444 WINE_ERR("error 0x%08X getting frame %d\n", hr, indices[i]);
447 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)sourceFrame, &sourceBitmap);
450 WINE_ERR("error 0x%08X converting bitmap to 32bppBGRA\n", hr);
455 IWICBitmapSource_GetSize(sourceBitmap, &width, &height);
456 if (width == 64) /* Classic Mode */
458 hr = IWICBitmapScaler_Initialize( scaler, sourceBitmap, 128, 128,
459 WICBitmapInterpolationModeNearestNeighbor);
461 WINE_ERR("error 0x%08X scaling bitmap\n", hr);
464 IWICBitmapSource_Release(sourceBitmap);
465 IWICBitmapScaler_QueryInterface(scaler, &IID_IWICBitmapSource, (LPVOID)&sourceBitmap);
469 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &dstFrame, &options);
472 WINE_ERR("error 0x%08X creating encoder frame\n", hr);
475 hr = IWICBitmapFrameEncode_Initialize(dstFrame, options);
478 WINE_ERR("error 0x%08X initializing encoder frame\n", hr);
481 hr = IWICBitmapSource_GetSize(sourceBitmap, &width, &height);
484 WINE_ERR("error 0x%08X getting source bitmap size\n", hr);
487 hr = IWICBitmapFrameEncode_SetSize(dstFrame, width, height);
490 WINE_ERR("error 0x%08X setting destination bitmap size\n", hr);
493 hr = IWICBitmapFrameEncode_SetResolution(dstFrame, 96, 96);
496 WINE_ERR("error 0x%08X setting destination bitmap resolution\n", hr);
499 hr = IWICBitmapFrameEncode_WriteSource(dstFrame, sourceBitmap, NULL);
502 WINE_ERR("error 0x%08X copying bitmaps\n", hr);
505 hr = IWICBitmapFrameEncode_Commit(dstFrame);
508 WINE_ERR("error 0x%08X committing frame\n", hr);
513 IWICBitmapFrameDecode_Release(sourceFrame);
515 IWICBitmapSource_Release(sourceBitmap);
517 IWICBitmapFrameEncode_Release(dstFrame);
520 hr = IWICBitmapEncoder_Commit(encoder);
523 WINE_ERR("error 0x%08X committing encoder\n", hr);
528 HeapFree(GetProcessHeap(), 0, dosOutputFileName);
530 IWICImagingFactory_Release(factory);
532 IWICBitmapDecoder_Release(decoder);
534 IWICBitmapScaler_Release(scaler);
536 IWICBitmapEncoder_Release(encoder);
538 IStream_Release(outputFile);
545 NE_TYPEINFO *iconResources;
546 WORD alignmentShiftCount;
549 static int populate_module16_icons(struct IconData16 *iconData16, GRPICONDIR *grpIconDir, ICONDIRENTRY *iconDirEntries, BYTE *icons, SIZE_T *iconOffset)
552 int validEntries = 0;
554 for (i = 0; i < grpIconDir->idCount; i++)
556 BYTE *iconPtr = (BYTE*)iconData16->iconResources;
557 NE_NAMEINFO *matchingIcon = NULL;
558 iconPtr += sizeof(NE_TYPEINFO);
559 for (j = 0; j < iconData16->iconResources->count; j++)
561 NE_NAMEINFO *iconInfo = (NE_NAMEINFO*)iconPtr;
562 if ((((BYTE*)iconPtr) + sizeof(NE_NAMEINFO)) > (iconData16->fileBytes + iconData16->fileSize))
564 WINE_WARN("file too small for icon NE_NAMEINFO\n");
567 if (iconInfo->id == (0x8000 | grpIconDir->idEntries[i].nID))
569 matchingIcon = iconInfo;
572 iconPtr += sizeof(NE_NAMEINFO);
575 if (matchingIcon == NULL)
577 if (((matchingIcon->offset << iconData16->alignmentShiftCount) + grpIconDir->idEntries[i].dwBytesInRes) > iconData16->fileSize)
579 WINE_WARN("file too small for icon contents\n");
583 iconDirEntries[validEntries].bWidth = grpIconDir->idEntries[i].bWidth;
584 iconDirEntries[validEntries].bHeight = grpIconDir->idEntries[i].bHeight;
585 iconDirEntries[validEntries].bColorCount = grpIconDir->idEntries[i].bColorCount;
586 iconDirEntries[validEntries].bReserved = grpIconDir->idEntries[i].bReserved;
587 iconDirEntries[validEntries].wPlanes = grpIconDir->idEntries[i].wPlanes;
588 iconDirEntries[validEntries].wBitCount = grpIconDir->idEntries[i].wBitCount;
589 iconDirEntries[validEntries].dwBytesInRes = grpIconDir->idEntries[i].dwBytesInRes;
590 iconDirEntries[validEntries].dwImageOffset = *iconOffset;
592 memcpy(&icons[*iconOffset], &iconData16->fileBytes[matchingIcon->offset << iconData16->alignmentShiftCount], grpIconDir->idEntries[i].dwBytesInRes);
593 *iconOffset += grpIconDir->idEntries[i].dwBytesInRes;
598 static int populate_module_icons(HMODULE hModule, GRPICONDIR *grpIconDir, ICONDIRENTRY *iconDirEntries, BYTE *icons, SIZE_T *iconOffset)
601 int validEntries = 0;
603 for (i = 0; i < grpIconDir->idCount; i++)
606 LPCWSTR lpName = MAKEINTRESOURCEW(grpIconDir->idEntries[i].nID);
607 if ((hResInfo = FindResourceW(hModule, lpName, (LPCWSTR)RT_ICON)))
610 if ((hResData = LoadResource(hModule, hResInfo)))
613 if ((pIcon = LockResource(hResData)))
615 iconDirEntries[validEntries].bWidth = grpIconDir->idEntries[i].bWidth;
616 iconDirEntries[validEntries].bHeight = grpIconDir->idEntries[i].bHeight;
617 iconDirEntries[validEntries].bColorCount = grpIconDir->idEntries[i].bColorCount;
618 iconDirEntries[validEntries].bReserved = grpIconDir->idEntries[i].bReserved;
619 iconDirEntries[validEntries].wPlanes = grpIconDir->idEntries[i].wPlanes;
620 iconDirEntries[validEntries].wBitCount = grpIconDir->idEntries[i].wBitCount;
621 iconDirEntries[validEntries].dwBytesInRes = grpIconDir->idEntries[i].dwBytesInRes;
622 iconDirEntries[validEntries].dwImageOffset = *iconOffset;
624 memcpy(&icons[*iconOffset], pIcon, grpIconDir->idEntries[i].dwBytesInRes);
625 *iconOffset += grpIconDir->idEntries[i].dwBytesInRes;
627 FreeResource(hResData);
634 static IStream *add_module_icons_to_stream(struct IconData16 *iconData16, HMODULE hModule, GRPICONDIR *grpIconDir)
637 SIZE_T iconsSize = 0;
639 ICONDIRENTRY *iconDirEntries = NULL;
640 IStream *stream = NULL;
645 int validEntries = 0;
648 for (i = 0; i < grpIconDir->idCount; i++)
649 iconsSize += grpIconDir->idEntries[i].dwBytesInRes;
650 icons = HeapAlloc(GetProcessHeap(), 0, iconsSize);
653 WINE_ERR("out of memory allocating icon\n");
657 iconDirEntries = HeapAlloc(GetProcessHeap(), 0, grpIconDir->idCount*sizeof(ICONDIRENTRY));
658 if (iconDirEntries == NULL)
660 WINE_ERR("out of memory allocating icon dir entries\n");
664 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
667 WINE_ERR("error creating icon stream\n");
673 validEntries = populate_module16_icons(iconData16, grpIconDir, iconDirEntries, icons, &iconOffset);
675 validEntries = populate_module_icons(hModule, grpIconDir, iconDirEntries, icons, &iconOffset);
677 if (validEntries == 0)
679 WINE_ERR("no valid icon entries\n");
683 iconDir.idReserved = 0;
685 iconDir.idCount = validEntries;
686 hr = IStream_Write(stream, &iconDir, sizeof(iconDir), &bytesWritten);
687 if (FAILED(hr) || bytesWritten != sizeof(iconDir))
689 WINE_ERR("error 0x%08X writing icon stream\n", hr);
692 for (i = 0; i < validEntries; i++)
693 iconDirEntries[i].dwImageOffset += sizeof(ICONDIR) + validEntries*sizeof(ICONDIRENTRY);
694 hr = IStream_Write(stream, iconDirEntries, validEntries*sizeof(ICONDIRENTRY), &bytesWritten);
695 if (FAILED(hr) || bytesWritten != validEntries*sizeof(ICONDIRENTRY))
697 WINE_ERR("error 0x%08X writing icon dir entries to stream\n", hr);
700 hr = IStream_Write(stream, icons, iconOffset, &bytesWritten);
701 if (FAILED(hr) || bytesWritten != iconOffset)
703 WINE_ERR("error 0x%08X writing icon images to stream\n", hr);
707 hr = IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
710 HeapFree(GetProcessHeap(), 0, icons);
711 HeapFree(GetProcessHeap(), 0, iconDirEntries);
712 if (FAILED(hr) && stream != NULL)
714 IStream_Release(stream);
720 static HRESULT open_module16_icon(LPCWSTR szFileName, int nIndex, IStream **ppStream)
722 HANDLE hFile = INVALID_HANDLE_VALUE;
723 HANDLE hFileMapping = NULL;
725 BYTE *fileBytes = NULL;
726 IMAGE_DOS_HEADER *dosHeader;
727 IMAGE_OS2_HEADER *neHeader;
729 NE_TYPEINFO *iconGroupResources;
730 NE_TYPEINFO *iconResources;
731 NE_NAMEINFO *iconDirPtr;
733 WORD alignmentShiftCount;
734 struct IconData16 iconData16;
737 hFile = CreateFileW(szFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
738 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, NULL);
739 if (hFile == INVALID_HANDLE_VALUE)
741 WINE_WARN("opening %s failed with error %d\n", wine_dbgstr_w(szFileName), GetLastError());
745 hFileMapping = CreateFileMappingW(hFile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
746 if (hFileMapping == NULL)
748 WINE_WARN("CreateFileMapping failed, error %d\n", GetLastError());
752 fileSize = GetFileSize(hFile, NULL);
754 fileBytes = MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);
755 if (fileBytes == NULL)
757 WINE_WARN("MapViewOfFile failed, error %d\n", GetLastError());
761 dosHeader = (IMAGE_DOS_HEADER*)fileBytes;
762 if (sizeof(IMAGE_DOS_HEADER) >= fileSize || dosHeader->e_magic != IMAGE_DOS_SIGNATURE)
764 WINE_WARN("file too small for MZ header\n");
768 neHeader = (IMAGE_OS2_HEADER*)(fileBytes + dosHeader->e_lfanew);
769 if ((((BYTE*)neHeader) + sizeof(IMAGE_OS2_HEADER)) > (fileBytes + fileSize) ||
770 neHeader->ne_magic != IMAGE_OS2_SIGNATURE)
772 WINE_WARN("file too small for NE header\n");
776 rsrcTab = ((BYTE*)neHeader) + neHeader->ne_rsrctab;
777 if ((rsrcTab + 2) > (fileBytes + fileSize))
779 WINE_WARN("file too small for resource table\n");
783 alignmentShiftCount = *(WORD*)rsrcTab;
785 iconGroupResources = NULL;
786 iconResources = NULL;
789 NE_TYPEINFO *neTypeInfo = (NE_TYPEINFO*)rsrcTab;
790 if ((rsrcTab + sizeof(NE_TYPEINFO)) > (fileBytes + fileSize))
792 WINE_WARN("file too small for resource table\n");
795 if (neTypeInfo->type_id == 0)
797 else if (neTypeInfo->type_id == NE_RSCTYPE_GROUP_ICON)
798 iconGroupResources = neTypeInfo;
799 else if (neTypeInfo->type_id == NE_RSCTYPE_ICON)
800 iconResources = neTypeInfo;
801 rsrcTab += sizeof(NE_TYPEINFO) + neTypeInfo->count*sizeof(NE_NAMEINFO);
803 if (iconGroupResources == NULL)
805 WINE_WARN("no group icon resource type found\n");
808 if (iconResources == NULL)
810 WINE_WARN("no icon resource type found\n");
814 if (nIndex >= iconGroupResources->count)
816 WINE_WARN("icon index out of range\n");
820 iconDirPtr = (NE_NAMEINFO*)(((BYTE*)iconGroupResources) + sizeof(NE_TYPEINFO) + nIndex*sizeof(NE_NAMEINFO));
821 if ((((BYTE*)iconDirPtr) + sizeof(NE_NAMEINFO)) > (fileBytes + fileSize))
823 WINE_WARN("file to small for icon group NE_NAMEINFO\n");
826 iconDir = (GRPICONDIR*)(fileBytes + (iconDirPtr->offset << alignmentShiftCount));
827 if ((((BYTE*)iconDir) + sizeof(GRPICONDIR) + iconDir->idCount*sizeof(GRPICONDIRENTRY)) > (fileBytes + fileSize))
829 WINE_WARN("file too small for GRPICONDIR\n");
833 iconData16.fileBytes = fileBytes;
834 iconData16.fileSize = fileSize;
835 iconData16.iconResources = iconResources;
836 iconData16.alignmentShiftCount = alignmentShiftCount;
837 *ppStream = add_module_icons_to_stream(&iconData16, NULL, iconDir);
842 if (hFile != INVALID_HANDLE_VALUE)
844 if (hFileMapping != NULL)
845 CloseHandle(hFileMapping);
846 if (fileBytes != NULL)
847 UnmapViewOfFile(fileBytes);
851 static BOOL CALLBACK EnumResNameProc(HMODULE hModule, LPCWSTR lpszType, LPWSTR lpszName, LONG_PTR lParam)
853 ENUMRESSTRUCT *sEnumRes = (ENUMRESSTRUCT *) lParam;
855 if (!sEnumRes->nIndex--)
857 *sEnumRes->pResInfo = FindResourceW(hModule, lpszName, (LPCWSTR)RT_GROUP_ICON);
864 static HRESULT open_module_icon(LPCWSTR szFileName, int nIndex, IStream **ppStream)
869 GRPICONDIR *pIconDir;
870 ENUMRESSTRUCT sEnumRes;
873 hModule = LoadLibraryExW(szFileName, 0, LOAD_LIBRARY_AS_DATAFILE);
876 if (GetLastError() == ERROR_BAD_EXE_FORMAT)
877 return open_module16_icon(szFileName, nIndex, ppStream);
880 WINE_WARN("LoadLibraryExW (%s) failed, error %d\n",
881 wine_dbgstr_w(szFileName), GetLastError());
882 return HRESULT_FROM_WIN32(GetLastError());
888 hResInfo = FindResourceW(hModule, MAKEINTRESOURCEW(-nIndex), (LPCWSTR)RT_GROUP_ICON);
889 WINE_TRACE("FindResourceW (%s) called, return %p, error %d\n",
890 wine_dbgstr_w(szFileName), hResInfo, GetLastError());
895 sEnumRes.pResInfo = &hResInfo;
896 sEnumRes.nIndex = nIndex;
897 if (!EnumResourceNamesW(hModule, (LPCWSTR)RT_GROUP_ICON,
898 EnumResNameProc, (LONG_PTR)&sEnumRes) &&
899 sEnumRes.nIndex != -1)
901 WINE_TRACE("EnumResourceNamesW failed, error %d\n", GetLastError());
907 if ((hResData = LoadResource(hModule, hResInfo)))
909 if ((pIconDir = LockResource(hResData)))
911 *ppStream = add_module_icons_to_stream(0, hModule, pIconDir);
916 FreeResource(hResData);
921 WINE_WARN("found no icon\n");
922 FreeLibrary(hModule);
923 return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
926 FreeLibrary(hModule);
930 static HRESULT read_ico_direntries(IStream *icoStream, ICONDIRENTRY **ppIconDirEntries, int *numEntries)
936 *ppIconDirEntries = NULL;
938 hr = IStream_Read(icoStream, &iconDir, sizeof(ICONDIR), &bytesRead);
939 if (FAILED(hr) || bytesRead != sizeof(ICONDIR) ||
940 (iconDir.idReserved != 0) || (iconDir.idType != 1))
942 WINE_WARN("Invalid ico file format (hr=0x%08X, bytesRead=%d)\n", hr, bytesRead);
946 *numEntries = iconDir.idCount;
948 if ((*ppIconDirEntries = HeapAlloc(GetProcessHeap(), 0, sizeof(ICONDIRENTRY)*iconDir.idCount)) == NULL)
953 hr = IStream_Read(icoStream, *ppIconDirEntries, sizeof(ICONDIRENTRY)*iconDir.idCount, &bytesRead);
954 if (FAILED(hr) || bytesRead != sizeof(ICONDIRENTRY)*iconDir.idCount)
956 if (SUCCEEDED(hr)) hr = E_FAIL;
962 HeapFree(GetProcessHeap(), 0, *ppIconDirEntries);
966 static HRESULT write_native_icon(IStream *iconStream, const char *icon_name, LPCWSTR szFileName)
968 ICONDIRENTRY *pIconDirEntry = NULL;
970 int nMax = 0, nMaxBits = 0;
973 LARGE_INTEGER position;
976 hr = read_ico_direntries(iconStream, &pIconDirEntry, &numEntries);
980 for (i = 0; i < numEntries; i++)
982 WINE_TRACE("[%d]: %d x %d @ %d\n", i, pIconDirEntry[i].bWidth, pIconDirEntry[i].bHeight, pIconDirEntry[i].wBitCount);
983 if (pIconDirEntry[i].wBitCount >= nMaxBits &&
984 (pIconDirEntry[i].bHeight * pIconDirEntry[i].bWidth) >= nMax)
987 nMax = pIconDirEntry[i].bHeight * pIconDirEntry[i].bWidth;
988 nMaxBits = pIconDirEntry[i].wBitCount;
991 WINE_TRACE("Selected: %d\n", nIndex);
993 position.QuadPart = 0;
994 hr = IStream_Seek(iconStream, position, STREAM_SEEK_SET, NULL);
997 hr = convert_to_native_icon(iconStream, &nIndex, 1, &CLSID_WICPngEncoder, icon_name, szFileName);
1000 HeapFree(GetProcessHeap(), 0, pIconDirEntry);
1004 static HRESULT open_file_type_icon(LPCWSTR szFileName, IStream **ppStream)
1006 static const WCHAR openW[] = {'o','p','e','n',0};
1010 WCHAR *executable = NULL;
1012 char *output_path = NULL;
1013 HRESULT hr = HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
1015 extension = strrchrW(szFileName, '.');
1016 if (extension == NULL)
1019 icon = assoc_query(ASSOCSTR_DEFAULTICON, extension, NULL);
1022 comma = strrchrW(icon, ',');
1026 index = atoiW(comma + 1);
1028 hr = open_icon(icon, index, FALSE, ppStream);
1032 executable = assoc_query(ASSOCSTR_EXECUTABLE, extension, openW);
1034 hr = open_icon(executable, 0, FALSE, ppStream);
1038 HeapFree(GetProcessHeap(), 0, icon);
1039 HeapFree(GetProcessHeap(), 0, executable);
1040 HeapFree(GetProcessHeap(), 0, output_path);
1044 static HRESULT open_default_icon(IStream **ppStream)
1046 static const WCHAR user32W[] = {'u','s','e','r','3','2',0};
1048 return open_module_icon(user32W, -(INT_PTR)IDI_WINLOGO, ppStream);
1051 static HRESULT open_icon(LPCWSTR filename, int index, BOOL bWait, IStream **ppStream)
1055 hr = open_module_icon(filename, index, ppStream);
1058 static const WCHAR dot_icoW[] = {'.','i','c','o',0};
1059 int len = strlenW(filename);
1060 if (len >= 4 && strcmpiW(&filename[len - 4], dot_icoW) == 0)
1061 hr = SHCreateStreamOnFileW(filename, STGM_READ, ppStream);
1064 hr = open_file_type_icon(filename, ppStream);
1065 if (FAILED(hr) && !bWait)
1066 hr = open_default_icon(ppStream);
1070 static char* compute_native_identifier(int exeIndex, LPCWSTR icoPathW)
1072 char* nativeIdentifier;
1075 char *basename, *ext;
1077 icoPathA = wchars_to_utf8_chars(icoPathW);
1078 if (icoPathA == NULL)
1081 crc = crc16(icoPathA);
1082 basename = strrchr(icoPathA, '\\');
1083 if (basename == NULL)
1084 basename = icoPathA;
1090 ext = strrchr(basename, '.');
1094 nativeIdentifier = heap_printf("%04X_%s.%d", crc, basename, exeIndex);
1095 HeapFree(GetProcessHeap(), 0, icoPathA);
1096 return nativeIdentifier;
1100 #define ICNS_SLOTS 6
1102 static inline int size_to_slot(int size)
1109 case 64: return -2; /* Classic Mode */
1118 #define CLASSIC_SLOT 3
1120 static HRESULT platform_write_icon(IStream *icoStream, int exeIndex, LPCWSTR icoPathW,
1121 const char *destFilename, char **nativeIdentifier)
1123 ICONDIRENTRY *iconDirEntries = NULL;
1130 int indexes[ICNS_SLOTS];
1132 char *icnsPath = NULL;
1136 hr = read_ico_direntries(icoStream, &iconDirEntries, &numEntries);
1139 for (i = 0; i < ICNS_SLOTS; i++)
1142 best[i].maxBits = 0;
1144 for (i = 0; i < numEntries; i++)
1147 int width = iconDirEntries[i].bWidth ? iconDirEntries[i].bWidth : 256;
1148 int height = iconDirEntries[i].bHeight ? iconDirEntries[i].bHeight : 256;
1149 BOOL scaled = FALSE;
1151 WINE_TRACE("[%d]: %d x %d @ %d\n", i, width, height, iconDirEntries[i].wBitCount);
1152 if (height != width)
1154 slot = size_to_slot(width);
1158 slot = CLASSIC_SLOT;
1162 if (scaled && best[slot].maxBits && !best[slot].scaled)
1163 continue; /* don't replace unscaled with scaled */
1164 if (iconDirEntries[i].wBitCount >= best[slot].maxBits || (!scaled && best[slot].scaled))
1166 best[slot].index = i;
1167 best[slot].maxBits = iconDirEntries[i].wBitCount;
1168 best[slot].scaled = scaled;
1171 /* remove the scaled icon if a larger unscaled icon exists */
1172 if (best[CLASSIC_SLOT].scaled)
1174 for (i = CLASSIC_SLOT+1; i < ICNS_SLOTS; i++)
1175 if (best[i].index >= 0 && !best[i].scaled)
1177 best[CLASSIC_SLOT].index = -1;
1183 for (i = 0; i < ICNS_SLOTS; i++)
1185 if (best[i].index >= 0)
1187 indexes[numEntries] = best[i].index;
1193 *nativeIdentifier = heap_printf("%s", destFilename);
1195 *nativeIdentifier = compute_native_identifier(exeIndex, icoPathW);
1196 if (*nativeIdentifier == NULL)
1201 icnsPath = heap_printf("/tmp/%s.icns", *nativeIdentifier);
1202 if (icnsPath == NULL)
1205 WINE_WARN("out of memory creating ICNS path\n");
1209 hr = IStream_Seek(icoStream, zero, STREAM_SEEK_SET, NULL);
1212 WINE_WARN("seeking icon stream failed, error 0x%08X\n", hr);
1215 hr = convert_to_native_icon(icoStream, indexes, numEntries, &CLSID_WICIcnsEncoder,
1216 icnsPath, icoPathW);
1219 WINE_WARN("converting %s to %s failed, error 0x%08X\n",
1220 wine_dbgstr_w(icoPathW), wine_dbgstr_a(icnsPath), hr);
1225 HeapFree(GetProcessHeap(), 0, iconDirEntries);
1226 HeapFree(GetProcessHeap(), 0, icnsPath);
1230 static void refresh_icon_cache(const char *iconsDir)
1232 /* The icon theme spec only requires the mtime on the "toplevel"
1233 * directory (whatever that is) to be changed for a refresh,
1234 * but on GNOME you have to create a file in that directory
1235 * instead. Creating a file also works on KDE, Xfce and LXDE.
1237 char *filename = heap_printf("%s/.wine-refresh-XXXXXX", iconsDir);
1238 if (filename != NULL)
1240 int fd = mkstemps(filename, 0);
1246 HeapFree(GetProcessHeap(), 0, filename);
1250 static HRESULT platform_write_icon(IStream *icoStream, int exeIndex, LPCWSTR icoPathW,
1251 const char *destFilename, char **nativeIdentifier)
1253 ICONDIRENTRY *iconDirEntries = NULL;
1256 char *iconsDir = NULL;
1260 hr = read_ico_direntries(icoStream, &iconDirEntries, &numEntries);
1265 *nativeIdentifier = heap_printf("%s", destFilename);
1267 *nativeIdentifier = compute_native_identifier(exeIndex, icoPathW);
1268 if (*nativeIdentifier == NULL)
1273 iconsDir = heap_printf("%s/icons/hicolor", xdg_data_dir);
1274 if (iconsDir == NULL)
1280 for (i = 0; i < numEntries; i++)
1284 BOOLEAN duplicate = FALSE;
1286 char *iconDir = NULL;
1287 char *pngPath = NULL;
1289 WINE_TRACE("[%d]: %d x %d @ %d\n", i, iconDirEntries[i].bWidth,
1290 iconDirEntries[i].bHeight, iconDirEntries[i].wBitCount);
1292 for (j = 0; j < i; j++)
1294 if (iconDirEntries[j].bWidth == iconDirEntries[i].bWidth &&
1295 iconDirEntries[j].bHeight == iconDirEntries[i].bHeight)
1303 for (j = i + 1; j < numEntries; j++)
1305 if (iconDirEntries[j].bWidth == iconDirEntries[i].bWidth &&
1306 iconDirEntries[j].bHeight == iconDirEntries[i].bHeight &&
1307 iconDirEntries[j].wBitCount >= iconDirEntries[bestIndex].wBitCount)
1312 WINE_TRACE("Selected: %d\n", bestIndex);
1314 w = iconDirEntries[bestIndex].bWidth ? iconDirEntries[bestIndex].bWidth : 256;
1315 h = iconDirEntries[bestIndex].bHeight ? iconDirEntries[bestIndex].bHeight : 256;
1316 iconDir = heap_printf("%s/%dx%d/apps", iconsDir, w, h);
1317 if (iconDir == NULL)
1322 create_directories(iconDir);
1323 pngPath = heap_printf("%s/%s.png", iconDir, *nativeIdentifier);
1324 if (pngPath == NULL)
1330 hr = IStream_Seek(icoStream, zero, STREAM_SEEK_SET, NULL);
1333 hr = convert_to_native_icon(icoStream, &bestIndex, 1, &CLSID_WICPngEncoder,
1337 HeapFree(GetProcessHeap(), 0, iconDir);
1338 HeapFree(GetProcessHeap(), 0, pngPath);
1340 refresh_icon_cache(iconsDir);
1343 HeapFree(GetProcessHeap(), 0, iconDirEntries);
1344 HeapFree(GetProcessHeap(), 0, iconsDir);
1347 #endif /* defined(__APPLE__) */
1349 /* extract an icon from an exe or icon file; helper for IPersistFile_fnSave */
1350 static char *extract_icon(LPCWSTR icoPathW, int index, const char *destFilename, BOOL bWait)
1352 IStream *stream = NULL;
1354 char *nativeIdentifier = NULL;
1356 WINE_TRACE("path=[%s] index=%d destFilename=[%s]\n", wine_dbgstr_w(icoPathW), index, wine_dbgstr_a(destFilename));
1358 hr = open_icon(icoPathW, index, bWait, &stream);
1361 WINE_WARN("opening icon %s index %d failed, hr=0x%08X\n", wine_dbgstr_w(icoPathW), index, hr);
1364 hr = platform_write_icon(stream, index, icoPathW, destFilename, &nativeIdentifier);
1366 WINE_WARN("writing icon failed, error 0x%08X\n", hr);
1370 IStream_Release(stream);
1373 HeapFree(GetProcessHeap(), 0, nativeIdentifier);
1374 nativeIdentifier = NULL;
1376 return nativeIdentifier;
1379 static HKEY open_menus_reg_key(void)
1381 static const WCHAR Software_Wine_FileOpenAssociationsW[] = {
1382 'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\','M','e','n','u','F','i','l','e','s',0};
1385 ret = RegCreateKeyW(HKEY_CURRENT_USER, Software_Wine_FileOpenAssociationsW, &assocKey);
1386 if (ret == ERROR_SUCCESS)
1392 static DWORD register_menus_entry(const char *unix_file, const char *windows_file)
1395 WCHAR *windows_fileW;
1399 size = MultiByteToWideChar(CP_UNIXCP, 0, unix_file, -1, NULL, 0);
1400 unix_fileW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
1403 MultiByteToWideChar(CP_UNIXCP, 0, unix_file, -1, unix_fileW, size);
1404 size = MultiByteToWideChar(CP_UNIXCP, 0, windows_file, -1, NULL, 0);
1405 windows_fileW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
1409 MultiByteToWideChar(CP_UNIXCP, 0, windows_file, -1, windows_fileW, size);
1410 hkey = open_menus_reg_key();
1413 ret = RegSetValueExW(hkey, unix_fileW, 0, REG_SZ, (const BYTE*)windows_fileW,
1414 (strlenW(windows_fileW) + 1) * sizeof(WCHAR));
1418 ret = GetLastError();
1419 HeapFree(GetProcessHeap(), 0, windows_fileW);
1422 ret = ERROR_NOT_ENOUGH_MEMORY;
1423 HeapFree(GetProcessHeap(), 0, unix_fileW);
1426 ret = ERROR_NOT_ENOUGH_MEMORY;
1430 static BOOL write_desktop_entry(const char *unix_link, const char *location, const char *linkname,
1431 const char *path, const char *args, const char *descr,
1432 const char *workdir, const char *icon)
1436 WINE_TRACE("(%s,%s,%s,%s,%s,%s,%s,%s)\n", wine_dbgstr_a(unix_link), wine_dbgstr_a(location),
1437 wine_dbgstr_a(linkname), wine_dbgstr_a(path), wine_dbgstr_a(args),
1438 wine_dbgstr_a(descr), wine_dbgstr_a(workdir), wine_dbgstr_a(icon));
1440 file = fopen(location, "w");
1444 fprintf(file, "[Desktop Entry]\n");
1445 fprintf(file, "Name=%s\n", linkname);
1446 fprintf(file, "Exec=env WINEPREFIX=\"%s\" wine %s %s\n",
1447 wine_get_config_dir(), path, args);
1448 fprintf(file, "Type=Application\n");
1449 fprintf(file, "StartupNotify=true\n");
1450 if (descr && lstrlenA(descr))
1451 fprintf(file, "Comment=%s\n", descr);
1452 if (workdir && lstrlenA(workdir))
1453 fprintf(file, "Path=%s\n", workdir);
1454 if (icon && lstrlenA(icon))
1455 fprintf(file, "Icon=%s\n", icon);
1461 DWORD ret = register_menus_entry(location, unix_link);
1462 if (ret != ERROR_SUCCESS)
1469 static BOOL write_directory_entry(const char *directory, const char *location)
1473 WINE_TRACE("(%s,%s)\n", wine_dbgstr_a(directory), wine_dbgstr_a(location));
1475 file = fopen(location, "w");
1479 fprintf(file, "[Desktop Entry]\n");
1480 fprintf(file, "Type=Directory\n");
1481 if (strcmp(directory, "wine") == 0)
1483 fprintf(file, "Name=Wine\n");
1484 fprintf(file, "Icon=wine\n");
1488 fprintf(file, "Name=%s\n", directory);
1489 fprintf(file, "Icon=folder\n");
1496 static BOOL write_menu_file(const char *unix_link, const char *filename)
1499 FILE *tempfile = NULL;
1502 char *menuPath = NULL;
1507 WINE_TRACE("(%s)\n", wine_dbgstr_a(filename));
1511 tempfilename = heap_printf("%s/wine-menu-XXXXXX", xdg_config_dir);
1514 int tempfd = mkstemps(tempfilename, 0);
1517 tempfile = fdopen(tempfd, "w");
1523 else if (errno == EEXIST)
1525 HeapFree(GetProcessHeap(), 0, tempfilename);
1528 HeapFree(GetProcessHeap(), 0, tempfilename);
1533 fprintf(tempfile, "<!DOCTYPE Menu PUBLIC \"-//freedesktop//DTD Menu 1.0//EN\"\n");
1534 fprintf(tempfile, "\"http://www.freedesktop.org/standards/menu-spec/menu-1.0.dtd\">\n");
1535 fprintf(tempfile, "<Menu>\n");
1536 fprintf(tempfile, " <Name>Applications</Name>\n");
1538 name = HeapAlloc(GetProcessHeap(), 0, lstrlenA(filename) + 1);
1539 if (name == NULL) goto end;
1541 for (i = 0; filename[i]; i++)
1543 name[i] = filename[i];
1544 if (filename[i] == '/')
1546 char *dir_file_name;
1549 fprintf(tempfile, " <Menu>\n");
1550 fprintf(tempfile, " <Name>%s", count ? "" : "wine-");
1551 write_xml_text(tempfile, name);
1552 fprintf(tempfile, "</Name>\n");
1553 fprintf(tempfile, " <Directory>%s", count ? "" : "wine-");
1554 write_xml_text(tempfile, name);
1555 fprintf(tempfile, ".directory</Directory>\n");
1556 dir_file_name = heap_printf("%s/desktop-directories/%s%s.directory",
1557 xdg_data_dir, count ? "" : "wine-", name);
1560 if (stat(dir_file_name, &st) != 0 && errno == ENOENT)
1561 write_directory_entry(lastEntry, dir_file_name);
1562 HeapFree(GetProcessHeap(), 0, dir_file_name);
1565 lastEntry = &name[i+1];
1571 fprintf(tempfile, " <Include>\n");
1572 fprintf(tempfile, " <Filename>");
1573 write_xml_text(tempfile, name);
1574 fprintf(tempfile, "</Filename>\n");
1575 fprintf(tempfile, " </Include>\n");
1576 for (i = 0; i < count; i++)
1577 fprintf(tempfile, " </Menu>\n");
1578 fprintf(tempfile, "</Menu>\n");
1580 menuPath = heap_printf("%s/%s", xdg_config_dir, name);
1581 if (menuPath == NULL) goto end;
1582 strcpy(menuPath + strlen(menuPath) - strlen(".desktop"), ".menu");
1589 ret = (rename(tempfilename, menuPath) == 0);
1590 if (!ret && tempfilename)
1591 remove(tempfilename);
1592 HeapFree(GetProcessHeap(), 0, tempfilename);
1594 register_menus_entry(menuPath, unix_link);
1595 HeapFree(GetProcessHeap(), 0, name);
1596 HeapFree(GetProcessHeap(), 0, menuPath);
1600 static BOOL write_menu_entry(const char *unix_link, const char *link, const char *path, const char *args,
1601 const char *descr, const char *workdir, const char *icon)
1603 const char *linkname;
1604 char *desktopPath = NULL;
1606 char *filename = NULL;
1609 WINE_TRACE("(%s, %s, %s, %s, %s, %s, %s)\n", wine_dbgstr_a(unix_link), wine_dbgstr_a(link),
1610 wine_dbgstr_a(path), wine_dbgstr_a(args), wine_dbgstr_a(descr),
1611 wine_dbgstr_a(workdir), wine_dbgstr_a(icon));
1613 linkname = strrchr(link, '/');
1614 if (linkname == NULL)
1619 desktopPath = heap_printf("%s/applications/wine/%s.desktop", xdg_data_dir, link);
1622 WINE_WARN("out of memory creating menu entry\n");
1626 desktopDir = strrchr(desktopPath, '/');
1628 if (!create_directories(desktopPath))
1630 WINE_WARN("couldn't make parent directories for %s\n", wine_dbgstr_a(desktopPath));
1635 if (!write_desktop_entry(unix_link, desktopPath, linkname, path, args, descr, workdir, icon))
1637 WINE_WARN("couldn't make desktop entry %s\n", wine_dbgstr_a(desktopPath));
1642 filename = heap_printf("wine/%s.desktop", link);
1643 if (!filename || !write_menu_file(unix_link, filename))
1645 WINE_WARN("couldn't make menu file %s\n", wine_dbgstr_a(filename));
1650 HeapFree(GetProcessHeap(), 0, desktopPath);
1651 HeapFree(GetProcessHeap(), 0, filename);
1655 /* This escapes reserved characters in .desktop files' Exec keys. */
1656 static LPSTR escape(LPCWSTR arg)
1659 WCHAR *escaped_string;
1662 escaped_string = HeapAlloc(GetProcessHeap(), 0, (4 * strlenW(arg) + 1) * sizeof(WCHAR));
1663 if (escaped_string == NULL) return NULL;
1664 for (i = j = 0; arg[i]; i++)
1669 escaped_string[j++] = '\\';
1670 escaped_string[j++] = '\\';
1671 escaped_string[j++] = '\\';
1672 escaped_string[j++] = '\\';
1692 escaped_string[j++] = '\\';
1693 escaped_string[j++] = '\\';
1696 escaped_string[j++] = arg[i];
1700 escaped_string[j] = 0;
1702 utf8_string = wchars_to_utf8_chars(escaped_string);
1703 if (utf8_string == NULL)
1705 WINE_ERR("out of memory\n");
1710 HeapFree(GetProcessHeap(), 0, escaped_string);
1714 /* Return a heap-allocated copy of the unix format difference between the two
1715 * Windows-format paths.
1716 * locn is the owning location
1717 * link is within locn
1719 static char *relative_path( LPCWSTR link, LPCWSTR locn )
1721 char *unix_locn, *unix_link;
1722 char *relative = NULL;
1724 unix_locn = wine_get_unix_file_name(locn);
1725 unix_link = wine_get_unix_file_name(link);
1726 if (unix_locn && unix_link)
1728 size_t len_unix_locn, len_unix_link;
1729 len_unix_locn = strlen (unix_locn);
1730 len_unix_link = strlen (unix_link);
1731 if (len_unix_locn < len_unix_link && memcmp (unix_locn, unix_link, len_unix_locn) == 0 && unix_link[len_unix_locn] == '/')
1734 char *p = strrchr (unix_link + len_unix_locn, '/');
1735 p = strrchr (p, '.');
1739 len_unix_link = p - unix_link;
1741 len_rel = len_unix_link - len_unix_locn;
1742 relative = HeapAlloc(GetProcessHeap(), 0, len_rel);
1745 memcpy (relative, unix_link + len_unix_locn + 1, len_rel);
1750 WINE_WARN("Could not separate the relative link path of %s in %s\n", wine_dbgstr_w(link), wine_dbgstr_w(locn));
1751 HeapFree(GetProcessHeap(), 0, unix_locn);
1752 HeapFree(GetProcessHeap(), 0, unix_link);
1756 /***********************************************************************
1760 * returns TRUE if successful
1761 * *loc will contain CS_DESKTOPDIRECTORY, CS_STARTMENU, CS_STARTUP etc.
1762 * *relative will contain the address of a heap-allocated copy of the portion
1763 * of the filename that is within the specified location, in unix form
1765 static BOOL GetLinkLocation( LPCWSTR linkfile, DWORD *loc, char **relative )
1767 WCHAR filename[MAX_PATH], shortfilename[MAX_PATH], buffer[MAX_PATH];
1768 DWORD len, i, r, filelen;
1769 const DWORD locations[] = {
1770 CSIDL_STARTUP, CSIDL_DESKTOPDIRECTORY, CSIDL_STARTMENU,
1771 CSIDL_COMMON_STARTUP, CSIDL_COMMON_DESKTOPDIRECTORY,
1772 CSIDL_COMMON_STARTMENU };
1774 WINE_TRACE("%s\n", wine_dbgstr_w(linkfile));
1775 filelen=GetFullPathNameW( linkfile, MAX_PATH, shortfilename, NULL );
1776 if (filelen==0 || filelen>MAX_PATH)
1779 WINE_TRACE("%s\n", wine_dbgstr_w(shortfilename));
1781 /* the CSLU Toolkit uses a short path name when creating .lnk files;
1782 * expand or our hardcoded list won't match.
1784 filelen=GetLongPathNameW(shortfilename, filename, MAX_PATH);
1785 if (filelen==0 || filelen>MAX_PATH)
1788 WINE_TRACE("%s\n", wine_dbgstr_w(filename));
1790 for( i=0; i<sizeof(locations)/sizeof(locations[0]); i++ )
1792 if (!SHGetSpecialFolderPathW( 0, buffer, locations[i], FALSE ))
1795 len = lstrlenW(buffer);
1796 if (len >= MAX_PATH)
1797 continue; /* We've just trashed memory! Hopefully we are OK */
1799 if (len > filelen || filename[len]!='\\')
1801 /* do a lstrcmpinW */
1803 r = lstrcmpiW( filename, buffer );
1804 filename[len] = '\\';
1808 /* return the remainder of the string and link type */
1809 *loc = locations[i];
1810 *relative = relative_path (filename, buffer);
1811 return (*relative != NULL);
1817 /* gets the target path directly or through MSI */
1818 static HRESULT get_cmdline( IShellLinkW *sl, LPWSTR szPath, DWORD pathSize,
1819 LPWSTR szArgs, DWORD argsSize)
1821 IShellLinkDataList *dl = NULL;
1822 EXP_DARWIN_LINK *dar = NULL;
1828 hr = IShellLinkW_GetPath( sl, szPath, pathSize, NULL, SLGP_RAWPATH );
1829 if (hr == S_OK && szPath[0])
1831 IShellLinkW_GetArguments( sl, szArgs, argsSize );
1835 hr = IShellLinkW_QueryInterface( sl, &IID_IShellLinkDataList, (LPVOID*) &dl );
1839 hr = IShellLinkDataList_CopyDataBlock( dl, EXP_DARWIN_ID_SIG, (LPVOID*) &dar );
1846 hr = CommandLineFromMsiDescriptor( dar->szwDarwinID, NULL, &cmdSize );
1847 if (hr == ERROR_SUCCESS)
1850 szCmdline = HeapAlloc( GetProcessHeap(), 0, cmdSize*sizeof(WCHAR) );
1851 hr = CommandLineFromMsiDescriptor( dar->szwDarwinID, szCmdline, &cmdSize );
1852 WINE_TRACE(" command : %s\n", wine_dbgstr_w(szCmdline));
1853 if (hr == ERROR_SUCCESS)
1856 int bcount, in_quotes;
1858 /* Extract the application path */
1865 if ((*s==0x0009 || *s==0x0020) && !in_quotes)
1867 /* skip the remaining spaces */
1870 } while (*s==0x0009 || *s==0x0020);
1873 else if (*s==0x005c)
1879 else if (*s==0x0022)
1882 if ((bcount & 1)==0)
1884 /* Preceded by an even number of '\', this is
1885 * half that number of '\', plus a quote which
1889 in_quotes=!in_quotes;
1894 /* Preceded by an odd number of '\', this is
1895 * half that number of '\' followed by a '"'
1905 /* a regular character */
1909 if ((d-szPath) == pathSize)
1911 /* Keep processing the path till we get to the
1912 * arguments, but 'stand still'
1917 /* Close the application path */
1920 lstrcpynW(szArgs, s, argsSize);
1922 HeapFree( GetProcessHeap(), 0, szCmdline );
1927 IShellLinkDataList_Release( dl );
1931 static WCHAR* assoc_query(ASSOCSTR assocStr, LPCWSTR name, LPCWSTR extra)
1934 WCHAR *value = NULL;
1936 hr = AssocQueryStringW(0, assocStr, name, extra, NULL, &size);
1939 value = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
1942 hr = AssocQueryStringW(0, assocStr, name, extra, value, &size);
1945 HeapFree(GetProcessHeap(), 0, value);
1953 static char *slashes_to_minuses(const char *string)
1956 char *ret = HeapAlloc(GetProcessHeap(), 0, lstrlenA(string) + 1);
1959 for (i = 0; string[i]; i++)
1961 if (string[i] == '/')
1972 static BOOL next_line(FILE *file, char **line, int *size)
1979 *line = HeapAlloc(GetProcessHeap(), 0, *size);
1981 while (*line != NULL)
1983 if (fgets(&(*line)[pos], *size - pos, file) == NULL)
1985 HeapFree(GetProcessHeap(), 0, *line);
1991 pos = strlen(*line);
1992 cr = strchr(*line, '\n');
1997 line2 = HeapReAlloc(GetProcessHeap(), 0, *line, *size);
2002 HeapFree(GetProcessHeap(), 0, *line);
2015 static BOOL add_mimes(const char *xdg_data_dir, struct list *mime_types)
2017 char *globs_filename = NULL;
2019 globs_filename = heap_printf("%s/mime/globs", xdg_data_dir);
2022 FILE *globs_file = fopen(globs_filename, "r");
2023 if (globs_file) /* doesn't have to exist */
2027 while (ret && (ret = next_line(globs_file, &line, &size)) && line)
2030 struct xdg_mime_type *mime_type_entry = NULL;
2031 if (line[0] != '#' && (pos = strchr(line, ':')))
2033 mime_type_entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct xdg_mime_type));
2034 if (mime_type_entry)
2037 mime_type_entry->mimeType = strdupA(line);
2038 mime_type_entry->glob = strdupA(pos + 1);
2039 mime_type_entry->lower_glob = strdupA(pos + 1);
2040 if (mime_type_entry->lower_glob)
2043 for (l = mime_type_entry->lower_glob; *l; l++)
2046 if (mime_type_entry->mimeType && mime_type_entry->glob && mime_type_entry->lower_glob)
2047 list_add_tail(mime_types, &mime_type_entry->entry);
2050 HeapFree(GetProcessHeap(), 0, mime_type_entry->mimeType);
2051 HeapFree(GetProcessHeap(), 0, mime_type_entry->glob);
2052 HeapFree(GetProcessHeap(), 0, mime_type_entry->lower_glob);
2053 HeapFree(GetProcessHeap(), 0, mime_type_entry);
2061 HeapFree(GetProcessHeap(), 0, line);
2064 HeapFree(GetProcessHeap(), 0, globs_filename);
2071 static void free_native_mime_types(struct list *native_mime_types)
2073 struct xdg_mime_type *mime_type_entry, *mime_type_entry2;
2075 LIST_FOR_EACH_ENTRY_SAFE(mime_type_entry, mime_type_entry2, native_mime_types, struct xdg_mime_type, entry)
2077 list_remove(&mime_type_entry->entry);
2078 HeapFree(GetProcessHeap(), 0, mime_type_entry->glob);
2079 HeapFree(GetProcessHeap(), 0, mime_type_entry->lower_glob);
2080 HeapFree(GetProcessHeap(), 0, mime_type_entry->mimeType);
2081 HeapFree(GetProcessHeap(), 0, mime_type_entry);
2083 HeapFree(GetProcessHeap(), 0, native_mime_types);
2086 static BOOL build_native_mime_types(const char *xdg_data_home, struct list **mime_types)
2088 char *xdg_data_dirs;
2093 xdg_data_dirs = getenv("XDG_DATA_DIRS");
2094 if (xdg_data_dirs == NULL)
2095 xdg_data_dirs = heap_printf("/usr/local/share/:/usr/share/");
2097 xdg_data_dirs = strdupA(xdg_data_dirs);
2101 *mime_types = HeapAlloc(GetProcessHeap(), 0, sizeof(struct list));
2107 list_init(*mime_types);
2108 ret = add_mimes(xdg_data_home, *mime_types);
2111 for (begin = xdg_data_dirs; (end = strchr(begin, ':')); begin = end + 1)
2114 ret = add_mimes(begin, *mime_types);
2120 ret = add_mimes(begin, *mime_types);
2125 HeapFree(GetProcessHeap(), 0, xdg_data_dirs);
2129 if (!ret && *mime_types)
2131 free_native_mime_types(*mime_types);
2137 static BOOL match_glob(struct list *native_mime_types, const char *extension,
2138 int ignoreGlobCase, char **match)
2141 struct xdg_mime_type *mime_type_entry;
2142 int matchLength = 0;
2146 LIST_FOR_EACH_ENTRY(mime_type_entry, native_mime_types, struct xdg_mime_type, entry)
2148 const char *glob = ignoreGlobCase ? mime_type_entry->lower_glob : mime_type_entry->glob;
2149 if (fnmatch(glob, extension, 0) == 0)
2151 if (*match == NULL || matchLength < strlen(glob))
2153 *match = mime_type_entry->mimeType;
2154 matchLength = strlen(glob);
2161 *match = strdupA(*match);
2171 static BOOL freedesktop_mime_type_for_extension(struct list *native_mime_types,
2172 const char *extensionA,
2176 WCHAR *lower_extensionW;
2178 BOOL ret = match_glob(native_mime_types, extensionA, 0, mime_type);
2179 if (ret == FALSE || *mime_type != NULL)
2181 len = strlenW(extensionW);
2182 lower_extensionW = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
2183 if (lower_extensionW)
2185 char *lower_extensionA;
2186 memcpy(lower_extensionW, extensionW, (len + 1)*sizeof(WCHAR));
2187 strlwrW(lower_extensionW);
2188 lower_extensionA = wchars_to_utf8_chars(lower_extensionW);
2189 if (lower_extensionA)
2191 ret = match_glob(native_mime_types, lower_extensionA, 1, mime_type);
2192 HeapFree(GetProcessHeap(), 0, lower_extensionA);
2197 WINE_FIXME("out of memory\n");
2199 HeapFree(GetProcessHeap(), 0, lower_extensionW);
2204 WINE_FIXME("out of memory\n");
2209 static WCHAR* reg_get_valW(HKEY key, LPCWSTR subkey, LPCWSTR name)
2212 if (RegGetValueW(key, subkey, name, RRF_RT_REG_SZ, NULL, NULL, &size) == ERROR_SUCCESS)
2214 WCHAR *ret = HeapAlloc(GetProcessHeap(), 0, size);
2217 if (RegGetValueW(key, subkey, name, RRF_RT_REG_SZ, NULL, ret, &size) == ERROR_SUCCESS)
2220 HeapFree(GetProcessHeap(), 0, ret);
2225 static CHAR* reg_get_val_utf8(HKEY key, LPCWSTR subkey, LPCWSTR name)
2227 WCHAR *valW = reg_get_valW(key, subkey, name);
2230 char *val = wchars_to_utf8_chars(valW);
2231 HeapFree(GetProcessHeap(), 0, valW);
2237 static HKEY open_associations_reg_key(void)
2239 static const WCHAR Software_Wine_FileOpenAssociationsW[] = {
2240 'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\','F','i','l','e','O','p','e','n','A','s','s','o','c','i','a','t','i','o','n','s',0};
2242 if (RegCreateKeyW(HKEY_CURRENT_USER, Software_Wine_FileOpenAssociationsW, &assocKey) == ERROR_SUCCESS)
2247 static BOOL has_association_changed(LPCWSTR extensionW, LPCSTR mimeType, LPCWSTR progId,
2248 LPCSTR appName, LPCSTR openWithIcon)
2250 static const WCHAR ProgIDW[] = {'P','r','o','g','I','D',0};
2251 static const WCHAR MimeTypeW[] = {'M','i','m','e','T','y','p','e',0};
2252 static const WCHAR AppNameW[] = {'A','p','p','N','a','m','e',0};
2253 static const WCHAR OpenWithIconW[] = {'O','p','e','n','W','i','t','h','I','c','o','n',0};
2257 if ((assocKey = open_associations_reg_key()))
2264 valueA = reg_get_val_utf8(assocKey, extensionW, MimeTypeW);
2265 if (!valueA || lstrcmpA(valueA, mimeType))
2267 HeapFree(GetProcessHeap(), 0, valueA);
2269 value = reg_get_valW(assocKey, extensionW, ProgIDW);
2270 if (!value || strcmpW(value, progId))
2272 HeapFree(GetProcessHeap(), 0, value);
2274 valueA = reg_get_val_utf8(assocKey, extensionW, AppNameW);
2275 if (!valueA || lstrcmpA(valueA, appName))
2277 HeapFree(GetProcessHeap(), 0, valueA);
2279 valueA = reg_get_val_utf8(assocKey, extensionW, OpenWithIconW);
2280 if ((openWithIcon && !valueA) ||
2281 (!openWithIcon && valueA) ||
2282 (openWithIcon && valueA && lstrcmpA(valueA, openWithIcon)))
2284 HeapFree(GetProcessHeap(), 0, valueA);
2286 RegCloseKey(assocKey);
2290 WINE_ERR("error opening associations registry key\n");
2296 static void update_association(LPCWSTR extension, LPCSTR mimeType, LPCWSTR progId,
2297 LPCSTR appName, LPCSTR desktopFile, LPCSTR openWithIcon)
2299 static const WCHAR ProgIDW[] = {'P','r','o','g','I','D',0};
2300 static const WCHAR MimeTypeW[] = {'M','i','m','e','T','y','p','e',0};
2301 static const WCHAR AppNameW[] = {'A','p','p','N','a','m','e',0};
2302 static const WCHAR DesktopFileW[] = {'D','e','s','k','t','o','p','F','i','l','e',0};
2303 static const WCHAR OpenWithIconW[] = {'O','p','e','n','W','i','t','h','I','c','o','n',0};
2304 HKEY assocKey = NULL;
2306 WCHAR *mimeTypeW = NULL;
2307 WCHAR *appNameW = NULL;
2308 WCHAR *desktopFileW = NULL;
2309 WCHAR *openWithIconW = NULL;
2311 assocKey = open_associations_reg_key();
2312 if (assocKey == NULL)
2314 WINE_ERR("could not open file associations key\n");
2318 if (RegCreateKeyW(assocKey, extension, &subkey) != ERROR_SUCCESS)
2320 WINE_ERR("could not create extension subkey\n");
2324 mimeTypeW = utf8_chars_to_wchars(mimeType);
2325 if (mimeTypeW == NULL)
2327 WINE_ERR("out of memory\n");
2331 appNameW = utf8_chars_to_wchars(appName);
2332 if (appNameW == NULL)
2334 WINE_ERR("out of memory\n");
2338 desktopFileW = utf8_chars_to_wchars(desktopFile);
2339 if (desktopFileW == NULL)
2341 WINE_ERR("out of memory\n");
2347 openWithIconW = utf8_chars_to_wchars(openWithIcon);
2348 if (openWithIconW == NULL)
2350 WINE_ERR("out of memory\n");
2355 RegSetValueExW(subkey, MimeTypeW, 0, REG_SZ, (const BYTE*) mimeTypeW, (lstrlenW(mimeTypeW) + 1) * sizeof(WCHAR));
2356 RegSetValueExW(subkey, ProgIDW, 0, REG_SZ, (const BYTE*) progId, (lstrlenW(progId) + 1) * sizeof(WCHAR));
2357 RegSetValueExW(subkey, AppNameW, 0, REG_SZ, (const BYTE*) appNameW, (lstrlenW(appNameW) + 1) * sizeof(WCHAR));
2358 RegSetValueExW(subkey, DesktopFileW, 0, REG_SZ, (const BYTE*) desktopFileW, (lstrlenW(desktopFileW) + 1) * sizeof(WCHAR));
2360 RegSetValueExW(subkey, OpenWithIconW, 0, REG_SZ, (const BYTE*) openWithIconW, (lstrlenW(openWithIconW) + 1) * sizeof(WCHAR));
2362 RegDeleteValueW(subkey, OpenWithIconW);
2365 RegCloseKey(assocKey);
2366 RegCloseKey(subkey);
2367 HeapFree(GetProcessHeap(), 0, mimeTypeW);
2368 HeapFree(GetProcessHeap(), 0, appNameW);
2369 HeapFree(GetProcessHeap(), 0, desktopFileW);
2370 HeapFree(GetProcessHeap(), 0, openWithIconW);
2373 static BOOL cleanup_associations(void)
2375 static const WCHAR openW[] = {'o','p','e','n',0};
2376 static const WCHAR DesktopFileW[] = {'D','e','s','k','t','o','p','F','i','l','e',0};
2378 BOOL hasChanged = FALSE;
2379 if ((assocKey = open_associations_reg_key()))
2385 WCHAR *extensionW = NULL;
2391 HeapFree(GetProcessHeap(), 0, extensionW);
2392 extensionW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
2393 if (extensionW == NULL)
2395 WINE_ERR("out of memory\n");
2396 ret = ERROR_OUTOFMEMORY;
2399 ret = RegEnumKeyExW(assocKey, i, extensionW, &size, NULL, NULL, NULL, NULL);
2401 } while (ret == ERROR_MORE_DATA);
2403 if (ret == ERROR_SUCCESS)
2406 command = assoc_query(ASSOCSTR_COMMAND, extensionW, openW);
2407 if (command == NULL)
2409 char *desktopFile = reg_get_val_utf8(assocKey, extensionW, DesktopFileW);
2412 WINE_TRACE("removing file type association for %s\n", wine_dbgstr_w(extensionW));
2413 remove(desktopFile);
2415 RegDeleteKeyW(assocKey, extensionW);
2417 HeapFree(GetProcessHeap(), 0, desktopFile);
2421 HeapFree(GetProcessHeap(), 0, command);
2425 if (ret != ERROR_NO_MORE_ITEMS)
2426 WINE_ERR("error %d while reading registry\n", ret);
2429 HeapFree(GetProcessHeap(), 0, extensionW);
2431 RegCloseKey(assocKey);
2434 WINE_ERR("could not open file associations key\n");
2438 static BOOL write_freedesktop_mime_type_entry(const char *packages_dir, const char *dot_extension,
2439 const char *mime_type, const char *comment)
2444 WINE_TRACE("writing MIME type %s, extension=%s, comment=%s\n", wine_dbgstr_a(mime_type),
2445 wine_dbgstr_a(dot_extension), wine_dbgstr_a(comment));
2447 filename = heap_printf("%s/x-wine-extension-%s.xml", packages_dir, &dot_extension[1]);
2450 FILE *packageFile = fopen(filename, "w");
2453 fprintf(packageFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
2454 fprintf(packageFile, "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n");
2455 fprintf(packageFile, " <mime-type type=\"");
2456 write_xml_text(packageFile, mime_type);
2457 fprintf(packageFile, "\">\n");
2458 fprintf(packageFile, " <glob pattern=\"*");
2459 write_xml_text(packageFile, dot_extension);
2460 fprintf(packageFile, "\"/>\n");
2463 fprintf(packageFile, " <comment>");
2464 write_xml_text(packageFile, comment);
2465 fprintf(packageFile, "</comment>\n");
2467 fprintf(packageFile, " </mime-type>\n");
2468 fprintf(packageFile, "</mime-info>\n");
2470 fclose(packageFile);
2473 WINE_ERR("error writing file %s\n", filename);
2474 HeapFree(GetProcessHeap(), 0, filename);
2477 WINE_ERR("out of memory\n");
2481 static BOOL is_extension_blacklisted(LPCWSTR extension)
2483 /* These are managed through external tools like wine.desktop, to evade malware created file type associations */
2484 static const WCHAR comW[] = {'.','c','o','m',0};
2485 static const WCHAR exeW[] = {'.','e','x','e',0};
2486 static const WCHAR msiW[] = {'.','m','s','i',0};
2488 if (!strcmpiW(extension, comW) ||
2489 !strcmpiW(extension, exeW) ||
2490 !strcmpiW(extension, msiW))
2495 static const char* get_special_mime_type(LPCWSTR extension)
2497 static const WCHAR lnkW[] = {'.','l','n','k',0};
2498 if (!strcmpiW(extension, lnkW))
2499 return "application/x-ms-shortcut";
2503 static BOOL write_freedesktop_association_entry(const char *desktopPath, const char *dot_extension,
2504 const char *friendlyAppName, const char *mimeType,
2505 const char *progId, const char *openWithIcon)
2510 WINE_TRACE("writing association for file type %s, friendlyAppName=%s, MIME type %s, progID=%s, icon=%s to file %s\n",
2511 wine_dbgstr_a(dot_extension), wine_dbgstr_a(friendlyAppName), wine_dbgstr_a(mimeType),
2512 wine_dbgstr_a(progId), wine_dbgstr_a(openWithIcon), wine_dbgstr_a(desktopPath));
2514 desktop = fopen(desktopPath, "w");
2517 fprintf(desktop, "[Desktop Entry]\n");
2518 fprintf(desktop, "Type=Application\n");
2519 fprintf(desktop, "Name=%s\n", friendlyAppName);
2520 fprintf(desktop, "MimeType=%s;\n", mimeType);
2521 fprintf(desktop, "Exec=env WINEPREFIX=\"%s\" wine start /ProgIDOpen %s %%f\n", wine_get_config_dir(), progId);
2522 fprintf(desktop, "NoDisplay=true\n");
2523 fprintf(desktop, "StartupNotify=true\n");
2525 fprintf(desktop, "Icon=%s\n", openWithIcon);
2530 WINE_ERR("error writing association file %s\n", wine_dbgstr_a(desktopPath));
2534 static BOOL generate_associations(const char *xdg_data_home, const char *packages_dir, const char *applications_dir)
2536 static const WCHAR openW[] = {'o','p','e','n',0};
2537 struct wine_rb_tree mimeProgidTree;
2538 struct list *nativeMimeTypes = NULL;
2541 BOOL hasChanged = FALSE;
2543 if (wine_rb_init(&mimeProgidTree, &winemenubuilder_rb_functions))
2545 WINE_ERR("wine_rb_init failed\n");
2548 if (!build_native_mime_types(xdg_data_home, &nativeMimeTypes))
2550 WINE_ERR("could not build native MIME types\n");
2556 WCHAR *extensionW = NULL;
2561 HeapFree(GetProcessHeap(), 0, extensionW);
2562 extensionW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
2563 if (extensionW == NULL)
2565 WINE_ERR("out of memory\n");
2566 ret = ERROR_OUTOFMEMORY;
2569 ret = RegEnumKeyExW(HKEY_CLASSES_ROOT, i, extensionW, &size, NULL, NULL, NULL, NULL);
2571 } while (ret == ERROR_MORE_DATA);
2573 if (ret == ERROR_SUCCESS && extensionW[0] == '.' && !is_extension_blacklisted(extensionW))
2575 char *extensionA = NULL;
2576 WCHAR *commandW = NULL;
2577 WCHAR *executableW = NULL;
2578 char *openWithIconA = NULL;
2579 WCHAR *friendlyDocNameW = NULL;
2580 char *friendlyDocNameA = NULL;
2581 WCHAR *iconW = NULL;
2583 WCHAR *contentTypeW = NULL;
2584 char *mimeTypeA = NULL;
2585 WCHAR *friendlyAppNameW = NULL;
2586 char *friendlyAppNameA = NULL;
2587 WCHAR *progIdW = NULL;
2588 char *progIdA = NULL;
2589 char *mimeProgId = NULL;
2591 extensionA = wchars_to_utf8_chars(strlwrW(extensionW));
2592 if (extensionA == NULL)
2594 WINE_ERR("out of memory\n");
2598 friendlyDocNameW = assoc_query(ASSOCSTR_FRIENDLYDOCNAME, extensionW, NULL);
2599 if (friendlyDocNameW)
2601 friendlyDocNameA = wchars_to_utf8_chars(friendlyDocNameW);
2602 if (friendlyDocNameA == NULL)
2604 WINE_ERR("out of memory\n");
2609 iconW = assoc_query(ASSOCSTR_DEFAULTICON, extensionW, NULL);
2611 contentTypeW = assoc_query(ASSOCSTR_CONTENTTYPE, extensionW, NULL);
2613 strlwrW(contentTypeW);
2615 if (!freedesktop_mime_type_for_extension(nativeMimeTypes, extensionA, extensionW, &mimeTypeA))
2618 if (mimeTypeA == NULL)
2620 if (contentTypeW != NULL && strchrW(contentTypeW, '/'))
2621 mimeTypeA = wchars_to_utf8_chars(contentTypeW);
2622 else if ((get_special_mime_type(extensionW)))
2623 mimeTypeA = strdupA(get_special_mime_type(extensionW));
2625 mimeTypeA = heap_printf("application/x-wine-extension-%s", &extensionA[1]);
2627 if (mimeTypeA != NULL)
2629 /* GNOME seems to ignore the <icon> tag in MIME packages,
2630 * and the default name is more intuitive anyway.
2634 char *flattened_mime = slashes_to_minuses(mimeTypeA);
2638 WCHAR *comma = strrchrW(iconW, ',');
2642 index = atoiW(comma + 1);
2644 iconA = extract_icon(iconW, index, flattened_mime, FALSE);
2645 HeapFree(GetProcessHeap(), 0, flattened_mime);
2649 write_freedesktop_mime_type_entry(packages_dir, extensionA, mimeTypeA, friendlyDocNameA);
2654 WINE_FIXME("out of memory\n");
2659 commandW = assoc_query(ASSOCSTR_COMMAND, extensionW, openW);
2660 if (commandW == NULL)
2661 /* no command => no application is associated */
2664 executableW = assoc_query(ASSOCSTR_EXECUTABLE, extensionW, openW);
2666 openWithIconA = extract_icon(executableW, 0, NULL, FALSE);
2668 friendlyAppNameW = assoc_query(ASSOCSTR_FRIENDLYAPPNAME, extensionW, openW);
2669 if (friendlyAppNameW)
2671 friendlyAppNameA = wchars_to_utf8_chars(friendlyAppNameW);
2672 if (friendlyAppNameA == NULL)
2674 WINE_ERR("out of memory\n");
2680 friendlyAppNameA = heap_printf("A Wine application");
2681 if (friendlyAppNameA == NULL)
2683 WINE_ERR("out of memory\n");
2688 progIdW = reg_get_valW(HKEY_CLASSES_ROOT, extensionW, NULL);
2691 progIdA = escape(progIdW);
2692 if (progIdA == NULL)
2694 WINE_ERR("out of memory\n");
2699 goto end; /* no progID => not a file type association */
2701 /* Do not allow duplicate ProgIDs for a MIME type, it causes unnecessary duplication in Open dialogs */
2702 mimeProgId = heap_printf("%s=>%s", mimeTypeA, progIdA);
2705 struct rb_string_entry *entry;
2706 if (wine_rb_get(&mimeProgidTree, mimeProgId))
2708 HeapFree(GetProcessHeap(), 0, mimeProgId);
2711 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct rb_string_entry));
2714 WINE_ERR("out of memory allocating rb_string_entry\n");
2717 entry->string = mimeProgId;
2718 if (wine_rb_put(&mimeProgidTree, mimeProgId, &entry->entry))
2720 WINE_ERR("error updating rb tree\n");
2725 if (has_association_changed(extensionW, mimeTypeA, progIdW, friendlyAppNameA, openWithIconA))
2727 char *desktopPath = heap_printf("%s/wine-extension-%s.desktop", applications_dir, &extensionA[1]);
2730 if (write_freedesktop_association_entry(desktopPath, extensionA, friendlyAppNameA, mimeTypeA, progIdA, openWithIconA))
2733 update_association(extensionW, mimeTypeA, progIdW, friendlyAppNameA, desktopPath, openWithIconA);
2735 HeapFree(GetProcessHeap(), 0, desktopPath);
2740 HeapFree(GetProcessHeap(), 0, extensionA);
2741 HeapFree(GetProcessHeap(), 0, commandW);
2742 HeapFree(GetProcessHeap(), 0, executableW);
2743 HeapFree(GetProcessHeap(), 0, openWithIconA);
2744 HeapFree(GetProcessHeap(), 0, friendlyDocNameW);
2745 HeapFree(GetProcessHeap(), 0, friendlyDocNameA);
2746 HeapFree(GetProcessHeap(), 0, iconW);
2747 HeapFree(GetProcessHeap(), 0, iconA);
2748 HeapFree(GetProcessHeap(), 0, contentTypeW);
2749 HeapFree(GetProcessHeap(), 0, mimeTypeA);
2750 HeapFree(GetProcessHeap(), 0, friendlyAppNameW);
2751 HeapFree(GetProcessHeap(), 0, friendlyAppNameA);
2752 HeapFree(GetProcessHeap(), 0, progIdW);
2753 HeapFree(GetProcessHeap(), 0, progIdA);
2755 HeapFree(GetProcessHeap(), 0, extensionW);
2756 if (ret != ERROR_SUCCESS)
2760 wine_rb_destroy(&mimeProgidTree, winemenubuilder_rb_destroy, NULL);
2761 free_native_mime_types(nativeMimeTypes);
2765 static char *get_start_exe_path(void)
2767 static const WCHAR startW[] = {'\\','c','o','m','m','a','n','d',
2768 '\\','s','t','a','r','t','.','e','x','e',0};
2769 WCHAR start_path[MAX_PATH];
2770 GetWindowsDirectoryW(start_path, MAX_PATH);
2771 lstrcatW(start_path, startW);
2772 return escape(start_path);
2775 static char* escape_unix_link_arg(LPCSTR unix_link)
2778 WCHAR *unix_linkW = utf8_chars_to_wchars(unix_link);
2781 char *escaped_lnk = escape(unix_linkW);
2784 ret = heap_printf("/Unix %s", escaped_lnk);
2785 HeapFree(GetProcessHeap(), 0, escaped_lnk);
2787 HeapFree(GetProcessHeap(), 0, unix_linkW);
2792 static BOOL InvokeShellLinker( IShellLinkW *sl, LPCWSTR link, BOOL bWait )
2794 static const WCHAR startW[] = {'\\','c','o','m','m','a','n','d',
2795 '\\','s','t','a','r','t','.','e','x','e',0};
2796 char *link_name = NULL, *icon_name = NULL, *work_dir = NULL;
2797 char *escaped_path = NULL, *escaped_args = NULL, *description = NULL;
2798 WCHAR szTmp[INFOTIPSIZE];
2799 WCHAR szDescription[INFOTIPSIZE], szPath[MAX_PATH], szWorkDir[MAX_PATH];
2800 WCHAR szArgs[INFOTIPSIZE], szIconPath[MAX_PATH];
2801 int iIconId = 0, r = -1;
2804 char *unix_link = NULL;
2805 char *start_path = NULL;
2809 WINE_ERR("Link name is null\n");
2813 if( !GetLinkLocation( link, &csidl, &link_name ) )
2815 WINE_WARN("Unknown link location %s. Ignoring.\n",wine_dbgstr_w(link));
2818 if (!in_desktop_dir(csidl) && !in_startmenu(csidl))
2820 WINE_WARN("Not under desktop or start menu. Ignoring.\n");
2823 WINE_TRACE("Link : %s\n", wine_dbgstr_a(link_name));
2826 IShellLinkW_GetWorkingDirectory( sl, szTmp, MAX_PATH );
2827 ExpandEnvironmentStringsW(szTmp, szWorkDir, MAX_PATH);
2828 WINE_TRACE("workdir : %s\n", wine_dbgstr_w(szWorkDir));
2831 IShellLinkW_GetDescription( sl, szTmp, INFOTIPSIZE );
2832 ExpandEnvironmentStringsW(szTmp, szDescription, INFOTIPSIZE);
2833 WINE_TRACE("description: %s\n", wine_dbgstr_w(szDescription));
2835 get_cmdline( sl, szTmp, MAX_PATH, szArgs, INFOTIPSIZE);
2836 ExpandEnvironmentStringsW(szTmp, szPath, MAX_PATH);
2837 WINE_TRACE("path : %s\n", wine_dbgstr_w(szPath));
2838 WINE_TRACE("args : %s\n", wine_dbgstr_w(szArgs));
2841 IShellLinkW_GetIconLocation( sl, szTmp, MAX_PATH, &iIconId );
2842 ExpandEnvironmentStringsW(szTmp, szIconPath, MAX_PATH);
2843 WINE_TRACE("icon file : %s\n", wine_dbgstr_w(szIconPath) );
2847 LPITEMIDLIST pidl = NULL;
2848 IShellLinkW_GetIDList( sl, &pidl );
2849 if( pidl && SHGetPathFromIDListW( pidl, szPath ) )
2850 WINE_TRACE("pidl path : %s\n", wine_dbgstr_w(szPath));
2853 /* extract the icon */
2855 icon_name = extract_icon( szIconPath , iIconId, NULL, bWait );
2857 icon_name = extract_icon( szPath, iIconId, NULL, bWait );
2859 /* fail - try once again after parent process exit */
2864 WINE_WARN("Unable to extract icon, deferring.\n");
2867 WINE_ERR("failed to extract icon from %s\n",
2868 wine_dbgstr_w( szIconPath[0] ? szIconPath : szPath ));
2871 unix_link = wine_get_unix_file_name(link);
2872 if (unix_link == NULL)
2874 WINE_WARN("couldn't find unix path of %s\n", wine_dbgstr_w(link));
2878 /* check the path */
2881 static const WCHAR exeW[] = {'.','e','x','e',0};
2884 /* check for .exe extension */
2885 if (!(p = strrchrW( szPath, '.' )) ||
2886 strchrW( p, '\\' ) || strchrW( p, '/' ) ||
2887 lstrcmpiW( p, exeW ))
2889 /* Not .exe - use 'start.exe' to launch this file */
2890 p = szArgs + lstrlenW(szPath) + 2;
2894 memmove( p+1, szArgs, min( (lstrlenW(szArgs) + 1) * sizeof(szArgs[0]),
2895 sizeof(szArgs) - (p + 1 - szArgs) * sizeof(szArgs[0]) ) );
2901 lstrcpyW(szArgs + 1, szPath);
2904 GetWindowsDirectoryW(szPath, MAX_PATH);
2905 lstrcatW(szPath, startW);
2908 /* convert app working dir */
2910 work_dir = wine_get_unix_file_name( szWorkDir );
2914 /* if there's no path... try run the link itself */
2915 lstrcpynW(szArgs, link, MAX_PATH);
2916 GetWindowsDirectoryW(szPath, MAX_PATH);
2917 lstrcatW(szPath, startW);
2920 /* escape the path and parameters */
2921 escaped_path = escape(szPath);
2922 escaped_args = escape(szArgs);
2923 description = wchars_to_utf8_chars(szDescription);
2924 if (escaped_path == NULL || escaped_args == NULL || description == NULL)
2926 WINE_ERR("out of memory allocating/escaping parameters\n");
2930 start_path = get_start_exe_path();
2931 if (start_path == NULL)
2933 WINE_ERR("out of memory\n");
2937 /* building multiple menus concurrently has race conditions */
2938 hsem = CreateSemaphoreA( NULL, 1, 1, "winemenubuilder_semaphore");
2939 if( WAIT_OBJECT_0 != MsgWaitForMultipleObjects( 1, &hsem, FALSE, INFINITE, QS_ALLINPUT ) )
2941 WINE_ERR("failed wait for semaphore\n");
2945 if (in_desktop_dir(csidl))
2948 const char *lastEntry;
2949 lastEntry = strrchr(link_name, '/');
2950 if (lastEntry == NULL)
2951 lastEntry = link_name;
2954 location = heap_printf("%s/%s.desktop", xdg_desktop_dir, lastEntry);
2957 if (csidl == CSIDL_COMMON_DESKTOPDIRECTORY)
2959 char *link_arg = escape_unix_link_arg(unix_link);
2962 r = !write_desktop_entry(unix_link, location, lastEntry,
2963 start_path, link_arg, description, work_dir, icon_name);
2964 HeapFree(GetProcessHeap(), 0, link_arg);
2968 r = !write_desktop_entry(NULL, location, lastEntry, escaped_path, escaped_args, description, work_dir, icon_name);
2970 chmod(location, 0755);
2971 HeapFree(GetProcessHeap(), 0, location);
2976 char *link_arg = escape_unix_link_arg(unix_link);
2979 r = !write_menu_entry(unix_link, link_name, start_path, link_arg, description, work_dir, icon_name);
2980 HeapFree(GetProcessHeap(), 0, link_arg);
2984 ReleaseSemaphore( hsem, 1, NULL );
2987 if (hsem) CloseHandle( hsem );
2988 HeapFree( GetProcessHeap(), 0, icon_name );
2989 HeapFree( GetProcessHeap(), 0, work_dir );
2990 HeapFree( GetProcessHeap(), 0, link_name );
2991 HeapFree( GetProcessHeap(), 0, escaped_args );
2992 HeapFree( GetProcessHeap(), 0, escaped_path );
2993 HeapFree( GetProcessHeap(), 0, description );
2994 HeapFree( GetProcessHeap(), 0, unix_link );
2995 HeapFree( GetProcessHeap(), 0, start_path );
2998 WINE_ERR("failed to build the menu\n" );
3003 static BOOL InvokeShellLinkerForURL( IUniformResourceLocatorW *url, LPCWSTR link, BOOL bWait )
3005 char *link_name = NULL, *icon_name = NULL;
3008 char *escaped_urlPath = NULL;
3013 char *unix_link = NULL;
3014 IPropertySetStorage *pPropSetStg;
3015 IPropertyStorage *pPropStg;
3018 char *start_path = NULL;
3019 BOOL has_icon = FALSE;
3023 WINE_ERR("Link name is null\n");
3027 if( !GetLinkLocation( link, &csidl, &link_name ) )
3029 WINE_WARN("Unknown link location %s. Ignoring.\n",wine_dbgstr_w(link));
3032 if (!in_desktop_dir(csidl) && !in_startmenu(csidl))
3034 WINE_WARN("Not under desktop or start menu. Ignoring.\n");
3038 WINE_TRACE("Link : %s\n", wine_dbgstr_a(link_name));
3040 hr = url->lpVtbl->GetURL(url, &urlPath);
3046 WINE_TRACE("path : %s\n", wine_dbgstr_w(urlPath));
3048 unix_link = wine_get_unix_file_name(link);
3049 if (unix_link == NULL)
3051 WINE_WARN("couldn't find unix path of %s\n", wine_dbgstr_w(link));
3055 escaped_urlPath = escape(urlPath);
3056 if (escaped_urlPath == NULL)
3058 WINE_ERR("couldn't escape url, out of memory\n");
3062 start_path = get_start_exe_path();
3063 if (start_path == NULL)
3065 WINE_ERR("out of memory\n");
3069 ps[0].ulKind = PRSPEC_PROPID;
3070 ps[0].u.propid = PID_IS_ICONFILE;
3071 ps[1].ulKind = PRSPEC_PROPID;
3072 ps[1].u.propid = PID_IS_ICONINDEX;
3074 hr = url->lpVtbl->QueryInterface(url, &IID_IPropertySetStorage, (void **) &pPropSetStg);
3077 hr = IPropertySetStorage_Open(pPropSetStg, &FMTID_Intshcut, STGM_READ | STGM_SHARE_EXCLUSIVE, &pPropStg);
3080 hr = IPropertyStorage_ReadMultiple(pPropStg, 2, ps, pv);
3083 if (pv[0].vt == VT_LPWSTR && pv[0].u.pwszVal && pv[0].u.pwszVal[0])
3086 icon_name = extract_icon( pv[0].u.pwszVal, pv[1].u.iVal, NULL, bWait );
3088 WINE_TRACE("URL icon path: %s icon index: %d icon name: %s\n", wine_dbgstr_w(pv[0].u.pwszVal), pv[1].u.iVal, icon_name);
3090 PropVariantClear(&pv[0]);
3091 PropVariantClear(&pv[1]);
3093 IPropertyStorage_Release(pPropStg);
3095 IPropertySetStorage_Release(pPropSetStg);
3098 /* fail - try once again after parent process exit */
3099 if( has_icon && !icon_name )
3103 WINE_WARN("Unable to extract icon, deferring.\n");
3107 WINE_ERR("failed to extract icon from %s\n",
3108 wine_dbgstr_w( pv[0].u.pwszVal ));
3111 hSem = CreateSemaphoreA( NULL, 1, 1, "winemenubuilder_semaphore");
3112 if( WAIT_OBJECT_0 != MsgWaitForMultipleObjects( 1, &hSem, FALSE, INFINITE, QS_ALLINPUT ) )
3114 WINE_ERR("failed wait for semaphore\n");
3117 if (in_desktop_dir(csidl))
3120 const char *lastEntry;
3121 lastEntry = strrchr(link_name, '/');
3122 if (lastEntry == NULL)
3123 lastEntry = link_name;
3126 location = heap_printf("%s/%s.desktop", xdg_desktop_dir, lastEntry);
3129 r = !write_desktop_entry(NULL, location, lastEntry, start_path, escaped_urlPath, NULL, NULL, icon_name);
3131 chmod(location, 0755);
3132 HeapFree(GetProcessHeap(), 0, location);
3136 r = !write_menu_entry(unix_link, link_name, start_path, escaped_urlPath, NULL, NULL, icon_name);
3138 ReleaseSemaphore(hSem, 1, NULL);
3143 HeapFree( GetProcessHeap(), 0, icon_name );
3144 HeapFree(GetProcessHeap(), 0, link_name);
3145 CoTaskMemFree( urlPath );
3146 HeapFree(GetProcessHeap(), 0, escaped_urlPath);
3147 HeapFree(GetProcessHeap(), 0, unix_link);
3151 static BOOL WaitForParentProcess( void )
3153 PROCESSENTRY32 procentry;
3154 HANDLE hsnapshot = NULL, hprocess = NULL;
3155 DWORD ourpid = GetCurrentProcessId();
3156 BOOL ret = FALSE, rc;
3158 WINE_TRACE("Waiting for parent process\n");
3159 if ((hsnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 )) ==
3160 INVALID_HANDLE_VALUE)
3162 WINE_ERR("CreateToolhelp32Snapshot failed, error %d\n", GetLastError());
3166 procentry.dwSize = sizeof(PROCESSENTRY32);
3167 rc = Process32First( hsnapshot, &procentry );
3170 if (procentry.th32ProcessID == ourpid) break;
3171 rc = Process32Next( hsnapshot, &procentry );
3175 WINE_WARN("Unable to find current process id %d when listing processes\n", ourpid);
3179 if ((hprocess = OpenProcess( SYNCHRONIZE, FALSE, procentry.th32ParentProcessID )) ==
3182 WINE_WARN("OpenProcess failed pid=%d, error %d\n", procentry.th32ParentProcessID,
3187 if (MsgWaitForMultipleObjects( 1, &hprocess, FALSE, INFINITE, QS_ALLINPUT ) == WAIT_OBJECT_0)
3190 WINE_ERR("Unable to wait for parent process, error %d\n", GetLastError());
3193 if (hprocess) CloseHandle( hprocess );
3194 if (hsnapshot) CloseHandle( hsnapshot );
3198 static BOOL Process_Link( LPCWSTR linkname, BOOL bWait )
3203 WCHAR fullname[MAX_PATH];
3206 WINE_TRACE("%s, wait %d\n", wine_dbgstr_w(linkname), bWait);
3210 WINE_ERR("link name missing\n");
3214 len=GetFullPathNameW( linkname, MAX_PATH, fullname, NULL );
3215 if (len==0 || len>MAX_PATH)
3217 WINE_ERR("couldn't get full path of link file\n");
3221 r = CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
3222 &IID_IShellLinkW, (LPVOID *) &sl );
3225 WINE_ERR("No IID_IShellLink\n");
3229 r = IShellLinkW_QueryInterface( sl, &IID_IPersistFile, (LPVOID*) &pf );
3232 WINE_ERR("No IID_IPersistFile\n");
3236 r = IPersistFile_Load( pf, fullname, STGM_READ );
3237 if( SUCCEEDED( r ) )
3239 /* If something fails (eg. Couldn't extract icon)
3240 * wait for parent process and try again
3242 if( ! InvokeShellLinker( sl, fullname, bWait ) && bWait )
3244 WaitForParentProcess();
3245 InvokeShellLinker( sl, fullname, FALSE );
3250 WINE_ERR("unable to load %s\n", wine_dbgstr_w(linkname));
3253 IPersistFile_Release( pf );
3254 IShellLinkW_Release( sl );
3259 static BOOL Process_URL( LPCWSTR urlname, BOOL bWait )
3261 IUniformResourceLocatorW *url;
3264 WCHAR fullname[MAX_PATH];
3267 WINE_TRACE("%s, wait %d\n", wine_dbgstr_w(urlname), bWait);
3271 WINE_ERR("URL name missing\n");
3275 len=GetFullPathNameW( urlname, MAX_PATH, fullname, NULL );
3276 if (len==0 || len>MAX_PATH)
3278 WINE_ERR("couldn't get full path of URL file\n");
3282 r = CoCreateInstance( &CLSID_InternetShortcut, NULL, CLSCTX_INPROC_SERVER,
3283 &IID_IUniformResourceLocatorW, (LPVOID *) &url );
3286 WINE_ERR("No IID_IUniformResourceLocatorW\n");
3290 r = url->lpVtbl->QueryInterface( url, &IID_IPersistFile, (LPVOID*) &pf );
3293 WINE_ERR("No IID_IPersistFile\n");
3296 r = IPersistFile_Load( pf, fullname, STGM_READ );
3297 if( SUCCEEDED( r ) )
3299 /* If something fails (eg. Couldn't extract icon)
3300 * wait for parent process and try again
3302 if( ! InvokeShellLinkerForURL( url, fullname, bWait ) && bWait )
3304 WaitForParentProcess();
3305 InvokeShellLinkerForURL( url, fullname, FALSE );
3309 IPersistFile_Release( pf );
3310 url->lpVtbl->Release( url );
3315 static void RefreshFileTypeAssociations(void)
3318 char *mime_dir = NULL;
3319 char *packages_dir = NULL;
3320 char *applications_dir = NULL;
3323 hSem = CreateSemaphoreA( NULL, 1, 1, "winemenubuilder_semaphore");
3324 if( WAIT_OBJECT_0 != MsgWaitForMultipleObjects( 1, &hSem, FALSE, INFINITE, QS_ALLINPUT ) )
3326 WINE_ERR("failed wait for semaphore\n");
3332 mime_dir = heap_printf("%s/mime", xdg_data_dir);
3333 if (mime_dir == NULL)
3335 WINE_ERR("out of memory\n");
3338 create_directories(mime_dir);
3340 packages_dir = heap_printf("%s/packages", mime_dir);
3341 if (packages_dir == NULL)
3343 WINE_ERR("out of memory\n");
3346 create_directories(packages_dir);
3348 applications_dir = heap_printf("%s/applications", xdg_data_dir);
3349 if (applications_dir == NULL)
3351 WINE_ERR("out of memory\n");
3354 create_directories(applications_dir);
3356 hasChanged = generate_associations(xdg_data_dir, packages_dir, applications_dir);
3357 hasChanged |= cleanup_associations();
3360 const char *argv[3];
3362 argv[0] = "update-mime-database";
3365 spawnvp( _P_DETACH, argv[0], argv );
3367 argv[0] = "update-desktop-database";
3368 argv[1] = applications_dir;
3369 spawnvp( _P_DETACH, argv[0], argv );
3375 ReleaseSemaphore(hSem, 1, NULL);
3378 HeapFree(GetProcessHeap(), 0, mime_dir);
3379 HeapFree(GetProcessHeap(), 0, packages_dir);
3380 HeapFree(GetProcessHeap(), 0, applications_dir);
3383 static void cleanup_menus(void)
3387 hkey = open_menus_reg_key();
3391 LSTATUS lret = ERROR_SUCCESS;
3392 for (i = 0; lret == ERROR_SUCCESS; )
3394 WCHAR *value = NULL;
3396 DWORD valueSize = 4096;
3397 DWORD dataSize = 4096;
3400 lret = ERROR_OUTOFMEMORY;
3401 value = HeapAlloc(GetProcessHeap(), 0, valueSize * sizeof(WCHAR));
3404 data = HeapAlloc(GetProcessHeap(), 0, dataSize * sizeof(WCHAR));
3407 lret = RegEnumValueW(hkey, i, value, &valueSize, NULL, NULL, (BYTE*)data, &dataSize);
3408 if (lret == ERROR_SUCCESS || lret != ERROR_MORE_DATA)
3412 HeapFree(GetProcessHeap(), 0, value);
3413 HeapFree(GetProcessHeap(), 0, data);
3414 value = data = NULL;
3416 if (lret == ERROR_SUCCESS)
3420 unix_file = wchars_to_unix_chars(value);
3421 windows_file = wchars_to_unix_chars(data);
3422 if (unix_file != NULL && windows_file != NULL)
3424 struct stat filestats;
3425 if (stat(windows_file, &filestats) < 0 && errno == ENOENT)
3427 WINE_TRACE("removing menu related file %s\n", unix_file);
3429 RegDeleteValueW(hkey, value);
3436 WINE_ERR("out of memory enumerating menus\n");
3437 lret = ERROR_OUTOFMEMORY;
3439 HeapFree(GetProcessHeap(), 0, unix_file);
3440 HeapFree(GetProcessHeap(), 0, windows_file);
3442 else if (lret != ERROR_NO_MORE_ITEMS)
3443 WINE_ERR("error %d reading registry\n", lret);
3444 HeapFree(GetProcessHeap(), 0, value);
3445 HeapFree(GetProcessHeap(), 0, data);
3450 WINE_ERR("error opening registry key, menu cleanup failed\n");
3453 static void thumbnail_lnk(LPCWSTR lnkPath, LPCWSTR outputPath)
3455 char *utf8lnkPath = NULL;
3456 char *utf8OutputPath = NULL;
3457 WCHAR *winLnkPath = NULL;
3458 IShellLinkW *shellLink = NULL;
3459 IPersistFile *persistFile = NULL;
3460 WCHAR szTmp[MAX_PATH];
3461 WCHAR szPath[MAX_PATH];
3462 WCHAR szArgs[INFOTIPSIZE];
3463 WCHAR szIconPath[MAX_PATH];
3465 IStream *stream = NULL;
3468 utf8lnkPath = wchars_to_utf8_chars(lnkPath);
3469 if (utf8lnkPath == NULL)
3471 WINE_ERR("out of memory converting paths\n");
3475 utf8OutputPath = wchars_to_utf8_chars(outputPath);
3476 if (utf8OutputPath == NULL)
3478 WINE_ERR("out of memory converting paths\n");
3482 winLnkPath = wine_get_dos_file_name(utf8lnkPath);
3483 if (winLnkPath == NULL)
3485 WINE_ERR("could not convert %s to DOS path\n", utf8lnkPath);
3489 hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
3490 &IID_IShellLinkW, (LPVOID*)&shellLink);
3493 WINE_ERR("could not create IShellLinkW, error 0x%08X\n", hr);
3497 hr = IShellLinkW_QueryInterface(shellLink, &IID_IPersistFile, (LPVOID)&persistFile);
3500 WINE_ERR("could not query IPersistFile, error 0x%08X\n", hr);
3504 hr = IPersistFile_Load(persistFile, winLnkPath, STGM_READ);
3507 WINE_ERR("could not read .lnk, error 0x%08X\n", hr);
3511 get_cmdline(shellLink, szTmp, MAX_PATH, szArgs, INFOTIPSIZE);
3512 ExpandEnvironmentStringsW(szTmp, szPath, MAX_PATH);
3514 IShellLinkW_GetIconLocation(shellLink, szTmp, MAX_PATH, &iconId);
3515 ExpandEnvironmentStringsW(szTmp, szIconPath, MAX_PATH);
3519 LPITEMIDLIST pidl = NULL;
3520 IShellLinkW_GetIDList(shellLink, &pidl);
3521 if (pidl && SHGetPathFromIDListW(pidl, szPath))
3522 WINE_TRACE("pidl path : %s\n", wine_dbgstr_w(szPath));
3527 hr = open_icon(szIconPath, iconId, FALSE, &stream);
3529 hr = write_native_icon(stream, utf8OutputPath, NULL);
3533 hr = open_icon(szPath, iconId, FALSE, &stream);
3535 hr = write_native_icon(stream, utf8OutputPath, NULL);
3539 HeapFree(GetProcessHeap(), 0, utf8lnkPath);
3540 HeapFree(GetProcessHeap(), 0, utf8OutputPath);
3541 HeapFree(GetProcessHeap(), 0, winLnkPath);
3542 if (shellLink != NULL)
3543 IShellLinkW_Release(shellLink);
3544 if (persistFile != NULL)
3545 IPersistFile_Release(persistFile);
3547 IStream_Release(stream);
3550 static WCHAR *next_token( LPWSTR *p )
3552 LPWSTR token = NULL, t = *p;
3557 while( t && !token )
3565 /* unquote the token */
3567 t = strchrW( token, '"' );
3576 t = strchrW( token, ' ' );
3586 static BOOL init_xdg(void)
3588 WCHAR shellDesktopPath[MAX_PATH];
3589 HRESULT hr = SHGetFolderPathW(NULL, CSIDL_DESKTOP, NULL, SHGFP_TYPE_CURRENT, shellDesktopPath);
3591 xdg_desktop_dir = wine_get_unix_file_name(shellDesktopPath);
3592 if (xdg_desktop_dir == NULL)
3594 WINE_ERR("error looking up the desktop directory\n");
3598 if (getenv("XDG_CONFIG_HOME"))
3599 xdg_config_dir = heap_printf("%s/menus/applications-merged", getenv("XDG_CONFIG_HOME"));
3601 xdg_config_dir = heap_printf("%s/.config/menus/applications-merged", getenv("HOME"));
3604 create_directories(xdg_config_dir);
3605 if (getenv("XDG_DATA_HOME"))
3606 xdg_data_dir = strdupA(getenv("XDG_DATA_HOME"));
3608 xdg_data_dir = heap_printf("%s/.local/share", getenv("HOME"));
3612 create_directories(xdg_data_dir);
3613 buffer = heap_printf("%s/desktop-directories", xdg_data_dir);
3616 mkdir(buffer, 0777);
3617 HeapFree(GetProcessHeap(), 0, buffer);
3621 HeapFree(GetProcessHeap(), 0, xdg_config_dir);
3623 WINE_ERR("out of memory\n");
3627 /***********************************************************************
3631 int PASCAL wWinMain (HINSTANCE hInstance, HINSTANCE prev, LPWSTR cmdline, int show)
3633 static const WCHAR dash_aW[] = {'-','a',0};
3634 static const WCHAR dash_rW[] = {'-','r',0};
3635 static const WCHAR dash_tW[] = {'-','t',0};
3636 static const WCHAR dash_uW[] = {'-','u',0};
3637 static const WCHAR dash_wW[] = {'-','w',0};
3639 LPWSTR token = NULL, p;
3648 hr = CoInitialize(NULL);
3651 WINE_ERR("could not initialize COM, error 0x%08X\n", hr);
3655 for( p = cmdline; p && *p; )
3657 token = next_token( &p );
3660 if( !strcmpW( token, dash_aW ) )
3662 RefreshFileTypeAssociations();
3665 if( !strcmpW( token, dash_rW ) )
3670 if( !strcmpW( token, dash_wW ) )
3672 else if ( !strcmpW( token, dash_uW ) )
3674 else if ( !strcmpW( token, dash_tW ) )
3676 WCHAR *lnkFile = next_token( &p );
3679 WCHAR *outputFile = next_token( &p );
3681 thumbnail_lnk(lnkFile, outputFile);
3684 else if( token[0] == '-' )
3686 WINE_ERR( "unknown option %s\n", wine_dbgstr_w(token) );
3693 bRet = Process_URL( token, bWait );
3695 bRet = Process_Link( token, bWait );
3698 WINE_ERR( "failed to build menu item for %s\n", wine_dbgstr_w(token) );