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