Merge from amd64-branch:
[reactos.git] / reactos / dll / win32 / shell32 / changenotify.c
1 /*
2 * shell change notification
3 *
4 * Copyright 2000 Juergen Schmied
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include <precomp.h>
22
23
24 WINE_DEFAULT_DEBUG_CHANNEL(shell);
25
26 static CRITICAL_SECTION SHELL32_ChangenotifyCS;
27 static CRITICAL_SECTION_DEBUG critsect_debug =
28 {
29 0, 0, &SHELL32_ChangenotifyCS,
30 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
31 0, 0, { (DWORD_PTR)(__FILE__ ": SHELL32_ChangenotifyCS") }
32 };
33 static CRITICAL_SECTION SHELL32_ChangenotifyCS = { &critsect_debug, -1, 0, 0, 0, 0 };
34
35 typedef SHChangeNotifyEntry *LPNOTIFYREGISTER;
36
37 /* internal list of notification clients (internal) */
38 typedef struct _NOTIFICATIONLIST
39 {
40 struct _NOTIFICATIONLIST *next;
41 struct _NOTIFICATIONLIST *prev;
42 HWND hwnd; /* window to notify */
43 DWORD uMsg; /* message to send */
44 LPNOTIFYREGISTER apidl; /* array of entries to watch*/
45 UINT cidl; /* number of pidls in array */
46 LONG wEventMask; /* subscribed events */
47 LONG wSignalledEvent; /* event that occurred */
48 DWORD dwFlags; /* client flags */
49 LPCITEMIDLIST pidlSignaled; /*pidl of the path that caused the signal*/
50
51 } NOTIFICATIONLIST, *LPNOTIFICATIONLIST;
52
53 static NOTIFICATIONLIST *head, *tail;
54
55 #define SHCNE_NOITEMEVENTS ( \
56 SHCNE_ASSOCCHANGED )
57
58 #define SHCNE_ONEITEMEVENTS ( \
59 SHCNE_ATTRIBUTES | SHCNE_CREATE | SHCNE_DELETE | SHCNE_DRIVEADD | \
60 SHCNE_DRIVEADDGUI | SHCNE_DRIVEREMOVED | SHCNE_FREESPACE | \
61 SHCNE_MEDIAINSERTED | SHCNE_MEDIAREMOVED | SHCNE_MKDIR | \
62 SHCNE_NETSHARE | SHCNE_NETUNSHARE | SHCNE_RMDIR | \
63 SHCNE_SERVERDISCONNECT | SHCNE_UPDATEDIR | SHCNE_UPDATEIMAGE )
64
65 #define SHCNE_TWOITEMEVENTS ( \
66 SHCNE_RENAMEFOLDER | SHCNE_RENAMEITEM | SHCNE_UPDATEITEM )
67
68 /* for dumping events */
69 static const char * DumpEvent( LONG event )
70 {
71 if( event == SHCNE_ALLEVENTS )
72 return "SHCNE_ALLEVENTS";
73 #define DUMPEV(x) ,( event & SHCNE_##x )? #x " " : ""
74 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"
75 DUMPEV(RENAMEITEM)
76 DUMPEV(CREATE)
77 DUMPEV(DELETE)
78 DUMPEV(MKDIR)
79 DUMPEV(RMDIR)
80 DUMPEV(MEDIAINSERTED)
81 DUMPEV(MEDIAREMOVED)
82 DUMPEV(DRIVEREMOVED)
83 DUMPEV(DRIVEADD)
84 DUMPEV(NETSHARE)
85 DUMPEV(NETUNSHARE)
86 DUMPEV(ATTRIBUTES)
87 DUMPEV(UPDATEDIR)
88 DUMPEV(UPDATEITEM)
89 DUMPEV(SERVERDISCONNECT)
90 DUMPEV(UPDATEIMAGE)
91 DUMPEV(DRIVEADDGUI)
92 DUMPEV(RENAMEFOLDER)
93 DUMPEV(FREESPACE)
94 DUMPEV(EXTENDED_EVENT)
95 DUMPEV(ASSOCCHANGED)
96 DUMPEV(INTERRUPT)
97 );
98 #undef DUMPEV
99 }
100
101 static const char * NodeName(const NOTIFICATIONLIST *item)
102 {
103 const char *str;
104 WCHAR path[MAX_PATH];
105
106 if(SHGetPathFromIDListW(item->apidl[0].pidl, path ))
107 str = wine_dbg_sprintf("%s", debugstr_w(path));
108 else
109 str = wine_dbg_sprintf("<not a disk file>" );
110 return str;
111 }
112
113 static void AddNode(LPNOTIFICATIONLIST item)
114 {
115 TRACE("item %p\n", item );
116
117 /* link items */
118 item->prev = tail;
119 item->next = NULL;
120 if( tail )
121 tail->next = item;
122 else
123 head = item;
124 tail = item;
125 }
126
127 static LPNOTIFICATIONLIST FindNode( HANDLE hitem )
128 {
129 LPNOTIFICATIONLIST ptr;
130 for( ptr = head; ptr; ptr = ptr->next )
131 if( ptr == (LPNOTIFICATIONLIST) hitem )
132 return ptr;
133 return NULL;
134 }
135
136 static void DeleteNode(LPNOTIFICATIONLIST item)
137 {
138 UINT i;
139
140 TRACE("item=%p prev=%p next=%p\n", item, item->prev, item->next);
141
142 /* remove item from list */
143 if( item->prev )
144 item->prev->next = item->next;
145 else
146 head = item->next;
147 if( item->next )
148 item->next->prev = item->prev;
149 else
150 tail = item->prev;
151
152 /* free the item */
153 for (i=0; i<item->cidl; i++)
154 SHFree((LPITEMIDLIST)item->apidl[i].pidl);
155 SHFree(item->apidl);
156 SHFree(item);
157 }
158
159 void InitChangeNotifications(void)
160 {
161 }
162
163 void FreeChangeNotifications(void)
164 {
165 TRACE("\n");
166
167 EnterCriticalSection(&SHELL32_ChangenotifyCS);
168
169 while( head )
170 DeleteNode( head );
171
172 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
173
174 // DeleteCriticalSection(&SHELL32_ChangenotifyCS); // static
175 }
176
177 /*************************************************************************
178 * SHChangeNotifyRegister [SHELL32.2]
179 *
180 */
181 ULONG WINAPI
182 SHChangeNotifyRegister(
183 HWND hwnd,
184 int fSources,
185 LONG wEventMask,
186 UINT uMsg,
187 int cItems,
188 const SHChangeNotifyEntry *lpItems)
189 {
190 LPNOTIFICATIONLIST item;
191 int i;
192
193 item = SHAlloc(sizeof(NOTIFICATIONLIST));
194
195 TRACE("(%p,0x%08x,0x%08x,0x%08x,%d,%p) item=%p\n",
196 hwnd, fSources, wEventMask, uMsg, cItems, lpItems, item);
197
198 item->next = NULL;
199 item->prev = NULL;
200 item->cidl = cItems;
201 item->apidl = SHAlloc(sizeof(SHChangeNotifyEntry) * cItems);
202 for(i=0;i<cItems;i++)
203 {
204 item->apidl[i].pidl = ILClone(lpItems[i].pidl);
205 item->apidl[i].fRecursive = lpItems[i].fRecursive;
206 }
207 item->hwnd = hwnd;
208 item->uMsg = uMsg;
209 item->wEventMask = wEventMask;
210 item->wSignalledEvent = 0;
211 item->dwFlags = fSources;
212
213 TRACE("new node: %s\n", NodeName( item ));
214
215 EnterCriticalSection(&SHELL32_ChangenotifyCS);
216
217 AddNode(item);
218
219 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
220
221 return (ULONG)item;
222 }
223
224 /*************************************************************************
225 * SHChangeNotifyDeregister [SHELL32.4]
226 */
227 BOOL WINAPI SHChangeNotifyDeregister(ULONG hNotify)
228 {
229 LPNOTIFICATIONLIST node;
230
231 TRACE("(0x%08x)\n", hNotify);
232
233 EnterCriticalSection(&SHELL32_ChangenotifyCS);
234
235 node = FindNode((HANDLE)hNotify);
236 if( node )
237 DeleteNode(node);
238
239 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
240
241 return node?TRUE:FALSE;
242 }
243
244 /*************************************************************************
245 * SHChangeNotifyUpdateEntryList [SHELL32.5]
246 */
247 BOOL WINAPI SHChangeNotifyUpdateEntryList(DWORD unknown1, DWORD unknown2,
248 DWORD unknown3, DWORD unknown4)
249 {
250 FIXME("(0x%08x, 0x%08x, 0x%08x, 0x%08x)\n",
251 unknown1, unknown2, unknown3, unknown4);
252
253 return -1;
254 }
255
256 static BOOL should_notify( LPCITEMIDLIST changed, LPCITEMIDLIST watched, BOOL sub )
257 {
258 TRACE("%p %p %d\n", changed, watched, sub );
259 if ( !watched )
260 return FALSE;
261 if (ILIsEqual( watched, changed ) )
262 return TRUE;
263 if( sub && ILIsParent( watched, changed, TRUE ) )
264 return TRUE;
265 return FALSE;
266 }
267
268 /*************************************************************************
269 * SHChangeNotify [SHELL32.@]
270 */
271 void WINAPI SHChangeNotify(LONG wEventId, UINT uFlags, LPCVOID dwItem1, LPCVOID dwItem2)
272 {
273 LPCITEMIDLIST Pidls[2];
274 LPNOTIFICATIONLIST ptr;
275 UINT typeFlag = uFlags & SHCNF_TYPE;
276
277 Pidls[0] = NULL;
278 Pidls[1] = NULL;
279
280 TRACE("(0x%08x,0x%08x,%p,%p):stub.\n", wEventId, uFlags, dwItem1, dwItem2);
281
282 if( ( wEventId & SHCNE_NOITEMEVENTS ) && ( dwItem1 || dwItem2 ) )
283 {
284 TRACE("dwItem1 and dwItem2 are not zero, but should be\n");
285 dwItem1 = 0;
286 dwItem2 = 0;
287 return;
288 }
289 else if( ( wEventId & SHCNE_ONEITEMEVENTS ) && dwItem2 )
290 {
291 TRACE("dwItem2 is not zero, but should be\n");
292 dwItem2 = 0;
293 return;
294 }
295
296 if( ( ( wEventId & SHCNE_NOITEMEVENTS ) &&
297 ( wEventId & ~SHCNE_NOITEMEVENTS ) ) ||
298 ( ( wEventId & SHCNE_ONEITEMEVENTS ) &&
299 ( wEventId & ~SHCNE_ONEITEMEVENTS ) ) ||
300 ( ( wEventId & SHCNE_TWOITEMEVENTS ) &&
301 ( wEventId & ~SHCNE_TWOITEMEVENTS ) ) )
302 {
303 WARN("mutually incompatible events listed\n");
304 return;
305 }
306
307 /* convert paths in IDLists*/
308 switch (typeFlag)
309 {
310 case SHCNF_PATHA:
311 if (dwItem1) Pidls[0] = SHSimpleIDListFromPathA((LPCSTR)dwItem1); //FIXME
312 if (dwItem2) Pidls[1] = SHSimpleIDListFromPathA((LPCSTR)dwItem2); //FIXME
313 break;
314 case SHCNF_PATHW:
315 if (dwItem1) Pidls[0] = SHSimpleIDListFromPathW((LPCWSTR)dwItem1);
316 if (dwItem2) Pidls[1] = SHSimpleIDListFromPathW((LPCWSTR)dwItem2);
317 break;
318 case SHCNF_IDLIST:
319 Pidls[0] = (LPCITEMIDLIST)dwItem1;
320 Pidls[1] = (LPCITEMIDLIST)dwItem2;
321 break;
322 case SHCNF_PRINTERA:
323 case SHCNF_PRINTERW:
324 FIXME("SHChangeNotify with (uFlags & SHCNF_PRINTER)\n");
325 return;
326 case SHCNF_DWORD:
327 default:
328 FIXME("unknown type %08x\n",typeFlag);
329 return;
330 }
331
332 {
333 WCHAR path[MAX_PATH];
334
335 if( Pidls[0] && SHGetPathFromIDListW(Pidls[0], path ))
336 TRACE("notify %08x on item1 = %s\n", wEventId, debugstr_w(path));
337
338 if( Pidls[1] && SHGetPathFromIDListW(Pidls[1], path ))
339 TRACE("notify %08x on item2 = %s\n", wEventId, debugstr_w(path));
340 }
341
342 EnterCriticalSection(&SHELL32_ChangenotifyCS);
343
344 /* loop through the list */
345 for( ptr = head; ptr; ptr = ptr->next )
346 {
347 BOOL notify;
348 DWORD i;
349
350 notify = FALSE;
351
352 TRACE("trying %p\n", ptr);
353
354 for( i=0; (i<ptr->cidl) && !notify ; i++ )
355 {
356 LPCITEMIDLIST pidl = ptr->apidl[i].pidl;
357 BOOL subtree = ptr->apidl[i].fRecursive;
358
359 if (wEventId & ptr->wEventMask)
360 {
361 if( !pidl ) /* all ? */
362 notify = TRUE;
363 else if( wEventId & SHCNE_NOITEMEVENTS )
364 notify = TRUE;
365 else if( wEventId & ( SHCNE_ONEITEMEVENTS | SHCNE_TWOITEMEVENTS ) )
366 notify = should_notify( Pidls[0], pidl, subtree );
367 else if( wEventId & SHCNE_TWOITEMEVENTS )
368 notify = should_notify( Pidls[1], pidl, subtree );
369 }
370 }
371
372 if( !notify )
373 continue;
374
375 ptr->pidlSignaled = ILClone(Pidls[0]);
376
377 TRACE("notifying %s, event %s(%x) before\n", NodeName( ptr ), DumpEvent(
378 wEventId ),wEventId );
379
380 ptr->wSignalledEvent |= wEventId;
381
382 if (ptr->dwFlags & SHCNRF_NewDelivery)
383 SendMessageW(ptr->hwnd, ptr->uMsg, (WPARAM) ptr, (LPARAM) GetCurrentProcessId());
384 else
385 SendMessageW(ptr->hwnd, ptr->uMsg, (WPARAM)Pidls, wEventId);
386
387 TRACE("notifying %s, event %s(%x) after\n", NodeName( ptr ), DumpEvent(
388 wEventId ),wEventId );
389
390 }
391 TRACE("notify Done\n");
392 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
393
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))
396 {
397 SHFree((LPITEMIDLIST)Pidls[0]);
398 SHFree((LPITEMIDLIST)Pidls[1]);
399 }
400 }
401
402 /*************************************************************************
403 * NTSHChangeNotifyRegister [SHELL32.640]
404 * NOTES
405 * Idlist is an array of structures and Count specifies how many items in the array.
406 * count should always be one when calling SHChangeNotifyRegister, or
407 * SHChangeNotifyDeregister will not work properly.
408 */
409 ULONG WINAPI NTSHChangeNotifyRegister(
410 HWND hwnd,
411 int fSources,
412 LONG fEvents,
413 UINT msg,
414 int count,
415 SHChangeNotifyEntry *idlist)
416 {
417 return SHChangeNotifyRegister(hwnd, fSources | SHCNRF_NewDelivery,
418 fEvents, msg, count, idlist);
419 }
420
421 /*************************************************************************
422 * SHChangeNotification_Lock [SHELL32.644]
423 */
424 HANDLE WINAPI SHChangeNotification_Lock(
425 HANDLE hChange,
426 DWORD dwProcessId,
427 LPITEMIDLIST **lppidls,
428 LPLONG lpwEventId)
429 {
430 DWORD i;
431 LPNOTIFICATIONLIST node;
432 LPCITEMIDLIST *idlist;
433
434 TRACE("%p %08x %p %p\n", hChange, dwProcessId, lppidls, lpwEventId);
435
436 /* EnterCriticalSection(&SHELL32_ChangenotifyCS); */
437
438 node = FindNode( hChange );
439 if( node )
440 {
441 idlist = SHAlloc( sizeof(LPCITEMIDLIST *) * node->cidl );
442 for(i=0; i<node->cidl; i++)
443 idlist[i] = (LPCITEMIDLIST)node->pidlSignaled;
444 *lpwEventId = node->wSignalledEvent;
445 *lppidls = (LPITEMIDLIST*)idlist;
446 node->wSignalledEvent = 0;
447 }
448 else
449 ERR("Couldn't find %p\n", hChange );
450
451 /* LeaveCriticalSection(&SHELL32_ChangenotifyCS); */
452
453 return (HANDLE) node;
454 }
455
456 /*************************************************************************
457 * SHChangeNotification_Unlock [SHELL32.645]
458 */
459 BOOL WINAPI SHChangeNotification_Unlock ( HANDLE hLock)
460 {
461 TRACE("\n");
462 return 1;
463 }
464
465 /*************************************************************************
466 * NTSHChangeNotifyDeregister [SHELL32.641]
467 */
468 DWORD WINAPI NTSHChangeNotifyDeregister(ULONG x1)
469 {
470 FIXME("(0x%08x):semi stub.\n",x1);
471
472 return SHChangeNotifyDeregister( x1 );
473 }