changenotify.c 13.1 KB
Newer Older
1 2 3
/*
 *	shell change notification
 *
4
 * Copyright 2000 Juergen Schmied
5
 *
6 7 8 9 10 11 12 13 14 15 16 17
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20
 */

21
#include <stdarg.h>
22 23
#include <string.h>

24 25
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
26
#include "windef.h"
27 28
#include "winbase.h"
#include "wine/debug.h"
29
#include "wingdi.h"
30 31
#include "shell32_main.h"

32
WINE_DEFAULT_DEBUG_CHANNEL(shell);
33

34 35 36 37 38
static CRITICAL_SECTION SHELL32_ChangenotifyCS;
static CRITICAL_SECTION_DEBUG critsect_debug =
{
    0, 0, &SHELL32_ChangenotifyCS,
    { &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 44
typedef SHChangeNotifyEntry *LPNOTIFYREGISTER;

45
/* internal list of notification clients (internal) */
46 47 48
typedef struct _NOTIFICATIONLIST
{
	struct _NOTIFICATIONLIST *next;
49
	struct _NOTIFICATIONLIST *prev;
50 51
	HWND hwnd;		/* window to notify */
	DWORD uMsg;		/* message to send */
52
	LPNOTIFYREGISTER apidl; /* array of entries to watch*/
53 54
	UINT cidl;		/* number of pidls in array */
	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 60
} NOTIFICATIONLIST, *LPNOTIFICATIONLIST;

61
static NOTIFICATIONLIST *head, *tail;
62

63 64
#define SHCNE_NOITEMEVENTS ( \
   SHCNE_ASSOCCHANGED )
65

66 67 68 69 70 71
#define SHCNE_ONEITEMEVENTS ( \
   SHCNE_ATTRIBUTES | SHCNE_CREATE | SHCNE_DELETE | SHCNE_DRIVEADD | \
   SHCNE_DRIVEADDGUI | SHCNE_DRIVEREMOVED | SHCNE_FREESPACE | \
   SHCNE_MEDIAINSERTED | SHCNE_MEDIAREMOVED | SHCNE_MKDIR | \
   SHCNE_NETSHARE | SHCNE_NETUNSHARE | SHCNE_RMDIR | \
   SHCNE_SERVERDISCONNECT | SHCNE_UPDATEDIR | SHCNE_UPDATEIMAGE )
72

73 74
#define SHCNE_TWOITEMEVENTS ( \
   SHCNE_RENAMEFOLDER | SHCNE_RENAMEITEM | SHCNE_UPDATEITEM )
75

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
/* for dumping events */
static const char * DumpEvent( LONG event )
{
    if( event == SHCNE_ALLEVENTS )
        return "SHCNE_ALLEVENTS";
#define DUMPEV(x)  ,( event & SHCNE_##x )? #x " " : ""
    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"
    DUMPEV(RENAMEITEM)
    DUMPEV(CREATE)
    DUMPEV(DELETE)
    DUMPEV(MKDIR)
    DUMPEV(RMDIR)
    DUMPEV(MEDIAINSERTED)
    DUMPEV(MEDIAREMOVED)
    DUMPEV(DRIVEREMOVED)
    DUMPEV(DRIVEADD)
    DUMPEV(NETSHARE)
    DUMPEV(NETUNSHARE)
    DUMPEV(ATTRIBUTES)
    DUMPEV(UPDATEDIR)
    DUMPEV(UPDATEITEM)
    DUMPEV(SERVERDISCONNECT)
    DUMPEV(UPDATEIMAGE)
    DUMPEV(DRIVEADDGUI)
    DUMPEV(RENAMEFOLDER)
    DUMPEV(FREESPACE)
    DUMPEV(EXTENDED_EVENT)
    DUMPEV(ASSOCCHANGED)
    DUMPEV(INTERRUPT)
    );
#undef DUMPEV
107 108
}

109
static const char * NodeName(LPNOTIFICATIONLIST item)
110
{
111 112 113
    const char *str;
    WCHAR path[MAX_PATH];

114
    if(SHGetPathFromIDListW(item->apidl[0].pidl, path ))
115 116 117 118
        str = wine_dbg_sprintf("%s", debugstr_w(path));
    else
        str = wine_dbg_sprintf("<not a disk file>" );
    return str;
119 120
}

121
static void AddNode(LPNOTIFICATIONLIST item)
122
{
123 124 125 126 127 128 129 130 131 132 133
    TRACE("item %p\n", item );

    /* link items */
    item->prev = tail;
    item->next = NULL;
    if( tail )
        tail->next = item;
    else
        head = item;
    tail = item;
}
134

