Clear tooltip when text is set to NULL.
[wine/wine64.git] / dlls / shell32 / changenotify.c
blobaab5da63ed261d882afb1af8ed3995b56ce1fae8
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 <stdarg.h>
22 #include <string.h>
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wine/debug.h"
29 #include "wingdi.h"
30 #include "pidl.h"
31 #include "shell32_main.h"
32 #include "undocshell.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(shell);
36 static CRITICAL_SECTION SHELL32_ChangenotifyCS;
37 static CRITICAL_SECTION_DEBUG critsect_debug =
39 0, 0, &SHELL32_ChangenotifyCS,
40 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
41 0, 0, { 0, (DWORD)(__FILE__ ": SHELL32_ChangenotifyCS") }
43 static CRITICAL_SECTION SHELL32_ChangenotifyCS = { &critsect_debug, -1, 0, 0, 0, 0 };
45 typedef SHChangeNotifyEntry *LPNOTIFYREGISTER;
47 /* internal list of notification clients (internal) */
48 typedef struct _NOTIFICATIONLIST
50 struct _NOTIFICATIONLIST *next;
51 struct _NOTIFICATIONLIST *prev;
52 HWND hwnd; /* window to notify */
53 DWORD uMsg; /* message to send */
54 LPNOTIFYREGISTER apidl; /* array of entries to watch*/
55 UINT cidl; /* number of pidls in array */
56 LONG wEventMask; /* subscribed events */
57 LONG wSignalledEvent; /* event that occurred */
58 DWORD dwFlags; /* client flags */
59 LPCITEMIDLIST pidlSignaled; /*pidl of the path that caused the signal*/
61 } NOTIFICATIONLIST, *LPNOTIFICATIONLIST;
63 static NOTIFICATIONLIST *head, *tail;
65 #define SHCNE_NOITEMEVENTS ( \
66 SHCNE_ASSOCCHANGED )
68 #define SHCNE_ONEITEMEVENTS ( \
69 SHCNE_ATTRIBUTES | SHCNE_CREATE | SHCNE_DELETE | SHCNE_DRIVEADD | \
70 SHCNE_DRIVEADDGUI | SHCNE_DRIVEREMOVED | SHCNE_FREESPACE | \
71 SHCNE_MEDIAINSERTED | SHCNE_MEDIAREMOVED | SHCNE_MKDIR | \
72 SHCNE_NETSHARE | SHCNE_NETUNSHARE | SHCNE_RMDIR | \
73 SHCNE_SERVERDISCONNECT | SHCNE_UPDATEDIR | SHCNE_UPDATEIMAGE )
75 #define SHCNE_TWOITEMEVENTS ( \
76 SHCNE_RENAMEFOLDER | SHCNE_RENAMEITEM | SHCNE_UPDATEITEM )
78 /* for dumping events */
79 static const char * DumpEvent( LONG event )
81 if( event == SHCNE_ALLEVENTS )
82 return "SHCNE_ALLEVENTS";
83 #define DUMPEV(x) ,( event & SHCNE_##x )? #x " " : ""
84 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"
85 DUMPEV(RENAMEITEM)
86 DUMPEV(CREATE)
87 DUMPEV(DELETE)
88 DUMPEV(MKDIR)
89 DUMPEV(RMDIR)
90 DUMPEV(MEDIAINSERTED)
91 DUMPEV(MEDIAREMOVED)
92 DUMPEV(DRIVEREMOVED)
93 DUMPEV(DRIVEADD)
94 DUMPEV(NETSHARE)
95 DUMPEV(NETUNSHARE)
96 DUMPEV(ATTRIBUTES)
97 DUMPEV(UPDATEDIR)
98 DUMPEV(UPDATEITEM)
99 DUMPEV(SERVERDISCONNECT)
100 DUMPEV(UPDATEIMAGE)
101 DUMPEV(DRIVEADDGUI)
102 DUMPEV(RENAMEFOLDER)
103 DUMPEV(FREESPACE)
104 DUMPEV(EXTENDED_EVENT)
105 DUMPEV(ASSOCCHANGED)
106 DUMPEV(INTERRUPT)
108 #undef DUMPEV
111 static const char * NodeName(LPNOTIFICATIONLIST item)
113 const char *str;
114 WCHAR path[MAX_PATH];
116 if(SHGetPathFromIDListW(item->apidl[0].pidl, path ))
117 str = wine_dbg_sprintf("%s", debugstr_w(path));
118 else
119 str = wine_dbg_sprintf("<not a disk file>" );
120 return str;
123 static void AddNode(LPNOTIFICATIONLIST item)
125 TRACE("item %p\n", item );
127 /* link items */
128 item->prev = tail;
129 item->next = NULL;
130 if( tail )
131 tail->next = item;
132 else
133 head = item;
134 tail = item;
137 static LPNOTIFICATIONLIST FindNode( HANDLE hitem )
139 LPNOTIFICATIONLIST ptr;
140 for( ptr = head; ptr; ptr = ptr->next )
141 if( ptr == (LPNOTIFICATIONLIST) hitem )
142 return ptr;
143 return NULL;
146 static void DeleteNode(LPNOTIFICATIONLIST item)
148 UINT i;
150 TRACE("item=%p prev=%p next=%p\n", item, item->prev, item->next);
152 /* remove item from list */
153 if( item->prev )
154 item->prev->next = item->next;
155 else
156 head = item->next;
157 if( item->next )
158 item->next->prev = item->prev;
159 else
160 tail = item->prev;
162 /* free the item */
163 for (i=0; i<item->cidl; i++)
164 SHFree((LPITEMIDLIST)item->apidl[i].pidl);
165 SHFree(item->apidl);
166 SHFree(item);
169 void InitChangeNotifications(void)
173 void FreeChangeNotifications(void)
175 TRACE("\n");
177 EnterCriticalSection(&SHELL32_ChangenotifyCS);
179 while( head )
180 DeleteNode( head );
182 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
184 DeleteCriticalSection(&SHELL32_ChangenotifyCS);
187 /*************************************************************************
188 * SHChangeNotifyRegister [SHELL32.2]
191 ULONG WINAPI
192 SHChangeNotifyRegister(
193 HWND hwnd,
194 int fSources,
195 LONG wEventMask,
196 UINT uMsg,
197 int cItems,
198 SHChangeNotifyEntry *lpItems)
200 LPNOTIFICATIONLIST item;
201 int i;
203 item = SHAlloc(sizeof(NOTIFICATIONLIST));
205 TRACE("(%p,0x%08x,0x%08lx,0x%08x,%d,%p) item=%p\n",
206 hwnd, fSources, wEventMask, uMsg, cItems, lpItems, item);
208 item->next = NULL;
209 item->prev = NULL;
210 item->cidl = cItems;
211 item->apidl = SHAlloc(sizeof(SHChangeNotifyEntry) * cItems);
212 for(i=0;i<cItems;i++)
214 item->apidl[i].pidl = ILClone(lpItems[i].pidl);
215 item->apidl[i].fRecursive = lpItems[i].fRecursive;
217 item->hwnd = hwnd;
218 item->uMsg = uMsg;
219 item->wEventMask = wEventMask;
220 item->wSignalledEvent = 0;
221 item->dwFlags = fSources;
223 TRACE("new node: %s\n", NodeName( item ));
225 EnterCriticalSection(&SHELL32_ChangenotifyCS);
227 AddNode(item);
229 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
231 return (ULONG)item;
234 /*************************************************************************
235 * SHChangeNotifyDeregister [SHELL32.4]
237 BOOL WINAPI SHChangeNotifyDeregister(ULONG hNotify)
239 LPNOTIFICATIONLIST node;
241 TRACE("(0x%08lx)\n", hNotify);
243 EnterCriticalSection(&SHELL32_ChangenotifyCS);
245 node = FindNode((HANDLE)hNotify);
246 if( node )
247 DeleteNode(node);
249 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
251 return node?TRUE:FALSE;
254 /*************************************************************************
255 * SHChangeNotifyUpdateEntryList [SHELL32.5]
257 BOOL WINAPI SHChangeNotifyUpdateEntryList(DWORD unknown1, DWORD unknown2,
258 DWORD unknown3, DWORD unknown4)
260 FIXME("(0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx)\n",
261 unknown1, unknown2, unknown3, unknown4);
263 return -1;
266 static BOOL should_notify( LPITEMIDLIST changed, LPCITEMIDLIST watched, BOOL sub )
268 TRACE("%p %p %d\n", changed, watched, sub );
269 if ( !watched )
270 return FALSE;
271 if (ILIsEqual( watched, changed ) )
272 return TRUE;
273 if( sub && ILIsParent( watched, changed, FALSE ) )
274 return TRUE;
275 return FALSE;
278 /*************************************************************************
279 * SHChangeNotify [SHELL32.@]
281 void WINAPI SHChangeNotify(LONG wEventId, UINT uFlags, LPCVOID dwItem1, LPCVOID dwItem2)
283 LPITEMIDLIST Pidls[2];
284 LPNOTIFICATIONLIST ptr;
285 UINT typeFlag = uFlags & SHCNF_TYPE;
287 Pidls[0] = NULL;
288 Pidls[1] = NULL;
290 TRACE("(0x%08lx,0x%08x,%p,%p):stub.\n", wEventId, uFlags, dwItem1, dwItem2);
292 if( ( wEventId & SHCNE_NOITEMEVENTS ) && ( dwItem1 || dwItem2 ) )
294 TRACE("dwItem1 and dwItem2 are not zero, but should be\n");
295 dwItem1 = 0;
296 dwItem2 = 0;
297 return;
299 else if( ( wEventId & SHCNE_ONEITEMEVENTS ) && dwItem2 )
301 TRACE("dwItem2 is not zero, but should be\n");
302 dwItem2 = 0;
303 return;
306 if( ( ( wEventId & SHCNE_NOITEMEVENTS ) &&
307 ( wEventId & ~SHCNE_NOITEMEVENTS ) ) ||
308 ( ( wEventId & SHCNE_ONEITEMEVENTS ) &&
309 ( wEventId & ~SHCNE_ONEITEMEVENTS ) ) ||
310 ( ( wEventId & SHCNE_TWOITEMEVENTS ) &&
311 ( wEventId & ~SHCNE_TWOITEMEVENTS ) ) )
313 WARN("mutually incompatible events listed\n");
314 return;
317 /* convert paths in IDLists*/
318 switch (typeFlag)
320 case SHCNF_PATHA:
321 if (dwItem1) Pidls[0] = SHSimpleIDListFromPathA((LPCSTR)dwItem1);
322 if (dwItem2) Pidls[1] = SHSimpleIDListFromPathA((LPCSTR)dwItem2);
323 break;
324 case SHCNF_PATHW:
325 if (dwItem1) Pidls[0] = SHSimpleIDListFromPathW((LPCWSTR)dwItem1);
326 if (dwItem2) Pidls[1] = SHSimpleIDListFromPathW((LPCWSTR)dwItem2);
327 break;
328 case SHCNF_IDLIST:
329 Pidls[0] = (LPITEMIDLIST)dwItem1;
330 Pidls[1] = (LPITEMIDLIST)dwItem2;
331 break;
332 case SHCNF_PRINTERA:
333 case SHCNF_PRINTERW:
334 FIXME("SHChangeNotify with (uFlags & SHCNF_PRINTER)\n");
335 return;
336 case SHCNF_DWORD:
337 default:
338 FIXME("unknown type %08x\n",typeFlag);
339 return;
343 WCHAR path[MAX_PATH];
345 if( Pidls[0] && SHGetPathFromIDListW(Pidls[0], path ))
346 TRACE("notify %08lx on item1 = %s\n", wEventId, debugstr_w(path));
348 if( Pidls[1] && SHGetPathFromIDListW(Pidls[1], path ))
349 TRACE("notify %08lx on item2 = %s\n", wEventId, debugstr_w(path));
352 EnterCriticalSection(&SHELL32_ChangenotifyCS);
354 /* loop through the list */
355 for( ptr = head; ptr; ptr = ptr->next )
357 BOOL notify;
358 DWORD i;
360 notify = FALSE;
362 TRACE("trying %p\n", ptr);
364 for( i=0; (i<ptr->cidl) && !notify ; i++ )
366 LPCITEMIDLIST pidl = ptr->apidl[i].pidl;
367 BOOL subtree = ptr->apidl[i].fRecursive;
369 if (wEventId & ptr->wEventMask)
371 if( !pidl ) /* all ? */
372 notify = TRUE;
373 else if( wEventId & SHCNE_NOITEMEVENTS )
374 notify = TRUE;
375 else if( wEventId & ( SHCNE_ONEITEMEVENTS | SHCNE_TWOITEMEVENTS ) )
376 notify = should_notify( Pidls[0], pidl, subtree );
377 else if( wEventId & SHCNE_TWOITEMEVENTS )
378 notify = should_notify( Pidls[1], pidl, subtree );
382 if( !notify )
383 continue;
385 ptr->pidlSignaled = ILClone(Pidls[0]);
387 TRACE("notifying %s, event %s(%lx) before\n", NodeName( ptr ), DumpEvent(
388 wEventId ),wEventId );
390 ptr->wSignalledEvent |= wEventId;
392 if (ptr->dwFlags & SHCNRF_NewDelivery)
393 SendMessageA(ptr->hwnd, ptr->uMsg, (WPARAM) ptr, (LPARAM) GetCurrentProcessId());
394 else
395 SendMessageA(ptr->hwnd, ptr->uMsg, (WPARAM)Pidls, wEventId);
397 TRACE("notifying %s, event %s(%lx) after\n", NodeName( ptr ), DumpEvent(
398 wEventId ),wEventId );
401 TRACE("notify Done\n");
402 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
404 /* if we allocated it, free it. The ANSI flag is also set in its Unicode sibling. */
405 if ((typeFlag & SHCNF_PATHA) || (typeFlag & SHCNF_PRINTERA))
407 if (Pidls[0]) SHFree(Pidls[0]);
408 if (Pidls[1]) SHFree(Pidls[1]);
412 /*************************************************************************
413 * NTSHChangeNotifyRegister [SHELL32.640]
414 * NOTES
415 * Idlist is an array of structures and Count specifies how many items in the array
416 * (usually just one I think).
418 DWORD WINAPI NTSHChangeNotifyRegister(
419 HWND hwnd,
420 LONG events1,
421 LONG events2,
422 DWORD msg,
423 int count,
424 SHChangeNotifyEntry *idlist)
426 FIXME("(%p,0x%08lx,0x%08lx,0x%08lx,0x%08x,%p):semi stub.\n",
427 hwnd,events1,events2,msg,count,idlist);
429 return (DWORD) SHChangeNotifyRegister(hwnd, events1, events2, msg, count, idlist);
432 /*************************************************************************
433 * SHChangeNotification_Lock [SHELL32.644]
435 HANDLE WINAPI SHChangeNotification_Lock(
436 HANDLE hChange,
437 DWORD dwProcessId,
438 LPITEMIDLIST **lppidls,
439 LPLONG lpwEventId)
441 DWORD i;
442 LPNOTIFICATIONLIST node;
443 LPITEMIDLIST *idlist;
445 TRACE("%p %08lx %p %p\n", hChange, dwProcessId, lppidls, lpwEventId);
447 /* EnterCriticalSection(&SHELL32_ChangenotifyCS); */
449 node = FindNode( hChange );
450 if( node )
452 idlist = SHAlloc( sizeof(LPCITEMIDLIST *) * node->cidl );
453 for(i=0; i<node->cidl; i++)
454 idlist[i] = (LPITEMIDLIST)node->pidlSignaled;
455 *lpwEventId = node->wSignalledEvent;
456 *lppidls = idlist;
457 node->wSignalledEvent = 0;
459 else
460 ERR("Couldn't find %p\n", hChange );
462 /* LeaveCriticalSection(&SHELL32_ChangenotifyCS); */
464 return (HANDLE) node;
467 /*************************************************************************
468 * SHChangeNotification_Unlock [SHELL32.645]
470 BOOL WINAPI SHChangeNotification_Unlock ( HANDLE hLock)
472 TRACE("\n");
473 return 1;
476 /*************************************************************************
477 * NTSHChangeNotifyDeregister [SHELL32.641]
479 DWORD WINAPI NTSHChangeNotifyDeregister(ULONG x1)
481 FIXME("(0x%08lx):semi stub.\n",x1);
483 return SHChangeNotifyDeregister( x1 );