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
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * This program is used to replicate the Windows desktop and start menu
26 * into the native desktop's copies. Desktop entries are merged directly
27 * into the native desktop. The Windows Start Menu corresponds to a Wine
28 * entry within the native "start" menu and replicates the whole tree
29 * structure of the Windows Start Menu. Currently it does not differentiate
30 * between the user's desktop/start menu and the "All Users" copies.
32 * This program will read a Windows shortcut file using the IShellLink
33 * interface, then invoke wineshelllink with the appropriate arguments
34 * to create a KDE/Gnome menu entry for the shortcut.
36 * winemenubuilder [ -r ] <shortcut.lnk>
38 * If the -r parameter is passed, and the shortcut cannot be created,
39 * this program will add a RunOnce entry to invoke itself at the next
40 * reboot. 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
57 #include "wine/port.h"
76 #include "wine/unicode.h"
77 #include "wine/debug.h"
80 WINE_DEFAULT_DEBUG_CHANNEL(menubuilder);
82 #define in_desktop_dir(csidl) ((csidl)==CSIDL_DESKTOPDIRECTORY || \
83 (csidl)==CSIDL_COMMON_DESKTOPDIRECTORY)
84 #define in_startmenu(csidl) ((csidl)==CSIDL_STARTMENU || \
85 (csidl)==CSIDL_COMMON_STARTMENU)
87 /* link file formats */
108 GRPICONDIRENTRY idEntries[1];
140 /* Icon extraction routines
142 * FIXME: should use PrivateExtractIcons and friends
143 * FIXME: should not use stdio
146 static BOOL SaveIconResAsXPM(const BITMAPINFO *pIcon, const char *szXPMFileName, LPCWSTR commentW)
156 BOOL aColorUsed[256] = {0};
161 if (!((pIcon->bmiHeader.biBitCount == 4) || (pIcon->bmiHeader.biBitCount == 8)))
163 WINE_FIXME("Unsupported color depth %d-bit\n", pIcon->bmiHeader.biBitCount);
167 if (!(fXPMFile = fopen(szXPMFileName, "w")))
169 WINE_TRACE("unable to open '%s' for writing: %s\n", szXPMFileName, strerror(errno));
173 i = WideCharToMultiByte(CP_UNIXCP, 0, commentW, -1, NULL, 0, NULL, NULL);
174 comment = HeapAlloc(GetProcessHeap(), 0, i);
175 WideCharToMultiByte(CP_UNIXCP, 0, commentW, -1, comment, i, NULL, NULL);
177 nHeight = pIcon->bmiHeader.biHeight / 2;
178 nXORWidthBytes = 4 * ((pIcon->bmiHeader.biWidth * pIcon->bmiHeader.biBitCount / 32)
179 + ((pIcon->bmiHeader.biWidth * pIcon->bmiHeader.biBitCount % 32) > 0));
180 nANDWidthBytes = 4 * ((pIcon->bmiHeader.biWidth / 32)
181 + ((pIcon->bmiHeader.biWidth % 32) > 0));
182 b8BitColors = pIcon->bmiHeader.biBitCount == 8;
183 nColors = pIcon->bmiHeader.biClrUsed ? pIcon->bmiHeader.biClrUsed
184 : 1 << pIcon->bmiHeader.biBitCount;
185 pXOR = (const BYTE*) pIcon + sizeof (BITMAPINFOHEADER) + (nColors * sizeof (RGBQUAD));
186 pAND = pXOR + nHeight * nXORWidthBytes;
188 #define MASK(x,y) (pAND[(x) / 8 + (nHeight - (y) - 1) * nANDWidthBytes] & (1 << (7 - (x) % 8)))
189 #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)
191 for (i = 0; i < nHeight; i++) {
192 for (j = 0; j < pIcon->bmiHeader.biWidth; j++) {
193 if (!aColorUsed[COLOR(j,i)] && !MASK(j,i))
195 aColorUsed[COLOR(j,i)] = TRUE;
201 if (fprintf(fXPMFile, "/* XPM */\n/* %s */\nstatic char *icon[] = {\n", comment) <= 0)
203 if (fprintf(fXPMFile, "\"%d %d %d %d\",\n",
204 (int) pIcon->bmiHeader.biWidth, nHeight, nColorsUsed + 1, 2) <=0)
207 for (i = 0; i < nColors; i++) {
209 if (fprintf(fXPMFile, "\"%.2X c #%.2X%.2X%.2X\",\n", i, pIcon->bmiColors[i].rgbRed,
210 pIcon->bmiColors[i].rgbGreen, pIcon->bmiColors[i].rgbBlue) <= 0)
213 if (fprintf(fXPMFile, "\" c None\"") <= 0)
216 for (i = 0; i < nHeight; i++)
218 if (fprintf(fXPMFile, ",\n\"") <= 0)
220 for (j = 0; j < pIcon->bmiHeader.biWidth; j++)
224 if (fprintf(fXPMFile, " ") <= 0)
228 if (fprintf(fXPMFile, "%.2X", COLOR(j,i)) <= 0)
231 if (fprintf(fXPMFile, "\"") <= 0)
234 if (fprintf(fXPMFile, "};\n") <= 0)
240 HeapFree(GetProcessHeap(), 0, comment);
245 HeapFree(GetProcessHeap(), 0, comment);
247 unlink( szXPMFileName );
251 static BOOL CALLBACK EnumResNameProc(HMODULE hModule, LPCWSTR lpszType, LPWSTR lpszName, LONG_PTR lParam)
253 ENUMRESSTRUCT *sEnumRes = (ENUMRESSTRUCT *) lParam;
255 if (!sEnumRes->nIndex--)
257 *sEnumRes->pResInfo = FindResourceW(hModule, lpszName, (LPCWSTR)RT_GROUP_ICON);
264 static BOOL extract_icon32(LPCWSTR szFileName, int nIndex, const char *szXPMFileName)
268 LPCWSTR lpName = NULL;
270 GRPICONDIR *pIconDir;
272 ENUMRESSTRUCT sEnumRes;
278 hModule = LoadLibraryExW(szFileName, 0, LOAD_LIBRARY_AS_DATAFILE);
281 WINE_ERR("LoadLibraryExW (%s) failed, error %d\n",
282 wine_dbgstr_w(szFileName), GetLastError());
288 hResInfo = FindResourceW(hModule, MAKEINTRESOURCEW(-nIndex), (LPCWSTR)RT_GROUP_ICON);
289 WINE_TRACE("FindResourceW (%s) called, return %p, error %d\n",
290 wine_dbgstr_w(szFileName), hResInfo, GetLastError());
295 sEnumRes.pResInfo = &hResInfo;
296 sEnumRes.nIndex = nIndex;
297 if (!EnumResourceNamesW(hModule, (LPCWSTR)RT_GROUP_ICON,
298 EnumResNameProc, (LONG_PTR)&sEnumRes))
300 WINE_TRACE("EnumResourceNamesW failed, error %d\n", GetLastError());
306 if ((hResData = LoadResource(hModule, hResInfo)))
308 if ((pIconDir = LockResource(hResData)))
310 for (i = 0; i < pIconDir->idCount; i++)
312 if ((pIconDir->idEntries[i].wBitCount >= nMaxBits) && (pIconDir->idEntries[i].wBitCount <= 8))
314 nMaxBits = pIconDir->idEntries[i].wBitCount;
316 if ((pIconDir->idEntries[i].bHeight * pIconDir->idEntries[i].bWidth) >= nMax)
318 lpName = MAKEINTRESOURCEW(pIconDir->idEntries[i].nID);
319 nMax = pIconDir->idEntries[i].bHeight * pIconDir->idEntries[i].bWidth;
325 FreeResource(hResData);
330 WINE_ERR("found no icon\n");
331 FreeLibrary(hModule);
335 if ((hResInfo = FindResourceW(hModule, lpName, (LPCWSTR)RT_ICON)))
337 if ((hResData = LoadResource(hModule, hResInfo)))
339 if ((pIcon = LockResource(hResData)))
341 if(SaveIconResAsXPM(pIcon, szXPMFileName, szFileName))
345 FreeResource(hResData);
349 FreeLibrary(hModule);
353 static BOOL ExtractFromEXEDLL(LPCWSTR szFileName, int nIndex, const char *szXPMFileName)
355 if (!extract_icon32(szFileName, nIndex, szXPMFileName) /*&&
356 !extract_icon16(szFileName, szXPMFileName)*/)
361 static int ExtractFromICO(LPCWSTR szFileName, const char *szXPMFileName)
365 ICONDIRENTRY *pIconDirEntry;
372 filename = wine_get_unix_file_name(szFileName);
373 if (!(fICOFile = fopen(filename, "r")))
375 WINE_TRACE("unable to open '%s' for reading: %s\n", filename, strerror(errno));
379 if (fread(&iconDir, sizeof (ICONDIR), 1, fICOFile) != 1 ||
380 (iconDir.idReserved != 0) || (iconDir.idType != 1))
382 WINE_ERR("Invalid ico file format\n");
386 if ((pIconDirEntry = HeapAlloc(GetProcessHeap(), 0, iconDir.idCount * sizeof (ICONDIRENTRY))) == NULL)
388 if (fread(pIconDirEntry, sizeof (ICONDIRENTRY), iconDir.idCount, fICOFile) != iconDir.idCount)
391 for (i = 0; i < iconDir.idCount; i++)
392 if ((pIconDirEntry[i].bHeight * pIconDirEntry[i].bWidth) > nMax)
395 nMax = pIconDirEntry[i].bHeight * pIconDirEntry[i].bWidth;
397 if ((pIcon = HeapAlloc(GetProcessHeap(), 0, pIconDirEntry[nIndex].dwBytesInRes)) == NULL)
399 if (fseek(fICOFile, pIconDirEntry[nIndex].dwImageOffset, SEEK_SET))
401 if (fread(pIcon, pIconDirEntry[nIndex].dwBytesInRes, 1, fICOFile) != 1)
404 if(!SaveIconResAsXPM(pIcon, szXPMFileName, szFileName))
407 HeapFree(GetProcessHeap(), 0, pIcon);
408 HeapFree(GetProcessHeap(), 0, pIconDirEntry);
410 HeapFree(GetProcessHeap(), 0, filename);
414 HeapFree(GetProcessHeap(), 0, pIcon);
416 HeapFree(GetProcessHeap(), 0, pIconDirEntry);
420 HeapFree(GetProcessHeap(), 0, filename);
424 static BOOL create_default_icon( const char *filename, const char* comment )
429 if (!(fXPM = fopen(filename, "w"))) return FALSE;
430 if (fprintf(fXPM, "/* XPM */\n/* %s */\nstatic char * icon[] = {", comment) <= 0)
432 for (i = 0; i < sizeof(wine_xpm)/sizeof(wine_xpm[0]); i++) {
433 if (fprintf( fXPM, "\n\"%s\",", wine_xpm[i]) <= 0)
436 if (fprintf( fXPM, "};\n" ) <=0)
447 static unsigned short crc16(const char* string)
449 unsigned short crc = 0;
452 for (i = 0; string[i] != 0; i++)
455 for (j = 0; j < 8; c >>= 1, j++)
457 xor_poly = (c ^ crc) & 1;
466 /* extract an icon from an exe or icon file; helper for IPersistFile_fnSave */
467 static char *extract_icon( LPCWSTR path, int index)
471 char *iconsdir, *ico_path, *ico_name, *xpm_path;
476 /* Where should we save the icon? */
477 WINE_TRACE("path=[%s] index=%d\n", wine_dbgstr_w(path), index);
478 iconsdir=NULL; /* Default is no icon */
479 /* @@ Wine registry key: HKCU\Software\Wine\WineMenuBuilder */
480 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\WineMenuBuilder", &hkey ))
482 static const WCHAR IconsDirW[] = {'I','c','o','n','s','D','i','r',0};
486 if (!RegQueryValueExW(hkey, IconsDirW, 0, NULL, NULL, &size))
488 iconsdirW = HeapAlloc(GetProcessHeap(), 0, size);
489 RegQueryValueExW(hkey, IconsDirW, 0, NULL, (LPBYTE)iconsdirW, &size);
491 if (!(iconsdir = wine_get_unix_file_name(iconsdirW)))
493 int n = WideCharToMultiByte(CP_UNIXCP, 0, iconsdirW, -1, NULL, 0, NULL, NULL);
494 iconsdir = HeapAlloc(GetProcessHeap(), 0, n);
495 WideCharToMultiByte(CP_UNIXCP, 0, iconsdirW, -1, iconsdir, n, NULL, NULL);
497 HeapFree(GetProcessHeap(), 0, iconsdirW);
504 WCHAR path[MAX_PATH];
505 if (GetTempPathW(MAX_PATH, path))
506 iconsdir = wine_get_unix_file_name(path);
509 WINE_TRACE("no IconsDir\n");
510 return NULL; /* No icon created */
516 WINE_TRACE("icon generation disabled\n");
517 HeapFree(GetProcessHeap(), 0, iconsdir);
518 return NULL; /* No icon created */
521 /* If icon path begins with a '*' then this is a deferred call */
528 /* Determine the icon base name */
529 n = WideCharToMultiByte(CP_UNIXCP, 0, path, -1, NULL, 0, NULL, NULL);
530 ico_path = HeapAlloc(GetProcessHeap(), 0, n);
531 WideCharToMultiByte(CP_UNIXCP, 0, path, -1, ico_path, n, NULL, NULL);
534 if (*s=='/' || *s=='\\') {
542 if (*ico_name=='\\') *ico_name++='\0';
543 s=strrchr(ico_name,'.');
546 /* Compute the source-path hash */
549 /* Try to treat the source file as an exe */
550 xpm_path=HeapAlloc(GetProcessHeap(), 0, strlen(iconsdir)+1+4+1+strlen(ico_name)+1+12+1+3);
551 sprintf(xpm_path,"%s/%04x_%s.%d.xpm",iconsdir,crc,ico_name,index);
552 if (ExtractFromEXEDLL( path, index, xpm_path ))
555 /* Must be something else, ignore the index in that case */
556 sprintf(xpm_path,"%s/%04x_%s.xpm",iconsdir,crc,ico_name);
557 if (ExtractFromICO( path, xpm_path))
560 if (create_default_icon( xpm_path, ico_path ))
563 HeapFree( GetProcessHeap(), 0, xpm_path );
567 HeapFree(GetProcessHeap(), 0, iconsdir);
568 HeapFree(GetProcessHeap(), 0, ico_path);
572 static BOOL DeferToRunOnce(LPWSTR link)
576 static const WCHAR szRunOnce[] = {
577 'S','o','f','t','w','a','r','e','\\',
578 'M','i','c','r','o','s','o','f','t','\\',
579 'W','i','n','d','o','w','s','\\',
580 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
581 'R','u','n','O','n','c','e',0
583 static const WCHAR szFormat[] = { '%','s',' ','"','%','s','"',0 };
585 WCHAR szExecutable[MAX_PATH];
587 WINE_TRACE( "Deferring icon creation to reboot.\n");
589 len = GetModuleFileNameW( 0, szExecutable, MAX_PATH );
590 if (!len || len >= MAX_PATH) return FALSE;
592 len = ( lstrlenW( link ) + lstrlenW( szExecutable ) + 4)*sizeof(WCHAR);
593 buffer = HeapAlloc( GetProcessHeap(), 0, len );
597 wsprintfW( buffer, szFormat, szExecutable, link );
599 r = RegCreateKeyExW(HKEY_LOCAL_MACHINE, szRunOnce, 0,
600 NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, NULL);
601 if ( r == ERROR_SUCCESS )
603 r = RegSetValueExW(hkey, link, 0, REG_SZ,
604 (LPBYTE) buffer, (lstrlenW(buffer) + 1)*sizeof(WCHAR));
607 HeapFree(GetProcessHeap(), 0, buffer);
612 /* This escapes \ in filenames */
613 static LPSTR escape(LPCWSTR arg)
620 while((esc = strchrW(esc, '\\')))
626 len += WideCharToMultiByte(CP_UNIXCP, 0, arg, -1, NULL, 0, NULL, NULL);
627 narg = HeapAlloc(GetProcessHeap(), 0, len);
632 n = WideCharToMultiByte(CP_UNIXCP, 0, arg, 1, x, len, NULL, NULL);
636 *x++='\\'; /* escape \ */
643 static int fork_and_wait( const char *linker, const char *link_name, const char *path,
644 int desktop, const char *args, const char *icon_name,
645 const char *workdir, const char *description )
648 const char *argv[20];
651 WINE_TRACE( "linker app='%s' link='%s' mode=%s "
652 "path='%s' args='%s' icon='%s' workdir='%s' descr='%s'\n",
653 linker, link_name, desktop ? "desktop" : "menu",
654 path, args, icon_name, workdir, description );
656 argv[pos++] = linker ;
657 argv[pos++] = "--link";
658 argv[pos++] = link_name;
659 argv[pos++] = "--path";
661 argv[pos++] = desktop ? "--desktop" : "--menu";
662 if (args && strlen(args))
664 argv[pos++] = "--args";
669 argv[pos++] = "--icon";
670 argv[pos++] = icon_name;
672 if (workdir && strlen(workdir))
674 argv[pos++] = "--workdir";
675 argv[pos++] = workdir;
677 if (description && strlen(description))
679 argv[pos++] = "--descr";
680 argv[pos++] = description;
684 retcode=spawnvp( _P_WAIT, linker, argv );
686 WINE_ERR("%s returned %d\n",linker,retcode);
690 /* Return a heap-allocated copy of the unix format difference between the two
691 * Windows-format paths.
692 * locn is the owning location
693 * link is within locn
695 static char *relative_path( LPCWSTR link, LPCWSTR locn )
697 char *unix_locn, *unix_link;
698 char *relative = NULL;
700 unix_locn = wine_get_unix_file_name(locn);
701 unix_link = wine_get_unix_file_name(link);
702 if (unix_locn && unix_link)
704 size_t len_unix_locn, len_unix_link;
705 len_unix_locn = strlen (unix_locn);
706 len_unix_link = strlen (unix_link);
707 if (len_unix_locn < len_unix_link && memcmp (unix_locn, unix_link, len_unix_locn) == 0 && unix_link[len_unix_locn] == '/')
710 char *p = strrchr (unix_link + len_unix_locn, '/');
711 p = strrchr (p, '.');
715 len_unix_link = p - unix_link;
717 len_rel = len_unix_link - len_unix_locn;
718 relative = HeapAlloc(GetProcessHeap(), 0, len_rel);
721 memcpy (relative, unix_link + len_unix_locn + 1, len_rel);
726 WINE_WARN("Could not separate the relative link path of %s in %s\n", wine_dbgstr_w(link), wine_dbgstr_w(locn));
727 HeapFree(GetProcessHeap(), 0, unix_locn);
728 HeapFree(GetProcessHeap(), 0, unix_link);
732 /***********************************************************************
736 * returns TRUE if successful
737 * *loc will contain CS_DESKTOPDIRECTORY, CS_STARTMENU, CS_STARTUP etc.
738 * *relative will contain the address of a heap-allocated copy of the portion
739 * of the filename that is within the specified location, in unix form
741 static BOOL GetLinkLocation( LPCWSTR linkfile, DWORD *loc, char **relative )
743 WCHAR filename[MAX_PATH], buffer[MAX_PATH];
744 DWORD len, i, r, filelen;
745 const DWORD locations[] = {
746 CSIDL_STARTUP, CSIDL_DESKTOPDIRECTORY, CSIDL_STARTMENU,
747 CSIDL_COMMON_STARTUP, CSIDL_COMMON_DESKTOPDIRECTORY,
748 CSIDL_COMMON_STARTMENU };
750 WINE_TRACE("%s\n", wine_dbgstr_w(linkfile));
751 filelen=GetFullPathNameW( linkfile, MAX_PATH, filename, NULL );
752 if (filelen==0 || filelen>MAX_PATH)
755 WINE_TRACE("%s\n", wine_dbgstr_w(filename));
757 for( i=0; i<sizeof(locations)/sizeof(locations[0]); i++ )
759 if (!SHGetSpecialFolderPathW( 0, buffer, locations[i], FALSE ))
762 len = lstrlenW(buffer);
764 continue; /* We've just trashed memory! Hopefully we are OK */
766 if (len > filelen || filename[len]!='\\')
768 /* do a lstrcmpinW */
770 r = lstrcmpiW( filename, buffer );
771 filename[len] = '\\';
775 /* return the remainder of the string and link type */
777 *relative = relative_path (filename, buffer);
778 return (*relative != NULL);
784 /* gets the target path directly or through MSI */
785 static HRESULT get_cmdline( IShellLinkW *sl, LPWSTR szPath, DWORD pathSize,
786 LPWSTR szArgs, DWORD argsSize)
788 IShellLinkDataList *dl = NULL;
789 EXP_DARWIN_LINK *dar = NULL;
795 hr = IShellLinkW_GetPath( sl, szPath, pathSize, NULL, SLGP_RAWPATH );
796 if (hr == S_OK && szPath[0])
798 IShellLinkW_GetArguments( sl, szArgs, argsSize );
802 hr = IShellLinkW_QueryInterface( sl, &IID_IShellLinkDataList, (LPVOID*) &dl );
806 hr = IShellLinkDataList_CopyDataBlock( dl, EXP_DARWIN_ID_SIG, (LPVOID*) &dar );
813 hr = CommandLineFromMsiDescriptor( dar->szwDarwinID, NULL, &cmdSize );
814 if (hr == ERROR_SUCCESS)
817 szCmdline = HeapAlloc( GetProcessHeap(), 0, cmdSize*sizeof(WCHAR) );
818 hr = CommandLineFromMsiDescriptor( dar->szwDarwinID, szCmdline, &cmdSize );
819 WINE_TRACE(" command : %s\n", wine_dbgstr_w(szCmdline));
820 if (hr == ERROR_SUCCESS)
823 int bcount, in_quotes;
825 /* Extract the application path */
832 if ((*s==0x0009 || *s==0x0020) && !in_quotes)
834 /* skip the remaining spaces */
837 } while (*s==0x0009 || *s==0x0020);
851 /* Preceded by an even number of '\', this is
852 * half that number of '\', plus a quote which
856 in_quotes=!in_quotes;
861 /* Preceded by an odd number of '\', this is
862 * half that number of '\' followed by a '"'
872 /* a regular character */
876 if ((d-szPath) == pathSize)
878 /* Keep processing the path till we get to the
879 * arguments, but 'stand still'
884 /* Close the application path */
887 lstrcpynW(szArgs, s, argsSize);
889 HeapFree( GetProcessHeap(), 0, szCmdline );
894 IShellLinkDataList_Release( dl );
898 static BOOL InvokeShellLinker( IShellLinkW *sl, LPCWSTR link, BOOL bAgain )
900 char *link_name = NULL, *icon_name = NULL, *work_dir = NULL;
901 char *escaped_path = NULL, *escaped_args = NULL, *escaped_description = NULL;
902 WCHAR szDescription[INFOTIPSIZE], szPath[MAX_PATH], szWorkDir[MAX_PATH];
903 WCHAR szArgs[INFOTIPSIZE], szIconPath[MAX_PATH];
904 int iIconId = 0, r = -1;
909 WINE_ERR("Link name is null\n");
913 if( !GetLinkLocation( link, &csidl, &link_name ) )
915 WINE_WARN("Unknown link location '%s'. Ignoring.\n",wine_dbgstr_w(link));
918 if (!in_desktop_dir(csidl) && !in_startmenu(csidl))
920 WINE_WARN("Not under desktop or start menu. Ignoring.\n");
923 WINE_TRACE("Link : %s\n", wine_dbgstr_a(link_name));
926 IShellLinkW_GetWorkingDirectory( sl, szWorkDir, MAX_PATH );
927 WINE_TRACE("workdir : %s\n", wine_dbgstr_w(szWorkDir));
929 szDescription[0] = 0;
930 IShellLinkW_GetDescription( sl, szDescription, INFOTIPSIZE );
931 WINE_TRACE("description: %s\n", wine_dbgstr_w(szDescription));
933 get_cmdline( sl, szPath, MAX_PATH, szArgs, INFOTIPSIZE);
934 WINE_TRACE("path : %s\n", wine_dbgstr_w(szPath));
935 WINE_TRACE("args : %s\n", wine_dbgstr_w(szArgs));
938 IShellLinkW_GetIconLocation( sl, szIconPath, MAX_PATH, &iIconId );
939 WINE_TRACE("icon file : %s\n", wine_dbgstr_w(szIconPath) );
943 LPITEMIDLIST pidl = NULL;
944 IShellLinkW_GetIDList( sl, &pidl );
945 if( pidl && SHGetPathFromIDListW( pidl, szPath ) )
946 WINE_TRACE("pidl path : %s\n", wine_dbgstr_w(szPath));
949 /* extract the icon */
951 icon_name = extract_icon( szIconPath , iIconId );
953 icon_name = extract_icon( szPath, iIconId );
955 /* fail - try once again at reboot time */
960 WINE_WARN("Unable to extract icon, deferring.\n");
963 WINE_ERR("failed to extract icon.\n");
969 static const WCHAR exeW[] = {'.','e','x','e',0};
972 /* check for .exe extension */
973 if (!(p = strrchrW( szPath, '.' ))) return FALSE;
974 if (strchrW( p, '\\' ) || strchrW( p, '/' )) return FALSE;
975 if (lstrcmpiW( p, exeW )) return FALSE;
977 /* convert app working dir */
979 work_dir = wine_get_unix_file_name( szWorkDir );
983 static const WCHAR startW[] = {
984 '\\','c','o','m','m','a','n','d',
985 '\\','s','t','a','r','t','.','e','x','e',0};
987 /* if there's no path... try run the link itself */
988 lstrcpynW(szArgs, link, MAX_PATH);
989 GetWindowsDirectoryW(szPath, MAX_PATH);
990 lstrcatW(szPath, startW);
993 /* escape the path and parameters */
994 escaped_path = escape(szPath);
995 escaped_args = escape(szArgs);
996 escaped_description = escape(szDescription);
998 r = fork_and_wait("wineshelllink", link_name, escaped_path,
999 in_desktop_dir(csidl), escaped_args, icon_name,
1000 work_dir ? work_dir : "", escaped_description);
1003 HeapFree( GetProcessHeap(), 0, icon_name );
1004 HeapFree( GetProcessHeap(), 0, work_dir );
1005 HeapFree( GetProcessHeap(), 0, link_name );
1006 HeapFree( GetProcessHeap(), 0, escaped_args );
1007 HeapFree( GetProcessHeap(), 0, escaped_path );
1008 HeapFree( GetProcessHeap(), 0, escaped_description );
1012 WINE_ERR("failed to fork and exec wineshelllink\n" );
1020 static BOOL Process_Link( LPCWSTR linkname, BOOL bAgain )
1025 WCHAR fullname[MAX_PATH];
1028 WINE_TRACE("%s, again %d\n", wine_dbgstr_w(linkname), bAgain);
1032 WINE_ERR("link name missing\n");
1036 len=GetFullPathNameW( linkname, MAX_PATH, fullname, NULL );
1037 if (len==0 || len>MAX_PATH)
1039 WINE_ERR("couldn't get full path of link file\n");
1043 r = CoInitialize( NULL );
1046 WINE_ERR("CoInitialize failed\n");
1050 r = CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1051 &IID_IShellLinkW, (LPVOID *) &sl );
1054 WINE_ERR("No IID_IShellLink\n");
1058 r = IShellLinkW_QueryInterface( sl, &IID_IPersistFile, (LPVOID*) &pf );
1061 WINE_ERR("No IID_IPersistFile\n");
1065 r = IPersistFile_Load( pf, fullname, STGM_READ );
1066 if( SUCCEEDED( r ) )
1068 /* If something fails (eg. Couldn't extract icon)
1069 * defer this menu entry to reboot via runonce
1071 if( ! InvokeShellLinker( sl, fullname, bAgain ) && bAgain )
1072 DeferToRunOnce( fullname );
1074 WINE_TRACE("Success.\n");
1077 IPersistFile_Release( pf );
1078 IShellLinkW_Release( sl );
1086 static CHAR *next_token( LPSTR *p )
1088 LPSTR token = NULL, t = *p;
1093 while( t && !token )
1101 /* unquote the token */
1103 t = strchr( token, '"' );
1112 t = strchr( token, ' ' );
1122 /***********************************************************************
1126 int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
1128 LPSTR token = NULL, p;
1129 BOOL bAgain = FALSE;
1130 HANDLE hsem = CreateSemaphoreA( NULL, 1, 1, "winemenubuilder_semaphore");
1133 /* running multiple instances of wineshelllink
1134 at the same time may be dangerous */
1135 if( WAIT_OBJECT_0 != WaitForSingleObject( hsem, INFINITE ) )
1141 for( p = cmdline; p && *p; )
1143 token = next_token( &p );
1146 if( !lstrcmpA( token, "-r" ) )
1148 else if( token[0] == '-' )
1150 WINE_ERR( "unknown option %s\n",token);
1154 WCHAR link[MAX_PATH];
1156 MultiByteToWideChar( CP_ACP, 0, token, -1, link, sizeof(link)/sizeof(WCHAR) );
1157 if( !Process_Link( link, bAgain ) )
1159 WINE_ERR( "failed to build menu item for %s\n",token);
1165 ReleaseSemaphore( hsem, 1, NULL );
1166 CloseHandle( hsem );