SHChangeNotify should use SHSimpleIDListFromPath as this function
[wine] / dlls / shell32 / changenotify.c
1 /*
2  *      shell change notification
3  *
4  * Copyright 2000 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 <string.h>
22
23 #include "wine/debug.h"
24 #include "windef.h"
25 #include "wingdi.h"
26 #include "pidl.h"
27 #include "shell32_main.h"
28 #include "undocshell.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(shell);
31
32 static CRITICAL_SECTION SHELL32_ChangenotifyCS;
33 static CRITICAL_SECTION_DEBUG critsect_debug =
34 {
35     0, 0, &SHELL32_ChangenotifyCS,
36     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
37       0, 0, { 0, (DWORD)(__FILE__ ": SHELL32_ChangenotifyCS") }
38 };
39 static CRITICAL_SECTION SHELL32_ChangenotifyCS = { &critsect_debug, -1, 0, 0, 0, 0 };
40
41 /* internal list of notification clients (internal) */
42 typedef struct _NOTIFICATIONLIST
43 {
44         struct _NOTIFICATIONLIST *next;
45         struct _NOTIFICATIONLIST *prev;
46         HWND hwnd;              /* window to notify */
47         DWORD uMsg;             /* message to send */
48         LPNOTIFYREGISTER apidl; /* array of entries to watch*/
49         UINT cidl;              /* number of pidls in array */
50         LONG wEventMask;        /* subscribed events */
51         LONG wSignalledEvent;   /* event that occurred */
52         DWORD dwFlags;          /* client flags */
53         LPCITEMIDLIST pidlSignaled; /*pidl of the path that caused the signal*/
54     
55 } NOTIFICATIONLIST, *LPNOTIFICATIONLIST;
56
57 static NOTIFICATIONLIST *head, *tail;
58
59 #define SHCNE_NOITEMEVENTS ( \
60    SHCNE_ASSOCCHANGED )
61
62 #define SHCNE_ONEITEMEVENTS ( \
63    SHCNE_ATTRIBUTES | SHCNE_CREATE | SHCNE_DELETE | SHCNE_DRIVEADD | \
64    SHCNE_DRIVEADDGUI | SHCNE_DRIVEREMOVED | SHCNE_FREESPACE | \
65    SHCNE_MEDIAINSERTED | SHCNE_MEDIAREMOVED | SHCNE_MKDIR | \
66    SHCNE_NETSHARE | SHCNE_NETUNSHARE | SHCNE_RMDIR | \
67    SHCNE_SERVERDISCONNECT | SHCNE_UPDATEDIR | SHCNE_UPDATEIMAGE )
68
69 #define SHCNE_TWOITEMEVENTS ( \
70    SHCNE_RENAMEFOLDER | SHCNE_RENAMEITEM | SHCNE_UPDATEITEM )
71
72 /* for dumping events */
73 static const char * DumpEvent( LONG event )
74 {
75     if( event == SHCNE_ALLEVENTS )
76         return "SHCNE_ALLEVENTS";
77 #define DUMPEV(x)  ,( event & SHCNE_##x )? #x " " : ""
78     return wine_dbg_sprintf( "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s"
79     DUMPEV(RENAMEITEM)
80     DUMPEV(CREATE)
81     DUMPEV(DELETE)
82     DUMPEV(MKDIR)
83     DUMPEV(RMDIR)
84     DUMPEV(MEDIAINSERTED)
85     DUMPEV(MEDIAREMOVED)
86     DUMPEV(DRIVEREMOVED)
87     DUMPEV(DRIVEADD)
88     DUMPEV(NETSHARE)
89     DUMPEV(NETUNSHARE)
90     DUMPEV(ATTRIBUTES)
91     DUMPEV(UPDATEDIR)
92     DUMPEV(UPDATEITEM)
93     DUMPEV(SERVERDISCONNECT)
94     DUMPEV(UPDATEIMAGE)
95     DUMPEV(DRIVEADDGUI)
96     DUMPEV(RENAMEFOLDER)
97     DUMPEV(FREESPACE)
98     DUMPEV(EXTENDED_EVENT)
99     DUMPEV(ASSOCCHANGED)
100     DUMPEV(INTERRUPT)
101     );
102 #undef DUMPEV
103 }
104
105 static const char * NodeName(LPNOTIFICATIONLIST item)
106 {
107     const char *str;
108     WCHAR path[MAX_PATH];
109
110     if(SHGetPathFromIDListW(item->apidl[0].pidlPath, path ))
111         str = wine_dbg_sprintf("%s", debugstr_w(path));
112     else
113         str = wine_dbg_sprintf("<not a disk file>" );
114     return str;
115 }
116
117 static void AddNode(LPNOTIFICATIONLIST item)
118 {
119     TRACE("item %p\n", item );
120
121     /* link items */
122     item->prev = tail;
123     item->next = NULL;
124     if( tail )
125         tail->next = item;
126     else
127         head = item;
128     tail = item;
129 }
130
131 static LPNOTIFICATIONLIST FindNode( HANDLE hitem )
132 {
133     LPNOTIFICATIONLIST ptr;
134     for( ptr = head; ptr; ptr = ptr->next )
135         if( ptr == (LPNOTIFICATIONLIST) hitem )
136             return ptr;
137     return NULL;
138 }
139
140 static void DeleteNode(LPNOTIFICATIONLIST item)
141 {
142     UINT i;
143
144     TRACE("item=%p prev=%p next=%p\n", item, item->prev, item->next);
145
146     /* remove item from list */
147     if( item->prev )
148         item->prev->next = item->next;
149     else
150         head = item->next;
151     if( item->next )
152         item->next->prev = item->prev;
153     else
154         tail = item->prev;
155
156     /* free the item */
157     for (i=0; i<item->cidl; i++)
158         SHFree(item->apidl[i].pidlPath);
159     SHFree(item->apidl);
160     SHFree(item);
161 }
162
163 void InitChangeNotifications(void)
164 {
165 }
166
167 void FreeChangeNotifications(void)
168 {
169     TRACE("\n");
170
171     EnterCriticalSection(&SHELL32_ChangenotifyCS);
172
173     while( head )
174         DeleteNode( head );
175
176     LeaveCriticalSection(&SHELL32_ChangenotifyCS);
177
178     DeleteCriticalSection(&SHELL32_ChangenotifyCS);
179 }
180
181 /*************************************************************************
182  * SHChangeNotifyRegister                       [SHELL32.2]
183  *
184  */
185 HANDLE WINAPI
186 SHChangeNotifyRegister(
187     HWND hwnd,
188     LONG dwFlags,
189     LONG wEventMask,
190     UINT uMsg,
191     int cItems,
192     LPCNOTIFYREGISTER lpItems)
193 {
194     LPNOTIFICATIONLIST item;
195     int i;
196
197     item = SHAlloc(sizeof(NOTIFICATIONLIST));
198
199     TRACE("(%p,0x%08lx,0x%08lx,0x%08x,0x%08x,%p) item=%p\n",
200         hwnd, dwFlags, wEventMask, uMsg, cItems, lpItems, item);
201
202     item->next = NULL;
203     item->prev = NULL;
204     item->cidl = cItems;
205     item->apidl = SHAlloc(sizeof(NOTIFYREGISTER) * cItems);
206     for(i=0;i<cItems;i++)
207     {
208         item->apidl[i].pidlPath = ILClone(lpItems[i].pidlPath);
209         item->apidl[i].bWatchSubtree = lpItems[i].bWatchSubtree;
210     }
211     item->hwnd = hwnd;
212     item->uMsg = uMsg;
213     item->wEventMask = wEventMask;
214     item->wSignalledEvent = 0;
215     item->dwFlags = dwFlags;
216
217     TRACE("new node: %s\n", NodeName( item ));
218
219     EnterCriticalSection(&SHELL32_ChangenotifyCS);
220
221     AddNode(item);
222
223     LeaveCriticalSection(&SHELL32_ChangenotifyCS);
224
225     return (HANDLE)item;
226 }
227
228 /*************************************************************************
229  * SHChangeNotifyDeregister                     [SHELL32.4]
230  */
231 BOOL WINAPI SHChangeNotifyDeregister(HANDLE hNotify)
232 {
233     LPNOTIFICATIONLIST node;
234
235     TRACE("(%p)\n",hNotify);
236
237     EnterCriticalSection(&SHELL32_ChangenotifyCS);
238
239     node = FindNode(hNotify);
240     if( node )
241         DeleteNode(node);
242
243     LeaveCriticalSection(&SHELL32_ChangenotifyCS);
244
245     return node?TRUE:FALSE;
246 }
247
248 /*************************************************************************
249  * SHChangeNotifyUpdateEntryList                [SHELL32.5]
250  */
251 BOOL WINAPI SHChangeNotifyUpdateEntryList(DWORD unknown1, DWORD unknown2,
252                               DWORD unknown3, DWORD unknown4)
253 {
254     FIXME("(0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx)\n",
255           unknown1, unknown2, unknown3, unknown4);
256
257     return -1;
258 }
259
260 static BOOL should_notify( LPITEMIDLIST changed, LPITEMIDLIST watched, BOOL sub )
261 {
262     TRACE("%p %p %d\n", changed, watched, sub );
263     if ( !watched )
264         return FALSE;
265     if (ILIsEqual( watched, changed ) )
266         return TRUE;
267     if( sub && ILIsParent( watched, changed, FALSE ) )
268         return TRUE;
269     return FALSE;
270 }
271
272 /*************************************************************************
273  * SHChangeNotify                               [SHELL32.@]
274  */
275 void WINAPI SHChangeNotify(LONG wEventId, UINT uFlags, LPCVOID dwItem1, LPCVOID dwItem2)
276 {
277     LPITEMIDLIST Pidls[2];
278     LPNOTIFICATIONLIST ptr;
279     UINT typeFlag = uFlags & SHCNF_TYPE;
280
281     Pidls[0] = NULL;
282     Pidls[1] = NULL;
283
284     TRACE("(0x%08lx,0x%08x,%p,%p):stub.\n", wEventId, uFlags, dwItem1, dwItem2);
285
286     if( ( wEventId & SHCNE_NOITEMEVENTS ) && ( dwItem1 || dwItem2 ) )
287     {
288         TRACE("dwItem1 and dwItem2 are not zero, but should be\n");
289         dwItem1 = 0;
290         dwItem2 = 0;
291         return;
292     }
293     else if( ( wEventId & SHCNE_ONEITEMEVENTS ) && dwItem2 )
294     {
295         TRACE("dwItem2 is not zero, but should be\n");
296         dwItem2 = 0;
297         return;
298     }
299
300     if( ( ( wEventId & SHCNE_NOITEMEVENTS ) && 
301           ( wEventId & ~SHCNE_NOITEMEVENTS ) ) ||
302         ( ( wEventId & SHCNE_ONEITEMEVENTS ) && 
303           ( wEventId & ~SHCNE_ONEITEMEVENTS ) ) ||
304         ( ( wEventId & SHCNE_TWOITEMEVENTS ) && 
305           ( wEventId & ~SHCNE_TWOITEMEVENTS ) ) )
306     {
307         WARN("mutually incompatible events listed\n");
308         return;
309     }
310
311     /* convert paths in IDLists*/
312     switch (typeFlag)
313     {
314     case SHCNF_PATHA:
315         if (dwItem1) Pidls[0] = SHSimpleIDListFromPathA((LPCSTR)dwItem1);
316         if (dwItem2) Pidls[1] = SHSimpleIDListFromPathA((LPCSTR)dwItem2);
317         break;
318     case SHCNF_PATHW:
319         if (dwItem1) Pidls[0] = SHSimpleIDListFromPathW((LPCWSTR)dwItem1);
320         if (dwItem2) Pidls[1] = SHSimpleIDListFromPathW((LPCWSTR)dwItem2);
321         break;
322     case SHCNF_IDLIST:
323         Pidls[0] = (LPITEMIDLIST)dwItem1;
324         Pidls[1] = (LPITEMIDLIST)dwItem2;
325         break;
326     case SHCNF_PRINTERA:
327     case SHCNF_PRINTERW:
328         FIXME("SHChangeNotify with (uFlags & SHCNF_PRINTER)");
329         return;
330     case SHCNF_DWORD:
331     default:
332         FIXME("unknown type %08x\n",typeFlag);
333         return;
334     }
335
336     {
337         WCHAR path[MAX_PATH];
338
339         if( Pidls[0] && SHGetPathFromIDListW(Pidls[0], path ))
340             TRACE("notify %08lx on item1 = %s\n", wEventId, debugstr_w(path));
341     
342         if( Pidls[1] && SHGetPathFromIDListW(Pidls[1], path ))
343             TRACE("notify %08lx on item2 = %s\n", wEventId, debugstr_w(path));
344     }
345
346     EnterCriticalSection(&SHELL32_ChangenotifyCS);
347
348     /* loop through the list */
349     for( ptr = head; ptr; ptr = ptr->next )
350     {
351         BOOL notify;
352         DWORD i;
353
354         notify = FALSE;
355
356         TRACE("trying %p\n", ptr);
357
358         for( i=0; (i<ptr->cidl) && !notify ; i++ )
359         {
360             LPITEMIDLIST pidl = ptr->apidl[i].pidlPath;
361             BOOL subtree = ptr->apidl[i].bWatchSubtree;
362
363             if (wEventId & ptr->wEventMask)
364             {
365                 if( !pidl )          /* all ? */
366                     notify = TRUE;
367                 else if( wEventId & SHCNE_NOITEMEVENTS )
368                     notify = TRUE;
369                 else if( wEventId & ( SHCNE_ONEITEMEVENTS | SHCNE_TWOITEMEVENTS ) )
370                     notify = should_notify( Pidls[0], pidl, subtree );
371                 else if( wEventId & SHCNE_TWOITEMEVENTS )
372                     notify = should_notify( Pidls[1], pidl, subtree );
373             }
374         }
375
376         if( !notify )
377             continue;
378
379         ptr->pidlSignaled = ILClone(Pidls[0]);
380
381         TRACE("notifying %s, event %s(%lx) before\n", NodeName( ptr ), DumpEvent(
382                wEventId ),wEventId );
383
384         ptr->wSignalledEvent |= wEventId;
385         SendMessageA(ptr->hwnd, ptr->uMsg, (WPARAM)ptr, GetCurrentProcessId());
386
387         TRACE("notifying %s, event %s(%lx) after\n", NodeName( ptr ), DumpEvent(
388                 wEventId ),wEventId );
389
390     }
391     TRACE("notify Done\n");
392     LeaveCriticalSection(&SHELL32_ChangenotifyCS);
393     
394     /* if we allocated it, free it. The ANSI flag is also set in its Unicode sibling. */
395     if ((typeFlag & SHCNF_PATHA) || (typeFlag & SHCNF_PRINTERA))
396     {
397         if (Pidls[0]) SHFree(Pidls[0]);
398         if (Pidls[1]) SHFree(Pidls[1]);
399     }
400 }
401
402 /*************************************************************************
403  * NTSHChangeNotifyRegister                     [SHELL32.640]
404  * NOTES
405  *   Idlist is an array of structures and Count specifies how many items in the array
406  *   (usually just one I think).
407  */
408 DWORD WINAPI NTSHChangeNotifyRegister(
409     HWND hwnd,
410     LONG events1,
411     LONG events2,
412     DWORD msg,
413     int count,
414     LPCNOTIFYREGISTER idlist)
415 {
416     FIXME("(%p,0x%08lx,0x%08lx,0x%08lx,0x%08x,%p):semi stub.\n",
417                 hwnd,events1,events2,msg,count,idlist);
418
419     return (DWORD) SHChangeNotifyRegister(hwnd, events1, events2, msg, count, idlist);
420 }
421
422 /*************************************************************************
423  * SHChangeNotification_Lock                    [SHELL32.644]
424  */
425 HANDLE WINAPI SHChangeNotification_Lock(
426         HANDLE hChange,
427         DWORD dwProcessId,
428         LPCITEMIDLIST **lppidls,
429         LPLONG lpwEventId)
430 {
431     DWORD i;
432     LPNOTIFICATIONLIST node;
433     LPCITEMIDLIST *idlist;
434
435     TRACE("%p %08lx %p %p\n", hChange, dwProcessId, lppidls, lpwEventId);
436
437     /* EnterCriticalSection(&SHELL32_ChangenotifyCS); */
438
439     node = FindNode( hChange );
440     if( node )
441     {
442         idlist = SHAlloc( sizeof(LPCITEMIDLIST *) * node->cidl );
443         for(i=0; i<node->cidl; i++)
444             idlist[i] = node->pidlSignaled; 
445         *lpwEventId = node->wSignalledEvent;
446         *lppidls = idlist;
447         node->wSignalledEvent = 0;
448     }
449     else
450         ERR("Couldn't find %p\n", hChange );
451
452     /* LeaveCriticalSection(&SHELL32_ChangenotifyCS); */
453
454     return (HANDLE) node;
455 }
456
457 /*************************************************************************
458  * SHChangeNotification_Unlock                  [SHELL32.645]
459  */
460 BOOL WINAPI SHChangeNotification_Unlock ( HANDLE hLock)
461 {
462     TRACE("\n");
463     return 1;
464 }
465
466 /*************************************************************************
467  * NTSHChangeNotifyDeregister                   [SHELL32.641]
468  */
469 DWORD WINAPI NTSHChangeNotifyDeregister(LONG x1)
470 {
471     FIXME("(0x%08lx):semi stub.\n",x1);
472
473     return SHChangeNotifyDeregister( (HANDLE)x1 );
474 }