recyclebin.c 40.1 KB
Newer Older
1
/*
2
 * Trash virtual folder support
3 4
 *
 * Copyright (C) 2006 Mikolaj Zalewski
5
 * Copyright 2011 Jay Yang
6
 * Copyright 2021 Alexandre Julliard
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#define COBJMACROS
24
#define NONAMELESSUNION
25 26 27 28 29 30

#include <stdarg.h>

#include "winerror.h"
#include "windef.h"
#include "winbase.h"
31
#include "winternl.h"
32 33 34
#include "winreg.h"
#include "winuser.h"
#include "shlwapi.h"
35
#include "ntquery.h"
36 37
#include "shlobj.h"
#include "shresdef.h"
38
#include "shellfolder.h"
39
#include "shellapi.h"
40
#include "knownfolders.h"
41 42 43
#include "wine/debug.h"

#include "shell32_main.h"
44
#include "pidl.h"
45
#include "shfldr.h"
46 47 48

WINE_DEFAULT_DEBUG_CHANNEL(recyclebin);

49 50 51 52 53 54 55 56 57 58
static const shvheader RecycleBinColumns[] =
{
    {&FMTID_Storage,   PID_STG_NAME,       IDS_SHV_COLUMN1,        SHCOLSTATE_TYPE_STR|SHCOLSTATE_ONBYDEFAULT,  LVCFMT_LEFT,  30},
    {&FMTID_Displaced, PID_DISPLACED_FROM, IDS_SHV_COLUMN_DELFROM, SHCOLSTATE_TYPE_STR|SHCOLSTATE_ONBYDEFAULT,  LVCFMT_LEFT,  30},
    {&FMTID_Displaced, PID_DISPLACED_DATE, IDS_SHV_COLUMN_DELDATE, SHCOLSTATE_TYPE_DATE|SHCOLSTATE_ONBYDEFAULT, LVCFMT_LEFT,  20},
    {&FMTID_Storage,   PID_STG_SIZE,       IDS_SHV_COLUMN2,        SHCOLSTATE_TYPE_INT|SHCOLSTATE_ONBYDEFAULT,  LVCFMT_RIGHT, 20},
    {&FMTID_Storage,   PID_STG_STORAGETYPE,IDS_SHV_COLUMN3,        SHCOLSTATE_TYPE_INT|SHCOLSTATE_ONBYDEFAULT,  LVCFMT_LEFT,  20},
    {&FMTID_Storage,   PID_STG_WRITETIME,  IDS_SHV_COLUMN4,        SHCOLSTATE_TYPE_DATE|SHCOLSTATE_ONBYDEFAULT, LVCFMT_LEFT,  20},
/*    {&FMTID_Storage,   PID_STG_CREATETIME, "creation time",  SHCOLSTATE_TYPE_DATE,                        LVCFMT_LEFT,  20}, */
/*    {&FMTID_Storage,   PID_STG_ATTRIBUTES, "attribs",        SHCOLSTATE_TYPE_STR,                         LVCFMT_LEFT,  20},       */
59 60 61 62 63 64 65 66 67 68 69
};

#define COLUMN_NAME    0
#define COLUMN_DELFROM 1
#define COLUMN_DATEDEL 2
#define COLUMN_SIZE    3
#define COLUMN_TYPE    4
#define COLUMN_MTIME   5

#define COLUMNS_COUNT  6

70 71 72 73 74 75 76
static const WIN32_FIND_DATAW *get_trash_item_data( LPCITEMIDLIST id )
{
    if (id->mkid.cb < 2 + 1 + sizeof(WIN32_FIND_DATAW) + 2) return NULL;
    if (id->mkid.abID[0] != 0) return NULL;
    return (const WIN32_FIND_DATAW *)(id->mkid.abID + 1);
}

77 78 79 80 81
static INIT_ONCE trash_dir_once;
static WCHAR *trash_dir;
static WCHAR *trash_info_dir;
static ULONG random_seed;

82 83 84 85 86 87 88 89 90 91
extern void CDECL wine_get_host_version( const char **sysname, const char **release );

static BOOL is_macos(void)
{
    const char *sysname;

    wine_get_host_version( &sysname, NULL );
    return !strcmp( sysname, "Darwin" );
}

92 93
static BOOL WINAPI init_trash_dirs( INIT_ONCE *once, void *param, void **context )
{
94 95
    const WCHAR *home = _wgetenv( L"WINEHOMEDIR" );
    WCHAR *info = NULL, *files = NULL;
96
    ULONG len;
97

98 99 100
    if (!home) return TRUE;
    if (is_macos())
    {
101
        files = heap_alloc( (lstrlenW(home) + lstrlenW(L"\\.Trash") + 1) * sizeof(WCHAR) );
102
        lstrcpyW( files, home );
103
        lstrcatW( files, L"\\.Trash" );
104 105 106
        files[1] = '\\';  /* change \??\ to \\?\ */
    }
    else
107
    {
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
        const WCHAR *data_home = _wgetenv( L"XDG_DATA_HOME" );
        const WCHAR *fmt = L"%s/.local/share/Trash";
        WCHAR *p;

        if (data_home && data_home[0] == '/')
        {
            home = data_home;
            fmt = L"\\??\\unix%s/Trash";
        }
        len = lstrlenW(home) + lstrlenW(fmt) + 7;
        files = heap_alloc( len * sizeof(WCHAR) );
        swprintf( files, len, fmt, home );
        files[1] = '\\';  /* change \??\ to \\?\ */
        for (p = files; *p; p++) if (*p == '/') *p = '\\';
        CreateDirectoryW( files, NULL );
        info = heap_alloc( len * sizeof(WCHAR) );
        lstrcpyW( info, files );
        lstrcatW( files, L"\\files" );
        lstrcatW( info, L"\\info" );
        if (!CreateDirectoryW( info, NULL ) && GetLastError() != ERROR_ALREADY_EXISTS) goto done;
        trash_info_dir = info;
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
    }

    if (!CreateDirectoryW( files, NULL ) && GetLastError() != ERROR_ALREADY_EXISTS) goto done;
    trash_dir = files;
    random_seed = GetTickCount();
    return TRUE;

done:
    heap_free( files );
    heap_free( info );
    return TRUE;
}

BOOL is_trash_available(void)
{
    InitOnceExecuteOnce( &trash_dir_once, init_trash_dirs, NULL, NULL );
    return trash_dir != NULL;
}

static void encode( const char *src, char *dst )
{
    static const char escape_chars[] = "^&`{}|[]'<>\\#%\"+";
    static const char hexchars[] = "0123456789ABCDEF";

    for ( ; *src; src++)
    {
        if (*src <= 0x20 || *src >= 0x7f || strchr( escape_chars, *src ))
        {
            *dst++ = '%';
            *dst++ = hexchars[(unsigned char)*src / 16];
            *dst++ = hexchars[(unsigned char)*src % 16];
        }
        else *dst++ = *src;
    }
    *dst = 0;
}

