Fix CSIDL_CONTROLS and CSIDL_PRINTERS PIDL types, and update tests now
[wine] / dlls / shell32 / debughlp.c
1 /*
2  * Helper functions for debugging
3  *
4  * Copyright 1998, 2002 Juergen Schmied
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include "windef.h"
26 #include "wingdi.h"
27 #include "pidl.h"
28 #include "shlguid.h"
29 #include "shldisp.h"
30 #include "wine/debug.h"
31 #include "debughlp.h"
32 #include "docobj.h"
33 #include "shell32_main.h"
34
35
36 WINE_DEFAULT_DEBUG_CHANNEL(pidl);
37
38 static
39 LPITEMIDLIST _dbg_ILGetNext(LPCITEMIDLIST pidl)
40 {
41         WORD len;
42
43         if(pidl)
44         {
45           len =  pidl->mkid.cb;
46           if (len)
47           {
48             return (LPITEMIDLIST) (((LPBYTE)pidl)+len);
49           }
50         }
51         return NULL;
52 }
53
54 static
55 BOOL _dbg_ILIsDesktop(LPCITEMIDLIST pidl)
56 {
57         return ( !pidl || (pidl && pidl->mkid.cb == 0x00) );
58 }
59
60 static
61 LPPIDLDATA _dbg_ILGetDataPointer(LPCITEMIDLIST pidl)
62 {
63         if(pidl && pidl->mkid.cb != 0x00)
64           return (LPPIDLDATA) &(pidl->mkid.abID);
65         return NULL;
66 }
67
68 static
69 LPSTR _dbg_ILGetTextPointer(LPCITEMIDLIST pidl)
70 {
71         LPPIDLDATA pdata =_dbg_ILGetDataPointer(pidl);
72
73         if (pdata)
74         {
75           switch (pdata->type)
76           {
77             case PT_GUID:
78             case PT_SHELLEXT:
79             case PT_YAGUID:
80               return NULL;
81
82             case PT_DRIVE:
83             case PT_DRIVE1:
84             case PT_DRIVE2:
85             case PT_DRIVE3:
86               return (LPSTR)&(pdata->u.drive.szDriveName);
87
88             case PT_FOLDER:
89             case PT_FOLDER1:
90             case PT_VALUE:
91             case PT_IESPECIAL1:
92             case PT_IESPECIAL2:
93               return (LPSTR)&(pdata->u.file.szNames);
94
95             case PT_WORKGRP:
96             case PT_COMP:
97             case PT_NETWORK:
98             case PT_NETPROVIDER:
99             case PT_SHARE:
100               return (LPSTR)&(pdata->u.network.szNames);
101           }
102         }
103         return NULL;
104 }
105
106 static
107 LPSTR _dbg_ILGetSTextPointer(LPCITEMIDLIST pidl)
108 {
109         LPPIDLDATA pdata =_dbg_ILGetDataPointer(pidl);
110
111         if (pdata)
112         {
113           switch (pdata->type)
114           {
115             case PT_FOLDER:
116             case PT_VALUE:
117             case PT_IESPECIAL1:
118             case PT_IESPECIAL2:
119               return (LPSTR)(pdata->u.file.szNames + strlen (pdata->u.file.szNames) + 1);
120
121             case PT_WORKGRP:
122               return (LPSTR)(pdata->u.network.szNames + strlen (pdata->u.network.szNames) + 1);
123           }
124         }
125         return NULL;
126 }
127
128 static
129 IID* _dbg_ILGetGUIDPointer(LPCITEMIDLIST pidl)
130 {
131         LPPIDLDATA pdata =_ILGetDataPointer(pidl);
132
133         if (pdata)
134         {
135           switch (pdata->type)
136           {
137             case PT_SHELLEXT:
138             case PT_GUID:
139               return &(pdata->u.guid.guid);
140           }
141         }
142         return NULL;
143 }
144
145 static
146 DWORD _dbg_ILSimpleGetText (LPCITEMIDLIST pidl, LPSTR szOut, UINT uOutSize)
147 {
148         DWORD           dwReturn=0;
149         LPSTR           szSrc;
150         GUID const *    riid;
151         char szTemp[MAX_PATH];
152
153         if (!pidl) return 0;
154
155         if (szOut)
156           *szOut = 0;
157
158         if (_dbg_ILIsDesktop(pidl))
159         {
160          /* desktop */
161           if (szOut) strncpy(szOut, "Desktop", uOutSize);
162           dwReturn = strlen ("Desktop");
163         }
164         else if (( szSrc = _dbg_ILGetTextPointer(pidl) ))
165         {
166           /* filesystem */
167           if (szOut) strncpy(szOut, szSrc, uOutSize);
168           dwReturn = strlen(szSrc);
169         }
170         else if (( riid = _dbg_ILGetGUIDPointer(pidl) ))
171         {
172           if (szOut)
173             sprintf( szOut, "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
174                  riid->Data1, riid->Data2, riid->Data3,
175                  riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
176                  riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7] );
177           dwReturn = strlen (szTemp);
178         }
179         return dwReturn;
180 }
181
182
183
184
185 void pdump (LPCITEMIDLIST pidl)
186 {
187         LPCITEMIDLIST pidltemp = pidl;
188
189         if (!TRACE_ON(pidl)) return;
190
191         if (! pidltemp)
192         {
193           MESSAGE ("-------- pidl=NULL (Desktop)\n");
194         }
195         else
196         {
197           MESSAGE ("-------- pidl=%p\n", pidl);
198           if (pidltemp->mkid.cb)
199           {
200             do
201             {
202               DWORD dwAttrib = 0;
203               LPPIDLDATA pData   = _dbg_ILGetDataPointer(pidltemp);
204               DWORD type         = pData->type;
205               LPSTR szLongName   = _dbg_ILGetTextPointer(pidltemp);
206               LPSTR szShortName  = _dbg_ILGetSTextPointer(pidltemp);
207               char szName[MAX_PATH];
208
209               _dbg_ILSimpleGetText(pidltemp, szName, MAX_PATH);
210               if( PT_FOLDER == type || PT_VALUE == type)
211                 dwAttrib = pData->u.file.uFileAttribs;
212
213               MESSAGE ("[%p] size=%04u type=%lx attr=0x%08lx name=\"%s\" (%s,%s)\n",
214                        pidltemp, pidltemp->mkid.cb,type,dwAttrib,szName,debugstr_a(szLongName), debugstr_a(szShortName));
215
216               pidltemp = _dbg_ILGetNext(pidltemp);
217
218             } while (pidltemp->mkid.cb);
219           }
220           else
221           {
222             MESSAGE ("empty pidl (Desktop)\n");
223           }
224           pcheck(pidl);
225         }
226 }
227 #define BYTES_PRINTED 32
228 BOOL pcheck (LPCITEMIDLIST pidl)
229 {
230         DWORD type, ret=TRUE;
231         LPCITEMIDLIST pidltemp = pidl;
232
233         if (pidltemp && pidltemp->mkid.cb)
234         { do
235           { type   = _dbg_ILGetDataPointer(pidltemp)->type;
236             switch (type)
237             { case PT_CPLAPPLET:
238               case PT_GUID:
239               case PT_SHELLEXT:
240               case PT_DRIVE:
241               case PT_DRIVE1:
242               case PT_DRIVE2:
243               case PT_DRIVE3:
244               case PT_FOLDER:
245               case PT_VALUE:
246               case PT_FOLDER1:
247               case PT_WORKGRP:
248               case PT_COMP:
249               case PT_NETPROVIDER:
250               case PT_NETWORK:
251               case PT_IESPECIAL1:
252               case PT_YAGUID:
253               case PT_IESPECIAL2:
254               case PT_SHARE:
255                 break;
256               default:
257               {
258                 char szTemp[BYTES_PRINTED*4 + 1];
259                 int i;
260                 unsigned char c;
261
262                 memset(szTemp, ' ', BYTES_PRINTED*4 + 1);
263                 for ( i = 0; (i<pidltemp->mkid.cb) && (i<BYTES_PRINTED); i++)
264                 {
265                   c = ((unsigned char *)pidltemp)[i];
266
267                   szTemp[i*3+0] = ((c>>4)>9)? (c>>4)+55 : (c>>4)+48;
268                   szTemp[i*3+1] = ((0x0F&c)>9)? (0x0F&c)+55 : (0x0F&c)+48;
269                   szTemp[i*3+2] = ' ';
270                   szTemp[i+BYTES_PRINTED*3]  =  (c>=0x20 && c <=0x80) ? c : '.';
271                 }
272                 szTemp[BYTES_PRINTED*4] = 0x00;
273                 ERR("unknown IDLIST %p [%p] size=%u type=%lx\n%s\n",pidl, pidltemp, pidltemp->mkid.cb,type, szTemp);
274                 ret = FALSE;
275               }
276             }
277             pidltemp = _dbg_ILGetNext(pidltemp);
278           } while (pidltemp->mkid.cb);
279         }
280         return ret;
281 }
282
283 static char shdebugstr_buf1[100];
284 static char shdebugstr_buf2[100];
285 static char * shdebugstr_buf = shdebugstr_buf1;
286
287 static struct {
288         REFIID  riid;
289         char    *name;
290 } InterfaceDesc[] = {
291         {&IID_IUnknown,                 "IID_IUnknown"},
292         {&IID_IClassFactory,            "IID_IClassFactory"},
293         {&IID_IShellView,               "IID_IShellView"},
294         {&IID_IOleCommandTarget,        "IID_IOleCommandTarget"},
295         {&IID_IDropTarget,              "IID_IDropTarget"},
296         {&IID_IDropSource,              "IID_IDropSource"},
297         {&IID_IViewObject,              "IID_IViewObject"},
298         {&IID_IContextMenu,             "IID_IContextMenu"},
299         {&IID_IShellExtInit,            "IID_IShellExtInit"},
300         {&IID_IShellFolder,             "IID_IShellFolder"},
301         {&IID_IShellFolder2,            "IID_IShellFolder2"},
302         {&IID_IPersist,                 "IID_IPersist"},
303         {&IID_IPersistFolder,           "IID_IPersistFolder"},
304         {&IID_IPersistFolder2,          "IID_IPersistFolder2"},
305         {&IID_IPersistFolder3,          "IID_IPersistFolder3"},
306         {&IID_IExtractIconA,            "IID_IExtractIconA"},
307         {&IID_IExtractIconW,            "IID_IExtractIconW"},
308         {&IID_IDataObject,              "IID_IDataObject"},
309         {&IID_IAutoComplete,            "IID_IAutoComplete"},
310         {&IID_IAutoComplete2,           "IID_IAutoComplete2"},
311         {NULL,NULL}};
312
313 const char * shdebugstr_guid( const struct _GUID *id )
314 {
315         int i;
316         char* name = NULL;
317         char clsidbuf[100];
318
319         shdebugstr_buf = (shdebugstr_buf == shdebugstr_buf1) ? shdebugstr_buf2 : shdebugstr_buf1;
320
321         if (!id) {
322           strcpy (shdebugstr_buf, "(null)");
323         } else {
324             for (i=0;InterfaceDesc[i].riid && !name;i++) {
325                 if (IsEqualIID(InterfaceDesc[i].riid, id)) name = InterfaceDesc[i].name;
326             }
327             if (!name) {
328                 if (HCR_GetClassNameA(id, clsidbuf, 100))
329                     name = clsidbuf;
330             }
331
332             sprintf( shdebugstr_buf, "\n\t{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x} (%s)",
333                  id->Data1, id->Data2, id->Data3,
334                  id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
335                  id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7], name ? name : "unknown" );
336         }
337         return shdebugstr_buf;
338 }