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 )
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(const NOTIFICATIONLIST
*item
)
108 WCHAR path
[MAX_PATH
];
110 if(SHGetPathFromIDListW(item
->apidl
[0].pidl
, path
))
111 str
= wine_dbg_sprintf("%s", debugstr_w(path
));
113 str
= wine_dbg_sprintf("<not a disk file>" );
117 static void DeleteNode(LPNOTIFICATIONLIST item
)
121 TRACE("item=%p\n", item
);
123 /* remove item from list */
124 list_remove( &item
->entry
);
127 for (i
=0; i
<item
->cidl
; i
++)
128 SHFree((LPITEMIDLIST
)item
->apidl
[i
].pidl
);
133 void InitChangeNotifications(void)
137 void FreeChangeNotifications(void)
139 LPNOTIFICATIONLIST ptr
, next
;
143 EnterCriticalSection(&SHELL32_ChangenotifyCS
);
145 LIST_FOR_EACH_ENTRY_SAFE( ptr
, next
, ¬ifications
, NOTIFICATIONLIST
, entry
)
148 LeaveCriticalSection(&SHELL32_ChangenotifyCS
);
150 DeleteCriticalSection(&SHELL32_ChangenotifyCS
);
153 /*************************************************************************
154 * SHChangeNotifyRegister [SHELL32.2]
158 SHChangeNotifyRegister(
164 SHChangeNotifyEntry
*lpItems
)
166 LPNOTIFICATIONLIST item
;
169 item
= SHAlloc(sizeof(NOTIFICATIONLIST
));
171 TRACE("(%p,0x%08x,0x%08x,0x%08x,%d,%p) item=%p\n",
172 hwnd
, fSources
, wEventMask
, uMsg
, cItems
, lpItems
, item
);
175 item
->apidl
= SHAlloc(sizeof(SHChangeNotifyEntry
) * cItems
);
176 for(i
=0;i
<cItems
;i
++)
178 item
->apidl
[i
].pidl
= ILClone(lpItems
[i
].pidl
);
179 item
->apidl
[i
].fRecursive
= lpItems
[i
].fRecursive
;
183 item
->wEventMask
= wEventMask
;
184 item
->dwFlags
= fSources
;
185 item
->id
= InterlockedIncrement( &next_id
);
187 TRACE("new node: %s\n", NodeName( item
));
189 EnterCriticalSection(&SHELL32_ChangenotifyCS
);
191 list_add_tail( ¬ifications
, &item
->entry
);
193 LeaveCriticalSection(&SHELL32_ChangenotifyCS
);
198 /*************************************************************************
199 * SHChangeNotifyDeregister [SHELL32.4]
201 BOOL WINAPI
SHChangeNotifyDeregister(ULONG hNotify
)
203 LPNOTIFICATIONLIST node
;
205 TRACE("(0x%08x)\n", hNotify
);
207 EnterCriticalSection(&SHELL32_ChangenotifyCS
);
209 LIST_FOR_EACH_ENTRY( node
, ¬ifications
, NOTIFICATIONLIST
, entry
)
211 if (node
->id
== hNotify
)
214 LeaveCriticalSection(&SHELL32_ChangenotifyCS
);
218 LeaveCriticalSection(&SHELL32_ChangenotifyCS
);
222 /*************************************************************************
223 * SHChangeNotifyUpdateEntryList [SHELL32.5]
225 BOOL WINAPI
SHChangeNotifyUpdateEntryList(DWORD unknown1
, DWORD unknown2
,
226 DWORD unknown3
, DWORD unknown4
)
228 FIXME("(0x%08x, 0x%08x, 0x%08x, 0x%08x)\n",
229 unknown1
, unknown2
, unknown3
, unknown4
);
234 struct new_delivery_notification
239 LPITEMIDLIST pidls
[2];
243 static BOOL
should_notify( LPCITEMIDLIST changed
, LPCITEMIDLIST watched
, BOOL sub
)
245 TRACE("%p %p %d\n", changed
, watched
, sub
);
248 if (ILIsEqual( watched
, changed
) )
250 if( sub
&& ILIsParent( watched
, changed
, FALSE
) )
255 /*************************************************************************
256 * SHChangeNotify [SHELL32.@]
258 void WINAPI
SHChangeNotify(LONG wEventId
, UINT uFlags
, LPCVOID dwItem1
, LPCVOID dwItem2
)
260 struct notification_recipients
{
267 HANDLE shared_data
= NULL
;
268 LPITEMIDLIST Pidls
[2];
269 LPNOTIFICATIONLIST ptr
;
270 struct list recipients
;
275 TRACE("(0x%08x,0x%08x,%p,%p)\n", wEventId
, uFlags
, dwItem1
, dwItem2
);
277 if(uFlags
& ~(SHCNF_TYPE
|SHCNF_FLUSH
))
278 FIXME("ignoring unsupported flags: %x\n", uFlags
);
280 if( ( wEventId
& SHCNE_NOITEMEVENTS
) && ( dwItem1
|| dwItem2
) )
282 TRACE("dwItem1 and dwItem2 are not zero, but should be\n");
287 else if( ( wEventId
& SHCNE_ONEITEMEVENTS
) && dwItem2
)
289 TRACE("dwItem2 is not zero, but should be\n");
294 if( ( ( wEventId
& SHCNE_NOITEMEVENTS
) &&
295 ( wEventId
& ~SHCNE_NOITEMEVENTS
) ) ||
296 ( ( wEventId
& SHCNE_ONEITEMEVENTS
) &&
297 ( wEventId
& ~SHCNE_ONEITEMEVENTS
) ) ||
298 ( ( wEventId
& SHCNE_TWOITEMEVENTS
) &&
299 ( wEventId
& ~SHCNE_TWOITEMEVENTS
) ) )
301 WARN("mutually incompatible events listed\n");
305 /* convert paths in IDLists*/
306 switch (uFlags
& SHCNF_TYPE
)
309 if (dwItem1
) Pidls
[0] = SHSimpleIDListFromPathA(dwItem1
);
310 if (dwItem2
) Pidls
[1] = SHSimpleIDListFromPathA(dwItem2
);
313 if (dwItem1
) Pidls
[0] = SHSimpleIDListFromPathW(dwItem1
);
314 if (dwItem2
) Pidls
[1] = SHSimpleIDListFromPathW(dwItem2
);
317 Pidls
[0] = ILClone(dwItem1
);
318 Pidls
[1] = ILClone(dwItem2
);
322 FIXME("SHChangeNotify with (uFlags & SHCNF_PRINTER)\n");
326 FIXME("unknown type %08x\n", uFlags
& SHCNF_TYPE
);
330 list_init(&recipients
);
331 EnterCriticalSection(&SHELL32_ChangenotifyCS
);
332 LIST_FOR_EACH_ENTRY( ptr
, ¬ifications
, NOTIFICATIONLIST
, entry
)
334 struct notification_recipients
*item
;
338 for( i
=0; (i
<ptr
->cidl
) && !notify
; i
++ )
340 LPCITEMIDLIST pidl
= ptr
->apidl
[i
].pidl
;
341 BOOL subtree
= ptr
->apidl
[i
].fRecursive
;
343 if (wEventId
& ptr
->wEventMask
)
345 if( !pidl
) /* all ? */
347 else if( wEventId
& SHCNE_NOITEMEVENTS
)
349 else if( wEventId
& ( SHCNE_ONEITEMEVENTS
| SHCNE_TWOITEMEVENTS
) )
350 notify
= should_notify( Pidls
[0], pidl
, subtree
);
351 else if( wEventId
& SHCNE_TWOITEMEVENTS
)
352 notify
= should_notify( Pidls
[1], pidl
, subtree
);
359 item
= SHAlloc(sizeof(struct notification_recipients
));
361 ERR("out of memory\n");
365 item
->hwnd
= ptr
->hwnd
;
366 item
->msg
= ptr
->uMsg
;
367 item
->flags
= ptr
->dwFlags
;
368 list_add_tail(&recipients
, &item
->entry
);
370 LeaveCriticalSection(&SHELL32_ChangenotifyCS
);
372 LIST_FOR_EACH_ENTRY_SAFE(cur
, next
, &recipients
, struct notification_recipients
, entry
)
374 TRACE("notifying %p, event %s(%x)\n", cur
->hwnd
, DumpEvent(wEventId
), wEventId
);
376 if (cur
->flags
& SHCNRF_NewDelivery
) {
378 struct new_delivery_notification
*notification
;
379 UINT size1
= ILGetSize(Pidls
[0]), size2
= ILGetSize(Pidls
[1]);
380 UINT offset
= (size1
+sizeof(int)-1)/sizeof(int)*sizeof(int);
382 notification
= SHAlloc(sizeof(struct new_delivery_notification
)+offset
+size2
);
384 ERR("out of memory\n");
386 notification
->event
= wEventId
;
387 notification
->pidl1_size
= size1
;
388 notification
->pidl2_size
= size2
;
390 memcpy(notification
->data
, Pidls
[0], size1
);
392 memcpy(notification
->data
+offset
, Pidls
[1], size2
);
394 shared_data
= SHAllocShared(notification
,
395 sizeof(struct new_delivery_notification
)+size1
+size2
,
396 GetCurrentProcessId());
397 SHFree(notification
);
402 SendMessageA(cur
->hwnd
, cur
->msg
, (WPARAM
)shared_data
, GetCurrentProcessId());
404 ERR("out of memory\n");
406 SendMessageA(cur
->hwnd
, cur
->msg
, (WPARAM
)Pidls
, wEventId
);
409 list_remove(&cur
->entry
);
412 SHFreeShared(shared_data
, GetCurrentProcessId());
416 if (wEventId
& SHCNE_ASSOCCHANGED
)
418 static const WCHAR args
[] = {' ','-','a',0 };
419 TRACE("refreshing file type associations\n");
420 run_winemenubuilder( args
);
424 /*************************************************************************
425 * NTSHChangeNotifyRegister [SHELL32.640]
427 * Idlist is an array of structures and Count specifies how many items in the array
428 * (usually just one I think).
430 DWORD WINAPI
NTSHChangeNotifyRegister(
436 SHChangeNotifyEntry
*idlist
)
438 FIXME("(%p,0x%08x,0x%08x,0x%08x,0x%08x,%p):semi stub.\n",
439 hwnd
,events1
,events2
,msg
,count
,idlist
);
441 return SHChangeNotifyRegister(hwnd
, events1
, events2
, msg
, count
, idlist
);
444 /*************************************************************************
445 * SHChangeNotification_Lock [SHELL32.644]
447 HANDLE WINAPI
SHChangeNotification_Lock(
450 LPITEMIDLIST
**lppidls
,
453 struct new_delivery_notification
*ndn
;
456 TRACE("%p %08x %p %p\n", hChange
, dwProcessId
, lppidls
, lpwEventId
);
458 ndn
= SHLockShared(hChange
, dwProcessId
);
460 WARN("SHLockShared failed\n");
465 offset
= (ndn
->pidl1_size
+sizeof(int)-1)/sizeof(int)*sizeof(int);
466 ndn
->pidls
[0] = ndn
->pidl1_size
? (LPITEMIDLIST
)ndn
->data
: NULL
;
467 ndn
->pidls
[1] = ndn
->pidl2_size
? (LPITEMIDLIST
)(ndn
->data
+offset
) : NULL
;
468 *lppidls
= ndn
->pidls
;
472 *lpwEventId
= ndn
->event
;
477 /*************************************************************************
478 * SHChangeNotification_Unlock [SHELL32.645]
480 BOOL WINAPI
SHChangeNotification_Unlock ( HANDLE hLock
)
482 TRACE("%p\n", hLock
);
483 return SHUnlockShared(hLock
);
486 /*************************************************************************
487 * NTSHChangeNotifyDeregister [SHELL32.641]
489 DWORD WINAPI
NTSHChangeNotifyDeregister(ULONG x1
)
491 FIXME("(0x%08x):semi stub.\n",x1
);
493 return SHChangeNotifyDeregister( x1
);