Split signal blocking code out of SIGNAL_Reset into SIGNAL_Block.
[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 "pidl.h"
25 #include "shell32_main.h"
26 #include "undocshell.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(shell);
29
30 static CRITICAL_SECTION SHELL32_ChangenotifyCS = CRITICAL_SECTION_INIT("SHELL32_ChangenotifyCS");
31
32 /* internal list of notification clients (internal) */
33 typedef struct _NOTIFICATIONLIST
34 {
35         struct _NOTIFICATIONLIST *next;
36         struct _NOTIFICATIONLIST *prev;
37         HWND hwnd;              /* window to notify */
38         DWORD uMsg;             /* message to send */
39         LPNOTIFYREGISTER apidl; /* array of entries to watch*/
40         UINT cidl;              /* number of pidls in array */
41         LONG wEventMask;        /* subscribed events */
42         DWORD dwFlags;          /* client flags */
43 } NOTIFICATIONLIST, *LPNOTIFICATIONLIST;
44
45 static NOTIFICATIONLIST head;
46 static NOTIFICATIONLIST tail;
47
48 void InitChangeNotifications()
49 {
50         TRACE("head=%p tail=%p\n", &head, &tail);
51         head.next = &tail;
52         tail.prev = &head;
53 }
54
55 void FreeChangeNotifications()
56 {
57         LPNOTIFICATIONLIST ptr, item;
58
59         TRACE("\n");
60
61         EnterCriticalSection(&SHELL32_ChangenotifyCS);
62         ptr = head.next;
63
64         while(ptr != &tail)
65         {
66           UINT i;
67           item = ptr;
68           ptr = ptr->next;
69
70           TRACE("item=%p\n", item);
71
72           /* free the item */
73           for (i=0; i<item->cidl;i++) SHFree(item->apidl[i].pidlPath);
74           SHFree(item->apidl);
75           SHFree(item);
76         }
77         head.next = NULL;
78         tail.prev = NULL;
79
80         LeaveCriticalSection(&SHELL32_ChangenotifyCS);
81
82         DeleteCriticalSection(&SHELL32_ChangenotifyCS);
83 }
84
85 static BOOL AddNode(LPNOTIFICATIONLIST item)
86 {
87         LPNOTIFICATIONLIST last;
88
89         EnterCriticalSection(&SHELL32_ChangenotifyCS);
90
91         /* get last entry */
92         last = tail.prev;
93
94         /* link items */
95         last->next = item;
96         item->prev = last;
97         item->next = &tail;
98         tail.prev = item;
99         TRACE("item=%p prev=%p next=%p\n", item, item->prev, item->next);
100
101         LeaveCriticalSection(&SHELL32_ChangenotifyCS);
102
103         return TRUE;
104 }
105
106 static BOOL DeleteNode(LPNOTIFICATIONLIST item)
107 {
108         LPNOTIFICATIONLIST ptr;
109         int ret = FALSE;
110
111         TRACE("item=%p\n", item);
112
113         EnterCriticalSection(&SHELL32_ChangenotifyCS);
114
115         ptr = head.next;
116         while(ptr != &tail)
117         {
118           TRACE("ptr=%p\n", ptr);
119
120           if (ptr == item)
121           {
122             UINT i;
123
124             TRACE("item=%p prev=%p next=%p\n", item, item->prev, item->next);
125
126             /* remove item from list */
127             item->prev->next = item->next;
128             item->next->prev = item->prev;
129
130             /* free the item */
131             for (i=0; i<item->cidl;i++) SHFree(item->apidl[i].pidlPath);
132             SHFree(item->apidl);
133             SHFree(item);
134
135             ret = TRUE;
136             break;
137           }
138           ptr = ptr->next;
139         }
140
141         LeaveCriticalSection(&SHELL32_ChangenotifyCS);
142         return ret;
143
144 }
145
146 /*************************************************************************
147  * SHChangeNotifyRegister                       [SHELL32.2]
148  *
149  */
150 HANDLE WINAPI
151 SHChangeNotifyRegister(
152     HWND hwnd,
153     LONG dwFlags,
154     LONG wEventMask,
155     DWORD uMsg,
156     int cItems,
157     LPCNOTIFYREGISTER lpItems)
158 {
159         LPNOTIFICATIONLIST item;
160         int i;
161
162         item = SHAlloc(sizeof(NOTIFICATIONLIST));
163
164         TRACE("(%p,0x%08lx,0x%08lx,0x%08lx,0x%08x,%p) item=%p\n",
165                 hwnd,dwFlags,wEventMask,uMsg,cItems,lpItems,item);
166
167         item->next = NULL;
168         item->prev = NULL;
169         item->cidl = cItems;
170         item->apidl = SHAlloc(sizeof(NOTIFYREGISTER) * cItems);
171         for(i=0;i<cItems;i++)
172         {
173           item->apidl[i].pidlPath = ILClone(lpItems[i].pidlPath);
174           item->apidl[i].bWatchSubtree = lpItems[i].bWatchSubtree;
175         }
176         item->hwnd = hwnd;
177         item->uMsg = uMsg;
178         item->wEventMask = wEventMask;
179         item->dwFlags = dwFlags;
180         AddNode(item);
181         return (HANDLE)item;
182 }
183
184 /*************************************************************************
185  * SHChangeNotifyDeregister                     [SHELL32.4]
186  */
187 BOOL WINAPI
188 SHChangeNotifyDeregister(HANDLE hNotify)
189 {
190         TRACE("(%p)\n",hNotify);
191
192         return DeleteNode((LPNOTIFICATIONLIST)hNotify);
193 }
194
195 /*************************************************************************
196  * SHChangeNotifyUpdateEntryList                [SHELL32.5]
197  */
198 BOOL WINAPI
199 SHChangeNotifyUpdateEntryList(DWORD unknown1, DWORD unknown2,
200                               DWORD unknown3, DWORD unknown4)
201 {
202         FIXME("(0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx)\n",
203               unknown1, unknown2, unknown3, unknown4);
204
205         return -1;
206 }
207
208 /*************************************************************************
209  * SHChangeNotify                               [SHELL32.@]
210  */
211 void WINAPI SHChangeNotify(LONG wEventId, UINT uFlags, LPCVOID dwItem1, LPCVOID dwItem2)
212 {
213         LPITEMIDLIST Pidls[2];
214         LPNOTIFICATIONLIST ptr;
215         DWORD dummy;
216         UINT typeFlag = uFlags & SHCNF_TYPE;
217
218         Pidls[0] = (LPITEMIDLIST)dwItem1;
219         Pidls[1] = (LPITEMIDLIST)dwItem2;
220
221         TRACE("(0x%08lx,0x%08x,%p,%p):stub.\n", wEventId, uFlags, dwItem1, dwItem2);
222
223         /* convert paths in IDLists*/
224         switch (typeFlag)
225         {
226           case SHCNF_PATHA:
227             if (dwItem1) SHILCreateFromPathA((LPCSTR)dwItem1, &Pidls[0], &dummy);
228             if (dwItem2) SHILCreateFromPathA((LPCSTR)dwItem2, &Pidls[1], &dummy);
229             break;
230           case SHCNF_PATHW:
231             if (dwItem1) SHILCreateFromPathW((LPCWSTR)dwItem1, &Pidls[0], &dummy);
232             if (dwItem2) SHILCreateFromPathW((LPCWSTR)dwItem2, &Pidls[1], &dummy);
233             break;
234           case SHCNF_PRINTERA:
235           case SHCNF_PRINTERW:
236             FIXME("SHChangeNotify with (uFlags & SHCNF_PRINTER)");
237             break;
238         }
239
240         EnterCriticalSection(&SHELL32_ChangenotifyCS);
241
242         /* loop through the list */
243         ptr = head.next;
244         while (ptr != &tail)
245         {
246           TRACE("trying %p\n", ptr);
247
248           if (wEventId & ptr->wEventMask)
249           {
250             TRACE("notifying\n");
251             SendMessageA(ptr->hwnd, ptr->uMsg, (WPARAM)&Pidls, (LPARAM)wEventId);
252           }
253           ptr = ptr->next;
254         }
255
256         LeaveCriticalSection(&SHELL32_ChangenotifyCS);
257
258         /* if we allocated it, free it */
259         if ((typeFlag == SHCNF_PATHA) || (typeFlag == SHCNF_PATHW))
260         {
261           if (Pidls[0]) SHFree(Pidls[0]);
262           if (Pidls[1]) SHFree(Pidls[1]);
263         }
264 }
265
266 /*************************************************************************
267  * NTSHChangeNotifyRegister                     [SHELL32.640]
268  * NOTES
269  *   Idlist is an array of structures and Count specifies how many items in the array
270  *   (usually just one I think).
271  */
272 DWORD WINAPI NTSHChangeNotifyRegister(
273     HWND hwnd,
274     LONG events1,
275     LONG events2,
276     DWORD msg,
277     int count,
278     LPNOTIFYREGISTER idlist)
279 {
280         FIXME("(%p,0x%08lx,0x%08lx,0x%08lx,0x%08x,%p):stub.\n",
281                 hwnd,events1,events2,msg,count,idlist);
282         return 0;
283 }
284
285 /*************************************************************************
286  * SHChangeNotification_Lock                    [SHELL32.644]
287  */
288 HANDLE WINAPI SHChangeNotification_Lock(
289         HANDLE hMemoryMap,
290         DWORD dwProcessId,
291         LPCITEMIDLIST **lppidls,
292         LPLONG lpwEventId)
293 {
294         FIXME("\n");
295         return 0;
296 }
297
298 /*************************************************************************
299  * SHChangeNotification_Unlock                  [SHELL32.645]
300  */
301 BOOL WINAPI SHChangeNotification_Unlock (
302         HANDLE hLock)
303 {
304         FIXME("\n");
305         return 0;
306 }
307
308 /*************************************************************************
309  * NTSHChangeNotifyDeregister                   [SHELL32.641]
310  */
311 DWORD WINAPI NTSHChangeNotifyDeregister(LONG x1)
312 {
313         FIXME("(0x%08lx):stub.\n",x1);
314         return 0;
315 }