[SHELL32]
[reactos.git] / reactos / dll / win32 / shell32 / changenotify.cpp
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 WINE_DEFAULT_DEBUG_CHANNEL(shell);
24
25 namespace
26 {
27 extern CRITICAL_SECTION SHELL32_ChangenotifyCS;
28 CRITICAL_SECTION_DEBUG critsect_debug =
29 {
30 0, 0, &SHELL32_ChangenotifyCS,
31 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
32 0, 0, { (DWORD_PTR)(__FILE__ ": SHELL32_ChangenotifyCS") }
33 };
34 CRITICAL_SECTION SHELL32_ChangenotifyCS = { &critsect_debug, -1, 0, 0, 0, 0 };
35 }
36
37 typedef SHChangeNotifyEntry *LPNOTIFYREGISTER;
38
39 /* internal list of notification clients (internal) */
40 typedef struct _NOTIFICATIONLIST
41 {
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*/
52
53 } NOTIFICATIONLIST, *LPNOTIFICATIONLIST;
54
55 static NOTIFICATIONLIST *head, *tail;
56
57 #define SHCNE_NOITEMEVENTS ( \
58 SHCNE_ASSOCCHANGED )
59
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 )
66
67 #define SHCNE_TWOITEMEVENTS ( \
68 SHCNE_RENAMEFOLDER | SHCNE_RENAMEITEM | SHCNE_UPDATEITEM )
69
70 /* for dumping events */
71 static const char * DumpEvent( LONG event )
72 {
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)
99 );
100 #undef DUMPEV
101 }
102
103 static const char * NodeName(const NOTIFICATIONLIST *item)
104 {
105 const char *str;
106 WCHAR path[MAX_PATH];
107
108 if(SHGetPathFromIDListW(item->apidl[0].pidl, path ))
109 str = wine_dbg_sprintf("%s", debugstr_w(path));
110 else
111 str = wine_dbg_sprintf("<not a disk file>" );
112 return str;
113 }
114
115 static void AddNode(LPNOTIFICATIONLIST item)
116 {
117 TRACE("item %p\n", item );
118
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;
127 }
128
129 static LPNOTIFICATIONLIST FindNode( HANDLE hitem )
130 {
131 LPNOTIFICATIONLIST ptr;
132 for( ptr = head; ptr; ptr = ptr->next )
133 if( ptr == (LPNOTIFICATIONLIST) hitem )
134 return ptr;
135 return NULL;
136 }
137
138 static void DeleteNode(LPNOTIFICATIONLIST item)
139 {
140 UINT i;
141
142 TRACE("item=%p prev=%p next=%p\n", item, item->prev, item->next);
143
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;
153
154 /* free the item */
155 for (i=0; i<item->cidl; i++)
156 SHFree((LPITEMIDLIST)item->apidl[i].pidl);
157 SHFree(item->apidl);
158 SHFree(item);
159 }
160
161 void InitChangeNotifications(void)
162 {
163 }
164
165 void FreeChangeNotifications(void)
166 {
167 TRACE("\n");
168
169 EnterCriticalSection(&SHELL32_ChangenotifyCS);
170
171 while( head )
172 DeleteNode( head );
173
174 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
175
176 // DeleteCriticalSection(&SHELL32_ChangenotifyCS); // static
177 }
178
179 /*************************************************************************
180 * SHChangeNotifyRegister [SHELL32.2]
181 *
182 */
183 ULONG WINAPI
184 SHChangeNotifyRegister(
185 HWND hwnd,
186 int fSources,
187 LONG wEventMask,
188 UINT uMsg,
189 int cItems,
190 SHChangeNotifyEntry *lpItems)
191 {
192 LPNOTIFICATIONLIST item;
193 int i;
194
195 item = (NOTIFICATIONLIST *)SHAlloc(sizeof(NOTIFICATIONLIST));
196
197 TRACE("(%p,0x%08x,0x%08x,0x%08x,%d,%p) item=%p\n",
198 hwnd, fSources, wEventMask, uMsg, cItems, lpItems, item);
199
200 item->next = NULL;
201 item->prev = NULL;
202 item->cidl = cItems;
203 item->apidl = (SHChangeNotifyEntry *)SHAlloc(sizeof(SHChangeNotifyEntry) * cItems);
204 for(i=0;i<cItems;i++)
205 {
206 item->apidl[i].pidl = ILClone(lpItems[i].pidl);
207 item->apidl[i].fRecursive = lpItems[i].fRecursive;
208 }
209 item->hwnd = hwnd;
210 item->uMsg = uMsg;
211 item->wEventMask = wEventMask;
212 item->wSignalledEvent = 0;
213 item->dwFlags = fSources;
214
215 TRACE("new node: %s\n", NodeName( item ));
216
217 EnterCriticalSection(&SHELL32_ChangenotifyCS);
218
219 AddNode(item);
220
221 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
222
223 return (ULONG)item;
224 }
225
226 /*************************************************************************
227 * SHChangeNotifyDeregister [SHELL32.4]
228 */
229 BOOL WINAPI SHChangeNotifyDeregister(ULONG hNotify)
230 {
231 LPNOTIFICATIONLIST node;
232
233 TRACE("(0x%08x)\n", hNotify);
234
235 EnterCriticalSection(&SHELL32_ChangenotifyCS);
236
237 node = FindNode((HANDLE)hNotify);
238 if( node )
239 DeleteNode(node);
240
241 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
242
243 return node?TRUE:FALSE;
244 }
245
246 /*************************************************************************
247 * SHChangeNotifyUpdateEntryList [SHELL32.5]
248 */
249 EXTERN_C BOOL WINAPI SHChangeNotifyUpdateEntryList(DWORD unknown1, DWORD unknown2,
250 DWORD unknown3, DWORD unknown4)
251 {
252 FIXME("(0x%08x, 0x%08x, 0x%08x, 0x%08x)\n",
253 unknown1, unknown2, unknown3, unknown4);
254
255 return -1;
256 }
257
258 static BOOL should_notify( LPCITEMIDLIST changed, LPCITEMIDLIST watched, BOOL sub )
259 {
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, TRUE ) )
266 return TRUE;
267 if (sub && _ILIsDesktop(watched)) {
268 WCHAR wszPath[MAX_PATH];
269 SHGetSpecialFolderPathW(0, wszPath, CSIDL_DESKTOPDIRECTORY, FALSE);
270 LPITEMIDLIST deskpidl = SHSimpleIDListFromPathW(wszPath);
271 if (ILIsParent(deskpidl, changed, TRUE))
272 {
273 ILFree(deskpidl);
274 return TRUE;
275 }
276 ILFree(deskpidl);
277 SHGetSpecialFolderPathW(0, wszPath, CSIDL_COMMON_DESKTOPDIRECTORY, FALSE);
278 deskpidl = SHSimpleIDListFromPathW(wszPath);
279 if (ILIsParent(deskpidl, changed, TRUE))
280 {
281 ILFree(deskpidl);
282 return TRUE;
283 }
284 ILFree(deskpidl);
285 }
286 return FALSE;
287 }
288
289 /*************************************************************************
290 * SHChangeNotify [SHELL32.@]
291 */
292 void WINAPI SHChangeNotify(LONG wEventId, UINT uFlags, LPCVOID dwItem1, LPCVOID dwItem2)
293 {
294 LPITEMIDLIST Pidls[2];
295 LPNOTIFICATIONLIST ptr;
296 UINT typeFlag = uFlags & SHCNF_TYPE;
297
298 Pidls[0] = NULL;
299 Pidls[1] = NULL;
300
301 TRACE("(0x%08x,0x%08x,%p,%p):stub.\n", wEventId, uFlags, dwItem1, dwItem2);
302
303 if( ( wEventId & SHCNE_NOITEMEVENTS ) && ( dwItem1 || dwItem2 ) )
304 {
305 TRACE("dwItem1 and dwItem2 are not zero, but should be\n");
306 dwItem1 = 0;
307 dwItem2 = 0;
308 return;
309 }
310 else if( ( wEventId & SHCNE_ONEITEMEVENTS ) && dwItem2 )
311 {
312 TRACE("dwItem2 is not zero, but should be\n");
313 dwItem2 = 0;
314 return;
315 }
316
317 if( ( ( wEventId & SHCNE_NOITEMEVENTS ) &&
318 ( wEventId & ~SHCNE_NOITEMEVENTS ) ) ||
319 ( ( wEventId & SHCNE_ONEITEMEVENTS ) &&
320 ( wEventId & ~SHCNE_ONEITEMEVENTS ) ) ||
321 ( ( wEventId & SHCNE_TWOITEMEVENTS ) &&
322 ( wEventId & ~SHCNE_TWOITEMEVENTS ) ) )
323 {
324 WARN("mutually incompatible events listed\n");
325 return;
326 }
327
328 /* convert paths in IDLists*/
329 switch (typeFlag)
330 {
331 case SHCNF_PATHA:
332 if (dwItem1) Pidls[0] = SHSimpleIDListFromPathA((LPCSTR)dwItem1); //FIXME
333 if (dwItem2) Pidls[1] = SHSimpleIDListFromPathA((LPCSTR)dwItem2); //FIXME
334 break;
335 case SHCNF_PATHW:
336 if (dwItem1) Pidls[0] = SHSimpleIDListFromPathW((LPCWSTR)dwItem1);
337 if (dwItem2) Pidls[1] = SHSimpleIDListFromPathW((LPCWSTR)dwItem2);
338 if (wEventId & (SHCNE_MKDIR | SHCNE_RMDIR | SHCNE_UPDATEDIR | SHCNE_RENAMEFOLDER))
339 {
340 /*
341 * The last items in the ID are currently files. So we chop off the last
342 * entry, and create a new one using a find data struct.
343 */
344 if (dwItem1 && Pidls[0]){
345 ILRemoveLastID(Pidls[0]);
346 WIN32_FIND_DATAW wfd;
347 LPWSTR p = PathFindFileNameW((LPCWSTR)dwItem1);
348 lstrcpynW(&wfd.cFileName[0], p, MAX_PATH);
349 wfd.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
350 LPITEMIDLIST newpidl = _ILCreateFromFindDataW(&wfd);
351 LPITEMIDLIST oldpidl = ILClone(Pidls[0]);
352 ILFree(Pidls[0]);
353 Pidls[0] = ILCombine(oldpidl, newpidl);
354 ILFree(newpidl);
355 ILFree(oldpidl);
356 }
357 if (dwItem2 && Pidls[1]){
358 ILRemoveLastID(Pidls[1]);
359 WIN32_FIND_DATAW wfd;
360 LPWSTR p = PathFindFileNameW((LPCWSTR)dwItem2);
361 lstrcpynW(&wfd.cFileName[0], p, MAX_PATH);
362 wfd.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
363 LPITEMIDLIST newpidl = _ILCreateFromFindDataW(&wfd);
364 LPITEMIDLIST oldpidl = ILClone(Pidls[0]);
365 ILFree(Pidls[1]);
366 Pidls[1] = ILCombine(oldpidl, newpidl);
367 ILFree(newpidl);
368 ILFree(oldpidl);
369 }
370 }
371 break;
372 case SHCNF_IDLIST:
373 Pidls[0] = (LPITEMIDLIST)dwItem1;
374 Pidls[1] = (LPITEMIDLIST)dwItem2;
375 break;
376 case SHCNF_PRINTERA:
377 case SHCNF_PRINTERW:
378 FIXME("SHChangeNotify with (uFlags & SHCNF_PRINTER)\n");
379 return;
380 case SHCNF_DWORD:
381 default:
382 FIXME("unknown type %08x\n",typeFlag);
383 return;
384 }
385
386 {
387 WCHAR path[MAX_PATH];
388
389 if( Pidls[0] && SHGetPathFromIDListW(Pidls[0], path ))
390 TRACE("notify %08x on item1 = %s\n", wEventId, debugstr_w(path));
391
392 if( Pidls[1] && SHGetPathFromIDListW(Pidls[1], path ))
393 TRACE("notify %08x on item2 = %s\n", wEventId, debugstr_w(path));
394 }
395
396 EnterCriticalSection(&SHELL32_ChangenotifyCS);
397
398 /* loop through the list */
399 for( ptr = head; ptr; ptr = ptr->next )
400 {
401 BOOL notify;
402 DWORD i;
403
404 notify = FALSE;
405
406 TRACE("trying %p\n", ptr);
407
408 for( i=0; (i<ptr->cidl) && !notify ; i++ )
409 {
410 LPCITEMIDLIST pidl = ptr->apidl[i].pidl;
411 BOOL subtree = ptr->apidl[i].fRecursive;
412
413 if (wEventId & ptr->wEventMask)
414 {
415 if( !pidl ) /* all ? */
416 notify = TRUE;
417 else if( wEventId & SHCNE_NOITEMEVENTS )
418 notify = TRUE;
419 else if( wEventId & ( SHCNE_ONEITEMEVENTS | SHCNE_TWOITEMEVENTS ) )
420 notify = should_notify( Pidls[0], pidl, subtree );
421 else if( wEventId & SHCNE_TWOITEMEVENTS )
422 notify = should_notify( Pidls[1], pidl, subtree );
423 }
424 }
425
426 if( !notify )
427 continue;
428
429 ptr->pidlSignaled = ILClone(Pidls[0]);
430
431 TRACE("notifying %s, event %s(%x) before\n", NodeName( ptr ), DumpEvent(
432 wEventId ),wEventId );
433
434 ptr->wSignalledEvent |= wEventId;
435
436 if (ptr->dwFlags & SHCNRF_NewDelivery)
437 SendMessageW(ptr->hwnd, ptr->uMsg, (WPARAM) ptr, (LPARAM) GetCurrentProcessId());
438 else
439 SendMessageW(ptr->hwnd, ptr->uMsg, (WPARAM)Pidls, wEventId);
440
441 TRACE("notifying %s, event %s(%x) after\n", NodeName( ptr ), DumpEvent(
442 wEventId ),wEventId );
443
444 }
445 TRACE("notify Done\n");
446 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
447
448 /* if we allocated it, free it. The ANSI flag is also set in its Unicode sibling. */
449 if ((typeFlag & SHCNF_PATHA) || (typeFlag & SHCNF_PRINTERA))
450 {
451 SHFree((LPITEMIDLIST)Pidls[0]);
452 SHFree((LPITEMIDLIST)Pidls[1]);
453 }
454 }
455
456 /*************************************************************************
457 * NTSHChangeNotifyRegister [SHELL32.640]
458 * NOTES
459 * Idlist is an array of structures and Count specifies how many items in the array.
460 * count should always be one when calling SHChangeNotifyRegister, or
461 * SHChangeNotifyDeregister will not work properly.
462 */
463 EXTERN_C ULONG WINAPI NTSHChangeNotifyRegister(
464 HWND hwnd,
465 int fSources,
466 LONG fEvents,
467 UINT msg,
468 int count,
469 SHChangeNotifyEntry *idlist)
470 {
471 return SHChangeNotifyRegister(hwnd, fSources | SHCNRF_NewDelivery,
472 fEvents, msg, count, idlist);
473 }
474
475 /*************************************************************************
476 * SHChangeNotification_Lock [SHELL32.644]
477 */
478 HANDLE WINAPI SHChangeNotification_Lock(
479 HANDLE hChange,
480 DWORD dwProcessId,
481 LPITEMIDLIST **lppidls,
482 LPLONG lpwEventId)
483 {
484 DWORD i;
485 LPNOTIFICATIONLIST node;
486 LPCITEMIDLIST *idlist;
487
488 TRACE("%p %08x %p %p\n", hChange, dwProcessId, lppidls, lpwEventId);
489
490 /* EnterCriticalSection(&SHELL32_ChangenotifyCS); */
491
492 node = FindNode( hChange );
493 if( node )
494 {
495 idlist = (LPCITEMIDLIST *)SHAlloc( sizeof(LPCITEMIDLIST *) * node->cidl );
496 for(i=0; i<node->cidl; i++)
497 idlist[i] = (LPCITEMIDLIST)node->pidlSignaled;
498 *lpwEventId = node->wSignalledEvent;
499 *lppidls = (LPITEMIDLIST*)idlist;
500 node->wSignalledEvent = 0;
501 }
502 else
503 ERR("Couldn't find %p\n", hChange );
504
505 /* LeaveCriticalSection(&SHELL32_ChangenotifyCS); */
506
507 return (HANDLE) node;
508 }
509
510 /*************************************************************************
511 * SHChangeNotification_Unlock [SHELL32.645]
512 */
513 BOOL WINAPI SHChangeNotification_Unlock ( HANDLE hLock)
514 {
515 TRACE("\n");
516 return 1;
517 }
518
519 /*************************************************************************
520 * NTSHChangeNotifyDeregister [SHELL32.641]
521 */
522 EXTERN_C DWORD WINAPI NTSHChangeNotifyDeregister(ULONG x1)
523 {
524 FIXME("(0x%08x):semi stub.\n",x1);
525
526 return SHChangeNotifyDeregister( x1 );
527 }