Fix the case of product and company names.
[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 <string.h>
22
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"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(shell);
31
32 static CRITICAL_SECTION SHELL32_ChangenotifyCS;
33 static CRITICAL_SECTION_DEBUG critsect_debug =
34 {
35     0, 0, &SHELL32_ChangenotifyCS,
36     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
37       0, 0, { 0, (DWORD)(__FILE__ ": SHELL32_ChangenotifyCS") }
38 };
39 static CRITICAL_SECTION SHELL32_ChangenotifyCS = { &critsect_debug, -1, 0, 0, 0, 0 };
40
41 /* internal list of notification clients (internal) */
42 typedef struct _NOTIFICATIONLIST
43 {
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*/
54     
55 } NOTIFICATIONLIST, *LPNOTIFICATIONLIST;
56
57 static NOTIFICATIONLIST *head, *tail;
58
59 #define SHCNE_NOITEMEVENTS ( \
60    SHCNE_ASSOCCHANGED )
61
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 )
68
69 #define SHCNE_TWOITEMEVENTS ( \
70    SHCNE_RENAMEFOLDER | SHCNE_RENAMEITEM | SHCNE_UPDATEITEM )
71
72 /* for dumping events */
73 static const char * DumpEvent( LONG event )
74 {
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)
101     );
102 #undef DUMPEV
103 }
104
105 static const char * NodeName(LPNOTIFICATIONLIST item)
106 {
107     const char *str;
108     WCHAR path[MAX_PATH];
109
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;
115 }
116
117 static void AddNode(LPNOTIFICATIONLIST item)
118 {
119     TRACE("item %p\n", item );
120
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;
129 }
130
131 static LPNOTIFICATIONLIST FindNode( HANDLE hitem )
132 {
133     LPNOTIFICATIONLIST ptr;
134     for( ptr = head; ptr; ptr = ptr->next )
135         if( ptr == (LPNOTIFICATIONLIST) hitem )
136             return ptr;
137     return NULL;
138 }
139
140 static void DeleteNode(LPNOTIFICATIONLIST item)
141 {
142     UINT i;
143
144     TRACE("item=%p prev=%p next=%p\n", item, item->prev, item->next);
145
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;
155
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);
161 }
162
163 void InitChangeNotifications(void)
164 {
165 }
166
167 void FreeChangeNotifications(void)
168 {
169     TRACE("\n");
170
171     EnterCriticalSection(&SHELL32_ChangenotifyCS);
172
173     while( head )
174         DeleteNode( head );
175
176     LeaveCriticalSection(&SHELL32_ChangenotifyCS);
177
178     DeleteCriticalSection(&SHELL32_ChangenotifyCS);
179 }
180
181 /*************************************************************************
182  * SHChangeNotifyRegister                       [SHELL32.2]
183  *
184  */
185 HANDLE WINAPI
186 SHChangeNotifyRegister(
187     HWND hwnd,
188     LONG dwFlags,
189     LONG wEventMask,
190     UINT uMsg,
191     int cItems,
192     LPCNOTIFYREGISTER lpItems)
193 {
194     LPNOTIFICATIONLIST item;
195     int i;
196
197     item = SHAlloc(sizeof(NOTIFICATIONLIST));
198
199     TRACE("(%p,0x%08lx,0x%08lx,0x%08x,0x%08x,%p) item=%p\n",
200         hwnd, dwFlags, wEventMask, uMsg, cItems, lpItems, item);
201
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++)
207     {
208         item->apidl[i].pidlPath = ILClone(lpItems[i].pidlPath);
209         item->apidl[i].bWatchSubtree = lpItems[i].bWatchSubtree;
210     }
211     item->hwnd = hwnd;
212     item->uMsg = uMsg;
213     item->wEventMask = wEventMask;
214     item->wSignalledEvent = 0;
215     item->dwFlags = dwFlags;
216
217     TRACE("new node: %s\n", NodeName( item ));
218
219     EnterCriticalSection(&SHELL32_ChangenotifyCS);
220
221     AddNode(item);
222
223     LeaveCriticalSection(&SHELL32_ChangenotifyCS);
224
225     return (HANDLE)item;
226 }
227
228 /*************************************************************************
229  * SHChangeNotifyDeregister                     [SHELL32.4]
230  */
231 BOOL WINAPI SHChangeNotifyDeregister(HANDLE hNotify)
232 {
233     LPNOTIFICATIONLIST node;
234
235     TRACE("(%p)\n",hNotify);
236
237     EnterCriticalSection(&SHELL32_ChangenotifyCS);
238
239     node = FindNode(hNotify);
240     if( node )
241         DeleteNode(node);
242
243     LeaveCriticalSection(&SHELL32_ChangenotifyCS);
244
245     return node?TRUE:FALSE;
246 }
247
248 /*************************************************************************
249  * SHChangeNotifyUpdateEntryList                [SHELL32.5]
250  */
251 BOOL WINAPI SHChangeNotifyUpdateEntryList(DWORD unknown1, DWORD unknown2,
252                               DWORD unknown3, DWORD unknown4)
253 {
254     FIXME("(0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx)\n",
255           unknown1, unknown2, unknown3, unknown4);
256
257     return -1;
258 }
259
260 static BOOL should_notify( LPITEMIDLIST changed, LPITEMIDLIST watched, BOOL sub )
261 {
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;
270 }
271
272 /*************************************************************************
273  * SHChangeNotify                               [SHELL32.@]
274  */
275 void WINAPI SHChangeNotify(LONG wEventId, UINT uFlags, LPCVOID dwItem1, LPCVOID dwItem2)
276 {
277     LPITEMIDLIST Pidls[2];
278     LPNOTIFICATIONLIST ptr;
279     DWORD dummy;
280     UINT typeFlag = uFlags & SHCNF_TYPE;
281
282     Pidls[0] = NULL;
283     Pidls[1] = NULL;
284
285     TRACE("(0x%08lx,0x%08x,%p,%p):stub.\n", wEventId, uFlags, dwItem1, dwItem2);
286
287     if( ( wEventId & SHCNE_NOITEMEVENTS ) && ( dwItem1 || dwItem2 ) )
288     {
289         TRACE("dwItem1 and dwItem2 are not zero, but should be\n");
290         dwItem1 = 0;
291         dwItem2 = 0;
292         return;
293     }
294     else if( ( wEventId & SHCNE_ONEITEMEVENTS ) && dwItem2 )
295     {
296         TRACE("dwItem2 is not zero, but should be\n");
297         dwItem2 = 0;
298         return;
299     }
300
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 ) ) )
307     {
308         WARN("mutually incompatible events listed\n");
309         return;
310     }
311
312     /* convert paths in IDLists*/
313     switch (typeFlag)
314     {
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;
335     }
336
337     {
338         WCHAR path[MAX_PATH];
339
340         if( Pidls[0] && SHGetPathFromIDListW(Pidls[0], path ))
341             TRACE("notify %08lx on item1 = %s\n", wEventId, debugstr_w(path));
342     
343         if( Pidls[1] && SHGetPathFromIDListW(Pidls[1], path ))
344             TRACE("notify %08lx on item2 = %s\n", wEventId, debugstr_w(path));
345     }
346
347     EnterCriticalSection(&SHELL32_ChangenotifyCS);
348
349     /* loop through the list */
350     for( ptr = head; ptr; ptr = ptr->next )
351     {
352         BOOL notify;
353         DWORD i;
354
355         notify = FALSE;
356
357         TRACE("trying %p\n", ptr);
358
359         for( i=0; (i<ptr->cidl) && !notify ; i++ )
360         {
361             LPITEMIDLIST pidl = ptr->apidl[i].pidlPath;
362             BOOL subtree = ptr->apidl[i].bWatchSubtree;
363
364             if (wEventId & ptr->wEventMask)
365             {
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 );
374             }
375         }
376
377         if( !notify )
378             continue;
379
380         ptr->pidlSignaled = ILClone(Pidls[0]);
381
382         TRACE("notifying %s, event %s(%lx) before\n", NodeName( ptr ), DumpEvent(
383                wEventId ),wEventId );
384
385         ptr->wSignalledEvent |= wEventId;
386         SendMessageA(ptr->hwnd, ptr->uMsg, (WPARAM)ptr, GetCurrentProcessId());
387
388         TRACE("notifying %s, event %s(%lx) after\n", NodeName( ptr ), DumpEvent(
389                 wEventId ),wEventId );
390
391     }
392     TRACE("notify Done\n");
393     LeaveCriticalSection(&SHELL32_ChangenotifyCS);
394     
395     /* if we allocated it, free it */
396     if ((typeFlag == SHCNF_PATHA) || (typeFlag == SHCNF_PATHW))
397     {
398         if (Pidls[0]) SHFree(Pidls[0]);
399         if (Pidls[1]) SHFree(Pidls[1]);
400     }
401 }
402
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).
408  */
409 DWORD WINAPI NTSHChangeNotifyRegister(
410     HWND hwnd,
411     LONG events1,
412     LONG events2,
413     DWORD msg,
414     int count,
415     LPCNOTIFYREGISTER idlist)
416 {
417     FIXME("(%p,0x%08lx,0x%08lx,0x%08lx,0x%08x,%p):semi stub.\n",
418                 hwnd,events1,events2,msg,count,idlist);
419
420     return (DWORD) SHChangeNotifyRegister(hwnd, events1, events2, msg, count, idlist);
421 }
422
423 /*************************************************************************
424  * SHChangeNotification_Lock                    [SHELL32.644]
425  */
426 HANDLE WINAPI SHChangeNotification_Lock(
427         HANDLE hChange,
428         DWORD dwProcessId,
429         LPCITEMIDLIST **lppidls,
430         LPLONG lpwEventId)
431 {
432     DWORD i;
433     LPNOTIFICATIONLIST node;
434     LPCITEMIDLIST *idlist;
435
436     TRACE("%p %08lx %p %p\n", hChange, dwProcessId, lppidls, lpwEventId);
437
438     /* EnterCriticalSection(&SHELL32_ChangenotifyCS); */
439
440     node = FindNode( hChange );
441     if( node )
442     {
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;
449     }
450     else
451         ERR("Couldn't find %p\n", hChange );
452
453     /* LeaveCriticalSection(&SHELL32_ChangenotifyCS); */
454
455     return (HANDLE) node;
456 }
457
458 /*************************************************************************
459  * SHChangeNotification_Unlock                  [SHELL32.645]
460  */
461 BOOL WINAPI SHChangeNotification_Unlock ( HANDLE hLock)
462 {
463     TRACE("\n");
464     return 1;
465 }
466
467 /*************************************************************************
468  * NTSHChangeNotifyDeregister                   [SHELL32.641]
469  */
470 DWORD WINAPI NTSHChangeNotifyDeregister(LONG x1)
471 {
472     FIXME("(0x%08lx):semi stub.\n",x1);
473
474     return SHChangeNotifyDeregister( (HANDLE)x1 );
475 }