static char *decode( char *buf )
{
    const char *src = buf;
    char *dst = buf;  /* decode in place */

    while (*src)
    {
        if (*src == '%')
        {
            unsigned char v;

            if (src[1] >= '0' && src[1] <= '9') v = src[1] - '0';
            else if (src[1] >= 'A' && src[1] <= 'F') v = src[1] - 'A' + 10;
            else if (src[1] >= 'a' && src[1] <= 'f') v = src[1] - 'a' + 10;
            else goto next;
            v <<= 4;
            if (src[2] >= '0' && src[2] <= '9') v += src[2] - '0';
            else if (src[2] >= 'A' && src[2] <= 'F') v += src[2] - 'A' + 10;
            else if (src[2] >= 'a' && src[2] <= 'f') v += src[2] - 'a' + 10;
            else goto next;
            *dst++ = v;
        next:
            if (src[1] && src[2]) src += 3;
            else break;
        }
        else *dst++ = *src++;
    }
    *dst = 0;
    return buf;
}

static BOOL write_trashinfo_file( HANDLE handle, const WCHAR *orig_path )
{
    char *path, *buffer;
    SYSTEMTIME now;

    if (!(path = wine_get_unix_file_name( orig_path ))) return FALSE;
    GetLocalTime( &now );

    buffer = heap_alloc( strlen(path) * 3 + 128 );
    strcpy( buffer, "[Trash Info]\nPath=" );
    encode( path, buffer + strlen(buffer) );
    sprintf( buffer + strlen(buffer), "\nDeletionDate=%04u-%02u-%02uT%02u:%02u:%02u\n",
             now.wYear, now.wMonth, now.wDay, now.wHour, now.wMinute, now.wSecond);
    WriteFile( handle, buffer, strlen(buffer), NULL, NULL );
    heap_free( path );
    heap_free( buffer );
    return TRUE;
}

static void read_trashinfo_file( HANDLE handle, WIN32_FIND_DATAW *data )
{
    ULONG size;
    WCHAR *path;
    char *buffer, *next, *p;
    int header = 0;

    size = GetFileSize( handle, NULL );
    if (!(buffer = heap_alloc( size + 1 ))) return;
    if (!ReadFile( handle, buffer, size, &size, NULL )) size = 0;
    buffer[size] = 0;
    next = buffer;
    while (next)
    {
        p = next;
        if ((next = strchr( next, '\n' ))) *next++ = 0;
        while (*p == ' ' || *p == '\t') p++;
        if (!strncmp( p, "[Trash Info]", 12 ))
        {
            header++;
            continue;
        }
        if (header && !strncmp( p, "Path=", 5 ))
        {
            p += 5;
            if ((path = wine_get_dos_file_name( decode( p ))))
            {
                lstrcpynW( data->cFileName, path, MAX_PATH );
                heap_free( path );
            }
            else
            {
                /* show only the file name */
                char *file = strrchr( p, '/' );
                if (!file) file = p;
                else file++;
                MultiByteToWideChar( CP_UNIXCP, 0, file, -1, data->cFileName, MAX_PATH );
            }
            continue;
        }
        if (header && !strncmp( p, "DeletionDate=", 13 ))
        {
            int year, mon, day, hour, min, sec;
            if (sscanf( p + 13, "%d-%d-%dT%d:%d:%d", &year, &mon, &day, &hour, &min, &sec ) == 6)
            {
                SYSTEMTIME time = { year, mon, 0, day, hour, min, sec };
                FILETIME ft;
                if (SystemTimeToFileTime( &time, &ft ))
                    LocalFileTimeToFileTime( &ft, &data->ftLastAccessTime );
            }
        }
    }
    heap_free( buffer );
}


BOOL trash_file( const WCHAR *path )
{
    WCHAR *dest = NULL, *file = PathFindFileNameW( path );
    BOOL ret = TRUE;
276
    ULONG i, len;
277 278 279 280

    InitOnceExecuteOnce( &trash_dir_once, init_trash_dirs, NULL, NULL );
    if (!trash_dir) return FALSE;

281 282
    len = lstrlenW(trash_dir) + lstrlenW(file) + 11;
    dest = heap_alloc( len * sizeof(WCHAR) );
283 284 285 286

    if (trash_info_dir)
    {
        HANDLE handle;
287 288
        ULONG infolen = lstrlenW(trash_info_dir) + lstrlenW(file) + 21;
        WCHAR *info = heap_alloc( infolen * sizeof(WCHAR) );
289

290
        swprintf( info, infolen, L"%s\\%s.trashinfo", trash_info_dir, file );
291 292 293 294
        for (i = 0; i < 1000; i++)
        {
            handle = CreateFileW( info, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, 0 );
            if (handle != INVALID_HANDLE_VALUE) break;
295
            swprintf( info, infolen, L"%s\\%s-%08x.trashinfo", trash_info_dir, file, RtlRandom( &random_seed ));
296 297 298 299 300
        }
        if (handle != INVALID_HANDLE_VALUE)
        {
            if ((ret = write_trashinfo_file( handle, path )))
            {
301
                ULONG namelen = lstrlenW(info) - lstrlenW(trash_info_dir) - 10 /* .trashinfo */;
302
                swprintf( dest, len, L"%s%.*s", trash_dir, namelen, info + lstrlenW(trash_info_dir) );
303 304 305 306 307 308 309 310
                ret = MoveFileW( path, dest );
            }
            CloseHandle( handle );
            if (!ret) DeleteFileW( info );
        }
    }
    else
    {
311
        swprintf( dest, len, L"%s\\%s", trash_dir, file );
312 313 314 315
        for (i = 0; i < 1000; i++)
        {
            ret = MoveFileW( path, dest );
            if (ret || GetLastError() != ERROR_ALREADY_EXISTS) break;
316
            swprintf( dest, len, L"%s\\%s-%08x", trash_dir, file, RtlRandom( &random_seed ));
317 318 319 320 321 322 323 324 325 326 327
        }
    }
    if (ret) TRACE( "%s -> %s\n", debugstr_w(path), debugstr_w(dest) );
    heap_free( dest );
    return ret;
}

