changenotify.c 14 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
#include "windef.h"
25
#include "winbase.h"
26
#include "wine/list.h"
27
#include "wine/debug.h"
28 29
#include "shell32_main.h"

30
WINE_DEFAULT_DEBUG_CHANNEL(shell);
31

32 33 34 35 36
static CRITICAL_SECTION SHELL32_ChangenotifyCS;
static CRITICAL_SECTION_DEBUG critsect_debug =
{
    0, 0, &SHELL32_ChangenotifyCS,
    { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
37
      0, 0, { (DWORD_PTR)(__FILE__ ": SHELL32_ChangenotifyCS") }
38 39
};
static CRITICAL_SECTION SHELL32_ChangenotifyCS = { &critsect_debug, -1, 0, 0, 0, 0 };
40

41 42
typedef SHChangeNotifyEntry *LPNOTIFYREGISTER;

43
/* internal list of notification clients (internal) */
44 45
typedef struct _NOTIFICATIONLIST
{
46
	struct list entry;
47 48
	HWND hwnd;		/* window to notify */
	DWORD uMsg;		/* message to send */
49
	LPNOTIFYREGISTER apidl; /* array of entries to watch*/
50 51 52
	UINT cidl;		/* number of pidls in array */
	LONG wEventMask;	/* subscribed events */
	DWORD dwFlags;		/* client flags */
53
	ULONG id;
54 55
} NOTIFICATIONLIST, *LPNOTIFICATIONLIST;

56
static struct list notifications = LIST_INIT( notifications );
57
static LONG next_id;
58

59 60
#define SHCNE_NOITEMEVENTS ( \
   SHCNE_ASSOCCHANGED )
61

62 63 64 65 66 67
#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 )
68

69 70
#define SHCNE_TWOITEMEVENTS ( \
   SHCNE_RENAMEFOLDER | SHCNE_RENAMEITEM | SHCNE_UPDATEITEM )
71

72 73 74 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
/* 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
103 104
}

105
static const char * NodeName(const NOTIFICATIONLIST *item)
106
{
107 108 109
    const char *str;
    WCHAR path[MAX_PATH];

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

117 118 119 120
static void DeleteNode(LPNOTIFICATIONLIST item)
{
    UINT i;

121
    TRACE("item=%p\n", item);
122 123

    /* remove item from list */
124
    list_remove( &item->entry );
125 126 127

    /* free the item */
    for (i=0; i<item->cidl; i++)
128
        SHFree((LPITEMIDLIST)item->apidl[i].pidl);
129 130 131
    SHFree(item->apidl);
    SHFree(item);
}
132

133 134 135
void InitChangeNotifications(void)
{
}
136

137 138
void FreeChangeNotifications(void)
{
139 140
    LPNOTIFICATIONLIST ptr, next;

141
    TRACE("\n");
142

143
    EnterCriticalSection(&SHELL32_ChangenotifyCS);
144

145 146
    LIST_FOR_EACH_ENTRY_SAFE( ptr, next, &notifications, NOTIFICATIONLIST, entry )
        DeleteNode( ptr );
147

148
    LeaveCriticalSection(&SHELL32_ChangenotifyCS);
149

150
    DeleteCriticalSection(&SHELL32_ChangenotifyCS);
151 152 153 154 155 156
}

/*************************************************************************
 * SHChangeNotifyRegister			[SHELL32.2]
 *
 */
157
ULONG WINAPI
158 159
SHChangeNotifyRegister(
    HWND hwnd,
160
    int fSources,
161
    LONG wEventMask,
162
    UINT uMsg,
163
    int cItems,
164
    SHChangeNotifyEntry *lpItems)
165
{
166
    LPNOTIFICATIONLIST item;
167
    int i;
168 169 170

    item = SHAlloc(sizeof(NOTIFICATIONLIST));

171
    TRACE("(%p,0x%08x,0x%08x,0x%08x,%d,%p) item=%p\n",
172
	hwnd, fSources, wEventMask, uMsg, cItems, lpItems, item);
173 174

    item->cidl = cItems;
175
    item->apidl = SHAlloc(sizeof(SHChangeNotifyEntry) * cItems);
176 177
    for(i=0;i<cItems;i++)
    {
178 179
        item->apidl[i].pidl = ILClone(lpItems[i].pidl);
        item->apidl[i].fRecursive = lpItems[i].fRecursive;
180 181 182 183
    }
    item->hwnd = hwnd;
    item->uMsg = uMsg;
    item->wEventMask = wEventMask;
184
    item->dwFlags = fSources;
185
    item->id = InterlockedIncrement( &next_id );
186 187 188 189 190

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

    EnterCriticalSection(&SHELL32_ChangenotifyCS);

191
    list_add_tail( &notifications, &item->entry );
192 193 194

    LeaveCriticalSection(&SHELL32_ChangenotifyCS);

195
    return item->id;
196 197 198 199 200
}

