2 * shell change notification
4 * Copyright 2000 Juergen Schmied
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.
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.
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
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
28 #include "wine/debug.h"
30 #include "shell32_main.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(shell);
34 static CRITICAL_SECTION SHELL32_ChangenotifyCS;
35 static CRITICAL_SECTION_DEBUG critsect_debug =
37 0, 0, &SHELL32_ChangenotifyCS,
38 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
39 0, 0, { (DWORD_PTR)(__FILE__ ": SHELL32_ChangenotifyCS") }
41 static CRITICAL_SECTION SHELL32_ChangenotifyCS = { &critsect_debug, -1, 0, 0, 0, 0 };
43 typedef SHChangeNotifyEntry *LPNOTIFYREGISTER;
45 /* internal list of notification clients (internal) */
46 typedef struct _NOTIFICATIONLIST
48 struct _NOTIFICATIONLIST *next;
49 struct _NOTIFICATIONLIST *prev;
50 HWND hwnd; /* window to notify */
51 DWORD uMsg; /* message to send */
52 LPNOTIFYREGISTER apidl; /* array of entries to watch*/
53 UINT cidl; /* number of pidls in array */
54 LONG wEventMask; /* subscribed events */
55 LONG wSignalledEvent; /* event that occurred */
56 DWORD dwFlags; /* client flags */
57 LPCITEMIDLIST pidlSignaled; /*pidl of the path that caused the signal*/
59 } NOTIFICATIONLIST, *LPNOTIFICATIONLIST;
61 static NOTIFICATIONLIST *head, *tail;
63 #define SHCNE_NOITEMEVENTS ( \
66 #define SHCNE_ONEITEMEVENTS ( \
67 SHCNE_ATTRIBUTES | SHCNE_CREATE | SHCNE_DELETE | SHCNE_DRIVEADD | \
68 SHCNE_DRIVEADDGUI | SHCNE_DRIVEREMOVED | SHCNE_FREESPACE | \
69 SHCNE_MEDIAINSERTED | SHCNE_MEDIAREMOVED | SHCNE_MKDIR | \
70 SHCNE_NETSHARE | SHCNE_NETUNSHARE | SHCNE_RMDIR | \
71 SHCNE_SERVERDISCONNECT | SHCNE_UPDATEDIR | SHCNE_UPDATEIMAGE )
73 #define SHCNE_TWOITEMEVENTS ( \
74 SHCNE_RENAMEFOLDER | SHCNE_RENAMEITEM | SHCNE_UPDATEITEM )
76 /* for dumping events */
77 static const char * DumpEvent( LONG event )
79 if( event == SHCNE_ALLEVENTS )
80 return "SHCNE_ALLEVENTS";
81 #define DUMPEV(x) ,( event & SHCNE_##x )? #x " " : ""
82 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"
97 DUMPEV(SERVERDISCONNECT)
102 DUMPEV(EXTENDED_EVENT)
109 static const char * NodeName(LPNOTIFICATIONLIST item)
112 WCHAR path[MAX_PATH];
114 if(SHGetPathFromIDListW(item->apidl[0].pidl, path ))
115 str = wine_dbg_sprintf("%s", debugstr_w(path));
117 str = wine_dbg_sprintf("<not a disk file>" );
121 static void AddNode(LPNOTIFICATIONLIST item)
123 TRACE("item %p\n", item );
135 static LPNOTIFICATIONLIST FindNode( HANDLE hitem )
137 LPNOTIFICATIONLIST ptr;
138 for( ptr = head; ptr; ptr = ptr->next )
139 if( ptr == (LPNOTIFICATIONLIST) hitem )
144 static void DeleteNode(LPNOTIFICATIONLIST item)
148 TRACE("item=%p prev=%p next=%p\n", item, item->prev, item->next);
150 /* remove item from list */
152 item->prev->next = item->next;
156 item->next->prev = item->prev;
161 for (i=0; i<item->cidl; i++)
162 SHFree((LPITEMIDLIST)item->apidl[i].pidl);
167 void InitChangeNotifications(void)
171 void FreeChangeNotifications(void)
175 EnterCriticalSection(&SHELL32_ChangenotifyCS);
180 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
182 DeleteCriticalSection(&SHELL32_ChangenotifyCS);
185 /*************************************************************************
186 * SHChangeNotifyRegister [SHELL32.2]
190 SHChangeNotifyRegister(
196 SHChangeNotifyEntry *lpItems)
198 LPNOTIFICATIONLIST item;
201 item = SHAlloc(sizeof(NOTIFICATIONLIST));
203 TRACE("(%p,0x%08x,0x%08lx,0x%08x,%d,%p) item=%p\n",
204 hwnd, fSources, wEventMask, uMsg, cItems, lpItems, item);
209 item->apidl = SHAlloc(sizeof(SHChangeNotifyEntry) * cItems);
210 for(i=0;i<cItems;i++)
212 item->apidl[i].pidl = ILClone(lpItems[i].pidl);
213 item->apidl[i].fRecursive = lpItems[i].fRecursive;
217 item->wEventMask = wEventMask;
218 item->wSignalledEvent = 0;
219 item->dwFlags = fSources;
221 TRACE("new node: %s\n", NodeName( item ));
223 EnterCriticalSection(&SHELL32_ChangenotifyCS);
227 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
232 /*************************************************************************
233 * SHChangeNotifyDeregister [SHELL32.4]
235 BOOL WINAPI SHChangeNotifyDeregister(ULONG hNotify)
237 LPNOTIFICATIONLIST node;
239 TRACE("(0x%08lx)\n", hNotify);
241 EnterCriticalSection(&SHELL32_ChangenotifyCS);
243 node = FindNode((HANDLE)hNotify);
247 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
249 return node?TRUE:FALSE;
252 /*************************************************************************
253 * SHChangeNotifyUpdateEntryList [SHELL32.5]
255 BOOL WINAPI SHChangeNotifyUpdateEntryList(DWORD unknown1, DWORD unknown2,
256 DWORD unknown3, DWORD unknown4)
258 FIXME("(0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx)\n",
259 unknown1, unknown2, unknown3, unknown4);
264 static BOOL should_notify( LPCITEMIDLIST changed, LPCITEMIDLIST watched, BOOL sub )
266 TRACE("%p %p %d\n", changed, watched, sub );
269 if (ILIsEqual( watched, changed ) )
271 if( sub && ILIsParent( watched, changed, FALSE ) )
276 /*************************************************************************
277 * SHChangeNotify [SHELL32.@]
279 void WINAPI SHChangeNotify(LONG wEventId, UINT uFlags, LPCVOID dwItem1, LPCVOID dwItem2)
281 LPCITEMIDLIST Pidls[2];
282 LPNOTIFICATIONLIST ptr;
283 UINT typeFlag = uFlags & SHCNF_TYPE;
288 TRACE("(0x%08lx,0x%08x,%p,%p):stub.\n", wEventId, uFlags, dwItem1, dwItem2);
290 if( ( wEventId & SHCNE_NOITEMEVENTS ) && ( dwItem1 || dwItem2 ) )
292 TRACE("dwItem1 and dwItem2 are not zero, but should be\n");
297 else if( ( wEventId & SHCNE_ONEITEMEVENTS ) && dwItem2 )
299 TRACE("dwItem2 is not zero, but should be\n");
304 if( ( ( wEventId & SHCNE_NOITEMEVENTS ) &&
305 ( wEventId & ~SHCNE_NOITEMEVENTS ) ) ||
306 ( ( wEventId & SHCNE_ONEITEMEVENTS ) &&
307 ( wEventId & ~SHCNE_ONEITEMEVENTS ) ) ||
308 ( ( wEventId & SHCNE_TWOITEMEVENTS ) &&
309 ( wEventId & ~SHCNE_TWOITEMEVENTS ) ) )
311 WARN("mutually incompatible events listed\n");
315 /* convert paths in IDLists*/
319 if (dwItem1) Pidls[0] = SHSimpleIDListFromPathA((LPCSTR)dwItem1);
320 if (dwItem2) Pidls[1] = SHSimpleIDListFromPathA((LPCSTR)dwItem2);
323 if (dwItem1) Pidls[0] = SHSimpleIDListFromPathW((LPCWSTR)dwItem1);
324 if (dwItem2) Pidls[1] = SHSimpleIDListFromPathW((LPCWSTR)dwItem2);
327 Pidls[0] = (LPCITEMIDLIST)dwItem1;
328 Pidls[1] = (LPCITEMIDLIST)dwItem2;
332 FIXME("SHChangeNotify with (uFlags & SHCNF_PRINTER)\n");
336 FIXME("unknown type %08x\n",typeFlag);
341 WCHAR path[MAX_PATH];
343 if( Pidls[0] && SHGetPathFromIDListW(Pidls[0], path ))
344 TRACE("notify %08lx on item1 = %s\n", wEventId, debugstr_w(path));
346 if( Pidls[1] && SHGetPathFromIDListW(Pidls[1], path ))
347 TRACE("notify %08lx on item2 = %s\n", wEventId, debugstr_w(path));
350 EnterCriticalSection(&SHELL32_ChangenotifyCS);
352 /* loop through the list */
353 for( ptr = head; ptr; ptr = ptr->next )
360 TRACE("trying %p\n", ptr);
362 for( i=0; (i<ptr->cidl) && !notify ; i++ )
364 LPCITEMIDLIST pidl = ptr->apidl[i].pidl;
365 BOOL subtree = ptr->apidl[i].fRecursive;
367 if (wEventId & ptr->wEventMask)
369 if( !pidl ) /* all ? */
371 else if( wEventId & SHCNE_NOITEMEVENTS )
373 else if( wEventId & ( SHCNE_ONEITEMEVENTS | SHCNE_TWOITEMEVENTS ) )
374 notify = should_notify( Pidls[0], pidl, subtree );
375 else if( wEventId & SHCNE_TWOITEMEVENTS )
376 notify = should_notify( Pidls[1], pidl, subtree );
383 ptr->pidlSignaled = ILClone(Pidls[0]);
385 TRACE("notifying %s, event %s(%lx) before\n", NodeName( ptr ), DumpEvent(
386 wEventId ),wEventId );
388 ptr->wSignalledEvent |= wEventId;
390 if (ptr->dwFlags & SHCNRF_NewDelivery)
391 SendMessageA(ptr->hwnd, ptr->uMsg, (WPARAM) ptr, (LPARAM) GetCurrentProcessId());
393 SendMessageA(ptr->hwnd, ptr->uMsg, (WPARAM)Pidls, wEventId);
395 TRACE("notifying %s, event %s(%lx) after\n", NodeName( ptr ), DumpEvent(
396 wEventId ),wEventId );
399 TRACE("notify Done\n");
400 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
402 /* if we allocated it, free it. The ANSI flag is also set in its Unicode sibling. */
403 if ((typeFlag & SHCNF_PATHA) || (typeFlag & SHCNF_PRINTERA))
405 if (Pidls[0]) SHFree((LPITEMIDLIST)Pidls[0]);
406 if (Pidls[1]) SHFree((LPITEMIDLIST)Pidls[1]);
410 /*************************************************************************
411 * NTSHChangeNotifyRegister [SHELL32.640]
413 * Idlist is an array of structures and Count specifies how many items in the array
414 * (usually just one I think).
416 DWORD WINAPI NTSHChangeNotifyRegister(
422 SHChangeNotifyEntry *idlist)
424 FIXME("(%p,0x%08lx,0x%08lx,0x%08lx,0x%08x,%p):semi stub.\n",
425 hwnd,events1,events2,msg,count,idlist);
427 return (DWORD) SHChangeNotifyRegister(hwnd, events1, events2, msg, count, idlist);
430 /*************************************************************************
431 * SHChangeNotification_Lock [SHELL32.644]
433 HANDLE WINAPI SHChangeNotification_Lock(
436 LPITEMIDLIST **lppidls,
440 LPNOTIFICATIONLIST node;
441 LPCITEMIDLIST *idlist;
443 TRACE("%p %08lx %p %p\n", hChange, dwProcessId, lppidls, lpwEventId);
445 /* EnterCriticalSection(&SHELL32_ChangenotifyCS); */
447 node = FindNode( hChange );
450 idlist = SHAlloc( sizeof(LPCITEMIDLIST *) * node->cidl );
451 for(i=0; i<node->cidl; i++)
452 idlist[i] = (LPCITEMIDLIST)node->pidlSignaled;
453 *lpwEventId = node->wSignalledEvent;
454 *lppidls = (LPITEMIDLIST*)idlist;
455 node->wSignalledEvent = 0;
458 ERR("Couldn't find %p\n", hChange );
460 /* LeaveCriticalSection(&SHELL32_ChangenotifyCS); */
462 return (HANDLE) node;
465 /*************************************************************************
466 * SHChangeNotification_Unlock [SHELL32.645]
468 BOOL WINAPI SHChangeNotification_Unlock ( HANDLE hLock)
474 /*************************************************************************
475 * NTSHChangeNotifyDeregister [SHELL32.641]
477 DWORD WINAPI NTSHChangeNotifyDeregister(ULONG x1)
479 FIXME("(0x%08lx):semi stub.\n",x1);
481 return SHChangeNotifyDeregister( x1 );