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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/list.h"
27 #include "wine/debug.h"
28 #include "shell32_main.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, { (DWORD_PTR
)(__FILE__
": SHELL32_ChangenotifyCS") }
39 static CRITICAL_SECTION SHELL32_ChangenotifyCS
= { &critsect_debug
, -1, 0, 0, 0, 0 };
41 typedef SHChangeNotifyEntry
*LPNOTIFYREGISTER
;
43 /* internal list of notification clients (internal) */
44 typedef struct _NOTIFICATIONLIST
47 HWND hwnd
; /* window to notify */
48 DWORD uMsg
; /* message to send */
49 LPNOTIFYREGISTER apidl
; /* array of entries to watch*/
50 UINT cidl
; /* number of pidls in array */
51 LONG wEventMask
; /* subscribed events */
52 DWORD dwFlags
; /* client flags */
54 } NOTIFICATIONLIST
, *LPNOTIFICATIONLIST
;
56 static struct list notifications
= LIST_INIT( notifications
);
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 | \
70 #define SHCNE_TWOITEMEVENTS ( \
71 SHCNE_RENAMEFOLDER | SHCNE_RENAMEITEM )
73 /* for dumping events */
74 static const char * DumpEvent( LONG event
)
76 if( event
== SHCNE_ALLEVENTS
)
77 return "SHCNE_ALLEVENTS";
78 #define DUMPEV(x) ,( event & SHCNE_##x )? #x " " : ""
79 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"
94 DUMPEV(SERVERDISCONNECT
)
99 DUMPEV(EXTENDED_EVENT
)
106 static const char * NodeName(const NOTIFICATIONLIST
*item
)
109 WCHAR path
[MAX_PATH
];
111 if(SHGetPathFromIDListW(item
->apidl
[0].pidl
, path
))
112 str
= wine_dbg_sprintf("%s", debugstr_w(path
));
114 str
= wine_dbg_sprintf("<not a disk file>" );
118 static void DeleteNode(LPNOTIFICATIONLIST item
)
122 TRACE("item=%p\n", item
);
124 /* remove item from list */
125 list_remove( &item
->entry
);
128 for (i
=0; i
<item
->cidl
; i
++)
129 SHFree((LPITEMIDLIST
)item
->apidl
[i
].pidl
);
134 void InitChangeNotifications(void)
138 void FreeChangeNotifications(void)
140 LPNOTIFICATIONLIST ptr
, next
;
144 EnterCriticalSection(&SHELL32_ChangenotifyCS
);
146 LIST_FOR_EACH_ENTRY_SAFE( ptr
, next
, ¬ifications
, NOTIFICATIONLIST
, entry
)
149 LeaveCriticalSection(&SHELL32_ChangenotifyCS
);
151 DeleteCriticalSection(&SHELL32_ChangenotifyCS
);
154 /*************************************************************************
155 * SHChangeNotifyRegister [SHELL32.2]
159 SHChangeNotifyRegister(
165 SHChangeNotifyEntry
*lpItems
)
167 LPNOTIFICATIONLIST item
;
170 item
= SHAlloc(sizeof(NOTIFICATIONLIST
));
172 TRACE("(%p,0x%08x,0x%08x,0x%08x,%d,%p) item=%p\n",
173 hwnd
, fSources
, wEventMask
, uMsg
, cItems
, lpItems
, item
);
176 item
->apidl
= SHAlloc(sizeof(SHChangeNotifyEntry
) * cItems
);
177 for(i
=0;i
<cItems
;i
++)
179 item
->apidl
[i
].pidl
= ILClone(lpItems
[i
].pidl
);
180 item
->apidl
[i
].fRecursive
= lpItems
[i
].fRecursive
;
184 item
->wEventMask
= wEventMask
;
185 item
->dwFlags
= fSources
;
186 item
->id
= InterlockedIncrement( &next_id
);
188 TRACE("new node: %s\n", NodeName( item
));
190 EnterCriticalSection(&SHELL32_ChangenotifyCS
);
192 list_add_tail( ¬ifications
, &item
->entry
);
194 LeaveCriticalSection(&SHELL32_ChangenotifyCS
);
199 /*************************************************************************
200 * SHChangeNotifyDeregister [SHELL32.4]
202 BOOL WINAPI
SHChangeNotifyDeregister(ULONG hNotify
)
204 LPNOTIFICATIONLIST node
;
206 TRACE("(0x%08x)\n", hNotify
);
208 EnterCriticalSection(&SHELL32_ChangenotifyCS
);
210 LIST_FOR_EACH_ENTRY( node
, ¬ifications
, NOTIFICATIONLIST
, entry
)
212 if (node
->id
== hNotify
)
215 LeaveCriticalSection(&SHELL32_ChangenotifyCS
);
219 LeaveCriticalSection(&SHELL32_ChangenotifyCS
);
223 /*************************************************************************
224 * SHChangeNotifyUpdateEntryList [SHELL32.5]
226 BOOL WINAPI
SHChangeNotifyUpdateEntryList(DWORD unknown1
, DWORD unknown2
,
227 DWORD unknown3
, DWORD unknown4
)
229 FIXME("(0x%08x, 0x%08x, 0x%08x, 0x%08x)\n",
230 unknown1
, unknown2
, unknown3
, unknown4
);
235 struct new_delivery_notification
240 LPITEMIDLIST pidls
[2];
244 static BOOL
should_notify( LPCITEMIDLIST changed
, LPCITEMIDLIST watched
, BOOL sub
)
246 TRACE("%p %p %d\n", changed
, watched
, sub
);
249 if (ILIsEqual( watched
, changed
) )
251 if( sub
&& ILIsParent( watched
, changed
, FALSE
) )
256 /*************************************************************************
257 * SHChangeNotify [SHELL32.@]
259 void WINAPI
SHChangeNotify(LONG wEventId
, UINT uFlags
, LPCVOID dwItem1
, LPCVOID dwItem2
)
261 struct notification_recipients
{
268 HANDLE shared_data
= NULL
;
269 LPITEMIDLIST Pidls
[2];
270 LPNOTIFICATIONLIST ptr
;
271 struct list recipients
;
276 TRACE("(0x%08x,0x%08x,%p,%p)\n", wEventId
, uFlags
, dwItem1
, dwItem2
);
278 if(uFlags
& ~(SHCNF_TYPE
|SHCNF_FLUSH
))
279 FIXME("ignoring unsupported flags: %x\n", uFlags
);
281 if( ( wEventId
& SHCNE_NOITEMEVENTS
) && ( dwItem1
|| dwItem2
) )
283 TRACE("dwItem1 and dwItem2 are not zero, but should be\n");
288 else if( ( wEventId
& SHCNE_ONEITEMEVENTS
) && dwItem2
)
290 TRACE("dwItem2 is not zero, but should be\n");
295 if( ( ( wEventId
& SHCNE_NOITEMEVENTS
) &&
296 ( wEventId
& ~SHCNE_NOITEMEVENTS
) ) ||
297 ( ( wEventId
& SHCNE_ONEITEMEVENTS
) &&
298 ( wEventId
& ~SHCNE_ONEITEMEVENTS
) ) ||
299 ( ( wEventId
& SHCNE_TWOITEMEVENTS
) &&
300 ( wEventId
& ~SHCNE_TWOITEMEVENTS
) ) )
302 WARN("mutually incompatible events listed\n");
306 /* convert paths in IDLists*/
307 switch (uFlags
& SHCNF_TYPE
)
310 if (dwItem1
) Pidls
[0] = SHSimpleIDListFromPathA(dwItem1
);
311 if (dwItem2
) Pidls
[1] = SHSimpleIDListFromPathA(dwItem2
);
314 if (dwItem1
) Pidls
[0] = SHSimpleIDListFromPathW(dwItem1
);
315 if (dwItem2
) Pidls
[1] = SHSimpleIDListFromPathW(dwItem2
);
318 Pidls
[0] = ILClone(dwItem1
);
319 Pidls
[1] = ILClone(dwItem2
);
323 FIXME("SHChangeNotify with (uFlags & SHCNF_PRINTER)\n");
327 FIXME("unknown type %08x\n", uFlags
& SHCNF_TYPE
);
331 list_init(&recipients
);
332 EnterCriticalSection(&SHELL32_ChangenotifyCS
);
333 LIST_FOR_EACH_ENTRY( ptr
, ¬ifications
, NOTIFICATIONLIST
, entry
)
335 struct notification_recipients
*item
;
339 for( i
=0; (i
<ptr
->cidl
) && !notify
; i
++ )
341 LPCITEMIDLIST pidl
= ptr
->apidl
[i
].pidl
;
342 BOOL subtree
= ptr
->apidl
[i
].fRecursive
;
344 if (wEventId
& ptr
->wEventMask
)
346 if( !pidl
) /* all ? */
348 else if( wEventId
& SHCNE_NOITEMEVENTS
)
350 else if( wEventId
& ( SHCNE_ONEITEMEVENTS
| SHCNE_TWOITEMEVENTS
) )
351 notify
= should_notify( Pidls
[0], pidl
, subtree
);
352 else if( wEventId
& SHCNE_TWOITEMEVENTS
)
353 notify
= should_notify( Pidls
[1], pidl
, subtree
);
360 item
= SHAlloc(sizeof(struct notification_recipients
));
362 ERR("out of memory\n");
366 item
->hwnd
= ptr
->hwnd
;
367 item
->msg
= ptr
->uMsg
;
368 item
->flags
= ptr
->dwFlags
;
369 list_add_tail(&recipients
, &item
->entry
);
371 LeaveCriticalSection(&SHELL32_ChangenotifyCS
);
373 LIST_FOR_EACH_ENTRY_SAFE(cur
, next
, &recipients
, struct notification_recipients
, entry
)
375 TRACE("notifying %p, event %s(%x)\n", cur
->hwnd
, DumpEvent(wEventId
), wEventId
);
377 if (cur
->flags
& SHCNRF_NewDelivery
) {
379 struct new_delivery_notification
*notification
;
380 UINT size1
= ILGetSize(Pidls
[0]), size2
= ILGetSize(Pidls
[1]);
381 UINT offset
= (size1
+sizeof(int)-1)/sizeof(int)*sizeof(int);
383 notification
= SHAlloc(sizeof(struct new_delivery_notification
)+offset
+size2
);
385 ERR("out of memory\n");
387 notification
->event
= wEventId
;
388 notification
->pidl1_size
= size1
;
389 notification
->pidl2_size
= size2
;
391 memcpy(notification
->data
, Pidls
[0], size1
);
393 memcpy(notification
->data
+offset
, Pidls
[1], size2
);
395 shared_data
= SHAllocShared(notification
,
396 sizeof(struct new_delivery_notification
)+size1
+size2
,
397 GetCurrentProcessId());
398 SHFree(notification
);
403 SendMessageA(cur
->hwnd
, cur
->msg
, (WPARAM
)shared_data
, GetCurrentProcessId());
405 ERR("out of memory\n");
407 SendMessageA(cur
->hwnd
, cur
->msg
, (WPARAM
)Pidls
, wEventId
);
410 list_remove(&cur
->entry
);
413 SHFreeShared(shared_data
, GetCurrentProcessId());
417 if (wEventId
& SHCNE_ASSOCCHANGED
)
419 static const WCHAR args
[] = {' ','-','a',0 };
420 TRACE("refreshing file type associations\n");
421 run_winemenubuilder( args
);
425 /*************************************************************************
426 * NTSHChangeNotifyRegister [SHELL32.640]
428 * Idlist is an array of structures and Count specifies how many items in the array
429 * (usually just one I think).
431 DWORD WINAPI
NTSHChangeNotifyRegister(
437 SHChangeNotifyEntry
*idlist
)
439 FIXME("(%p,0x%08x,0x%08x,0x%08x,0x%08x,%p):semi stub.\n",
440 hwnd
,events1
,events2
,msg
,count
,idlist
);
442 return SHChangeNotifyRegister(hwnd
, events1
, events2
, msg
, count
, idlist
);
445 /*************************************************************************
446 * SHChangeNotification_Lock [SHELL32.644]
448 HANDLE WINAPI
SHChangeNotification_Lock(
451 LPITEMIDLIST
**lppidls
,
454 struct new_delivery_notification
*ndn
;
457 TRACE("%p %08x %p %p\n", hChange
, dwProcessId
, lppidls
, lpwEventId
);
459 ndn
= SHLockShared(hChange
, dwProcessId
);
461 WARN("SHLockShared failed\n");
466 offset
= (ndn
->pidl1_size
+sizeof(int)-1)/sizeof(int)*sizeof(int);
467 ndn
->pidls
[0] = ndn
->pidl1_size
? (LPITEMIDLIST
)ndn
->data
: NULL
;
468 ndn
->pidls
[1] = ndn
->pidl2_size
? (LPITEMIDLIST
)(ndn
->data
+offset
) : NULL
;
469 *lppidls
= ndn
->pidls
;
473 *lpwEventId
= ndn
->event
;
478 /*************************************************************************
479 * SHChangeNotification_Unlock [SHELL32.645]
481 BOOL WINAPI
SHChangeNotification_Unlock ( HANDLE hLock
)
483 TRACE("%p\n", hLock
);
484 return SHUnlockShared(hLock
);
487 /*************************************************************************
488 * NTSHChangeNotifyDeregister [SHELL32.641]
490 DWORD WINAPI
NTSHChangeNotifyDeregister(ULONG x1
)
492 FIXME("(0x%08x):semi stub.\n",x1
);
494 return SHChangeNotifyDeregister( x1
);