static BOOL get_trash_item_info( const WCHAR *filename, WIN32_FIND_DATAW *data )
{
    if (!trash_info_dir)
    {
328
        return !!wcscmp( filename, L".DS_Store" );
329 330 331 332
    }
    else
    {
        HANDLE handle;
333 334
        ULONG len = lstrlenW(trash_info_dir) + lstrlenW(filename) + 12;
        WCHAR *info = heap_alloc( len * sizeof(WCHAR) );
335

336
        swprintf( info, len, L"%s\\%s.trashinfo", trash_info_dir, filename );
337 338
        handle = CreateFileW( info, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
        heap_free( info );
339
        if (handle == INVALID_HANDLE_VALUE) return FALSE;
340 341 342 343 344 345 346 347 348 349 350 351
        read_trashinfo_file( handle, data );
        CloseHandle( handle );
        return TRUE;
    }
}

static HRESULT add_trash_item( WIN32_FIND_DATAW *orig_data, LPITEMIDLIST **pidls,
                               ULONG *count, ULONG *size )
{
    ITEMIDLIST *pidl;
    WIN32_FIND_DATAW *data;
    const WCHAR *filename = orig_data->cFileName;
352
    ULONG len = offsetof( ITEMIDLIST, mkid.abID[1 + sizeof(*data) + (lstrlenW(filename)+1) * sizeof(WCHAR)]);
353 354 355 356 357 358

    if (!(pidl = SHAlloc( len + 2 ))) return E_OUTOFMEMORY;
    pidl->mkid.cb = len;
    pidl->mkid.abID[0] = 0;
    data = (WIN32_FIND_DATAW *)(pidl->mkid.abID + 1);
    memcpy( data, orig_data, sizeof(*data) );
359
    lstrcpyW( (WCHAR *)(data + 1), filename );
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
    *(USHORT *)((char *)pidl + len) = 0;

    if (get_trash_item_info( filename, data ))
    {
        if (*count == *size)
        {
            LPITEMIDLIST *new;

            *size = max( *size * 2, 32 );
            new = heap_realloc( *pidls, *size * sizeof(**pidls) );
            if (!new)
            {
                SHFree( pidl );
                return E_OUTOFMEMORY;
            }
            *pidls = new;
        }
        (*pidls)[(*count)++] = pidl;
    }
    else SHFree( pidl );  /* ignore it */
    return S_OK;
}

static HRESULT enum_trash_items( LPITEMIDLIST **pidls, int *ret_count )
{
    HANDLE handle;
    WCHAR *file;
    WIN32_FIND_DATAW data;
    LPITEMIDLIST *ret = NULL;
    ULONG count = 0, size = 0;
    HRESULT hr = S_OK;

    InitOnceExecuteOnce( &trash_dir_once, init_trash_dirs, NULL, NULL );
    if (!trash_dir) return E_FAIL;

395
    *pidls = NULL;
396
    file = heap_alloc( (lstrlenW(trash_dir) + lstrlenW(L"\\*") + 1) * sizeof(WCHAR) );
397
    lstrcpyW( file, trash_dir );
398
    lstrcatW( file, L"\\*" );
399 400 401 402 403
    handle = FindFirstFileW( file, &data );
    if (handle != INVALID_HANDLE_VALUE)
    {
        do
        {
404 405
            if (!wcscmp( data.cFileName, L"." )) continue;
            if (!wcscmp( data.cFileName, L".." )) continue;
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
            hr = add_trash_item( &data, &ret, &count, &size );
        } while (hr == S_OK && FindNextFileW( handle, &data ));
        FindClose( handle );
    }

    if (FAILED(hr)) while (count) SHFree( &ret[--count] );
    else if (count)
    {
        *pidls = SHAlloc( count * sizeof(**pidls) );
        memcpy( *pidls, ret, count * sizeof(**pidls) );
    }
    heap_free( ret );
    *ret_count = count;
    return hr;
}

static void remove_trashinfo( const WCHAR *filename )
{
    WCHAR *info;
425
    ULONG len;
426 427

    if (!trash_info_dir) return;
428 429 430
    len = lstrlenW(trash_info_dir) + lstrlenW(filename) + 12;
    info = heap_alloc( len * sizeof(WCHAR) );
    swprintf( info, len, L"%s\\%s.trashinfo", trash_info_dir, filename );
431 432 433 434 435 436 437 438
    DeleteFileW( info );
    heap_free( info );
}

static HRESULT restore_trash_item( LPCITEMIDLIST pidl )
{
    const WIN32_FIND_DATAW *data = get_trash_item_data( pidl );
    WCHAR *from, *filename = (WCHAR *)(data + 1);
439
    ULONG len;
440

441
    if (!wcschr( data->cFileName, '\\' ))
442 443 444 445
    {
        FIXME( "original name for %s not available\n", debugstr_w(data->cFileName) );
        return E_NOTIMPL;
    }
446 447 448
    len = lstrlenW(trash_dir) + lstrlenW(filename) + 2;
    from = heap_alloc( len * sizeof(WCHAR) );
    swprintf( from, len, L"%s\\%s", trash_dir, filename );
449 450 451 452 453 454 455 456 457 458
    if (MoveFileW( from, data->cFileName )) remove_trashinfo( filename );
    else WARN( "failed to restore %s to %s\n", debugstr_w(from), debugstr_w(data->cFileName) );
    heap_free( from );
    return S_OK;
}

static HRESULT erase_trash_item( LPCITEMIDLIST pidl )
{
    const WIN32_FIND_DATAW *data = get_trash_item_data( pidl );
    WCHAR *from, *filename = (WCHAR *)(data + 1);
459
    ULONG len = lstrlenW(trash_dir) + lstrlenW(filename) + 2;
460

461 462
    from = heap_alloc( len * sizeof(WCHAR) );
    swprintf( from, len, L"%s\\%s", trash_dir, filename );
463 464 465 466 467 468
    if (DeleteFileW( from )) remove_trashinfo( filename );
    heap_free( from );
    return S_OK;
}


469
static HRESULT FormatDateTime(LPWSTR buffer, int size, FILETIME ft)
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
{
    FILETIME lft;
    SYSTEMTIME time;
    int ret;

    FileTimeToLocalFileTime(&ft, &lft);
    FileTimeToSystemTime(&lft, &time);

    ret = GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &time, NULL, buffer, size);
    if (ret>0 && ret<size)
    {
        /* Append space + time without seconds */
        buffer[ret-1] = ' ';
        GetTimeFormatW(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &time, NULL, &buffer[ret], size - ret);
    }

    return (ret!=0 ? E_FAIL : S_OK);
}

489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
typedef struct tagRecycleBinMenu
{
    IContextMenu2 IContextMenu2_iface;
    LONG refCount;

    UINT cidl;
    LPITEMIDLIST *apidl;
    IShellFolder2 *folder;
} RecycleBinMenu;

static const IContextMenu2Vtbl recycleBinMenuVtbl;

static RecycleBinMenu *impl_from_IContextMenu2(IContextMenu2 *iface)
{
    return CONTAINING_RECORD(iface, RecycleBinMenu, IContextMenu2_iface);
}

static IContextMenu2* RecycleBinMenu_Constructor(UINT cidl, LPCITEMIDLIST *apidl, IShellFolder2 *folder)
{
    RecycleBinMenu *This = SHAlloc(sizeof(RecycleBinMenu));
    TRACE("(%u,%p)\n",cidl,apidl);
    This->IContextMenu2_iface.lpVtbl = &recycleBinMenuVtbl;
    This->cidl = cidl;
    This->apidl = _ILCopyaPidl(apidl,cidl);
    IShellFolder2_AddRef(folder);
    This->folder = folder;
    This->refCount = 1;
    return &This->IContextMenu2_iface;
}

