winemenubuilder: Add a trailing semicolon to MimeType in desktop files.
[wine.git] / programs / winemenubuilder / winemenubuilder.c
blob8414d7b9052f03d1f79da7141bd13e02b77a9877
1 /*
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.
43 * TODO
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
47 * files).
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
52 * group.
53 * Clean up fd.o menu icons and .directory files when the menu is deleted
54 * in Windows.
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
60 * one-to-one either.
61 * Wine's HKCR is broken - it doesn't merge HKCU\Software\Classes, so apps
62 * that write associations there won't associate (#17019).
65 #include "config.h"
66 #include "wine/port.h"
68 #include <ctype.h>
69 #include <stdio.h>
70 #include <string.h>
71 #ifdef HAVE_UNISTD_H
72 #include <unistd.h>
73 #endif
74 #include <errno.h>
75 #include <stdarg.h>
76 #ifdef HAVE_FNMATCH_H
77 #include <fnmatch.h>
78 #endif
80 #define COBJMACROS
82 #include <windows.h>
83 #include <shlobj.h>
84 #include <objidl.h>
85 #include <shlguid.h>
86 #include <appmgmt.h>
87 #include <tlhelp32.h>
88 #include <intshcut.h>
89 #include <shlwapi.h>
90 #include <initguid.h>
91 #include <wincodec.h>
93 #include "wine/unicode.h"
94 #include "wine/debug.h"
95 #include "wine/library.h"
96 #include "wine/list.h"
97 #include "wine/rbtree.h"
99 WINE_DEFAULT_DEBUG_CHANNEL(menubuilder);
101 #define in_desktop_dir(csidl) ((csidl)==CSIDL_DESKTOPDIRECTORY || \
102 (csidl)==CSIDL_COMMON_DESKTOPDIRECTORY)
103 #define in_startmenu(csidl) ((csidl)==CSIDL_STARTMENU || \
104 (csidl)==CSIDL_COMMON_STARTMENU)
106 /* link file formats */
108 #include "pshpack1.h"
110 typedef struct
112 BYTE bWidth;
113 BYTE bHeight;
114 BYTE bColorCount;
115 BYTE bReserved;
116 WORD wPlanes;
117 WORD wBitCount;
118 DWORD dwBytesInRes;
119 WORD nID;
120 } GRPICONDIRENTRY;
122 typedef struct
124 WORD idReserved;
125 WORD idType;
126 WORD idCount;
127 GRPICONDIRENTRY idEntries[1];
128 } GRPICONDIR;
130 typedef struct
132 BYTE bWidth;
133 BYTE bHeight;
134 BYTE bColorCount;
135 BYTE bReserved;
136 WORD wPlanes;
137 WORD wBitCount;
138 DWORD dwBytesInRes;
139 DWORD dwImageOffset;
140 } ICONDIRENTRY;
142 typedef struct
144 WORD idReserved;
145 WORD idType;
146 WORD idCount;
147 } ICONDIR;
150 #include "poppack.h"
152 typedef struct
154 HRSRC *pResInfo;
155 int nIndex;
156 } ENUMRESSTRUCT;
158 struct xdg_mime_type
160 char *mimeType;
161 char *glob;
162 struct list entry;
165 struct rb_string_entry
167 char *string;
168 struct wine_rb_entry entry;
171 DEFINE_GUID(CLSID_WICIcnsEncoder, 0x312fb6f1,0xb767,0x409d,0x8a,0x6d,0x0f,0xc1,0x54,0xd4,0xf0,0x5c);
173 static char *xdg_config_dir;
174 static char *xdg_data_dir;
175 static char *xdg_desktop_dir;
177 static WCHAR* assoc_query(ASSOCSTR assocStr, LPCWSTR name, LPCWSTR extra);
178 static HRESULT open_icon(LPCWSTR filename, int index, BOOL bWait, IStream **ppStream);
180 /* Utility routines */
181 static unsigned short crc16(const char* string)
183 unsigned short crc = 0;
184 int i, j, xor_poly;
186 for (i = 0; string[i] != 0; i++)
188 char c = string[i];
189 for (j = 0; j < 8; c >>= 1, j++)
191 xor_poly = (c ^ crc) & 1;
192 crc >>= 1;
193 if (xor_poly)
194 crc ^= 0xa001;
197 return crc;
200 static char *strdupA( const char *str )
202 char *ret;
204 if (!str) return NULL;
205 if ((ret = HeapAlloc( GetProcessHeap(), 0, strlen(str) + 1 ))) strcpy( ret, str );
206 return ret;
209 static char* heap_printf(const char *format, ...)
211 va_list args;
212 int size = 4096;
213 char *buffer, *ret;
214 int n;
216 va_start(args, format);
217 while (1)
219 buffer = HeapAlloc(GetProcessHeap(), 0, size);
220 if (buffer == NULL)
221 break;
222 n = vsnprintf(buffer, size, format, args);
223 if (n == -1)
224 size *= 2;
225 else if (n >= size)
226 size = n + 1;
227 else
228 break;
229 HeapFree(GetProcessHeap(), 0, buffer);
231 va_end(args);
232 if (!buffer) return NULL;
233 ret = HeapReAlloc(GetProcessHeap(), 0, buffer, strlen(buffer) + 1 );
234 if (!ret) ret = buffer;
235 return ret;
238 static int winemenubuilder_rb_string_compare(const void *key, const struct wine_rb_entry *entry)
240 const struct rb_string_entry *t = WINE_RB_ENTRY_VALUE(entry, const struct rb_string_entry, entry);
242 return strcmp((char*)key, t->string);
245 static void *winemenubuilder_rb_alloc(size_t size)
247 return HeapAlloc(GetProcessHeap(), 0, size);
250 static void *winemenubuilder_rb_realloc(void *ptr, size_t size)
252 return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
255 static void winemenubuilder_rb_free(void *ptr)
257 HeapFree(GetProcessHeap(), 0, ptr);
260 static void winemenubuilder_rb_destroy(struct wine_rb_entry *entry, void *context)
262 struct rb_string_entry *t = WINE_RB_ENTRY_VALUE(entry, struct rb_string_entry, entry);
263 HeapFree(GetProcessHeap(), 0, t->string);
264 HeapFree(GetProcessHeap(), 0, t);
267 static const struct wine_rb_functions winemenubuilder_rb_functions =
269 winemenubuilder_rb_alloc,
270 winemenubuilder_rb_realloc,
271 winemenubuilder_rb_free,
272 winemenubuilder_rb_string_compare,
275 static void write_xml_text(FILE *file, const char *text)
277 int i;
278 for (i = 0; text[i]; i++)
280 if (text[i] == '&')
281 fputs("&amp;", file);
282 else if (text[i] == '<')
283 fputs("&lt;", file);
284 else if (text[i] == '>')
285 fputs("&gt;", file);
286 else if (text[i] == '\'')
287 fputs("&apos;", file);
288 else if (text[i] == '"')
289 fputs("&quot;", file);
290 else
291 fputc(text[i], file);
295 static BOOL create_directories(char *directory)
297 BOOL ret = TRUE;
298 int i;
300 for (i = 0; directory[i]; i++)
302 if (i > 0 && directory[i] == '/')
304 directory[i] = 0;
305 mkdir(directory, 0777);
306 directory[i] = '/';
309 if (mkdir(directory, 0777) && errno != EEXIST)
310 ret = FALSE;
312 return ret;
315 static char* wchars_to_utf8_chars(LPCWSTR string)
317 char *ret;
318 INT size = WideCharToMultiByte(CP_UTF8, 0, string, -1, NULL, 0, NULL, NULL);
319 ret = HeapAlloc(GetProcessHeap(), 0, size);
320 if (ret)
321 WideCharToMultiByte(CP_UTF8, 0, string, -1, ret, size, NULL, NULL);
322 return ret;
325 static char* wchars_to_unix_chars(LPCWSTR string)
327 char *ret;
328 INT size = WideCharToMultiByte(CP_UNIXCP, 0, string, -1, NULL, 0, NULL, NULL);
329 ret = HeapAlloc(GetProcessHeap(), 0, size);
330 if (ret)
331 WideCharToMultiByte(CP_UNIXCP, 0, string, -1, ret, size, NULL, NULL);
332 return ret;
335 static WCHAR* utf8_chars_to_wchars(LPCSTR string)
337 WCHAR *ret;
338 INT size = MultiByteToWideChar(CP_UTF8, 0, string, -1, NULL, 0);
339 ret = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
340 if (ret)
341 MultiByteToWideChar(CP_UTF8, 0, string, -1, ret, size);
342 return ret;
345 /* Icon extraction routines
347 * FIXME: should use PrivateExtractIcons and friends
348 * FIXME: should not use stdio
351 static HRESULT convert_to_native_icon(IStream *icoFile, int *indeces, int numIndeces,
352 const CLSID *outputFormat, const char *outputFileName, LPCWSTR commentW)
354 WCHAR *dosOutputFileName = NULL;
355 IWICImagingFactory *factory = NULL;
356 IWICBitmapDecoder *decoder = NULL;
357 IWICBitmapEncoder *encoder = NULL;
358 IStream *outputFile = NULL;
359 int i;
360 HRESULT hr = E_FAIL;
362 dosOutputFileName = wine_get_dos_file_name(outputFileName);
363 if (dosOutputFileName == NULL)
365 WINE_ERR("error converting %s to DOS file name\n", outputFileName);
366 goto end;
368 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
369 &IID_IWICImagingFactory, (void**)&factory);
370 if (FAILED(hr))
372 WINE_ERR("error 0x%08X creating IWICImagingFactory\n", hr);
373 goto end;
375 hr = IWICImagingFactory_CreateDecoderFromStream(factory, icoFile, NULL,
376 WICDecodeMetadataCacheOnDemand, &decoder);
377 if (FAILED(hr))
379 WINE_ERR("error 0x%08X creating IWICBitmapDecoder\n", hr);
380 goto end;
382 hr = CoCreateInstance(outputFormat, NULL, CLSCTX_INPROC_SERVER,
383 &IID_IWICBitmapEncoder, (void**)&encoder);
384 if (FAILED(hr))
386 WINE_ERR("error 0x%08X creating bitmap encoder\n", hr);
387 goto end;
389 hr = SHCreateStreamOnFileW(dosOutputFileName, STGM_CREATE | STGM_WRITE, &outputFile);
390 if (FAILED(hr))
392 WINE_ERR("error 0x%08X creating output file\n", hr);
393 goto end;
395 hr = IWICBitmapEncoder_Initialize(encoder, outputFile, GENERIC_WRITE);
396 if (FAILED(hr))
398 WINE_ERR("error 0x%08X initializing encoder\n", hr);
399 goto end;
402 for (i = 0; i < numIndeces; i++)
404 IWICBitmapFrameDecode *sourceFrame = NULL;
405 IWICBitmapSource *sourceBitmap = NULL;
406 IWICBitmapFrameEncode *dstFrame = NULL;
407 IPropertyBag2 *options = NULL;
408 UINT width, height;
410 hr = IWICBitmapDecoder_GetFrame(decoder, indeces[i], &sourceFrame);
411 if (FAILED(hr))
413 WINE_ERR("error 0x%08X getting frame %d\n", hr, indeces[i]);
414 goto endloop;
416 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)sourceFrame, &sourceBitmap);
417 if (FAILED(hr))
419 WINE_ERR("error 0x%08X converting bitmap to 32bppBGRA\n", hr);
420 goto endloop;
422 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &dstFrame, &options);
423 if (FAILED(hr))
425 WINE_ERR("error 0x%08X creating encoder frame\n", hr);
426 goto endloop;
428 hr = IWICBitmapFrameEncode_Initialize(dstFrame, options);
429 if (FAILED(hr))
431 WINE_ERR("error 0x%08X initializing encoder frame\n", hr);
432 goto endloop;
434 hr = IWICBitmapSource_GetSize(sourceBitmap, &width, &height);
435 if (FAILED(hr))
437 WINE_ERR("error 0x%08X getting source bitmap size\n", hr);
438 goto endloop;
440 hr = IWICBitmapFrameEncode_SetSize(dstFrame, width, height);
441 if (FAILED(hr))
443 WINE_ERR("error 0x%08X setting destination bitmap size\n", hr);
444 goto endloop;
446 hr = IWICBitmapFrameEncode_SetResolution(dstFrame, 96, 96);
447 if (FAILED(hr))
449 WINE_ERR("error 0x%08X setting destination bitmap resolution\n", hr);
450 goto endloop;
452 hr = IWICBitmapFrameEncode_WriteSource(dstFrame, sourceBitmap, NULL);
453 if (FAILED(hr))
455 WINE_ERR("error 0x%08X copying bitmaps\n", hr);
456 goto endloop;
458 hr = IWICBitmapFrameEncode_Commit(dstFrame);
459 if (FAILED(hr))
461 WINE_ERR("error 0x%08X committing frame\n", hr);
462 goto endloop;
464 endloop:
465 if (sourceFrame)
466 IWICBitmapFrameDecode_Release(sourceFrame);
467 if (sourceBitmap)
468 IWICBitmapSource_Release(sourceBitmap);
469 if (dstFrame)
470 IWICBitmapFrameEncode_Release(dstFrame);
473 hr = IWICBitmapEncoder_Commit(encoder);
474 if (FAILED(hr))
476 WINE_ERR("error 0x%08X committing encoder\n", hr);
477 goto end;
480 end:
481 HeapFree(GetProcessHeap(), 0, dosOutputFileName);
482 if (factory)
483 IWICImagingFactory_Release(factory);
484 if (decoder)
485 IWICBitmapDecoder_Release(decoder);
486 if (encoder)
487 IWICBitmapEncoder_Release(encoder);
488 if (outputFile)
489 IStream_Release(outputFile);
490 return hr;
493 static IStream *add_module_icons_to_stream(HMODULE hModule, GRPICONDIR *grpIconDir)
495 int i;
496 SIZE_T iconsSize = 0;
497 BYTE *icons = NULL;
498 ICONDIRENTRY *iconDirEntries = NULL;
499 IStream *stream = NULL;
500 HRESULT hr = E_FAIL;
501 ULONG bytesWritten;
502 ICONDIR iconDir;
503 SIZE_T iconOffset;
504 int validEntries = 0;
505 LARGE_INTEGER zero;
507 for (i = 0; i < grpIconDir->idCount; i++)
508 iconsSize += grpIconDir->idEntries[i].dwBytesInRes;
509 icons = HeapAlloc(GetProcessHeap(), 0, iconsSize);
510 if (icons == NULL)
512 WINE_ERR("out of memory allocating icon\n");
513 goto end;
516 iconDirEntries = HeapAlloc(GetProcessHeap(), 0, grpIconDir->idCount*sizeof(ICONDIRENTRY));
517 if (iconDirEntries == NULL)
519 WINE_ERR("out of memory allocating icon dir entries\n");
520 goto end;
523 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
524 if (FAILED(hr))
526 WINE_ERR("error creating icon stream\n");
527 goto end;
530 iconOffset = 0;
531 for (i = 0; i < grpIconDir->idCount; i++)
533 HRSRC hResInfo;
534 LPCWSTR lpName = MAKEINTRESOURCEW(grpIconDir->idEntries[i].nID);
535 if ((hResInfo = FindResourceW(hModule, lpName, (LPCWSTR)RT_ICON)))
537 HGLOBAL hResData;
538 if ((hResData = LoadResource(hModule, hResInfo)))
540 BITMAPINFO *pIcon;
541 if ((pIcon = LockResource(hResData)))
543 iconDirEntries[validEntries].bWidth = grpIconDir->idEntries[i].bWidth;
544 iconDirEntries[validEntries].bHeight = grpIconDir->idEntries[i].bHeight;
545 iconDirEntries[validEntries].bColorCount = grpIconDir->idEntries[i].bColorCount;
546 iconDirEntries[validEntries].bReserved = grpIconDir->idEntries[i].bReserved;
547 iconDirEntries[validEntries].wPlanes = grpIconDir->idEntries[i].wPlanes;
548 iconDirEntries[validEntries].wBitCount = grpIconDir->idEntries[i].wBitCount;
549 iconDirEntries[validEntries].dwBytesInRes = grpIconDir->idEntries[i].dwBytesInRes;
550 iconDirEntries[validEntries].dwImageOffset = iconOffset;
551 validEntries++;
552 memcpy(&icons[iconOffset], pIcon, grpIconDir->idEntries[i].dwBytesInRes);
553 iconOffset += grpIconDir->idEntries[i].dwBytesInRes;
555 FreeResource(hResData);
560 if (validEntries == 0)
562 WINE_ERR("no valid icon entries\n");
563 goto end;
566 iconDir.idReserved = 0;
567 iconDir.idType = 1;
568 iconDir.idCount = validEntries;
569 hr = IStream_Write(stream, &iconDir, sizeof(iconDir), &bytesWritten);
570 if (FAILED(hr) || bytesWritten != sizeof(iconDir))
572 WINE_ERR("error 0x%08X writing icon stream\n", hr);
573 goto end;
575 for (i = 0; i < validEntries; i++)
576 iconDirEntries[i].dwImageOffset += sizeof(ICONDIR) + validEntries*sizeof(ICONDIRENTRY);
577 hr = IStream_Write(stream, iconDirEntries, validEntries*sizeof(ICONDIRENTRY), &bytesWritten);
578 if (FAILED(hr) || bytesWritten != validEntries*sizeof(ICONDIRENTRY))
580 WINE_ERR("error 0x%08X writing icon dir entries to stream\n", hr);
581 goto end;
583 hr = IStream_Write(stream, icons, iconOffset, &bytesWritten);
584 if (FAILED(hr) || bytesWritten != iconOffset)
586 WINE_ERR("error 0x%08X writing icon images to stream\n", hr);
587 goto end;
589 zero.QuadPart = 0;
590 hr = IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
592 end:
593 HeapFree(GetProcessHeap(), 0, icons);
594 HeapFree(GetProcessHeap(), 0, iconDirEntries);
595 if (FAILED(hr) && stream != NULL)
597 IStream_Release(stream);
598 stream = NULL;
600 return stream;
603 static BOOL CALLBACK EnumResNameProc(HMODULE hModule, LPCWSTR lpszType, LPWSTR lpszName, LONG_PTR lParam)
605 ENUMRESSTRUCT *sEnumRes = (ENUMRESSTRUCT *) lParam;
607 if (!sEnumRes->nIndex--)
609 *sEnumRes->pResInfo = FindResourceW(hModule, lpszName, (LPCWSTR)RT_GROUP_ICON);
610 return FALSE;
612 else
613 return TRUE;
616 static HRESULT open_module_icon(LPCWSTR szFileName, int nIndex, IStream **ppStream)
618 HMODULE hModule;
619 HRSRC hResInfo;
620 HGLOBAL hResData;
621 GRPICONDIR *pIconDir;
622 ENUMRESSTRUCT sEnumRes;
623 HRESULT hr = E_FAIL;
625 hModule = LoadLibraryExW(szFileName, 0, LOAD_LIBRARY_AS_DATAFILE);
626 if (!hModule)
628 WINE_WARN("LoadLibraryExW (%s) failed, error %d\n",
629 wine_dbgstr_w(szFileName), GetLastError());
630 return HRESULT_FROM_WIN32(GetLastError());
633 if (nIndex < 0)
635 hResInfo = FindResourceW(hModule, MAKEINTRESOURCEW(-nIndex), (LPCWSTR)RT_GROUP_ICON);
636 WINE_TRACE("FindResourceW (%s) called, return %p, error %d\n",
637 wine_dbgstr_w(szFileName), hResInfo, GetLastError());
639 else
641 hResInfo=NULL;
642 sEnumRes.pResInfo = &hResInfo;
643 sEnumRes.nIndex = nIndex;
644 if (!EnumResourceNamesW(hModule, (LPCWSTR)RT_GROUP_ICON,
645 EnumResNameProc, (LONG_PTR)&sEnumRes) &&
646 sEnumRes.nIndex != -1)
648 WINE_TRACE("EnumResourceNamesW failed, error %d\n", GetLastError());
652 if (hResInfo)
654 if ((hResData = LoadResource(hModule, hResInfo)))
656 if ((pIconDir = LockResource(hResData)))
658 *ppStream = add_module_icons_to_stream(hModule, pIconDir);
659 if (*ppStream)
660 hr = S_OK;
663 FreeResource(hResData);
666 else
668 WINE_WARN("found no icon\n");
669 FreeLibrary(hModule);
670 return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
673 FreeLibrary(hModule);
674 return hr;
677 static HRESULT read_ico_direntries(IStream *icoStream, ICONDIRENTRY **ppIconDirEntries, int *numEntries)
679 ICONDIR iconDir;
680 ULONG bytesRead;
681 HRESULT hr;
683 *ppIconDirEntries = NULL;
685 hr = IStream_Read(icoStream, &iconDir, sizeof(ICONDIR), &bytesRead);
686 if (FAILED(hr) || bytesRead != sizeof(ICONDIR) ||
687 (iconDir.idReserved != 0) || (iconDir.idType != 1))
689 WINE_WARN("Invalid ico file format (hr=0x%08X, bytesRead=%d)\n", hr, bytesRead);
690 hr = E_FAIL;
691 goto end;
693 *numEntries = iconDir.idCount;
695 if ((*ppIconDirEntries = HeapAlloc(GetProcessHeap(), 0, sizeof(ICONDIRENTRY)*iconDir.idCount)) == NULL)
697 hr = E_OUTOFMEMORY;
698 goto end;
700 hr = IStream_Read(icoStream, *ppIconDirEntries, sizeof(ICONDIRENTRY)*iconDir.idCount, &bytesRead);
701 if (FAILED(hr) || bytesRead != sizeof(ICONDIRENTRY)*iconDir.idCount)
703 if (SUCCEEDED(hr)) hr = E_FAIL;
704 goto end;
707 end:
708 if (FAILED(hr))
709 HeapFree(GetProcessHeap(), 0, *ppIconDirEntries);
710 return hr;
713 static HRESULT write_native_icon(IStream *iconStream, const char *icon_name, LPCWSTR szFileName)
715 ICONDIRENTRY *pIconDirEntry = NULL;
716 int numEntries;
717 int nMax = 0, nMaxBits = 0;
718 int nIndex = 0;
719 int i;
720 LARGE_INTEGER position;
721 HRESULT hr;
723 hr = read_ico_direntries(iconStream, &pIconDirEntry, &numEntries);
724 if (FAILED(hr))
725 goto end;
727 for (i = 0; i < numEntries; i++)
729 WINE_TRACE("[%d]: %d x %d @ %d\n", i, pIconDirEntry[i].bWidth, pIconDirEntry[i].bHeight, pIconDirEntry[i].wBitCount);
730 if (pIconDirEntry[i].wBitCount >= nMaxBits &&
731 (pIconDirEntry[i].bHeight * pIconDirEntry[i].bWidth) >= nMax)
733 nIndex = i;
734 nMax = pIconDirEntry[i].bHeight * pIconDirEntry[i].bWidth;
735 nMaxBits = pIconDirEntry[i].wBitCount;
738 WINE_TRACE("Selected: %d\n", nIndex);
740 position.QuadPart = 0;
741 hr = IStream_Seek(iconStream, position, STREAM_SEEK_SET, NULL);
742 if (FAILED(hr))
743 goto end;
744 hr = convert_to_native_icon(iconStream, &nIndex, 1, &CLSID_WICPngEncoder, icon_name, szFileName);
746 end:
747 HeapFree(GetProcessHeap(), 0, pIconDirEntry);
748 return hr;
751 static HRESULT open_file_type_icon(LPCWSTR szFileName, IStream **ppStream)
753 WCHAR *extension;
754 WCHAR *icon = NULL;
755 WCHAR *comma;
756 WCHAR *executable = NULL;
757 int index = 0;
758 char *output_path = NULL;
759 HRESULT hr = HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
761 extension = strrchrW(szFileName, '.');
762 if (extension == NULL)
763 goto end;
765 icon = assoc_query(ASSOCSTR_DEFAULTICON, extension, NULL);
766 if (icon)
768 comma = strrchrW(icon, ',');
769 if (comma)
771 *comma = 0;
772 index = atoiW(comma + 1);
774 hr = open_icon(icon, index, FALSE, ppStream);
776 else
778 executable = assoc_query(ASSOCSTR_EXECUTABLE, extension, NULL);
779 if (executable)
780 hr = open_icon(executable, 0, FALSE, ppStream);
783 end:
784 HeapFree(GetProcessHeap(), 0, icon);
785 HeapFree(GetProcessHeap(), 0, executable);
786 HeapFree(GetProcessHeap(), 0, output_path);
787 return hr;
790 static HRESULT open_default_icon(IStream **ppStream)
792 static const WCHAR user32W[] = {'u','s','e','r','3','2',0};
794 return open_module_icon(user32W, -(INT_PTR)IDI_WINLOGO, ppStream);
797 static HRESULT open_icon(LPCWSTR filename, int index, BOOL bWait, IStream **ppStream)
799 HRESULT hr;
801 hr = open_module_icon(filename, index, ppStream);
802 if (FAILED(hr))
804 static const WCHAR dot_icoW[] = {'.','i','c','o',0};
805 int len = strlenW(filename);
806 if (len >= 4 && strcmpiW(&filename[len - 4], dot_icoW) == 0)
807 hr = SHCreateStreamOnFileW(filename, STGM_READ, ppStream);
809 if (FAILED(hr))
810 hr = open_file_type_icon(filename, ppStream);
811 if (FAILED(hr) && !bWait)
812 hr = open_default_icon(ppStream);
813 return hr;
816 #ifdef __APPLE__
817 static HRESULT platform_write_icon(IStream *icoStream, int exeIndex,
818 int bestIndex, LPCWSTR icoPathW,
819 const char *destFilename, char **nativeIdentifier)
821 GUID guid;
822 WCHAR *guidStrW = NULL;
823 char *guidStrA = NULL;
824 char *icnsPath = NULL;
825 LARGE_INTEGER zero;
826 HRESULT hr;
828 hr = CoCreateGuid(&guid);
829 if (FAILED(hr))
831 WINE_WARN("CoCreateGuid failed, error 0x%08X\n", hr);
832 goto end;
834 hr = StringFromCLSID(&guid, &guidStrW);
835 if (FAILED(hr))
837 WINE_WARN("StringFromCLSID failed, error 0x%08X\n", hr);
838 goto end;
840 guidStrA = wchars_to_utf8_chars(guidStrW);
841 if (guidStrA == NULL)
843 hr = E_OUTOFMEMORY;
844 WINE_WARN("out of memory converting GUID string\n");
845 goto end;
847 icnsPath = heap_printf("/tmp/%s.icns", guidStrA);
848 if (icnsPath == NULL)
850 hr = E_OUTOFMEMORY;
851 WINE_WARN("out of memory creating ICNS path\n");
852 goto end;
854 zero.QuadPart = 0;
855 hr = IStream_Seek(icoStream, zero, STREAM_SEEK_SET, NULL);
856 if (FAILED(hr))
858 WINE_WARN("seeking icon stream failed, error 0x%08X\n", hr);
859 goto end;
861 hr = convert_to_native_icon(icoStream, &bestIndex, 1, &CLSID_WICIcnsEncoder,
862 icnsPath, icoPathW);
863 if (FAILED(hr))
865 WINE_WARN("converting %s to %s failed, error 0x%08X\n",
866 wine_dbgstr_w(icoPathW), wine_dbgstr_a(icnsPath), hr);
867 goto end;
870 end:
871 CoTaskMemFree(guidStrW);
872 HeapFree(GetProcessHeap(), 0, guidStrA);
873 if (SUCCEEDED(hr))
874 *nativeIdentifier = icnsPath;
875 else
876 HeapFree(GetProcessHeap(), 0, icnsPath);
877 return hr;
879 #else
880 static HRESULT platform_write_icon(IStream *icoStream, int exeIndex,
881 int bestIndex, LPCWSTR icoPathW,
882 const char *destFilename, char **nativeIdentifier)
884 char *icoPathA = NULL;
885 char *iconsDir = NULL;
886 char *pngPath = NULL;
887 unsigned short crc;
888 char *p, *q;
889 HRESULT hr = S_OK;
890 LARGE_INTEGER zero;
892 icoPathA = wchars_to_utf8_chars(icoPathW);
893 if (icoPathA == NULL)
895 hr = E_OUTOFMEMORY;
896 goto end;
898 crc = crc16(icoPathA);
899 p = strrchr(icoPathA, '\\');
900 if (p == NULL)
901 p = icoPathA;
902 else
904 *p = 0;
905 p++;
907 q = strrchr(p, '.');
908 if (q)
909 *q = 0;
910 if (destFilename)
911 *nativeIdentifier = heap_printf("%s", destFilename);
912 else
913 *nativeIdentifier = heap_printf("%04X_%s.%d", crc, p, exeIndex);
914 if (*nativeIdentifier == NULL)
916 hr = E_OUTOFMEMORY;
917 goto end;
919 iconsDir = heap_printf("%s/icons", xdg_data_dir);
920 if (iconsDir == NULL)
922 hr = E_OUTOFMEMORY;
923 goto end;
925 create_directories(iconsDir);
926 pngPath = heap_printf("%s/%s.png", iconsDir, *nativeIdentifier);
927 if (pngPath == NULL)
929 hr = E_OUTOFMEMORY;
930 goto end;
932 zero.QuadPart = 0;
933 hr = IStream_Seek(icoStream, zero, STREAM_SEEK_SET, NULL);
934 if (FAILED(hr))
935 goto end;
936 hr = convert_to_native_icon(icoStream, &bestIndex, 1, &CLSID_WICPngEncoder,
937 pngPath, icoPathW);
939 end:
940 HeapFree(GetProcessHeap(), 0, icoPathA);
941 HeapFree(GetProcessHeap(), 0, iconsDir);
942 HeapFree(GetProcessHeap(), 0, pngPath);
943 return hr;
945 #endif /* defined(__APPLE__) */
947 /* extract an icon from an exe or icon file; helper for IPersistFile_fnSave */
948 static char *extract_icon(LPCWSTR icoPathW, int index, const char *destFilename, BOOL bWait)
950 IStream *stream = NULL;
951 HRESULT hr;
952 char *nativeIdentifier = NULL;
953 ICONDIRENTRY *iconDirEntries = NULL;
954 int numEntries;
955 int bestIndex = -1;
956 int maxPixels = 0;
957 int maxBits = 0;
958 int i;
960 WINE_TRACE("path=[%s] index=%d destFilename=[%s]\n", wine_dbgstr_w(icoPathW), index, wine_dbgstr_a(destFilename));
962 hr = open_icon(icoPathW, index, bWait, &stream);
963 if (FAILED(hr))
965 WINE_WARN("opening icon %s index %d failed, hr=0x%08X\n", wine_dbgstr_w(icoPathW), index, hr);
966 goto end;
968 hr = read_ico_direntries(stream, &iconDirEntries, &numEntries);
969 if (FAILED(hr))
970 goto end;
971 for (i = 0; i < numEntries; i++)
973 WINE_TRACE("[%d]: %d x %d @ %d\n", i, iconDirEntries[i].bWidth,
974 iconDirEntries[i].bHeight, iconDirEntries[i].wBitCount);
975 if (iconDirEntries[i].wBitCount >= maxBits &&
976 (iconDirEntries[i].bHeight * iconDirEntries[i].bWidth) >= maxPixels)
978 bestIndex = i;
979 maxPixels = iconDirEntries[i].bHeight * iconDirEntries[i].bWidth;
980 maxBits = iconDirEntries[i].wBitCount;
983 WINE_TRACE("Selected: %d\n", bestIndex);
984 hr = platform_write_icon(stream, index, bestIndex, icoPathW, destFilename, &nativeIdentifier);
985 if (FAILED(hr))
986 WINE_WARN("writing icon failed, error 0x%08X\n", hr);
988 end:
989 if (stream)
990 IStream_Release(stream);
991 HeapFree(GetProcessHeap(), 0, iconDirEntries);
992 if (FAILED(hr))
994 HeapFree(GetProcessHeap(), 0, nativeIdentifier);
995 nativeIdentifier = NULL;
997 return nativeIdentifier;
1000 static HKEY open_menus_reg_key(void)
1002 static const WCHAR Software_Wine_FileOpenAssociationsW[] = {
1003 'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\','M','e','n','u','F','i','l','e','s',0};
1004 HKEY assocKey;
1005 DWORD ret;
1006 ret = RegCreateKeyW(HKEY_CURRENT_USER, Software_Wine_FileOpenAssociationsW, &assocKey);
1007 if (ret == ERROR_SUCCESS)
1008 return assocKey;
1009 SetLastError(ret);
1010 return NULL;
1013 static DWORD register_menus_entry(const char *unix_file, const char *windows_file)
1015 WCHAR *unix_fileW;
1016 WCHAR *windows_fileW;
1017 INT size;
1018 DWORD ret;
1020 size = MultiByteToWideChar(CP_UNIXCP, 0, unix_file, -1, NULL, 0);
1021 unix_fileW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
1022 if (unix_fileW)
1024 MultiByteToWideChar(CP_UNIXCP, 0, unix_file, -1, unix_fileW, size);
1025 size = MultiByteToWideChar(CP_UNIXCP, 0, windows_file, -1, NULL, 0);
1026 windows_fileW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
1027 if (windows_fileW)
1029 HKEY hkey;
1030 MultiByteToWideChar(CP_UNIXCP, 0, windows_file, -1, windows_fileW, size);
1031 hkey = open_menus_reg_key();
1032 if (hkey)
1034 ret = RegSetValueExW(hkey, unix_fileW, 0, REG_SZ, (const BYTE*)windows_fileW,
1035 (strlenW(windows_fileW) + 1) * sizeof(WCHAR));
1036 RegCloseKey(hkey);
1038 else
1039 ret = GetLastError();
1040 HeapFree(GetProcessHeap(), 0, windows_fileW);
1042 else
1043 ret = ERROR_NOT_ENOUGH_MEMORY;
1044 HeapFree(GetProcessHeap(), 0, unix_fileW);
1046 else
1047 ret = ERROR_NOT_ENOUGH_MEMORY;
1048 return ret;
1051 static BOOL write_desktop_entry(const char *unix_link, const char *location, const char *linkname,
1052 const char *path, const char *args, const char *descr,
1053 const char *workdir, const char *icon)
1055 FILE *file;
1057 WINE_TRACE("(%s,%s,%s,%s,%s,%s,%s,%s)\n", wine_dbgstr_a(unix_link), wine_dbgstr_a(location),
1058 wine_dbgstr_a(linkname), wine_dbgstr_a(path), wine_dbgstr_a(args),
1059 wine_dbgstr_a(descr), wine_dbgstr_a(workdir), wine_dbgstr_a(icon));
1061 file = fopen(location, "w");
1062 if (file == NULL)
1063 return FALSE;
1065 fprintf(file, "[Desktop Entry]\n");
1066 fprintf(file, "Name=%s\n", linkname);
1067 fprintf(file, "Exec=env WINEPREFIX=\"%s\" wine %s %s\n",
1068 wine_get_config_dir(), path, args);
1069 fprintf(file, "Type=Application\n");
1070 fprintf(file, "StartupNotify=true\n");
1071 if (descr && lstrlenA(descr))
1072 fprintf(file, "Comment=%s\n", descr);
1073 if (workdir && lstrlenA(workdir))
1074 fprintf(file, "Path=%s\n", workdir);
1075 if (icon && lstrlenA(icon))
1076 fprintf(file, "Icon=%s\n", icon);
1078 fclose(file);
1080 if (unix_link)
1082 DWORD ret = register_menus_entry(location, unix_link);
1083 if (ret != ERROR_SUCCESS)
1084 return FALSE;
1087 return TRUE;
1090 static BOOL write_directory_entry(const char *directory, const char *location)
1092 FILE *file;
1094 WINE_TRACE("(%s,%s)\n", wine_dbgstr_a(directory), wine_dbgstr_a(location));
1096 file = fopen(location, "w");
1097 if (file == NULL)
1098 return FALSE;
1100 fprintf(file, "[Desktop Entry]\n");
1101 fprintf(file, "Type=Directory\n");
1102 if (strcmp(directory, "wine") == 0)
1104 fprintf(file, "Name=Wine\n");
1105 fprintf(file, "Icon=wine\n");
1107 else
1109 fprintf(file, "Name=%s\n", directory);
1110 fprintf(file, "Icon=folder\n");
1113 fclose(file);
1114 return TRUE;
1117 static BOOL write_menu_file(const char *unix_link, const char *filename)
1119 char *tempfilename;
1120 FILE *tempfile = NULL;
1121 char *lastEntry;
1122 char *name = NULL;
1123 char *menuPath = NULL;
1124 int i;
1125 int count = 0;
1126 BOOL ret = FALSE;
1128 WINE_TRACE("(%s)\n", wine_dbgstr_a(filename));
1130 while (1)
1132 tempfilename = heap_printf("%s/wine-menu-XXXXXX", xdg_config_dir);
1133 if (tempfilename)
1135 int tempfd = mkstemps(tempfilename, 0);
1136 if (tempfd >= 0)
1138 tempfile = fdopen(tempfd, "w");
1139 if (tempfile)
1140 break;
1141 close(tempfd);
1142 goto end;
1144 else if (errno == EEXIST)
1146 HeapFree(GetProcessHeap(), 0, tempfilename);
1147 continue;
1149 HeapFree(GetProcessHeap(), 0, tempfilename);
1151 return FALSE;
1154 fprintf(tempfile, "<!DOCTYPE Menu PUBLIC \"-//freedesktop//DTD Menu 1.0//EN\"\n");
1155 fprintf(tempfile, "\"http://www.freedesktop.org/standards/menu-spec/menu-1.0.dtd\">\n");
1156 fprintf(tempfile, "<Menu>\n");
1157 fprintf(tempfile, " <Name>Applications</Name>\n");
1159 name = HeapAlloc(GetProcessHeap(), 0, lstrlenA(filename) + 1);
1160 if (name == NULL) goto end;
1161 lastEntry = name;
1162 for (i = 0; filename[i]; i++)
1164 name[i] = filename[i];
1165 if (filename[i] == '/')
1167 char *dir_file_name;
1168 struct stat st;
1169 name[i] = 0;
1170 fprintf(tempfile, " <Menu>\n");
1171 fprintf(tempfile, " <Name>%s", count ? "" : "wine-");
1172 write_xml_text(tempfile, name);
1173 fprintf(tempfile, "</Name>\n");
1174 fprintf(tempfile, " <Directory>%s", count ? "" : "wine-");
1175 write_xml_text(tempfile, name);
1176 fprintf(tempfile, ".directory</Directory>\n");
1177 dir_file_name = heap_printf("%s/desktop-directories/%s%s.directory",
1178 xdg_data_dir, count ? "" : "wine-", name);
1179 if (dir_file_name)
1181 if (stat(dir_file_name, &st) != 0 && errno == ENOENT)
1182 write_directory_entry(lastEntry, dir_file_name);
1183 HeapFree(GetProcessHeap(), 0, dir_file_name);
1185 name[i] = '-';
1186 lastEntry = &name[i+1];
1187 ++count;
1190 name[i] = 0;
1192 fprintf(tempfile, " <Include>\n");
1193 fprintf(tempfile, " <Filename>");
1194 write_xml_text(tempfile, name);
1195 fprintf(tempfile, "</Filename>\n");
1196 fprintf(tempfile, " </Include>\n");
1197 for (i = 0; i < count; i++)
1198 fprintf(tempfile, " </Menu>\n");
1199 fprintf(tempfile, "</Menu>\n");
1201 menuPath = heap_printf("%s/%s", xdg_config_dir, name);
1202 if (menuPath == NULL) goto end;
1203 strcpy(menuPath + strlen(menuPath) - strlen(".desktop"), ".menu");
1204 ret = TRUE;
1206 end:
1207 if (tempfile)
1208 fclose(tempfile);
1209 if (ret)
1210 ret = (rename(tempfilename, menuPath) == 0);
1211 if (!ret && tempfilename)
1212 remove(tempfilename);
1213 HeapFree(GetProcessHeap(), 0, tempfilename);
1214 if (ret)
1215 register_menus_entry(menuPath, unix_link);
1216 HeapFree(GetProcessHeap(), 0, name);
1217 HeapFree(GetProcessHeap(), 0, menuPath);
1218 return ret;
1221 static BOOL write_menu_entry(const char *unix_link, const char *link, const char *path, const char *args,
1222 const char *descr, const char *workdir, const char *icon)
1224 const char *linkname;
1225 char *desktopPath = NULL;
1226 char *desktopDir;
1227 char *filename = NULL;
1228 BOOL ret = TRUE;
1230 WINE_TRACE("(%s, %s, %s, %s, %s, %s, %s)\n", wine_dbgstr_a(unix_link), wine_dbgstr_a(link),
1231 wine_dbgstr_a(path), wine_dbgstr_a(args), wine_dbgstr_a(descr),
1232 wine_dbgstr_a(workdir), wine_dbgstr_a(icon));
1234 linkname = strrchr(link, '/');
1235 if (linkname == NULL)
1236 linkname = link;
1237 else
1238 ++linkname;
1240 desktopPath = heap_printf("%s/applications/wine/%s.desktop", xdg_data_dir, link);
1241 if (!desktopPath)
1243 WINE_WARN("out of memory creating menu entry\n");
1244 ret = FALSE;
1245 goto end;
1247 desktopDir = strrchr(desktopPath, '/');
1248 *desktopDir = 0;
1249 if (!create_directories(desktopPath))
1251 WINE_WARN("couldn't make parent directories for %s\n", wine_dbgstr_a(desktopPath));
1252 ret = FALSE;
1253 goto end;
1255 *desktopDir = '/';
1256 if (!write_desktop_entry(unix_link, desktopPath, linkname, path, args, descr, workdir, icon))
1258 WINE_WARN("couldn't make desktop entry %s\n", wine_dbgstr_a(desktopPath));
1259 ret = FALSE;
1260 goto end;
1263 filename = heap_printf("wine/%s.desktop", link);
1264 if (!filename || !write_menu_file(unix_link, filename))
1266 WINE_WARN("couldn't make menu file %s\n", wine_dbgstr_a(filename));
1267 ret = FALSE;
1270 end:
1271 HeapFree(GetProcessHeap(), 0, desktopPath);
1272 HeapFree(GetProcessHeap(), 0, filename);
1273 return ret;
1276 /* This escapes reserved characters in .desktop files' Exec keys. */
1277 static LPSTR escape(LPCWSTR arg)
1279 int i, j;
1280 WCHAR *escaped_string;
1281 char *utf8_string;
1283 escaped_string = HeapAlloc(GetProcessHeap(), 0, (4 * strlenW(arg) + 1) * sizeof(WCHAR));
1284 if (escaped_string == NULL) return NULL;
1285 for (i = j = 0; arg[i]; i++)
1287 switch (arg[i])
1289 case '\\':
1290 escaped_string[j++] = '\\';
1291 escaped_string[j++] = '\\';
1292 escaped_string[j++] = '\\';
1293 escaped_string[j++] = '\\';
1294 break;
1295 case ' ':
1296 case '\t':
1297 case '\n':
1298 case '"':
1299 case '\'':
1300 case '>':
1301 case '<':
1302 case '~':
1303 case '|':
1304 case '&':
1305 case ';':
1306 case '$':
1307 case '*':
1308 case '?':
1309 case '#':
1310 case '(':
1311 case ')':
1312 case '`':
1313 escaped_string[j++] = '\\';
1314 escaped_string[j++] = '\\';
1315 /* fall through */
1316 default:
1317 escaped_string[j++] = arg[i];
1318 break;
1321 escaped_string[j] = 0;
1323 utf8_string = wchars_to_utf8_chars(escaped_string);
1324 if (utf8_string == NULL)
1326 WINE_ERR("out of memory\n");
1327 goto end;
1330 end:
1331 HeapFree(GetProcessHeap(), 0, escaped_string);
1332 return utf8_string;
1335 /* Return a heap-allocated copy of the unix format difference between the two
1336 * Windows-format paths.
1337 * locn is the owning location
1338 * link is within locn
1340 static char *relative_path( LPCWSTR link, LPCWSTR locn )
1342 char *unix_locn, *unix_link;
1343 char *relative = NULL;
1345 unix_locn = wine_get_unix_file_name(locn);
1346 unix_link = wine_get_unix_file_name(link);
1347 if (unix_locn && unix_link)
1349 size_t len_unix_locn, len_unix_link;
1350 len_unix_locn = strlen (unix_locn);
1351 len_unix_link = strlen (unix_link);
1352 if (len_unix_locn < len_unix_link && memcmp (unix_locn, unix_link, len_unix_locn) == 0 && unix_link[len_unix_locn] == '/')
1354 size_t len_rel;
1355 char *p = strrchr (unix_link + len_unix_locn, '/');
1356 p = strrchr (p, '.');
1357 if (p)
1359 *p = '\0';
1360 len_unix_link = p - unix_link;
1362 len_rel = len_unix_link - len_unix_locn;
1363 relative = HeapAlloc(GetProcessHeap(), 0, len_rel);
1364 if (relative)
1366 memcpy (relative, unix_link + len_unix_locn + 1, len_rel);
1370 if (!relative)
1371 WINE_WARN("Could not separate the relative link path of %s in %s\n", wine_dbgstr_w(link), wine_dbgstr_w(locn));
1372 HeapFree(GetProcessHeap(), 0, unix_locn);
1373 HeapFree(GetProcessHeap(), 0, unix_link);
1374 return relative;
1377 /***********************************************************************
1379 * GetLinkLocation
1381 * returns TRUE if successful
1382 * *loc will contain CS_DESKTOPDIRECTORY, CS_STARTMENU, CS_STARTUP etc.
1383 * *relative will contain the address of a heap-allocated copy of the portion
1384 * of the filename that is within the specified location, in unix form
1386 static BOOL GetLinkLocation( LPCWSTR linkfile, DWORD *loc, char **relative )
1388 WCHAR filename[MAX_PATH], shortfilename[MAX_PATH], buffer[MAX_PATH];
1389 DWORD len, i, r, filelen;
1390 const DWORD locations[] = {
1391 CSIDL_STARTUP, CSIDL_DESKTOPDIRECTORY, CSIDL_STARTMENU,
1392 CSIDL_COMMON_STARTUP, CSIDL_COMMON_DESKTOPDIRECTORY,
1393 CSIDL_COMMON_STARTMENU };
1395 WINE_TRACE("%s\n", wine_dbgstr_w(linkfile));
1396 filelen=GetFullPathNameW( linkfile, MAX_PATH, shortfilename, NULL );
1397 if (filelen==0 || filelen>MAX_PATH)
1398 return FALSE;
1400 WINE_TRACE("%s\n", wine_dbgstr_w(shortfilename));
1402 /* the CSLU Toolkit uses a short path name when creating .lnk files;
1403 * expand or our hardcoded list won't match.
1405 filelen=GetLongPathNameW(shortfilename, filename, MAX_PATH);
1406 if (filelen==0 || filelen>MAX_PATH)
1407 return FALSE;
1409 WINE_TRACE("%s\n", wine_dbgstr_w(filename));
1411 for( i=0; i<sizeof(locations)/sizeof(locations[0]); i++ )
1413 if (!SHGetSpecialFolderPathW( 0, buffer, locations[i], FALSE ))
1414 continue;
1416 len = lstrlenW(buffer);
1417 if (len >= MAX_PATH)
1418 continue; /* We've just trashed memory! Hopefully we are OK */
1420 if (len > filelen || filename[len]!='\\')
1421 continue;
1422 /* do a lstrcmpinW */
1423 filename[len] = 0;
1424 r = lstrcmpiW( filename, buffer );
1425 filename[len] = '\\';
1426 if ( r )
1427 continue;
1429 /* return the remainder of the string and link type */
1430 *loc = locations[i];
1431 *relative = relative_path (filename, buffer);
1432 return (*relative != NULL);
1435 return FALSE;
1438 /* gets the target path directly or through MSI */
1439 static HRESULT get_cmdline( IShellLinkW *sl, LPWSTR szPath, DWORD pathSize,
1440 LPWSTR szArgs, DWORD argsSize)
1442 IShellLinkDataList *dl = NULL;
1443 EXP_DARWIN_LINK *dar = NULL;
1444 HRESULT hr;
1446 szPath[0] = 0;
1447 szArgs[0] = 0;
1449 hr = IShellLinkW_GetPath( sl, szPath, pathSize, NULL, SLGP_RAWPATH );
1450 if (hr == S_OK && szPath[0])
1452 IShellLinkW_GetArguments( sl, szArgs, argsSize );
1453 return hr;
1456 hr = IShellLinkW_QueryInterface( sl, &IID_IShellLinkDataList, (LPVOID*) &dl );
1457 if (FAILED(hr))
1458 return hr;
1460 hr = IShellLinkDataList_CopyDataBlock( dl, EXP_DARWIN_ID_SIG, (LPVOID*) &dar );
1461 if (SUCCEEDED(hr))
1463 WCHAR* szCmdline;
1464 DWORD cmdSize;
1466 cmdSize=0;
1467 hr = CommandLineFromMsiDescriptor( dar->szwDarwinID, NULL, &cmdSize );
1468 if (hr == ERROR_SUCCESS)
1470 cmdSize++;
1471 szCmdline = HeapAlloc( GetProcessHeap(), 0, cmdSize*sizeof(WCHAR) );
1472 hr = CommandLineFromMsiDescriptor( dar->szwDarwinID, szCmdline, &cmdSize );
1473 WINE_TRACE(" command : %s\n", wine_dbgstr_w(szCmdline));
1474 if (hr == ERROR_SUCCESS)
1476 WCHAR *s, *d;
1477 int bcount, in_quotes;
1479 /* Extract the application path */
1480 bcount=0;
1481 in_quotes=0;
1482 s=szCmdline;
1483 d=szPath;
1484 while (*s)
1486 if ((*s==0x0009 || *s==0x0020) && !in_quotes)
1488 /* skip the remaining spaces */
1489 do {
1490 s++;
1491 } while (*s==0x0009 || *s==0x0020);
1492 break;
1494 else if (*s==0x005c)
1496 /* '\\' */
1497 *d++=*s++;
1498 bcount++;
1500 else if (*s==0x0022)
1502 /* '"' */
1503 if ((bcount & 1)==0)
1505 /* Preceded by an even number of '\', this is
1506 * half that number of '\', plus a quote which
1507 * we erase.
1509 d-=bcount/2;
1510 in_quotes=!in_quotes;
1511 s++;
1513 else
1515 /* Preceded by an odd number of '\', this is
1516 * half that number of '\' followed by a '"'
1518 d=d-bcount/2-1;
1519 *d++='"';
1520 s++;
1522 bcount=0;
1524 else
1526 /* a regular character */
1527 *d++=*s++;
1528 bcount=0;
1530 if ((d-szPath) == pathSize)
1532 /* Keep processing the path till we get to the
1533 * arguments, but 'stand still'
1535 d--;
1538 /* Close the application path */
1539 *d=0;
1541 lstrcpynW(szArgs, s, argsSize);
1543 HeapFree( GetProcessHeap(), 0, szCmdline );
1545 LocalFree( dar );
1548 IShellLinkDataList_Release( dl );
1549 return hr;
1552 static WCHAR* assoc_query(ASSOCSTR assocStr, LPCWSTR name, LPCWSTR extra)
1554 HRESULT hr;
1555 WCHAR *value = NULL;
1556 DWORD size = 0;
1557 hr = AssocQueryStringW(0, assocStr, name, extra, NULL, &size);
1558 if (SUCCEEDED(hr))
1560 value = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
1561 if (value)
1563 hr = AssocQueryStringW(0, assocStr, name, extra, value, &size);
1564 if (FAILED(hr))
1566 HeapFree(GetProcessHeap(), 0, value);
1567 value = NULL;
1571 return value;
1574 static char *slashes_to_minuses(const char *string)
1576 int i;
1577 char *ret = HeapAlloc(GetProcessHeap(), 0, lstrlenA(string) + 1);
1578 if (ret)
1580 for (i = 0; string[i]; i++)
1582 if (string[i] == '/')
1583 ret[i] = '-';
1584 else
1585 ret[i] = string[i];
1587 ret[i] = 0;
1588 return ret;
1590 return NULL;
1593 static BOOL next_line(FILE *file, char **line, int *size)
1595 int pos = 0;
1596 char *cr;
1597 if (*line == NULL)
1599 *size = 4096;
1600 *line = HeapAlloc(GetProcessHeap(), 0, *size);
1602 while (*line != NULL)
1604 if (fgets(&(*line)[pos], *size - pos, file) == NULL)
1606 HeapFree(GetProcessHeap(), 0, *line);
1607 *line = NULL;
1608 if (feof(file))
1609 return TRUE;
1610 return FALSE;
1612 pos = strlen(*line);
1613 cr = strchr(*line, '\n');
1614 if (cr == NULL)
1616 char *line2;
1617 (*size) *= 2;
1618 line2 = HeapReAlloc(GetProcessHeap(), 0, *line, *size);
1619 if (line2)
1620 *line = line2;
1621 else
1623 HeapFree(GetProcessHeap(), 0, *line);
1624 *line = NULL;
1627 else
1629 *cr = 0;
1630 return TRUE;
1633 return FALSE;
1636 static BOOL add_mimes(const char *xdg_data_dir, struct list *mime_types)
1638 char *globs_filename = NULL;
1639 BOOL ret = TRUE;
1640 globs_filename = heap_printf("%s/mime/globs", xdg_data_dir);
1641 if (globs_filename)
1643 FILE *globs_file = fopen(globs_filename, "r");
1644 if (globs_file) /* doesn't have to exist */
1646 char *line = NULL;
1647 int size = 0;
1648 while (ret && (ret = next_line(globs_file, &line, &size)) && line)
1650 char *pos;
1651 struct xdg_mime_type *mime_type_entry = NULL;
1652 if (line[0] != '#' && (pos = strchr(line, ':')))
1654 mime_type_entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct xdg_mime_type));
1655 if (mime_type_entry)
1657 *pos = 0;
1658 mime_type_entry->mimeType = strdupA(line);
1659 mime_type_entry->glob = strdupA(pos + 1);
1660 if (mime_type_entry->mimeType && mime_type_entry->glob)
1661 list_add_tail(mime_types, &mime_type_entry->entry);
1662 else
1664 HeapFree(GetProcessHeap(), 0, mime_type_entry->mimeType);
1665 HeapFree(GetProcessHeap(), 0, mime_type_entry->glob);
1666 HeapFree(GetProcessHeap(), 0, mime_type_entry);
1667 ret = FALSE;
1670 else
1671 ret = FALSE;
1674 HeapFree(GetProcessHeap(), 0, line);
1675 fclose(globs_file);
1677 HeapFree(GetProcessHeap(), 0, globs_filename);
1679 else
1680 ret = FALSE;
1681 return ret;
1684 static void free_native_mime_types(struct list *native_mime_types)
1686 struct xdg_mime_type *mime_type_entry, *mime_type_entry2;
1688 LIST_FOR_EACH_ENTRY_SAFE(mime_type_entry, mime_type_entry2, native_mime_types, struct xdg_mime_type, entry)
1690 list_remove(&mime_type_entry->entry);
1691 HeapFree(GetProcessHeap(), 0, mime_type_entry->glob);
1692 HeapFree(GetProcessHeap(), 0, mime_type_entry->mimeType);
1693 HeapFree(GetProcessHeap(), 0, mime_type_entry);
1695 HeapFree(GetProcessHeap(), 0, native_mime_types);
1698 static BOOL build_native_mime_types(const char *xdg_data_home, struct list **mime_types)
1700 char *xdg_data_dirs;
1701 BOOL ret;
1703 *mime_types = NULL;
1705 xdg_data_dirs = getenv("XDG_DATA_DIRS");
1706 if (xdg_data_dirs == NULL)
1707 xdg_data_dirs = heap_printf("/usr/local/share/:/usr/share/");
1708 else
1709 xdg_data_dirs = strdupA(xdg_data_dirs);
1711 if (xdg_data_dirs)
1713 *mime_types = HeapAlloc(GetProcessHeap(), 0, sizeof(struct list));
1714 if (*mime_types)
1716 const char *begin;
1717 char *end;
1719 list_init(*mime_types);
1720 ret = add_mimes(xdg_data_home, *mime_types);
1721 if (ret)
1723 for (begin = xdg_data_dirs; (end = strchr(begin, ':')); begin = end + 1)
1725 *end = '\0';
1726 ret = add_mimes(begin, *mime_types);
1727 *end = ':';
1728 if (!ret)
1729 break;
1731 if (ret)
1732 ret = add_mimes(begin, *mime_types);
1735 else
1736 ret = FALSE;
1737 HeapFree(GetProcessHeap(), 0, xdg_data_dirs);
1739 else
1740 ret = FALSE;
1741 if (!ret && *mime_types)
1743 free_native_mime_types(*mime_types);
1744 *mime_types = NULL;
1746 return ret;
1749 static BOOL match_glob(struct list *native_mime_types, const char *extension,
1750 char **match)
1752 #ifdef HAVE_FNMATCH
1753 struct xdg_mime_type *mime_type_entry;
1754 int matchLength = 0;
1756 *match = NULL;
1758 LIST_FOR_EACH_ENTRY(mime_type_entry, native_mime_types, struct xdg_mime_type, entry)
1760 if (fnmatch(mime_type_entry->glob, extension, 0) == 0)
1762 if (*match == NULL || matchLength < strlen(mime_type_entry->glob))
1764 *match = mime_type_entry->mimeType;
1765 matchLength = strlen(mime_type_entry->glob);
1770 if (*match != NULL)
1772 *match = strdupA(*match);
1773 if (*match == NULL)
1774 return FALSE;
1776 #else
1777 *match = NULL;
1778 #endif
1779 return TRUE;
1782 static BOOL freedesktop_mime_type_for_extension(struct list *native_mime_types,
1783 const char *extensionA,
1784 LPCWSTR extensionW,
1785 char **mime_type)
1787 WCHAR *lower_extensionW;
1788 INT len;
1789 BOOL ret = match_glob(native_mime_types, extensionA, mime_type);
1790 if (ret == FALSE || *mime_type != NULL)
1791 return ret;
1792 len = strlenW(extensionW);
1793 lower_extensionW = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
1794 if (lower_extensionW)
1796 char *lower_extensionA;
1797 memcpy(lower_extensionW, extensionW, (len + 1)*sizeof(WCHAR));
1798 strlwrW(lower_extensionW);
1799 lower_extensionA = wchars_to_utf8_chars(lower_extensionW);
1800 if (lower_extensionA)
1802 ret = match_glob(native_mime_types, lower_extensionA, mime_type);
1803 HeapFree(GetProcessHeap(), 0, lower_extensionA);
1805 else
1807 ret = FALSE;
1808 WINE_FIXME("out of memory\n");
1810 HeapFree(GetProcessHeap(), 0, lower_extensionW);
1812 else
1814 ret = FALSE;
1815 WINE_FIXME("out of memory\n");
1817 return ret;
1820 static WCHAR* reg_get_valW(HKEY key, LPCWSTR subkey, LPCWSTR name)
1822 DWORD size;
1823 if (RegGetValueW(key, subkey, name, RRF_RT_REG_SZ, NULL, NULL, &size) == ERROR_SUCCESS)
1825 WCHAR *ret = HeapAlloc(GetProcessHeap(), 0, size);
1826 if (ret)
1828 if (RegGetValueW(key, subkey, name, RRF_RT_REG_SZ, NULL, ret, &size) == ERROR_SUCCESS)
1829 return ret;
1831 HeapFree(GetProcessHeap(), 0, ret);
1833 return NULL;
1836 static CHAR* reg_get_val_utf8(HKEY key, LPCWSTR subkey, LPCWSTR name)
1838 WCHAR *valW = reg_get_valW(key, subkey, name);
1839 if (valW)
1841 char *val = wchars_to_utf8_chars(valW);
1842 HeapFree(GetProcessHeap(), 0, valW);
1843 return val;
1845 return NULL;
1848 static HKEY open_associations_reg_key(void)
1850 static const WCHAR Software_Wine_FileOpenAssociationsW[] = {
1851 '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};
1852 HKEY assocKey;
1853 if (RegCreateKeyW(HKEY_CURRENT_USER, Software_Wine_FileOpenAssociationsW, &assocKey) == ERROR_SUCCESS)
1854 return assocKey;
1855 return NULL;
1858 static BOOL has_association_changed(LPCWSTR extensionW, LPCSTR mimeType, LPCWSTR progId,
1859 LPCSTR appName, LPCWSTR docName, LPCSTR openWithIcon)
1861 static const WCHAR ProgIDW[] = {'P','r','o','g','I','D',0};
1862 static const WCHAR DocNameW[] = {'D','o','c','N','a','m','e',0};
1863 static const WCHAR MimeTypeW[] = {'M','i','m','e','T','y','p','e',0};
1864 static const WCHAR AppNameW[] = {'A','p','p','N','a','m','e',0};
1865 static const WCHAR OpenWithIconW[] = {'O','p','e','n','W','i','t','h','I','c','o','n',0};
1866 HKEY assocKey;
1867 BOOL ret;
1869 if ((assocKey = open_associations_reg_key()))
1871 CHAR *valueA;
1872 WCHAR *value;
1874 ret = FALSE;
1876 valueA = reg_get_val_utf8(assocKey, extensionW, MimeTypeW);
1877 if (!valueA || lstrcmpA(valueA, mimeType))
1878 ret = TRUE;
1879 HeapFree(GetProcessHeap(), 0, valueA);
1881 value = reg_get_valW(assocKey, extensionW, ProgIDW);
1882 if (!value || strcmpW(value, progId))
1883 ret = TRUE;
1884 HeapFree(GetProcessHeap(), 0, value);
1886 valueA = reg_get_val_utf8(assocKey, extensionW, AppNameW);
1887 if (!valueA || lstrcmpA(valueA, appName))
1888 ret = TRUE;
1889 HeapFree(GetProcessHeap(), 0, valueA);
1891 value = reg_get_valW(assocKey, extensionW, DocNameW);
1892 if (docName && (!value || strcmpW(value, docName)))
1893 ret = TRUE;
1894 HeapFree(GetProcessHeap(), 0, value);
1896 valueA = reg_get_val_utf8(assocKey, extensionW, OpenWithIconW);
1897 if ((openWithIcon && !valueA) ||
1898 (!openWithIcon && valueA) ||
1899 (openWithIcon && valueA && lstrcmpA(valueA, openWithIcon)))
1900 ret = TRUE;
1901 HeapFree(GetProcessHeap(), 0, valueA);
1903 RegCloseKey(assocKey);
1905 else
1907 WINE_ERR("error opening associations registry key\n");
1908 ret = FALSE;
1910 return ret;
1913 static void update_association(LPCWSTR extension, LPCSTR mimeType, LPCWSTR progId,
1914 LPCSTR appName, LPCWSTR docName, LPCSTR desktopFile, LPCSTR openWithIcon)
1916 static const WCHAR ProgIDW[] = {'P','r','o','g','I','D',0};
1917 static const WCHAR DocNameW[] = {'D','o','c','N','a','m','e',0};
1918 static const WCHAR MimeTypeW[] = {'M','i','m','e','T','y','p','e',0};
1919 static const WCHAR AppNameW[] = {'A','p','p','N','a','m','e',0};
1920 static const WCHAR DesktopFileW[] = {'D','e','s','k','t','o','p','F','i','l','e',0};
1921 static const WCHAR OpenWithIconW[] = {'O','p','e','n','W','i','t','h','I','c','o','n',0};
1922 HKEY assocKey = NULL;
1923 HKEY subkey = NULL;
1924 WCHAR *mimeTypeW = NULL;
1925 WCHAR *appNameW = NULL;
1926 WCHAR *desktopFileW = NULL;
1927 WCHAR *openWithIconW = NULL;
1929 assocKey = open_associations_reg_key();
1930 if (assocKey == NULL)
1932 WINE_ERR("could not open file associations key\n");
1933 goto done;
1936 if (RegCreateKeyW(assocKey, extension, &subkey) != ERROR_SUCCESS)
1938 WINE_ERR("could not create extension subkey\n");
1939 goto done;
1942 mimeTypeW = utf8_chars_to_wchars(mimeType);
1943 if (mimeTypeW == NULL)
1945 WINE_ERR("out of memory\n");
1946 goto done;
1949 appNameW = utf8_chars_to_wchars(appName);
1950 if (appNameW == NULL)
1952 WINE_ERR("out of memory\n");
1953 goto done;
1956 desktopFileW = utf8_chars_to_wchars(desktopFile);
1957 if (desktopFileW == NULL)
1959 WINE_ERR("out of memory\n");
1960 goto done;
1963 if (openWithIcon)
1965 openWithIconW = utf8_chars_to_wchars(openWithIcon);
1966 if (openWithIconW == NULL)
1968 WINE_ERR("out of memory\n");
1969 goto done;
1973 RegSetValueExW(subkey, MimeTypeW, 0, REG_SZ, (const BYTE*) mimeTypeW, (lstrlenW(mimeTypeW) + 1) * sizeof(WCHAR));
1974 RegSetValueExW(subkey, ProgIDW, 0, REG_SZ, (const BYTE*) progId, (lstrlenW(progId) + 1) * sizeof(WCHAR));
1975 RegSetValueExW(subkey, AppNameW, 0, REG_SZ, (const BYTE*) appNameW, (lstrlenW(appNameW) + 1) * sizeof(WCHAR));
1976 if (docName)
1977 RegSetValueExW(subkey, DocNameW, 0, REG_SZ, (const BYTE*) docName, (lstrlenW(docName) + 1) * sizeof(WCHAR));
1978 RegSetValueExW(subkey, DesktopFileW, 0, REG_SZ, (const BYTE*) desktopFileW, (lstrlenW(desktopFileW) + 1) * sizeof(WCHAR));
1979 if (openWithIcon)
1980 RegSetValueExW(subkey, OpenWithIconW, 0, REG_SZ, (const BYTE*) openWithIconW, (lstrlenW(openWithIconW) + 1) * sizeof(WCHAR));
1981 else
1982 RegDeleteValueW(subkey, OpenWithIconW);
1984 done:
1985 RegCloseKey(assocKey);
1986 RegCloseKey(subkey);
1987 HeapFree(GetProcessHeap(), 0, mimeTypeW);
1988 HeapFree(GetProcessHeap(), 0, appNameW);
1989 HeapFree(GetProcessHeap(), 0, desktopFileW);
1990 HeapFree(GetProcessHeap(), 0, openWithIconW);
1993 static BOOL cleanup_associations(void)
1995 static const WCHAR openW[] = {'o','p','e','n',0};
1996 static const WCHAR DesktopFileW[] = {'D','e','s','k','t','o','p','F','i','l','e',0};
1997 HKEY assocKey;
1998 BOOL hasChanged = FALSE;
1999 if ((assocKey = open_associations_reg_key()))
2001 int i;
2002 BOOL done = FALSE;
2003 for (i = 0; !done; i++)
2005 WCHAR *extensionW = NULL;
2006 DWORD size = 1024;
2007 LSTATUS ret;
2011 HeapFree(GetProcessHeap(), 0, extensionW);
2012 extensionW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
2013 if (extensionW == NULL)
2015 WINE_ERR("out of memory\n");
2016 ret = ERROR_OUTOFMEMORY;
2017 break;
2019 ret = RegEnumKeyExW(assocKey, i, extensionW, &size, NULL, NULL, NULL, NULL);
2020 size *= 2;
2021 } while (ret == ERROR_MORE_DATA);
2023 if (ret == ERROR_SUCCESS)
2025 WCHAR *command;
2026 command = assoc_query(ASSOCSTR_COMMAND, extensionW, openW);
2027 if (command == NULL)
2029 char *desktopFile = reg_get_val_utf8(assocKey, extensionW, DesktopFileW);
2030 if (desktopFile)
2032 WINE_TRACE("removing file type association for %s\n", wine_dbgstr_w(extensionW));
2033 remove(desktopFile);
2035 RegDeleteKeyW(assocKey, extensionW);
2036 hasChanged = TRUE;
2037 HeapFree(GetProcessHeap(), 0, desktopFile);
2039 HeapFree(GetProcessHeap(), 0, command);
2041 else
2043 if (ret != ERROR_NO_MORE_ITEMS)
2044 WINE_ERR("error %d while reading registry\n", ret);
2045 done = TRUE;
2047 HeapFree(GetProcessHeap(), 0, extensionW);
2049 RegCloseKey(assocKey);
2051 else
2052 WINE_ERR("could not open file associations key\n");
2053 return hasChanged;
2056 static BOOL write_freedesktop_mime_type_entry(const char *packages_dir, const char *dot_extension,
2057 const char *mime_type, const char *comment)
2059 BOOL ret = FALSE;
2060 char *filename;
2062 WINE_TRACE("writing MIME type %s, extension=%s, comment=%s\n", wine_dbgstr_a(mime_type),
2063 wine_dbgstr_a(dot_extension), wine_dbgstr_a(comment));
2065 filename = heap_printf("%s/x-wine-extension-%s.xml", packages_dir, &dot_extension[1]);
2066 if (filename)
2068 FILE *packageFile = fopen(filename, "w");
2069 if (packageFile)
2071 fprintf(packageFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
2072 fprintf(packageFile, "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n");
2073 fprintf(packageFile, " <mime-type type=\"");
2074 write_xml_text(packageFile, mime_type);
2075 fprintf(packageFile, "\">\n");
2076 fprintf(packageFile, " <glob pattern=\"*");
2077 write_xml_text(packageFile, dot_extension);
2078 fprintf(packageFile, "\"/>\n");
2079 if (comment)
2081 fprintf(packageFile, " <comment>");
2082 write_xml_text(packageFile, comment);
2083 fprintf(packageFile, "</comment>\n");
2085 fprintf(packageFile, " </mime-type>\n");
2086 fprintf(packageFile, "</mime-info>\n");
2087 ret = TRUE;
2088 fclose(packageFile);
2090 else
2091 WINE_ERR("error writing file %s\n", filename);
2092 HeapFree(GetProcessHeap(), 0, filename);
2094 else
2095 WINE_ERR("out of memory\n");
2096 return ret;
2099 static BOOL is_extension_blacklisted(LPCWSTR extension)
2101 /* These are managed through external tools like wine.desktop, to evade malware created file type associations */
2102 static const WCHAR comW[] = {'.','c','o','m',0};
2103 static const WCHAR exeW[] = {'.','e','x','e',0};
2104 static const WCHAR msiW[] = {'.','m','s','i',0};
2106 if (!strcmpiW(extension, comW) ||
2107 !strcmpiW(extension, exeW) ||
2108 !strcmpiW(extension, msiW))
2109 return TRUE;
2110 return FALSE;
2113 static const char* get_special_mime_type(LPCWSTR extension)
2115 static const WCHAR lnkW[] = {'.','l','n','k',0};
2116 if (!strcmpiW(extension, lnkW))
2117 return "application/x-ms-shortcut";
2118 return NULL;
2121 static BOOL write_freedesktop_association_entry(const char *desktopPath, const char *dot_extension,
2122 const char *friendlyAppName, const char *mimeType,
2123 const char *progId, const char *openWithIcon)
2125 BOOL ret = FALSE;
2126 FILE *desktop;
2128 WINE_TRACE("writing association for file type %s, friendlyAppName=%s, MIME type %s, progID=%s, icon=%s to file %s\n",
2129 wine_dbgstr_a(dot_extension), wine_dbgstr_a(friendlyAppName), wine_dbgstr_a(mimeType),
2130 wine_dbgstr_a(progId), wine_dbgstr_a(openWithIcon), wine_dbgstr_a(desktopPath));
2132 desktop = fopen(desktopPath, "w");
2133 if (desktop)
2135 fprintf(desktop, "[Desktop Entry]\n");
2136 fprintf(desktop, "Type=Application\n");
2137 fprintf(desktop, "Name=%s\n", friendlyAppName);
2138 fprintf(desktop, "MimeType=%s;\n", mimeType);
2139 fprintf(desktop, "Exec=wine start /ProgIDOpen %s %%f\n", progId);
2140 fprintf(desktop, "NoDisplay=true\n");
2141 fprintf(desktop, "StartupNotify=true\n");
2142 if (openWithIcon)
2143 fprintf(desktop, "Icon=%s\n", openWithIcon);
2144 ret = TRUE;
2145 fclose(desktop);
2147 else
2148 WINE_ERR("error writing association file %s\n", wine_dbgstr_a(desktopPath));
2149 return ret;
2152 static BOOL generate_associations(const char *xdg_data_home, const char *packages_dir, const char *applications_dir)
2154 static const WCHAR openW[] = {'o','p','e','n',0};
2155 struct wine_rb_tree mimeProgidTree;
2156 struct list *nativeMimeTypes = NULL;
2157 LSTATUS ret = 0;
2158 int i;
2159 BOOL hasChanged = FALSE;
2161 if (wine_rb_init(&mimeProgidTree, &winemenubuilder_rb_functions))
2163 WINE_ERR("wine_rb_init failed\n");
2164 return FALSE;
2166 if (!build_native_mime_types(xdg_data_home, &nativeMimeTypes))
2168 WINE_ERR("could not build native MIME types\n");
2169 return FALSE;
2172 for (i = 0; ; i++)
2174 WCHAR *extensionW = NULL;
2175 DWORD size = 1024;
2179 HeapFree(GetProcessHeap(), 0, extensionW);
2180 extensionW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
2181 if (extensionW == NULL)
2183 WINE_ERR("out of memory\n");
2184 ret = ERROR_OUTOFMEMORY;
2185 break;
2187 ret = RegEnumKeyExW(HKEY_CLASSES_ROOT, i, extensionW, &size, NULL, NULL, NULL, NULL);
2188 size *= 2;
2189 } while (ret == ERROR_MORE_DATA);
2191 if (ret == ERROR_SUCCESS && extensionW[0] == '.' && !is_extension_blacklisted(extensionW))
2193 char *extensionA = NULL;
2194 WCHAR *commandW = NULL;
2195 WCHAR *executableW = NULL;
2196 char *openWithIconA = NULL;
2197 WCHAR *friendlyDocNameW = NULL;
2198 char *friendlyDocNameA = NULL;
2199 WCHAR *iconW = NULL;
2200 char *iconA = NULL;
2201 WCHAR *contentTypeW = NULL;
2202 char *mimeTypeA = NULL;
2203 WCHAR *friendlyAppNameW = NULL;
2204 char *friendlyAppNameA = NULL;
2205 WCHAR *progIdW = NULL;
2206 char *progIdA = NULL;
2207 char *mimeProgId = NULL;
2209 extensionA = wchars_to_utf8_chars(extensionW);
2210 if (extensionA == NULL)
2212 WINE_ERR("out of memory\n");
2213 goto end;
2216 friendlyDocNameW = assoc_query(ASSOCSTR_FRIENDLYDOCNAME, extensionW, NULL);
2217 if (friendlyDocNameW)
2219 friendlyDocNameA = wchars_to_utf8_chars(friendlyDocNameW);
2220 if (friendlyDocNameA == NULL)
2222 WINE_ERR("out of memory\n");
2223 goto end;
2227 iconW = assoc_query(ASSOCSTR_DEFAULTICON, extensionW, NULL);
2229 contentTypeW = assoc_query(ASSOCSTR_CONTENTTYPE, extensionW, NULL);
2230 if (contentTypeW)
2231 strlwrW(contentTypeW);
2233 if (!freedesktop_mime_type_for_extension(nativeMimeTypes, extensionA, extensionW, &mimeTypeA))
2234 goto end;
2236 if (mimeTypeA == NULL)
2238 if (contentTypeW != NULL && strchrW(contentTypeW, '/'))
2239 mimeTypeA = wchars_to_utf8_chars(contentTypeW);
2240 else if ((get_special_mime_type(extensionW)))
2241 mimeTypeA = strdupA(get_special_mime_type(extensionW));
2242 else
2243 mimeTypeA = heap_printf("application/x-wine-extension-%s", &extensionA[1]);
2245 if (mimeTypeA != NULL)
2247 /* Gnome seems to ignore the <icon> tag in MIME packages,
2248 * and the default name is more intuitive anyway.
2250 if (iconW)
2252 char *flattened_mime = slashes_to_minuses(mimeTypeA);
2253 if (flattened_mime)
2255 int index = 0;
2256 WCHAR *comma = strrchrW(iconW, ',');
2257 if (comma)
2259 *comma = 0;
2260 index = atoiW(comma + 1);
2262 iconA = extract_icon(iconW, index, flattened_mime, FALSE);
2263 HeapFree(GetProcessHeap(), 0, flattened_mime);
2267 write_freedesktop_mime_type_entry(packages_dir, extensionA, mimeTypeA, friendlyDocNameA);
2268 hasChanged = TRUE;
2270 else
2272 WINE_FIXME("out of memory\n");
2273 goto end;
2277 commandW = assoc_query(ASSOCSTR_COMMAND, extensionW, openW);
2278 if (commandW == NULL)
2279 /* no command => no application is associated */
2280 goto end;
2282 executableW = assoc_query(ASSOCSTR_EXECUTABLE, extensionW, openW);
2283 if (executableW)
2284 openWithIconA = extract_icon(executableW, 0, NULL, FALSE);
2286 friendlyAppNameW = assoc_query(ASSOCSTR_FRIENDLYAPPNAME, extensionW, NULL);
2287 if (friendlyAppNameW)
2289 friendlyAppNameA = wchars_to_utf8_chars(friendlyAppNameW);
2290 if (friendlyAppNameA == NULL)
2292 WINE_ERR("out of memory\n");
2293 goto end;
2296 else
2298 friendlyAppNameA = heap_printf("A Wine application");
2299 if (friendlyAppNameA == NULL)
2301 WINE_ERR("out of memory\n");
2302 goto end;
2306 progIdW = reg_get_valW(HKEY_CLASSES_ROOT, extensionW, NULL);
2307 if (progIdW)
2309 progIdA = escape(progIdW);
2310 if (progIdA == NULL)
2312 WINE_ERR("out of memory\n");
2313 goto end;
2316 else
2317 goto end; /* no progID => not a file type association */
2319 /* Do not allow duplicate ProgIDs for a MIME type, it causes unnecessary duplication in Open dialogs */
2320 mimeProgId = heap_printf("%s=>%s", mimeTypeA, progIdA);
2321 if (mimeProgId)
2323 struct rb_string_entry *entry;
2324 if (wine_rb_get(&mimeProgidTree, mimeProgId))
2326 HeapFree(GetProcessHeap(), 0, mimeProgId);
2327 goto end;
2329 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct rb_string_entry));
2330 if (!entry)
2332 WINE_ERR("out of memory allocating rb_string_entry\n");
2333 goto end;
2335 entry->string = mimeProgId;
2336 if (wine_rb_put(&mimeProgidTree, mimeProgId, &entry->entry))
2338 WINE_ERR("error updating rb tree\n");
2339 goto end;
2343 if (has_association_changed(extensionW, mimeTypeA, progIdW, friendlyAppNameA, friendlyDocNameW, openWithIconA))
2345 char *desktopPath = heap_printf("%s/wine-extension-%s.desktop", applications_dir, &extensionA[1]);
2346 if (desktopPath)
2348 if (write_freedesktop_association_entry(desktopPath, extensionA, friendlyAppNameA, mimeTypeA, progIdA, openWithIconA))
2350 hasChanged = TRUE;
2351 update_association(extensionW, mimeTypeA, progIdW, friendlyAppNameA, friendlyDocNameW, desktopPath, openWithIconA);
2353 HeapFree(GetProcessHeap(), 0, desktopPath);
2357 end:
2358 HeapFree(GetProcessHeap(), 0, extensionA);
2359 HeapFree(GetProcessHeap(), 0, commandW);
2360 HeapFree(GetProcessHeap(), 0, executableW);
2361 HeapFree(GetProcessHeap(), 0, openWithIconA);
2362 HeapFree(GetProcessHeap(), 0, friendlyDocNameW);
2363 HeapFree(GetProcessHeap(), 0, friendlyDocNameA);
2364 HeapFree(GetProcessHeap(), 0, iconW);
2365 HeapFree(GetProcessHeap(), 0, iconA);
2366 HeapFree(GetProcessHeap(), 0, contentTypeW);
2367 HeapFree(GetProcessHeap(), 0, mimeTypeA);
2368 HeapFree(GetProcessHeap(), 0, friendlyAppNameW);
2369 HeapFree(GetProcessHeap(), 0, friendlyAppNameA);
2370 HeapFree(GetProcessHeap(), 0, progIdW);
2371 HeapFree(GetProcessHeap(), 0, progIdA);
2373 HeapFree(GetProcessHeap(), 0, extensionW);
2374 if (ret != ERROR_SUCCESS)
2375 break;
2378 wine_rb_destroy(&mimeProgidTree, winemenubuilder_rb_destroy, NULL);
2379 free_native_mime_types(nativeMimeTypes);
2380 return hasChanged;
2383 static char *get_start_exe_path(void)
2385 static const WCHAR startW[] = {'\\','c','o','m','m','a','n','d',
2386 '\\','s','t','a','r','t','.','e','x','e',0};
2387 WCHAR start_path[MAX_PATH];
2388 GetWindowsDirectoryW(start_path, MAX_PATH);
2389 lstrcatW(start_path, startW);
2390 return escape(start_path);
2393 static char* escape_unix_link_arg(LPCSTR unix_link)
2395 char *ret = NULL;
2396 WCHAR *unix_linkW = utf8_chars_to_wchars(unix_link);
2397 if (unix_linkW)
2399 char *escaped_lnk = escape(unix_linkW);
2400 if (escaped_lnk)
2402 ret = heap_printf("/Unix %s", escaped_lnk);
2403 HeapFree(GetProcessHeap(), 0, escaped_lnk);
2405 HeapFree(GetProcessHeap(), 0, unix_linkW);
2407 return ret;
2410 static BOOL InvokeShellLinker( IShellLinkW *sl, LPCWSTR link, BOOL bWait )
2412 static const WCHAR startW[] = {'\\','c','o','m','m','a','n','d',
2413 '\\','s','t','a','r','t','.','e','x','e',0};
2414 char *link_name = NULL, *icon_name = NULL, *work_dir = NULL;
2415 char *escaped_path = NULL, *escaped_args = NULL, *description = NULL;
2416 WCHAR szTmp[INFOTIPSIZE];
2417 WCHAR szDescription[INFOTIPSIZE], szPath[MAX_PATH], szWorkDir[MAX_PATH];
2418 WCHAR szArgs[INFOTIPSIZE], szIconPath[MAX_PATH];
2419 int iIconId = 0, r = -1;
2420 DWORD csidl = -1;
2421 HANDLE hsem = NULL;
2422 char *unix_link = NULL;
2423 char *start_path = NULL;
2425 if ( !link )
2427 WINE_ERR("Link name is null\n");
2428 return FALSE;
2431 if( !GetLinkLocation( link, &csidl, &link_name ) )
2433 WINE_WARN("Unknown link location %s. Ignoring.\n",wine_dbgstr_w(link));
2434 return TRUE;
2436 if (!in_desktop_dir(csidl) && !in_startmenu(csidl))
2438 WINE_WARN("Not under desktop or start menu. Ignoring.\n");
2439 return TRUE;
2441 WINE_TRACE("Link : %s\n", wine_dbgstr_a(link_name));
2443 szTmp[0] = 0;
2444 IShellLinkW_GetWorkingDirectory( sl, szTmp, MAX_PATH );
2445 ExpandEnvironmentStringsW(szTmp, szWorkDir, MAX_PATH);
2446 WINE_TRACE("workdir : %s\n", wine_dbgstr_w(szWorkDir));
2448 szTmp[0] = 0;
2449 IShellLinkW_GetDescription( sl, szTmp, INFOTIPSIZE );
2450 ExpandEnvironmentStringsW(szTmp, szDescription, INFOTIPSIZE);
2451 WINE_TRACE("description: %s\n", wine_dbgstr_w(szDescription));
2453 get_cmdline( sl, szTmp, MAX_PATH, szArgs, INFOTIPSIZE);
2454 ExpandEnvironmentStringsW(szTmp, szPath, MAX_PATH);
2455 WINE_TRACE("path : %s\n", wine_dbgstr_w(szPath));
2456 WINE_TRACE("args : %s\n", wine_dbgstr_w(szArgs));
2458 szTmp[0] = 0;
2459 IShellLinkW_GetIconLocation( sl, szTmp, MAX_PATH, &iIconId );
2460 ExpandEnvironmentStringsW(szTmp, szIconPath, MAX_PATH);
2461 WINE_TRACE("icon file : %s\n", wine_dbgstr_w(szIconPath) );
2463 if( !szPath[0] )
2465 LPITEMIDLIST pidl = NULL;
2466 IShellLinkW_GetIDList( sl, &pidl );
2467 if( pidl && SHGetPathFromIDListW( pidl, szPath ) )
2468 WINE_TRACE("pidl path : %s\n", wine_dbgstr_w(szPath));
2471 /* extract the icon */
2472 if( szIconPath[0] )
2473 icon_name = extract_icon( szIconPath , iIconId, NULL, bWait );
2474 else
2475 icon_name = extract_icon( szPath, iIconId, NULL, bWait );
2477 /* fail - try once again after parent process exit */
2478 if( !icon_name )
2480 if (bWait)
2482 WINE_WARN("Unable to extract icon, deferring.\n");
2483 goto cleanup;
2485 WINE_ERR("failed to extract icon from %s\n",
2486 wine_dbgstr_w( szIconPath[0] ? szIconPath : szPath ));
2489 unix_link = wine_get_unix_file_name(link);
2490 if (unix_link == NULL)
2492 WINE_WARN("couldn't find unix path of %s\n", wine_dbgstr_w(link));
2493 goto cleanup;
2496 /* check the path */
2497 if( szPath[0] )
2499 static const WCHAR exeW[] = {'.','e','x','e',0};
2500 WCHAR *p;
2502 /* check for .exe extension */
2503 if (!(p = strrchrW( szPath, '.' )) ||
2504 strchrW( p, '\\' ) || strchrW( p, '/' ) ||
2505 lstrcmpiW( p, exeW ))
2507 /* Not .exe - use 'start.exe' to launch this file */
2508 p = szArgs + lstrlenW(szPath) + 2;
2509 if (szArgs[0])
2511 p[0] = ' ';
2512 memmove( p+1, szArgs, min( (lstrlenW(szArgs) + 1) * sizeof(szArgs[0]),
2513 sizeof(szArgs) - (p + 1 - szArgs) * sizeof(szArgs[0]) ) );
2515 else
2516 p[0] = 0;
2518 szArgs[0] = '"';
2519 lstrcpyW(szArgs + 1, szPath);
2520 p[-1] = '"';
2522 GetWindowsDirectoryW(szPath, MAX_PATH);
2523 lstrcatW(szPath, startW);
2526 /* convert app working dir */
2527 if (szWorkDir[0])
2528 work_dir = wine_get_unix_file_name( szWorkDir );
2530 else
2532 /* if there's no path... try run the link itself */
2533 lstrcpynW(szArgs, link, MAX_PATH);
2534 GetWindowsDirectoryW(szPath, MAX_PATH);
2535 lstrcatW(szPath, startW);
2538 /* escape the path and parameters */
2539 escaped_path = escape(szPath);
2540 escaped_args = escape(szArgs);
2541 description = wchars_to_utf8_chars(szDescription);
2542 if (escaped_path == NULL || escaped_args == NULL || description == NULL)
2544 WINE_ERR("out of memory allocating/escaping parameters\n");
2545 goto cleanup;
2548 start_path = get_start_exe_path();
2549 if (start_path == NULL)
2551 WINE_ERR("out of memory\n");
2552 goto cleanup;
2555 /* building multiple menus concurrently has race conditions */
2556 hsem = CreateSemaphoreA( NULL, 1, 1, "winemenubuilder_semaphore");
2557 if( WAIT_OBJECT_0 != MsgWaitForMultipleObjects( 1, &hsem, FALSE, INFINITE, QS_ALLINPUT ) )
2559 WINE_ERR("failed wait for semaphore\n");
2560 goto cleanup;
2563 if (in_desktop_dir(csidl))
2565 char *location;
2566 const char *lastEntry;
2567 lastEntry = strrchr(link_name, '/');
2568 if (lastEntry == NULL)
2569 lastEntry = link_name;
2570 else
2571 ++lastEntry;
2572 location = heap_printf("%s/%s.desktop", xdg_desktop_dir, lastEntry);
2573 if (location)
2575 if (csidl == CSIDL_COMMON_DESKTOPDIRECTORY)
2577 char *link_arg = escape_unix_link_arg(unix_link);
2578 if (link_arg)
2580 r = !write_desktop_entry(unix_link, location, lastEntry,
2581 start_path, link_arg, description, work_dir, icon_name);
2582 HeapFree(GetProcessHeap(), 0, link_arg);
2585 else
2586 r = !write_desktop_entry(NULL, location, lastEntry, escaped_path, escaped_args, description, work_dir, icon_name);
2587 if (r == 0)
2588 chmod(location, 0755);
2589 HeapFree(GetProcessHeap(), 0, location);
2592 else
2594 char *link_arg = escape_unix_link_arg(unix_link);
2595 if (link_arg)
2597 r = !write_menu_entry(unix_link, link_name, start_path, link_arg, description, work_dir, icon_name);
2598 HeapFree(GetProcessHeap(), 0, link_arg);
2602 ReleaseSemaphore( hsem, 1, NULL );
2604 cleanup:
2605 if (hsem) CloseHandle( hsem );
2606 HeapFree( GetProcessHeap(), 0, icon_name );
2607 HeapFree( GetProcessHeap(), 0, work_dir );
2608 HeapFree( GetProcessHeap(), 0, link_name );
2609 HeapFree( GetProcessHeap(), 0, escaped_args );
2610 HeapFree( GetProcessHeap(), 0, escaped_path );
2611 HeapFree( GetProcessHeap(), 0, description );
2612 HeapFree( GetProcessHeap(), 0, unix_link );
2613 HeapFree( GetProcessHeap(), 0, start_path );
2615 if (r && !bWait)
2616 WINE_ERR("failed to build the menu\n" );
2618 return ( r == 0 );
2621 static BOOL InvokeShellLinkerForURL( IUniformResourceLocatorW *url, LPCWSTR link, BOOL bWait )
2623 char *link_name = NULL, *icon_name = NULL;
2624 DWORD csidl = -1;
2625 LPWSTR urlPath;
2626 char *escaped_urlPath = NULL;
2627 HRESULT hr;
2628 HANDLE hSem = NULL;
2629 BOOL ret = TRUE;
2630 int r = -1;
2631 char *unix_link = NULL;
2632 IPropertySetStorage *pPropSetStg;
2633 IPropertyStorage *pPropStg;
2634 PROPSPEC ps[2];
2635 PROPVARIANT pv[2];
2637 if ( !link )
2639 WINE_ERR("Link name is null\n");
2640 return TRUE;
2643 if( !GetLinkLocation( link, &csidl, &link_name ) )
2645 WINE_WARN("Unknown link location %s. Ignoring.\n",wine_dbgstr_w(link));
2646 return TRUE;
2648 if (!in_desktop_dir(csidl) && !in_startmenu(csidl))
2650 WINE_WARN("Not under desktop or start menu. Ignoring.\n");
2651 ret = TRUE;
2652 goto cleanup;
2654 WINE_TRACE("Link : %s\n", wine_dbgstr_a(link_name));
2656 hr = url->lpVtbl->GetURL(url, &urlPath);
2657 if (FAILED(hr))
2659 ret = TRUE;
2660 goto cleanup;
2662 WINE_TRACE("path : %s\n", wine_dbgstr_w(urlPath));
2664 unix_link = wine_get_unix_file_name(link);
2665 if (unix_link == NULL)
2667 WINE_WARN("couldn't find unix path of %s\n", wine_dbgstr_w(link));
2668 goto cleanup;
2671 escaped_urlPath = escape(urlPath);
2672 if (escaped_urlPath == NULL)
2674 WINE_ERR("couldn't escape url, out of memory\n");
2675 goto cleanup;
2678 ps[0].ulKind = PRSPEC_PROPID;
2679 ps[0].propid = PID_IS_ICONFILE;
2680 ps[1].ulKind = PRSPEC_PROPID;
2681 ps[1].propid = PID_IS_ICONINDEX;
2683 hr = url->lpVtbl->QueryInterface(url, &IID_IPropertySetStorage, (void **) &pPropSetStg);
2684 if (SUCCEEDED(hr))
2686 hr = IPropertySetStorage_Open(pPropSetStg, &FMTID_Intshcut, STGM_READ | STGM_SHARE_EXCLUSIVE, &pPropStg);
2687 if (SUCCEEDED(hr))
2689 hr = IPropertyStorage_ReadMultiple(pPropStg, 2, ps, pv);
2690 if (SUCCEEDED(hr))
2692 icon_name = extract_icon( pv[0].pwszVal, pv[1].iVal, NULL, bWait );
2694 WINE_TRACE("URL icon path: %s icon index: %d icon name: %s\n", wine_dbgstr_w(pv[0].pwszVal), pv[1].iVal, icon_name);
2695 PropVariantClear(&pv[0]);
2696 PropVariantClear(&pv[1]);
2698 IPropertyStorage_Release(pPropStg);
2700 IPropertySetStorage_Release(pPropSetStg);
2703 /* fail - try once again after parent process exit */
2704 if( !icon_name )
2706 if (bWait)
2708 WINE_WARN("Unable to extract icon, deferring.\n");
2709 ret = FALSE;
2710 goto cleanup;
2712 WINE_ERR("failed to extract icon from %s\n",
2713 wine_dbgstr_w( pv[0].pwszVal ));
2716 hSem = CreateSemaphoreA( NULL, 1, 1, "winemenubuilder_semaphore");
2717 if( WAIT_OBJECT_0 != MsgWaitForMultipleObjects( 1, &hSem, FALSE, INFINITE, QS_ALLINPUT ) )
2719 WINE_ERR("failed wait for semaphore\n");
2720 goto cleanup;
2722 if (in_desktop_dir(csidl))
2724 char *location;
2725 const char *lastEntry;
2726 lastEntry = strrchr(link_name, '/');
2727 if (lastEntry == NULL)
2728 lastEntry = link_name;
2729 else
2730 ++lastEntry;
2731 location = heap_printf("%s/%s.desktop", xdg_desktop_dir, lastEntry);
2732 if (location)
2734 r = !write_desktop_entry(NULL, location, lastEntry, "winebrowser", escaped_urlPath, NULL, NULL, icon_name);
2735 if (r == 0)
2736 chmod(location, 0755);
2737 HeapFree(GetProcessHeap(), 0, location);
2740 else
2741 r = !write_menu_entry(unix_link, link_name, "winebrowser", escaped_urlPath, NULL, NULL, icon_name);
2742 ret = (r != 0);
2743 ReleaseSemaphore(hSem, 1, NULL);
2745 cleanup:
2746 if (hSem)
2747 CloseHandle(hSem);
2748 HeapFree( GetProcessHeap(), 0, icon_name );
2749 HeapFree(GetProcessHeap(), 0, link_name);
2750 CoTaskMemFree( urlPath );
2751 HeapFree(GetProcessHeap(), 0, escaped_urlPath);
2752 HeapFree(GetProcessHeap(), 0, unix_link);
2753 return ret;
2756 static BOOL WaitForParentProcess( void )
2758 PROCESSENTRY32 procentry;
2759 HANDLE hsnapshot = NULL, hprocess = NULL;
2760 DWORD ourpid = GetCurrentProcessId();
2761 BOOL ret = FALSE, rc;
2763 WINE_TRACE("Waiting for parent process\n");
2764 if ((hsnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 )) ==
2765 INVALID_HANDLE_VALUE)
2767 WINE_ERR("CreateToolhelp32Snapshot failed, error %d\n", GetLastError());
2768 goto done;
2771 procentry.dwSize = sizeof(PROCESSENTRY32);
2772 rc = Process32First( hsnapshot, &procentry );
2773 while (rc)
2775 if (procentry.th32ProcessID == ourpid) break;
2776 rc = Process32Next( hsnapshot, &procentry );
2778 if (!rc)
2780 WINE_WARN("Unable to find current process id %d when listing processes\n", ourpid);
2781 goto done;
2784 if ((hprocess = OpenProcess( SYNCHRONIZE, FALSE, procentry.th32ParentProcessID )) ==
2785 NULL)
2787 WINE_WARN("OpenProcess failed pid=%d, error %d\n", procentry.th32ParentProcessID,
2788 GetLastError());
2789 goto done;
2792 if (MsgWaitForMultipleObjects( 1, &hprocess, FALSE, INFINITE, QS_ALLINPUT ) == WAIT_OBJECT_0)
2793 ret = TRUE;
2794 else
2795 WINE_ERR("Unable to wait for parent process, error %d\n", GetLastError());
2797 done:
2798 if (hprocess) CloseHandle( hprocess );
2799 if (hsnapshot) CloseHandle( hsnapshot );
2800 return ret;
2803 static BOOL Process_Link( LPCWSTR linkname, BOOL bWait )
2805 IShellLinkW *sl;
2806 IPersistFile *pf;
2807 HRESULT r;
2808 WCHAR fullname[MAX_PATH];
2809 DWORD len;
2811 WINE_TRACE("%s, wait %d\n", wine_dbgstr_w(linkname), bWait);
2813 if( !linkname[0] )
2815 WINE_ERR("link name missing\n");
2816 return 1;
2819 len=GetFullPathNameW( linkname, MAX_PATH, fullname, NULL );
2820 if (len==0 || len>MAX_PATH)
2822 WINE_ERR("couldn't get full path of link file\n");
2823 return 1;
2826 r = CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
2827 &IID_IShellLinkW, (LPVOID *) &sl );
2828 if( FAILED( r ) )
2830 WINE_ERR("No IID_IShellLink\n");
2831 return 1;
2834 r = IShellLinkW_QueryInterface( sl, &IID_IPersistFile, (LPVOID*) &pf );
2835 if( FAILED( r ) )
2837 WINE_ERR("No IID_IPersistFile\n");
2838 return 1;
2841 r = IPersistFile_Load( pf, fullname, STGM_READ );
2842 if( SUCCEEDED( r ) )
2844 /* If something fails (eg. Couldn't extract icon)
2845 * wait for parent process and try again
2847 if( ! InvokeShellLinker( sl, fullname, bWait ) && bWait )
2849 WaitForParentProcess();
2850 InvokeShellLinker( sl, fullname, FALSE );
2853 else
2855 WINE_ERR("unable to load %s\n", wine_dbgstr_w(linkname));
2858 IPersistFile_Release( pf );
2859 IShellLinkW_Release( sl );
2861 return !r;
2864 static BOOL Process_URL( LPCWSTR urlname, BOOL bWait )
2866 IUniformResourceLocatorW *url;
2867 IPersistFile *pf;
2868 HRESULT r;
2869 WCHAR fullname[MAX_PATH];
2870 DWORD len;
2872 WINE_TRACE("%s, wait %d\n", wine_dbgstr_w(urlname), bWait);
2874 if( !urlname[0] )
2876 WINE_ERR("URL name missing\n");
2877 return 1;
2880 len=GetFullPathNameW( urlname, MAX_PATH, fullname, NULL );
2881 if (len==0 || len>MAX_PATH)
2883 WINE_ERR("couldn't get full path of URL file\n");
2884 return 1;
2887 r = CoCreateInstance( &CLSID_InternetShortcut, NULL, CLSCTX_INPROC_SERVER,
2888 &IID_IUniformResourceLocatorW, (LPVOID *) &url );
2889 if( FAILED( r ) )
2891 WINE_ERR("No IID_IUniformResourceLocatorW\n");
2892 return 1;
2895 r = url->lpVtbl->QueryInterface( url, &IID_IPersistFile, (LPVOID*) &pf );
2896 if( FAILED( r ) )
2898 WINE_ERR("No IID_IPersistFile\n");
2899 return 1;
2901 r = IPersistFile_Load( pf, fullname, STGM_READ );
2902 if( SUCCEEDED( r ) )
2904 /* If something fails (eg. Couldn't extract icon)
2905 * wait for parent process and try again
2907 if( ! InvokeShellLinkerForURL( url, fullname, bWait ) && bWait )
2909 WaitForParentProcess();
2910 InvokeShellLinkerForURL( url, fullname, FALSE );
2914 IPersistFile_Release( pf );
2915 url->lpVtbl->Release( url );
2917 return !r;
2920 static void RefreshFileTypeAssociations(void)
2922 HANDLE hSem = NULL;
2923 char *mime_dir = NULL;
2924 char *packages_dir = NULL;
2925 char *applications_dir = NULL;
2926 BOOL hasChanged;
2928 hSem = CreateSemaphoreA( NULL, 1, 1, "winemenubuilder_semaphore");
2929 if( WAIT_OBJECT_0 != MsgWaitForMultipleObjects( 1, &hSem, FALSE, INFINITE, QS_ALLINPUT ) )
2931 WINE_ERR("failed wait for semaphore\n");
2932 CloseHandle(hSem);
2933 hSem = NULL;
2934 goto end;
2937 mime_dir = heap_printf("%s/mime", xdg_data_dir);
2938 if (mime_dir == NULL)
2940 WINE_ERR("out of memory\n");
2941 goto end;
2943 create_directories(mime_dir);
2945 packages_dir = heap_printf("%s/packages", mime_dir);
2946 if (packages_dir == NULL)
2948 WINE_ERR("out of memory\n");
2949 goto end;
2951 create_directories(packages_dir);
2953 applications_dir = heap_printf("%s/applications", xdg_data_dir);
2954 if (applications_dir == NULL)
2956 WINE_ERR("out of memory\n");
2957 goto end;
2959 create_directories(applications_dir);
2961 hasChanged = generate_associations(xdg_data_dir, packages_dir, applications_dir);
2962 hasChanged |= cleanup_associations();
2963 if (hasChanged)
2965 const char *argv[3];
2967 argv[0] = "update-mime-database";
2968 argv[1] = mime_dir;
2969 argv[2] = NULL;
2970 spawnvp( _P_NOWAIT, argv[0], argv );
2972 argv[0] = "update-desktop-database";
2973 argv[1] = applications_dir;
2974 spawnvp( _P_NOWAIT, argv[0], argv );
2977 end:
2978 if (hSem)
2980 ReleaseSemaphore(hSem, 1, NULL);
2981 CloseHandle(hSem);
2983 HeapFree(GetProcessHeap(), 0, mime_dir);
2984 HeapFree(GetProcessHeap(), 0, packages_dir);
2985 HeapFree(GetProcessHeap(), 0, applications_dir);
2988 static void cleanup_menus(void)
2990 HKEY hkey;
2992 hkey = open_menus_reg_key();
2993 if (hkey)
2995 int i;
2996 LSTATUS lret = ERROR_SUCCESS;
2997 for (i = 0; lret == ERROR_SUCCESS; )
2999 WCHAR *value = NULL;
3000 WCHAR *data = NULL;
3001 DWORD valueSize = 4096;
3002 DWORD dataSize = 4096;
3003 while (1)
3005 lret = ERROR_OUTOFMEMORY;
3006 value = HeapAlloc(GetProcessHeap(), 0, valueSize * sizeof(WCHAR));
3007 if (value == NULL)
3008 break;
3009 data = HeapAlloc(GetProcessHeap(), 0, dataSize * sizeof(WCHAR));
3010 if (data == NULL)
3011 break;
3012 lret = RegEnumValueW(hkey, i, value, &valueSize, NULL, NULL, (BYTE*)data, &dataSize);
3013 if (lret == ERROR_SUCCESS || lret != ERROR_MORE_DATA)
3014 break;
3015 valueSize *= 2;
3016 dataSize *= 2;
3017 HeapFree(GetProcessHeap(), 0, value);
3018 HeapFree(GetProcessHeap(), 0, data);
3019 value = data = NULL;
3021 if (lret == ERROR_SUCCESS)
3023 char *unix_file;
3024 char *windows_file;
3025 unix_file = wchars_to_unix_chars(value);
3026 windows_file = wchars_to_unix_chars(data);
3027 if (unix_file != NULL && windows_file != NULL)
3029 struct stat filestats;
3030 if (stat(windows_file, &filestats) < 0 && errno == ENOENT)
3032 WINE_TRACE("removing menu related file %s\n", unix_file);
3033 remove(unix_file);
3034 RegDeleteValueW(hkey, value);
3036 else
3037 i++;
3039 else
3041 WINE_ERR("out of memory enumerating menus\n");
3042 lret = ERROR_OUTOFMEMORY;
3044 HeapFree(GetProcessHeap(), 0, unix_file);
3045 HeapFree(GetProcessHeap(), 0, windows_file);
3047 else if (lret != ERROR_NO_MORE_ITEMS)
3048 WINE_ERR("error %d reading registry\n", lret);
3049 HeapFree(GetProcessHeap(), 0, value);
3050 HeapFree(GetProcessHeap(), 0, data);
3052 RegCloseKey(hkey);
3054 else
3055 WINE_ERR("error opening registry key, menu cleanup failed\n");
3058 static void thumbnail_lnk(LPCWSTR lnkPath, LPCWSTR outputPath)
3060 char *utf8lnkPath = NULL;
3061 char *utf8OutputPath = NULL;
3062 WCHAR *winLnkPath = NULL;
3063 IShellLinkW *shellLink = NULL;
3064 IPersistFile *persistFile = NULL;
3065 WCHAR szTmp[MAX_PATH];
3066 WCHAR szPath[MAX_PATH];
3067 WCHAR szArgs[INFOTIPSIZE];
3068 WCHAR szIconPath[MAX_PATH];
3069 int iconId;
3070 IStream *stream = NULL;
3071 HRESULT hr;
3073 utf8lnkPath = wchars_to_utf8_chars(lnkPath);
3074 if (utf8lnkPath == NULL)
3076 WINE_ERR("out of memory converting paths\n");
3077 goto end;
3080 utf8OutputPath = wchars_to_utf8_chars(outputPath);
3081 if (utf8OutputPath == NULL)
3083 WINE_ERR("out of memory converting paths\n");
3084 goto end;
3087 winLnkPath = wine_get_dos_file_name(utf8lnkPath);
3088 if (winLnkPath == NULL)
3090 WINE_ERR("could not convert %s to DOS path\n", utf8lnkPath);
3091 goto end;
3094 hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
3095 &IID_IShellLinkW, (LPVOID*)&shellLink);
3096 if (FAILED(hr))
3098 WINE_ERR("could not create IShellLinkW, error 0x%08X\n", hr);
3099 goto end;
3102 hr = IShellLinkW_QueryInterface(shellLink, &IID_IPersistFile, (LPVOID)&persistFile);
3103 if (FAILED(hr))
3105 WINE_ERR("could not query IPersistFile, error 0x%08X\n", hr);
3106 goto end;
3109 hr = IPersistFile_Load(persistFile, winLnkPath, STGM_READ);
3110 if (FAILED(hr))
3112 WINE_ERR("could not read .lnk, error 0x%08X\n", hr);
3113 goto end;
3116 get_cmdline(shellLink, szTmp, MAX_PATH, szArgs, INFOTIPSIZE);
3117 ExpandEnvironmentStringsW(szTmp, szPath, MAX_PATH);
3118 szTmp[0] = 0;
3119 IShellLinkW_GetIconLocation(shellLink, szTmp, MAX_PATH, &iconId);
3120 ExpandEnvironmentStringsW(szTmp, szIconPath, MAX_PATH);
3122 if(!szPath[0])
3124 LPITEMIDLIST pidl = NULL;
3125 IShellLinkW_GetIDList(shellLink, &pidl);
3126 if (pidl && SHGetPathFromIDListW(pidl, szPath))
3127 WINE_TRACE("pidl path : %s\n", wine_dbgstr_w(szPath));
3130 if (szIconPath[0])
3132 hr = open_icon(szIconPath, iconId, FALSE, &stream);
3133 if (SUCCEEDED(hr))
3134 hr = write_native_icon(stream, utf8OutputPath, NULL);
3136 else
3138 hr = open_icon(szPath, iconId, FALSE, &stream);
3139 if (SUCCEEDED(hr))
3140 hr = write_native_icon(stream, utf8OutputPath, NULL);
3143 end:
3144 HeapFree(GetProcessHeap(), 0, utf8lnkPath);
3145 HeapFree(GetProcessHeap(), 0, utf8OutputPath);
3146 HeapFree(GetProcessHeap(), 0, winLnkPath);
3147 if (shellLink != NULL)
3148 IShellLinkW_Release(shellLink);
3149 if (persistFile != NULL)
3150 IPersistFile_Release(persistFile);
3151 if (stream != NULL)
3152 IStream_Release(stream);
3155 static WCHAR *next_token( LPWSTR *p )
3157 LPWSTR token = NULL, t = *p;
3159 if( !t )
3160 return NULL;
3162 while( t && !token )
3164 switch( *t )
3166 case ' ':
3167 t++;
3168 continue;
3169 case '"':
3170 /* unquote the token */
3171 token = ++t;
3172 t = strchrW( token, '"' );
3173 if( t )
3174 *t++ = 0;
3175 break;
3176 case 0:
3177 t = NULL;
3178 break;
3179 default:
3180 token = t;
3181 t = strchrW( token, ' ' );
3182 if( t )
3183 *t++ = 0;
3184 break;
3187 *p = t;
3188 return token;
3191 static BOOL init_xdg(void)
3193 WCHAR shellDesktopPath[MAX_PATH];
3194 HRESULT hr = SHGetFolderPathW(NULL, CSIDL_DESKTOP, NULL, SHGFP_TYPE_CURRENT, shellDesktopPath);
3195 if (SUCCEEDED(hr))
3196 xdg_desktop_dir = wine_get_unix_file_name(shellDesktopPath);
3197 if (xdg_desktop_dir == NULL)
3199 WINE_ERR("error looking up the desktop directory\n");
3200 return FALSE;
3203 if (getenv("XDG_CONFIG_HOME"))
3204 xdg_config_dir = heap_printf("%s/menus/applications-merged", getenv("XDG_CONFIG_HOME"));
3205 else
3206 xdg_config_dir = heap_printf("%s/.config/menus/applications-merged", getenv("HOME"));
3207 if (xdg_config_dir)
3209 create_directories(xdg_config_dir);
3210 if (getenv("XDG_DATA_HOME"))
3211 xdg_data_dir = strdupA(getenv("XDG_DATA_HOME"));
3212 else
3213 xdg_data_dir = heap_printf("%s/.local/share", getenv("HOME"));
3214 if (xdg_data_dir)
3216 char *buffer;
3217 create_directories(xdg_data_dir);
3218 buffer = heap_printf("%s/desktop-directories", xdg_data_dir);
3219 if (buffer)
3221 mkdir(buffer, 0777);
3222 HeapFree(GetProcessHeap(), 0, buffer);
3224 return TRUE;
3226 HeapFree(GetProcessHeap(), 0, xdg_config_dir);
3228 WINE_ERR("out of memory\n");
3229 return FALSE;
3232 /***********************************************************************
3234 * wWinMain
3236 int PASCAL wWinMain (HINSTANCE hInstance, HINSTANCE prev, LPWSTR cmdline, int show)
3238 static const WCHAR dash_aW[] = {'-','a',0};
3239 static const WCHAR dash_rW[] = {'-','r',0};
3240 static const WCHAR dash_tW[] = {'-','t',0};
3241 static const WCHAR dash_uW[] = {'-','u',0};
3242 static const WCHAR dash_wW[] = {'-','w',0};
3244 LPWSTR token = NULL, p;
3245 BOOL bWait = FALSE;
3246 BOOL bURL = FALSE;
3247 HRESULT hr;
3248 int ret = 0;
3250 if (!init_xdg())
3251 return 1;
3253 hr = CoInitialize(NULL);
3254 if (FAILED(hr))
3256 WINE_ERR("could not initialize COM, error 0x%08X\n", hr);
3257 return 1;
3260 for( p = cmdline; p && *p; )
3262 token = next_token( &p );
3263 if( !token )
3264 break;
3265 if( !strcmpW( token, dash_aW ) )
3267 RefreshFileTypeAssociations();
3268 continue;
3270 if( !strcmpW( token, dash_rW ) )
3272 cleanup_menus();
3273 continue;
3275 if( !strcmpW( token, dash_wW ) )
3276 bWait = TRUE;
3277 else if ( !strcmpW( token, dash_uW ) )
3278 bURL = TRUE;
3279 else if ( !strcmpW( token, dash_tW ) )
3281 WCHAR *lnkFile = next_token( &p );
3282 if (lnkFile)
3284 WCHAR *outputFile = next_token( &p );
3285 if (outputFile)
3286 thumbnail_lnk(lnkFile, outputFile);
3289 else if( token[0] == '-' )
3291 WINE_ERR( "unknown option %s\n", wine_dbgstr_w(token) );
3293 else
3295 BOOL bRet;
3297 if (bURL)
3298 bRet = Process_URL( token, bWait );
3299 else
3300 bRet = Process_Link( token, bWait );
3301 if (!bRet)
3303 WINE_ERR( "failed to build menu item for %s\n", wine_dbgstr_w(token) );
3304 ret = 1;
3309 CoUninitialize();
3310 return ret;