Fix French translation.
[wine/multimedia.git] / dlls / shell32 / changenotify.c
blobfaf6a267c37813b2fbe1d25d0e86ca53e46e7bed
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 "windef.h"
25 #include "wingdi.h"
26 #include "pidl.h"
27 #include "shell32_main.h"
28 #include "undocshell.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(shell);
32 static CRITICAL_SECTION SHELL32_ChangenotifyCS;
33 static CRITICAL_SECTION_DEBUG critsect_debug =
35 0, 0, &SHELL32_ChangenotifyCS,
36 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
37 0, 0, { 0, (DWORD)(__FILE__ ": SHELL32_ChangenotifyCS") }
39 static CRITICAL_SECTION SHELL32_ChangenotifyCS = { &critsect_debug, -1, 0, 0, 0, 0 };
41 /* internal list of notification clients (internal) */
42 typedef struct _NOTIFICATIONLIST
44 struct _NOTIFICATIONLIST *next;
45 struct _NOTIFICATIONLIST *prev;
46 HWND hwnd; /* window to notify */
47 DWORD uMsg; /* message to send */
48 LPNOTIFYREGISTER apidl; /* array of entries to watch*/
49 UINT cidl; /* number of pidls in array */
50 LONG wEventMask; /* subscribed events */
51 LONG wSignalledEvent; /* event that occurred */
52 DWORD dwFlags; /* client flags */
53 LPCITEMIDLIST pidlSignaled; /*pidl of the path that caused the signal*/
55 } NOTIFICATIONLIST, *LPNOTIFICATIONLIST;
57 static NOTIFICATIONLIST *head, *tail;
59 #define SHCNE_NOITEMEVENTS ( \
60 SHCNE_ASSOCCHANGED )
62 #define SHCNE_ONEITEMEVENTS ( \
63 SHCNE_ATTRIBUTES | SHCNE_CREATE | SHCNE_DELETE | SHCNE_DRIVEADD | \
64 SHCNE_DRIVEADDGUI | SHCNE_DRIVEREMOVED | SHCNE_FREESPACE | \
65 SHCNE_MEDIAINSERTED | SHCNE_MEDIAREMOVED | SHCNE_MKDIR | \
66 SHCNE_NETSHARE | SHCNE_NETUNSHARE | SHCNE_RMDIR | \
67 SHCNE_SERVERDISCONNECT | SHCNE_UPDATEDIR | SHCNE_UPDATEIMAGE )
69 #define SHCNE_TWOITEMEVENTS ( \
70 SHCNE_RENAMEFOLDER | SHCNE_RENAMEITEM | SHCNE_UPDATEITEM )
72 /* for dumping events */
73 static const char * DumpEvent( LONG event )
75 if( event == SHCNE_ALLEVENTS )
76 return "SHCNE_ALLEVENTS";
77 #define DUMPEV(x) ,( event & SHCNE_##x )? #x " " : ""
78 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"
79 DUMPEV(RENAMEITEM)
80 DUMPEV(CREATE)
81 DUMPEV(DELETE)
82 DUMPEV(MKDIR)
83 DUMPEV(RMDIR)
84 DUMPEV(MEDIAINSERTED)
85 DUMPEV(MEDIAREMOVED)
86 DUMPEV(DRIVEREMOVED)
87 DUMPEV(DRIVEADD)
88 DUMPEV(NETSHARE)
89 DUMPEV(NETUNSHARE)
90 DUMPEV(ATTRIBUTES)
91 DUMPEV(UPDATEDIR)
92 DUMPEV(UPDATEITEM)
93 DUMPEV(SERVERDISCONNECT)
94 DUMPEV(UPDATEIMAGE)
95 DUMPEV(DRIVEADDGUI)
96 DUMPEV(RENAMEFOLDER)
97 DUMPEV(FREESPACE)
98 DUMPEV(EXTENDED_EVENT)
99 DUMPEV(ASSOCCHANGED)
100 DUMPEV(INTERRUPT)
102 #undef DUMPEV
105 static const char * NodeName(LPNOTIFICATIONLIST item)
107 const char *str;
108 WCHAR path[MAX_PATH];
110 if(SHGetPathFromIDListW(item->apidl[0].pidlPath, path ))
111 str = wine_dbg_sprintf("%s", debugstr_w(path));
112 else
113 str = wine_dbg_sprintf("<not a disk file>" );
114 return str;
117 static void AddNode(LPNOTIFICATIONLIST item)
119 TRACE("item %p\n", item );
121 /* link items */
122 item->prev = tail;
123 item->next = NULL;
124 if( tail )
125 tail->next = item;
126 else
127 head = item;
128 tail = item;
131 static LPNOTIFICATIONLIST FindNode( HANDLE hitem )
133 LPNOTIFICATIONLIST ptr;
134 for( ptr = head; ptr; ptr = ptr->next )
135 if( ptr == (LPNOTIFICATIONLIST) hitem )
136 return ptr;
137 return NULL;
140 static void DeleteNode(LPNOTIFICATIONLIST item)
142 UINT i;
144 TRACE("item=%p prev=%p next=%p\n", item, item->prev, item->next);
146 /* remove item from list */
147 if( item->prev )
148 item->prev->next = item->next;
149 else
150 head = item->next;
151 if( item->next )
152 item->next->prev = item->prev;
153 else
154 tail = item->prev;
156 /* free the item */
157 for (i=0; i<item->cidl; i++)
158 SHFree(item->apidl[i].pidlPath);
159 SHFree(item->apidl);
160 SHFree(item);
163 void InitChangeNotifications(void)
167 void FreeChangeNotifications(void)
169 TRACE("\n");
171 EnterCriticalSection(&SHELL32_ChangenotifyCS);
173 while( head )
174 DeleteNode( head );
176 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
178 DeleteCriticalSection(&SHELL32_ChangenotifyCS);
181 /*************************************************************************
182 * SHChangeNotifyRegister [SHELL32.2]
185 HANDLE WINAPI
186 SHChangeNotifyRegister(
187 HWND hwnd,
188 LONG dwFlags,
189 LONG wEventMask,
190 UINT uMsg,
191 int cItems,
192 LPCNOTIFYREGISTER lpItems)
194 LPNOTIFICATIONLIST item;
195 int i;
197 item = SHAlloc(sizeof(NOTIFICATIONLIST));
199 TRACE("(%p,0x%08lx,0x%08lx,0x%08x,0x%08x,%p) item=%p\n",
200 hwnd, dwFlags, wEventMask, uMsg, cItems, lpItems, item);
202 item->next = NULL;
203 item->prev = NULL;
204 item->cidl = cItems;
205 item->apidl = SHAlloc(sizeof(NOTIFYREGISTER) * cItems);
206 for(i=0;i<cItems;i++)
208 item->apidl[i].pidlPath = ILClone(lpItems[i].pidlPath);
209 item->apidl[i].bWatchSubtree = lpItems[i].bWatchSubtree;
211 item->hwnd = hwnd;
212 item->uMsg = uMsg;
213 item->wEventMask = wEventMask;
214 item->wSignalledEvent = 0;
215 item->dwFlags = dwFlags;
217 TRACE("new node: %s\n", NodeName( item ));
219 EnterCriticalSection(&SHELL32_ChangenotifyCS);
221 AddNode(item);
223 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
225 return (HANDLE)item;
228 /*************************************************************************
229 * SHChangeNotifyDeregister [SHELL32.4]
231 BOOL WINAPI SHChangeNotifyDeregister(HANDLE hNotify)
233 LPNOTIFICATIONLIST node;
235 TRACE("(%p)\n",hNotify);
237 EnterCriticalSection(&SHELL32_ChangenotifyCS);
239 node = FindNode(hNotify);
240 if( node )
241 DeleteNode(node);
243 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
245 return node?TRUE:FALSE;
248 /*************************************************************************
249 * SHChangeNotifyUpdateEntryList [SHELL32.5]
251 BOOL WINAPI SHChangeNotifyUpdateEntryList(DWORD unknown1, DWORD unknown2,
252 DWORD unknown3, DWORD unknown4)
254 FIXME("(0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx)\n",
255 unknown1, unknown2, unknown3, unknown4);
257 return -1;
260 static BOOL should_notify( LPITEMIDLIST changed, LPITEMIDLIST watched, BOOL sub )
262 TRACE("%p %p %d\n", changed, watched, sub );
263 if ( !watched )
264 return FALSE;
265 if (ILIsEqual( watched, changed ) )
266 return TRUE;
267 if( sub && ILIsParent( watched, changed, FALSE ) )
268 return TRUE;
269 return FALSE;
272 /*************************************************************************
273 * SHChangeNotify [SHELL32.@]
275 void WINAPI SHChangeNotify(LONG wEventId, UINT uFlags, LPCVOID dwItem1, LPCVOID dwItem2)
277 LPITEMIDLIST Pidls[2];
278 LPNOTIFICATIONLIST ptr;
279 DWORD dummy;
280 UINT typeFlag = uFlags & SHCNF_TYPE;
282 Pidls[0] = NULL;
283 Pidls[1] = NULL;
285 TRACE("(0x%08lx,0x%08x,%p,%p):stub.\n", wEventId, uFlags, dwItem1, dwItem2);
287 if( ( wEventId & SHCNE_NOITEMEVENTS ) && ( dwItem1 || dwItem2 ) )
289 TRACE("dwItem1 and dwItem2 are not zero, but should be\n");
290 dwItem1 = 0;
291 dwItem2 = 0;
292 return;
294 else if( ( wEventId & SHCNE_ONEITEMEVENTS ) && dwItem2 )
296 TRACE("dwItem2 is not zero, but should be\n");
297 dwItem2 = 0;
298 return;
301 if( ( ( wEventId & SHCNE_NOITEMEVENTS ) &&
302 ( wEventId & ~SHCNE_NOITEMEVENTS ) ) ||
303 ( ( wEventId & SHCNE_ONEITEMEVENTS ) &&
304 ( wEventId & ~SHCNE_ONEITEMEVENTS ) ) ||
305 ( ( wEventId & SHCNE_TWOITEMEVENTS ) &&
306 ( wEventId & ~SHCNE_TWOITEMEVENTS ) ) )
308 WARN("mutually incompatible events listed\n");
309 return;
312 /* convert paths in IDLists*/
313 switch (typeFlag)
315 case SHCNF_PATHA:
316 if (dwItem1) SHILCreateFromPathA((LPCSTR)dwItem1, &Pidls[0], &dummy);
317 if (dwItem2) SHILCreateFromPathA((LPCSTR)dwItem2, &Pidls[1], &dummy);
318 break;
319 case SHCNF_PATHW:
320 if (dwItem1) SHILCreateFromPathW((LPCWSTR)dwItem1, &Pidls[0], &dummy);
321 if (dwItem2) SHILCreateFromPathW((LPCWSTR)dwItem2, &Pidls[1], &dummy);
322 break;
323 case SHCNF_IDLIST:
324 Pidls[0] = (LPITEMIDLIST)dwItem1;
325 Pidls[1] = (LPITEMIDLIST)dwItem2;
326 break;
327 case SHCNF_PRINTERA:
328 case SHCNF_PRINTERW:
329 FIXME("SHChangeNotify with (uFlags & SHCNF_PRINTER)");
330 return;
331 case SHCNF_DWORD:
332 default:
333 FIXME("unknown type %08x\n",typeFlag);
334 return;
338 WCHAR path[MAX_PATH];
340 if( Pidls[0] && SHGetPathFromIDListW(Pidls[0], path ))
341 TRACE("notify %08lx on item1 = %s\n", wEventId, debugstr_w(path));
343 if( Pidls[1] && SHGetPathFromIDListW(Pidls[1], path ))
344 TRACE("notify %08lx on item2 = %s\n", wEventId, debugstr_w(path));
347 EnterCriticalSection(&SHELL32_ChangenotifyCS);
349 /* loop through the list */
350 for( ptr = head; ptr; ptr = ptr->next )
352 BOOL notify;
353 DWORD i;
355 notify = FALSE;
357 TRACE("trying %p\n", ptr);
359 for( i=0; (i<ptr->cidl) && !notify ; i++ )
361 LPITEMIDLIST pidl = ptr->apidl[i].pidlPath;
362 BOOL subtree = ptr->apidl[i].bWatchSubtree;
364 if (wEventId & ptr->wEventMask)
366 if( !pidl ) /* all ? */
367 notify = TRUE;
368 else if( wEventId & SHCNE_NOITEMEVENTS )
369 notify = TRUE;
370 else if( wEventId & ( SHCNE_ONEITEMEVENTS | SHCNE_TWOITEMEVENTS ) )
371 notify = should_notify( Pidls[0], pidl, subtree );
372 else if( wEventId & SHCNE_TWOITEMEVENTS )
373 notify = should_notify( Pidls[1], pidl, subtree );
377 if( !notify )
378 continue;
380 ptr->pidlSignaled = ILClone(Pidls[0]);
382 TRACE("notifying %s, event %s(%lx) before\n", NodeName( ptr ), DumpEvent(
383 wEventId ),wEventId );
385 ptr->wSignalledEvent |= wEventId;
386 SendMessageA(ptr->hwnd, ptr->uMsg, (WPARAM)ptr, GetCurrentProcessId());
388 TRACE("notifying %s, event %s(%lx) after\n", NodeName( ptr ), DumpEvent(
389 wEventId ),wEventId );
392 TRACE("notify Done\n");
393 LeaveCriticalSection(&SHELL32_ChangenotifyCS);
395 /* if we allocated it, free it */
396 if ((typeFlag == SHCNF_PATHA) || (typeFlag == SHCNF_PATHW))
398 if (Pidls[0]) SHFree(Pidls[0]);
399 if (Pidls[1]) SHFree(Pidls[1]);
403 /*************************************************************************
404 * NTSHChangeNotifyRegister [SHELL32.640]
405 * NOTES
406 * Idlist is an array of structures and Count specifies how many items in the array
407 * (usually just one I think).
409 DWORD WINAPI NTSHChangeNotifyRegister(
410 HWND hwnd,
411 LONG events1,
412 LONG events2,
413 DWORD msg,
414 int count,
415 LPCNOTIFYREGISTER idlist)
417 FIXME("(%p,0x%08lx,0x%08lx,0x%08lx,0x%08x,%p):semi stub.\n",
418 hwnd,events1,events2,msg,count,idlist);
420 return (DWORD) SHChangeNotifyRegister(hwnd, events1, events2, msg, count, idlist);
423 /*************************************************************************
424 * SHChangeNotification_Lock [SHELL32.644]
426 HANDLE WINAPI SHChangeNotification_Lock(
427 HANDLE hChange,
428 DWORD dwProcessId,
429 LPCITEMIDLIST **lppidls,
430 LPLONG lpwEventId)
432 DWORD i;
433 LPNOTIFICATIONLIST node;
434 LPCITEMIDLIST *idlist;
436 TRACE("%p %08lx %p %p\n", hChange, dwProcessId, lppidls, lpwEventId);
438 /* EnterCriticalSection(&SHELL32_ChangenotifyCS); */
440 node = FindNode( hChange );
441 if( node )
443 idlist = SHAlloc( sizeof(LPCITEMIDLIST *) * node->cidl );
444 for(i=0; i<node->cidl; i++)
445 idlist[i] = node->pidlSignaled;
446 *lpwEventId = node->wSignalledEvent;
447 *lppidls = idlist;
448 node->wSignalledEvent = 0;
450 else
451 ERR("Couldn't find %p\n", hChange );
453 /* LeaveCriticalSection(&SHELL32_ChangenotifyCS); */
455 return (HANDLE) node;
458 /*************************************************************************
459 * SHChangeNotification_Unlock [SHELL32.645]
461 BOOL WINAPI SHChangeNotification_Unlock ( HANDLE hLock)
463 TRACE("\n");
464 return 1;
467 /*************************************************************************
468 * NTSHChangeNotifyDeregister [SHELL32.641]
470 DWORD WINAPI NTSHChangeNotifyDeregister(LONG x1)
472 FIXME("(0x%08lx):semi stub.\n",x1);
474 return SHChangeNotifyDeregister( (HANDLE)x1 );