winemenubuilder.c 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742
  1. /*
  2. * Helper program to build unix menu entries
  3. *
  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
  10. *
  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.
  15. *
  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.
  20. *
  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
  24. *
  25. *
  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.
  32. *
  33. * This program will read a Windows shortcut file using the IShellLink
  34. * interface, then create a KDE/GNOME menu entry for the shortcut.
  35. *
  36. * winemenubuilder [ -w ] <shortcut.lnk>
  37. *
  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.
  42. *
  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).
  63. */
  64. #include "config.h"
  65. #include "wine/port.h"
  66. #include <ctype.h>
  67. #include <stdio.h>
  68. #include <string.h>
  69. #ifdef HAVE_UNISTD_H
  70. #include <unistd.h>
  71. #endif
  72. #include <errno.h>
  73. #include <stdarg.h>
  74. #ifdef HAVE_FNMATCH_H
  75. #include <fnmatch.h>
  76. #endif
  77. #define COBJMACROS
  78. #define NONAMELESSUNION
  79. #include <windows.h>
  80. #include <shlobj.h>
  81. #include <objidl.h>
  82. #include <shlguid.h>
  83. #include <appmgmt.h>
  84. #include <tlhelp32.h>
  85. #include <intshcut.h>
  86. #include <shlwapi.h>
  87. #include <initguid.h>
  88. #include <wincodec.h>
  89. #include "wine/unicode.h"
  90. #include "wine/debug.h"
  91. #include "wine/list.h"
  92. #include "wine/rbtree.h"
  93. WINE_DEFAULT_DEBUG_CHANNEL(menubuilder);
  94. #define in_desktop_dir(csidl) ((csidl)==CSIDL_DESKTOPDIRECTORY || \
  95. (csidl)==CSIDL_COMMON_DESKTOPDIRECTORY)
  96. #define in_startmenu(csidl) ((csidl)==CSIDL_STARTMENU || \
  97. (csidl)==CSIDL_COMMON_STARTMENU)
  98. #define IS_OPTION_TRUE(ch) \
  99. ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
  100. /* link file formats */
  101. #include "pshpack1.h"
  102. typedef struct
  103. {
  104. BYTE bWidth;
  105. BYTE bHeight;
  106. BYTE bColorCount;
  107. BYTE bReserved;
  108. WORD wPlanes;
  109. WORD wBitCount;
  110. DWORD dwBytesInRes;
  111. WORD nID;
  112. } GRPICONDIRENTRY;
  113. typedef struct
  114. {
  115. WORD idReserved;
  116. WORD idType;
  117. WORD idCount;
  118. GRPICONDIRENTRY idEntries[1];
  119. } GRPICONDIR;
  120. typedef struct
  121. {
  122. BYTE bWidth;
  123. BYTE bHeight;
  124. BYTE bColorCount;
  125. BYTE bReserved;
  126. WORD wPlanes;
  127. WORD wBitCount;
  128. DWORD dwBytesInRes;
  129. DWORD dwImageOffset;
  130. } ICONDIRENTRY;
  131. typedef struct
  132. {
  133. WORD idReserved;
  134. WORD idType;
  135. WORD idCount;
  136. } ICONDIR;
  137. typedef struct
  138. {
  139. WORD offset;
  140. WORD length;
  141. WORD flags;
  142. WORD id;
  143. WORD handle;
  144. WORD usage;
  145. } NE_NAMEINFO;
  146. typedef struct
  147. {
  148. WORD type_id;
  149. WORD count;
  150. DWORD resloader;
  151. } NE_TYPEINFO;
  152. #define NE_RSCTYPE_ICON 0x8003
  153. #define NE_RSCTYPE_GROUP_ICON 0x800e
  154. #include "poppack.h"
  155. typedef struct
  156. {
  157. HRSRC *pResInfo;
  158. int nIndex;
  159. } ENUMRESSTRUCT;
  160. struct xdg_mime_type
  161. {
  162. char *mimeType;
  163. char *glob;
  164. char *lower_glob;
  165. struct list entry;
  166. };
  167. struct rb_string_entry
  168. {
  169. char *string;
  170. struct wine_rb_entry entry;
  171. };
  172. 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;
  176. /* Utility routines */
  177. static unsigned short crc16(const char* string)
  178. {
  179. unsigned short crc = 0;
  180. int i, j, xor_poly;
  181. for (i = 0; string[i] != 0; i++)
  182. {
  183. char c = string[i];
  184. for (j = 0; j < 8; c >>= 1, j++)
  185. {
  186. xor_poly = (c ^ crc) & 1;
  187. crc >>= 1;
  188. if (xor_poly)
  189. crc ^= 0xa001;
  190. }
  191. }
  192. return crc;
  193. }
  194. static char *strdupA( const char *str )
  195. {
  196. char *ret;
  197. if (!str) return NULL;
  198. if ((ret = HeapAlloc( GetProcessHeap(), 0, strlen(str) + 1 ))) strcpy( ret, str );
  199. return ret;
  200. }
  201. static char* heap_printf(const char *format, ...)
  202. {
  203. va_list args;
  204. int size = 4096;
  205. char *buffer, *ret;
  206. int n;
  207. while (1)
  208. {
  209. buffer = HeapAlloc(GetProcessHeap(), 0, size);
  210. if (buffer == NULL)
  211. break;
  212. va_start(args, format);
  213. n = vsnprintf(buffer, size, format, args);
  214. va_end(args);
  215. if (n == -1)
  216. size *= 2;
  217. else if (n >= size)
  218. size = n + 1;
  219. else
  220. break;
  221. HeapFree(GetProcessHeap(), 0, buffer);
  222. }
  223. if (!buffer) return NULL;
  224. ret = HeapReAlloc(GetProcessHeap(), 0, buffer, strlen(buffer) + 1 );
  225. if (!ret) ret = buffer;
  226. return ret;
  227. }
  228. static int winemenubuilder_rb_string_compare(const void *key, const struct wine_rb_entry *entry)
  229. {
  230. const struct rb_string_entry *t = WINE_RB_ENTRY_VALUE(entry, const struct rb_string_entry, entry);
  231. return strcmp((char*)key, t->string);
  232. }
  233. static void winemenubuilder_rb_destroy(struct wine_rb_entry *entry, void *context)
  234. {
  235. struct rb_string_entry *t = WINE_RB_ENTRY_VALUE(entry, struct rb_string_entry, entry);
  236. HeapFree(GetProcessHeap(), 0, t->string);
  237. HeapFree(GetProcessHeap(), 0, t);
  238. }
  239. static void write_xml_text(FILE *file, const char *text)
  240. {
  241. int i;
  242. for (i = 0; text[i]; i++)
  243. {
  244. if (text[i] == '&')
  245. fputs("&amp;", file);
  246. else if (text[i] == '<')
  247. fputs("&lt;", file);
  248. else if (text[i] == '>')
  249. fputs("&gt;", file);
  250. else if (text[i] == '\'')
  251. fputs("&apos;", file);
  252. else if (text[i] == '"')
  253. fputs("&quot;", file);
  254. else
  255. fputc(text[i], file);
  256. }
  257. }
  258. static BOOL create_directories(char *directory)
  259. {
  260. BOOL ret = TRUE;
  261. int i;
  262. for (i = 0; directory[i]; i++)
  263. {
  264. if (i > 0 && directory[i] == '/')
  265. {
  266. directory[i] = 0;
  267. mkdir(directory, 0777);
  268. directory[i] = '/';
  269. }
  270. }
  271. if (mkdir(directory, 0777) && errno != EEXIST)
  272. ret = FALSE;
  273. return ret;
  274. }
  275. static char* wchars_to_utf8_chars(LPCWSTR string)
  276. {
  277. char *ret;
  278. INT size = WideCharToMultiByte(CP_UTF8, 0, string, -1, NULL, 0, NULL, NULL);
  279. ret = HeapAlloc(GetProcessHeap(), 0, size);
  280. if (ret)
  281. WideCharToMultiByte(CP_UTF8, 0, string, -1, ret, size, NULL, NULL);
  282. return ret;
  283. }
  284. static char* wchars_to_unix_chars(LPCWSTR string)
  285. {
  286. char *ret;
  287. INT size = WideCharToMultiByte(CP_UNIXCP, 0, string, -1, NULL, 0, NULL, NULL);
  288. ret = HeapAlloc(GetProcessHeap(), 0, size);
  289. if (ret)
  290. WideCharToMultiByte(CP_UNIXCP, 0, string, -1, ret, size, NULL, NULL);
  291. return ret;
  292. }
  293. static WCHAR* utf8_chars_to_wchars(LPCSTR string)
  294. {
  295. WCHAR *ret;
  296. INT size = MultiByteToWideChar(CP_UTF8, 0, string, -1, NULL, 0);
  297. ret = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
  298. if (ret)
  299. MultiByteToWideChar(CP_UTF8, 0, string, -1, ret, size);
  300. return ret;
  301. }
  302. /* Icon extraction routines
  303. *
  304. * FIXME: should use PrivateExtractIcons and friends
  305. * FIXME: should not use stdio
  306. */
  307. static HRESULT convert_to_native_icon(IStream *icoFile, int *indices, int numIndices,
  308. const CLSID *outputFormat, const char *outputFileName, LPCWSTR commentW)
  309. {
  310. WCHAR *dosOutputFileName = NULL;
  311. IWICImagingFactory *factory = NULL;
  312. IWICBitmapDecoder *decoder = NULL;
  313. IWICBitmapEncoder *encoder = NULL;
  314. IWICBitmapScaler *scaler = NULL;
  315. IStream *outputFile = NULL;
  316. int i;
  317. HRESULT hr = E_FAIL;
  318. dosOutputFileName = wine_get_dos_file_name(outputFileName);
  319. if (dosOutputFileName == NULL)
  320. {
  321. WINE_ERR("error converting %s to DOS file name\n", outputFileName);
  322. goto end;
  323. }
  324. hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
  325. &IID_IWICImagingFactory, (void**)&factory);
  326. if (FAILED(hr))
  327. {
  328. WINE_ERR("error 0x%08X creating IWICImagingFactory\n", hr);
  329. goto end;
  330. }
  331. hr = IWICImagingFactory_CreateDecoderFromStream(factory, icoFile, NULL,
  332. WICDecodeMetadataCacheOnDemand, &decoder);
  333. if (FAILED(hr))
  334. {
  335. WINE_ERR("error 0x%08X creating IWICBitmapDecoder\n", hr);
  336. goto end;
  337. }
  338. if (IsEqualCLSID(outputFormat,&CLSID_WICIcnsEncoder))
  339. {
  340. hr = IWICImagingFactory_CreateBitmapScaler(factory, &scaler);
  341. if (FAILED(hr))
  342. {
  343. WINE_WARN("error 0x%08X creating IWICBitmapScaler\n", hr);
  344. }
  345. }
  346. hr = CoCreateInstance(outputFormat, NULL, CLSCTX_INPROC_SERVER,
  347. &IID_IWICBitmapEncoder, (void**)&encoder);
  348. if (FAILED(hr))
  349. {
  350. WINE_ERR("error 0x%08X creating bitmap encoder\n", hr);
  351. goto end;
  352. }
  353. hr = SHCreateStreamOnFileW(dosOutputFileName, STGM_CREATE | STGM_WRITE, &outputFile);
  354. if (FAILED(hr))
  355. {
  356. WINE_ERR("error 0x%08X creating output file %s\n", hr, wine_dbgstr_w(dosOutputFileName));
  357. goto end;
  358. }
  359. hr = IWICBitmapEncoder_Initialize(encoder, outputFile, WICBitmapEncoderNoCache);
  360. if (FAILED(hr))
  361. {
  362. WINE_ERR("error 0x%08X initializing encoder\n", hr);
  363. goto end;
  364. }
  365. for (i = 0; i < numIndices; i++)
  366. {
  367. IWICBitmapFrameDecode *sourceFrame = NULL;
  368. IWICBitmapSource *sourceBitmap = NULL;
  369. IWICBitmapFrameEncode *dstFrame = NULL;
  370. IPropertyBag2 *options = NULL;
  371. UINT width, height;
  372. hr = IWICBitmapDecoder_GetFrame(decoder, indices[i], &sourceFrame);
  373. if (FAILED(hr))
  374. {
  375. WINE_ERR("error 0x%08X getting frame %d\n", hr, indices[i]);
  376. goto endloop;
  377. }
  378. hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)sourceFrame, &sourceBitmap);
  379. if (FAILED(hr))
  380. {
  381. WINE_ERR("error 0x%08X converting bitmap to 32bppBGRA\n", hr);
  382. goto endloop;
  383. }
  384. if ( scaler)
  385. {
  386. IWICBitmapSource_GetSize(sourceBitmap, &width, &height);
  387. if (width == 64) /* Classic Mode */
  388. {
  389. hr = IWICBitmapScaler_Initialize( scaler, sourceBitmap, 128, 128,
  390. WICBitmapInterpolationModeNearestNeighbor);
  391. if (FAILED(hr))
  392. WINE_ERR("error 0x%08X scaling bitmap\n", hr);
  393. else
  394. {
  395. IWICBitmapSource_Release(sourceBitmap);
  396. IWICBitmapScaler_QueryInterface(scaler, &IID_IWICBitmapSource, (LPVOID)&sourceBitmap);
  397. }
  398. }
  399. }
  400. hr = IWICBitmapEncoder_CreateNewFrame(encoder, &dstFrame, &options);
  401. if (FAILED(hr))
  402. {
  403. WINE_ERR("error 0x%08X creating encoder frame\n", hr);
  404. goto endloop;
  405. }
  406. hr = IWICBitmapFrameEncode_Initialize(dstFrame, options);
  407. if (FAILED(hr))
  408. {
  409. WINE_ERR("error 0x%08X initializing encoder frame\n", hr);
  410. goto endloop;
  411. }
  412. hr = IWICBitmapSource_GetSize(sourceBitmap, &width, &height);
  413. if (FAILED(hr))
  414. {
  415. WINE_ERR("error 0x%08X getting source bitmap size\n", hr);
  416. goto endloop;
  417. }
  418. hr = IWICBitmapFrameEncode_SetSize(dstFrame, width, height);
  419. if (FAILED(hr))
  420. {
  421. WINE_ERR("error 0x%08X setting destination bitmap size\n", hr);
  422. goto endloop;
  423. }
  424. hr = IWICBitmapFrameEncode_SetResolution(dstFrame, 96, 96);
  425. if (FAILED(hr))
  426. {
  427. WINE_ERR("error 0x%08X setting destination bitmap resolution\n", hr);
  428. goto endloop;
  429. }
  430. hr = IWICBitmapFrameEncode_WriteSource(dstFrame, sourceBitmap, NULL);
  431. if (FAILED(hr))
  432. {
  433. WINE_ERR("error 0x%08X copying bitmaps\n", hr);
  434. goto endloop;
  435. }
  436. hr = IWICBitmapFrameEncode_Commit(dstFrame);
  437. if (FAILED(hr))
  438. {
  439. WINE_ERR("error 0x%08X committing frame\n", hr);
  440. goto endloop;
  441. }
  442. endloop:
  443. if (sourceFrame)
  444. IWICBitmapFrameDecode_Release(sourceFrame);
  445. if (sourceBitmap)
  446. IWICBitmapSource_Release(sourceBitmap);
  447. if (dstFrame)
  448. IWICBitmapFrameEncode_Release(dstFrame);
  449. if (options)
  450. IPropertyBag2_Release(options);
  451. }
  452. hr = IWICBitmapEncoder_Commit(encoder);
  453. if (FAILED(hr))
  454. {
  455. WINE_ERR("error 0x%08X committing encoder\n", hr);
  456. goto end;
  457. }
  458. end:
  459. HeapFree(GetProcessHeap(), 0, dosOutputFileName);
  460. if (factory)
  461. IWICImagingFactory_Release(factory);
  462. if (decoder)
  463. IWICBitmapDecoder_Release(decoder);
  464. if (scaler)
  465. IWICBitmapScaler_Release(scaler);
  466. if (encoder)
  467. IWICBitmapEncoder_Release(encoder);
  468. if (outputFile)
  469. IStream_Release(outputFile);
  470. return hr;
  471. }
  472. struct IconData16 {
  473. BYTE *fileBytes;
  474. DWORD fileSize;
  475. NE_TYPEINFO *iconResources;
  476. WORD alignmentShiftCount;
  477. };
  478. static int populate_module16_icons(struct IconData16 *iconData16, GRPICONDIR *grpIconDir, ICONDIRENTRY *iconDirEntries, BYTE *icons, SIZE_T *iconOffset)
  479. {
  480. int i, j;
  481. int validEntries = 0;
  482. for (i = 0; i < grpIconDir->idCount; i++)
  483. {
  484. BYTE *iconPtr = (BYTE*)iconData16->iconResources;
  485. NE_NAMEINFO *matchingIcon = NULL;
  486. iconPtr += sizeof(NE_TYPEINFO);
  487. for (j = 0; j < iconData16->iconResources->count; j++)
  488. {
  489. NE_NAMEINFO *iconInfo = (NE_NAMEINFO*)iconPtr;
  490. if ((iconPtr + sizeof(NE_NAMEINFO)) > (iconData16->fileBytes + iconData16->fileSize))
  491. {
  492. WINE_WARN("file too small for icon NE_NAMEINFO\n");
  493. break;
  494. }
  495. if (iconInfo->id == (0x8000 | grpIconDir->idEntries[i].nID))
  496. {
  497. matchingIcon = iconInfo;
  498. break;
  499. }
  500. iconPtr += sizeof(NE_NAMEINFO);
  501. }
  502. if (matchingIcon == NULL)
  503. continue;
  504. if (((matchingIcon->offset << iconData16->alignmentShiftCount) + grpIconDir->idEntries[i].dwBytesInRes) > iconData16->fileSize)
  505. {
  506. WINE_WARN("file too small for icon contents\n");
  507. break;
  508. }
  509. iconDirEntries[validEntries].bWidth = grpIconDir->idEntries[i].bWidth;
  510. iconDirEntries[validEntries].bHeight = grpIconDir->idEntries[i].bHeight;
  511. iconDirEntries[validEntries].bColorCount = grpIconDir->idEntries[i].bColorCount;
  512. iconDirEntries[validEntries].bReserved = grpIconDir->idEntries[i].bReserved;
  513. iconDirEntries[validEntries].wPlanes = grpIconDir->idEntries[i].wPlanes;
  514. iconDirEntries[validEntries].wBitCount = grpIconDir->idEntries[i].wBitCount;
  515. iconDirEntries[validEntries].dwBytesInRes = grpIconDir->idEntries[i].dwBytesInRes;
  516. iconDirEntries[validEntries].dwImageOffset = *iconOffset;
  517. validEntries++;
  518. memcpy(&icons[*iconOffset], &iconData16->fileBytes[matchingIcon->offset << iconData16->alignmentShiftCount], grpIconDir->idEntries[i].dwBytesInRes);
  519. *iconOffset += grpIconDir->idEntries[i].dwBytesInRes;
  520. }
  521. return validEntries;
  522. }
  523. static int populate_module_icons(HMODULE hModule, GRPICONDIR *grpIconDir, ICONDIRENTRY *iconDirEntries, BYTE *icons, SIZE_T *iconOffset)
  524. {
  525. int i;
  526. int validEntries = 0;
  527. for (i = 0; i < grpIconDir->idCount; i++)
  528. {
  529. HRSRC hResInfo;
  530. LPCWSTR lpName = MAKEINTRESOURCEW(grpIconDir->idEntries[i].nID);
  531. if ((hResInfo = FindResourceW(hModule, lpName, (LPCWSTR)RT_ICON)))
  532. {
  533. HGLOBAL hResData;
  534. if ((hResData = LoadResource(hModule, hResInfo)))
  535. {
  536. BITMAPINFO *pIcon;
  537. DWORD size = min( grpIconDir->idEntries[i].dwBytesInRes, ((IMAGE_RESOURCE_DATA_ENTRY *)hResInfo)->Size );
  538. if ((pIcon = LockResource(hResData)))
  539. {
  540. iconDirEntries[validEntries].bWidth = grpIconDir->idEntries[i].bWidth;
  541. iconDirEntries[validEntries].bHeight = grpIconDir->idEntries[i].bHeight;
  542. iconDirEntries[validEntries].bColorCount = grpIconDir->idEntries[i].bColorCount;
  543. iconDirEntries[validEntries].bReserved = grpIconDir->idEntries[i].bReserved;
  544. iconDirEntries[validEntries].wPlanes = grpIconDir->idEntries[i].wPlanes;
  545. iconDirEntries[validEntries].wBitCount = grpIconDir->idEntries[i].wBitCount;
  546. iconDirEntries[validEntries].dwBytesInRes = size;
  547. iconDirEntries[validEntries].dwImageOffset = *iconOffset;
  548. validEntries++;
  549. memcpy(&icons[*iconOffset], pIcon, size);
  550. *iconOffset += size;
  551. }
  552. FreeResource(hResData);
  553. }
  554. }
  555. }
  556. return validEntries;
  557. }
  558. static IStream *add_module_icons_to_stream(struct IconData16 *iconData16, HMODULE hModule, GRPICONDIR *grpIconDir)
  559. {
  560. int i;
  561. SIZE_T iconsSize = 0;
  562. BYTE *icons = NULL;
  563. ICONDIRENTRY *iconDirEntries = NULL;
  564. IStream *stream = NULL;
  565. HRESULT hr = E_FAIL;
  566. ULONG bytesWritten;
  567. ICONDIR iconDir;
  568. SIZE_T iconOffset;
  569. int validEntries = 0;
  570. LARGE_INTEGER zero;
  571. for (i = 0; i < grpIconDir->idCount; i++)
  572. iconsSize += grpIconDir->idEntries[i].dwBytesInRes;
  573. icons = HeapAlloc(GetProcessHeap(), 0, iconsSize);
  574. if (icons == NULL)
  575. {
  576. WINE_ERR("out of memory allocating icon\n");
  577. goto end;
  578. }
  579. iconDirEntries = HeapAlloc(GetProcessHeap(), 0, grpIconDir->idCount*sizeof(ICONDIRENTRY));
  580. if (iconDirEntries == NULL)
  581. {
  582. WINE_ERR("out of memory allocating icon dir entries\n");
  583. goto end;
  584. }
  585. hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
  586. if (FAILED(hr))
  587. {
  588. WINE_ERR("error creating icon stream\n");
  589. goto end;
  590. }
  591. iconOffset = 0;
  592. if (iconData16)
  593. validEntries = populate_module16_icons(iconData16, grpIconDir, iconDirEntries, icons, &iconOffset);
  594. else if (hModule)
  595. validEntries = populate_module_icons(hModule, grpIconDir, iconDirEntries, icons, &iconOffset);
  596. if (validEntries == 0)
  597. {
  598. WINE_ERR("no valid icon entries\n");
  599. goto end;
  600. }
  601. iconDir.idReserved = 0;
  602. iconDir.idType = 1;
  603. iconDir.idCount = validEntries;
  604. hr = IStream_Write(stream, &iconDir, sizeof(iconDir), &bytesWritten);
  605. if (FAILED(hr) || bytesWritten != sizeof(iconDir))
  606. {
  607. WINE_ERR("error 0x%08X writing icon stream\n", hr);
  608. goto end;
  609. }
  610. for (i = 0; i < validEntries; i++)
  611. iconDirEntries[i].dwImageOffset += sizeof(ICONDIR) + validEntries*sizeof(ICONDIRENTRY);
  612. hr = IStream_Write(stream, iconDirEntries, validEntries*sizeof(ICONDIRENTRY), &bytesWritten);
  613. if (FAILED(hr) || bytesWritten != validEntries*sizeof(ICONDIRENTRY))
  614. {
  615. WINE_ERR("error 0x%08X writing icon dir entries to stream\n", hr);
  616. goto end;
  617. }
  618. hr = IStream_Write(stream, icons, iconOffset, &bytesWritten);
  619. if (FAILED(hr) || bytesWritten != iconOffset)
  620. {
  621. WINE_ERR("error 0x%08X writing icon images to stream\n", hr);
  622. goto end;
  623. }
  624. zero.QuadPart = 0;
  625. hr = IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
  626. end:
  627. HeapFree(GetProcessHeap(), 0, icons);
  628. HeapFree(GetProcessHeap(), 0, iconDirEntries);
  629. if (FAILED(hr) && stream != NULL)
  630. {
  631. IStream_Release(stream);
  632. stream = NULL;
  633. }
  634. return stream;
  635. }
  636. static HRESULT open_module16_icon(LPCWSTR szFileName, int nIndex, IStream **ppStream)
  637. {
  638. HANDLE hFile = INVALID_HANDLE_VALUE;
  639. HANDLE hFileMapping = NULL;
  640. DWORD fileSize;
  641. BYTE *fileBytes = NULL;
  642. IMAGE_DOS_HEADER *dosHeader;
  643. IMAGE_OS2_HEADER *neHeader;
  644. BYTE *rsrcTab;
  645. NE_TYPEINFO *iconGroupResources;
  646. NE_TYPEINFO *iconResources;
  647. NE_NAMEINFO *iconDirPtr;
  648. GRPICONDIR *iconDir;
  649. WORD alignmentShiftCount;
  650. struct IconData16 iconData16;
  651. HRESULT hr = E_FAIL;
  652. hFile = CreateFileW(szFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
  653. OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, NULL);
  654. if (hFile == INVALID_HANDLE_VALUE)
  655. {
  656. WINE_WARN("opening %s failed with error %d\n", wine_dbgstr_w(szFileName), GetLastError());
  657. goto end;
  658. }
  659. hFileMapping = CreateFileMappingW(hFile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
  660. if (hFileMapping == NULL)
  661. {
  662. WINE_WARN("CreateFileMapping failed, error %d\n", GetLastError());
  663. goto end;
  664. }
  665. fileSize = GetFileSize(hFile, NULL);
  666. fileBytes = MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);
  667. if (fileBytes == NULL)
  668. {
  669. WINE_WARN("MapViewOfFile failed, error %d\n", GetLastError());
  670. goto end;
  671. }
  672. dosHeader = (IMAGE_DOS_HEADER*)fileBytes;
  673. if (sizeof(IMAGE_DOS_HEADER) >= fileSize || dosHeader->e_magic != IMAGE_DOS_SIGNATURE)
  674. {
  675. WINE_WARN("file too small for MZ header\n");
  676. goto end;
  677. }
  678. neHeader = (IMAGE_OS2_HEADER*)(fileBytes + dosHeader->e_lfanew);
  679. if ((((BYTE*)neHeader) + sizeof(IMAGE_OS2_HEADER)) > (fileBytes + fileSize) ||
  680. neHeader->ne_magic != IMAGE_OS2_SIGNATURE)
  681. {
  682. WINE_WARN("file too small for NE header\n");
  683. goto end;
  684. }
  685. rsrcTab = ((BYTE*)neHeader) + neHeader->ne_rsrctab;
  686. if ((rsrcTab + 2) > (fileBytes + fileSize))
  687. {
  688. WINE_WARN("file too small for resource table\n");
  689. goto end;
  690. }
  691. alignmentShiftCount = *(WORD*)rsrcTab;
  692. rsrcTab += 2;
  693. iconGroupResources = NULL;
  694. iconResources = NULL;
  695. for (;;)
  696. {
  697. NE_TYPEINFO *neTypeInfo = (NE_TYPEINFO*)rsrcTab;
  698. if ((rsrcTab + sizeof(NE_TYPEINFO)) > (fileBytes + fileSize))
  699. {
  700. WINE_WARN("file too small for resource table\n");
  701. goto end;
  702. }
  703. if (neTypeInfo->type_id == 0)
  704. break;
  705. else if (neTypeInfo->type_id == NE_RSCTYPE_GROUP_ICON)
  706. iconGroupResources = neTypeInfo;
  707. else if (neTypeInfo->type_id == NE_RSCTYPE_ICON)
  708. iconResources = neTypeInfo;
  709. rsrcTab += sizeof(NE_TYPEINFO) + neTypeInfo->count*sizeof(NE_NAMEINFO);
  710. }
  711. if (iconGroupResources == NULL)
  712. {
  713. WINE_WARN("no group icon resource type found\n");
  714. goto end;
  715. }
  716. if (iconResources == NULL)
  717. {
  718. WINE_WARN("no icon resource type found\n");
  719. goto end;
  720. }
  721. if (nIndex >= iconGroupResources->count)
  722. {
  723. WINE_WARN("icon index out of range\n");
  724. goto end;
  725. }
  726. iconDirPtr = (NE_NAMEINFO*)(((BYTE*)iconGroupResources) + sizeof(NE_TYPEINFO) + nIndex*sizeof(NE_NAMEINFO));
  727. if ((((BYTE*)iconDirPtr) + sizeof(NE_NAMEINFO)) > (fileBytes + fileSize))
  728. {
  729. WINE_WARN("file too small for icon group NE_NAMEINFO\n");
  730. goto end;
  731. }
  732. iconDir = (GRPICONDIR*)(fileBytes + (iconDirPtr->offset << alignmentShiftCount));
  733. if ((((BYTE*)iconDir) + sizeof(GRPICONDIR) + iconDir->idCount*sizeof(GRPICONDIRENTRY)) > (fileBytes + fileSize))
  734. {
  735. WINE_WARN("file too small for GRPICONDIR\n");
  736. goto end;
  737. }
  738. iconData16.fileBytes = fileBytes;
  739. iconData16.fileSize = fileSize;
  740. iconData16.iconResources = iconResources;
  741. iconData16.alignmentShiftCount = alignmentShiftCount;
  742. *ppStream = add_module_icons_to_stream(&iconData16, NULL, iconDir);
  743. if (*ppStream)
  744. hr = S_OK;
  745. end:
  746. if (hFile != INVALID_HANDLE_VALUE)
  747. CloseHandle(hFile);
  748. if (hFileMapping != NULL)
  749. CloseHandle(hFileMapping);
  750. if (fileBytes != NULL)
  751. UnmapViewOfFile(fileBytes);
  752. return hr;
  753. }
  754. static BOOL CALLBACK EnumResNameProc(HMODULE hModule, LPCWSTR lpszType, LPWSTR lpszName, LONG_PTR lParam)
  755. {
  756. ENUMRESSTRUCT *sEnumRes = (ENUMRESSTRUCT *) lParam;
  757. if (!sEnumRes->nIndex--)
  758. {
  759. *sEnumRes->pResInfo = FindResourceW(hModule, lpszName, (LPCWSTR)RT_GROUP_ICON);
  760. return FALSE;
  761. }
  762. else
  763. return TRUE;
  764. }
  765. static HRESULT open_module_icon(LPCWSTR szFileName, int nIndex, IStream **ppStream)
  766. {
  767. HMODULE hModule;
  768. HRSRC hResInfo;
  769. HGLOBAL hResData;
  770. GRPICONDIR *pIconDir;
  771. ENUMRESSTRUCT sEnumRes;
  772. HRESULT hr = E_FAIL;
  773. hModule = LoadLibraryExW(szFileName, 0, LOAD_LIBRARY_AS_DATAFILE);
  774. if (!hModule)
  775. {
  776. if (GetLastError() == ERROR_BAD_EXE_FORMAT)
  777. return open_module16_icon(szFileName, nIndex, ppStream);
  778. else
  779. {
  780. WINE_WARN("LoadLibraryExW (%s) failed, error %d\n",
  781. wine_dbgstr_w(szFileName), GetLastError());
  782. return HRESULT_FROM_WIN32(GetLastError());
  783. }
  784. }
  785. if (nIndex < 0)
  786. {
  787. hResInfo = FindResourceW(hModule, MAKEINTRESOURCEW(-nIndex), (LPCWSTR)RT_GROUP_ICON);
  788. WINE_TRACE("FindResourceW (%s) called, return %p, error %d\n",
  789. wine_dbgstr_w(szFileName), hResInfo, GetLastError());
  790. }
  791. else
  792. {
  793. hResInfo=NULL;
  794. sEnumRes.pResInfo = &hResInfo;
  795. sEnumRes.nIndex = nIndex;
  796. if (!EnumResourceNamesW(hModule, (LPCWSTR)RT_GROUP_ICON,
  797. EnumResNameProc, (LONG_PTR)&sEnumRes) &&
  798. sEnumRes.nIndex != -1)
  799. {
  800. WINE_TRACE("EnumResourceNamesW failed, error %d\n", GetLastError());
  801. }
  802. }
  803. if (hResInfo)
  804. {
  805. if ((hResData = LoadResource(hModule, hResInfo)))
  806. {
  807. if ((pIconDir = LockResource(hResData)))
  808. {
  809. *ppStream = add_module_icons_to_stream(0, hModule, pIconDir);
  810. if (*ppStream)
  811. hr = S_OK;
  812. }
  813. FreeResource(hResData);
  814. }
  815. }
  816. else
  817. {
  818. WINE_WARN("found no icon\n");
  819. FreeLibrary(hModule);
  820. return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
  821. }
  822. FreeLibrary(hModule);
  823. return hr;
  824. }
  825. static HRESULT read_ico_direntries(IStream *icoStream, ICONDIRENTRY **ppIconDirEntries, int *numEntries)
  826. {
  827. ICONDIR iconDir;
  828. ULONG bytesRead;
  829. HRESULT hr;
  830. *ppIconDirEntries = NULL;
  831. hr = IStream_Read(icoStream, &iconDir, sizeof(ICONDIR), &bytesRead);
  832. if (FAILED(hr) || bytesRead != sizeof(ICONDIR) ||
  833. (iconDir.idReserved != 0) || (iconDir.idType != 1))
  834. {
  835. WINE_WARN("Invalid ico file format (hr=0x%08X, bytesRead=%d)\n", hr, bytesRead);
  836. hr = E_FAIL;
  837. goto end;
  838. }
  839. *numEntries = iconDir.idCount;
  840. if ((*ppIconDirEntries = HeapAlloc(GetProcessHeap(), 0, sizeof(ICONDIRENTRY)*iconDir.idCount)) == NULL)
  841. {
  842. hr = E_OUTOFMEMORY;
  843. goto end;
  844. }
  845. hr = IStream_Read(icoStream, *ppIconDirEntries, sizeof(ICONDIRENTRY)*iconDir.idCount, &bytesRead);
  846. if (FAILED(hr) || bytesRead != sizeof(ICONDIRENTRY)*iconDir.idCount)
  847. {
  848. if (SUCCEEDED(hr)) hr = E_FAIL;
  849. goto end;
  850. }
  851. end:
  852. if (FAILED(hr))
  853. HeapFree(GetProcessHeap(), 0, *ppIconDirEntries);
  854. return hr;
  855. }
  856. static HRESULT validate_ico(IStream **ppStream, ICONDIRENTRY **ppIconDirEntries, int *numEntries)
  857. {
  858. HRESULT hr;
  859. hr = read_ico_direntries(*ppStream, ppIconDirEntries, numEntries);
  860. if (SUCCEEDED(hr))
  861. {
  862. if (*numEntries)
  863. return hr;
  864. HeapFree(GetProcessHeap(), 0, *ppIconDirEntries);
  865. *ppIconDirEntries = NULL;
  866. }
  867. IStream_Release(*ppStream);
  868. *ppStream = NULL;
  869. return E_FAIL;
  870. }
  871. static HRESULT write_native_icon(IStream *iconStream, ICONDIRENTRY *pIconDirEntry,
  872. int numEntries, const char *icon_name, LPCWSTR szFileName)
  873. {
  874. int nMax = 0, nMaxBits = 0;
  875. int nIndex = 0;
  876. int i;
  877. LARGE_INTEGER position;
  878. HRESULT hr;
  879. for (i = 0; i < numEntries; i++)
  880. {
  881. WINE_TRACE("[%d]: %d x %d @ %d\n", i, pIconDirEntry[i].bWidth, pIconDirEntry[i].bHeight, pIconDirEntry[i].wBitCount);
  882. if (pIconDirEntry[i].wBitCount >= nMaxBits &&
  883. (pIconDirEntry[i].bHeight * pIconDirEntry[i].bWidth) >= nMax)
  884. {
  885. nIndex = i;
  886. nMax = pIconDirEntry[i].bHeight * pIconDirEntry[i].bWidth;
  887. nMaxBits = pIconDirEntry[i].wBitCount;
  888. }
  889. }
  890. WINE_TRACE("Selected: %d\n", nIndex);
  891. position.QuadPart = 0;
  892. hr = IStream_Seek(iconStream, position, STREAM_SEEK_SET, NULL);
  893. if (FAILED(hr)) return hr;
  894. return convert_to_native_icon(iconStream, &nIndex, 1, &CLSID_WICPngEncoder, icon_name, szFileName);
  895. }
  896. static WCHAR* assoc_query(ASSOCSTR assocStr, LPCWSTR name, LPCWSTR extra)
  897. {
  898. HRESULT hr;
  899. WCHAR *value = NULL;
  900. DWORD size = 0;
  901. hr = AssocQueryStringW(0, assocStr, name, extra, NULL, &size);
  902. if (SUCCEEDED(hr))
  903. {
  904. value = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
  905. if (value)
  906. {
  907. hr = AssocQueryStringW(0, assocStr, name, extra, value, &size);
  908. if (FAILED(hr))
  909. {
  910. HeapFree(GetProcessHeap(), 0, value);
  911. value = NULL;
  912. }
  913. }
  914. }
  915. return value;
  916. }
  917. static HRESULT open_file_type_icon(LPCWSTR szFileName, IStream **ppStream)
  918. {
  919. static const WCHAR openW[] = {'o','p','e','n',0};
  920. WCHAR *extension;
  921. WCHAR *icon = NULL;
  922. WCHAR *comma;
  923. WCHAR *executable = NULL;
  924. int index = 0;
  925. HRESULT hr = HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
  926. extension = strrchrW(szFileName, '.');
  927. if (extension == NULL)
  928. goto end;
  929. icon = assoc_query(ASSOCSTR_DEFAULTICON, extension, NULL);
  930. if (icon)
  931. {
  932. comma = strrchrW(icon, ',');
  933. if (comma)
  934. {
  935. *comma = 0;
  936. index = atoiW(comma + 1);
  937. }
  938. hr = open_module_icon(icon, index, ppStream);
  939. }
  940. else
  941. {
  942. executable = assoc_query(ASSOCSTR_EXECUTABLE, extension, openW);
  943. if (executable)
  944. hr = open_module_icon(executable, 0, ppStream);
  945. }
  946. end:
  947. HeapFree(GetProcessHeap(), 0, icon);
  948. HeapFree(GetProcessHeap(), 0, executable);
  949. return hr;
  950. }
  951. static HRESULT open_default_icon(IStream **ppStream)
  952. {
  953. static const WCHAR user32W[] = {'u','s','e','r','3','2',0};
  954. return open_module_icon(user32W, -(INT_PTR)IDI_WINLOGO, ppStream);
  955. }
  956. static HRESULT open_icon(LPCWSTR filename, int index, BOOL bWait, IStream **ppStream, ICONDIRENTRY **ppIconDirEntries, int *numEntries)
  957. {
  958. HRESULT hr;
  959. hr = open_module_icon(filename, index, ppStream);
  960. if (FAILED(hr))
  961. {
  962. if(bWait && hr == HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND))
  963. {
  964. WINE_WARN("Can't find file: %s, give a chance to parent process to create it\n",
  965. wine_dbgstr_w(filename));
  966. return hr;
  967. }
  968. else
  969. {
  970. /* This might be a raw .ico file */
  971. hr = SHCreateStreamOnFileW(filename, STGM_READ, ppStream);
  972. }
  973. }
  974. if (SUCCEEDED(hr))
  975. hr = validate_ico(ppStream, ppIconDirEntries, numEntries);
  976. if (FAILED(hr))
  977. {
  978. hr = open_file_type_icon(filename, ppStream);
  979. if (SUCCEEDED(hr))
  980. hr = validate_ico(ppStream, ppIconDirEntries, numEntries);
  981. }
  982. if (FAILED(hr) && !bWait)
  983. {
  984. hr = open_default_icon(ppStream);
  985. if (SUCCEEDED(hr))
  986. hr = validate_ico(ppStream, ppIconDirEntries, numEntries);
  987. }
  988. return hr;
  989. }
  990. static char* compute_native_identifier(int exeIndex, LPCWSTR icoPathW)
  991. {
  992. char* nativeIdentifier;
  993. char *icoPathA;
  994. unsigned short crc;
  995. char *basename, *ext;
  996. icoPathA = wchars_to_utf8_chars(icoPathW);
  997. if (icoPathA == NULL)
  998. return NULL;
  999. crc = crc16(icoPathA);
  1000. basename = strrchr(icoPathA, '\\');
  1001. if (basename == NULL)
  1002. basename = icoPathA;
  1003. else
  1004. {
  1005. *basename = 0;
  1006. basename++;
  1007. }
  1008. ext = strrchr(basename, '.');
  1009. if (ext)
  1010. *ext = 0;
  1011. nativeIdentifier = heap_printf("%04X_%s.%d", crc, basename, exeIndex);
  1012. HeapFree(GetProcessHeap(), 0, icoPathA);
  1013. return nativeIdentifier;
  1014. }
  1015. #ifdef __APPLE__
  1016. #define ICNS_SLOTS 6
  1017. static inline int size_to_slot(int size)
  1018. {
  1019. switch (size)
  1020. {
  1021. case 16: return 0;
  1022. case 32: return 1;
  1023. case 48: return 2;
  1024. case 64: return -2; /* Classic Mode */
  1025. case 128: return 3;
  1026. case 256: return 4;
  1027. case 512: return 5;
  1028. }
  1029. return -1;
  1030. }
  1031. #define CLASSIC_SLOT 3
  1032. static HRESULT platform_write_icon(IStream *icoStream, ICONDIRENTRY *iconDirEntries,
  1033. int numEntries, int exeIndex, LPCWSTR icoPathW,
  1034. const char *destFilename, char **nativeIdentifier)
  1035. {
  1036. struct {
  1037. int index;
  1038. int maxBits;
  1039. BOOL scaled;
  1040. } best[ICNS_SLOTS];
  1041. int indexes[ICNS_SLOTS];
  1042. int i;
  1043. const char* tmpdir;
  1044. char *icnsPath = NULL;
  1045. LARGE_INTEGER zero;
  1046. HRESULT hr;
  1047. for (i = 0; i < ICNS_SLOTS; i++)
  1048. {
  1049. best[i].index = -1;
  1050. best[i].maxBits = 0;
  1051. }
  1052. for (i = 0; i < numEntries; i++)
  1053. {
  1054. int slot;
  1055. int width = iconDirEntries[i].bWidth ? iconDirEntries[i].bWidth : 256;
  1056. int height = iconDirEntries[i].bHeight ? iconDirEntries[i].bHeight : 256;
  1057. BOOL scaled = FALSE;
  1058. WINE_TRACE("[%d]: %d x %d @ %d\n", i, width, height, iconDirEntries[i].wBitCount);
  1059. if (height != width)
  1060. continue;
  1061. slot = size_to_slot(width);
  1062. if (slot == -2)
  1063. {
  1064. scaled = TRUE;
  1065. slot = CLASSIC_SLOT;
  1066. }
  1067. else if (slot < 0)
  1068. continue;
  1069. if (scaled && best[slot].maxBits && !best[slot].scaled)
  1070. continue; /* don't replace unscaled with scaled */
  1071. if (iconDirEntries[i].wBitCount >= best[slot].maxBits || (!scaled && best[slot].scaled))
  1072. {
  1073. best[slot].index = i;
  1074. best[slot].maxBits = iconDirEntries[i].wBitCount;
  1075. best[slot].scaled = scaled;
  1076. }
  1077. }
  1078. /* remove the scaled icon if a larger unscaled icon exists */
  1079. if (best[CLASSIC_SLOT].scaled)
  1080. {
  1081. for (i = CLASSIC_SLOT+1; i < ICNS_SLOTS; i++)
  1082. if (best[i].index >= 0 && !best[i].scaled)
  1083. {
  1084. best[CLASSIC_SLOT].index = -1;
  1085. break;
  1086. }
  1087. }
  1088. numEntries = 0;
  1089. for (i = 0; i < ICNS_SLOTS; i++)
  1090. {
  1091. if (best[i].index >= 0)
  1092. {
  1093. indexes[numEntries] = best[i].index;
  1094. numEntries++;
  1095. }
  1096. }
  1097. if (destFilename)
  1098. *nativeIdentifier = heap_printf("%s", destFilename);
  1099. else
  1100. *nativeIdentifier = compute_native_identifier(exeIndex, icoPathW);
  1101. if (*nativeIdentifier == NULL)
  1102. {
  1103. hr = E_OUTOFMEMORY;
  1104. goto end;
  1105. }
  1106. if (!(tmpdir = getenv("TMPDIR"))) tmpdir = "/tmp";
  1107. icnsPath = heap_printf("%s/%s.icns", tmpdir, *nativeIdentifier);
  1108. if (icnsPath == NULL)
  1109. {
  1110. hr = E_OUTOFMEMORY;
  1111. WINE_WARN("out of memory creating ICNS path\n");
  1112. goto end;
  1113. }
  1114. zero.QuadPart = 0;
  1115. hr = IStream_Seek(icoStream, zero, STREAM_SEEK_SET, NULL);
  1116. if (FAILED(hr))
  1117. {
  1118. WINE_WARN("seeking icon stream failed, error 0x%08X\n", hr);
  1119. goto end;
  1120. }
  1121. hr = convert_to_native_icon(icoStream, indexes, numEntries, &CLSID_WICIcnsEncoder,
  1122. icnsPath, icoPathW);
  1123. if (FAILED(hr))
  1124. {
  1125. WINE_WARN("converting %s to %s failed, error 0x%08X\n",
  1126. wine_dbgstr_w(icoPathW), wine_dbgstr_a(icnsPath), hr);
  1127. goto end;
  1128. }
  1129. end:
  1130. HeapFree(GetProcessHeap(), 0, icnsPath);
  1131. return hr;
  1132. }
  1133. #else
  1134. static void refresh_icon_cache(const char *iconsDir)
  1135. {
  1136. /* The icon theme spec only requires the mtime on the "toplevel"
  1137. * directory (whatever that is) to be changed for a refresh,
  1138. * but on GNOME you have to create a file in that directory
  1139. * instead. Creating a file also works on KDE, Xfce and LXDE.
  1140. */
  1141. char *filename = heap_printf("%s/.wine-refresh-XXXXXX", iconsDir);
  1142. if (filename != NULL)
  1143. {
  1144. int fd = mkstemps(filename, 0);
  1145. if (fd >= 0)
  1146. {
  1147. close(fd);
  1148. unlink(filename);
  1149. }
  1150. HeapFree(GetProcessHeap(), 0, filename);
  1151. }
  1152. }
  1153. static HRESULT platform_write_icon(IStream *icoStream, ICONDIRENTRY *iconDirEntries,
  1154. int numEntries, int exeIndex, LPCWSTR icoPathW,
  1155. const char *destFilename, char **nativeIdentifier)
  1156. {
  1157. int i;
  1158. char *iconsDir = NULL;
  1159. HRESULT hr = S_OK;
  1160. LARGE_INTEGER zero;
  1161. if (destFilename)
  1162. *nativeIdentifier = heap_printf("%s", destFilename);
  1163. else
  1164. *nativeIdentifier = compute_native_identifier(exeIndex, icoPathW);
  1165. if (*nativeIdentifier == NULL)
  1166. {
  1167. hr = E_OUTOFMEMORY;
  1168. goto end;
  1169. }
  1170. iconsDir = heap_printf("%s/icons/hicolor", xdg_data_dir);
  1171. if (iconsDir == NULL)
  1172. {
  1173. hr = E_OUTOFMEMORY;
  1174. goto end;
  1175. }
  1176. for (i = 0; i < numEntries; i++)
  1177. {
  1178. int bestIndex = i;
  1179. int j;
  1180. BOOLEAN duplicate = FALSE;
  1181. int w, h;
  1182. char *iconDir = NULL;
  1183. char *pngPath = NULL;
  1184. WINE_TRACE("[%d]: %d x %d @ %d\n", i, iconDirEntries[i].bWidth,
  1185. iconDirEntries[i].bHeight, iconDirEntries[i].wBitCount);
  1186. for (j = 0; j < i; j++)
  1187. {
  1188. if (iconDirEntries[j].bWidth == iconDirEntries[i].bWidth &&
  1189. iconDirEntries[j].bHeight == iconDirEntries[i].bHeight)
  1190. {
  1191. duplicate = TRUE;
  1192. break;
  1193. }
  1194. }
  1195. if (duplicate)
  1196. continue;
  1197. for (j = i + 1; j < numEntries; j++)
  1198. {
  1199. if (iconDirEntries[j].bWidth == iconDirEntries[i].bWidth &&
  1200. iconDirEntries[j].bHeight == iconDirEntries[i].bHeight &&
  1201. iconDirEntries[j].wBitCount >= iconDirEntries[bestIndex].wBitCount)
  1202. {
  1203. bestIndex = j;
  1204. }
  1205. }
  1206. WINE_TRACE("Selected: %d\n", bestIndex);
  1207. w = iconDirEntries[bestIndex].bWidth ? iconDirEntries[bestIndex].bWidth : 256;
  1208. h = iconDirEntries[bestIndex].bHeight ? iconDirEntries[bestIndex].bHeight : 256;
  1209. iconDir = heap_printf("%s/%dx%d/apps", iconsDir, w, h);
  1210. if (iconDir == NULL)
  1211. {
  1212. hr = E_OUTOFMEMORY;
  1213. goto endloop;
  1214. }
  1215. create_directories(iconDir);
  1216. pngPath = heap_printf("%s/%s.png", iconDir, *nativeIdentifier);
  1217. if (pngPath == NULL)
  1218. {
  1219. hr = E_OUTOFMEMORY;
  1220. goto endloop;
  1221. }
  1222. zero.QuadPart = 0;
  1223. hr = IStream_Seek(icoStream, zero, STREAM_SEEK_SET, NULL);
  1224. if (FAILED(hr))
  1225. goto endloop;
  1226. hr = convert_to_native_icon(icoStream, &bestIndex, 1, &CLSID_WICPngEncoder,
  1227. pngPath, icoPathW);
  1228. endloop:
  1229. HeapFree(GetProcessHeap(), 0, iconDir);
  1230. HeapFree(GetProcessHeap(), 0, pngPath);
  1231. }
  1232. refresh_icon_cache(iconsDir);
  1233. end:
  1234. HeapFree(GetProcessHeap(), 0, iconsDir);
  1235. return hr;
  1236. }
  1237. #endif /* defined(__APPLE__) */
  1238. /* extract an icon from an exe or icon file; helper for IPersistFile_fnSave */
  1239. static char *extract_icon(LPCWSTR icoPathW, int index, const char *destFilename, BOOL bWait)
  1240. {
  1241. IStream *stream = NULL;
  1242. ICONDIRENTRY *pIconDirEntries = NULL;
  1243. int numEntries;
  1244. HRESULT hr;
  1245. char *nativeIdentifier = NULL;
  1246. WINE_TRACE("path=[%s] index=%d destFilename=[%s]\n", wine_dbgstr_w(icoPathW), index, wine_dbgstr_a(destFilename));
  1247. hr = open_icon(icoPathW, index, bWait, &stream, &pIconDirEntries, &numEntries);
  1248. if (FAILED(hr))
  1249. {
  1250. WINE_WARN("opening icon %s index %d failed, hr=0x%08X\n", wine_dbgstr_w(icoPathW), index, hr);
  1251. goto end;
  1252. }
  1253. hr = platform_write_icon(stream, pIconDirEntries, numEntries, index, icoPathW, destFilename, &nativeIdentifier);
  1254. if (FAILED(hr))
  1255. WINE_WARN("writing icon failed, error 0x%08X\n", hr);
  1256. end:
  1257. if (stream)
  1258. IStream_Release(stream);
  1259. HeapFree(GetProcessHeap(), 0, pIconDirEntries);
  1260. if (FAILED(hr))
  1261. {
  1262. HeapFree(GetProcessHeap(), 0, nativeIdentifier);
  1263. nativeIdentifier = NULL;
  1264. }
  1265. return nativeIdentifier;
  1266. }
  1267. static HKEY open_menus_reg_key(void)
  1268. {
  1269. static const WCHAR Software_Wine_FileOpenMenuFilesW[] = {
  1270. 'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\','M','e','n','u','F','i','l','e','s',0};
  1271. HKEY assocKey;
  1272. DWORD ret;
  1273. ret = RegCreateKeyW(HKEY_CURRENT_USER, Software_Wine_FileOpenMenuFilesW, &assocKey);
  1274. if (ret == ERROR_SUCCESS)
  1275. return assocKey;
  1276. SetLastError(ret);
  1277. return NULL;
  1278. }
  1279. static DWORD register_menus_entry(const char *unix_file, const char *windows_file)
  1280. {
  1281. WCHAR *unix_fileW;
  1282. WCHAR *windows_fileW;
  1283. INT size;
  1284. DWORD ret;
  1285. size = MultiByteToWideChar(CP_UNIXCP, 0, unix_file, -1, NULL, 0);
  1286. unix_fileW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
  1287. if (unix_fileW)
  1288. {
  1289. MultiByteToWideChar(CP_UNIXCP, 0, unix_file, -1, unix_fileW, size);
  1290. size = MultiByteToWideChar(CP_UNIXCP, 0, windows_file, -1, NULL, 0);
  1291. windows_fileW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
  1292. if (windows_fileW)
  1293. {
  1294. HKEY hkey;
  1295. MultiByteToWideChar(CP_UNIXCP, 0, windows_file, -1, windows_fileW, size);
  1296. hkey = open_menus_reg_key();
  1297. if (hkey)
  1298. {
  1299. ret = RegSetValueExW(hkey, unix_fileW, 0, REG_SZ, (const BYTE*)windows_fileW,
  1300. (strlenW(windows_fileW) + 1) * sizeof(WCHAR));
  1301. RegCloseKey(hkey);
  1302. }
  1303. else
  1304. ret = GetLastError();
  1305. HeapFree(GetProcessHeap(), 0, windows_fileW);
  1306. }
  1307. else
  1308. ret = ERROR_NOT_ENOUGH_MEMORY;
  1309. HeapFree(GetProcessHeap(), 0, unix_fileW);
  1310. }
  1311. else
  1312. ret = ERROR_NOT_ENOUGH_MEMORY;
  1313. return ret;
  1314. }
  1315. static BOOL write_desktop_entry(const char *unix_link, const char *location, const char *linkname,
  1316. const char *path, const char *args, const char *descr,
  1317. const char *workdir, const char *icon, const char *wmclass)
  1318. {
  1319. FILE *file;
  1320. const char *prefix = getenv("WINEPREFIX");
  1321. const char *home = getenv("HOME");
  1322. WINE_TRACE("(%s,%s,%s,%s,%s,%s,%s,%s,%s)\n", wine_dbgstr_a(unix_link), wine_dbgstr_a(location),
  1323. wine_dbgstr_a(linkname), wine_dbgstr_a(path), wine_dbgstr_a(args),
  1324. wine_dbgstr_a(descr), wine_dbgstr_a(workdir), wine_dbgstr_a(icon),
  1325. wine_dbgstr_a(wmclass));
  1326. file = fopen(location, "w");
  1327. if (file == NULL)
  1328. return FALSE;
  1329. fprintf(file, "[Desktop Entry]\n");
  1330. fprintf(file, "Name=%s\n", linkname);
  1331. if (prefix)
  1332. fprintf(file, "Exec=env WINEPREFIX=\"%s\" wine %s %s\n", prefix, path, args);
  1333. else if (home)
  1334. fprintf(file, "Exec=env WINEPREFIX=\"%s/.wine\" wine %s %s\n", home, path, args);
  1335. else
  1336. fprintf(file, "Exec=wine %s %s\n", path, args);
  1337. fprintf(file, "Type=Application\n");
  1338. fprintf(file, "StartupNotify=true\n");
  1339. if (descr && *descr)
  1340. fprintf(file, "Comment=%s\n", descr);
  1341. if (workdir && *workdir)
  1342. fprintf(file, "Path=%s\n", workdir);
  1343. if (icon && *icon)
  1344. fprintf(file, "Icon=%s\n", icon);
  1345. if (wmclass && *wmclass)
  1346. fprintf(file, "StartupWMClass=%s\n", wmclass);
  1347. fclose(file);
  1348. if (unix_link)
  1349. {
  1350. DWORD ret = register_menus_entry(location, unix_link);
  1351. if (ret != ERROR_SUCCESS)
  1352. return FALSE;
  1353. }
  1354. return TRUE;
  1355. }
  1356. static BOOL write_directory_entry(const char *directory, const char *location)
  1357. {
  1358. FILE *file;
  1359. WINE_TRACE("(%s,%s)\n", wine_dbgstr_a(directory), wine_dbgstr_a(location));
  1360. file = fopen(location, "w");
  1361. if (file == NULL)
  1362. return FALSE;
  1363. fprintf(file, "[Desktop Entry]\n");
  1364. fprintf(file, "Type=Directory\n");
  1365. if (strcmp(directory, "wine") == 0)
  1366. {
  1367. fprintf(file, "Name=Wine\n");
  1368. fprintf(file, "Icon=wine\n");
  1369. }
  1370. else
  1371. {
  1372. fprintf(file, "Name=%s\n", directory);
  1373. fprintf(file, "Icon=folder\n");
  1374. }
  1375. fclose(file);
  1376. return TRUE;
  1377. }
  1378. static BOOL write_menu_file(const char *unix_link, const char *filename)
  1379. {
  1380. char *tempfilename;
  1381. FILE *tempfile = NULL;
  1382. char *lastEntry;
  1383. char *name = NULL;
  1384. char *menuPath = NULL;
  1385. int i;
  1386. int count = 0;
  1387. BOOL ret = FALSE;
  1388. WINE_TRACE("(%s)\n", wine_dbgstr_a(filename));
  1389. while (1)
  1390. {
  1391. tempfilename = heap_printf("%s/wine-menu-XXXXXX", xdg_config_dir);
  1392. if (tempfilename)
  1393. {
  1394. int tempfd = mkstemps(tempfilename, 0);
  1395. if (tempfd >= 0)
  1396. {
  1397. tempfile = fdopen(tempfd, "w");
  1398. if (tempfile)
  1399. break;
  1400. close(tempfd);
  1401. goto end;
  1402. }
  1403. else if (errno == EEXIST)
  1404. {
  1405. HeapFree(GetProcessHeap(), 0, tempfilename);
  1406. continue;
  1407. }
  1408. HeapFree(GetProcessHeap(), 0, tempfilename);
  1409. }
  1410. return FALSE;
  1411. }
  1412. fprintf(tempfile, "<!DOCTYPE Menu PUBLIC \"-//freedesktop//DTD Menu 1.0//EN\"\n");
  1413. fprintf(tempfile, "\"http://www.freedesktop.org/standards/menu-spec/menu-1.0.dtd\">\n");
  1414. fprintf(tempfile, "<Menu>\n");
  1415. fprintf(tempfile, " <Name>Applications</Name>\n");
  1416. name = HeapAlloc(GetProcessHeap(), 0, lstrlenA(filename) + 1);
  1417. if (name == NULL) goto end;
  1418. lastEntry = name;
  1419. for (i = 0; filename[i]; i++)
  1420. {
  1421. name[i] = filename[i];
  1422. if (filename[i] == '/')
  1423. {
  1424. char *dir_file_name;
  1425. struct stat st;
  1426. name[i] = 0;
  1427. fprintf(tempfile, " <Menu>\n");
  1428. fprintf(tempfile, " <Name>%s", count ? "" : "wine-");
  1429. write_xml_text(tempfile, name);
  1430. fprintf(tempfile, "</Name>\n");
  1431. fprintf(tempfile, " <Directory>%s", count ? "" : "wine-");
  1432. write_xml_text(tempfile, name);
  1433. fprintf(tempfile, ".directory</Directory>\n");
  1434. dir_file_name = heap_printf("%s/desktop-directories/%s%s.directory",
  1435. xdg_data_dir, count ? "" : "wine-", name);
  1436. if (dir_file_name)
  1437. {
  1438. if (stat(dir_file_name, &st) != 0 && errno == ENOENT)
  1439. write_directory_entry(lastEntry, dir_file_name);
  1440. HeapFree(GetProcessHeap(), 0, dir_file_name);
  1441. }
  1442. name[i] = '-';
  1443. lastEntry = &name[i+1];
  1444. ++count;
  1445. }
  1446. }
  1447. name[i] = 0;
  1448. fprintf(tempfile, " <Include>\n");
  1449. fprintf(tempfile, " <Filename>");
  1450. write_xml_text(tempfile, name);
  1451. fprintf(tempfile, "</Filename>\n");
  1452. fprintf(tempfile, " </Include>\n");
  1453. for (i = 0; i < count; i++)
  1454. fprintf(tempfile, " </Menu>\n");
  1455. fprintf(tempfile, "</Menu>\n");
  1456. menuPath = heap_printf("%s/%s", xdg_config_dir, name);
  1457. if (menuPath == NULL) goto end;
  1458. strcpy(menuPath + strlen(menuPath) - strlen(".desktop"), ".menu");
  1459. ret = TRUE;
  1460. end:
  1461. if (tempfile)
  1462. fclose(tempfile);
  1463. if (ret)
  1464. ret = (rename(tempfilename, menuPath) == 0);
  1465. if (!ret && tempfilename)
  1466. remove(tempfilename);
  1467. HeapFree(GetProcessHeap(), 0, tempfilename);
  1468. if (ret)
  1469. register_menus_entry(menuPath, unix_link);
  1470. HeapFree(GetProcessHeap(), 0, name);
  1471. HeapFree(GetProcessHeap(), 0, menuPath);
  1472. return ret;
  1473. }
  1474. static BOOL write_menu_entry(const char *unix_link, const char *link, const char *path, const char *args,
  1475. const char *descr, const char *workdir, const char *icon, const char *wmclass)
  1476. {
  1477. const char *linkname;
  1478. char *desktopPath = NULL;
  1479. char *desktopDir;
  1480. char *filename = NULL;
  1481. BOOL ret = TRUE;
  1482. WINE_TRACE("(%s, %s, %s, %s, %s, %s, %s, %s)\n", wine_dbgstr_a(unix_link), wine_dbgstr_a(link),
  1483. wine_dbgstr_a(path), wine_dbgstr_a(args), wine_dbgstr_a(descr),
  1484. wine_dbgstr_a(workdir), wine_dbgstr_a(icon), wine_dbgstr_a(wmclass));
  1485. linkname = strrchr(link, '/');
  1486. if (linkname == NULL)
  1487. linkname = link;
  1488. else
  1489. ++linkname;
  1490. desktopPath = heap_printf("%s/applications/wine/%s.desktop", xdg_data_dir, link);
  1491. if (!desktopPath)
  1492. {
  1493. WINE_WARN("out of memory creating menu entry\n");
  1494. ret = FALSE;
  1495. goto end;
  1496. }
  1497. desktopDir = strrchr(desktopPath, '/');
  1498. *desktopDir = 0;
  1499. if (!create_directories(desktopPath))
  1500. {
  1501. WINE_WARN("couldn't make parent directories for %s\n", wine_dbgstr_a(desktopPath));
  1502. ret = FALSE;
  1503. goto end;
  1504. }
  1505. *desktopDir = '/';
  1506. if (!write_desktop_entry(unix_link, desktopPath, linkname, path, args, descr, workdir, icon, wmclass))
  1507. {
  1508. WINE_WARN("couldn't make desktop entry %s\n", wine_dbgstr_a(desktopPath));
  1509. ret = FALSE;
  1510. goto end;
  1511. }
  1512. filename = heap_printf("wine/%s.desktop", link);
  1513. if (!filename || !write_menu_file(unix_link, filename))
  1514. {
  1515. WINE_WARN("couldn't make menu file %s\n", wine_dbgstr_a(filename));
  1516. ret = FALSE;
  1517. }
  1518. end:
  1519. HeapFree(GetProcessHeap(), 0, desktopPath);
  1520. HeapFree(GetProcessHeap(), 0, filename);
  1521. return ret;
  1522. }
  1523. /* This escapes reserved characters in .desktop files' Exec keys. */
  1524. static LPSTR escape(LPCWSTR arg)
  1525. {
  1526. int i, j;
  1527. WCHAR *escaped_string;
  1528. char *utf8_string;
  1529. escaped_string = HeapAlloc(GetProcessHeap(), 0, (4 * strlenW(arg) + 1) * sizeof(WCHAR));
  1530. if (escaped_string == NULL) return NULL;
  1531. for (i = j = 0; arg[i]; i++)
  1532. {
  1533. switch (arg[i])
  1534. {
  1535. case '\\':
  1536. escaped_string[j++] = '\\';
  1537. escaped_string[j++] = '\\';
  1538. escaped_string[j++] = '\\';
  1539. escaped_string[j++] = '\\';
  1540. break;
  1541. case ' ':
  1542. case '\t':
  1543. case '\n':
  1544. case '"':
  1545. case '\'':
  1546. case '>':
  1547. case '<':
  1548. case '~':
  1549. case '|':
  1550. case '&':
  1551. case ';':
  1552. case '$':
  1553. case '*':
  1554. case '?':
  1555. case '#':
  1556. case '(':
  1557. case ')':
  1558. case '`':
  1559. escaped_string[j++] = '\\';
  1560. escaped_string[j++] = '\\';
  1561. /* fall through */
  1562. default:
  1563. escaped_string[j++] = arg[i];
  1564. break;
  1565. }
  1566. }
  1567. escaped_string[j] = 0;
  1568. utf8_string = wchars_to_utf8_chars(escaped_string);
  1569. if (utf8_string == NULL)
  1570. {
  1571. WINE_ERR("out of memory\n");
  1572. goto end;
  1573. }
  1574. end:
  1575. HeapFree(GetProcessHeap(), 0, escaped_string);
  1576. return utf8_string;
  1577. }
  1578. /* Return a heap-allocated copy of the unix format difference between the two
  1579. * Windows-format paths.
  1580. * locn is the owning location
  1581. * link is within locn
  1582. */
  1583. static char *relative_path( LPCWSTR link, LPCWSTR locn )
  1584. {
  1585. char *unix_locn, *unix_link;
  1586. char *relative = NULL;
  1587. unix_locn = wine_get_unix_file_name(locn);
  1588. unix_link = wine_get_unix_file_name(link);
  1589. if (unix_locn && unix_link)
  1590. {
  1591. size_t len_unix_locn, len_unix_link;
  1592. len_unix_locn = strlen (unix_locn);
  1593. len_unix_link = strlen (unix_link);
  1594. if (len_unix_locn < len_unix_link && memcmp (unix_locn, unix_link, len_unix_locn) == 0 && unix_link[len_unix_locn] == '/')
  1595. {
  1596. size_t len_rel;
  1597. char *p = strrchr (unix_link + len_unix_locn, '/');
  1598. p = strrchr (p, '.');
  1599. if (p)
  1600. {
  1601. *p = '\0';
  1602. len_unix_link = p - unix_link;
  1603. }
  1604. len_rel = len_unix_link - len_unix_locn;
  1605. relative = HeapAlloc(GetProcessHeap(), 0, len_rel);
  1606. if (relative)
  1607. {
  1608. memcpy (relative, unix_link + len_unix_locn + 1, len_rel);
  1609. }
  1610. }
  1611. }
  1612. if (!relative)
  1613. WINE_WARN("Could not separate the relative link path of %s in %s\n", wine_dbgstr_w(link), wine_dbgstr_w(locn));
  1614. HeapFree(GetProcessHeap(), 0, unix_locn);
  1615. HeapFree(GetProcessHeap(), 0, unix_link);
  1616. return relative;
  1617. }
  1618. /***********************************************************************
  1619. *
  1620. * GetLinkLocation
  1621. *
  1622. * returns TRUE if successful
  1623. * *loc will contain CS_DESKTOPDIRECTORY, CS_STARTMENU, CS_STARTUP etc.
  1624. * *relative will contain the address of a heap-allocated copy of the portion
  1625. * of the filename that is within the specified location, in unix form
  1626. */
  1627. static BOOL GetLinkLocation( LPCWSTR linkfile, DWORD *loc, char **relative )
  1628. {
  1629. WCHAR filename[MAX_PATH], shortfilename[MAX_PATH], buffer[MAX_PATH];
  1630. DWORD len, i, r, filelen;
  1631. const DWORD locations[] = {
  1632. CSIDL_STARTUP, CSIDL_DESKTOPDIRECTORY, CSIDL_STARTMENU,
  1633. CSIDL_COMMON_STARTUP, CSIDL_COMMON_DESKTOPDIRECTORY,
  1634. CSIDL_COMMON_STARTMENU };
  1635. WINE_TRACE("%s\n", wine_dbgstr_w(linkfile));
  1636. filelen=GetFullPathNameW( linkfile, MAX_PATH, shortfilename, NULL );
  1637. if (filelen==0 || filelen>MAX_PATH)
  1638. return FALSE;
  1639. WINE_TRACE("%s\n", wine_dbgstr_w(shortfilename));
  1640. /* the CSLU Toolkit uses a short path name when creating .lnk files;
  1641. * expand or our hardcoded list won't match.
  1642. */
  1643. filelen=GetLongPathNameW(shortfilename, filename, MAX_PATH);
  1644. if (filelen==0 || filelen>MAX_PATH)
  1645. return FALSE;
  1646. WINE_TRACE("%s\n", wine_dbgstr_w(filename));
  1647. for( i=0; i<ARRAY_SIZE( locations ); i++ )
  1648. {
  1649. if (!SHGetSpecialFolderPathW( 0, buffer, locations[i], FALSE ))
  1650. continue;
  1651. len = lstrlenW(buffer);
  1652. if (len >= MAX_PATH)
  1653. continue; /* We've just trashed memory! Hopefully we are OK */
  1654. if (len > filelen || filename[len]!='\\')
  1655. continue;
  1656. /* do a lstrcmpinW */
  1657. filename[len] = 0;
  1658. r = lstrcmpiW( filename, buffer );
  1659. filename[len] = '\\';
  1660. if ( r )
  1661. continue;
  1662. /* return the remainder of the string and link type */
  1663. *loc = locations[i];
  1664. *relative = relative_path (filename, buffer);
  1665. return (*relative != NULL);
  1666. }
  1667. return FALSE;
  1668. }
  1669. /* gets the target path directly or through MSI */
  1670. static HRESULT get_cmdline( IShellLinkW *sl, LPWSTR szPath, DWORD pathSize,
  1671. LPWSTR szArgs, DWORD argsSize)
  1672. {
  1673. IShellLinkDataList *dl = NULL;
  1674. EXP_DARWIN_LINK *dar = NULL;
  1675. HRESULT hr;
  1676. szPath[0] = 0;
  1677. szArgs[0] = 0;
  1678. hr = IShellLinkW_GetPath( sl, szPath, pathSize, NULL, SLGP_RAWPATH );
  1679. if (hr == S_OK && szPath[0])
  1680. {
  1681. IShellLinkW_GetArguments( sl, szArgs, argsSize );
  1682. return hr;
  1683. }
  1684. hr = IShellLinkW_QueryInterface( sl, &IID_IShellLinkDataList, (LPVOID*) &dl );
  1685. if (FAILED(hr))
  1686. return hr;
  1687. hr = IShellLinkDataList_CopyDataBlock( dl, EXP_DARWIN_ID_SIG, (LPVOID*) &dar );
  1688. if (SUCCEEDED(hr))
  1689. {
  1690. WCHAR* szCmdline;
  1691. DWORD cmdSize;
  1692. cmdSize=0;
  1693. hr = CommandLineFromMsiDescriptor( dar->szwDarwinID, NULL, &cmdSize );
  1694. if (hr == ERROR_SUCCESS)
  1695. {
  1696. cmdSize++;
  1697. szCmdline = HeapAlloc( GetProcessHeap(), 0, cmdSize*sizeof(WCHAR) );
  1698. hr = CommandLineFromMsiDescriptor( dar->szwDarwinID, szCmdline, &cmdSize );
  1699. WINE_TRACE(" command : %s\n", wine_dbgstr_w(szCmdline));
  1700. if (hr == ERROR_SUCCESS)
  1701. {
  1702. WCHAR *s, *d;
  1703. int bcount = 0;
  1704. BOOL in_quotes = FALSE;
  1705. /* Extract the application path */
  1706. s=szCmdline;
  1707. d=szPath;
  1708. while (*s)
  1709. {
  1710. if ((*s==0x0009 || *s==0x0020) && !in_quotes)
  1711. {
  1712. /* skip the remaining spaces */
  1713. do {
  1714. s++;
  1715. } while (*s==0x0009 || *s==0x0020);
  1716. break;
  1717. }
  1718. else if (*s==0x005c)
  1719. {
  1720. /* '\\' */
  1721. *d++=*s++;
  1722. bcount++;
  1723. }
  1724. else if (*s==0x0022)
  1725. {
  1726. /* '"' */
  1727. if ((bcount & 1)==0)
  1728. {
  1729. /* Preceded by an even number of '\', this is
  1730. * half that number of '\', plus a quote which
  1731. * we erase.
  1732. */
  1733. d-=bcount/2;
  1734. in_quotes=!in_quotes;
  1735. s++;
  1736. }
  1737. else
  1738. {
  1739. /* Preceded by an odd number of '\', this is
  1740. * half that number of '\' followed by a '"'
  1741. */
  1742. d=d-bcount/2-1;
  1743. *d++='"';
  1744. s++;
  1745. }
  1746. bcount=0;
  1747. }
  1748. else
  1749. {
  1750. /* a regular character */
  1751. *d++=*s++;
  1752. bcount=0;
  1753. }
  1754. if ((d-szPath) == pathSize)
  1755. {
  1756. /* Keep processing the path till we get to the
  1757. * arguments, but 'stand still'
  1758. */
  1759. d--;
  1760. }
  1761. }
  1762. /* Close the application path */
  1763. *d=0;
  1764. lstrcpynW(szArgs, s, argsSize);
  1765. }
  1766. HeapFree( GetProcessHeap(), 0, szCmdline );
  1767. }
  1768. LocalFree( dar );
  1769. }
  1770. IShellLinkDataList_Release( dl );
  1771. return hr;
  1772. }
  1773. static char *slashes_to_minuses(const char *string)
  1774. {
  1775. int i;
  1776. char *ret = HeapAlloc(GetProcessHeap(), 0, lstrlenA(string) + 1);
  1777. if (ret)
  1778. {
  1779. for (i = 0; string[i]; i++)
  1780. {
  1781. if (string[i] == '/')
  1782. ret[i] = '-';
  1783. else
  1784. ret[i] = string[i];
  1785. }
  1786. ret[i] = 0;
  1787. return ret;
  1788. }
  1789. return NULL;
  1790. }
  1791. static BOOL next_line(FILE *file, char **line, int *size)
  1792. {
  1793. int pos = 0;
  1794. char *cr;
  1795. if (*line == NULL)
  1796. {
  1797. *size = 4096;
  1798. *line = HeapAlloc(GetProcessHeap(), 0, *size);
  1799. }
  1800. while (*line != NULL)
  1801. {
  1802. if (fgets(&(*line)[pos], *size - pos, file) == NULL)
  1803. {
  1804. HeapFree(GetProcessHeap(), 0, *line);
  1805. *line = NULL;
  1806. if (feof(file))
  1807. return TRUE;
  1808. return FALSE;
  1809. }
  1810. pos = strlen(*line);
  1811. cr = strchr(*line, '\n');
  1812. if (cr == NULL)
  1813. {
  1814. char *line2;
  1815. (*size) *= 2;
  1816. line2 = HeapReAlloc(GetProcessHeap(), 0, *line, *size);
  1817. if (line2)
  1818. *line = line2;
  1819. else
  1820. {
  1821. HeapFree(GetProcessHeap(), 0, *line);
  1822. *line = NULL;
  1823. }
  1824. }
  1825. else
  1826. {
  1827. *cr = 0;
  1828. return TRUE;
  1829. }
  1830. }
  1831. return FALSE;
  1832. }
  1833. static BOOL add_mimes(const char *xdg_data_dir, struct list *mime_types)
  1834. {
  1835. char *globs_filename = NULL;
  1836. BOOL ret = TRUE;
  1837. globs_filename = heap_printf("%s/mime/globs", xdg_data_dir);
  1838. if (globs_filename)
  1839. {
  1840. FILE *globs_file = fopen(globs_filename, "r");
  1841. if (globs_file) /* doesn't have to exist */
  1842. {
  1843. char *line = NULL;
  1844. int size = 0;
  1845. while (ret && (ret = next_line(globs_file, &line, &size)) && line)
  1846. {
  1847. char *pos;
  1848. struct xdg_mime_type *mime_type_entry = NULL;
  1849. if (line[0] != '#' && (pos = strchr(line, ':')))
  1850. {
  1851. mime_type_entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct xdg_mime_type));
  1852. if (mime_type_entry)
  1853. {
  1854. *pos = 0;
  1855. mime_type_entry->mimeType = strdupA(line);
  1856. mime_type_entry->glob = strdupA(pos + 1);
  1857. mime_type_entry->lower_glob = strdupA(pos + 1);
  1858. if (mime_type_entry->lower_glob)
  1859. {
  1860. char *l;
  1861. for (l = mime_type_entry->lower_glob; *l; l++)
  1862. *l = tolower(*l);
  1863. }
  1864. if (mime_type_entry->mimeType && mime_type_entry->glob && mime_type_entry->lower_glob)
  1865. list_add_tail(mime_types, &mime_type_entry->entry);
  1866. else
  1867. {
  1868. HeapFree(GetProcessHeap(), 0, mime_type_entry->mimeType);
  1869. HeapFree(GetProcessHeap(), 0, mime_type_entry->glob);
  1870. HeapFree(GetProcessHeap(), 0, mime_type_entry->lower_glob);
  1871. HeapFree(GetProcessHeap(), 0, mime_type_entry);
  1872. ret = FALSE;
  1873. }
  1874. }
  1875. else
  1876. ret = FALSE;
  1877. }
  1878. }
  1879. HeapFree(GetProcessHeap(), 0, line);
  1880. fclose(globs_file);
  1881. }
  1882. HeapFree(GetProcessHeap(), 0, globs_filename);
  1883. }
  1884. else
  1885. ret = FALSE;
  1886. return ret;
  1887. }
  1888. static void free_native_mime_types(struct list *native_mime_types)
  1889. {
  1890. struct xdg_mime_type *mime_type_entry, *mime_type_entry2;
  1891. LIST_FOR_EACH_ENTRY_SAFE(mime_type_entry, mime_type_entry2, native_mime_types, struct xdg_mime_type, entry)
  1892. {
  1893. list_remove(&mime_type_entry->entry);
  1894. HeapFree(GetProcessHeap(), 0, mime_type_entry->glob);
  1895. HeapFree(GetProcessHeap(), 0, mime_type_entry->lower_glob);
  1896. HeapFree(GetProcessHeap(), 0, mime_type_entry->mimeType);
  1897. HeapFree(GetProcessHeap(), 0, mime_type_entry);
  1898. }
  1899. }
  1900. static BOOL build_native_mime_types(const char *xdg_data_home, struct list *mime_types)
  1901. {
  1902. char *xdg_data_dirs;
  1903. BOOL ret;
  1904. xdg_data_dirs = getenv("XDG_DATA_DIRS");
  1905. if (xdg_data_dirs == NULL)
  1906. xdg_data_dirs = heap_printf("/usr/local/share/:/usr/share/");
  1907. else
  1908. xdg_data_dirs = strdupA(xdg_data_dirs);
  1909. if (xdg_data_dirs)
  1910. {
  1911. const char *begin;
  1912. char *end;
  1913. ret = add_mimes(xdg_data_home, mime_types);
  1914. if (ret)
  1915. {
  1916. for (begin = xdg_data_dirs; (end = strchr(begin, ':')); begin = end + 1)
  1917. {
  1918. *end = '\0';
  1919. ret = add_mimes(begin, mime_types);
  1920. *end = ':';
  1921. if (!ret)
  1922. break;
  1923. }
  1924. if (ret)
  1925. ret = add_mimes(begin, mime_types);
  1926. }
  1927. HeapFree(GetProcessHeap(), 0, xdg_data_dirs);
  1928. }
  1929. else
  1930. ret = FALSE;
  1931. if (!ret)
  1932. free_native_mime_types(mime_types);
  1933. return ret;
  1934. }
  1935. static BOOL match_glob(struct list *native_mime_types, const char *extension,
  1936. int ignoreGlobCase, char **match)
  1937. {
  1938. #ifdef HAVE_FNMATCH
  1939. struct xdg_mime_type *mime_type_entry;
  1940. int matchLength = 0;
  1941. *match = NULL;
  1942. LIST_FOR_EACH_ENTRY(mime_type_entry, native_mime_types, struct xdg_mime_type, entry)
  1943. {
  1944. const char *glob = ignoreGlobCase ? mime_type_entry->lower_glob : mime_type_entry->glob;
  1945. if (fnmatch(glob, extension, 0) == 0)
  1946. {
  1947. if (*match == NULL || matchLength < strlen(glob))
  1948. {
  1949. *match = mime_type_entry->mimeType;
  1950. matchLength = strlen(glob);
  1951. }
  1952. }
  1953. }
  1954. if (*match != NULL)
  1955. {
  1956. *match = strdupA(*match);
  1957. if (*match == NULL)
  1958. return FALSE;
  1959. }
  1960. #else
  1961. *match = NULL;
  1962. #endif
  1963. return TRUE;
  1964. }
  1965. static BOOL freedesktop_mime_type_for_extension(struct list *native_mime_types,
  1966. const char *extensionA,
  1967. LPCWSTR extensionW,
  1968. char **mime_type)
  1969. {
  1970. WCHAR *lower_extensionW;
  1971. INT len;
  1972. BOOL ret = match_glob(native_mime_types, extensionA, 0, mime_type);
  1973. if (ret == FALSE || *mime_type != NULL)
  1974. return ret;
  1975. len = strlenW(extensionW);
  1976. lower_extensionW = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
  1977. if (lower_extensionW)
  1978. {
  1979. char *lower_extensionA;
  1980. memcpy(lower_extensionW, extensionW, (len + 1)*sizeof(WCHAR));
  1981. strlwrW(lower_extensionW);
  1982. lower_extensionA = wchars_to_utf8_chars(lower_extensionW);
  1983. if (lower_extensionA)
  1984. {
  1985. ret = match_glob(native_mime_types, lower_extensionA, 1, mime_type);
  1986. HeapFree(GetProcessHeap(), 0, lower_extensionA);
  1987. }
  1988. else
  1989. {
  1990. ret = FALSE;
  1991. WINE_FIXME("out of memory\n");
  1992. }
  1993. HeapFree(GetProcessHeap(), 0, lower_extensionW);
  1994. }
  1995. else
  1996. {
  1997. ret = FALSE;
  1998. WINE_FIXME("out of memory\n");
  1999. }
  2000. return ret;
  2001. }
  2002. static WCHAR* reg_get_valW(HKEY key, LPCWSTR subkey, LPCWSTR name)
  2003. {
  2004. DWORD size;
  2005. if (RegGetValueW(key, subkey, name, RRF_RT_REG_SZ, NULL, NULL, &size) == ERROR_SUCCESS)
  2006. {
  2007. WCHAR *ret = HeapAlloc(GetProcessHeap(), 0, size);
  2008. if (ret)
  2009. {
  2010. if (RegGetValueW(key, subkey, name, RRF_RT_REG_SZ, NULL, ret, &size) == ERROR_SUCCESS)
  2011. return ret;
  2012. }
  2013. HeapFree(GetProcessHeap(), 0, ret);
  2014. }
  2015. return NULL;
  2016. }
  2017. static CHAR* reg_get_val_utf8(HKEY key, LPCWSTR subkey, LPCWSTR name)
  2018. {
  2019. WCHAR *valW = reg_get_valW(key, subkey, name);
  2020. if (valW)
  2021. {
  2022. char *val = wchars_to_utf8_chars(valW);
  2023. HeapFree(GetProcessHeap(), 0, valW);
  2024. return val;
  2025. }
  2026. return NULL;
  2027. }
  2028. static HKEY open_associations_reg_key(void)
  2029. {
  2030. static const WCHAR Software_Wine_FileOpenAssociationsW[] = {
  2031. '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};
  2032. HKEY assocKey;
  2033. if (RegCreateKeyW(HKEY_CURRENT_USER, Software_Wine_FileOpenAssociationsW, &assocKey) == ERROR_SUCCESS)
  2034. return assocKey;
  2035. return NULL;
  2036. }
  2037. static BOOL has_association_changed(LPCWSTR extensionW, LPCSTR mimeType, LPCWSTR progId,
  2038. LPCSTR appName, LPCSTR openWithIcon)
  2039. {
  2040. static const WCHAR ProgIDW[] = {'P','r','o','g','I','D',0};
  2041. static const WCHAR MimeTypeW[] = {'M','i','m','e','T','y','p','e',0};
  2042. static const WCHAR AppNameW[] = {'A','p','p','N','a','m','e',0};
  2043. static const WCHAR OpenWithIconW[] = {'O','p','e','n','W','i','t','h','I','c','o','n',0};
  2044. HKEY assocKey;
  2045. BOOL ret;
  2046. if ((assocKey = open_associations_reg_key()))
  2047. {
  2048. CHAR *valueA;
  2049. WCHAR *value;
  2050. ret = FALSE;
  2051. valueA = reg_get_val_utf8(assocKey, extensionW, MimeTypeW);
  2052. if (!valueA || lstrcmpA(valueA, mimeType))
  2053. ret = TRUE;
  2054. HeapFree(GetProcessHeap(), 0, valueA);
  2055. value = reg_get_valW(assocKey, extensionW, ProgIDW);
  2056. if (!value || strcmpW(value, progId))
  2057. ret = TRUE;
  2058. HeapFree(GetProcessHeap(), 0, value);
  2059. valueA = reg_get_val_utf8(assocKey, extensionW, AppNameW);
  2060. if (!valueA || lstrcmpA(valueA, appName))
  2061. ret = TRUE;
  2062. HeapFree(GetProcessHeap(), 0, valueA);
  2063. valueA = reg_get_val_utf8(assocKey, extensionW, OpenWithIconW);
  2064. if ((openWithIcon && !valueA) ||
  2065. (!openWithIcon && valueA) ||
  2066. (openWithIcon && valueA && lstrcmpA(valueA, openWithIcon)))
  2067. ret = TRUE;
  2068. HeapFree(GetProcessHeap(), 0, valueA);
  2069. RegCloseKey(assocKey);
  2070. }
  2071. else
  2072. {
  2073. WINE_ERR("error opening associations registry key\n");
  2074. ret = FALSE;
  2075. }
  2076. return ret;
  2077. }
  2078. static void update_association(LPCWSTR extension, LPCSTR mimeType, LPCWSTR progId,
  2079. LPCSTR appName, LPCSTR desktopFile, LPCSTR openWithIcon)
  2080. {
  2081. static const WCHAR ProgIDW[] = {'P','r','o','g','I','D',0};
  2082. static const WCHAR MimeTypeW[] = {'M','i','m','e','T','y','p','e',0};
  2083. static const WCHAR AppNameW[] = {'A','p','p','N','a','m','e',0};
  2084. static const WCHAR DesktopFileW[] = {'D','e','s','k','t','o','p','F','i','l','e',0};
  2085. static const WCHAR OpenWithIconW[] = {'O','p','e','n','W','i','t','h','I','c','o','n',0};
  2086. HKEY assocKey = NULL;
  2087. HKEY subkey = NULL;
  2088. WCHAR *mimeTypeW = NULL;
  2089. WCHAR *appNameW = NULL;
  2090. WCHAR *desktopFileW = NULL;
  2091. WCHAR *openWithIconW = NULL;
  2092. assocKey = open_associations_reg_key();
  2093. if (assocKey == NULL)
  2094. {
  2095. WINE_ERR("could not open file associations key\n");
  2096. goto done;
  2097. }
  2098. if (RegCreateKeyW(assocKey, extension, &subkey) != ERROR_SUCCESS)
  2099. {
  2100. WINE_ERR("could not create extension subkey\n");
  2101. goto done;
  2102. }
  2103. mimeTypeW = utf8_chars_to_wchars(mimeType);
  2104. if (mimeTypeW == NULL)
  2105. {
  2106. WINE_ERR("out of memory\n");
  2107. goto done;
  2108. }
  2109. appNameW = utf8_chars_to_wchars(appName);
  2110. if (appNameW == NULL)
  2111. {
  2112. WINE_ERR("out of memory\n");
  2113. goto done;
  2114. }
  2115. desktopFileW = utf8_chars_to_wchars(desktopFile);
  2116. if (desktopFileW == NULL)
  2117. {
  2118. WINE_ERR("out of memory\n");
  2119. goto done;
  2120. }
  2121. if (openWithIcon)
  2122. {
  2123. openWithIconW = utf8_chars_to_wchars(openWithIcon);
  2124. if (openWithIconW == NULL)
  2125. {
  2126. WINE_ERR("out of memory\n");
  2127. goto done;
  2128. }
  2129. }
  2130. RegSetValueExW(subkey, MimeTypeW, 0, REG_SZ, (const BYTE*) mimeTypeW, (lstrlenW(mimeTypeW) + 1) * sizeof(WCHAR));
  2131. RegSetValueExW(subkey, ProgIDW, 0, REG_SZ, (const BYTE*) progId, (lstrlenW(progId) + 1) * sizeof(WCHAR));
  2132. RegSetValueExW(subkey, AppNameW, 0, REG_SZ, (const BYTE*) appNameW, (lstrlenW(appNameW) + 1) * sizeof(WCHAR));
  2133. RegSetValueExW(subkey, DesktopFileW, 0, REG_SZ, (const BYTE*) desktopFileW, (lstrlenW(desktopFileW) + 1) * sizeof(WCHAR));
  2134. if (openWithIcon)
  2135. RegSetValueExW(subkey, OpenWithIconW, 0, REG_SZ, (const BYTE*) openWithIconW, (lstrlenW(openWithIconW) + 1) * sizeof(WCHAR));
  2136. else
  2137. RegDeleteValueW(subkey, OpenWithIconW);
  2138. done:
  2139. RegCloseKey(assocKey);
  2140. RegCloseKey(subkey);
  2141. HeapFree(GetProcessHeap(), 0, mimeTypeW);
  2142. HeapFree(GetProcessHeap(), 0, appNameW);
  2143. HeapFree(GetProcessHeap(), 0, desktopFileW);
  2144. HeapFree(GetProcessHeap(), 0, openWithIconW);
  2145. }
  2146. static BOOL cleanup_associations(void)
  2147. {
  2148. static const WCHAR openW[] = {'o','p','e','n',0};
  2149. static const WCHAR DesktopFileW[] = {'D','e','s','k','t','o','p','F','i','l','e',0};
  2150. HKEY assocKey;
  2151. BOOL hasChanged = FALSE;
  2152. if ((assocKey = open_associations_reg_key()))
  2153. {
  2154. int i;
  2155. BOOL done = FALSE;
  2156. for (i = 0; !done;)
  2157. {
  2158. WCHAR *extensionW = NULL;
  2159. DWORD size = 1024;
  2160. LSTATUS ret;
  2161. do
  2162. {
  2163. HeapFree(GetProcessHeap(), 0, extensionW);
  2164. extensionW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
  2165. if (extensionW == NULL)
  2166. {
  2167. WINE_ERR("out of memory\n");
  2168. ret = ERROR_OUTOFMEMORY;
  2169. break;
  2170. }
  2171. ret = RegEnumKeyExW(assocKey, i, extensionW, &size, NULL, NULL, NULL, NULL);
  2172. size *= 2;
  2173. } while (ret == ERROR_MORE_DATA);
  2174. if (ret == ERROR_SUCCESS)
  2175. {
  2176. WCHAR *command;
  2177. command = assoc_query(ASSOCSTR_COMMAND, extensionW, openW);
  2178. if (command == NULL)
  2179. {
  2180. char *desktopFile = reg_get_val_utf8(assocKey, extensionW, DesktopFileW);
  2181. if (desktopFile)
  2182. {
  2183. WINE_TRACE("removing file type association for %s\n", wine_dbgstr_w(extensionW));
  2184. remove(desktopFile);
  2185. }
  2186. RegDeleteKeyW(assocKey, extensionW);
  2187. hasChanged = TRUE;
  2188. HeapFree(GetProcessHeap(), 0, desktopFile);
  2189. }
  2190. else
  2191. i++;
  2192. HeapFree(GetProcessHeap(), 0, command);
  2193. }
  2194. else
  2195. {
  2196. if (ret != ERROR_NO_MORE_ITEMS)
  2197. WINE_ERR("error %d while reading registry\n", ret);
  2198. done = TRUE;
  2199. }
  2200. HeapFree(GetProcessHeap(), 0, extensionW);
  2201. }
  2202. RegCloseKey(assocKey);
  2203. }
  2204. else
  2205. WINE_ERR("could not open file associations key\n");
  2206. return hasChanged;
  2207. }
  2208. static BOOL write_freedesktop_mime_type_entry(const char *packages_dir, const char *dot_extension,
  2209. const char *mime_type, const char *comment)
  2210. {
  2211. BOOL ret = FALSE;
  2212. char *filename;
  2213. WINE_TRACE("writing MIME type %s, extension=%s, comment=%s\n", wine_dbgstr_a(mime_type),
  2214. wine_dbgstr_a(dot_extension), wine_dbgstr_a(comment));
  2215. filename = heap_printf("%s/x-wine-extension-%s.xml", packages_dir, &dot_extension[1]);
  2216. if (filename)
  2217. {
  2218. FILE *packageFile = fopen(filename, "w");
  2219. if (packageFile)
  2220. {
  2221. fprintf(packageFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  2222. fprintf(packageFile, "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n");
  2223. fprintf(packageFile, " <mime-type type=\"");
  2224. write_xml_text(packageFile, mime_type);
  2225. fprintf(packageFile, "\">\n");
  2226. fprintf(packageFile, " <glob pattern=\"*");
  2227. write_xml_text(packageFile, dot_extension);
  2228. fprintf(packageFile, "\"/>\n");
  2229. if (comment)
  2230. {
  2231. fprintf(packageFile, " <comment>");
  2232. write_xml_text(packageFile, comment);
  2233. fprintf(packageFile, "</comment>\n");
  2234. }
  2235. fprintf(packageFile, " </mime-type>\n");
  2236. fprintf(packageFile, "</mime-info>\n");
  2237. ret = TRUE;
  2238. fclose(packageFile);
  2239. }
  2240. else
  2241. WINE_ERR("error writing file %s\n", filename);
  2242. HeapFree(GetProcessHeap(), 0, filename);
  2243. }
  2244. else
  2245. WINE_ERR("out of memory\n");
  2246. return ret;
  2247. }
  2248. static BOOL is_extension_banned(LPCWSTR extension)
  2249. {
  2250. /* These are managed through external tools like wine.desktop, to evade malware created file type associations */
  2251. static const WCHAR comW[] = {'.','c','o','m',0};
  2252. static const WCHAR exeW[] = {'.','e','x','e',0};
  2253. static const WCHAR msiW[] = {'.','m','s','i',0};
  2254. if (!strcmpiW(extension, comW) ||
  2255. !strcmpiW(extension, exeW) ||
  2256. !strcmpiW(extension, msiW))
  2257. return TRUE;
  2258. return FALSE;
  2259. }
  2260. static const char* get_special_mime_type(LPCWSTR extension)
  2261. {
  2262. static const WCHAR lnkW[] = {'.','l','n','k',0};
  2263. if (!strcmpiW(extension, lnkW))
  2264. return "application/x-ms-shortcut";
  2265. return NULL;
  2266. }
  2267. static BOOL write_freedesktop_association_entry(const char *desktopPath, const char *dot_extension,
  2268. const char *friendlyAppName, const char *mimeType,
  2269. const char *progId, const char *openWithIcon)
  2270. {
  2271. BOOL ret = FALSE;
  2272. FILE *desktop;
  2273. const char *prefix = getenv("WINEPREFIX");
  2274. const char *home = getenv("HOME");
  2275. WINE_TRACE("writing association for file type %s, friendlyAppName=%s, MIME type %s, progID=%s, icon=%s to file %s\n",
  2276. wine_dbgstr_a(dot_extension), wine_dbgstr_a(friendlyAppName), wine_dbgstr_a(mimeType),
  2277. wine_dbgstr_a(progId), wine_dbgstr_a(openWithIcon), wine_dbgstr_a(desktopPath));
  2278. desktop = fopen(desktopPath, "w");
  2279. if (desktop)
  2280. {
  2281. fprintf(desktop, "[Desktop Entry]\n");
  2282. fprintf(desktop, "Type=Application\n");
  2283. fprintf(desktop, "Name=%s\n", friendlyAppName);
  2284. fprintf(desktop, "MimeType=%s;\n", mimeType);
  2285. if (prefix)
  2286. fprintf(desktop, "Exec=env WINEPREFIX=\"%s\" wine start /ProgIDOpen %s %%f\n", prefix, progId);
  2287. else if (home)
  2288. fprintf(desktop, "Exec=env WINEPREFIX=\"%s/.wine\" wine start /ProgIDOpen %s %%f\n", home, progId);
  2289. else
  2290. fprintf(desktop, "Exec=wine start /ProgIDOpen %s %%f\n", progId);
  2291. fprintf(desktop, "NoDisplay=true\n");
  2292. fprintf(desktop, "StartupNotify=true\n");
  2293. if (openWithIcon)
  2294. fprintf(desktop, "Icon=%s\n", openWithIcon);
  2295. ret = TRUE;
  2296. fclose(desktop);
  2297. }
  2298. else
  2299. WINE_ERR("error writing association file %s\n", wine_dbgstr_a(desktopPath));
  2300. return ret;
  2301. }
  2302. static BOOL generate_associations(const char *xdg_data_home, const char *packages_dir, const char *applications_dir)
  2303. {
  2304. static const WCHAR openW[] = {'o','p','e','n',0};
  2305. struct wine_rb_tree mimeProgidTree = { winemenubuilder_rb_string_compare };
  2306. struct list nativeMimeTypes = LIST_INIT(nativeMimeTypes);
  2307. LSTATUS ret = 0;
  2308. int i;
  2309. BOOL hasChanged = FALSE;
  2310. if (!build_native_mime_types(xdg_data_home, &nativeMimeTypes))
  2311. {
  2312. WINE_ERR("could not build native MIME types\n");
  2313. return FALSE;
  2314. }
  2315. for (i = 0; ; i++)
  2316. {
  2317. WCHAR *extensionW = NULL;
  2318. DWORD size = 1024;
  2319. do
  2320. {
  2321. HeapFree(GetProcessHeap(), 0, extensionW);
  2322. extensionW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
  2323. if (extensionW == NULL)
  2324. {
  2325. WINE_ERR("out of memory\n");
  2326. ret = ERROR_OUTOFMEMORY;
  2327. break;
  2328. }
  2329. ret = RegEnumKeyExW(HKEY_CLASSES_ROOT, i, extensionW, &size, NULL, NULL, NULL, NULL);
  2330. size *= 2;
  2331. } while (ret == ERROR_MORE_DATA);
  2332. if (ret == ERROR_SUCCESS && extensionW[0] == '.' && !is_extension_banned(extensionW))
  2333. {
  2334. char *extensionA = NULL;
  2335. WCHAR *commandW = NULL;
  2336. WCHAR *executableW = NULL;
  2337. char *openWithIconA = NULL;
  2338. WCHAR *friendlyDocNameW = NULL;
  2339. char *friendlyDocNameA = NULL;
  2340. WCHAR *iconW = NULL;
  2341. char *iconA = NULL;
  2342. WCHAR *contentTypeW = NULL;
  2343. char *mimeTypeA = NULL;
  2344. WCHAR *friendlyAppNameW = NULL;
  2345. char *friendlyAppNameA = NULL;
  2346. WCHAR *progIdW = NULL;
  2347. char *progIdA = NULL;
  2348. char *mimeProgId = NULL;
  2349. extensionA = wchars_to_utf8_chars(strlwrW(extensionW));
  2350. if (extensionA == NULL)
  2351. {
  2352. WINE_ERR("out of memory\n");
  2353. goto end;
  2354. }
  2355. friendlyDocNameW = assoc_query(ASSOCSTR_FRIENDLYDOCNAME, extensionW, NULL);
  2356. if (friendlyDocNameW)
  2357. {
  2358. friendlyDocNameA = wchars_to_utf8_chars(friendlyDocNameW);
  2359. if (friendlyDocNameA == NULL)
  2360. {
  2361. WINE_ERR("out of memory\n");
  2362. goto end;
  2363. }
  2364. }
  2365. iconW = assoc_query(ASSOCSTR_DEFAULTICON, extensionW, NULL);
  2366. contentTypeW = assoc_query(ASSOCSTR_CONTENTTYPE, extensionW, NULL);
  2367. if (contentTypeW)
  2368. strlwrW(contentTypeW);
  2369. if (!freedesktop_mime_type_for_extension(&nativeMimeTypes, extensionA, extensionW, &mimeTypeA))
  2370. goto end;
  2371. if (mimeTypeA == NULL)
  2372. {
  2373. if (contentTypeW != NULL && strchrW(contentTypeW, '/'))
  2374. mimeTypeA = wchars_to_utf8_chars(contentTypeW);
  2375. else if ((get_special_mime_type(extensionW)))
  2376. mimeTypeA = strdupA(get_special_mime_type(extensionW));
  2377. else
  2378. mimeTypeA = heap_printf("application/x-wine-extension-%s", &extensionA[1]);
  2379. if (mimeTypeA != NULL)
  2380. {
  2381. /* GNOME seems to ignore the <icon> tag in MIME packages,
  2382. * and the default name is more intuitive anyway.
  2383. */
  2384. if (iconW)
  2385. {
  2386. char *flattened_mime = slashes_to_minuses(mimeTypeA);
  2387. if (flattened_mime)
  2388. {
  2389. int index = 0;
  2390. WCHAR *comma = strrchrW(iconW, ',');
  2391. if (comma)
  2392. {
  2393. *comma = 0;
  2394. index = atoiW(comma + 1);
  2395. }
  2396. iconA = extract_icon(iconW, index, flattened_mime, FALSE);
  2397. HeapFree(GetProcessHeap(), 0, flattened_mime);
  2398. }
  2399. }
  2400. write_freedesktop_mime_type_entry(packages_dir, extensionA, mimeTypeA, friendlyDocNameA);
  2401. hasChanged = TRUE;
  2402. }
  2403. else
  2404. {
  2405. WINE_FIXME("out of memory\n");
  2406. goto end;
  2407. }
  2408. }
  2409. commandW = assoc_query(ASSOCSTR_COMMAND, extensionW, openW);
  2410. if (commandW == NULL)
  2411. /* no command => no application is associated */
  2412. goto end;
  2413. executableW = assoc_query(ASSOCSTR_EXECUTABLE, extensionW, openW);
  2414. if (executableW)
  2415. openWithIconA = compute_native_identifier(0, executableW);
  2416. friendlyAppNameW = assoc_query(ASSOCSTR_FRIENDLYAPPNAME, extensionW, openW);
  2417. if (friendlyAppNameW)
  2418. {
  2419. friendlyAppNameA = wchars_to_utf8_chars(friendlyAppNameW);
  2420. if (friendlyAppNameA == NULL)
  2421. {
  2422. WINE_ERR("out of memory\n");
  2423. goto end;
  2424. }
  2425. }
  2426. else
  2427. {
  2428. friendlyAppNameA = heap_printf("A Wine application");
  2429. if (friendlyAppNameA == NULL)
  2430. {
  2431. WINE_ERR("out of memory\n");
  2432. goto end;
  2433. }
  2434. }
  2435. progIdW = reg_get_valW(HKEY_CLASSES_ROOT, extensionW, NULL);
  2436. if (progIdW)
  2437. {
  2438. progIdA = escape(progIdW);
  2439. if (progIdA == NULL)
  2440. {
  2441. WINE_ERR("out of memory\n");
  2442. goto end;
  2443. }
  2444. }
  2445. else
  2446. goto end; /* no progID => not a file type association */
  2447. /* Do not allow duplicate ProgIDs for a MIME type, it causes unnecessary duplication in Open dialogs */
  2448. mimeProgId = heap_printf("%s=>%s", mimeTypeA, progIdA);
  2449. if (mimeProgId)
  2450. {
  2451. struct rb_string_entry *entry;
  2452. if (wine_rb_get(&mimeProgidTree, mimeProgId))
  2453. {
  2454. HeapFree(GetProcessHeap(), 0, mimeProgId);
  2455. goto end;
  2456. }
  2457. entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct rb_string_entry));
  2458. if (!entry)
  2459. {
  2460. WINE_ERR("out of memory allocating rb_string_entry\n");
  2461. goto end;
  2462. }
  2463. entry->string = mimeProgId;
  2464. if (wine_rb_put(&mimeProgidTree, mimeProgId, &entry->entry))
  2465. {
  2466. WINE_ERR("error updating rb tree\n");
  2467. goto end;
  2468. }
  2469. }
  2470. if (has_association_changed(extensionW, mimeTypeA, progIdW, friendlyAppNameA, openWithIconA))
  2471. {
  2472. char *desktopPath = heap_printf("%s/wine-extension-%s.desktop", applications_dir, &extensionA[1]);
  2473. if (desktopPath)
  2474. {
  2475. if (write_freedesktop_association_entry(desktopPath, extensionA, friendlyAppNameA, mimeTypeA, progIdA, openWithIconA))
  2476. {
  2477. hasChanged = TRUE;
  2478. update_association(extensionW, mimeTypeA, progIdW, friendlyAppNameA, desktopPath, openWithIconA);
  2479. }
  2480. HeapFree(GetProcessHeap(), 0, desktopPath);
  2481. }
  2482. }
  2483. if (hasChanged && openWithIconA)
  2484. {
  2485. char *outputIconA = extract_icon(executableW, 0, openWithIconA, FALSE);
  2486. HeapFree(GetProcessHeap(), 0, outputIconA);
  2487. }
  2488. end:
  2489. HeapFree(GetProcessHeap(), 0, extensionA);
  2490. HeapFree(GetProcessHeap(), 0, commandW);
  2491. HeapFree(GetProcessHeap(), 0, executableW);
  2492. HeapFree(GetProcessHeap(), 0, openWithIconA);
  2493. HeapFree(GetProcessHeap(), 0, friendlyDocNameW);
  2494. HeapFree(GetProcessHeap(), 0, friendlyDocNameA);
  2495. HeapFree(GetProcessHeap(), 0, iconW);
  2496. HeapFree(GetProcessHeap(), 0, iconA);
  2497. HeapFree(GetProcessHeap(), 0, contentTypeW);
  2498. HeapFree(GetProcessHeap(), 0, mimeTypeA);
  2499. HeapFree(GetProcessHeap(), 0, friendlyAppNameW);
  2500. HeapFree(GetProcessHeap(), 0, friendlyAppNameA);
  2501. HeapFree(GetProcessHeap(), 0, progIdW);
  2502. HeapFree(GetProcessHeap(), 0, progIdA);
  2503. }
  2504. HeapFree(GetProcessHeap(), 0, extensionW);
  2505. if (ret != ERROR_SUCCESS)
  2506. break;
  2507. }
  2508. wine_rb_destroy(&mimeProgidTree, winemenubuilder_rb_destroy, NULL);
  2509. free_native_mime_types(&nativeMimeTypes);
  2510. return hasChanged;
  2511. }
  2512. static char *get_start_exe_path(void)
  2513. {
  2514. static const WCHAR startW[] = {'\\','c','o','m','m','a','n','d',
  2515. '\\','s','t','a','r','t','.','e','x','e',0};
  2516. WCHAR start_path[MAX_PATH];
  2517. GetWindowsDirectoryW(start_path, MAX_PATH);
  2518. lstrcatW(start_path, startW);
  2519. return escape(start_path);
  2520. }
  2521. static char* escape_unix_link_arg(LPCSTR unix_link)
  2522. {
  2523. char *ret = NULL;
  2524. WCHAR *unix_linkW = utf8_chars_to_wchars(unix_link);
  2525. if (unix_linkW)
  2526. {
  2527. char *escaped_lnk = escape(unix_linkW);
  2528. if (escaped_lnk)
  2529. {
  2530. ret = heap_printf("/Unix %s", escaped_lnk);
  2531. HeapFree(GetProcessHeap(), 0, escaped_lnk);
  2532. }
  2533. HeapFree(GetProcessHeap(), 0, unix_linkW);
  2534. }
  2535. return ret;
  2536. }
  2537. static BOOL InvokeShellLinker( IShellLinkW *sl, LPCWSTR link, BOOL bWait )
  2538. {
  2539. static const WCHAR startW[] = {'\\','c','o','m','m','a','n','d',
  2540. '\\','s','t','a','r','t','.','e','x','e',0};
  2541. char *link_name = NULL, *icon_name = NULL, *work_dir = NULL;
  2542. char *escaped_path = NULL, *escaped_args = NULL, *description = NULL;
  2543. char *wmclass = NULL;
  2544. WCHAR szTmp[INFOTIPSIZE];
  2545. WCHAR szDescription[INFOTIPSIZE], szPath[MAX_PATH], szWorkDir[MAX_PATH];
  2546. WCHAR szArgs[INFOTIPSIZE], szIconPath[MAX_PATH], szWMClass[MAX_PATH];
  2547. int iIconId = 0, r = -1;
  2548. DWORD csidl = -1;
  2549. HANDLE hsem = NULL;
  2550. char *unix_link = NULL;
  2551. char *start_path = NULL;
  2552. if ( !link )
  2553. {
  2554. WINE_ERR("Link name is null\n");
  2555. return FALSE;
  2556. }
  2557. if( !GetLinkLocation( link, &csidl, &link_name ) )
  2558. {
  2559. WINE_WARN("Unknown link location %s. Ignoring.\n",wine_dbgstr_w(link));
  2560. return TRUE;
  2561. }
  2562. if (!in_desktop_dir(csidl) && !in_startmenu(csidl))
  2563. {
  2564. WINE_WARN("Not under desktop or start menu. Ignoring.\n");
  2565. return TRUE;
  2566. }
  2567. WINE_TRACE("Link : %s\n", wine_dbgstr_a(link_name));
  2568. szTmp[0] = 0;
  2569. IShellLinkW_GetWorkingDirectory( sl, szTmp, MAX_PATH );
  2570. ExpandEnvironmentStringsW(szTmp, szWorkDir, MAX_PATH);
  2571. WINE_TRACE("workdir : %s\n", wine_dbgstr_w(szWorkDir));
  2572. szTmp[0] = 0;
  2573. IShellLinkW_GetDescription( sl, szTmp, INFOTIPSIZE );
  2574. ExpandEnvironmentStringsW(szTmp, szDescription, INFOTIPSIZE);
  2575. WINE_TRACE("description: %s\n", wine_dbgstr_w(szDescription));
  2576. get_cmdline( sl, szTmp, MAX_PATH, szArgs, INFOTIPSIZE);
  2577. ExpandEnvironmentStringsW(szTmp, szPath, MAX_PATH);
  2578. WINE_TRACE("path : %s\n", wine_dbgstr_w(szPath));
  2579. WINE_TRACE("args : %s\n", wine_dbgstr_w(szArgs));
  2580. szTmp[0] = 0;
  2581. IShellLinkW_GetIconLocation( sl, szTmp, MAX_PATH, &iIconId );
  2582. ExpandEnvironmentStringsW(szTmp, szIconPath, MAX_PATH);
  2583. WINE_TRACE("icon file : %s\n", wine_dbgstr_w(szIconPath) );
  2584. szWMClass[0] = 0;
  2585. if( !szPath[0] )
  2586. {
  2587. LPITEMIDLIST pidl = NULL;
  2588. IShellLinkW_GetIDList( sl, &pidl );
  2589. if( pidl && SHGetPathFromIDListW( pidl, szPath ) )
  2590. WINE_TRACE("pidl path : %s\n", wine_dbgstr_w(szPath));
  2591. }
  2592. /* extract the icon */
  2593. if( szIconPath[0] )
  2594. icon_name = extract_icon( szIconPath , iIconId, NULL, bWait );
  2595. else
  2596. icon_name = extract_icon( szPath, iIconId, NULL, bWait );
  2597. /* fail - try once again after parent process exit */
  2598. if( !icon_name )
  2599. {
  2600. if (bWait)
  2601. {
  2602. WINE_WARN("Unable to extract icon, deferring.\n");
  2603. goto cleanup;
  2604. }
  2605. WINE_ERR("failed to extract icon from %s\n",
  2606. wine_dbgstr_w( szIconPath[0] ? szIconPath : szPath ));
  2607. }
  2608. unix_link = wine_get_unix_file_name(link);
  2609. if (unix_link == NULL)
  2610. {
  2611. WINE_WARN("couldn't find unix path of %s\n", wine_dbgstr_w(link));
  2612. goto cleanup;
  2613. }
  2614. /* check the path */
  2615. if( szPath[0] )
  2616. {
  2617. static const WCHAR exeW[] = {'.','e','x','e',0};
  2618. WCHAR *p;
  2619. /* check for .exe extension */
  2620. if (!(p = strrchrW( szPath, '.' )) ||
  2621. strchrW( p, '\\' ) || strchrW( p, '/' ) ||
  2622. lstrcmpiW( p, exeW ))
  2623. {
  2624. /* Not .exe - use 'start.exe' to launch this file */
  2625. p = szArgs + lstrlenW(szPath) + 2;
  2626. if (szArgs[0])
  2627. {
  2628. p[0] = ' ';
  2629. memmove( p+1, szArgs, min( (lstrlenW(szArgs) + 1) * sizeof(szArgs[0]),
  2630. sizeof(szArgs) - (p + 1 - szArgs) * sizeof(szArgs[0]) ) );
  2631. }
  2632. else
  2633. p[0] = 0;
  2634. szArgs[0] = '"';
  2635. lstrcpyW(szArgs + 1, szPath);
  2636. szArgs[lstrlenW(szArgs)] = '"';
  2637. GetWindowsDirectoryW(szPath, MAX_PATH);
  2638. lstrcatW(szPath, startW);
  2639. }
  2640. else
  2641. {
  2642. /* FIXME: Use AppUserModelID if present. */
  2643. WCHAR *p = PathFindFileNameW(szPath);
  2644. lstrcpyW(szWMClass, p);
  2645. CharLowerW(szWMClass);
  2646. }
  2647. /* convert app working dir */
  2648. if (szWorkDir[0])
  2649. work_dir = wine_get_unix_file_name( szWorkDir );
  2650. }
  2651. else
  2652. {
  2653. /* if there's no path... try run the link itself */
  2654. lstrcpynW(szArgs, link, MAX_PATH);
  2655. GetWindowsDirectoryW(szPath, MAX_PATH);
  2656. lstrcatW(szPath, startW);
  2657. }
  2658. /* escape the path and parameters */
  2659. escaped_path = escape(szPath);
  2660. escaped_args = escape(szArgs);
  2661. description = wchars_to_utf8_chars(szDescription);
  2662. wmclass = wchars_to_utf8_chars(szWMClass);
  2663. if (escaped_path == NULL || escaped_args == NULL || description == NULL || wmclass == NULL)
  2664. {
  2665. WINE_ERR("out of memory allocating/escaping parameters\n");
  2666. goto cleanup;
  2667. }
  2668. start_path = get_start_exe_path();
  2669. if (start_path == NULL)
  2670. {
  2671. WINE_ERR("out of memory\n");
  2672. goto cleanup;
  2673. }
  2674. /* building multiple menus concurrently has race conditions */
  2675. hsem = CreateSemaphoreA( NULL, 1, 1, "winemenubuilder_semaphore");
  2676. if( WAIT_OBJECT_0 != MsgWaitForMultipleObjects( 1, &hsem, FALSE, INFINITE, QS_ALLINPUT ) )
  2677. {
  2678. WINE_ERR("failed wait for semaphore\n");
  2679. goto cleanup;
  2680. }
  2681. if (in_desktop_dir(csidl))
  2682. {
  2683. char *location;
  2684. const char *lastEntry;
  2685. lastEntry = strrchr(link_name, '/');
  2686. if (lastEntry == NULL)
  2687. lastEntry = link_name;
  2688. else
  2689. ++lastEntry;
  2690. location = heap_printf("%s/%s.desktop", xdg_desktop_dir, lastEntry);
  2691. if (location)
  2692. {
  2693. if (csidl == CSIDL_COMMON_DESKTOPDIRECTORY)
  2694. {
  2695. char *link_arg = escape_unix_link_arg(unix_link);
  2696. if (link_arg)
  2697. {
  2698. r = !write_desktop_entry(unix_link, location, lastEntry,
  2699. start_path, link_arg, description, work_dir, icon_name, wmclass);
  2700. HeapFree(GetProcessHeap(), 0, link_arg);
  2701. }
  2702. }
  2703. else
  2704. r = !write_desktop_entry(NULL, location, lastEntry, escaped_path, escaped_args, description, work_dir, icon_name, wmclass);
  2705. if (r == 0)
  2706. chmod(location, 0755);
  2707. HeapFree(GetProcessHeap(), 0, location);
  2708. }
  2709. }
  2710. else
  2711. {
  2712. char *link_arg = escape_unix_link_arg(unix_link);
  2713. if (link_arg)
  2714. {
  2715. r = !write_menu_entry(unix_link, link_name, start_path, link_arg, description, work_dir, icon_name, wmclass);
  2716. HeapFree(GetProcessHeap(), 0, link_arg);
  2717. }
  2718. }
  2719. ReleaseSemaphore( hsem, 1, NULL );
  2720. cleanup:
  2721. if (hsem) CloseHandle( hsem );
  2722. HeapFree( GetProcessHeap(), 0, icon_name );
  2723. HeapFree( GetProcessHeap(), 0, work_dir );
  2724. HeapFree( GetProcessHeap(), 0, link_name );
  2725. HeapFree( GetProcessHeap(), 0, escaped_args );
  2726. HeapFree( GetProcessHeap(), 0, escaped_path );
  2727. HeapFree( GetProcessHeap(), 0, description );
  2728. HeapFree( GetProcessHeap(), 0, wmclass );
  2729. HeapFree( GetProcessHeap(), 0, unix_link );
  2730. HeapFree( GetProcessHeap(), 0, start_path );
  2731. if (r && !bWait)
  2732. WINE_ERR("failed to build the menu\n" );
  2733. return ( r == 0 );
  2734. }
  2735. static BOOL InvokeShellLinkerForURL( IUniformResourceLocatorW *url, LPCWSTR link, BOOL bWait )
  2736. {
  2737. char *link_name = NULL, *icon_name = NULL;
  2738. DWORD csidl = -1;
  2739. LPWSTR urlPath = NULL;
  2740. char *escaped_urlPath = NULL;
  2741. HRESULT hr;
  2742. HANDLE hSem = NULL;
  2743. BOOL ret = TRUE;
  2744. int r = -1;
  2745. char *unix_link = NULL;
  2746. IPropertySetStorage *pPropSetStg;
  2747. IPropertyStorage *pPropStg;
  2748. PROPSPEC ps[2];
  2749. PROPVARIANT pv[2];
  2750. char *start_path = NULL;
  2751. BOOL has_icon = FALSE;
  2752. if ( !link )
  2753. {
  2754. WINE_ERR("Link name is null\n");
  2755. return TRUE;
  2756. }
  2757. if( !GetLinkLocation( link, &csidl, &link_name ) )
  2758. {
  2759. WINE_WARN("Unknown link location %s. Ignoring.\n",wine_dbgstr_w(link));
  2760. return TRUE;
  2761. }
  2762. if (!in_desktop_dir(csidl) && !in_startmenu(csidl))
  2763. {
  2764. WINE_WARN("Not under desktop or start menu. Ignoring.\n");
  2765. ret = TRUE;
  2766. goto cleanup;
  2767. }
  2768. WINE_TRACE("Link : %s\n", wine_dbgstr_a(link_name));
  2769. hr = url->lpVtbl->GetURL(url, &urlPath);
  2770. if (FAILED(hr))
  2771. {
  2772. ret = TRUE;
  2773. goto cleanup;
  2774. }
  2775. WINE_TRACE("path : %s\n", wine_dbgstr_w(urlPath));
  2776. unix_link = wine_get_unix_file_name(link);
  2777. if (unix_link == NULL)
  2778. {
  2779. WINE_WARN("couldn't find unix path of %s\n", wine_dbgstr_w(link));
  2780. goto cleanup;
  2781. }
  2782. escaped_urlPath = escape(urlPath);
  2783. if (escaped_urlPath == NULL)
  2784. {
  2785. WINE_ERR("couldn't escape url, out of memory\n");
  2786. goto cleanup;
  2787. }
  2788. start_path = get_start_exe_path();
  2789. if (start_path == NULL)
  2790. {
  2791. WINE_ERR("out of memory\n");
  2792. goto cleanup;
  2793. }
  2794. ps[0].ulKind = PRSPEC_PROPID;
  2795. ps[0].u.propid = PID_IS_ICONFILE;
  2796. ps[1].ulKind = PRSPEC_PROPID;
  2797. ps[1].u.propid = PID_IS_ICONINDEX;
  2798. hr = url->lpVtbl->QueryInterface(url, &IID_IPropertySetStorage, (void **) &pPropSetStg);
  2799. if (SUCCEEDED(hr))
  2800. {
  2801. hr = IPropertySetStorage_Open(pPropSetStg, &FMTID_Intshcut, STGM_READ | STGM_SHARE_EXCLUSIVE, &pPropStg);
  2802. if (SUCCEEDED(hr))
  2803. {
  2804. hr = IPropertyStorage_ReadMultiple(pPropStg, 2, ps, pv);
  2805. if (SUCCEEDED(hr))
  2806. {
  2807. if (pv[0].vt == VT_LPWSTR && pv[0].pwszVal && pv[0].pwszVal[0])
  2808. {
  2809. has_icon = TRUE;
  2810. icon_name = extract_icon( pv[0].pwszVal, pv[1].iVal, NULL, bWait );
  2811. WINE_TRACE("URL icon path: %s icon index: %d icon name: %s\n", wine_dbgstr_w(pv[0].pwszVal), pv[1].iVal, icon_name);
  2812. }
  2813. PropVariantClear(&pv[0]);
  2814. PropVariantClear(&pv[1]);
  2815. }
  2816. IPropertyStorage_Release(pPropStg);
  2817. }
  2818. IPropertySetStorage_Release(pPropSetStg);
  2819. }
  2820. /* fail - try once again after parent process exit */
  2821. if( has_icon && !icon_name )
  2822. {
  2823. if (bWait)
  2824. {
  2825. WINE_WARN("Unable to extract icon, deferring.\n");
  2826. ret = FALSE;
  2827. goto cleanup;
  2828. }
  2829. WINE_ERR("failed to extract icon from %s\n",
  2830. wine_dbgstr_w( pv[0].pwszVal ));
  2831. }
  2832. hSem = CreateSemaphoreA( NULL, 1, 1, "winemenubuilder_semaphore");
  2833. if( WAIT_OBJECT_0 != MsgWaitForMultipleObjects( 1, &hSem, FALSE, INFINITE, QS_ALLINPUT ) )
  2834. {
  2835. WINE_ERR("failed wait for semaphore\n");
  2836. goto cleanup;
  2837. }
  2838. if (in_desktop_dir(csidl))
  2839. {
  2840. char *location;
  2841. const char *lastEntry;
  2842. lastEntry = strrchr(link_name, '/');
  2843. if (lastEntry == NULL)
  2844. lastEntry = link_name;
  2845. else
  2846. ++lastEntry;
  2847. location = heap_printf("%s/%s.desktop", xdg_desktop_dir, lastEntry);
  2848. if (location)
  2849. {
  2850. r = !write_desktop_entry(NULL, location, lastEntry, start_path, escaped_urlPath, NULL, NULL, icon_name, NULL);
  2851. if (r == 0)
  2852. chmod(location, 0755);
  2853. HeapFree(GetProcessHeap(), 0, location);
  2854. }
  2855. }
  2856. else
  2857. r = !write_menu_entry(unix_link, link_name, start_path, escaped_urlPath, NULL, NULL, icon_name, NULL);
  2858. ret = (r == 0);
  2859. ReleaseSemaphore(hSem, 1, NULL);
  2860. cleanup:
  2861. if (hSem)
  2862. CloseHandle(hSem);
  2863. HeapFree( GetProcessHeap(), 0, icon_name );
  2864. HeapFree(GetProcessHeap(), 0, link_name);
  2865. CoTaskMemFree( urlPath );
  2866. HeapFree(GetProcessHeap(), 0, escaped_urlPath);
  2867. HeapFree(GetProcessHeap(), 0, unix_link);
  2868. return ret;
  2869. }
  2870. static BOOL WaitForParentProcess( void )
  2871. {
  2872. PROCESSENTRY32 procentry;
  2873. HANDLE hsnapshot = NULL, hprocess = NULL;
  2874. DWORD ourpid = GetCurrentProcessId();
  2875. BOOL ret = FALSE, rc;
  2876. WINE_TRACE("Waiting for parent process\n");
  2877. if ((hsnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 )) ==
  2878. INVALID_HANDLE_VALUE)
  2879. {
  2880. WINE_ERR("CreateToolhelp32Snapshot failed, error %d\n", GetLastError());
  2881. goto done;
  2882. }
  2883. procentry.dwSize = sizeof(PROCESSENTRY32);
  2884. rc = Process32First( hsnapshot, &procentry );
  2885. while (rc)
  2886. {
  2887. if (procentry.th32ProcessID == ourpid) break;
  2888. rc = Process32Next( hsnapshot, &procentry );
  2889. }
  2890. if (!rc)
  2891. {
  2892. WINE_WARN("Unable to find current process id %d when listing processes\n", ourpid);
  2893. goto done;
  2894. }
  2895. if ((hprocess = OpenProcess( SYNCHRONIZE, FALSE, procentry.th32ParentProcessID )) ==
  2896. NULL)
  2897. {
  2898. WINE_WARN("OpenProcess failed pid=%d, error %d\n", procentry.th32ParentProcessID,
  2899. GetLastError());
  2900. goto done;
  2901. }
  2902. if (MsgWaitForMultipleObjects( 1, &hprocess, FALSE, INFINITE, QS_ALLINPUT ) == WAIT_OBJECT_0)
  2903. ret = TRUE;
  2904. else
  2905. WINE_ERR("Unable to wait for parent process, error %d\n", GetLastError());
  2906. done:
  2907. if (hprocess) CloseHandle( hprocess );
  2908. if (hsnapshot) CloseHandle( hsnapshot );
  2909. return ret;
  2910. }
  2911. static BOOL Process_Link( LPCWSTR linkname, BOOL bWait )
  2912. {
  2913. IShellLinkW *sl;
  2914. IPersistFile *pf;
  2915. HRESULT r;
  2916. WCHAR fullname[MAX_PATH];
  2917. DWORD len;
  2918. WINE_TRACE("%s, wait %d\n", wine_dbgstr_w(linkname), bWait);
  2919. if( !linkname[0] )
  2920. {
  2921. WINE_ERR("link name missing\n");
  2922. return FALSE;
  2923. }
  2924. len=GetFullPathNameW( linkname, MAX_PATH, fullname, NULL );
  2925. if (len==0 || len>MAX_PATH)
  2926. {
  2927. WINE_ERR("couldn't get full path of link file\n");
  2928. return FALSE;
  2929. }
  2930. r = CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
  2931. &IID_IShellLinkW, (LPVOID *) &sl );
  2932. if( FAILED( r ) )
  2933. {
  2934. WINE_ERR("No IID_IShellLink\n");
  2935. return FALSE;
  2936. }
  2937. r = IShellLinkW_QueryInterface( sl, &IID_IPersistFile, (LPVOID*) &pf );
  2938. if( FAILED( r ) )
  2939. {
  2940. WINE_ERR("No IID_IPersistFile\n");
  2941. return FALSE;
  2942. }
  2943. r = IPersistFile_Load( pf, fullname, STGM_READ );
  2944. if( SUCCEEDED( r ) )
  2945. {
  2946. /* If something fails (eg. Couldn't extract icon)
  2947. * wait for parent process and try again
  2948. */
  2949. if( ! InvokeShellLinker( sl, fullname, bWait ) && bWait )
  2950. {
  2951. WaitForParentProcess();
  2952. InvokeShellLinker( sl, fullname, FALSE );
  2953. }
  2954. }
  2955. else
  2956. {
  2957. WINE_ERR("unable to load %s\n", wine_dbgstr_w(linkname));
  2958. }
  2959. IPersistFile_Release( pf );
  2960. IShellLinkW_Release( sl );
  2961. return !r;
  2962. }
  2963. static BOOL Process_URL( LPCWSTR urlname, BOOL bWait )
  2964. {
  2965. IUniformResourceLocatorW *url;
  2966. IPersistFile *pf;
  2967. HRESULT r;
  2968. WCHAR fullname[MAX_PATH];
  2969. DWORD len;
  2970. WINE_TRACE("%s, wait %d\n", wine_dbgstr_w(urlname), bWait);
  2971. if( !urlname[0] )
  2972. {
  2973. WINE_ERR("URL name missing\n");
  2974. return FALSE;
  2975. }
  2976. len=GetFullPathNameW( urlname, MAX_PATH, fullname, NULL );
  2977. if (len==0 || len>MAX_PATH)
  2978. {
  2979. WINE_ERR("couldn't get full path of URL file\n");
  2980. return FALSE;
  2981. }
  2982. r = CoCreateInstance( &CLSID_InternetShortcut, NULL, CLSCTX_INPROC_SERVER,
  2983. &IID_IUniformResourceLocatorW, (LPVOID *) &url );
  2984. if( FAILED( r ) )
  2985. {
  2986. WINE_ERR("No IID_IUniformResourceLocatorW\n");
  2987. return FALSE;
  2988. }
  2989. r = url->lpVtbl->QueryInterface( url, &IID_IPersistFile, (LPVOID*) &pf );
  2990. if( FAILED( r ) )
  2991. {
  2992. WINE_ERR("No IID_IPersistFile\n");
  2993. return FALSE;
  2994. }
  2995. r = IPersistFile_Load( pf, fullname, STGM_READ );
  2996. if( SUCCEEDED( r ) )
  2997. {
  2998. /* If something fails (eg. Couldn't extract icon)
  2999. * wait for parent process and try again
  3000. */
  3001. if( ! InvokeShellLinkerForURL( url, fullname, bWait ) && bWait )
  3002. {
  3003. WaitForParentProcess();
  3004. InvokeShellLinkerForURL( url, fullname, FALSE );
  3005. }
  3006. }
  3007. IPersistFile_Release( pf );
  3008. url->lpVtbl->Release( url );
  3009. return !r;
  3010. }
  3011. static void RefreshFileTypeAssociations(void)
  3012. {
  3013. HANDLE hSem = NULL;
  3014. char *mime_dir = NULL;
  3015. char *packages_dir = NULL;
  3016. char *applications_dir = NULL;
  3017. BOOL hasChanged;
  3018. hSem = CreateSemaphoreA( NULL, 1, 1, "winemenubuilder_semaphore");
  3019. if( WAIT_OBJECT_0 != MsgWaitForMultipleObjects( 1, &hSem, FALSE, INFINITE, QS_ALLINPUT ) )
  3020. {
  3021. WINE_ERR("failed wait for semaphore\n");
  3022. CloseHandle(hSem);
  3023. hSem = NULL;
  3024. goto end;
  3025. }
  3026. mime_dir = heap_printf("%s/mime", xdg_data_dir);
  3027. if (mime_dir == NULL)
  3028. {
  3029. WINE_ERR("out of memory\n");
  3030. goto end;
  3031. }
  3032. create_directories(mime_dir);
  3033. packages_dir = heap_printf("%s/packages", mime_dir);
  3034. if (packages_dir == NULL)
  3035. {
  3036. WINE_ERR("out of memory\n");
  3037. goto end;
  3038. }
  3039. create_directories(packages_dir);
  3040. applications_dir = heap_printf("%s/applications", xdg_data_dir);
  3041. if (applications_dir == NULL)
  3042. {
  3043. WINE_ERR("out of memory\n");
  3044. goto end;
  3045. }
  3046. create_directories(applications_dir);
  3047. hasChanged = generate_associations(xdg_data_dir, packages_dir, applications_dir);
  3048. hasChanged |= cleanup_associations();
  3049. if (hasChanged)
  3050. {
  3051. const char *argv[3];
  3052. argv[0] = "update-mime-database";
  3053. argv[1] = mime_dir;
  3054. argv[2] = NULL;
  3055. _spawnvp( _P_DETACH, argv[0], argv );
  3056. argv[0] = "update-desktop-database";
  3057. argv[1] = applications_dir;
  3058. _spawnvp( _P_DETACH, argv[0], argv );
  3059. }
  3060. end:
  3061. if (hSem)
  3062. {
  3063. ReleaseSemaphore(hSem, 1, NULL);
  3064. CloseHandle(hSem);
  3065. }
  3066. HeapFree(GetProcessHeap(), 0, mime_dir);
  3067. HeapFree(GetProcessHeap(), 0, packages_dir);
  3068. HeapFree(GetProcessHeap(), 0, applications_dir);
  3069. }
  3070. static void cleanup_menus(void)
  3071. {
  3072. HKEY hkey;
  3073. hkey = open_menus_reg_key();
  3074. if (hkey)
  3075. {
  3076. int i;
  3077. LSTATUS lret = ERROR_SUCCESS;
  3078. for (i = 0; lret == ERROR_SUCCESS; )
  3079. {
  3080. WCHAR *value = NULL;
  3081. WCHAR *data = NULL;
  3082. DWORD valueSize = 4096;
  3083. DWORD dataSize = 4096;
  3084. while (1)
  3085. {
  3086. lret = ERROR_OUTOFMEMORY;
  3087. value = HeapAlloc(GetProcessHeap(), 0, valueSize * sizeof(WCHAR));
  3088. if (value == NULL)
  3089. break;
  3090. data = HeapAlloc(GetProcessHeap(), 0, dataSize * sizeof(WCHAR));
  3091. if (data == NULL)
  3092. break;
  3093. lret = RegEnumValueW(hkey, i, value, &valueSize, NULL, NULL, (BYTE*)data, &dataSize);
  3094. if (lret != ERROR_MORE_DATA)
  3095. break;
  3096. valueSize *= 2;
  3097. dataSize *= 2;
  3098. HeapFree(GetProcessHeap(), 0, value);
  3099. HeapFree(GetProcessHeap(), 0, data);
  3100. value = data = NULL;
  3101. }
  3102. if (lret == ERROR_SUCCESS)
  3103. {
  3104. char *unix_file;
  3105. char *windows_file;
  3106. unix_file = wchars_to_unix_chars(value);
  3107. windows_file = wchars_to_unix_chars(data);
  3108. if (unix_file != NULL && windows_file != NULL)
  3109. {
  3110. struct stat filestats;
  3111. if (stat(windows_file, &filestats) < 0 && errno == ENOENT)
  3112. {
  3113. WINE_TRACE("removing menu related file %s\n", unix_file);
  3114. remove(unix_file);
  3115. RegDeleteValueW(hkey, value);
  3116. }
  3117. else
  3118. i++;
  3119. }
  3120. else
  3121. {
  3122. WINE_ERR("out of memory enumerating menus\n");
  3123. lret = ERROR_OUTOFMEMORY;
  3124. }
  3125. HeapFree(GetProcessHeap(), 0, unix_file);
  3126. HeapFree(GetProcessHeap(), 0, windows_file);
  3127. }
  3128. else if (lret != ERROR_NO_MORE_ITEMS)
  3129. WINE_ERR("error %d reading registry\n", lret);
  3130. HeapFree(GetProcessHeap(), 0, value);
  3131. HeapFree(GetProcessHeap(), 0, data);
  3132. }
  3133. RegCloseKey(hkey);
  3134. }
  3135. else
  3136. WINE_ERR("error opening registry key, menu cleanup failed\n");
  3137. }
  3138. static void thumbnail_lnk(LPCWSTR lnkPath, LPCWSTR outputPath)
  3139. {
  3140. char *utf8lnkPath = NULL;
  3141. char *utf8OutputPath = NULL;
  3142. WCHAR *winLnkPath = NULL;
  3143. IShellLinkW *shellLink = NULL;
  3144. IPersistFile *persistFile = NULL;
  3145. WCHAR szTmp[MAX_PATH];
  3146. WCHAR szPath[MAX_PATH];
  3147. WCHAR szArgs[INFOTIPSIZE];
  3148. WCHAR szIconPath[MAX_PATH];
  3149. int iconId;
  3150. IStream *stream = NULL;
  3151. ICONDIRENTRY *pIconDirEntries = NULL;
  3152. int numEntries;
  3153. HRESULT hr;
  3154. utf8lnkPath = wchars_to_utf8_chars(lnkPath);
  3155. if (utf8lnkPath == NULL)
  3156. {
  3157. WINE_ERR("out of memory converting paths\n");
  3158. goto end;
  3159. }
  3160. utf8OutputPath = wchars_to_utf8_chars(outputPath);
  3161. if (utf8OutputPath == NULL)
  3162. {
  3163. WINE_ERR("out of memory converting paths\n");
  3164. goto end;
  3165. }
  3166. winLnkPath = wine_get_dos_file_name(utf8lnkPath);
  3167. if (winLnkPath == NULL)
  3168. {
  3169. WINE_ERR("could not convert %s to DOS path\n", utf8lnkPath);
  3170. goto end;
  3171. }
  3172. hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
  3173. &IID_IShellLinkW, (LPVOID*)&shellLink);
  3174. if (FAILED(hr))
  3175. {
  3176. WINE_ERR("could not create IShellLinkW, error 0x%08X\n", hr);
  3177. goto end;
  3178. }
  3179. hr = IShellLinkW_QueryInterface(shellLink, &IID_IPersistFile, (LPVOID)&persistFile);
  3180. if (FAILED(hr))
  3181. {
  3182. WINE_ERR("could not query IPersistFile, error 0x%08X\n", hr);
  3183. goto end;
  3184. }
  3185. hr = IPersistFile_Load(persistFile, winLnkPath, STGM_READ);
  3186. if (FAILED(hr))
  3187. {
  3188. WINE_ERR("could not read .lnk, error 0x%08X\n", hr);
  3189. goto end;
  3190. }
  3191. get_cmdline(shellLink, szTmp, MAX_PATH, szArgs, INFOTIPSIZE);
  3192. ExpandEnvironmentStringsW(szTmp, szPath, MAX_PATH);
  3193. szTmp[0] = 0;
  3194. IShellLinkW_GetIconLocation(shellLink, szTmp, MAX_PATH, &iconId);
  3195. ExpandEnvironmentStringsW(szTmp, szIconPath, MAX_PATH);
  3196. if(!szPath[0])
  3197. {
  3198. LPITEMIDLIST pidl = NULL;
  3199. IShellLinkW_GetIDList(shellLink, &pidl);
  3200. if (pidl && SHGetPathFromIDListW(pidl, szPath))
  3201. WINE_TRACE("pidl path : %s\n", wine_dbgstr_w(szPath));
  3202. }
  3203. if (szIconPath[0])
  3204. {
  3205. hr = open_icon(szIconPath, iconId, FALSE, &stream, &pIconDirEntries, &numEntries);
  3206. if (SUCCEEDED(hr))
  3207. hr = write_native_icon(stream, pIconDirEntries, numEntries, utf8OutputPath, NULL);
  3208. }
  3209. else
  3210. {
  3211. hr = open_icon(szPath, iconId, FALSE, &stream, &pIconDirEntries, &numEntries);
  3212. if (SUCCEEDED(hr))
  3213. hr = write_native_icon(stream, pIconDirEntries, numEntries, utf8OutputPath, NULL);
  3214. }
  3215. end:
  3216. HeapFree(GetProcessHeap(), 0, utf8lnkPath);
  3217. HeapFree(GetProcessHeap(), 0, utf8OutputPath);
  3218. HeapFree(GetProcessHeap(), 0, winLnkPath);
  3219. if (shellLink != NULL)
  3220. IShellLinkW_Release(shellLink);
  3221. if (persistFile != NULL)
  3222. IPersistFile_Release(persistFile);
  3223. if (stream != NULL)
  3224. IStream_Release(stream);
  3225. HeapFree(GetProcessHeap(), 0, pIconDirEntries);
  3226. }
  3227. static WCHAR *next_token( LPWSTR *p )
  3228. {
  3229. LPWSTR token = NULL, t = *p;
  3230. if( !t )
  3231. return NULL;
  3232. while( t && !token )
  3233. {
  3234. switch( *t )
  3235. {
  3236. case ' ':
  3237. t++;
  3238. continue;
  3239. case '"':
  3240. /* unquote the token */
  3241. token = ++t;
  3242. t = strchrW( token, '"' );
  3243. if( t )
  3244. *t++ = 0;
  3245. break;
  3246. case 0:
  3247. t = NULL;
  3248. break;
  3249. default:
  3250. token = t;
  3251. t = strchrW( token, ' ' );
  3252. if( t )
  3253. *t++ = 0;
  3254. break;
  3255. }
  3256. }
  3257. *p = t;
  3258. return token;
  3259. }
  3260. static BOOL init_xdg(void)
  3261. {
  3262. WCHAR shellDesktopPath[MAX_PATH];
  3263. HRESULT hr = SHGetFolderPathW(NULL, CSIDL_DESKTOP, NULL, SHGFP_TYPE_CURRENT, shellDesktopPath);
  3264. if (SUCCEEDED(hr))
  3265. xdg_desktop_dir = wine_get_unix_file_name(shellDesktopPath);
  3266. if (xdg_desktop_dir == NULL)
  3267. {
  3268. WINE_ERR("error looking up the desktop directory\n");
  3269. return FALSE;
  3270. }
  3271. if (getenv("XDG_CONFIG_HOME"))
  3272. xdg_config_dir = heap_printf("%s/menus/applications-merged", getenv("XDG_CONFIG_HOME"));
  3273. else
  3274. xdg_config_dir = heap_printf("%s/.config/menus/applications-merged", getenv("HOME"));
  3275. if (xdg_config_dir)
  3276. {
  3277. create_directories(xdg_config_dir);
  3278. if (getenv("XDG_DATA_HOME"))
  3279. xdg_data_dir = strdupA(getenv("XDG_DATA_HOME"));
  3280. else
  3281. xdg_data_dir = heap_printf("%s/.local/share", getenv("HOME"));
  3282. if (xdg_data_dir)
  3283. {
  3284. char *buffer;
  3285. create_directories(xdg_data_dir);
  3286. buffer = heap_printf("%s/desktop-directories", xdg_data_dir);
  3287. if (buffer)
  3288. {
  3289. mkdir(buffer, 0777);
  3290. HeapFree(GetProcessHeap(), 0, buffer);
  3291. }
  3292. return TRUE;
  3293. }
  3294. HeapFree(GetProcessHeap(), 0, xdg_config_dir);
  3295. }
  3296. WINE_ERR("out of memory\n");
  3297. return FALSE;
  3298. }
  3299. static BOOL associations_enabled(void)
  3300. {
  3301. BOOL ret = TRUE;
  3302. HKEY hkey;
  3303. BYTE buf[32];
  3304. DWORD len;
  3305. if ((hkey = open_associations_reg_key()))
  3306. {
  3307. len = sizeof(buf);
  3308. if (!RegQueryValueExA(hkey, "Enable", NULL, NULL, buf, &len))
  3309. ret = IS_OPTION_TRUE(buf[0]);
  3310. RegCloseKey( hkey );
  3311. }
  3312. return ret;
  3313. }
  3314. /***********************************************************************
  3315. *
  3316. * wWinMain
  3317. */
  3318. int PASCAL wWinMain (HINSTANCE hInstance, HINSTANCE prev, LPWSTR cmdline, int show)
  3319. {
  3320. static const WCHAR dash_aW[] = {'-','a',0};
  3321. static const WCHAR dash_rW[] = {'-','r',0};
  3322. static const WCHAR dash_tW[] = {'-','t',0};
  3323. static const WCHAR dash_uW[] = {'-','u',0};
  3324. static const WCHAR dash_wW[] = {'-','w',0};
  3325. LPWSTR token = NULL, p;
  3326. BOOL bWait = FALSE;
  3327. BOOL bURL = FALSE;
  3328. HRESULT hr;
  3329. int ret = 0;
  3330. if (!init_xdg())
  3331. return 1;
  3332. hr = CoInitialize(NULL);
  3333. if (FAILED(hr))
  3334. {
  3335. WINE_ERR("could not initialize COM, error 0x%08X\n", hr);
  3336. return 1;
  3337. }
  3338. for( p = cmdline; p && *p; )
  3339. {
  3340. token = next_token( &p );
  3341. if( !token )
  3342. break;
  3343. if( !strcmpW( token, dash_aW ) )
  3344. {
  3345. if (associations_enabled())
  3346. RefreshFileTypeAssociations();
  3347. continue;
  3348. }
  3349. if( !strcmpW( token, dash_rW ) )
  3350. {
  3351. cleanup_menus();
  3352. continue;
  3353. }
  3354. if( !strcmpW( token, dash_wW ) )
  3355. bWait = TRUE;
  3356. else if ( !strcmpW( token, dash_uW ) )
  3357. bURL = TRUE;
  3358. else if ( !strcmpW( token, dash_tW ) )
  3359. {
  3360. WCHAR *lnkFile = next_token( &p );
  3361. if (lnkFile)
  3362. {
  3363. WCHAR *outputFile = next_token( &p );
  3364. if (outputFile)
  3365. thumbnail_lnk(lnkFile, outputFile);
  3366. }
  3367. }
  3368. else if( token[0] == '-' )
  3369. {
  3370. WINE_ERR( "unknown option %s\n", wine_dbgstr_w(token) );
  3371. }
  3372. else
  3373. {
  3374. BOOL bRet;
  3375. if (bURL)
  3376. bRet = Process_URL( token, bWait );
  3377. else
  3378. bRet = Process_Link( token, bWait );
  3379. if (!bRet)
  3380. {
  3381. WINE_ERR( "failed to build menu item for %s\n", wine_dbgstr_w(token) );
  3382. ret = 1;
  3383. }
  3384. }
  3385. }
  3386. CoUninitialize();
  3387. return ret;
  3388. }