static HRESULT WINAPI RecycleBinMenu_QueryInterface(IContextMenu2 *iface,
                                                    REFIID riid,
                                                    void **ppvObject)
{
    RecycleBinMenu *This = impl_from_IContextMenu2(iface);
    TRACE("(%p, %s, %p) - stub\n", This, debugstr_guid(riid), ppvObject);
    return E_NOTIMPL;
}

static ULONG WINAPI RecycleBinMenu_AddRef(IContextMenu2 *iface)
{
    RecycleBinMenu *This = impl_from_IContextMenu2(iface);
    TRACE("(%p)\n", This);
    return InterlockedIncrement(&This->refCount);

}

static ULONG WINAPI RecycleBinMenu_Release(IContextMenu2 *iface)
{
    RecycleBinMenu *This = impl_from_IContextMenu2(iface);
    UINT result;
    TRACE("(%p)\n", This);
    result = InterlockedDecrement(&This->refCount);
    if (result == 0)
    {
        TRACE("Destroying object\n");
        _ILFreeaPidl(This->apidl,This->cidl);
546
        IShellFolder2_Release(This->folder);
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
        SHFree(This);
    }
    return result;
}

static HRESULT WINAPI RecycleBinMenu_QueryContextMenu(IContextMenu2 *iface,
                                                      HMENU hmenu,
                                                      UINT indexMenu,
                                                      UINT idCmdFirst,
                                                      UINT idCmdLast,
                                                      UINT uFlags)
{
    HMENU menures = LoadMenuW(shell32_hInstance,MAKEINTRESOURCEW(MENU_RECYCLEBIN));
    if(uFlags & CMF_DEFAULTONLY)
        return E_NOTIMPL;
    else{
        UINT idMax = Shell_MergeMenus(hmenu,GetSubMenu(menures,0),indexMenu,idCmdFirst,idCmdLast,MM_SUBMENUSHAVEIDS);
564
        TRACE("Added %d id(s)\n",idMax-idCmdFirst);
565 566 567 568 569 570 571 572 573 574 575 576
        return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, idMax-idCmdFirst+1);
    }
}

static void DoErase(RecycleBinMenu *This)
{
    ISFHelper *helper;
    IShellFolder2_QueryInterface(This->folder,&IID_ISFHelper,(void**)&helper);
    if(helper)
        ISFHelper_DeleteItems(helper,This->cidl,(LPCITEMIDLIST*)This->apidl);
}

577 578 579 580 581 582 583
static void DoRestore(RecycleBinMenu *This)
{

    /*TODO add prompts*/
    UINT i;
    for(i=0;i<This->cidl;i++)
    {
584 585 586
        const WIN32_FIND_DATAW *data = get_trash_item_data( This->apidl[i] );

        if(PathFileExistsW(data->cFileName))
587
        {
588
            PIDLIST_ABSOLUTE dest_pidl = ILCreateFromPathW(data->cFileName);
589 590 591
            WCHAR message[100];
            WCHAR caption[50];
            if(_ILIsFolder(ILFindLastID(dest_pidl)))
592
                LoadStringW(shell32_hInstance, IDS_RECYCLEBIN_OVERWRITEFOLDER, message, ARRAY_SIZE(message));
593
            else
594 595
                LoadStringW(shell32_hInstance, IDS_RECYCLEBIN_OVERWRITEFILE, message, ARRAY_SIZE(message));
            LoadStringW(shell32_hInstance, IDS_RECYCLEBIN_OVERWRITE_CAPTION, caption, ARRAY_SIZE(caption));
596 597 598

            if(ShellMessageBoxW(shell32_hInstance,GetActiveWindow(),message,
                                caption,MB_YESNO|MB_ICONEXCLAMATION,
599
                                data->cFileName)!=IDYES)
600 601
                continue;
        }
602
        if(SUCCEEDED(restore_trash_item(This->apidl[i])))
603 604 605
        {
            IPersistFolder2 *persist;
            LPITEMIDLIST root_pidl;
606
            PIDLIST_ABSOLUTE dest_pidl = ILCreateFromPathW(data->cFileName);
607 608 609 610 611 612 613 614 615 616 617 618 619
            BOOL is_folder = _ILIsFolder(ILFindLastID(dest_pidl));
            IShellFolder2_QueryInterface(This->folder,&IID_IPersistFolder2,
                                         (void**)&persist);
            IPersistFolder2_GetCurFolder(persist,&root_pidl);
            SHChangeNotify(is_folder ? SHCNE_RMDIR : SHCNE_DELETE,
                           SHCNF_IDLIST,ILCombine(root_pidl,This->apidl[i]),0);
            SHChangeNotify(is_folder ? SHCNE_MKDIR : SHCNE_CREATE,
                           SHCNF_IDLIST,dest_pidl,0);
            ILFree(dest_pidl);
            ILFree(root_pidl);
        }
    }
}
620 621 622 623 624 625

static HRESULT WINAPI RecycleBinMenu_InvokeCommand(IContextMenu2 *iface,
                                                   LPCMINVOKECOMMANDINFO pici)
{
    RecycleBinMenu *This = impl_from_IContextMenu2(iface);
    LPCSTR verb = pici->lpVerb;
626
    if(IS_INTRESOURCE(verb))
627
    {
628
        switch(LOWORD(verb))
629 630 631 632
        {
        case IDM_RECYCLEBIN_ERASE:
            DoErase(This);
            break;
633 634 635
        case IDM_RECYCLEBIN_RESTORE:
            DoRestore(This);
            break;
636 637 638 639 640 641 642 643 644 645 646 647 648 649
        default:
            return E_NOTIMPL;
        }
    }
    return S_OK;
}

static HRESULT WINAPI RecycleBinMenu_GetCommandString(IContextMenu2 *iface,
                                                      UINT_PTR idCmd,
                                                      UINT uType,
                                                      UINT *pwReserved,
                                                      LPSTR pszName,
                                                      UINT cchMax)
{
650
    TRACE("(%p, %Iu, %u, %p, %s, %u) - stub\n",iface,idCmd,uType,pwReserved,debugstr_a(pszName),cchMax);
651 652 653 654 655 656 657
    return E_NOTIMPL;
}

static HRESULT WINAPI RecycleBinMenu_HandleMenuMsg(IContextMenu2 *iface,
                                                   UINT uMsg, WPARAM wParam,
                                                   LPARAM lParam)
{
658
    TRACE("(%p, %u, 0x%Ix, 0x%Ix) - stub\n",iface,uMsg,wParam,lParam);
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
    return E_NOTIMPL;
}


static const IContextMenu2Vtbl recycleBinMenuVtbl =
{
    RecycleBinMenu_QueryInterface,
    RecycleBinMenu_AddRef,
    RecycleBinMenu_Release,
    RecycleBinMenu_QueryContextMenu,
    RecycleBinMenu_InvokeCommand,
    RecycleBinMenu_GetCommandString,
    RecycleBinMenu_HandleMenuMsg,
};

