Fix the DDE PFNCALLBACK prototype.
[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 "pidl.h"
31 #include "shell32_main.h"
32 #include "undocshell.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(shell);
35
36 static CRITICAL_SECTION SHELL32_ChangenotifyCS;
37 static CRITICAL_SECTION_DEBUG critsect_debug =
38 {
39     0, 0, &SHELL32_ChangenotifyCS,
40     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
41       0, 0, { 0, (DWORD)(__FILE__ ": SHELL32_ChangenotifyCS") }
42 };
43 static CRITICAL_SECTION SHELL32_ChangenotifyCS = { &critsect_debug, -1, 0, 0, 0, 0 };
44
45 typedef SHChangeNotifyEntry *LPNOTIFYREGISTER;
46
47 /* internal list of notification clients (internal) */
48 typedef struct _NOTIFICATIONLIST
49 {
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*/
60     
61 } NOTIFICATIONLIST, *LPNOTIFICATIONLIST;
62
63 static NOTIFICATIONLIST *head, *tail;
64
65 #define SHCNE_NOITEMEVENTS ( \
66    SHCNE_ASSOCCHANGED )
67
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 )
74
75 #define SHCNE_TWOITEMEVENTS ( \
76    SHCNE_RENAMEFOLDER | SHCNE_RENAMEITEM | SHCNE_UPDATEITEM )
77
78 /* for dumping events */
79 static const char * DumpEvent( LONG event )
80 {
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)
107     );
108 #undef DUMPEV
109 }
110
111 static const char * NodeName(LPNOTIFICATIONLIST item)
112 {
113     const char *str;
114     WCHAR path[MAX_PATH];
115
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;
121 }
122
123 static void AddNode(LPNOTIFICATIONLIST item)
124 {
125     TRACE("item %p\n", item );
126
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;
135 }
136
137 static LPNOTIFICATIONLIST FindNode( HANDLE hitem )
138 {
139     LPNOTIFICATIONLIST ptr;
140     for( ptr = head; ptr; ptr = ptr->next )
141         if( ptr == (LPNOTIFICATIONLIST) hitem )
142             return ptr;
143     return NULL;
144 }
145
146 static void DeleteNode(LPNOTIFICATIONLIST item)
147 {
148     UINT i;
149
150     TRACE("item=%p prev=%p next=%p\n", item, item->prev, item->next);
151
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;
161
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);
167 }
168
169 void InitChangeNotifications(void)
170 {
171 }
172
173 void FreeChangeNotifications(void)
174 {
175     TRACE("\n");
176
177     EnterCriticalSection(&SHELL32_ChangenotifyCS);
178
179     while( head )
180         DeleteNode( head );
181
182     LeaveCriticalSection(&SHELL32_ChangenotifyCS);
183
184     DeleteCriticalSection(&SHELL32_ChangenotifyCS);
185 }
186
187 /*************************************************************************
188  * SHChangeNotifyRegister                       [SHELL32.2]
189  *
190  */
191 ULONG WINAPI
192 SHChangeNotifyRegister(
193     HWND hwnd,
194     int fSources,
195     LONG wEventMask,
196     UINT uMsg,
197     int cItems,
198     SHChangeNotifyEntry *lpItems)
199 {
200     LPNOTIFICATIONLIST item;
201     int i;
202
203     item = SHAlloc(sizeof(NOTIFICATIONLIST));
204
205     TRACE("(%p,0x%08x,0x%08lx,0x%08x,%d,%p) item=%p\n",
206         hwnd, fSources, wEventMask, uMsg, cItems, lpItems, item);
207
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++)
213     {
214         item->apidl[i].pidl = ILClone(lpItems[i].pidl);
215         item->apidl[i].fRecursive = lpItems[i].fRecursive;
216     }
217     item->hwnd = hwnd;
218     item->uMsg = uMsg;
219     item->wEventMask = wEventMask;
220     item->wSignalledEvent = 0;
221     item->dwFlags = fSources;
222
223     TRACE("new node: %s\n", NodeName( item ));
224
225     EnterCriticalSection(&SHELL32_ChangenotifyCS);
226
227     AddNode(item);
228
229     LeaveCriticalSection(&SHELL32_ChangenotifyCS);
230
231     return (ULONG)item;
232 }
233
234 /*************************************************************************
235  * SHChangeNotifyDeregister                     [SHELL32.4]
236  */
237 BOOL WINAPI SHChangeNotifyDeregister(ULONG hNotify)
238 {
239     LPNOTIFICATIONLIST node;
240
241     TRACE("(0x%08lx)\n", hNotify);
242
243     EnterCriticalSection(&SHELL32_ChangenotifyCS);
244
245     node = FindNode((HANDLE)hNotify);
246     if( node )
247         DeleteNode(node);
248
249     LeaveCriticalSection(&SHELL32_ChangenotifyCS);
250
251     return node?TRUE:FALSE;
252 }
253
254 /*************************************************************************
255  * SHChangeNotifyUpdateEntryList                [SHELL32.5]
256  */
257 BOOL WINAPI SHChangeNotifyUpdateEntryList(DWORD unknown1, DWORD unknown2,
258                               DWORD unknown3, DWORD unknown4)
259 {
260     FIXME("(0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx)\n",
261           unknown1, unknown2, unknown3, unknown4);
262
263     return -1;
264 }
265
266 static BOOL should_notify( LPITEMIDLIST changed, LPCITEMIDLIST watched, BOOL sub )
267 {
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;
276 }
277
278 /*************************************************************************
279  * SHChangeNotify                               [SHELL32.@]
280  */
281 void WINAPI SHChangeNotify(LONG wEventId, UINT uFlags, LPCVOID dwItem1, LPCVOID dwItem2)
282 {
283     LPITEMIDLIST Pidls[2];
284     LPNOTIFICATIONLIST ptr;
285     UINT typeFlag = uFlags & SHCNF_TYPE;
286
287     Pidls[0] = NULL;
288     Pidls[1] = NULL;
289
290     TRACE("(0x%08lx,0x%08x,%p,%p):stub.\n", wEventId, uFlags, dwItem1, dwItem2);
291
292     if( ( wEventId & SHCNE_NOITEMEVENTS ) && ( dwItem1 || dwItem2 ) )
293     {
294         TRACE("dwItem1 and dwItem2 are not zero, but should be\n");
295         dwItem1 = 0;
296         dwItem2 = 0;
297         return;
298     }
299     else if( ( wEventId & SHCNE_ONEITEMEVENTS ) && dwItem2 )
300     {
301         TRACE("dwItem2 is not zero, but should be\n");
302         dwItem2 = 0;
303         return;
304     }
305
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 ) ) )
312     {
313         WARN("mutually incompatible events listed\n");
314         return;
315     }
316
317     /* convert paths in IDLists*/
318     switch (typeFlag)
319     {
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;
340     }
341
342     {
343         WCHAR path[MAX_PATH];
344
345         if( Pidls[0] && SHGetPathFromIDListW(Pidls[0], path ))
346             TRACE("notify %08lx on item1 = %s\n", wEventId, debugstr_w(path));
347     
348         if( Pidls[1] && SHGetPathFromIDListW(Pidls[1], path ))
349             TRACE("notify %08lx on item2 = %s\n", wEventId, debugstr_w(path));
350     }
351
352     EnterCriticalSection(&SHELL32_ChangenotifyCS);
353
354     /* loop through the list */
355     for( ptr = head; ptr; ptr = ptr->next )
356     {
357         BOOL notify;
358         DWORD i;
359
360         notify = FALSE;
361
362         TRACE("trying %p\n", ptr);
363
364         for( i=0; (i<ptr->cidl) && !notify ; i++ )
365         {
366             LPCITEMIDLIST pidl = ptr->apidl[i].pidl;
367             BOOL subtree = ptr->apidl[i].fRecursive;
368
369             if (wEventId & ptr->wEventMask)
370             {
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 );
379             }
380         }
381
382         if( !notify )
383             continue;
384
385         ptr->pidlSignaled = ILClone(Pidls[0]);
386
387         TRACE("notifying %s, event %s(%lx) before\n", NodeName( ptr ), DumpEvent(
388                wEventId ),wEventId );
389
390         ptr->wSignalledEvent |= wEventId;
391
392         SendMessageA(ptr->hwnd, ptr->uMsg, (WPARAM)Pidls, wEventId);
393
394         TRACE("notifying %s, event %s(%lx) after\n", NodeName( ptr ), DumpEvent(
395                 wEventId ),wEventId );
396
397     }
398     TRACE("notify Done\n");
399     LeaveCriticalSection(&SHELL32_ChangenotifyCS);
400     
401     /* if we allocated it, free it. The ANSI flag is also set in its Unicode sibling. */
402     if ((typeFlag & SHCNF_PATHA) || (typeFlag & SHCNF_PRINTERA))
403     {
404         if (Pidls[0]) SHFree(Pidls[0]);
405         if (Pidls[1]) SHFree(Pidls[1]);
406     }
407 }
408
409 /*************************************************************************
410  * NTSHChangeNotifyRegister                     [SHELL32.640]
411  * NOTES
412  *   Idlist is an array of structures and Count specifies how many items in the array
413  *   (usually just one I think).
414  */
415 DWORD WINAPI NTSHChangeNotifyRegister(
416     HWND hwnd,
417     LONG events1,
418     LONG events2,
419     DWORD msg,
420     int count,
421     SHChangeNotifyEntry *idlist)
422 {
423     FIXME("(%p,0x%08lx,0x%08lx,0x%08lx,0x%08x,%p):semi stub.\n",
424                 hwnd,events1,events2,msg,count,idlist);
425
426     return (DWORD) SHChangeNotifyRegister(hwnd, events1, events2, msg, count, idlist);
427 }
428
429 /*************************************************************************
430  * SHChangeNotification_Lock                    [SHELL32.644]
431  */
432 HANDLE WINAPI SHChangeNotification_Lock(
433         HANDLE hChange,
434         DWORD dwProcessId,
435         LPITEMIDLIST **lppidls,
436         LPLONG lpwEventId)
437 {
438     DWORD i;
439     LPNOTIFICATIONLIST node;
440     LPITEMIDLIST *idlist;
441
442     TRACE("%p %08lx %p %p\n", hChange, dwProcessId, lppidls, lpwEventId);
443
444     /* EnterCriticalSection(&SHELL32_ChangenotifyCS); */
445
446     node = FindNode( hChange );
447     if( node )
448     {
449         idlist = SHAlloc( sizeof(LPCITEMIDLIST *) * node->cidl );
450         for(i=0; i<node->cidl; i++)
451             idlist[i] = (LPITEMIDLIST)node->pidlSignaled;
452         *lpwEventId = node->wSignalledEvent;
453         *lppidls = idlist;
454         node->wSignalledEvent = 0;
455     }
456     else
457         ERR("Couldn't find %p\n", hChange );
458
459     /* LeaveCriticalSection(&SHELL32_ChangenotifyCS); */
460
461     return (HANDLE) node;
462 }
463
464 /*************************************************************************
465  * SHChangeNotification_Unlock                  [SHELL32.645]
466  */
467 BOOL WINAPI SHChangeNotification_Unlock ( HANDLE hLock)
468 {
469     TRACE("\n");
470     return 1;
471 }
472
473 /*************************************************************************
474  * NTSHChangeNotifyDeregister                   [SHELL32.641]
475  */
476 DWORD WINAPI NTSHChangeNotifyDeregister(ULONG x1)
477 {
478     FIXME("(0x%08lx):semi stub.\n",x1);
479
480     return SHChangeNotifyDeregister( x1 );
481 }