135 136 137 138 139 140 141 142
static LPNOTIFICATIONLIST FindNode( HANDLE hitem )
{
    LPNOTIFICATIONLIST ptr;
    for( ptr = head; ptr; ptr = ptr->next )
        if( ptr == (LPNOTIFICATIONLIST) hitem )
            return ptr;
    return NULL;
}
143

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
static void DeleteNode(LPNOTIFICATIONLIST item)
{
    UINT i;

    TRACE("item=%p prev=%p next=%p\n", item, item->prev, item->next);

    /* remove item from list */
    if( item->prev )
        item->prev->next = item->next;
    else
        head = item->next;
    if( item->next )
        item->next->prev = item->prev;
    else
        tail = item->prev;

    /* free the item */
    for (i=0; i<item->cidl; i++)
162
        SHFree((LPITEMIDLIST)item->apidl[i].pidl);
163 164 165
    SHFree(item->apidl);
    SHFree(item);
}
166

167 168 169
void InitChangeNotifications(void)
{
}
170

171 172 173
void FreeChangeNotifications(void)
{
    TRACE("\n");
174

175
    EnterCriticalSection(&SHELL32_ChangenotifyCS);
176

177 178
    while( head )
        DeleteNode( head );
179

180
    LeaveCriticalSection(&SHELL32_ChangenotifyCS);
181

182
    DeleteCriticalSection(&SHELL32_ChangenotifyCS);
183 184 185 186 187 188
}

/*************************************************************************
 * SHChangeNotifyRegister			[SHELL32.2]
 *
 */
189
ULONG WINAPI
190 191
SHChangeNotifyRegister(
    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 202

    item = SHAlloc(sizeof(NOTIFICATIONLIST));

203
    TRACE("(%p,0x%08x,0x%08x,0x%08x,%d,%p) item=%p\n",
204
	hwnd, fSources, wEventMask, uMsg, cItems, lpItems, item);
205 206 207 208

    item->next = NULL;
    item->prev = NULL;
    item->cidl = cItems;
209
    item->apidl = SHAlloc(sizeof(SHChangeNotifyEntry) * cItems);
210 211
    for(i=0;i<cItems;i++)
    {
212 213
        item->apidl[i].pidl = ILClone(lpItems[i].pidl);
        item->apidl[i].fRecursive = lpItems[i].fRecursive;
214 215 216 217 218
    }
    item->hwnd = hwnd;
    item->uMsg = uMsg;
    item->wEventMask = wEventMask;
    item->wSignalledEvent = 0;
219
    item->dwFlags = fSources;
220 221 222 223 224 225 226 227 228

    TRACE("new node: %s\n", NodeName( item ));

    EnterCriticalSection(&SHELL32_ChangenotifyCS);

    AddNode(item);

    LeaveCriticalSection(&SHELL32_ChangenotifyCS);

229
    return (ULONG)item;
230 231 232 233 234
}

/*************************************************************************
 * SHChangeNotifyDeregister			[SHELL32.4]
 */
235
BOOL WINAPI SHChangeNotifyDeregister(ULONG hNotify)
236
{
237 238
    LPNOTIFICATIONLIST node;

239
    TRACE("(0x%08x)\n", hNotify);
240 241 242

    EnterCriticalSection(&SHELL32_ChangenotifyCS);

243
    node = FindNode((HANDLE)hNotify);
244 245 246 247
    if( node )
        DeleteNode(node);

    LeaveCriticalSection(&SHELL32_ChangenotifyCS);
248

249
    return node?TRUE:FALSE;
250 251
}

252 253 254
/*************************************************************************
 * SHChangeNotifyUpdateEntryList       		[SHELL32.5]
 */
255
BOOL WINAPI SHChangeNotifyUpdateEntryList(DWORD unknown1, DWORD unknown2,
256 257
			      DWORD unknown3, DWORD unknown4)
{
258
    FIXME("(0x%08x, 0x%08x, 0x%08x, 0x%08x)\n",
259
          unknown1, unknown2, unknown3, unknown4);
260

261 262 263
    return -1;
}

Eric Pouech's avatar
Eric Pouech committed
264
static BOOL should_notify( LPCITEMIDLIST changed, LPCITEMIDLIST watched, BOOL sub )
265 266 267 268 269 270 271 272 273
{
    TRACE("%p %p %d\n", changed, watched, sub );
    if ( !watched )
        return FALSE;
    if (ILIsEqual( watched, changed ) )
        return TRUE;
    if( sub && ILIsParent( watched, changed, FALSE ) )
        return TRUE;
    return FALSE;
274 275
}

