Change protoype of SHChangeNotifyRegister to what MSDN says.
[wine.git] / dlls / shell32 / changenotify.c
blob673698187244ed4c9b4b1965cab12d7844eb29ed
1 /*
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
21 #include <string.h>
23 #include "wine/debug.h"
24 #include "pidl.h"
25 #include "shell32_main.h"
26 #include "undocshell.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(shell);
30 static CRITICAL_SECTION SHELL32_ChangenotifyCS;
31 static CRITICAL_SECTION_DEBUG critsect_debug =
33 0, 0, &SHELL32_ChangenotifyCS,
34 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
35 0, 0, { 0, (DWORD)(__FILE__ ": SHELL32_ChangenotifyCS") }
37 static CRITICAL_SECTION SHELL32_ChangenotifyCS = { &critsect_debug, -1, 0, 0, 0, 0 };
39 /* internal list of notification clients (internal) */
40 typedef struct _NOTIFICATIONLIST
42 struct _NOTIFICATIONLIST *next;
43 struct _NOTIFICATIONLIST *prev;
44 HWND hwnd; /* window to notify */
45 DWORD uMsg; /* message to send */
46 LPNOTIFYREGISTER apidl; /* array of entries to watch*/
47 UINT cidl; /* number of pidls in array */
48 LONG wEventMask; /* subscribed events */
49 LONG wSignalledEvent; /* event that occurred */
50 DWORD dwFlags; /* client flags */
51 LPCITEMIDLIST pidlSignaled; /*pidl of the path that caused the signal*/
53 } NOTIFICATIONLIST, *LPNOTIFICATIONLIST;
55 static NOTIFICATIONLIST *head, *tail;
57 #define SHCNE_NOITEMEVENTS ( \
58 SHCNE_ASSOCCHANGED )
60 #define SHCNE_ONEITEMEVENTS ( \
61 SHCNE_ATTRIBUTES | SHCNE_CREATE | SHCNE_DELETE | SHCNE_DRIVEADD | \
62 SHCNE_DRIVEADDGUI | SHCNE_DRIVEREMOVED | SHCNE_FREESPACE | \
63 SHCNE_MEDIAINSERTED | SHCNE_MEDIAREMOVED | SHCNE_MKDIR | \
64 SHCNE_NETSHARE | SHCNE_NETUNSHARE | SHCNE_RMDIR | \
65 SHCNE_SERVERDISCONNECT | SHCNE_UPDATEDIR | SHCNE_UPDATEIMAGE )
67 #define SHCNE_TWOITEMEVENTS ( \
68 SHCNE_RENAMEFOLDER | SHCNE_RENAMEITEM | SHCNE_UPDATEITEM )
70 /* for dumping events */
71 static const char * DumpEvent( LONG event )
73 if( event == SHCNE_ALLEVENTS )
74 return "SHCNE_ALLEVENTS";
75 #define DUMPEV(x) ,( event & SHCNE_##x )? #x " " : ""
76 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"
77 DUMPEV(RENAMEITEM)
78 DUMPEV(CREATE)
79 DUMPEV(DELETE)
80 DUMPEV(MKDIR)
81 DUMPEV(RMDIR)
82 DUMPEV(MEDIAINSERTED)
83 DUMPEV(MEDIAREMOVED)
84 DUMPEV(DRIVEREMOVED)
85 DUMPEV(DRIVEADD)
86 DUMPEV(NETSHARE)
87 DUMPEV(NETUNSHARE)
88 DUMPEV(ATTRIBUTES)
89 DUMPEV(UPDATEDIR)
90 DUMPEV(UPDATEITEM)
91 DUMPEV(SERVERDISCONNECT)
92 DUMPEV(UPDATEIMAGE)
93 DUMPEV(DRIVEADDGUI)
94 DUMPEV(RENAMEFOLDER)
95 DUMPEV(FREESPACE)
96 DUMPEV(EXTENDED_EVENT)
97 DUMPEV(ASSOCCHANGED)
98 DUMPEV(INTERRUPT)
100 #undef DUMPEV
103 static const char * NodeName(LPNOTIFICATIONLIST item)
105 const char *str;
106 WCHAR path[MAX_PATH];
108 if(SHGetPathFromIDListW(item->apidl[0].pidlPath, path ))
109 str = wine_dbg_sprintf("%s", debugstr_w(path));
110 else
111 str = wine_dbg_sprintf("<not a disk file>" );
112 return str;
115 static void AddNode(LPNOTIFICATIONLIST item)
117 TRACE("item %p\n", item );
119 /* link items */
120 item->prev = tail;
121 item->next = NULL;
122 if( tail )
123 tail->next = item;
124 else
125 head = item;
126 tail = item;
129 static LPNOTIFICATIONLIST FindNode( HANDLE hitem )
131 LPNOTIFICATIONLIST ptr;
132 for( ptr = head; ptr; ptr = ptr->next )
133 if( ptr == (LPNOTIFICATIONLIST) hitem )
134 return ptr;
135 return NULL;
138 static void DeleteNode(LPNOTIFICATIONLIST item)
140 UINT i;
142 TRACE("item=%p prev=%p next=%p\n", item, item->prev, item->next);
144 /* remove item from list */
145 if( item->prev )
146 item->prev->next = item->next;
147 else
148 head = item->next;
149 if( item->next )
150 item->next->prev = item->prev;
151 else
152 tail = item->prev;
154 /* free the item */
155 for (i=0; i<item->cidl; i++)
156 SHFree(item->apidl[i].pidlPath);
157 SHFree(item->apidl);
158 SHFree(item);
161 void InitChangeNotifications(void)
165 void FreeChangeNotifications(void)
167 TRACE("\n");
169 EnterCriticalSection(&SHELL32_ChangenotifyCS);
171 while( head )
172 DeleteNode( head );
174 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
176 DeleteCriticalSection(&SHELL32_ChangenotifyCS);
179 /*************************************************************************
180 * SHChangeNotifyRegister [SHELL32.2]
183 HANDLE WINAPI
184 SHChangeNotifyRegister(
185 HWND hwnd,
186 LONG dwFlags,
187 LONG wEventMask,
188 UINT uMsg,
189 int cItems,
190 LPCNOTIFYREGISTER lpItems)
192 LPNOTIFICATIONLIST item;
193 int i;
195 item = SHAlloc(sizeof(NOTIFICATIONLIST));
197 TRACE("(%p,0x%08lx,0x%08lx,0x%08x,0x%08x,%p) item=%p\n",
198 hwnd, dwFlags, wEventMask, uMsg, cItems, lpItems, item);
200 item->next = NULL;
201 item->prev = NULL;
202 item->cidl = cItems;
203 item->apidl = SHAlloc(sizeof(NOTIFYREGISTER) * cItems);
204 for(i=0;i<cItems;i++)
206 item->apidl[i].pidlPath = ILClone(lpItems[i].pidlPath);
207 item->apidl[i].bWatchSubtree = lpItems[i].bWatchSubtree;
209 item->hwnd = hwnd;
210 item->uMsg = uMsg;
211 item->wEventMask = wEventMask;
212 item->wSignalledEvent = 0;
213 item->dwFlags = dwFlags;
215 TRACE("new node: %s\n", NodeName( item ));
217 EnterCriticalSection(&SHELL32_ChangenotifyCS);
219 AddNode(item);
221 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
223 return (HANDLE)item;
226 /*************************************************************************
227 * SHChangeNotifyDeregister [SHELL32.4]
229 BOOL WINAPI SHChangeNotifyDeregister(HANDLE hNotify)
231 LPNOTIFICATIONLIST node;
233 TRACE("(%p)\n",hNotify);
235 EnterCriticalSection(&SHELL32_ChangenotifyCS);
237 node = FindNode(hNotify);
238 if( node )
239 DeleteNode(node);
241 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
243 return node?TRUE:FALSE;
246 /*************************************************************************
247 * SHChangeNotifyUpdateEntryList [SHELL32.5]
249 BOOL WINAPI SHChangeNotifyUpdateEntryList(DWORD unknown1, DWORD unknown2,
250 DWORD unknown3, DWORD unknown4)
252 FIXME("(0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx)\n",
253 unknown1, unknown2, unknown3, unknown4);
255 return -1;
258 static BOOL should_notify( LPITEMIDLIST changed, LPITEMIDLIST watched, BOOL sub )
260 TRACE("%p %p %d\n", changed, watched, sub );
261 if ( !watched )
262 return FALSE;
263 if (ILIsEqual( watched, changed ) )
264 return TRUE;
265 if( sub && ILIsParent( watched, changed, FALSE ) )
266 return TRUE;
267 return FALSE;
270 /*************************************************************************
271 * SHChangeNotify [SHELL32.@]
273 void WINAPI SHChangeNotify(LONG wEventId, UINT uFlags, LPCVOID dwItem1, LPCVOID dwItem2)
275 LPITEMIDLIST Pidls[2];
276 LPNOTIFICATIONLIST ptr;
277 DWORD dummy;
278 UINT typeFlag = uFlags & SHCNF_TYPE;
280 Pidls[0] = NULL;
281 Pidls[1] = NULL;
283 TRACE("(0x%08lx,0x%08x,%p,%p):stub.\n", wEventId, uFlags, dwItem1, dwItem2);
285 if( ( wEventId & SHCNE_NOITEMEVENTS ) && ( dwItem1 || dwItem2 ) )
287 TRACE("dwItem1 and dwItem2 are not zero, but should be\n");
288 dwItem1 = 0;
289 dwItem2 = 0;
290 return;
292 else if( ( wEventId & SHCNE_ONEITEMEVENTS ) && dwItem2 )
294 TRACE("dwItem2 is not zero, but should be\n");
295 dwItem2 = 0;
296 return;
299 if( ( ( wEventId & SHCNE_NOITEMEVENTS ) &&
300 ( wEventId & ~SHCNE_NOITEMEVENTS ) ) ||
301 ( ( wEventId & SHCNE_ONEITEMEVENTS ) &&
302 ( wEventId & ~SHCNE_ONEITEMEVENTS ) ) ||
303 ( ( wEventId & SHCNE_TWOITEMEVENTS ) &&
304 ( wEventId & ~SHCNE_TWOITEMEVENTS ) ) )
306 WARN("mutually incompatible events listed\n");
307 return;
310 /* convert paths in IDLists*/
311 switch (typeFlag)
313 case SHCNF_PATHA:
314 if (dwItem1) SHILCreateFromPathA((LPCSTR)dwItem1, &Pidls[0], &dummy);
315 if (dwItem2) SHILCreateFromPathA((LPCSTR)dwItem2, &Pidls[1], &dummy);
316 break;
317 case SHCNF_PATHW:
318 if (dwItem1) SHILCreateFromPathW((LPCWSTR)dwItem1, &Pidls[0], &dummy);
319 if (dwItem2) SHILCreateFromPathW((LPCWSTR)dwItem2, &Pidls[1], &dummy);
320 break;
321 case SHCNF_IDLIST:
322 Pidls[0] = (LPITEMIDLIST)dwItem1;
323 Pidls[1] = (LPITEMIDLIST)dwItem2;
324 break;
325 case SHCNF_PRINTERA:
326 case SHCNF_PRINTERW:
327 FIXME("SHChangeNotify with (uFlags & SHCNF_PRINTER)");
328 return;
329 case SHCNF_DWORD:
330 default:
331 FIXME("unknown type %08x\n",typeFlag);
332 return;
336 WCHAR path[MAX_PATH];
338 if( Pidls[0] && SHGetPathFromIDListW(Pidls[0], path ))
339 TRACE("notify %08lx on item1 = %s\n", wEventId, debugstr_w(path));
341 if( Pidls[1] && SHGetPathFromIDListW(Pidls[1], path ))
342 TRACE("notify %08lx on item2 = %s\n", wEventId, debugstr_w(path));
345 EnterCriticalSection(&SHELL32_ChangenotifyCS);
347 /* loop through the list */
348 for( ptr = head; ptr; ptr = ptr->next )
350 BOOL notify;
351 DWORD i;
353 notify = FALSE;
355 TRACE("trying %p\n", ptr);
357 for( i=0; (i<ptr->cidl) && !notify ; i++ )
359 LPITEMIDLIST pidl = ptr->apidl[i].pidlPath;
360 BOOL subtree = ptr->apidl[i].bWatchSubtree;
362 if (wEventId & ptr->wEventMask)
364 if( !pidl ) /* all ? */
365 notify = TRUE;
366 else if( wEventId & SHCNE_NOITEMEVENTS )
367 notify = TRUE;
368 else if( wEventId & ( SHCNE_ONEITEMEVENTS | SHCNE_TWOITEMEVENTS ) )
369 notify = should_notify( Pidls[0], pidl, subtree );
370 else if( wEventId & SHCNE_TWOITEMEVENTS )
371 notify = should_notify( Pidls[1], pidl, subtree );
375 if( !notify )
376 continue;
378 ptr->pidlSignaled = ILClone(Pidls[0]);
380 TRACE("notifying %s, event %s(%lx) before\n", NodeName( ptr ), DumpEvent(
381 wEventId ),wEventId );
383 ptr->wSignalledEvent |= wEventId;
384 SendMessageA(ptr->hwnd, ptr->uMsg, (WPARAM)ptr, GetCurrentProcessId());
386 TRACE("notifying %s, event %s(%lx) after\n", NodeName( ptr ), DumpEvent(
387 wEventId ),wEventId );
390 TRACE("notify Done\n");
391 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
393 /* if we allocated it, free it */
394 if ((typeFlag == SHCNF_PATHA) || (typeFlag == SHCNF_PATHW))
396 if (Pidls[0]) SHFree(Pidls[0]);
397 if (Pidls[1]) SHFree(Pidls[1]);
401 /*************************************************************************
402 * NTSHChangeNotifyRegister [SHELL32.640]
403 * NOTES
404 * Idlist is an array of structures and Count specifies how many items in the array
405 * (usually just one I think).
407 DWORD WINAPI NTSHChangeNotifyRegister(
408 HWND hwnd,
409 LONG events1,
410 LONG events2,
411 DWORD msg,
412 int count,
413 LPCNOTIFYREGISTER idlist)
415 FIXME("(%p,0x%08lx,0x%08lx,0x%08lx,0x%08x,%p):semi stub.\n",
416 hwnd,events1,events2,msg,count,idlist);
418 return (DWORD) SHChangeNotifyRegister(hwnd, events1, events2, msg, count, idlist);
421 /*************************************************************************
422 * SHChangeNotification_Lock [SHELL32.644]
424 HANDLE WINAPI SHChangeNotification_Lock(
425 HANDLE hChange,
426 DWORD dwProcessId,
427 LPITEMIDLIST **lppidls,
428 LPLONG lpwEventId)
430 DWORD i;
431 LPNOTIFICATIONLIST node;
432 LPCITEMIDLIST *idlist;
434 TRACE("%p %08lx %p %p\n", hChange, dwProcessId, lppidls, lpwEventId);
436 /* EnterCriticalSection(&SHELL32_ChangenotifyCS); */
438 node = FindNode( hChange );
439 if( node )
441 idlist = SHAlloc( sizeof(LPCITEMIDLIST *) * node->cidl );
442 for(i=0; i<node->cidl; i++)
443 idlist[i] = node->pidlSignaled;
444 *lpwEventId = node->wSignalledEvent;
445 *lppidls = idlist;
446 node->wSignalledEvent = 0;
448 else
449 ERR("Couldn't find %p\n", hChange );
451 /* LeaveCriticalSection(&SHELL32_ChangenotifyCS); */
453 return (HANDLE) node;
456 /*************************************************************************
457 * SHChangeNotification_Unlock [SHELL32.645]
459 BOOL WINAPI SHChangeNotification_Unlock ( HANDLE hLock)
461 TRACE("\n");
462 return 1;
465 /*************************************************************************
466 * NTSHChangeNotifyDeregister [SHELL32.641]
468 DWORD WINAPI NTSHChangeNotifyDeregister(LONG x1)
470 FIXME("(0x%08lx):semi stub.\n",x1);
472 return SHChangeNotifyDeregister( (HANDLE)x1 );