674 675 676 677 678 679
/*
 * Recycle Bin folder
 */

typedef struct tagRecycleBin
{
680 681
    IShellFolder2 IShellFolder2_iface;
    IPersistFolder2 IPersistFolder2_iface;
682
    ISFHelper ISFHelper_iface;
683 684 685 686 687
    LONG refCount;

    LPITEMIDLIST pidl;
} RecycleBin;

688 689
static const IShellFolder2Vtbl recycleBinVtbl;
static const IPersistFolder2Vtbl recycleBinPersistVtbl;
690
static const ISFHelperVtbl sfhelperVtbl;
691

692
static inline RecycleBin *impl_from_IShellFolder2(IShellFolder2 *iface)
693
{
694 695 696 697 698 699
    return CONTAINING_RECORD(iface, RecycleBin, IShellFolder2_iface);
}

static RecycleBin *impl_from_IPersistFolder2(IPersistFolder2 *iface)
{
    return CONTAINING_RECORD(iface, RecycleBin, IPersistFolder2_iface);
700 701
}

702 703 704 705 706
static RecycleBin *impl_from_ISFHelper(ISFHelper *iface)
{
    return CONTAINING_RECORD(iface, RecycleBin, ISFHelper_iface);
}

707
static void RecycleBin_Destructor(RecycleBin *This);
708 709 710 711 712 713 714 715 716 717 718 719

HRESULT WINAPI RecycleBin_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppOutput)
{
    RecycleBin *obj;
    HRESULT ret;
    if (pUnkOuter)
        return CLASS_E_NOAGGREGATION;

    obj = SHAlloc(sizeof(RecycleBin));
    if (obj == NULL)
        return E_OUTOFMEMORY;
    ZeroMemory(obj, sizeof(RecycleBin));
720 721
    obj->IShellFolder2_iface.lpVtbl = &recycleBinVtbl;
    obj->IPersistFolder2_iface.lpVtbl = &recycleBinPersistVtbl;
722
    obj->ISFHelper_iface.lpVtbl = &sfhelperVtbl;
723
    if (FAILED(ret = IPersistFolder2_QueryInterface(&obj->IPersistFolder2_iface, riid, ppOutput)))
724 725 726 727 728 729 730 731
    {
        RecycleBin_Destructor(obj);
        return ret;
    }
/*    InterlockedIncrement(&objCount);*/
    return S_OK;
}

732
static void RecycleBin_Destructor(RecycleBin *This)
733 734 735 736 737 738 739 740
{
/*    InterlockedDecrement(&objCount);*/
    SHFree(This->pidl);
    SHFree(This);
}

static HRESULT WINAPI RecycleBin_QueryInterface(IShellFolder2 *iface, REFIID riid, void **ppvObject)
{
741
    RecycleBin *This = impl_from_IShellFolder2(iface);
742 743 744 745 746
    TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppvObject);

    *ppvObject = NULL;
    if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_IShellFolder)
            || IsEqualGUID(riid, &IID_IShellFolder2))
747
        *ppvObject = &This->IShellFolder2_iface;
748 749 750

    if (IsEqualGUID(riid, &IID_IPersist) || IsEqualGUID(riid, &IID_IPersistFolder)
            || IsEqualGUID(riid, &IID_IPersistFolder2))
751
        *ppvObject = &This->IPersistFolder2_iface;
752 753
    if (IsEqualGUID(riid, &IID_ISFHelper))
        *ppvObject = &This->ISFHelper_iface;
754 755 756 757 758 759 760 761 762 763 764 765

    if (*ppvObject != NULL)
    {
        IUnknown_AddRef((IUnknown *)*ppvObject);
        return S_OK;
    }
    WARN("no interface %s\n", debugstr_guid(riid));
    return E_NOINTERFACE;
}

static ULONG WINAPI RecycleBin_AddRef(IShellFolder2 *iface)
{
766
    RecycleBin *This = impl_from_IShellFolder2(iface);
767 768 769 770 771 772
    TRACE("(%p)\n", This);
    return InterlockedIncrement(&This->refCount);
}

static ULONG WINAPI RecycleBin_Release(IShellFolder2 *iface)
{
773
    RecycleBin *This = impl_from_IShellFolder2(iface);
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
    LONG result;

    TRACE("(%p)\n", This);
    result = InterlockedDecrement(&This->refCount);
    if (result == 0)
    {
        TRACE("Destroy object\n");
        RecycleBin_Destructor(This);
    }
    return result;
}