276
/*************************************************************************
277
 * SHChangeNotify				[SHELL32.@]
278
 */
279
void WINAPI SHChangeNotify(LONG wEventId, UINT uFlags, LPCVOID dwItem1, LPCVOID dwItem2)
280
{
Eric Pouech's avatar
Eric Pouech committed
281
    LPCITEMIDLIST Pidls[2];
282 283 284 285 286 287
    LPNOTIFICATIONLIST ptr;
    UINT typeFlag = uFlags & SHCNF_TYPE;

    Pidls[0] = NULL;
    Pidls[1] = NULL;

288
    TRACE("(0x%08x,0x%08x,%p,%p):stub.\n", wEventId, uFlags, dwItem1, dwItem2);
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318

    if( ( wEventId & SHCNE_NOITEMEVENTS ) && ( dwItem1 || dwItem2 ) )
    {
        TRACE("dwItem1 and dwItem2 are not zero, but should be\n");
        dwItem1 = 0;
        dwItem2 = 0;
        return;
    }
    else if( ( wEventId & SHCNE_ONEITEMEVENTS ) && dwItem2 )
    {
        TRACE("dwItem2 is not zero, but should be\n");
        dwItem2 = 0;
        return;
    }

    if( ( ( wEventId & SHCNE_NOITEMEVENTS ) && 
          ( wEventId & ~SHCNE_NOITEMEVENTS ) ) ||
        ( ( wEventId & SHCNE_ONEITEMEVENTS ) && 
          ( wEventId & ~SHCNE_ONEITEMEVENTS ) ) ||
        ( ( wEventId & SHCNE_TWOITEMEVENTS ) && 
          ( wEventId & ~SHCNE_TWOITEMEVENTS ) ) )
    {
        WARN("mutually incompatible events listed\n");
        return;
    }

    /* convert paths in IDLists*/
    switch (typeFlag)
    {
    case SHCNF_PATHA:
319 320
        if (dwItem1) Pidls[0] = SHSimpleIDListFromPathA((LPCSTR)dwItem1);
        if (dwItem2) Pidls[1] = SHSimpleIDListFromPathA((LPCSTR)dwItem2);
321 322
        break;
    case SHCNF_PATHW:
323 324
        if (dwItem1) Pidls[0] = SHSimpleIDListFromPathW((LPCWSTR)dwItem1);
        if (dwItem2) Pidls[1] = SHSimpleIDListFromPathW((LPCWSTR)dwItem2);
325 326
        break;
    case SHCNF_IDLIST:
Eric Pouech's avatar
Eric Pouech committed
327 328
        Pidls[0] = (LPCITEMIDLIST)dwItem1;
        Pidls[1] = (LPCITEMIDLIST)dwItem2;
329 330 331
        break;
    case SHCNF_PRINTERA:
    case SHCNF_PRINTERW:
332
        FIXME("SHChangeNotify with (uFlags & SHCNF_PRINTER)\n");
333 334 335 336 337 338 339 340 341 342 343
        return;
    case SHCNF_DWORD:
    default:
        FIXME("unknown type %08x\n",typeFlag);
        return;
    }

    {
        WCHAR path[MAX_PATH];

        if( Pidls[0] && SHGetPathFromIDListW(Pidls[0], path ))
344
            TRACE("notify %08x on item1 = %s\n", wEventId, debugstr_w(path));
345 346
    
        if( Pidls[1] && SHGetPathFromIDListW(Pidls[1], path ))
347
            TRACE("notify %08x on item2 = %s\n", wEventId, debugstr_w(path));
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
    }

    EnterCriticalSection(&SHELL32_ChangenotifyCS);

    /* loop through the list */
    for( ptr = head; ptr; ptr = ptr->next )
    {
        BOOL notify;
        DWORD i;

        notify = FALSE;

        TRACE("trying %p\n", ptr);

        for( i=0; (i<ptr->cidl) && !notify ; i++ )
        {
364 365
            LPCITEMIDLIST pidl = ptr->apidl[i].pidl;
            BOOL subtree = ptr->apidl[i].fRecursive;
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

            if (wEventId & ptr->wEventMask)
            {
                if( !pidl )          /* all ? */
                    notify = TRUE;
                else if( wEventId & SHCNE_NOITEMEVENTS )
                    notify = TRUE;
                else if( wEventId & ( SHCNE_ONEITEMEVENTS | SHCNE_TWOITEMEVENTS ) )
                    notify = should_notify( Pidls[0], pidl, subtree );
                else if( wEventId & SHCNE_TWOITEMEVENTS )
                    notify = should_notify( Pidls[1], pidl, subtree );
            }
        }

        if( !notify )
            continue;

        ptr->pidlSignaled = ILClone(Pidls[0]);

385
        TRACE("notifying %s, event %s(%x) before\n", NodeName( ptr ), DumpEvent(
386 387 388
               wEventId ),wEventId );

        ptr->wSignalledEvent |= wEventId;
389

390 391 392 393
        if (ptr->dwFlags  & SHCNRF_NewDelivery)
            SendMessageA(ptr->hwnd, ptr->uMsg, (WPARAM) ptr, (LPARAM) GetCurrentProcessId());
        else
            SendMessageA(ptr->hwnd, ptr->uMsg, (WPARAM)Pidls, wEventId);
394

395
        TRACE("notifying %s, event %s(%x) after\n", NodeName( ptr ), DumpEvent(
396 397 398 399 400 401
                wEventId ),wEventId );

    }
    TRACE("notify Done\n");
    LeaveCriticalSection(&SHELL32_ChangenotifyCS);
    
402 403
    /* if we allocated it, free it. The ANSI flag is also set in its Unicode sibling. */
    if ((typeFlag & SHCNF_PATHA) || (typeFlag & SHCNF_PRINTERA))
404
    {
405 406
        SHFree((LPITEMIDLIST)Pidls[0]);
        SHFree((LPITEMIDLIST)Pidls[1]);
407
    }
408 409 410 411 412 413 414 415 416 417 418 419 420 421
}

