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 * Generate icons for file open handlers to go into the "Open with..."
56 * list. What does Windows use, the default icon for the .EXE file? It's
57 * not in the registry.
58 * Associate applications under HKCR\Applications to open any MIME type
59 * (by associating with application/octet-stream, or how?).
60 * Clean up fd.o MIME types when they are deleted in Windows, their icons
61 * too. Very hard - once we associate them with fd.o, we can't tell whether
62 * they are ours or not, and the extension <-> MIME type mapping isn't
64 * Wine's HKCR is broken - it doesn't merge HKCU\Software\Classes, so apps
65 * that write associations there won't associate (#17019).
69 #include "wine/port.h"
94 #include "wine/unicode.h"
95 #include "wine/debug.h"
96 #include "wine/library.h"
97 #include "wine/list.h"
105 WINE_DEFAULT_DEBUG_CHANNEL(menubuilder
);
107 #define in_desktop_dir(csidl) ((csidl)==CSIDL_DESKTOPDIRECTORY || \
108 (csidl)==CSIDL_COMMON_DESKTOPDIRECTORY)
109 #define in_startmenu(csidl) ((csidl)==CSIDL_STARTMENU || \
110 (csidl)==CSIDL_COMMON_STARTMENU)
112 /* link file formats */
114 #include "pshpack1.h"
133 GRPICONDIRENTRY idEntries
[1];
171 static char *xdg_config_dir
;
172 static char *xdg_data_dir
;
173 static char *xdg_desktop_dir
;
175 /* Icon extraction routines
177 * FIXME: should use PrivateExtractIcons and friends
178 * FIXME: should not use stdio
181 #define MASK(x,y) (pAND[(x) / 8 + (nHeight - (y) - 1) * nANDWidthBytes] & (1 << (7 - (x) % 8)))
183 /* PNG-specific code */
186 static void *libpng_handle
;
187 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
188 MAKE_FUNCPTR(png_create_info_struct
);
189 MAKE_FUNCPTR(png_create_write_struct
);
190 MAKE_FUNCPTR(png_destroy_write_struct
);
191 MAKE_FUNCPTR(png_init_io
);
192 MAKE_FUNCPTR(png_set_bgr
);
193 MAKE_FUNCPTR(png_set_text
);
194 MAKE_FUNCPTR(png_set_IHDR
);
195 MAKE_FUNCPTR(png_write_end
);
196 MAKE_FUNCPTR(png_write_info
);
197 MAKE_FUNCPTR(png_write_row
);
200 static void *load_libpng(void)
202 if ((libpng_handle
= wine_dlopen(SONAME_LIBPNG
, RTLD_NOW
, NULL
, 0)) != NULL
)
204 #define LOAD_FUNCPTR(f) \
205 if((p##f = wine_dlsym(libpng_handle, #f, NULL, 0)) == NULL) { \
206 libpng_handle = NULL; \
209 LOAD_FUNCPTR(png_create_info_struct
);
210 LOAD_FUNCPTR(png_create_write_struct
);
211 LOAD_FUNCPTR(png_destroy_write_struct
);
212 LOAD_FUNCPTR(png_init_io
);
213 LOAD_FUNCPTR(png_set_bgr
);
214 LOAD_FUNCPTR(png_set_IHDR
);
215 LOAD_FUNCPTR(png_set_text
);
216 LOAD_FUNCPTR(png_write_end
);
217 LOAD_FUNCPTR(png_write_info
);
218 LOAD_FUNCPTR(png_write_row
);
221 return libpng_handle
;
224 static BOOL
SaveIconResAsPNG(const BITMAPINFO
*pIcon
, const char *png_filename
, LPCWSTR commentW
)
226 static const char comment_key
[] = "Created from";
231 int nXORWidthBytes
, nANDWidthBytes
, color_type
= 0, i
, j
;
232 BYTE
*row
, *copy
= NULL
;
233 const BYTE
*pXOR
, *pAND
= NULL
;
234 int nWidth
= pIcon
->bmiHeader
.biWidth
;
235 int nHeight
= pIcon
->bmiHeader
.biHeight
;
236 int nBpp
= pIcon
->bmiHeader
.biBitCount
;
241 color_type
|= PNG_COLOR_MASK_ALPHA
;
244 color_type
|= PNG_COLOR_MASK_COLOR
;
250 if (!libpng_handle
&& !load_libpng())
252 WINE_WARN("Unable to load libpng\n");
256 if (!(fp
= fopen(png_filename
, "w")))
258 WINE_ERR("unable to open '%s' for writing: %s\n", png_filename
, strerror(errno
));
262 nXORWidthBytes
= 4 * ((nWidth
* nBpp
+ 31) / 32);
263 nANDWidthBytes
= 4 * ((nWidth
+ 31 ) / 32);
264 pXOR
= (const BYTE
*) pIcon
+ sizeof(BITMAPINFOHEADER
) + pIcon
->bmiHeader
.biClrUsed
* sizeof(RGBQUAD
);
265 if (nHeight
> nWidth
)
268 pAND
= pXOR
+ nHeight
* nXORWidthBytes
;
271 /* Apply mask if present */
276 /* copy bytes before modifying them */
277 copy
= HeapAlloc( GetProcessHeap(), 0, nHeight
* nXORWidthBytes
);
278 memcpy( copy
, pXOR
, nHeight
* nXORWidthBytes
);
281 /* image and mask are upside down reversed */
282 row
= copy
+ (nHeight
- 1) * nXORWidthBytes
;
284 /* top left corner */
285 bgColor
.rgbRed
= row
[0];
286 bgColor
.rgbGreen
= row
[1];
287 bgColor
.rgbBlue
= row
[2];
288 bgColor
.rgbReserved
= 0;
290 for (i
= 0; i
< nHeight
; i
++, row
-= nXORWidthBytes
)
291 for (j
= 0; j
< nWidth
; j
++, row
+= nBpp
>> 3)
294 RGBQUAD
*pixel
= (RGBQUAD
*)row
;
295 pixel
->rgbBlue
= bgColor
.rgbBlue
;
296 pixel
->rgbGreen
= bgColor
.rgbGreen
;
297 pixel
->rgbRed
= bgColor
.rgbRed
;
299 pixel
->rgbReserved
= bgColor
.rgbReserved
;
305 if (!(png_ptr
= ppng_create_write_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
)) ||
306 !(info_ptr
= ppng_create_info_struct(png_ptr
)))
309 if (setjmp(png_jmpbuf(png_ptr
)))
311 /* All future errors jump here */
312 WINE_ERR("png error\n");
316 ppng_init_io(png_ptr
, fp
);
317 ppng_set_IHDR(png_ptr
, info_ptr
, nWidth
, nHeight
, 8,
320 PNG_COMPRESSION_TYPE_DEFAULT
,
321 PNG_FILTER_TYPE_DEFAULT
);
324 comment
.compression
= PNG_TEXT_COMPRESSION_NONE
;
325 comment
.key
= (png_charp
)comment_key
;
326 i
= WideCharToMultiByte(CP_UNIXCP
, 0, commentW
, -1, NULL
, 0, NULL
, NULL
);
327 comment
.text
= HeapAlloc(GetProcessHeap(), 0, i
);
328 WideCharToMultiByte(CP_UNIXCP
, 0, commentW
, -1, comment
.text
, i
, NULL
, NULL
);
329 comment
.text_length
= i
- 1;
330 ppng_set_text(png_ptr
, info_ptr
, &comment
, 1);
333 ppng_write_info(png_ptr
, info_ptr
);
334 ppng_set_bgr(png_ptr
);
335 for (i
= nHeight
- 1; i
>= 0 ; i
--)
336 ppng_write_row(png_ptr
, (png_bytep
)pXOR
+ nXORWidthBytes
* i
);
337 ppng_write_end(png_ptr
, info_ptr
);
339 ppng_destroy_write_struct(&png_ptr
, &info_ptr
);
340 if (png_ptr
) ppng_destroy_write_struct(&png_ptr
, NULL
);
342 HeapFree(GetProcessHeap(), 0, copy
);
343 HeapFree(GetProcessHeap(), 0, comment
.text
);
347 if (png_ptr
) ppng_destroy_write_struct(&png_ptr
, NULL
);
349 unlink(png_filename
);
350 HeapFree(GetProcessHeap(), 0, copy
);
351 HeapFree(GetProcessHeap(), 0, comment
.text
);
354 #endif /* SONAME_LIBPNG */
356 static BOOL
SaveIconResAsXPM(const BITMAPINFO
*pIcon
, const char *szXPMFileName
, LPCWSTR commentW
)
366 BOOL aColorUsed
[256] = {0};
371 if (!((pIcon
->bmiHeader
.biBitCount
== 4) || (pIcon
->bmiHeader
.biBitCount
== 8)))
373 WINE_FIXME("Unsupported color depth %d-bit\n", pIcon
->bmiHeader
.biBitCount
);
377 if (!(fXPMFile
= fopen(szXPMFileName
, "w")))
379 WINE_TRACE("unable to open '%s' for writing: %s\n", szXPMFileName
, strerror(errno
));
383 i
= WideCharToMultiByte(CP_UNIXCP
, 0, commentW
, -1, NULL
, 0, NULL
, NULL
);
384 comment
= HeapAlloc(GetProcessHeap(), 0, i
);
385 WideCharToMultiByte(CP_UNIXCP
, 0, commentW
, -1, comment
, i
, NULL
, NULL
);
387 nHeight
= pIcon
->bmiHeader
.biHeight
/ 2;
388 nXORWidthBytes
= 4 * ((pIcon
->bmiHeader
.biWidth
* pIcon
->bmiHeader
.biBitCount
/ 32)
389 + ((pIcon
->bmiHeader
.biWidth
* pIcon
->bmiHeader
.biBitCount
% 32) > 0));
390 nANDWidthBytes
= 4 * ((pIcon
->bmiHeader
.biWidth
/ 32)
391 + ((pIcon
->bmiHeader
.biWidth
% 32) > 0));
392 b8BitColors
= pIcon
->bmiHeader
.biBitCount
== 8;
393 nColors
= pIcon
->bmiHeader
.biClrUsed
? pIcon
->bmiHeader
.biClrUsed
394 : 1 << pIcon
->bmiHeader
.biBitCount
;
395 pXOR
= (const BYTE
*) pIcon
+ sizeof (BITMAPINFOHEADER
) + (nColors
* sizeof (RGBQUAD
));
396 pAND
= pXOR
+ nHeight
* nXORWidthBytes
;
398 #define COLOR(x,y) (b8BitColors ? pXOR[(x) + (nHeight - (y) - 1) * nXORWidthBytes] : (x) % 2 ? pXOR[(x) / 2 + (nHeight - (y) - 1) * nXORWidthBytes] & 0xF : (pXOR[(x) / 2 + (nHeight - (y) - 1) * nXORWidthBytes] & 0xF0) >> 4)
400 for (i
= 0; i
< nHeight
; i
++) {
401 for (j
= 0; j
< pIcon
->bmiHeader
.biWidth
; j
++) {
402 if (!aColorUsed
[COLOR(j
,i
)] && !MASK(j
,i
))
404 aColorUsed
[COLOR(j
,i
)] = TRUE
;
410 if (fprintf(fXPMFile
, "/* XPM */\n/* %s */\nstatic char *icon[] = {\n", comment
) <= 0)
412 if (fprintf(fXPMFile
, "\"%d %d %d %d\",\n",
413 (int) pIcon
->bmiHeader
.biWidth
, nHeight
, nColorsUsed
+ 1, 2) <=0)
416 for (i
= 0; i
< nColors
; i
++) {
418 if (fprintf(fXPMFile
, "\"%.2X c #%.2X%.2X%.2X\",\n", i
, pIcon
->bmiColors
[i
].rgbRed
,
419 pIcon
->bmiColors
[i
].rgbGreen
, pIcon
->bmiColors
[i
].rgbBlue
) <= 0)
422 if (fprintf(fXPMFile
, "\" c None\"") <= 0)
425 for (i
= 0; i
< nHeight
; i
++)
427 if (fprintf(fXPMFile
, ",\n\"") <= 0)
429 for (j
= 0; j
< pIcon
->bmiHeader
.biWidth
; j
++)
433 if (fprintf(fXPMFile
, " ") <= 0)
437 if (fprintf(fXPMFile
, "%.2X", COLOR(j
,i
)) <= 0)
440 if (fprintf(fXPMFile
, "\"") <= 0)
443 if (fprintf(fXPMFile
, "};\n") <= 0)
449 HeapFree(GetProcessHeap(), 0, comment
);
454 HeapFree(GetProcessHeap(), 0, comment
);
456 unlink( szXPMFileName
);
460 static BOOL CALLBACK
EnumResNameProc(HMODULE hModule
, LPCWSTR lpszType
, LPWSTR lpszName
, LONG_PTR lParam
)
462 ENUMRESSTRUCT
*sEnumRes
= (ENUMRESSTRUCT
*) lParam
;
464 if (!sEnumRes
->nIndex
--)
466 *sEnumRes
->pResInfo
= FindResourceW(hModule
, lpszName
, (LPCWSTR
)RT_GROUP_ICON
);
473 static BOOL
extract_icon32(LPCWSTR szFileName
, int nIndex
, char *szXPMFileName
)
477 LPCWSTR lpName
= NULL
;
479 GRPICONDIR
*pIconDir
;
481 ENUMRESSTRUCT sEnumRes
;
487 hModule
= LoadLibraryExW(szFileName
, 0, LOAD_LIBRARY_AS_DATAFILE
);
490 WINE_WARN("LoadLibraryExW (%s) failed, error %d\n",
491 wine_dbgstr_w(szFileName
), GetLastError());
497 hResInfo
= FindResourceW(hModule
, MAKEINTRESOURCEW(-nIndex
), (LPCWSTR
)RT_GROUP_ICON
);
498 WINE_TRACE("FindResourceW (%s) called, return %p, error %d\n",
499 wine_dbgstr_w(szFileName
), hResInfo
, GetLastError());
504 sEnumRes
.pResInfo
= &hResInfo
;
505 sEnumRes
.nIndex
= nIndex
;
506 if (!EnumResourceNamesW(hModule
, (LPCWSTR
)RT_GROUP_ICON
,
507 EnumResNameProc
, (LONG_PTR
)&sEnumRes
) &&
508 sEnumRes
.nIndex
!= -1)
510 WINE_TRACE("EnumResourceNamesW failed, error %d\n", GetLastError());
516 if ((hResData
= LoadResource(hModule
, hResInfo
)))
518 if ((pIconDir
= LockResource(hResData
)))
520 for (i
= 0; i
< pIconDir
->idCount
; i
++)
522 if (pIconDir
->idEntries
[i
].wBitCount
>= nMaxBits
)
524 if ((pIconDir
->idEntries
[i
].bHeight
* pIconDir
->idEntries
[i
].bWidth
) >= nMax
)
526 lpName
= MAKEINTRESOURCEW(pIconDir
->idEntries
[i
].nID
);
527 nMax
= pIconDir
->idEntries
[i
].bHeight
* pIconDir
->idEntries
[i
].bWidth
;
528 nMaxBits
= pIconDir
->idEntries
[i
].wBitCount
;
534 FreeResource(hResData
);
539 WINE_WARN("found no icon\n");
540 FreeLibrary(hModule
);
544 if ((hResInfo
= FindResourceW(hModule
, lpName
, (LPCWSTR
)RT_ICON
)))
546 if ((hResData
= LoadResource(hModule
, hResInfo
)))
548 if ((pIcon
= LockResource(hResData
)))
551 if (SaveIconResAsPNG(pIcon
, szXPMFileName
, szFileName
))
556 memcpy(szXPMFileName
+ strlen(szXPMFileName
) - 3, "xpm", 3);
557 if (SaveIconResAsXPM(pIcon
, szXPMFileName
, szFileName
))
562 FreeResource(hResData
);
566 FreeLibrary(hModule
);
570 static BOOL
ExtractFromEXEDLL(LPCWSTR szFileName
, int nIndex
, char *szXPMFileName
)
572 if (!extract_icon32(szFileName
, nIndex
, szXPMFileName
) /*&&
573 !extract_icon16(szFileName, szXPMFileName)*/)
578 static int ExtractFromICO(LPCWSTR szFileName
, char *szXPMFileName
)
580 FILE *fICOFile
= NULL
;
582 ICONDIRENTRY
*pIconDirEntry
= NULL
;
583 int nMax
= 0, nMaxBits
= 0;
587 char *filename
= NULL
;
589 filename
= wine_get_unix_file_name(szFileName
);
590 if (!(fICOFile
= fopen(filename
, "r")))
592 WINE_TRACE("unable to open '%s' for reading: %s\n", filename
, strerror(errno
));
596 if (fread(&iconDir
, sizeof (ICONDIR
), 1, fICOFile
) != 1 ||
597 (iconDir
.idReserved
!= 0) || (iconDir
.idType
!= 1))
599 WINE_WARN("Invalid ico file format\n");
603 if ((pIconDirEntry
= HeapAlloc(GetProcessHeap(), 0, iconDir
.idCount
* sizeof (ICONDIRENTRY
))) == NULL
)
605 if (fread(pIconDirEntry
, sizeof (ICONDIRENTRY
), iconDir
.idCount
, fICOFile
) != iconDir
.idCount
)
608 for (i
= 0; i
< iconDir
.idCount
; i
++)
610 WINE_TRACE("[%d]: %d x %d @ %d\n", i
, pIconDirEntry
[i
].bWidth
, pIconDirEntry
[i
].bHeight
, pIconDirEntry
[i
].wBitCount
);
611 if (pIconDirEntry
[i
].wBitCount
>= nMaxBits
&&
612 (pIconDirEntry
[i
].bHeight
* pIconDirEntry
[i
].bWidth
) >= nMax
)
615 nMax
= pIconDirEntry
[i
].bHeight
* pIconDirEntry
[i
].bWidth
;
616 nMaxBits
= pIconDirEntry
[i
].wBitCount
;
619 WINE_TRACE("Selected: %d\n", nIndex
);
621 if ((pIcon
= HeapAlloc(GetProcessHeap(), 0, pIconDirEntry
[nIndex
].dwBytesInRes
)) == NULL
)
623 if (fseek(fICOFile
, pIconDirEntry
[nIndex
].dwImageOffset
, SEEK_SET
))
625 if (fread(pIcon
, pIconDirEntry
[nIndex
].dwBytesInRes
, 1, fICOFile
) != 1)
629 /* Prefer PNG over XPM */
631 if (!SaveIconResAsPNG(pIcon
, szXPMFileName
, szFileName
))
634 memcpy(szXPMFileName
+ strlen(szXPMFileName
) - 3, "xpm", 3);
635 if (!SaveIconResAsXPM(pIcon
, szXPMFileName
, szFileName
))
639 HeapFree(GetProcessHeap(), 0, pIcon
);
640 HeapFree(GetProcessHeap(), 0, pIconDirEntry
);
642 HeapFree(GetProcessHeap(), 0, filename
);
646 HeapFree(GetProcessHeap(), 0, pIcon
);
647 HeapFree(GetProcessHeap(), 0, pIconDirEntry
);
648 if (fICOFile
) fclose(fICOFile
);
649 HeapFree(GetProcessHeap(), 0, filename
);
653 static BOOL
create_default_icon( const char *filename
, const char* comment
)
658 if (!(fXPM
= fopen(filename
, "w"))) return FALSE
;
659 if (fprintf(fXPM
, "/* XPM */\n/* %s */\nstatic char * icon[] = {", comment
) <= 0)
661 for (i
= 0; i
< sizeof(wine_xpm
)/sizeof(wine_xpm
[0]); i
++) {
662 if (fprintf( fXPM
, "\n\"%s\",", wine_xpm
[i
]) <= 0)
665 if (fprintf( fXPM
, "};\n" ) <=0)
676 static unsigned short crc16(const char* string
)
678 unsigned short crc
= 0;
681 for (i
= 0; string
[i
] != 0; i
++)
684 for (j
= 0; j
< 8; c
>>= 1, j
++)
686 xor_poly
= (c
^ crc
) & 1;
695 static char *strdupA( const char *str
)
699 if (!str
) return NULL
;
700 if ((ret
= HeapAlloc( GetProcessHeap(), 0, strlen(str
) + 1 ))) strcpy( ret
, str
);
704 static char* heap_printf(const char *format
, ...)
711 va_start(args
, format
);
714 buffer
= HeapAlloc(GetProcessHeap(), 0, size
);
717 n
= vsnprintf(buffer
, size
, format
, args
);
724 HeapFree(GetProcessHeap(), 0, buffer
);
727 if (!buffer
) return NULL
;
728 ret
= HeapReAlloc(GetProcessHeap(), 0, buffer
, strlen(buffer
) + 1 );
729 if (!ret
) ret
= buffer
;
733 static BOOL
create_directories(char *directory
)
738 for (i
= 0; directory
[i
]; i
++)
740 if (i
> 0 && directory
[i
] == '/')
743 mkdir(directory
, 0777);
747 if (mkdir(directory
, 0777) && errno
!= EEXIST
)
753 /* extract an icon from an exe or icon file; helper for IPersistFile_fnSave */
754 static char *extract_icon( LPCWSTR path
, int index
, const char *destFilename
, BOOL bWait
)
757 char *iconsdir
= NULL
, *ico_path
= NULL
, *ico_name
, *xpm_path
= NULL
;
761 /* Where should we save the icon? */
762 WINE_TRACE("path=[%s] index=%d\n", wine_dbgstr_w(path
), index
);
763 iconsdir
= heap_printf("%s/icons", xdg_data_dir
);
766 if (mkdir(iconsdir
, 0777) && errno
!= EEXIST
)
768 WINE_WARN("couldn't make icons directory %s\n", wine_dbgstr_a(iconsdir
));
774 WINE_TRACE("no icon created\n");
778 /* Determine the icon base name */
779 n
= WideCharToMultiByte(CP_UNIXCP
, 0, path
, -1, NULL
, 0, NULL
, NULL
);
780 ico_path
= HeapAlloc(GetProcessHeap(), 0, n
);
781 WideCharToMultiByte(CP_UNIXCP
, 0, path
, -1, ico_path
, n
, NULL
, NULL
);
784 if (*s
=='/' || *s
=='\\') {
792 if (*ico_name
=='\\') *ico_name
++='\0';
793 s
=strrchr(ico_name
,'.');
796 /* Compute the source-path hash */
799 /* Try to treat the source file as an exe */
801 xpm_path
=heap_printf("%s/%s.png",iconsdir
,destFilename
);
803 xpm_path
=heap_printf("%s/%04x_%s.%d.png",iconsdir
,crc
,ico_name
,index
);
804 if (xpm_path
== NULL
)
806 WINE_ERR("could not extract icon %s, out of memory\n", wine_dbgstr_a(ico_name
));
810 if (ExtractFromEXEDLL( path
, index
, xpm_path
))
813 /* Must be something else, ignore the index in that case */
815 sprintf(xpm_path
,"%s/%s.png",iconsdir
,destFilename
);
817 sprintf(xpm_path
,"%s/%04x_%s.png",iconsdir
,crc
,ico_name
);
818 if (ExtractFromICO( path
, xpm_path
))
823 sprintf(xpm_path
,"%s/%s.xpm",iconsdir
,destFilename
);
825 sprintf(xpm_path
,"%s/%04x_%s.xpm",iconsdir
,crc
,ico_name
);
826 if (create_default_icon( xpm_path
, ico_path
))
830 HeapFree( GetProcessHeap(), 0, xpm_path
);
834 HeapFree(GetProcessHeap(), 0, iconsdir
);
835 HeapFree(GetProcessHeap(), 0, ico_path
);
839 static HKEY
open_menus_reg_key(void)
841 static const WCHAR Software_Wine_FileOpenAssociationsW
[] = {
842 'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\','M','e','n','u','F','i','l','e','s',0};
844 if (RegCreateKeyW(HKEY_CURRENT_USER
, Software_Wine_FileOpenAssociationsW
, &assocKey
) == ERROR_SUCCESS
)
849 static BOOL
write_desktop_entry(const char *unix_link
, const char *location
, const char *linkname
,
850 const char *path
, const char *args
, const char *descr
,
851 const char *workdir
, const char *icon
)
855 WINE_TRACE("(%s,%s,%s,%s,%s,%s,%s,%s)\n", wine_dbgstr_a(unix_link
), wine_dbgstr_a(location
),
856 wine_dbgstr_a(linkname
), wine_dbgstr_a(path
), wine_dbgstr_a(args
),
857 wine_dbgstr_a(descr
), wine_dbgstr_a(workdir
), wine_dbgstr_a(icon
));
859 file
= fopen(location
, "w");
863 fprintf(file
, "[Desktop Entry]\n");
864 fprintf(file
, "Name=%s\n", linkname
);
865 fprintf(file
, "Exec=env WINEPREFIX=\"%s\" wine \"%s\" %s\n",
866 wine_get_config_dir(), path
, args
);
867 fprintf(file
, "Type=Application\n");
868 fprintf(file
, "StartupNotify=true\n");
869 if (descr
&& lstrlenA(descr
))
870 fprintf(file
, "Comment=%s\n", descr
);
871 if (workdir
&& lstrlenA(workdir
))
872 fprintf(file
, "Path=%s\n", workdir
);
873 if (icon
&& lstrlenA(icon
))
874 fprintf(file
, "Icon=%s\n", icon
);
880 HKEY hkey
= open_menus_reg_key();
883 RegSetValueExA(hkey
, location
, 0, REG_SZ
, (BYTE
*) unix_link
, lstrlenA(unix_link
) + 1);
893 static BOOL
write_directory_entry(const char *directory
, const char *location
)
897 WINE_TRACE("(%s,%s)\n", wine_dbgstr_a(directory
), wine_dbgstr_a(location
));
899 file
= fopen(location
, "w");
903 fprintf(file
, "[Desktop Entry]\n");
904 fprintf(file
, "Type=Directory\n");
905 if (strcmp(directory
, "wine") == 0)
907 fprintf(file
, "Name=Wine\n");
908 fprintf(file
, "Icon=wine\n");
912 fprintf(file
, "Name=%s\n", directory
);
913 fprintf(file
, "Icon=folder\n");
920 static BOOL
write_menu_file(const char *unix_link
, const char *filename
)
923 FILE *tempfile
= NULL
;
926 char *menuPath
= NULL
;
931 WINE_TRACE("(%s)\n", wine_dbgstr_a(filename
));
935 tempfilename
= heap_printf("%s/wine-menu-XXXXXX", xdg_config_dir
);
938 int tempfd
= mkstemps(tempfilename
, 0);
941 tempfile
= fdopen(tempfd
, "w");
947 else if (errno
== EEXIST
)
949 HeapFree(GetProcessHeap(), 0, tempfilename
);
952 HeapFree(GetProcessHeap(), 0, tempfilename
);
957 fprintf(tempfile
, "<!DOCTYPE Menu PUBLIC \"-//freedesktop//DTD Menu 1.0//EN\"\n");
958 fprintf(tempfile
, "\"http://www.freedesktop.org/standards/menu-spec/menu-1.0.dtd\">\n");
959 fprintf(tempfile
, "<Menu>\n");
960 fprintf(tempfile
, " <Name>Applications</Name>\n");
962 name
= HeapAlloc(GetProcessHeap(), 0, lstrlenA(filename
) + 1);
963 if (name
== NULL
) goto end
;
965 for (i
= 0; filename
[i
]; i
++)
967 name
[i
] = filename
[i
];
968 if (filename
[i
] == '/')
973 fprintf(tempfile
, " <Menu>\n");
974 fprintf(tempfile
, " <Name>%s%s</Name>\n", count
? "" : "wine-", name
);
975 fprintf(tempfile
, " <Directory>%s%s.directory</Directory>\n", count
? "" : "wine-", name
);
976 dir_file_name
= heap_printf("%s/desktop-directories/%s%s.directory",
977 xdg_data_dir
, count
? "" : "wine-", name
);
980 if (stat(dir_file_name
, &st
) != 0 && errno
== ENOENT
)
981 write_directory_entry(lastEntry
, dir_file_name
);
982 HeapFree(GetProcessHeap(), 0, dir_file_name
);
985 lastEntry
= &name
[i
+1];
991 fprintf(tempfile
, " <Include>\n");
992 fprintf(tempfile
, " <Filename>%s</Filename>\n", name
);
993 fprintf(tempfile
, " </Include>\n");
994 for (i
= 0; i
< count
; i
++)
995 fprintf(tempfile
, " </Menu>\n");
996 fprintf(tempfile
, "</Menu>\n");
998 menuPath
= heap_printf("%s/%s", xdg_config_dir
, name
);
999 if (menuPath
== NULL
) goto end
;
1000 strcpy(menuPath
+ strlen(menuPath
) - strlen(".desktop"), ".menu");
1007 ret
= (rename(tempfilename
, menuPath
) == 0);
1008 if (!ret
&& tempfilename
)
1009 remove(tempfilename
);
1010 HeapFree(GetProcessHeap(), 0, tempfilename
);
1013 HKEY hkey
= open_menus_reg_key();
1016 RegSetValueExA(hkey
, menuPath
, 0, REG_SZ
, (BYTE
*) unix_link
, lstrlenA(unix_link
) + 1);
1020 HeapFree(GetProcessHeap(), 0, name
);
1021 HeapFree(GetProcessHeap(), 0, menuPath
);
1025 static BOOL
write_menu_entry(const char *unix_link
, const char *link
, const char *path
, const char *args
,
1026 const char *descr
, const char *workdir
, const char *icon
)
1028 const char *linkname
;
1029 char *desktopPath
= NULL
;
1031 char *filename
= NULL
;
1034 WINE_TRACE("(%s, %s, %s, %s, %s, %s, %s)\n", wine_dbgstr_a(unix_link
), wine_dbgstr_a(link
),
1035 wine_dbgstr_a(path
), wine_dbgstr_a(args
), wine_dbgstr_a(descr
),
1036 wine_dbgstr_a(workdir
), wine_dbgstr_a(icon
));
1038 linkname
= strrchr(link
, '/');
1039 if (linkname
== NULL
)
1044 desktopPath
= heap_printf("%s/applications/wine/%s.desktop", xdg_data_dir
, link
);
1047 WINE_WARN("out of memory creating menu entry\n");
1051 desktopDir
= strrchr(desktopPath
, '/');
1053 if (!create_directories(desktopPath
))
1055 WINE_WARN("couldn't make parent directories for %s\n", wine_dbgstr_a(desktopPath
));
1060 if (!write_desktop_entry(unix_link
, desktopPath
, linkname
, path
, args
, descr
, workdir
, icon
))
1062 WINE_WARN("couldn't make desktop entry %s\n", wine_dbgstr_a(desktopPath
));
1067 filename
= heap_printf("wine/%s.desktop", link
);
1068 if (!filename
|| !write_menu_file(unix_link
, filename
))
1070 WINE_WARN("couldn't make menu file %s\n", wine_dbgstr_a(filename
));
1075 HeapFree(GetProcessHeap(), 0, desktopPath
);
1076 HeapFree(GetProcessHeap(), 0, filename
);
1080 /* This escapes \ in filenames */
1081 static LPSTR
escape(LPCWSTR arg
)
1088 while((esc
= strchrW(esc
, '\\')))
1094 len
+= WideCharToMultiByte(CP_UNIXCP
, 0, arg
, -1, NULL
, 0, NULL
, NULL
);
1095 narg
= HeapAlloc(GetProcessHeap(), 0, len
);
1100 n
= WideCharToMultiByte(CP_UNIXCP
, 0, arg
, 1, x
, len
, NULL
, NULL
);
1104 *x
++='\\'; /* escape \ */
1111 /* Return a heap-allocated copy of the unix format difference between the two
1112 * Windows-format paths.
1113 * locn is the owning location
1114 * link is within locn
1116 static char *relative_path( LPCWSTR link
, LPCWSTR locn
)
1118 char *unix_locn
, *unix_link
;
1119 char *relative
= NULL
;
1121 unix_locn
= wine_get_unix_file_name(locn
);
1122 unix_link
= wine_get_unix_file_name(link
);
1123 if (unix_locn
&& unix_link
)
1125 size_t len_unix_locn
, len_unix_link
;
1126 len_unix_locn
= strlen (unix_locn
);
1127 len_unix_link
= strlen (unix_link
);
1128 if (len_unix_locn
< len_unix_link
&& memcmp (unix_locn
, unix_link
, len_unix_locn
) == 0 && unix_link
[len_unix_locn
] == '/')
1131 char *p
= strrchr (unix_link
+ len_unix_locn
, '/');
1132 p
= strrchr (p
, '.');
1136 len_unix_link
= p
- unix_link
;
1138 len_rel
= len_unix_link
- len_unix_locn
;
1139 relative
= HeapAlloc(GetProcessHeap(), 0, len_rel
);
1142 memcpy (relative
, unix_link
+ len_unix_locn
+ 1, len_rel
);
1147 WINE_WARN("Could not separate the relative link path of %s in %s\n", wine_dbgstr_w(link
), wine_dbgstr_w(locn
));
1148 HeapFree(GetProcessHeap(), 0, unix_locn
);
1149 HeapFree(GetProcessHeap(), 0, unix_link
);
1153 /***********************************************************************
1157 * returns TRUE if successful
1158 * *loc will contain CS_DESKTOPDIRECTORY, CS_STARTMENU, CS_STARTUP etc.
1159 * *relative will contain the address of a heap-allocated copy of the portion
1160 * of the filename that is within the specified location, in unix form
1162 static BOOL
GetLinkLocation( LPCWSTR linkfile
, DWORD
*loc
, char **relative
)
1164 WCHAR filename
[MAX_PATH
], shortfilename
[MAX_PATH
], buffer
[MAX_PATH
];
1165 DWORD len
, i
, r
, filelen
;
1166 const DWORD locations
[] = {
1167 CSIDL_STARTUP
, CSIDL_DESKTOPDIRECTORY
, CSIDL_STARTMENU
,
1168 CSIDL_COMMON_STARTUP
, CSIDL_COMMON_DESKTOPDIRECTORY
,
1169 CSIDL_COMMON_STARTMENU
};
1171 WINE_TRACE("%s\n", wine_dbgstr_w(linkfile
));
1172 filelen
=GetFullPathNameW( linkfile
, MAX_PATH
, shortfilename
, NULL
);
1173 if (filelen
==0 || filelen
>MAX_PATH
)
1176 WINE_TRACE("%s\n", wine_dbgstr_w(shortfilename
));
1178 /* the CSLU Toolkit uses a short path name when creating .lnk files;
1179 * expand or our hardcoded list won't match.
1181 filelen
=GetLongPathNameW(shortfilename
, filename
, MAX_PATH
);
1182 if (filelen
==0 || filelen
>MAX_PATH
)
1185 WINE_TRACE("%s\n", wine_dbgstr_w(filename
));
1187 for( i
=0; i
<sizeof(locations
)/sizeof(locations
[0]); i
++ )
1189 if (!SHGetSpecialFolderPathW( 0, buffer
, locations
[i
], FALSE
))
1192 len
= lstrlenW(buffer
);
1193 if (len
>= MAX_PATH
)
1194 continue; /* We've just trashed memory! Hopefully we are OK */
1196 if (len
> filelen
|| filename
[len
]!='\\')
1198 /* do a lstrcmpinW */
1200 r
= lstrcmpiW( filename
, buffer
);
1201 filename
[len
] = '\\';
1205 /* return the remainder of the string and link type */
1206 *loc
= locations
[i
];
1207 *relative
= relative_path (filename
, buffer
);
1208 return (*relative
!= NULL
);
1214 /* gets the target path directly or through MSI */
1215 static HRESULT
get_cmdline( IShellLinkW
*sl
, LPWSTR szPath
, DWORD pathSize
,
1216 LPWSTR szArgs
, DWORD argsSize
)
1218 IShellLinkDataList
*dl
= NULL
;
1219 EXP_DARWIN_LINK
*dar
= NULL
;
1225 hr
= IShellLinkW_GetPath( sl
, szPath
, pathSize
, NULL
, SLGP_RAWPATH
);
1226 if (hr
== S_OK
&& szPath
[0])
1228 IShellLinkW_GetArguments( sl
, szArgs
, argsSize
);
1232 hr
= IShellLinkW_QueryInterface( sl
, &IID_IShellLinkDataList
, (LPVOID
*) &dl
);
1236 hr
= IShellLinkDataList_CopyDataBlock( dl
, EXP_DARWIN_ID_SIG
, (LPVOID
*) &dar
);
1243 hr
= CommandLineFromMsiDescriptor( dar
->szwDarwinID
, NULL
, &cmdSize
);
1244 if (hr
== ERROR_SUCCESS
)
1247 szCmdline
= HeapAlloc( GetProcessHeap(), 0, cmdSize
*sizeof(WCHAR
) );
1248 hr
= CommandLineFromMsiDescriptor( dar
->szwDarwinID
, szCmdline
, &cmdSize
);
1249 WINE_TRACE(" command : %s\n", wine_dbgstr_w(szCmdline
));
1250 if (hr
== ERROR_SUCCESS
)
1253 int bcount
, in_quotes
;
1255 /* Extract the application path */
1262 if ((*s
==0x0009 || *s
==0x0020) && !in_quotes
)
1264 /* skip the remaining spaces */
1267 } while (*s
==0x0009 || *s
==0x0020);
1270 else if (*s
==0x005c)
1276 else if (*s
==0x0022)
1279 if ((bcount
& 1)==0)
1281 /* Preceded by an even number of '\', this is
1282 * half that number of '\', plus a quote which
1286 in_quotes
=!in_quotes
;
1291 /* Preceded by an odd number of '\', this is
1292 * half that number of '\' followed by a '"'
1302 /* a regular character */
1306 if ((d
-szPath
) == pathSize
)
1308 /* Keep processing the path till we get to the
1309 * arguments, but 'stand still'
1314 /* Close the application path */
1317 lstrcpynW(szArgs
, s
, argsSize
);
1319 HeapFree( GetProcessHeap(), 0, szCmdline
);
1324 IShellLinkDataList_Release( dl
);
1328 static WCHAR
* assoc_query(ASSOCSTR assocStr
, LPCWSTR name
, LPCWSTR extra
)
1331 WCHAR
*value
= NULL
;
1333 hr
= AssocQueryStringW(0, assocStr
, name
, extra
, NULL
, &size
);
1336 value
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
1339 hr
= AssocQueryStringW(0, assocStr
, name
, extra
, value
, &size
);
1342 HeapFree(GetProcessHeap(), 0, value
);
1350 static char* wchars_to_utf8_chars(LPCWSTR string
)
1353 INT size
= WideCharToMultiByte(CP_UTF8
, 0, string
, -1, NULL
, 0, NULL
, NULL
);
1354 ret
= HeapAlloc(GetProcessHeap(), 0, size
);
1356 WideCharToMultiByte(CP_UTF8
, 0, string
, -1, ret
, size
, NULL
, NULL
);
1360 static char *slashes_to_minuses(const char *string
)
1363 char *ret
= HeapAlloc(GetProcessHeap(), 0, lstrlenA(string
) + 1);
1366 for (i
= 0; string
[i
]; i
++)
1368 if (string
[i
] == '/')
1379 static BOOL
next_line(FILE *file
, char **line
, int *size
)
1386 *line
= HeapAlloc(GetProcessHeap(), 0, *size
);
1388 while (*line
!= NULL
)
1390 if (fgets(&(*line
)[pos
], *size
- pos
, file
) == NULL
)
1392 HeapFree(GetProcessHeap(), 0, *line
);
1398 pos
= strlen(*line
);
1399 cr
= strchr(*line
, '\n');
1404 line2
= HeapReAlloc(GetProcessHeap(), 0, *line
, *size
);
1409 HeapFree(GetProcessHeap(), 0, *line
);
1422 static BOOL
add_mimes(const char *xdg_data_dir
, struct list
*mime_types
)
1424 char *globs_filename
= NULL
;
1426 globs_filename
= heap_printf("%s/mime/globs", xdg_data_dir
);
1429 FILE *globs_file
= fopen(globs_filename
, "r");
1430 if (globs_file
) /* doesn't have to exist */
1434 while (ret
&& (ret
= next_line(globs_file
, &line
, &size
)) && line
)
1437 struct xdg_mime_type
*mime_type_entry
= NULL
;
1438 if (line
[0] != '#' && (pos
= strchr(line
, ':')))
1440 mime_type_entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct xdg_mime_type
));
1441 if (mime_type_entry
)
1444 mime_type_entry
->mimeType
= strdupA(line
);
1445 mime_type_entry
->glob
= strdupA(pos
+ 1);
1446 if (mime_type_entry
->mimeType
&& mime_type_entry
->glob
)
1447 list_add_tail(mime_types
, &mime_type_entry
->entry
);
1450 HeapFree(GetProcessHeap(), 0, mime_type_entry
->mimeType
);
1451 HeapFree(GetProcessHeap(), 0, mime_type_entry
->glob
);
1452 HeapFree(GetProcessHeap(), 0, mime_type_entry
);
1460 HeapFree(GetProcessHeap(), 0, line
);
1463 HeapFree(GetProcessHeap(), 0, globs_filename
);
1470 static void free_native_mime_types(struct list
*native_mime_types
)
1472 struct xdg_mime_type
*mime_type_entry
, *mime_type_entry2
;
1474 LIST_FOR_EACH_ENTRY_SAFE(mime_type_entry
, mime_type_entry2
, native_mime_types
, struct xdg_mime_type
, entry
)
1476 list_remove(&mime_type_entry
->entry
);
1477 HeapFree(GetProcessHeap(), 0, mime_type_entry
->glob
);
1478 HeapFree(GetProcessHeap(), 0, mime_type_entry
->mimeType
);
1479 HeapFree(GetProcessHeap(), 0, mime_type_entry
);
1481 HeapFree(GetProcessHeap(), 0, native_mime_types
);
1484 static BOOL
build_native_mime_types(const char *xdg_data_home
, struct list
**mime_types
)
1486 char *xdg_data_dirs
;
1491 xdg_data_dirs
= getenv("XDG_DATA_DIRS");
1492 if (xdg_data_dirs
== NULL
)
1493 xdg_data_dirs
= heap_printf("/usr/local/share/:/usr/share/");
1495 xdg_data_dirs
= strdupA(xdg_data_dirs
);
1499 *mime_types
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct list
));
1505 list_init(*mime_types
);
1506 ret
= add_mimes(xdg_data_home
, *mime_types
);
1509 for (begin
= xdg_data_dirs
; (end
= strchr(begin
, ':')); begin
= end
+ 1)
1512 ret
= add_mimes(begin
, *mime_types
);
1518 ret
= add_mimes(begin
, *mime_types
);
1523 HeapFree(GetProcessHeap(), 0, xdg_data_dirs
);
1527 if (!ret
&& *mime_types
)
1529 free_native_mime_types(*mime_types
);
1535 static BOOL
match_glob(struct list
*native_mime_types
, const char *extension
,
1539 struct xdg_mime_type
*mime_type_entry
;
1540 int matchLength
= 0;
1544 LIST_FOR_EACH_ENTRY(mime_type_entry
, native_mime_types
, struct xdg_mime_type
, entry
)
1546 if (fnmatch(mime_type_entry
->glob
, extension
, 0) == 0)
1548 if (*match
== NULL
|| matchLength
< strlen(mime_type_entry
->glob
))
1550 *match
= mime_type_entry
->mimeType
;
1551 matchLength
= strlen(mime_type_entry
->glob
);
1558 *match
= strdupA(*match
);
1568 static BOOL
freedesktop_mime_type_for_extension(struct list
*native_mime_types
,
1569 const char *extensionA
,
1573 WCHAR
*lower_extensionW
;
1575 BOOL ret
= match_glob(native_mime_types
, extensionA
, mime_type
);
1576 if (ret
== FALSE
|| *mime_type
!= NULL
)
1578 len
= strlenW(extensionW
);
1579 lower_extensionW
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1)*sizeof(WCHAR
));
1580 if (lower_extensionW
)
1582 char *lower_extensionA
;
1583 memcpy(lower_extensionW
, extensionW
, (len
+ 1)*sizeof(WCHAR
));
1584 strlwrW(lower_extensionW
);
1585 lower_extensionA
= wchars_to_utf8_chars(lower_extensionW
);
1586 if (lower_extensionA
)
1588 ret
= match_glob(native_mime_types
, lower_extensionA
, mime_type
);
1589 HeapFree(GetProcessHeap(), 0, lower_extensionA
);
1594 WINE_FIXME("out of memory\n");
1596 HeapFree(GetProcessHeap(), 0, lower_extensionW
);
1601 WINE_FIXME("out of memory\n");
1606 static CHAR
* reg_get_valA(HKEY key
, LPCSTR subkey
, LPCSTR name
)
1609 if (RegGetValueA(key
, subkey
, name
, RRF_RT_REG_SZ
, NULL
, NULL
, &size
) == ERROR_SUCCESS
)
1611 CHAR
*ret
= HeapAlloc(GetProcessHeap(), 0, size
);
1614 if (RegGetValueA(key
, subkey
, name
, RRF_RT_REG_SZ
, NULL
, ret
, &size
) == ERROR_SUCCESS
)
1617 HeapFree(GetProcessHeap(), 0, ret
);
1622 static WCHAR
* reg_get_valW(HKEY key
, LPCWSTR subkey
, LPCWSTR name
)
1625 if (RegGetValueW(key
, subkey
, name
, RRF_RT_REG_SZ
, NULL
, NULL
, &size
) == ERROR_SUCCESS
)
1627 WCHAR
*ret
= HeapAlloc(GetProcessHeap(), 0, size
);
1630 if (RegGetValueW(key
, subkey
, name
, RRF_RT_REG_SZ
, NULL
, ret
, &size
) == ERROR_SUCCESS
)
1633 HeapFree(GetProcessHeap(), 0, ret
);
1638 static HKEY
open_associations_reg_key(void)
1640 static const WCHAR Software_Wine_FileOpenAssociationsW
[] = {
1641 '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};
1643 if (RegCreateKeyW(HKEY_CURRENT_USER
, Software_Wine_FileOpenAssociationsW
, &assocKey
) == ERROR_SUCCESS
)
1648 static BOOL
has_association_changed(LPCSTR extensionA
, LPCWSTR extensionW
, LPCSTR mimeType
, LPCWSTR progId
, LPCSTR appName
, LPCWSTR docName
)
1650 static const WCHAR ProgIDW
[] = {'P','r','o','g','I','D',0};
1651 static const WCHAR DocNameW
[] = {'D','o','c','N','a','m','e',0};
1655 if ((assocKey
= open_associations_reg_key()))
1662 valueA
= reg_get_valA(assocKey
, extensionA
, "MimeType");
1663 if (!valueA
|| lstrcmpA(valueA
, mimeType
))
1665 HeapFree(GetProcessHeap(), 0, valueA
);
1667 value
= reg_get_valW(assocKey
, extensionW
, ProgIDW
);
1668 if (!value
|| strcmpW(value
, progId
))
1670 HeapFree(GetProcessHeap(), 0, value
);
1672 valueA
= reg_get_valA(assocKey
, extensionA
, "AppName");
1673 if (!valueA
|| lstrcmpA(valueA
, appName
))
1675 HeapFree(GetProcessHeap(), 0, valueA
);
1677 value
= reg_get_valW(assocKey
, extensionW
, DocNameW
);
1678 if (docName
&& (!value
|| strcmpW(value
, docName
)))
1680 HeapFree(GetProcessHeap(), 0, value
);
1682 RegCloseKey(assocKey
);
1686 WINE_ERR("error opening associations registry key\n");
1692 static void update_association(LPCWSTR extension
, LPCSTR mimeType
, LPCWSTR progId
, LPCSTR appName
, LPCWSTR docName
, LPCSTR desktopFile
)
1694 static const WCHAR ProgIDW
[] = {'P','r','o','g','I','D',0};
1695 static const WCHAR DocNameW
[] = {'D','o','c','N','a','m','e',0};
1698 if ((assocKey
= open_associations_reg_key()))
1701 if (RegCreateKeyW(assocKey
, extension
, &subkey
) == ERROR_SUCCESS
)
1703 RegSetValueExA(subkey
, "MimeType", 0, REG_SZ
, (BYTE
*) mimeType
, lstrlenA(mimeType
) + 1);
1704 RegSetValueExW(subkey
, ProgIDW
, 0, REG_SZ
, (BYTE
*) progId
, (lstrlenW(progId
) + 1) * sizeof(WCHAR
));
1705 RegSetValueExA(subkey
, "AppName", 0, REG_SZ
, (BYTE
*) appName
, lstrlenA(appName
) + 1);
1707 RegSetValueExW(subkey
, DocNameW
, 0, REG_SZ
, (BYTE
*) docName
, (lstrlenW(docName
) + 1) * sizeof(WCHAR
));
1708 RegSetValueExA(subkey
, "DesktopFile", 0, REG_SZ
, (BYTE
*) desktopFile
, (lstrlenA(desktopFile
) + 1));
1709 RegCloseKey(subkey
);
1712 WINE_ERR("could not create extension subkey\n");
1713 RegCloseKey(assocKey
);
1716 WINE_ERR("could not open file associations key\n");
1719 static BOOL
cleanup_associations(void)
1721 static const WCHAR openW
[] = {'o','p','e','n',0};
1723 BOOL hasChanged
= FALSE
;
1724 if ((assocKey
= open_associations_reg_key()))
1728 for (i
= 0; !done
; i
++)
1730 WCHAR
*extensionW
= NULL
;
1731 char *extensionA
= NULL
;
1737 HeapFree(GetProcessHeap(), 0, extensionW
);
1738 extensionW
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
1739 if (extensionW
== NULL
)
1741 WINE_ERR("out of memory\n");
1742 ret
= ERROR_OUTOFMEMORY
;
1745 ret
= RegEnumKeyExW(assocKey
, i
, extensionW
, &size
, NULL
, NULL
, NULL
, NULL
);
1747 } while (ret
== ERROR_MORE_DATA
);
1749 if (ret
== ERROR_SUCCESS
)
1752 extensionA
= wchars_to_utf8_chars(extensionW
);
1753 if (extensionA
== NULL
)
1755 WINE_ERR("out of memory\n");
1759 command
= assoc_query(ASSOCSTR_COMMAND
, extensionW
, openW
);
1760 if (command
== NULL
)
1762 char *desktopFile
= reg_get_valA(assocKey
, extensionA
, "DesktopFile");
1765 WINE_TRACE("removing file type association for %s\n", wine_dbgstr_a(extensionA
));
1766 remove(desktopFile
);
1768 RegDeleteKeyW(assocKey
, extensionW
);
1770 HeapFree(GetProcessHeap(), 0, desktopFile
);
1772 HeapFree(GetProcessHeap(), 0, command
);
1776 if (ret
!= ERROR_NO_MORE_ITEMS
)
1777 WINE_ERR("error %d while reading registry\n", ret
);
1781 HeapFree(GetProcessHeap(), 0, extensionA
);
1782 HeapFree(GetProcessHeap(), 0, extensionW
);
1784 RegCloseKey(assocKey
);
1787 WINE_ERR("could not open file associations key\n");
1791 static BOOL
write_freedesktop_mime_type_entry(const char *packages_dir
, const char *dot_extension
,
1792 const char *mime_type
, const char *comment
)
1797 WINE_TRACE("writing MIME type %s, extension=%s, comment=%s\n", wine_dbgstr_a(mime_type
),
1798 wine_dbgstr_a(dot_extension
), wine_dbgstr_a(comment
));
1800 filename
= heap_printf("%s/x-wine-extension-%s.xml", packages_dir
, &dot_extension
[1]);
1803 FILE *packageFile
= fopen(filename
, "w");
1806 fprintf(packageFile
, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1807 fprintf(packageFile
, "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n");
1808 fprintf(packageFile
, " <mime-type type=\"%s\">\n", mime_type
);
1809 fprintf(packageFile
, " <glob pattern=\"*%s\"/>\n", dot_extension
);
1811 fprintf(packageFile
, " <comment>%s</comment>\n", comment
);
1812 fprintf(packageFile
, " </mime-type>\n");
1813 fprintf(packageFile
, "</mime-info>\n");
1815 fclose(packageFile
);
1818 WINE_ERR("error writing file %s\n", filename
);
1819 HeapFree(GetProcessHeap(), 0, filename
);
1822 WINE_ERR("out of memory\n");
1826 static BOOL
is_extension_blacklisted(LPCWSTR extension
)
1828 /* These are managed through external tools like wine.desktop, to evade malware created file type associations */
1829 static const WCHAR comW
[] = {'.','c','o','m',0};
1830 static const WCHAR exeW
[] = {'.','e','x','e',0};
1831 static const WCHAR msiW
[] = {'.','m','s','i',0};
1833 if (!strcmpiW(extension
, comW
) ||
1834 !strcmpiW(extension
, exeW
) ||
1835 !strcmpiW(extension
, msiW
))
1840 static BOOL
write_freedesktop_association_entry(const char *desktopPath
, const char *dot_extension
,
1841 const char *friendlyAppName
, const char *mimeType
,
1847 WINE_TRACE("writing association for file type %s, friendlyAppName=%s, MIME type %s, progID=%s, to file %s\n",
1848 wine_dbgstr_a(dot_extension
), wine_dbgstr_a(friendlyAppName
), wine_dbgstr_a(mimeType
),
1849 wine_dbgstr_a(progId
), wine_dbgstr_a(desktopPath
));
1851 desktop
= fopen(desktopPath
, "w");
1854 fprintf(desktop
, "[Desktop Entry]\n");
1855 fprintf(desktop
, "Type=Application\n");
1856 fprintf(desktop
, "Name=%s\n", friendlyAppName
);
1857 fprintf(desktop
, "MimeType=%s\n", mimeType
);
1858 fprintf(desktop
, "Exec=wine start /ProgIDOpen %s %%f\n", progId
);
1859 fprintf(desktop
, "NoDisplay=true\n");
1860 fprintf(desktop
, "StartupNotify=true\n");
1865 WINE_ERR("error writing association file %s\n", wine_dbgstr_a(desktopPath
));
1869 static BOOL
generate_associations(const char *xdg_data_home
, const char *packages_dir
, const char *applications_dir
)
1871 static const WCHAR openW
[] = {'o','p','e','n',0};
1872 struct list
*nativeMimeTypes
= NULL
;
1875 BOOL hasChanged
= FALSE
;
1877 if (!build_native_mime_types(xdg_data_home
, &nativeMimeTypes
))
1879 WINE_ERR("could not build native MIME types\n");
1885 WCHAR
*extensionW
= NULL
;
1890 HeapFree(GetProcessHeap(), 0, extensionW
);
1891 extensionW
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
1892 if (extensionW
== NULL
)
1894 WINE_ERR("out of memory\n");
1895 ret
= ERROR_OUTOFMEMORY
;
1898 ret
= RegEnumKeyExW(HKEY_CLASSES_ROOT
, i
, extensionW
, &size
, NULL
, NULL
, NULL
, NULL
);
1900 } while (ret
== ERROR_MORE_DATA
);
1902 if (ret
== ERROR_SUCCESS
&& extensionW
[0] == '.' && !is_extension_blacklisted(extensionW
))
1904 char *extensionA
= NULL
;
1905 WCHAR
*commandW
= NULL
;
1906 WCHAR
*friendlyDocNameW
= NULL
;
1907 char *friendlyDocNameA
= NULL
;
1908 WCHAR
*iconW
= NULL
;
1910 WCHAR
*contentTypeW
= NULL
;
1911 char *mimeTypeA
= NULL
;
1912 WCHAR
*friendlyAppNameW
= NULL
;
1913 char *friendlyAppNameA
= NULL
;
1914 WCHAR
*progIdW
= NULL
;
1915 char *progIdA
= NULL
;
1917 extensionA
= wchars_to_utf8_chars(extensionW
);
1918 if (extensionA
== NULL
)
1920 WINE_ERR("out of memory\n");
1924 friendlyDocNameW
= assoc_query(ASSOCSTR_FRIENDLYDOCNAME
, extensionW
, NULL
);
1925 if (friendlyDocNameW
)
1927 friendlyDocNameA
= wchars_to_utf8_chars(friendlyDocNameW
);
1928 if (friendlyDocNameA
== NULL
)
1930 WINE_ERR("out of memory\n");
1935 iconW
= assoc_query(ASSOCSTR_DEFAULTICON
, extensionW
, NULL
);
1937 contentTypeW
= assoc_query(ASSOCSTR_CONTENTTYPE
, extensionW
, NULL
);
1939 if (!freedesktop_mime_type_for_extension(nativeMimeTypes
, extensionA
, extensionW
, &mimeTypeA
))
1942 if (mimeTypeA
== NULL
)
1944 if (contentTypeW
!= NULL
)
1945 mimeTypeA
= wchars_to_utf8_chars(contentTypeW
);
1947 mimeTypeA
= heap_printf("application/x-wine-extension-%s", &extensionA
[1]);
1949 if (mimeTypeA
!= NULL
)
1951 /* Gnome seems to ignore the <icon> tag in MIME packages,
1952 * and the default name is more intuitive anyway.
1956 char *flattened_mime
= slashes_to_minuses(mimeTypeA
);
1960 WCHAR
*comma
= strrchrW(iconW
, ',');
1964 index
= atoiW(comma
+ 1);
1966 iconA
= extract_icon(iconW
, index
, flattened_mime
, FALSE
);
1967 HeapFree(GetProcessHeap(), 0, flattened_mime
);
1971 write_freedesktop_mime_type_entry(packages_dir
, extensionA
, mimeTypeA
, friendlyDocNameA
);
1976 WINE_FIXME("out of memory\n");
1981 commandW
= assoc_query(ASSOCSTR_COMMAND
, extensionW
, openW
);
1982 if (commandW
== NULL
)
1983 /* no command => no application is associated */
1986 friendlyAppNameW
= assoc_query(ASSOCSTR_FRIENDLYAPPNAME
, extensionW
, NULL
);
1987 if (friendlyAppNameW
)
1989 friendlyAppNameA
= wchars_to_utf8_chars(friendlyAppNameW
);
1990 if (friendlyAppNameA
== NULL
)
1992 WINE_ERR("out of memory\n");
1998 friendlyAppNameA
= heap_printf("A Wine application");
1999 if (friendlyAppNameA
== NULL
)
2001 WINE_ERR("out of memory\n");
2006 progIdW
= reg_get_valW(HKEY_CLASSES_ROOT
, extensionW
, NULL
);
2009 progIdA
= wchars_to_utf8_chars(progIdW
);
2010 if (progIdA
== NULL
)
2012 WINE_ERR("out of memory\n");
2017 goto end
; /* no progID => not a file type association */
2019 if (has_association_changed(extensionA
, extensionW
, mimeTypeA
, progIdW
, friendlyAppNameA
, friendlyDocNameW
))
2021 char *desktopPath
= heap_printf("%s/wine-extension-%s.desktop", applications_dir
, &extensionA
[1]);
2024 if (write_freedesktop_association_entry(desktopPath
, extensionA
, friendlyAppNameA
, mimeTypeA
, progIdA
))
2027 update_association(extensionW
, mimeTypeA
, progIdW
, friendlyAppNameA
, friendlyDocNameW
, desktopPath
);
2029 HeapFree(GetProcessHeap(), 0, desktopPath
);
2034 HeapFree(GetProcessHeap(), 0, extensionA
);
2035 HeapFree(GetProcessHeap(), 0, commandW
);
2036 HeapFree(GetProcessHeap(), 0, friendlyDocNameW
);
2037 HeapFree(GetProcessHeap(), 0, friendlyDocNameA
);
2038 HeapFree(GetProcessHeap(), 0, iconW
);
2039 HeapFree(GetProcessHeap(), 0, iconA
);
2040 HeapFree(GetProcessHeap(), 0, contentTypeW
);
2041 HeapFree(GetProcessHeap(), 0, mimeTypeA
);
2042 HeapFree(GetProcessHeap(), 0, friendlyAppNameW
);
2043 HeapFree(GetProcessHeap(), 0, friendlyAppNameA
);
2044 HeapFree(GetProcessHeap(), 0, progIdW
);
2045 HeapFree(GetProcessHeap(), 0, progIdA
);
2047 HeapFree(GetProcessHeap(), 0, extensionW
);
2048 if (ret
!= ERROR_SUCCESS
)
2052 free_native_mime_types(nativeMimeTypes
);
2056 static BOOL
InvokeShellLinker( IShellLinkW
*sl
, LPCWSTR link
, BOOL bWait
)
2058 static const WCHAR startW
[] = {'\\','c','o','m','m','a','n','d',
2059 '\\','s','t','a','r','t','.','e','x','e',0};
2060 char *link_name
= NULL
, *icon_name
= NULL
, *work_dir
= NULL
;
2061 char *escaped_path
= NULL
, *escaped_args
= NULL
, *escaped_description
= NULL
;
2062 WCHAR szTmp
[INFOTIPSIZE
];
2063 WCHAR szDescription
[INFOTIPSIZE
], szPath
[MAX_PATH
], szWorkDir
[MAX_PATH
];
2064 WCHAR szArgs
[INFOTIPSIZE
], szIconPath
[MAX_PATH
];
2065 int iIconId
= 0, r
= -1;
2068 char *unix_link
= NULL
;
2072 WINE_ERR("Link name is null\n");
2076 if( !GetLinkLocation( link
, &csidl
, &link_name
) )
2078 WINE_WARN("Unknown link location %s. Ignoring.\n",wine_dbgstr_w(link
));
2081 if (!in_desktop_dir(csidl
) && !in_startmenu(csidl
))
2083 WINE_WARN("Not under desktop or start menu. Ignoring.\n");
2086 WINE_TRACE("Link : %s\n", wine_dbgstr_a(link_name
));
2089 IShellLinkW_GetWorkingDirectory( sl
, szTmp
, MAX_PATH
);
2090 ExpandEnvironmentStringsW(szTmp
, szWorkDir
, MAX_PATH
);
2091 WINE_TRACE("workdir : %s\n", wine_dbgstr_w(szWorkDir
));
2094 IShellLinkW_GetDescription( sl
, szTmp
, INFOTIPSIZE
);
2095 ExpandEnvironmentStringsW(szTmp
, szDescription
, INFOTIPSIZE
);
2096 WINE_TRACE("description: %s\n", wine_dbgstr_w(szDescription
));
2098 get_cmdline( sl
, szPath
, MAX_PATH
, szArgs
, INFOTIPSIZE
);
2099 WINE_TRACE("path : %s\n", wine_dbgstr_w(szPath
));
2100 WINE_TRACE("args : %s\n", wine_dbgstr_w(szArgs
));
2103 IShellLinkW_GetIconLocation( sl
, szTmp
, MAX_PATH
, &iIconId
);
2104 ExpandEnvironmentStringsW(szTmp
, szIconPath
, MAX_PATH
);
2105 WINE_TRACE("icon file : %s\n", wine_dbgstr_w(szIconPath
) );
2109 LPITEMIDLIST pidl
= NULL
;
2110 IShellLinkW_GetIDList( sl
, &pidl
);
2111 if( pidl
&& SHGetPathFromIDListW( pidl
, szPath
) )
2112 WINE_TRACE("pidl path : %s\n", wine_dbgstr_w(szPath
));
2115 /* extract the icon */
2117 icon_name
= extract_icon( szIconPath
, iIconId
, NULL
, bWait
);
2119 icon_name
= extract_icon( szPath
, iIconId
, NULL
, bWait
);
2121 /* fail - try once again after parent process exit */
2126 WINE_WARN("Unable to extract icon, deferring.\n");
2129 WINE_ERR("failed to extract icon from %s\n",
2130 wine_dbgstr_w( szIconPath
[0] ? szIconPath
: szPath
));
2133 unix_link
= wine_get_unix_file_name(link
);
2134 if (unix_link
== NULL
)
2136 WINE_WARN("couldn't find unix path of %s\n", wine_dbgstr_w(link
));
2140 /* check the path */
2143 static const WCHAR exeW
[] = {'.','e','x','e',0};
2146 /* check for .exe extension */
2147 if (!(p
= strrchrW( szPath
, '.' )) ||
2148 strchrW( p
, '\\' ) || strchrW( p
, '/' ) ||
2149 lstrcmpiW( p
, exeW
))
2151 /* Not .exe - use 'start.exe' to launch this file */
2152 p
= szArgs
+ lstrlenW(szPath
) + 2;
2156 memmove( p
+1, szArgs
, min( (lstrlenW(szArgs
) + 1) * sizeof(szArgs
[0]),
2157 sizeof(szArgs
) - (p
+ 1 - szArgs
) * sizeof(szArgs
[0]) ) );
2163 lstrcpyW(szArgs
+ 1, szPath
);
2166 GetWindowsDirectoryW(szPath
, MAX_PATH
);
2167 lstrcatW(szPath
, startW
);
2170 /* convert app working dir */
2172 work_dir
= wine_get_unix_file_name( szWorkDir
);
2176 /* if there's no path... try run the link itself */
2177 lstrcpynW(szArgs
, link
, MAX_PATH
);
2178 GetWindowsDirectoryW(szPath
, MAX_PATH
);
2179 lstrcatW(szPath
, startW
);
2182 /* escape the path and parameters */
2183 escaped_path
= escape(szPath
);
2184 escaped_args
= escape(szArgs
);
2185 escaped_description
= escape(szDescription
);
2187 /* building multiple menus concurrently has race conditions */
2188 hsem
= CreateSemaphoreA( NULL
, 1, 1, "winemenubuilder_semaphore");
2189 if( WAIT_OBJECT_0
!= MsgWaitForMultipleObjects( 1, &hsem
, FALSE
, INFINITE
, QS_ALLINPUT
) )
2191 WINE_ERR("failed wait for semaphore\n");
2195 if (in_desktop_dir(csidl
))
2198 const char *lastEntry
;
2199 lastEntry
= strrchr(link_name
, '/');
2200 if (lastEntry
== NULL
)
2201 lastEntry
= link_name
;
2204 location
= heap_printf("%s/%s.desktop", xdg_desktop_dir
, lastEntry
);
2207 r
= !write_desktop_entry(NULL
, location
, lastEntry
, escaped_path
, escaped_args
, escaped_description
, work_dir
, icon_name
);
2208 HeapFree(GetProcessHeap(), 0, location
);
2212 r
= !write_menu_entry(unix_link
, link_name
, escaped_path
, escaped_args
, escaped_description
, work_dir
, icon_name
);
2214 ReleaseSemaphore( hsem
, 1, NULL
);
2217 if (hsem
) CloseHandle( hsem
);
2218 HeapFree( GetProcessHeap(), 0, icon_name
);
2219 HeapFree( GetProcessHeap(), 0, work_dir
);
2220 HeapFree( GetProcessHeap(), 0, link_name
);
2221 HeapFree( GetProcessHeap(), 0, escaped_args
);
2222 HeapFree( GetProcessHeap(), 0, escaped_path
);
2223 HeapFree( GetProcessHeap(), 0, escaped_description
);
2224 HeapFree( GetProcessHeap(), 0, unix_link
);
2227 WINE_ERR("failed to build the menu\n" );
2232 static BOOL
InvokeShellLinkerForURL( IUniformResourceLocatorW
*url
, LPCWSTR link
, BOOL bWait
)
2234 char *link_name
= NULL
;
2237 char *escaped_urlPath
= NULL
;
2242 char *unix_link
= NULL
;
2246 WINE_ERR("Link name is null\n");
2250 if( !GetLinkLocation( link
, &csidl
, &link_name
) )
2252 WINE_WARN("Unknown link location %s. Ignoring.\n",wine_dbgstr_w(link
));
2255 if (!in_desktop_dir(csidl
) && !in_startmenu(csidl
))
2257 WINE_WARN("Not under desktop or start menu. Ignoring.\n");
2261 WINE_TRACE("Link : %s\n", wine_dbgstr_a(link_name
));
2263 hr
= url
->lpVtbl
->GetURL(url
, &urlPath
);
2269 WINE_TRACE("path : %s\n", wine_dbgstr_w(urlPath
));
2271 unix_link
= wine_get_unix_file_name(link
);
2272 if (unix_link
== NULL
)
2274 WINE_WARN("couldn't find unix path of %s\n", wine_dbgstr_w(link
));
2278 escaped_urlPath
= escape(urlPath
);
2280 hSem
= CreateSemaphoreA( NULL
, 1, 1, "winemenubuilder_semaphore");
2281 if( WAIT_OBJECT_0
!= MsgWaitForMultipleObjects( 1, &hSem
, FALSE
, INFINITE
, QS_ALLINPUT
) )
2283 WINE_ERR("failed wait for semaphore\n");
2286 if (in_desktop_dir(csidl
))
2289 const char *lastEntry
;
2290 lastEntry
= strrchr(link_name
, '/');
2291 if (lastEntry
== NULL
)
2292 lastEntry
= link_name
;
2295 location
= heap_printf("%s/%s.desktop", xdg_desktop_dir
, lastEntry
);
2298 r
= !write_desktop_entry(NULL
, location
, lastEntry
, "winebrowser", escaped_urlPath
, NULL
, NULL
, NULL
);
2299 HeapFree(GetProcessHeap(), 0, location
);
2303 r
= !write_menu_entry(unix_link
, link_name
, "winebrowser", escaped_urlPath
, NULL
, NULL
, NULL
);
2305 ReleaseSemaphore(hSem
, 1, NULL
);
2310 HeapFree(GetProcessHeap(), 0, link_name
);
2311 CoTaskMemFree( urlPath
);
2312 HeapFree(GetProcessHeap(), 0, escaped_urlPath
);
2313 HeapFree(GetProcessHeap(), 0, unix_link
);
2317 static BOOL
WaitForParentProcess( void )
2319 PROCESSENTRY32 procentry
;
2320 HANDLE hsnapshot
= NULL
, hprocess
= NULL
;
2321 DWORD ourpid
= GetCurrentProcessId();
2322 BOOL ret
= FALSE
, rc
;
2324 WINE_TRACE("Waiting for parent process\n");
2325 if ((hsnapshot
= CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS
, 0 )) ==
2326 INVALID_HANDLE_VALUE
)
2328 WINE_ERR("CreateToolhelp32Snapshot failed, error %d\n", GetLastError());
2332 procentry
.dwSize
= sizeof(PROCESSENTRY32
);
2333 rc
= Process32First( hsnapshot
, &procentry
);
2336 if (procentry
.th32ProcessID
== ourpid
) break;
2337 rc
= Process32Next( hsnapshot
, &procentry
);
2341 WINE_WARN("Unable to find current process id %d when listing processes\n", ourpid
);
2345 if ((hprocess
= OpenProcess( SYNCHRONIZE
, FALSE
, procentry
.th32ParentProcessID
)) ==
2348 WINE_WARN("OpenProcess failed pid=%d, error %d\n", procentry
.th32ParentProcessID
,
2353 if (MsgWaitForMultipleObjects( 1, &hprocess
, FALSE
, INFINITE
, QS_ALLINPUT
) == WAIT_OBJECT_0
)
2356 WINE_ERR("Unable to wait for parent process, error %d\n", GetLastError());
2359 if (hprocess
) CloseHandle( hprocess
);
2360 if (hsnapshot
) CloseHandle( hsnapshot
);
2364 static BOOL
Process_Link( LPCWSTR linkname
, BOOL bWait
)
2369 WCHAR fullname
[MAX_PATH
];
2372 WINE_TRACE("%s, wait %d\n", wine_dbgstr_w(linkname
), bWait
);
2376 WINE_ERR("link name missing\n");
2380 len
=GetFullPathNameW( linkname
, MAX_PATH
, fullname
, NULL
);
2381 if (len
==0 || len
>MAX_PATH
)
2383 WINE_ERR("couldn't get full path of link file\n");
2387 r
= CoInitialize( NULL
);
2390 WINE_ERR("CoInitialize failed\n");
2394 r
= CoCreateInstance( &CLSID_ShellLink
, NULL
, CLSCTX_INPROC_SERVER
,
2395 &IID_IShellLinkW
, (LPVOID
*) &sl
);
2398 WINE_ERR("No IID_IShellLink\n");
2402 r
= IShellLinkW_QueryInterface( sl
, &IID_IPersistFile
, (LPVOID
*) &pf
);
2405 WINE_ERR("No IID_IPersistFile\n");
2409 r
= IPersistFile_Load( pf
, fullname
, STGM_READ
);
2410 if( SUCCEEDED( r
) )
2412 /* If something fails (eg. Couldn't extract icon)
2413 * wait for parent process and try again
2415 if( ! InvokeShellLinker( sl
, fullname
, bWait
) && bWait
)
2417 WaitForParentProcess();
2418 InvokeShellLinker( sl
, fullname
, FALSE
);
2423 WINE_ERR("unable to load %s\n", wine_dbgstr_w(linkname
));
2426 IPersistFile_Release( pf
);
2427 IShellLinkW_Release( sl
);
2434 static BOOL
Process_URL( LPCWSTR urlname
, BOOL bWait
)
2436 IUniformResourceLocatorW
*url
;
2439 WCHAR fullname
[MAX_PATH
];
2442 WINE_TRACE("%s, wait %d\n", wine_dbgstr_w(urlname
), bWait
);
2446 WINE_ERR("URL name missing\n");
2450 len
=GetFullPathNameW( urlname
, MAX_PATH
, fullname
, NULL
);
2451 if (len
==0 || len
>MAX_PATH
)
2453 WINE_ERR("couldn't get full path of URL file\n");
2457 r
= CoInitialize( NULL
);
2460 WINE_ERR("CoInitialize failed\n");
2464 r
= CoCreateInstance( &CLSID_InternetShortcut
, NULL
, CLSCTX_INPROC_SERVER
,
2465 &IID_IUniformResourceLocatorW
, (LPVOID
*) &url
);
2468 WINE_ERR("No IID_IUniformResourceLocatorW\n");
2472 r
= url
->lpVtbl
->QueryInterface( url
, &IID_IPersistFile
, (LPVOID
*) &pf
);
2475 WINE_ERR("No IID_IPersistFile\n");
2478 r
= IPersistFile_Load( pf
, fullname
, STGM_READ
);
2479 if( SUCCEEDED( r
) )
2481 /* If something fails (eg. Couldn't extract icon)
2482 * wait for parent process and try again
2484 if( ! InvokeShellLinkerForURL( url
, fullname
, bWait
) && bWait
)
2486 WaitForParentProcess();
2487 InvokeShellLinkerForURL( url
, fullname
, FALSE
);
2491 IPersistFile_Release( pf
);
2492 url
->lpVtbl
->Release( url
);
2499 static void RefreshFileTypeAssociations(void)
2502 char *mime_dir
= NULL
;
2503 char *packages_dir
= NULL
;
2504 char *applications_dir
= NULL
;
2507 hSem
= CreateSemaphoreA( NULL
, 1, 1, "winemenubuilder_semaphore");
2508 if( WAIT_OBJECT_0
!= MsgWaitForMultipleObjects( 1, &hSem
, FALSE
, INFINITE
, QS_ALLINPUT
) )
2510 WINE_ERR("failed wait for semaphore\n");
2516 mime_dir
= heap_printf("%s/mime", xdg_data_dir
);
2517 if (mime_dir
== NULL
)
2519 WINE_ERR("out of memory\n");
2522 create_directories(mime_dir
);
2524 packages_dir
= heap_printf("%s/packages", mime_dir
);
2525 if (packages_dir
== NULL
)
2527 WINE_ERR("out of memory\n");
2530 create_directories(packages_dir
);
2532 applications_dir
= heap_printf("%s/applications", xdg_data_dir
);
2533 if (applications_dir
== NULL
)
2535 WINE_ERR("out of memory\n");
2538 create_directories(applications_dir
);
2540 hasChanged
= generate_associations(xdg_data_dir
, packages_dir
, applications_dir
);
2541 hasChanged
|= cleanup_associations();
2544 const char *argv
[3];
2546 argv
[0] = "update-mime-database";
2549 spawnvp( _P_NOWAIT
, argv
[0], argv
);
2551 argv
[0] = "update-desktop-database";
2552 argv
[1] = applications_dir
;
2553 spawnvp( _P_NOWAIT
, argv
[0], argv
);
2559 ReleaseSemaphore(hSem
, 1, NULL
);
2562 HeapFree(GetProcessHeap(), 0, mime_dir
);
2563 HeapFree(GetProcessHeap(), 0, packages_dir
);
2564 HeapFree(GetProcessHeap(), 0, applications_dir
);
2567 static void cleanup_menus(void)
2571 hkey
= open_menus_reg_key();
2575 LSTATUS lret
= ERROR_SUCCESS
;
2576 for (i
= 0; lret
== ERROR_SUCCESS
; )
2580 DWORD valueSize
= 4096;
2581 DWORD dataSize
= 4096;
2584 lret
= ERROR_OUTOFMEMORY
;
2585 value
= HeapAlloc(GetProcessHeap(), 0, valueSize
);
2588 data
= HeapAlloc(GetProcessHeap(), 0, dataSize
);
2591 lret
= RegEnumValueA(hkey
, i
, value
, &valueSize
, NULL
, NULL
, (BYTE
*)data
, &dataSize
);
2592 if (lret
== ERROR_SUCCESS
|| lret
!= ERROR_MORE_DATA
)
2596 HeapFree(GetProcessHeap(), 0, value
);
2597 HeapFree(GetProcessHeap(), 0, data
);
2598 value
= data
= NULL
;
2600 if (lret
== ERROR_SUCCESS
)
2602 struct stat filestats
;
2603 if (stat(data
, &filestats
) < 0 && errno
== ENOENT
)
2605 WINE_TRACE("removing menu related file %s\n", value
);
2607 RegDeleteValueA(hkey
, value
);
2612 else if (lret
!= ERROR_NO_MORE_ITEMS
)
2613 WINE_WARN("error %d reading registry\n", lret
);
2614 HeapFree(GetProcessHeap(), 0, value
);
2615 HeapFree(GetProcessHeap(), 0, data
);
2620 WINE_ERR("error opening registry key, menu cleanup failed\n");
2623 static CHAR
*next_token( LPSTR
*p
)
2625 LPSTR token
= NULL
, t
= *p
;
2630 while( t
&& !token
)
2638 /* unquote the token */
2640 t
= strchr( token
, '"' );
2649 t
= strchr( token
, ' ' );
2659 static BOOL
init_xdg(void)
2661 WCHAR shellDesktopPath
[MAX_PATH
];
2662 HRESULT hr
= SHGetFolderPathW(NULL
, CSIDL_DESKTOP
, NULL
, SHGFP_TYPE_CURRENT
, shellDesktopPath
);
2664 xdg_desktop_dir
= wine_get_unix_file_name(shellDesktopPath
);
2665 if (xdg_desktop_dir
== NULL
)
2667 WINE_ERR("error looking up the desktop directory\n");
2671 if (getenv("XDG_CONFIG_HOME"))
2672 xdg_config_dir
= heap_printf("%s/menus/applications-merged", getenv("XDG_CONFIG_HOME"));
2674 xdg_config_dir
= heap_printf("%s/.config/menus/applications-merged", getenv("HOME"));
2677 create_directories(xdg_config_dir
);
2678 if (getenv("XDG_DATA_HOME"))
2679 xdg_data_dir
= strdupA(getenv("XDG_DATA_HOME"));
2681 xdg_data_dir
= heap_printf("%s/.local/share", getenv("HOME"));
2685 create_directories(xdg_data_dir
);
2686 buffer
= heap_printf("%s/desktop-directories", xdg_data_dir
);
2689 mkdir(buffer
, 0777);
2690 HeapFree(GetProcessHeap(), 0, buffer
);
2694 HeapFree(GetProcessHeap(), 0, xdg_config_dir
);
2696 WINE_ERR("out of memory\n");
2700 /***********************************************************************
2704 int PASCAL
WinMain (HINSTANCE hInstance
, HINSTANCE prev
, LPSTR cmdline
, int show
)
2706 LPSTR token
= NULL
, p
;
2714 for( p
= cmdline
; p
&& *p
; )
2716 token
= next_token( &p
);
2719 if( !lstrcmpA( token
, "-a" ) )
2721 RefreshFileTypeAssociations();
2724 if( !lstrcmpA( token
, "-r" ) )
2729 if( !lstrcmpA( token
, "-w" ) )
2731 else if ( !lstrcmpA( token
, "-u" ) )
2733 else if( token
[0] == '-' )
2735 WINE_ERR( "unknown option %s\n",token
);
2739 WCHAR link
[MAX_PATH
];
2742 MultiByteToWideChar( CP_ACP
, 0, token
, -1, link
, sizeof(link
)/sizeof(WCHAR
) );
2744 bRet
= Process_URL( link
, bWait
);
2746 bRet
= Process_Link( link
, bWait
);
2749 WINE_ERR( "failed to build menu item for %s\n",token
);