static HRESULT WINAPI RecycleBin_ParseDisplayName(IShellFolder2 *This, HWND hwnd, LPBC pbc,
            LPOLESTR pszDisplayName, ULONG *pchEaten, LPITEMIDLIST *ppidl,
            ULONG *pdwAttributes)
{
    FIXME("stub\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI RecycleBin_EnumObjects(IShellFolder2 *iface, HWND hwnd, SHCONTF grfFlags, IEnumIDList **ppenumIDList)
{
796
    RecycleBin *This = impl_from_IShellFolder2(iface);
797
    IEnumIDListImpl *list;
798
    LPITEMIDLIST *pidls;
799
    HRESULT ret = E_OUTOFMEMORY;
800
    int pidls_count = 0;
801
    int i=0;
802

803
    TRACE("(%p, %p, %lx, %p)\n", This, hwnd, grfFlags, ppenumIDList);
804

805 806 807 808 809
    *ppenumIDList = NULL;
    list = IEnumIDList_Constructor();
    if (!list)
        return E_OUTOFMEMORY;

810 811
    if (grfFlags & SHCONTF_NONFOLDERS)
    {
812
        if (FAILED(ret = enum_trash_items(&pidls, &pidls_count)))
813 814
            goto failed;
        for (i=0; i<pidls_count; i++)
815
            if (!AddToEnumList(list, pidls[i]))
816 817
                goto failed;
    }
818

819
    *ppenumIDList = &list->IEnumIDList_iface;
820 821 822
    return S_OK;

failed:
823
    IEnumIDList_Release(&list->IEnumIDList_iface);
824 825 826
    for (; i<pidls_count; i++)
        ILFree(pidls[i]);
    SHFree(pidls);
827
    return ret;
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
}

static HRESULT WINAPI RecycleBin_BindToObject(IShellFolder2 *This, LPCITEMIDLIST pidl, LPBC pbc, REFIID riid, void **ppv)
{
    FIXME("(%p, %p, %p, %s, %p) - stub\n", This, pidl, pbc, debugstr_guid(riid), ppv);
    return E_NOTIMPL;
}

static HRESULT WINAPI RecycleBin_BindToStorage(IShellFolder2 *This, LPCITEMIDLIST pidl, LPBC pbc, REFIID riid, void **ppv)
{
    FIXME("(%p, %p, %p, %s, %p) - stub\n", This, pidl, pbc, debugstr_guid(riid), ppv);
    return E_NOTIMPL;
}

static HRESULT WINAPI RecycleBin_CompareIDs(IShellFolder2 *iface, LPARAM lParam, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
{
844
    RecycleBin *This = impl_from_IShellFolder2(iface);
845
    int ret;
846 847 848 849 850

    /* TODO */
    TRACE("(%p, %p, %p, %p)\n", This, (void *)lParam, pidl1, pidl2);
    if (pidl1->mkid.cb != pidl2->mkid.cb)
        return MAKE_HRESULT(SEVERITY_SUCCESS, 0, pidl1->mkid.cb - pidl2->mkid.cb);
851 852 853 854 855 856 857
    /* Looks too complicated, but in optimized memcpy we might get
     * a 32bit wide difference and would truncate it to 16 bit, so
     * erroneously returning equality. */
    ret = memcmp(pidl1->mkid.abID, pidl2->mkid.abID, pidl1->mkid.cb);
    if (ret < 0) ret = -1;
    if (ret > 0) ret =  1;
    return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (unsigned short)ret);
858 859 860 861
}

static HRESULT WINAPI RecycleBin_CreateViewObject(IShellFolder2 *iface, HWND hwndOwner, REFIID riid, void **ppv)
{
862
    RecycleBin *This = impl_from_IShellFolder2(iface);
863 864 865 866 867 868 869 870 871 872 873
    HRESULT ret;
    TRACE("(%p, %p, %s, %p)\n", This, hwndOwner, debugstr_guid(riid), ppv);

    *ppv = NULL;
    if (IsEqualGUID(riid, &IID_IShellView))
    {
        IShellView *tmp;
        CSFV sfv;

        ZeroMemory(&sfv, sizeof(sfv));
        sfv.cbSize = sizeof(sfv);
874
        sfv.pshf = (IShellFolder *)&This->IShellFolder2_iface;
875 876 877 878 879 880 881

        TRACE("Calling SHCreateShellFolderViewEx\n");
        ret = SHCreateShellFolderViewEx(&sfv, &tmp);
        TRACE("Result: %08x, output: %p\n", (unsigned int)ret, tmp);
        *ppv = tmp;
        return ret;
    }
882 883
    else
        FIXME("invalid/unsupported interface %s\n", debugstr_guid(riid));
884 885 886 887 888 889 890

    return E_NOINTERFACE;
}

static HRESULT WINAPI RecycleBin_GetAttributesOf(IShellFolder2 *This, UINT cidl, LPCITEMIDLIST *apidl,
                                   SFGAOF *rgfInOut)
{
891
    TRACE("(%p, %d, {%p, ...}, {%lx})\n", This, cidl, apidl[0], *rgfInOut);
892 893 894 895
    *rgfInOut &= SFGAO_CANMOVE|SFGAO_CANDELETE|SFGAO_HASPROPSHEET|SFGAO_FILESYSTEM;
    return S_OK;
}

896
static HRESULT WINAPI RecycleBin_GetUIObjectOf(IShellFolder2 *iface, HWND hwndOwner, UINT cidl, LPCITEMIDLIST *apidl,
897 898
                      REFIID riid, UINT *rgfReserved, void **ppv)
{
899
    RecycleBin *This = impl_from_IShellFolder2(iface);
900
    *ppv = NULL;
901 902 903 904 905 906 907 908
    if(IsEqualGUID(riid, &IID_IContextMenu) || IsEqualGUID(riid, &IID_IContextMenu2))
    {
        TRACE("(%p, %p, %d, {%p, ...}, %s, %p, %p)\n", This, hwndOwner, cidl, apidl[0], debugstr_guid(riid), rgfReserved, ppv);
        *ppv = RecycleBinMenu_Constructor(cidl,apidl,&(This->IShellFolder2_iface));
        return S_OK;
    }
    FIXME("(%p, %p, %d, {%p, ...}, %s, %p, %p): stub!\n", iface, hwndOwner, cidl, apidl[0], debugstr_guid(riid), rgfReserved, ppv);

909 910 911 912 913
    return E_NOTIMPL;
}

static HRESULT WINAPI RecycleBin_GetDisplayNameOf(IShellFolder2 *This, LPCITEMIDLIST pidl, SHGDNF uFlags, STRRET *pName)
{
914
    const WIN32_FIND_DATAW *data = get_trash_item_data( pidl );
915

916
    TRACE("(%p, %p, %lx, %p)\n", This, pidl, uFlags, pName);
917
    pName->uType = STRRET_WSTR;
918
    return SHStrDupW(PathFindFileNameW(data->cFileName), &pName->u.pOleStr);
919 920 921 922 923 924 925 926 927 928 929 930 931 932
}

static HRESULT WINAPI RecycleBin_SetNameOf(IShellFolder2 *This, HWND hwnd, LPCITEMIDLIST pidl, LPCOLESTR pszName,
            SHGDNF uFlags, LPITEMIDLIST *ppidlOut)
{
    TRACE("\n");
    return E_FAIL; /* not supported */
}

static HRESULT WINAPI RecycleBin_GetClassID(IPersistFolder2 *This, CLSID *pClassID)
{
    TRACE("(%p, %p)\n", This, pClassID);
    if (This == NULL || pClassID == NULL)
        return E_INVALIDARG;
933
    *pClassID = CLSID_RecycleBin;
934 935 936 937 938
    return S_OK;
}

static HRESULT WINAPI RecycleBin_Initialize(IPersistFolder2 *iface, LPCITEMIDLIST pidl)
{
939
    RecycleBin *This = impl_from_IPersistFolder2(iface);
940
    TRACE("(%p, %p)\n", This, pidl);
941

942 943 944 945 946 947 948 949
    This->pidl = ILClone(pidl);
    if (This->pidl == NULL)
        return E_OUTOFMEMORY;
    return S_OK;
}

static HRESULT WINAPI RecycleBin_GetCurFolder(IPersistFolder2 *iface, LPITEMIDLIST *ppidl)
{
950
    RecycleBin *This = impl_from_IPersistFolder2(iface);
951 952 953 954 955
    TRACE("\n");
    *ppidl = ILClone(This->pidl);
    return S_OK;
}

956
static HRESULT WINAPI RecycleBin_GetDefaultSearchGUID(IShellFolder2 *iface, GUID *guid)
957
{
958 959
    RecycleBin *This = impl_from_IShellFolder2(iface);
    TRACE("(%p)->(%p)\n", This, guid);
960 961 962 963 964 965 966 967 968 969
    return E_NOTIMPL;
}

static HRESULT WINAPI RecycleBin_EnumSearches(IShellFolder2 *iface, IEnumExtraSearch **ppEnum)
{
    FIXME("stub\n");
    *ppEnum = NULL;
    return E_NOTIMPL;
}

970
static HRESULT WINAPI RecycleBin_GetDefaultColumn(IShellFolder2 *iface, DWORD reserved, ULONG *sort, ULONG *display)
971
{
972
    RecycleBin *This = impl_from_IShellFolder2(iface);
973

974
    TRACE("(%p)->(%#lx, %p, %p)\n", This, reserved, sort, display);
975 976

    return E_NOTIMPL;
977 978 979 980
}

static HRESULT WINAPI RecycleBin_GetDefaultColumnState(IShellFolder2 *iface, UINT iColumn, SHCOLSTATEF *pcsFlags)
{
981
    RecycleBin *This = impl_from_IShellFolder2(iface);
982
    TRACE("(%p, %d, %p)\n", This, iColumn, pcsFlags);
983
    if (iColumn >= COLUMNS_COUNT)
984 985 986 987 988 989 990 991 992 993 994 995 996
        return E_INVALIDARG;
    *pcsFlags = RecycleBinColumns[iColumn].pcsFlags;
    return S_OK;
}

static HRESULT WINAPI RecycleBin_GetDetailsEx(IShellFolder2 *iface, LPCITEMIDLIST pidl, const SHCOLUMNID *pscid, VARIANT *pv)
{
    FIXME("stub\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI RecycleBin_GetDetailsOf(IShellFolder2 *iface, LPCITEMIDLIST pidl, UINT iColumn, LPSHELLDETAILS pDetails)
{
997
    RecycleBin *This = impl_from_IShellFolder2(iface);
998
    const WIN32_FIND_DATAW *data;
999 1000 1001
    WCHAR buffer[MAX_PATH];

    TRACE("(%p, %p, %d, %p)\n", This, pidl, iColumn, pDetails);
1002
    if (iColumn >= COLUMNS_COUNT)
1003
        return E_FAIL;
1004 1005

    if (!pidl) return SHELL32_GetColumnDetails( RecycleBinColumns, iColumn, pDetails );
1006 1007 1008 1009

    if (iColumn == COLUMN_NAME)
        return RecycleBin_GetDisplayNameOf(iface, pidl, SHGDN_NORMAL, &pDetails->str);

1010
    data = get_trash_item_data( pidl );
1011 1012 1013
    switch (iColumn)
    {
        case COLUMN_DATEDEL:
1014
            FormatDateTime(buffer, MAX_PATH, data->ftLastAccessTime);
1015 1016
            break;
        case COLUMN_DELFROM:
1017
            lstrcpyW(buffer, data->cFileName);
1018 1019 1020
            PathRemoveFileSpecW(buffer);
            break;
        case COLUMN_SIZE:
1021
            StrFormatKBSizeW(((LONGLONG)data->nFileSizeHigh<<32)|data->nFileSizeLow, buffer, MAX_PATH);
1022 1023
            break;
        case COLUMN_MTIME:
1024
            FormatDateTime(buffer, MAX_PATH, data->ftLastWriteTime);
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
            break;
        case COLUMN_TYPE:
            /* TODO */
            buffer[0] = 0;
            break;
        default:
            return E_FAIL;
    }
    
    pDetails->str.uType = STRRET_WSTR;
1035
    return SHStrDupW(buffer, &pDetails->str.u.pOleStr);
1036 1037 1038 1039
}

static HRESULT WINAPI RecycleBin_MapColumnToSCID(IShellFolder2 *iface, UINT iColumn, SHCOLUMNID *pscid)
{
1040
    RecycleBin *This = impl_from_IShellFolder2(iface);
1041
    TRACE("(%p, %d, %p)\n", This, iColumn, pscid);
1042
    if (iColumn>=COLUMNS_COUNT)
1043
        return E_INVALIDARG;
1044 1045

    return shellfolder_map_column_to_scid(RecycleBinColumns, iColumn, pscid);
1046 1047
}

1048
static const IShellFolder2Vtbl recycleBinVtbl = 
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
{
    /* IUnknown */
    RecycleBin_QueryInterface,
    RecycleBin_AddRef,
    RecycleBin_Release,

    /* IShellFolder */
    RecycleBin_ParseDisplayName,
    RecycleBin_EnumObjects,
    RecycleBin_BindToObject,
    RecycleBin_BindToStorage,
    RecycleBin_CompareIDs,
    RecycleBin_CreateViewObject,
    RecycleBin_GetAttributesOf,
    RecycleBin_GetUIObjectOf,
    RecycleBin_GetDisplayNameOf,
    RecycleBin_SetNameOf,

    /* IShellFolder2 */
    RecycleBin_GetDefaultSearchGUID,
    RecycleBin_EnumSearches,
    RecycleBin_GetDefaultColumn,
    RecycleBin_GetDefaultColumnState,
    RecycleBin_GetDetailsEx,
    RecycleBin_GetDetailsOf,
    RecycleBin_MapColumnToSCID
};

1077 1078
static HRESULT WINAPI RecycleBin_IPersistFolder2_QueryInterface(IPersistFolder2 *iface, REFIID riid,
        void **ppvObject)
1079
{
1080 1081
    RecycleBin *This = impl_from_IPersistFolder2(iface);

1082
    return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppvObject);
1083 1084
}

1085
static ULONG WINAPI RecycleBin_IPersistFolder2_AddRef(IPersistFolder2 *iface)
1086
{
1087 1088
    RecycleBin *This = impl_from_IPersistFolder2(iface);

1089
    return IShellFolder2_AddRef(&This->IShellFolder2_iface);
1090 1091
}

1092
static ULONG WINAPI RecycleBin_IPersistFolder2_Release(IPersistFolder2 *iface)
1093
{
1094 1095
    RecycleBin *This = impl_from_IPersistFolder2(iface);

1096
    return IShellFolder2_Release(&This->IShellFolder2_iface);
1097 1098
}

1099
static const IPersistFolder2Vtbl recycleBinPersistVtbl =
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
{
    /* IUnknown */
    RecycleBin_IPersistFolder2_QueryInterface,
    RecycleBin_IPersistFolder2_AddRef,
    RecycleBin_IPersistFolder2_Release,

    /* IPersist */
    RecycleBin_GetClassID,
    /* IPersistFolder */
    RecycleBin_Initialize,
    /* IPersistFolder2 */
    RecycleBin_GetCurFolder
};
1113

1114 1115 1116 1117 1118
static HRESULT WINAPI RecycleBin_ISFHelper_QueryInterface(ISFHelper *iface, REFIID riid,
        void **ppvObject)
{
    RecycleBin *This = impl_from_ISFHelper(iface);

1119
    return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppvObject);
1120 1121 1122 1123 1124 1125
}

static ULONG WINAPI RecycleBin_ISFHelper_AddRef(ISFHelper *iface)
{
    RecycleBin *This = impl_from_ISFHelper(iface);

1126
    return IShellFolder2_AddRef(&This->IShellFolder2_iface);
1127 1128 1129 1130 1131 1132
}

static ULONG WINAPI RecycleBin_ISFHelper_Release(ISFHelper *iface)
{
    RecycleBin *This = impl_from_ISFHelper(iface);

1133
    return IShellFolder2_Release(&This->IShellFolder2_iface);
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
}

static HRESULT WINAPI RecycleBin_GetUniqueName(ISFHelper *iface,LPWSTR lpName,
                                               UINT  uLen)
{
    return E_NOTIMPL;
}

static HRESULT WINAPI RecycleBin_AddFolder(ISFHelper * iface, HWND hwnd,
                                           LPCWSTR pwszName,
                                           LPITEMIDLIST * ppidlOut)
{
    /*Adding folders doesn't make sense in the recycle bin*/
    return E_NOTIMPL;
}

1150
static HRESULT erase_items(HWND parent,const LPCITEMIDLIST * apidl, UINT cidl, BOOL confirm)
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
{
    UINT i=0;
    HRESULT ret = S_OK;
    LPITEMIDLIST recyclebin;

    if(confirm)
    {
        WCHAR arg[MAX_PATH];
        WCHAR message[100];
        WCHAR caption[50];
        switch(cidl)
        {
        case 0:
            return S_OK;
        case 1:
            {
1167 1168
                const WIN32_FIND_DATAW *data = get_trash_item_data( apidl[0] );
                lstrcpynW(arg,data->cFileName,MAX_PATH);
1169
                LoadStringW(shell32_hInstance, IDS_RECYCLEBIN_ERASEITEM, message, ARRAY_SIZE(message));
1170 1171 1172 1173
                break;
            }
        default:
            {
1174
                LoadStringW(shell32_hInstance, IDS_RECYCLEBIN_ERASEMULTIPLE, message, ARRAY_SIZE(message));
1175
                swprintf(arg, ARRAY_SIZE(arg), L"%u", cidl);
1176 1177 1178 1179
                break;
            }

        }
1180
        LoadStringW(shell32_hInstance, IDS_RECYCLEBIN_ERASE_CAPTION, caption, ARRAY_SIZE(caption));
1181 1182 1183 1184 1185 1186 1187 1188
        if(ShellMessageBoxW(shell32_hInstance,parent,message,caption,
                            MB_YESNO|MB_ICONEXCLAMATION,arg)!=IDYES)
            return ret;

    }
    SHGetFolderLocation(parent,CSIDL_BITBUCKET,0,0,&recyclebin);
    for (; i<cidl; i++)
    {
1189
        if(SUCCEEDED(erase_trash_item(apidl[i])))
1190 1191 1192 1193 1194 1195 1196
            SHChangeNotify(SHCNE_DELETE,SHCNF_IDLIST,
                           ILCombine(recyclebin,apidl[i]),0);
    }
    ILFree(recyclebin);
    return S_OK;
}

1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
static HRESULT WINAPI RecycleBin_DeleteItems(ISFHelper * iface, UINT cidl,
                                             LPCITEMIDLIST * apidl)
{
    TRACE("(%p,%u,%p)\n",iface,cidl,apidl);
    return erase_items(GetActiveWindow(),apidl,cidl,TRUE);
}

static HRESULT WINAPI RecycleBin_CopyItems(ISFHelper * iface,
                                           IShellFolder * pSFFrom,
                                           UINT cidl, LPCITEMIDLIST * apidl)
{
    return E_NOTIMPL;
}

static const ISFHelperVtbl sfhelperVtbl =
{
    RecycleBin_ISFHelper_QueryInterface,
    RecycleBin_ISFHelper_AddRef,
    RecycleBin_ISFHelper_Release,
    RecycleBin_GetUniqueName,
    RecycleBin_AddFolder,
    RecycleBin_DeleteItems,
    RecycleBin_CopyItems
};

1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
HRESULT WINAPI SHQueryRecycleBinA(LPCSTR pszRootPath, LPSHQUERYRBINFO pSHQueryRBInfo)
{
    WCHAR wszRootPath[MAX_PATH];
    MultiByteToWideChar(CP_ACP, 0, pszRootPath, -1, wszRootPath, MAX_PATH);
    return SHQueryRecycleBinW(wszRootPath, pSHQueryRBInfo);
}

HRESULT WINAPI SHQueryRecycleBinW(LPCWSTR pszRootPath, LPSHQUERYRBINFO pSHQueryRBInfo)
{
    LPITEMIDLIST *apidl;
    INT cidl;
    INT i=0;
1234 1235
    HRESULT hr;

1236 1237
    TRACE("(%s, %p)\n", debugstr_w(pszRootPath), pSHQueryRBInfo);

1238
    hr = enum_trash_items(&apidl, &cidl);
1239 1240
    if (FAILED(hr))
        return hr;
1241 1242 1243 1244
    pSHQueryRBInfo->i64NumItems = cidl;
    pSHQueryRBInfo->i64Size = 0;
    for (; i<cidl; i++)
    {
1245 1246
        const WIN32_FIND_DATAW *data = get_trash_item_data( apidl[i] );
        pSHQueryRBInfo->i64Size += ((DWORDLONG)data->nFileSizeHigh << 32) + data->nFileSizeLow;
1247 1248 1249 1250 1251 1252
        ILFree(apidl[i]);
    }
    SHFree(apidl);
    return S_OK;
}

1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
HRESULT WINAPI SHEmptyRecycleBinA(HWND hwnd, LPCSTR pszRootPath, DWORD dwFlags)
{
    WCHAR wszRootPath[MAX_PATH];
    MultiByteToWideChar(CP_ACP, 0, pszRootPath, -1, wszRootPath, MAX_PATH);
    return SHEmptyRecycleBinW(hwnd, wszRootPath, dwFlags);
}

#define SHERB_NOCONFIRMATION 1
#define SHERB_NOPROGRESSUI   2
#define SHERB_NOSOUND        4

HRESULT WINAPI SHEmptyRecycleBinW(HWND hwnd, LPCWSTR pszRootPath, DWORD dwFlags)
{
    LPITEMIDLIST *apidl;
    INT cidl;
    INT i=0;
    HRESULT ret;
1270

1271
    TRACE("(%p, %s, 0x%08lx)\n", hwnd, debugstr_w(pszRootPath) , dwFlags);
1272

1273
    ret = enum_trash_items(&apidl, &cidl);
1274 1275 1276
    if (FAILED(ret))
        return ret;

1277 1278 1279 1280 1281 1282 1283
    ret = erase_items(hwnd,(const LPCITEMIDLIST*)apidl,cidl,!(dwFlags & SHERB_NOCONFIRMATION));
    for (;i<cidl;i++)
        ILFree(apidl[i]);
    SHFree(apidl);
    return ret;
}

1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
/*************************************************************************
 * SHUpdateRecycleBinIcon                                [SHELL32.@]
 *
 * Undocumented
 */
HRESULT WINAPI SHUpdateRecycleBinIcon(void)
{
    FIXME("stub\n");
    return S_OK;
}