Fixed buffer overflow.
[wine] / dlls / shell32 / brsfolder.c
1 #include <stdlib.h>
2 #include <string.h>
3
4 #include "winerror.h"
5 #include "heap.h"
6 #include "dlgs.h"
7 #include "win.h"
8 #include "sysmetrics.h"
9 #include "debugtools.h"
10 #include "winreg.h"
11 #include "authors.h"
12 #include "winnls.h"
13 #include "commctrl.h"
14 #include "spy.h"
15
16 #include "wine/obj_base.h"
17 #include "wine/obj_enumidlist.h"
18 #include "wine/obj_shellfolder.h"
19 #include "wine/undocshell.h"
20
21 #include "pidl.h"
22 #include "shell32_main.h"
23 #include "shellapi.h"
24
25 DEFAULT_DEBUG_CHANNEL(shell)
26
27 #define         IDD_TREEVIEW 99
28
29 static HWND             hwndTreeView;
30 static LPBROWSEINFOA  lpBrowseInfo;
31 static LPITEMIDLIST     pidlRet;
32
33 static void FillTreeView(LPSHELLFOLDER lpsf, LPITEMIDLIST  lpifq, HTREEITEM hParent);
34
35 static void InitializeTreeView(HWND hwndParent)
36 {
37         HIMAGELIST      hImageList;
38         IShellFolder *  lpsf;
39         HRESULT hr;
40
41         hwndTreeView = GetDlgItem (hwndParent, IDD_TREEVIEW);
42         Shell_GetImageList(NULL, &hImageList);
43         
44         TRACE("dlg=%x tree=%x\n", hwndParent, hwndTreeView );
45
46         if (hImageList && hwndTreeView)
47         { TreeView_SetImageList(hwndTreeView, hImageList, 0);
48         }
49
50         hr = SHGetDesktopFolder(&lpsf);
51
52         if (SUCCEEDED(hr) && hwndTreeView)
53         { TreeView_DeleteAllItems(hwndTreeView);
54           FillTreeView(lpsf, NULL, TVI_ROOT);
55         }
56         
57         if (SUCCEEDED(hr))
58         { IShellFolder_Release(lpsf);
59         }
60 }
61
62 static int GetIcon(LPITEMIDLIST lpi, UINT uFlags)
63 {       SHFILEINFOA    sfi;
64         SHGetFileInfoA((LPCSTR)lpi,0,&sfi, sizeof(SHFILEINFOA), uFlags);
65         return sfi.iIcon;
66 }
67
68 static void GetNormalAndSelectedIcons(LPITEMIDLIST lpifq,LPTVITEMA lpTV_ITEM)
69 {       TRACE("%p %p\n",lpifq, lpTV_ITEM);
70
71         lpTV_ITEM->iImage = GetIcon(lpifq, SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
72         lpTV_ITEM->iSelectedImage = GetIcon(lpifq, SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_OPENICON);
73
74         return;
75 }
76
77 typedef struct tagID
78 {
79    LPSHELLFOLDER lpsfParent;
80    LPITEMIDLIST  lpi;
81    LPITEMIDLIST  lpifq;
82 } TV_ITEMDATA, *LPTV_ITEMDATA;
83
84 static BOOL GetName(LPSHELLFOLDER lpsf, LPITEMIDLIST lpi, DWORD dwFlags, LPSTR lpFriendlyName)
85 {
86         BOOL   bSuccess=TRUE;
87         STRRET str;
88
89         TRACE("%p %p %lx %p\n", lpsf, lpi, dwFlags, lpFriendlyName);
90         if (SUCCEEDED(IShellFolder_GetDisplayNameOf(lpsf, lpi, dwFlags, &str)))
91         {
92           if(FAILED(StrRetToStrNA (lpFriendlyName, MAX_PATH, &str, lpi)))
93           {
94               bSuccess = FALSE;
95           }
96         }
97         else
98           bSuccess = FALSE;
99
100         TRACE("-- %s\n",lpFriendlyName);
101         return bSuccess;
102 }
103
104 static void FillTreeView(IShellFolder * lpsf, LPITEMIDLIST  pidl, HTREEITEM hParent)
105 {
106         TVITEMA         tvi;
107         TVINSERTSTRUCTA tvins;
108         HTREEITEM       hPrev = 0;
109         LPENUMIDLIST    lpe=0;
110         LPITEMIDLIST    pidlTemp=0;
111         LPTV_ITEMDATA   lptvid=0;
112         ULONG           ulFetched;
113         HRESULT         hr;
114         char            szBuff[256];
115         HWND            hwnd=GetParent(hwndTreeView);
116
117         TRACE("%p %p %x\n",lpsf, pidl, (INT)hParent);
118         SetCapture(GetParent(hwndTreeView));
119         SetCursor(LoadCursorA(0, IDC_WAITA));
120
121         hr=IShellFolder_EnumObjects(lpsf,hwnd, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS,&lpe);
122
123         if (SUCCEEDED(hr))
124         { while (NOERROR == IEnumIDList_Next(lpe,1,&pidlTemp,&ulFetched))
125           { ULONG ulAttrs = SFGAO_HASSUBFOLDER | SFGAO_FOLDER;
126             IShellFolder_GetAttributesOf(lpsf, 1, &pidlTemp, &ulAttrs);
127             if (ulAttrs & (SFGAO_HASSUBFOLDER | SFGAO_FOLDER))
128             { if (ulAttrs & SFGAO_FOLDER)
129               { tvi.mask  = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
130
131                 if (ulAttrs & SFGAO_HASSUBFOLDER)
132                 {  tvi.cChildren=1;
133                    tvi.mask |= TVIF_CHILDREN;
134                 }
135
136                 if (!( lptvid = (LPTV_ITEMDATA)SHAlloc(sizeof(TV_ITEMDATA))))
137                     goto Done;
138
139                 if (!GetName(lpsf, pidlTemp, SHGDN_NORMAL, szBuff))
140                     goto Done;
141
142                 tvi.pszText    = szBuff;
143                 tvi.cchTextMax = MAX_PATH;
144                 tvi.lParam = (LPARAM)lptvid;
145
146                 IShellFolder_AddRef(lpsf);
147                 lptvid->lpsfParent = lpsf;
148                 lptvid->lpi     = ILClone(pidlTemp);
149                 lptvid->lpifq   = ILCombine(pidl, pidlTemp);
150                 GetNormalAndSelectedIcons(lptvid->lpifq, &tvi);
151
152                 tvins.DUMMYUNIONNAME.item         = tvi;
153                 tvins.hInsertAfter = hPrev;
154                 tvins.hParent      = hParent;
155
156                 hPrev = (HTREEITEM)TreeView_InsertItemA (hwndTreeView, &tvins);
157
158               }
159             }
160             SHFree(pidlTemp);  /* Finally, free the pidl that the shell gave us... */
161             pidlTemp=0;
162           }
163         }
164
165 Done:
166         ReleaseCapture();
167         SetCursor(LoadCursorA(0, IDC_ARROWA));
168
169         if (lpe)
170           IEnumIDList_Release(lpe);
171         if (pidlTemp )
172           SHFree(pidlTemp);
173 }
174
175 static LRESULT MsgNotify(HWND hWnd,  UINT CtlID, LPNMHDR lpnmh)
176 {       
177         NMTREEVIEWA     *pnmtv   = (NMTREEVIEWA *)lpnmh;
178         LPTV_ITEMDATA   lptvid;  /* Long pointer to TreeView item data */
179         IShellFolder *  lpsf2=0;
180         
181
182         TRACE("%x %x %p msg=%x\n", hWnd,  CtlID, lpnmh, pnmtv->hdr.code);
183
184         switch (pnmtv->hdr.idFrom)
185         { case IDD_TREEVIEW:
186             switch (pnmtv->hdr.code)   
187             { case TVN_DELETEITEMA:
188                 { FIXME("TVN_DELETEITEMA\n");
189                   lptvid=(LPTV_ITEMDATA)pnmtv->itemOld.lParam;
190                   IShellFolder_Release(lptvid->lpsfParent);
191                   SHFree(lptvid->lpi);  
192                   SHFree(lptvid->lpifq);  
193                   SHFree(lptvid);  
194                 }
195                 break;
196                         
197               case TVN_ITEMEXPANDINGA:
198                 { FIXME("TVN_ITEMEXPANDINGA\n");
199                   if ((pnmtv->itemNew.state & TVIS_EXPANDEDONCE))
200                     break;
201                 
202                   lptvid=(LPTV_ITEMDATA)pnmtv->itemNew.lParam;
203                   if (SUCCEEDED(IShellFolder_BindToObject(lptvid->lpsfParent, lptvid->lpi,0,(REFIID)&IID_IShellFolder,(LPVOID *)&lpsf2)))
204                   { FillTreeView( lpsf2, lptvid->lpifq, pnmtv->itemNew.hItem );
205                   }
206                   TreeView_SortChildren(hwndTreeView, pnmtv->itemNew.hItem, FALSE);
207                 }
208                 break;
209               case TVN_SELCHANGEDA:
210                 lptvid=(LPTV_ITEMDATA)pnmtv->itemNew.lParam;
211                 pidlRet = lptvid->lpifq;
212                 break;
213
214               default:
215                 FIXME("unhandled\n");
216                 break;
217             }
218             break;
219
220           default:
221             break;
222         }
223
224         return 0;
225 }
226
227
228 /*************************************************************************
229  *             BrsFolderDlgProc32  (not an exported API function)
230  */
231 static BOOL WINAPI BrsFolderDlgProc( HWND hWnd, UINT msg, WPARAM wParam,
232                                      LPARAM lParam )
233 {    TRACE("hwnd=%i msg=%i 0x%08x 0x%08lx\n", hWnd,  msg, wParam, lParam );
234
235         switch(msg)
236         { case WM_INITDIALOG:
237             pidlRet = NULL;
238             lpBrowseInfo = (LPBROWSEINFOA) lParam;
239             if (lpBrowseInfo->lpfn)
240               FIXME("Callbacks not implemented\n");
241             if (lpBrowseInfo->ulFlags)
242               FIXME("flag %x not implemented\n", lpBrowseInfo->ulFlags);
243             if (lpBrowseInfo->lpszTitle)
244               FIXME("title %s not displayed\n", lpBrowseInfo->lpszTitle);
245             if ( lpBrowseInfo->pidlRoot )
246               FIXME("root is desktop\n");
247
248             InitializeTreeView ( hWnd);
249             return 1;
250
251           case WM_NOTIFY:
252             MsgNotify( hWnd, (UINT)wParam, (LPNMHDR)lParam);
253             break;
254             
255           case WM_COMMAND:
256             switch (wParam)
257             { case IDOK:
258                 pdump ( pidlRet );
259                 SHGetPathFromIDListA(pidlRet, lpBrowseInfo->pszDisplayName);
260                 EndDialog(hWnd, (DWORD) ILClone(pidlRet));
261                 return TRUE;
262
263               case IDCANCEL:
264                 EndDialog(hWnd, 0);
265                 return TRUE;
266             }
267             break;
268         }
269         return 0;
270 }
271
272 /*************************************************************************
273  * SHBrowseForFolderA [SHELL32.209]
274  *
275  */
276 LPITEMIDLIST WINAPI SHBrowseForFolderA (LPBROWSEINFOA lpbi)
277 {
278         TRACE("(%p{lpszTitle=%s})\n", lpbi, debugstr_a(lpbi->lpszTitle));
279
280         return (LPITEMIDLIST) DialogBoxParamA( shell32_hInstance,
281                         "SHBRSFORFOLDER_MSGBOX",  lpbi->hwndOwner,
282                         BrsFolderDlgProc, (INT)lpbi );
283 }