/*************************************************************************
 * SHChangeNotifyDeregister			[SHELL32.4]
 */
201
BOOL WINAPI SHChangeNotifyDeregister(ULONG hNotify)
202
{
203 204
    LPNOTIFICATIONLIST node;

205
    TRACE("(0x%08x)\n", hNotify);
206 207 208

    EnterCriticalSection(&SHELL32_ChangenotifyCS);

209 210 211 212 213 214 215 216 217
    LIST_FOR_EACH_ENTRY( node, &notifications, NOTIFICATIONLIST, entry )
    {
        if (node->id == hNotify)
        {
            DeleteNode( node );
            LeaveCriticalSection(&SHELL32_ChangenotifyCS);
            return TRUE;
        }
    }
218
    LeaveCriticalSection(&SHELL32_ChangenotifyCS);
219
    return FALSE;
220 221
}

222 223 224
/*************************************************************************
 * SHChangeNotifyUpdateEntryList       		[SHELL32.5]
 */
225
BOOL WINAPI SHChangeNotifyUpdateEntryList(DWORD unknown1, DWORD unknown2,
226 227
			      DWORD unknown3, DWORD unknown4)
{
228
    FIXME("(0x%08x, 0x%08x, 0x%08x, 0x%08x)\n",
229
          unknown1, unknown2, unknown3, unknown4);
230

231
    return TRUE;
232 233
}

234 235 236 237 238 239 240 241 242
struct new_delivery_notification
{
    LONG event;
    DWORD pidl1_size;
    DWORD pidl2_size;
    LPITEMIDLIST pidls[2];
    BYTE data[1];
};

Eric Pouech's avatar
Eric Pouech committed
243
static BOOL should_notify( LPCITEMIDLIST changed, LPCITEMIDLIST watched, BOOL sub )
244 245 246 247 248 249
{
    TRACE("%p %p %d\n", changed, watched, sub );
    if ( !watched )
        return FALSE;
    if (ILIsEqual( watched, changed ) )
        return TRUE;
250
    if( sub && ILIsParent( watched, changed, FALSE ) )
251 252
        return TRUE;
    return FALSE;
253 254
}

255
/*************************************************************************
256
 * SHChangeNotify				[SHELL32.@]
257
 */
