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
23 #include "wine/debug.h"
27 #include "shell32_main.h"
28 #include "undocshell.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(shell);
32 static CRITICAL_SECTION SHELL32_ChangenotifyCS;
33 static CRITICAL_SECTION_DEBUG critsect_debug =
35 0, 0, &SHELL32_ChangenotifyCS,
36 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
37 0, 0, { 0, (DWORD)(__FILE__ ": SHELL32_ChangenotifyCS") }
39 static CRITICAL_SECTION SHELL32_ChangenotifyCS = { &critsect_debug, -1, 0, 0, 0, 0 };
41 /* internal list of notification clients (internal) */
42 typedef struct _NOTIFICATIONLIST
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*/
55 } NOTIFICATIONLIST, *LPNOTIFICATIONLIST;
57 static NOTIFICATIONLIST *head, *tail;
59 #define SHCNE_NOITEMEVENTS ( \
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 )
69 #define SHCNE_TWOITEMEVENTS ( \
70 SHCNE_RENAMEFOLDER | SHCNE_RENAMEITEM | SHCNE_UPDATEITEM )
72 /* for dumping events */
73 static const char * DumpEvent( LONG event )
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"
93 DUMPEV(SERVERDISCONNECT)
98 DUMPEV(EXTENDED_EVENT)
105 static const char * NodeName(LPNOTIFICATIONLIST item)
108 WCHAR path[MAX_PATH];
110 if(SHGetPathFromIDListW(item->apidl[0].pidlPath, path ))
111 str = wine_dbg_sprintf("%s", debugstr_w(path));
113 str = wine_dbg_sprintf("<not a disk file>" );
117 static void AddNode(LPNOTIFICATIONLIST item)
119 TRACE("item %p\n", item );
131 static LPNOTIFICATIONLIST FindNode( HANDLE hitem )
133 LPNOTIFICATIONLIST ptr;
134 for( ptr = head; ptr; ptr = ptr->next )
135 if( ptr == (LPNOTIFICATIONLIST) hitem )
140 static void DeleteNode(LPNOTIFICATIONLIST item)
144 TRACE("item=%p prev=%p next=%p\n", item, item->prev, item->next);
146 /* remove item from list */
148 item->prev->next = item->next;
152 item->next->prev = item->prev;
157 for (i=0; i<item->cidl; i++)
158 SHFree(item->apidl[i].pidlPath);
163 void InitChangeNotifications(void)
167 void FreeChangeNotifications(void)
171 EnterCriticalSection(&SHELL32_ChangenotifyCS);
176 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
178 DeleteCriticalSection(&SHELL32_ChangenotifyCS);
181 /*************************************************************************
182 * SHChangeNotifyRegister [SHELL32.2]
186 SHChangeNotifyRegister(
192 LPCNOTIFYREGISTER lpItems)
194 LPNOTIFICATIONLIST item;
197 item = SHAlloc(sizeof(NOTIFICATIONLIST));
199 TRACE("(%p,0x%08lx,0x%08lx,0x%08x,0x%08x,%p) item=%p\n",
200 hwnd, dwFlags, wEventMask, uMsg, cItems, lpItems, item);
205 item->apidl = SHAlloc(sizeof(NOTIFYREGISTER) * cItems);
206 for(i=0;i<cItems;i++)
208 item->apidl[i].pidlPath = ILClone(lpItems[i].pidlPath);
209 item->apidl[i].bWatchSubtree = lpItems[i].bWatchSubtree;
213 item->wEventMask = wEventMask;
214 item->wSignalledEvent = 0;
215 item->dwFlags = dwFlags;
217 TRACE("new node: %s\n", NodeName( item ));
219 EnterCriticalSection(&SHELL32_ChangenotifyCS);
223 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
228 /*************************************************************************
229 * SHChangeNotifyDeregister [SHELL32.4]
231 BOOL WINAPI SHChangeNotifyDeregister(HANDLE hNotify)
233 LPNOTIFICATIONLIST node;
235 TRACE("(%p)\n",hNotify);
237 EnterCriticalSection(&SHELL32_ChangenotifyCS);
239 node = FindNode(hNotify);
243 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
245 return node?TRUE:FALSE;
248 /*************************************************************************
249 * SHChangeNotifyUpdateEntryList [SHELL32.5]
251 BOOL WINAPI SHChangeNotifyUpdateEntryList(DWORD unknown1, DWORD unknown2,
252 DWORD unknown3, DWORD unknown4)
254 FIXME("(0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx)\n",
255 unknown1, unknown2, unknown3, unknown4);
260 static BOOL should_notify( LPITEMIDLIST changed, LPITEMIDLIST watched, BOOL sub )
262 TRACE("%p %p %d\n", changed, watched, sub );
265 if (ILIsEqual( watched, changed ) )
267 if( sub && ILIsParent( watched, changed, FALSE ) )
272 /*************************************************************************
273 * SHChangeNotify [SHELL32.@]
275 void WINAPI SHChangeNotify(LONG wEventId, UINT uFlags, LPCVOID dwItem1, LPCVOID dwItem2)
277 LPITEMIDLIST Pidls[2];
278 LPNOTIFICATIONLIST ptr;
279 UINT typeFlag = uFlags & SHCNF_TYPE;
284 TRACE("(0x%08lx,0x%08x,%p,%p):stub.\n", wEventId, uFlags, dwItem1, dwItem2);
286 if( ( wEventId & SHCNE_NOITEMEVENTS ) && ( dwItem1 || dwItem2 ) )
288 TRACE("dwItem1 and dwItem2 are not zero, but should be\n");
293 else if( ( wEventId & SHCNE_ONEITEMEVENTS ) && dwItem2 )
295 TRACE("dwItem2 is not zero, but should be\n");
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 ) ) )
307 WARN("mutually incompatible events listed\n");
311 /* convert paths in IDLists*/
315 if (dwItem1) Pidls[0] = SHSimpleIDListFromPathA((LPCSTR)dwItem1);
316 if (dwItem2) Pidls[1] = SHSimpleIDListFromPathA((LPCSTR)dwItem2);
319 if (dwItem1) Pidls[0] = SHSimpleIDListFromPathW((LPCWSTR)dwItem1);
320 if (dwItem2) Pidls[1] = SHSimpleIDListFromPathW((LPCWSTR)dwItem2);
323 Pidls[0] = (LPITEMIDLIST)dwItem1;
324 Pidls[1] = (LPITEMIDLIST)dwItem2;
328 FIXME("SHChangeNotify with (uFlags & SHCNF_PRINTER)");
332 FIXME("unknown type %08x\n",typeFlag);
337 WCHAR path[MAX_PATH];
339 if( Pidls[0] && SHGetPathFromIDListW(Pidls[0], path ))
340 TRACE("notify %08lx on item1 = %s\n", wEventId, debugstr_w(path));
342 if( Pidls[1] && SHGetPathFromIDListW(Pidls[1], path ))
343 TRACE("notify %08lx on item2 = %s\n", wEventId, debugstr_w(path));
346 EnterCriticalSection(&SHELL32_ChangenotifyCS);
348 /* loop through the list */
349 for( ptr = head; ptr; ptr = ptr->next )
356 TRACE("trying %p\n", ptr);
358 for( i=0; (i<ptr->cidl) && !notify ; i++ )
360 LPITEMIDLIST pidl = ptr->apidl[i].pidlPath;
361 BOOL subtree = ptr->apidl[i].bWatchSubtree;
363 if (wEventId & ptr->wEventMask)
365 if( !pidl ) /* all ? */
367 else if( wEventId & SHCNE_NOITEMEVENTS )
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 );
379 ptr->pidlSignaled = ILClone(Pidls[0]);
381 TRACE("notifying %s, event %s(%lx) before\n", NodeName( ptr ), DumpEvent(
382 wEventId ),wEventId );
384 ptr->wSignalledEvent |= wEventId;
385 SendMessageA(ptr->hwnd, ptr->uMsg, (WPARAM)ptr, GetCurrentProcessId());
387 TRACE("notifying %s, event %s(%lx) after\n", NodeName( ptr ), DumpEvent(
388 wEventId ),wEventId );
391 TRACE("notify Done\n");
392 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
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))
397 if (Pidls[0]) SHFree(Pidls[0]);
398 if (Pidls[1]) SHFree(Pidls[1]);
402 /*************************************************************************
403 * NTSHChangeNotifyRegister [SHELL32.640]
405 * Idlist is an array of structures and Count specifies how many items in the array
406 * (usually just one I think).
408 DWORD WINAPI NTSHChangeNotifyRegister(
414 LPCNOTIFYREGISTER idlist)
416 FIXME("(%p,0x%08lx,0x%08lx,0x%08lx,0x%08x,%p):semi stub.\n",
417 hwnd,events1,events2,msg,count,idlist);
419 return (DWORD) SHChangeNotifyRegister(hwnd, events1, events2, msg, count, idlist);
422 /*************************************************************************
423 * SHChangeNotification_Lock [SHELL32.644]
425 HANDLE WINAPI SHChangeNotification_Lock(
428 LPCITEMIDLIST **lppidls,
432 LPNOTIFICATIONLIST node;
433 LPCITEMIDLIST *idlist;
435 TRACE("%p %08lx %p %p\n", hChange, dwProcessId, lppidls, lpwEventId);
437 /* EnterCriticalSection(&SHELL32_ChangenotifyCS); */
439 node = FindNode( hChange );
442 idlist = SHAlloc( sizeof(LPCITEMIDLIST *) * node->cidl );
443 for(i=0; i<node->cidl; i++)
444 idlist[i] = node->pidlSignaled;
445 *lpwEventId = node->wSignalledEvent;
447 node->wSignalledEvent = 0;
450 ERR("Couldn't find %p\n", hChange );
452 /* LeaveCriticalSection(&SHELL32_ChangenotifyCS); */
454 return (HANDLE) node;
457 /*************************************************************************
458 * SHChangeNotification_Unlock [SHELL32.645]
460 BOOL WINAPI SHChangeNotification_Unlock ( HANDLE hLock)
466 /*************************************************************************
467 * NTSHChangeNotifyDeregister [SHELL32.641]
469 DWORD WINAPI NTSHChangeNotifyDeregister(LONG x1)
471 FIXME("(0x%08lx):semi stub.\n",x1);
473 return SHChangeNotifyDeregister( (HANDLE)x1 );