/*************************************************************************
 * NTSHChangeNotifyRegister			[SHELL32.640]
 * NOTES
 *   Idlist is an array of structures and Count specifies how many items in the array
 *   (usually just one I think).
 */
DWORD WINAPI NTSHChangeNotifyRegister(
    HWND hwnd,
    LONG events1,
    LONG events2,
    DWORD msg,
    int count,
422
    SHChangeNotifyEntry *idlist)
423
{
424
    FIXME("(%p,0x%08x,0x%08x,0x%08x,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 432 433
}

/*************************************************************************
 * SHChangeNotification_Lock			[SHELL32.644]
 */
HANDLE WINAPI SHChangeNotification_Lock(
434
	HANDLE hChange,
435
	DWORD dwProcessId,
436
	LPITEMIDLIST **lppidls,
437 438
	LPLONG lpwEventId)
{
439 440
    DWORD i;
    LPNOTIFICATIONLIST node;
Eric Pouech's avatar
Eric Pouech committed
441
    LPCITEMIDLIST *idlist;
442

443
    TRACE("%p %08x %p %p\n", hChange, dwProcessId, lppidls, lpwEventId);
444 445 446 447 448 449 450 451

    /* EnterCriticalSection(&SHELL32_ChangenotifyCS); */

    node = FindNode( hChange );
    if( node )
    {
        idlist = SHAlloc( sizeof(LPCITEMIDLIST *) * node->cidl );
        for(i=0; i<node->cidl; i++)
Eric Pouech's avatar
Eric Pouech committed
452
            idlist[i] = (LPCITEMIDLIST)node->pidlSignaled;
453
        *lpwEventId = node->wSignalledEvent;
Eric Pouech's avatar
Eric Pouech committed
454
        *lppidls = (LPITEMIDLIST*)idlist;
455 456 457 458 459 460 461 462
        node->wSignalledEvent = 0;
    }
    else
        ERR("Couldn't find %p\n", hChange );

    /* LeaveCriticalSection(&SHELL32_ChangenotifyCS); */

    return (HANDLE) node;
463 464 465 466 467
}

/*************************************************************************
 * SHChangeNotification_Unlock			[SHELL32.645]
 */
468
BOOL WINAPI SHChangeNotification_Unlock ( HANDLE hLock)
469
{
470 471
    TRACE("\n");
    return 1;
472 473 474 475 476
}

/*************************************************************************
 * NTSHChangeNotifyDeregister			[SHELL32.641]
 */
477
DWORD WINAPI NTSHChangeNotifyDeregister(ULONG x1)
478
{
479
    FIXME("(0x%08x):semi stub.\n",x1);
480

481
    return SHChangeNotifyDeregister( x1 );
482
}