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"
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
49 HWND hwnd
; /* window to notify */
50 DWORD uMsg
; /* message to send */
51 LPNOTIFYREGISTER apidl
; /* array of entries to watch*/
52 UINT cidl
; /* number of pidls in array */
53 LONG wEventMask
; /* subscribed events */
54 DWORD dwFlags
; /* client flags */
56 } NOTIFICATIONLIST
, *LPNOTIFICATIONLIST
;
58 static struct list notifications
= LIST_INIT( notifications
);
61 #define SHCNE_NOITEMEVENTS ( \
64 #define SHCNE_ONEITEMEVENTS ( \
65 SHCNE_ATTRIBUTES | SHCNE_CREATE | SHCNE_DELETE | SHCNE_DRIVEADD | \
66 SHCNE_DRIVEADDGUI | SHCNE_DRIVEREMOVED | SHCNE_FREESPACE | \
67 SHCNE_MEDIAINSERTED | SHCNE_MEDIAREMOVED | SHCNE_MKDIR | \
68 SHCNE_NETSHARE | SHCNE_NETUNSHARE | SHCNE_RMDIR | \
69 SHCNE_SERVERDISCONNECT | SHCNE_UPDATEDIR | SHCNE_UPDATEIMAGE | \
72 #define SHCNE_TWOITEMEVENTS ( \
73 SHCNE_RENAMEFOLDER | SHCNE_RENAMEITEM )
75 /* for dumping events */
76 static const char * DumpEvent( LONG event
)
78 if( event
== SHCNE_ALLEVENTS
)
79 return "SHCNE_ALLEVENTS";
80 #define DUMPEV(x) ,( event & SHCNE_##x )? #x " " : ""
81 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"
96 DUMPEV(SERVERDISCONNECT
)
101 DUMPEV(EXTENDED_EVENT
)
108 static const char * NodeName(const NOTIFICATIONLIST
*item
)
111 WCHAR path
[MAX_PATH
];
113 if(SHGetPathFromIDListW(item
->apidl
[0].pidl
, path
))
114 str
= wine_dbg_sprintf("%s", debugstr_w(path
));
116 str
= wine_dbg_sprintf("<not a disk file>" );
120 static void DeleteNode(LPNOTIFICATIONLIST item
)
124 TRACE("item=%p\n", item
);
126 /* remove item from list */
127 list_remove( &item
->entry
);
130 for (i
=0; i
<item
->cidl
; i
++)
131 SHFree((LPITEMIDLIST
)item
->apidl
[i
].pidl
);
136 void InitChangeNotifications(void)
140 void FreeChangeNotifications(void)
142 LPNOTIFICATIONLIST ptr
, next
;
146 EnterCriticalSection(&SHELL32_ChangenotifyCS
);
148 LIST_FOR_EACH_ENTRY_SAFE( ptr
, next
, ¬ifications
, NOTIFICATIONLIST
, entry
)
151 LeaveCriticalSection(&SHELL32_ChangenotifyCS
);
153 DeleteCriticalSection(&SHELL32_ChangenotifyCS
);
156 /*************************************************************************
157 * SHChangeNotifyRegister [SHELL32.2]
161 SHChangeNotifyRegister(
167 SHChangeNotifyEntry
*lpItems
)
169 LPNOTIFICATIONLIST item
;
172 item
= SHAlloc(sizeof(NOTIFICATIONLIST
));
174 TRACE("(%p,0x%08x,0x%08lx,0x%08x,%d,%p) item=%p\n",
175 hwnd
, fSources
, wEventMask
, uMsg
, cItems
, lpItems
, item
);
178 item
->apidl
= SHAlloc(sizeof(SHChangeNotifyEntry
) * cItems
);
179 for(i
=0;i
<cItems
;i
++)
181 item
->apidl
[i
].pidl
= ILClone(lpItems
[i
].pidl
);
182 item
->apidl
[i
].fRecursive
= lpItems
[i
].fRecursive
;
186 item
->wEventMask
= wEventMask
;
187 item
->dwFlags
= fSources
;
188 item
->id
= InterlockedIncrement( &next_id
);
190 TRACE("new node: %s\n", NodeName( item
));
192 EnterCriticalSection(&SHELL32_ChangenotifyCS
);
194 list_add_tail( ¬ifications
, &item
->entry
);
196 LeaveCriticalSection(&SHELL32_ChangenotifyCS
);
201 /*************************************************************************
202 * SHChangeNotifyDeregister [SHELL32.4]
204 BOOL WINAPI
SHChangeNotifyDeregister(ULONG hNotify
)
206 LPNOTIFICATIONLIST node
;
208 TRACE("(0x%08lx)\n", hNotify
);
210 EnterCriticalSection(&SHELL32_ChangenotifyCS
);
212 LIST_FOR_EACH_ENTRY( node
, ¬ifications
, NOTIFICATIONLIST
, entry
)
214 if (node
->id
== hNotify
)
217 LeaveCriticalSection(&SHELL32_ChangenotifyCS
);
221 LeaveCriticalSection(&SHELL32_ChangenotifyCS
);
225 /*************************************************************************
226 * SHChangeNotifyUpdateEntryList [SHELL32.5]
228 BOOL WINAPI
SHChangeNotifyUpdateEntryList(DWORD unknown1
, DWORD unknown2
,
229 DWORD unknown3
, DWORD unknown4
)
231 FIXME("(0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx)\n",
232 unknown1
, unknown2
, unknown3
, unknown4
);
237 struct new_delivery_notification
242 LPITEMIDLIST pidls
[2];
246 static BOOL
should_notify( LPCITEMIDLIST changed
, LPCITEMIDLIST watched
, BOOL sub
)
248 TRACE("%p %p %d\n", changed
, watched
, sub
);
251 if (ILIsEqual( watched
, changed
) )
253 if( sub
&& ILIsParent( watched
, changed
, FALSE
) )
258 /*************************************************************************
259 * SHChangeNotify [SHELL32.@]
261 void WINAPI
SHChangeNotify(LONG wEventId
, UINT uFlags
, LPCVOID dwItem1
, LPCVOID dwItem2
)
263 struct notification_recipients
{
270 HANDLE shared_data
= NULL
;
271 LPITEMIDLIST Pidls
[2];
272 LPNOTIFICATIONLIST ptr
;
273 struct list recipients
;
278 TRACE("(0x%08lx,0x%08x,%p,%p)\n", wEventId
, uFlags
, dwItem1
, dwItem2
);
280 if(uFlags
& ~(SHCNF_TYPE
|SHCNF_FLUSH
))
281 FIXME("ignoring unsupported flags: %x\n", uFlags
);
283 if( ( wEventId
& SHCNE_NOITEMEVENTS
) && ( dwItem1
|| dwItem2
) )
285 TRACE("dwItem1 and dwItem2 are not zero, but should be\n");
290 else if( ( wEventId
& SHCNE_ONEITEMEVENTS
) && dwItem2
)
292 TRACE("dwItem2 is not zero, but should be\n");
297 if( ( ( wEventId
& SHCNE_NOITEMEVENTS
) &&
298 ( wEventId
& ~SHCNE_NOITEMEVENTS
) ) ||
299 ( ( wEventId
& SHCNE_ONEITEMEVENTS
) &&
300 ( wEventId
& ~SHCNE_ONEITEMEVENTS
) ) ||
301 ( ( wEventId
& SHCNE_TWOITEMEVENTS
) &&
302 ( wEventId
& ~SHCNE_TWOITEMEVENTS
) ) )
304 WARN("mutually incompatible events listed\n");
308 /* convert paths in IDLists*/
309 switch (uFlags
& SHCNF_TYPE
)
312 if (dwItem1
) Pidls
[0] = SHSimpleIDListFromPathA(dwItem1
);
313 if (dwItem2
) Pidls
[1] = SHSimpleIDListFromPathA(dwItem2
);
316 if (dwItem1
) Pidls
[0] = SHSimpleIDListFromPathW(dwItem1
);
317 if (dwItem2
) Pidls
[1] = SHSimpleIDListFromPathW(dwItem2
);
320 Pidls
[0] = ILClone(dwItem1
);
321 Pidls
[1] = ILClone(dwItem2
);
325 FIXME("SHChangeNotify with (uFlags & SHCNF_PRINTER)\n");
329 FIXME("unknown type %08x\n", uFlags
& SHCNF_TYPE
);
333 list_init(&recipients
);
334 EnterCriticalSection(&SHELL32_ChangenotifyCS
);
335 LIST_FOR_EACH_ENTRY( ptr
, ¬ifications
, NOTIFICATIONLIST
, entry
)
337 struct notification_recipients
*item
;
341 for( i
=0; (i
<ptr
->cidl
) && !notify
; i
++ )
343 LPCITEMIDLIST pidl
= ptr
->apidl
[i
].pidl
;
344 BOOL subtree
= ptr
->apidl
[i
].fRecursive
;
346 if (wEventId
& ptr
->wEventMask
)
348 if( !pidl
) /* all ? */
350 else if( wEventId
& SHCNE_NOITEMEVENTS
)
352 else if( wEventId
& ( SHCNE_ONEITEMEVENTS
| SHCNE_TWOITEMEVENTS
) )
353 notify
= should_notify( Pidls
[0], pidl
, subtree
);
354 else if( wEventId
& SHCNE_TWOITEMEVENTS
)
355 notify
= should_notify( Pidls
[1], pidl
, subtree
);
362 item
= SHAlloc(sizeof(struct notification_recipients
));
364 ERR("out of memory\n");
368 item
->hwnd
= ptr
->hwnd
;
369 item
->msg
= ptr
->uMsg
;
370 item
->flags
= ptr
->dwFlags
;
371 list_add_tail(&recipients
, &item
->entry
);
373 LeaveCriticalSection(&SHELL32_ChangenotifyCS
);
375 LIST_FOR_EACH_ENTRY_SAFE(cur
, next
, &recipients
, struct notification_recipients
, entry
)
377 TRACE("notifying %p, event %s(%lx)\n", cur
->hwnd
, DumpEvent(wEventId
), wEventId
);
379 if (cur
->flags
& SHCNRF_NewDelivery
) {
381 struct new_delivery_notification
*notification
;
382 UINT size1
= ILGetSize(Pidls
[0]), size2
= ILGetSize(Pidls
[1]);
383 UINT offset
= (size1
+sizeof(int)-1)/sizeof(int)*sizeof(int);
385 notification
= SHAlloc(sizeof(struct new_delivery_notification
)+offset
+size2
);
387 ERR("out of memory\n");
389 notification
->event
= wEventId
;
390 notification
->pidl1_size
= size1
;
391 notification
->pidl2_size
= size2
;
393 memcpy(notification
->data
, Pidls
[0], size1
);
395 memcpy(notification
->data
+offset
, Pidls
[1], size2
);
397 shared_data
= SHAllocShared(notification
,
398 sizeof(struct new_delivery_notification
)+size1
+size2
,
399 GetCurrentProcessId());
400 SHFree(notification
);
405 SendMessageA(cur
->hwnd
, cur
->msg
, (WPARAM
)shared_data
, GetCurrentProcessId());
407 ERR("out of memory\n");
409 SendMessageA(cur
->hwnd
, cur
->msg
, (WPARAM
)Pidls
, wEventId
);
412 list_remove(&cur
->entry
);
415 SHFreeShared(shared_data
, GetCurrentProcessId());
419 if (wEventId
& SHCNE_ASSOCCHANGED
)
421 TRACE("refreshing file type associations\n");
422 run_winemenubuilder( L
" -a" );
426 /*************************************************************************
427 * NTSHChangeNotifyRegister [SHELL32.640]
429 * Idlist is an array of structures and Count specifies how many items in the array
430 * (usually just one I think).
432 DWORD WINAPI
NTSHChangeNotifyRegister(
438 SHChangeNotifyEntry
*idlist
)
440 FIXME("(%p,0x%08lx,0x%08lx,0x%08lx,0x%08x,%p):semi stub.\n",
441 hwnd
,events1
,events2
,msg
,count
,idlist
);
443 return SHChangeNotifyRegister(hwnd
, events1
, events2
, msg
, count
, idlist
);
446 /*************************************************************************
447 * SHChangeNotification_Lock [SHELL32.644]
449 HANDLE WINAPI
SHChangeNotification_Lock(
452 LPITEMIDLIST
**lppidls
,
455 struct new_delivery_notification
*ndn
;
458 TRACE("%p %08lx %p %p\n", hChange
, dwProcessId
, lppidls
, lpwEventId
);
460 ndn
= SHLockShared(hChange
, dwProcessId
);
462 WARN("SHLockShared failed\n");
467 offset
= (ndn
->pidl1_size
+sizeof(int)-1)/sizeof(int)*sizeof(int);
468 ndn
->pidls
[0] = ndn
->pidl1_size
? (LPITEMIDLIST
)ndn
->data
: NULL
;
469 ndn
->pidls
[1] = ndn
->pidl2_size
? (LPITEMIDLIST
)(ndn
->data
+offset
) : NULL
;
470 *lppidls
= ndn
->pidls
;
474 *lpwEventId
= ndn
->event
;
479 /*************************************************************************
480 * SHChangeNotification_Unlock [SHELL32.645]
482 BOOL WINAPI
SHChangeNotification_Unlock ( HANDLE hLock
)
484 TRACE("%p\n", hLock
);
485 return SHUnlockShared(hLock
);
488 /*************************************************************************
489 * NTSHChangeNotifyDeregister [SHELL32.641]
491 DWORD WINAPI
NTSHChangeNotifyDeregister(ULONG x1
)
493 FIXME("(0x%08lx):semi stub.\n",x1
);
495 return SHChangeNotifyDeregister( x1
);