258
void WINAPI SHChangeNotify(LONG wEventId, UINT uFlags, LPCVOID dwItem1, LPCVOID dwItem2)
259
{
260 261 262 263 264 265 266
    struct notification_recipients {
        struct list entry;
        HWND hwnd;
        DWORD msg;
        DWORD flags;
    } *cur, *next;

267
    HANDLE shared_data = NULL;
268
    LPITEMIDLIST Pidls[2];
269
    LPNOTIFICATIONLIST ptr;
270
    struct list recipients;
271 272 273 274

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

275 276 277 278
    TRACE("(0x%08x,0x%08x,%p,%p)\n", wEventId, uFlags, dwItem1, dwItem2);

    if(uFlags & ~(SHCNF_TYPE|SHCNF_FLUSH))
        FIXME("ignoring unsupported flags: %x\n", uFlags);
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

    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*/
306
    switch (uFlags & SHCNF_TYPE)
307 308
    {
    case SHCNF_PATHA:
309 310
        if (dwItem1) Pidls[0] = SHSimpleIDListFromPathA(dwItem1);
        if (dwItem2) Pidls[1] = SHSimpleIDListFromPathA(dwItem2);
311 312
        break;
    case SHCNF_PATHW:
313 314
        if (dwItem1) Pidls[0] = SHSimpleIDListFromPathW(dwItem1);
        if (dwItem2) Pidls[1] = SHSimpleIDListFromPathW(dwItem2);
315 316
        break;
    case SHCNF_IDLIST:
317 318
        Pidls[0] = ILClone(dwItem1);
        Pidls[1] = ILClone(dwItem2);
319 320 321
        break;
    case SHCNF_PRINTERA:
    case SHCNF_PRINTERW:
322
        FIXME("SHChangeNotify with (uFlags & SHCNF_PRINTER)\n");
323 324 325
        return;
    case SHCNF_DWORD:
    default:
326
        FIXME("unknown type %08x\n", uFlags & SHCNF_TYPE);
327 328 329
        return;
    }

330
    list_init(&recipients);
331
    EnterCriticalSection(&SHELL32_ChangenotifyCS);
332
    LIST_FOR_EACH_ENTRY( ptr, &notifications, NOTIFICATIONLIST, entry )
333
    {
334 335
        struct notification_recipients *item;
        BOOL notify = FALSE;
336 337 338 339
        DWORD i;

        for( i=0; (i<ptr->cidl) && !notify ; i++ )
        {
340 341
            LPCITEMIDLIST pidl = ptr->apidl[i].pidl;
            BOOL subtree = ptr->apidl[i].fRecursive;
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358

            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;

359 360 361 362 363
        item = SHAlloc(sizeof(struct notification_recipients));
        if(!item) {
            ERR("out of memory\n");
            continue;
        }
364

365 366 367 368 369 370 371 372 373 374
        item->hwnd = ptr->hwnd;
        item->msg = ptr->uMsg;
        item->flags = ptr->dwFlags;
        list_add_tail(&recipients, &item->entry);
    }
    LeaveCriticalSection(&SHELL32_ChangenotifyCS);

    LIST_FOR_EACH_ENTRY_SAFE(cur, next, &recipients, struct notification_recipients, entry)
    {
        TRACE("notifying %p, event %s(%x)\n", cur->hwnd, DumpEvent(wEventId), wEventId);
375

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
        if (cur->flags  & SHCNRF_NewDelivery) {
            if(!shared_data) {
                struct new_delivery_notification *notification;
                UINT size1 = ILGetSize(Pidls[0]), size2 = ILGetSize(Pidls[1]);
                UINT offset = (size1+sizeof(int)-1)/sizeof(int)*sizeof(int);

                notification = SHAlloc(sizeof(struct new_delivery_notification)+offset+size2);
                if(!notification) {
                    ERR("out of memory\n");
                } else {
                    notification->event = wEventId;
                    notification->pidl1_size = size1;
                    notification->pidl2_size = size2;
                    if(size1)
                        memcpy(notification->data, Pidls[0], size1);
                    if(size2)
                        memcpy(notification->data+offset, Pidls[1], size2);

                    shared_data = SHAllocShared(notification,
                        sizeof(struct new_delivery_notification)+size1+size2,
                        GetCurrentProcessId());
                    SHFree(notification);
                }
            }
400

401 402 403 404 405
            if(shared_data)
                SendMessageA(cur->hwnd, cur->msg, (WPARAM)shared_data, GetCurrentProcessId());
            else
                ERR("out of memory\n");
        } else {
406
            SendMessageA(cur->hwnd, cur->msg, (WPARAM)Pidls, wEventId);
407
        }
408

409 410
        list_remove(&cur->entry);
        SHFree(cur);
411
    }
412
    SHFreeShared(shared_data, GetCurrentProcessId());
413 414
    SHFree(Pidls[0]);
    SHFree(Pidls[1]);
415 416 417

    if (wEventId & SHCNE_ASSOCCHANGED)
    {
418
        static const WCHAR args[] = {' ','-','a',0 };
419
        TRACE("refreshing file type associations\n");
420
        run_winemenubuilder( args );
421
    }
422 423 424 425 426 427 428 429 430 431 432 433 434 435
}

/*************************************************************************
 * 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,
436
    SHChangeNotifyEntry *idlist)
437
{
438
    FIXME("(%p,0x%08x,0x%08x,0x%08x,0x%08x,%p):semi stub.\n",
439
		hwnd,events1,events2,msg,count,idlist);
440

441
    return SHChangeNotifyRegister(hwnd, events1, events2, msg, count, idlist);
442 443 444 445 446 447
}

/*************************************************************************
 * SHChangeNotification_Lock			[SHELL32.644]
 */
HANDLE WINAPI SHChangeNotification_Lock(
448
	HANDLE hChange,
449
	DWORD dwProcessId,
450
	LPITEMIDLIST **lppidls,
451 452
	LPLONG lpwEventId)
{
453 454
    struct new_delivery_notification *ndn;
    UINT offset;
455

456
    TRACE("%p %08x %p %p\n", hChange, dwProcessId, lppidls, lpwEventId);
457

458 459 460 461 462
    ndn = SHLockShared(hChange, dwProcessId);
    if(!ndn) {
        WARN("SHLockShared failed\n");
        return NULL;
    }
463

464 465 466 467 468
    if(lppidls) {
        offset = (ndn->pidl1_size+sizeof(int)-1)/sizeof(int)*sizeof(int);
        ndn->pidls[0] = ndn->pidl1_size ? (LPITEMIDLIST)ndn->data : NULL;
        ndn->pidls[1] = ndn->pidl2_size ? (LPITEMIDLIST)(ndn->data+offset) : NULL;
        *lppidls = ndn->pidls;
469 470
    }

471 472
    if(lpwEventId)
        *lpwEventId = ndn->event;
473

474
    return ndn;
475 476 477 478 479
}

/*************************************************************************
 * SHChangeNotification_Unlock			[SHELL32.645]
 */
480
BOOL WINAPI SHChangeNotification_Unlock ( HANDLE hLock)
481
{
482 483
    TRACE("%p\n", hLock);
    return SHUnlockShared(hLock);
484 485 486 487 488
}

/*************************************************************************
 * NTSHChangeNotifyDeregister			[SHELL32.641]
 */
489
DWORD WINAPI NTSHChangeNotifyDeregister(ULONG x1)
490
{
491
    FIXME("(0x%08x):semi stub.\n",x1);
492

493
    return SHChangeNotifyDeregister( x1 );
494
}