aclmulti.c 8.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
/*
 *	Multisource AutoComplete list
 *
 *	Copyright 2007	Mikolaj Zalewski
 *
 * 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
 */

#include "config.h"

#include <stdarg.h>

#define COBJMACROS

#include "wine/debug.h"
#include "windef.h"
#include "winbase.h"
#include "winreg.h"
#include "winuser.h"
#include "shlwapi.h"
#include "winerror.h"
#include "objbase.h"

#include "shlguid.h"
#include "shlobj.h"

#include "wine/unicode.h"

#include "browseui.h"

WINE_DEFAULT_DEBUG_CHANNEL(browseui);

struct ACLMultiSublist {
    IUnknown *punk;
    IEnumString *pEnum;
    IACList *pACL;
};

typedef struct tagACLMulti {
    const IEnumStringVtbl *vtbl;
    const IACListVtbl *aclVtbl;
    const IObjMgrVtbl *objmgrVtbl;
    LONG refCount;
    INT nObjs;
    INT currObj;
    struct ACLMultiSublist *objs;
} ACLMulti;

static inline ACLMulti *impl_from_IACList(IACList *iface)
{
    return (ACLMulti *)((char *)iface - FIELD_OFFSET(ACLMulti, aclVtbl));
}

static inline ACLMulti *impl_from_IObjMgr(IObjMgr *iface)
{
    return (ACLMulti *)((char *)iface - FIELD_OFFSET(ACLMulti, objmgrVtbl));
}

static void release_obj(struct ACLMultiSublist *obj)
{
    IUnknown_Release(obj->punk);
    if (obj->pEnum)
        IEnumString_Release(obj->pEnum);
    if (obj->pACL)
        IACList_Release(obj->pACL);
}

Jacek Caban's avatar
Jacek Caban committed
80
static void ACLMulti_Destructor(ACLMulti *This)
81 82 83 84 85
{
    int i;
    TRACE("destroying %p\n", This);
    for (i = 0; i < This->nObjs; i++)
        release_obj(&This->objs[i]);
Jacek Caban's avatar
Jacek Caban committed
86 87
    heap_free(This->objs);
    heap_free(This);
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    BROWSEUI_refCount--;
}

static HRESULT WINAPI ACLMulti_QueryInterface(IEnumString *iface, REFIID iid, LPVOID *ppvOut)
{
    ACLMulti *This = (ACLMulti *)iface;
    *ppvOut = NULL;

    if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumString))
    {
        *ppvOut = This;
    }
    else if (IsEqualIID(iid, &IID_IACList))
    {
        *ppvOut = &This->aclVtbl;
    }
    else if (IsEqualIID(iid, &IID_IObjMgr))
    {
        *ppvOut = &This->objmgrVtbl;
    }

    if (*ppvOut)
    {
        IUnknown_AddRef(iface);
        return S_OK;
    }

    WARN("unsupported interface: %s\n", debugstr_guid(iid));
    return E_NOINTERFACE;
}

static ULONG WINAPI ACLMulti_AddRef(IEnumString *iface)
{
    ACLMulti *This = (ACLMulti *)iface;
    return InterlockedIncrement(&This->refCount);
}

static ULONG WINAPI ACLMulti_Release(IEnumString *iface)
{
    ACLMulti *This = (ACLMulti *)iface;
    ULONG ret;

    ret = InterlockedDecrement(&This->refCount);
    if (ret == 0)
        ACLMulti_Destructor(This);
    return ret;
}

static HRESULT WINAPI ACLMulti_Append(IObjMgr *iface, IUnknown *obj)
{
    ACLMulti *This = impl_from_IObjMgr(iface);

    TRACE("(%p, %p)\n", This, obj);
    if (obj == NULL)
        return E_FAIL;

Jacek Caban's avatar
Jacek Caban committed
144
    This->objs = heap_realloc(This->objs, sizeof(This->objs[0]) * (This->nObjs+1));
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    This->objs[This->nObjs].punk = obj;
    IUnknown_AddRef(obj);
    if (FAILED(IUnknown_QueryInterface(obj, &IID_IEnumString, (LPVOID *)&This->objs[This->nObjs].pEnum)))
        This->objs[This->nObjs].pEnum = NULL;
    if (FAILED(IUnknown_QueryInterface(obj, &IID_IACList, (LPVOID *)&This->objs[This->nObjs].pACL)))
        This->objs[This->nObjs].pACL = NULL;
    This->nObjs++;
    return S_OK;
}

static HRESULT WINAPI ACLMulti_Remove(IObjMgr *iface, IUnknown *obj)
{
    ACLMulti *This = impl_from_IObjMgr(iface);
    int i;

    TRACE("(%p, %p)\n", This, obj);
    for (i = 0; i < This->nObjs; i++)
        if (This->objs[i].punk == obj)
        {
            release_obj(&This->objs[i]);
            memmove(&This->objs[i], &This->objs[i+1], (This->nObjs-i-1)*sizeof(struct ACLMultiSublist));
            This->nObjs--;
Jacek Caban's avatar
Jacek Caban committed
167
            This->objs = heap_realloc(This->objs, sizeof(This->objs[0]) * This->nObjs);
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
            return S_OK;
        }

    return E_FAIL;
}

static HRESULT WINAPI ACLMulti_Next(IEnumString *iface, ULONG celt, LPOLESTR *rgelt, ULONG *pceltFetched)
{
    ACLMulti *This = (ACLMulti *)iface;

    TRACE("(%p, %d, %p, %p)\n", iface, celt, rgelt, pceltFetched);
    while (This->currObj < This->nObjs)
    {
        if (This->objs[This->currObj].pEnum)
        {
            /* native browseui 6.0 also returns only one element */
            HRESULT ret = IEnumString_Next(This->objs[This->currObj].pEnum, 1, rgelt, pceltFetched);
            if (ret != S_FALSE)
                return ret;
        }
        This->currObj++;
    }

    if (pceltFetched)
        *pceltFetched = 0;
    *rgelt = NULL;
    return S_FALSE;
}

static HRESULT WINAPI ACLMulti_Reset(IEnumString *iface)
{
    ACLMulti *This = (ACLMulti *)iface;
    int i;

    This->currObj = 0;
    for (i = 0; i < This->nObjs; i++)
    {
        if (This->objs[i].pEnum)
            IEnumString_Reset(This->objs[i].pEnum);
    }
    return S_OK;
}

static HRESULT WINAPI ACLMulti_Skip(IEnumString *iface, ULONG celt)
{
    /* native browseui 6.0 returns this: */
    return E_NOTIMPL;
}

static HRESULT WINAPI ACLMulti_Clone(IEnumString *iface, IEnumString **ppOut)
{
    *ppOut = NULL;
    /* native browseui 6.0 returns this: */
    return E_OUTOFMEMORY;
}

static HRESULT WINAPI ACLMulti_Expand(IACList *iface, LPCWSTR wstr)
{
    ACLMulti *This = impl_from_IACList(iface);
    HRESULT res = S_OK;
    int i;

    for (i = 0; i < This->nObjs; i++)
    {
        if (!This->objs[i].pACL)
            continue;
        res = IACList_Expand(This->objs[i].pACL, wstr);
235
        /* Vista behaviour - XP would break out of the loop if res == S_OK (usually calling Expand only once) */
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 276 277 278 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
    }
    return res;
}

static const IEnumStringVtbl ACLMultiVtbl =
{
    ACLMulti_QueryInterface,
    ACLMulti_AddRef,
    ACLMulti_Release,

    ACLMulti_Next,
    ACLMulti_Skip,
    ACLMulti_Reset,
    ACLMulti_Clone
};

static HRESULT WINAPI ACLMulti_IObjMgr_QueryInterface(IObjMgr *iface, REFIID iid, LPVOID *ppvOut)
{
    ACLMulti *This = impl_from_IObjMgr(iface);
    return ACLMulti_QueryInterface((IEnumString *)This, iid, ppvOut);
}

static ULONG WINAPI ACLMulti_IObjMgr_AddRef(IObjMgr *iface)
{
    ACLMulti *This = impl_from_IObjMgr(iface);
    return ACLMulti_AddRef((IEnumString *)This);
}

static ULONG WINAPI ACLMulti_IObjMgr_Release(IObjMgr *iface)
{
    ACLMulti *This = impl_from_IObjMgr(iface);
    return ACLMulti_Release((IEnumString *)This);
}

static const IObjMgrVtbl ACLMulti_ObjMgrVtbl =
{
    ACLMulti_IObjMgr_QueryInterface,
    ACLMulti_IObjMgr_AddRef,
    ACLMulti_IObjMgr_Release,

    ACLMulti_Append,
    ACLMulti_Remove
};

static HRESULT WINAPI ACLMulti_IACList_QueryInterface(IACList *iface, REFIID iid, LPVOID *ppvOut)
{
    ACLMulti *This = impl_from_IACList(iface);
    return ACLMulti_QueryInterface((IEnumString *)This, iid, ppvOut);
}

static ULONG WINAPI ACLMulti_IACList_AddRef(IACList *iface)
{
    ACLMulti *This = impl_from_IACList(iface);
    return ACLMulti_AddRef((IEnumString *)This);
}

static ULONG WINAPI ACLMulti_IACList_Release(IACList *iface)
{
    ACLMulti *This = impl_from_IACList(iface);
    return ACLMulti_Release((IEnumString *)This);
}

static const IACListVtbl ACLMulti_ACListVtbl =
{
    ACLMulti_IACList_QueryInterface,
    ACLMulti_IACList_AddRef,
    ACLMulti_IACList_Release,

    ACLMulti_Expand
};
Jacek Caban's avatar
Jacek Caban committed
306 307 308 309 310 311 312

HRESULT ACLMulti_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
{
    ACLMulti *This;
    if (pUnkOuter)
        return CLASS_E_NOAGGREGATION;

Jacek Caban's avatar
Jacek Caban committed
313
    This = heap_alloc_zero(sizeof(ACLMulti));
Jacek Caban's avatar
Jacek Caban committed
314 315
    if (This == NULL)
        return E_OUTOFMEMORY;
Jacek Caban's avatar
Jacek Caban committed
316

Jacek Caban's avatar
Jacek Caban committed
317 318 319 320 321 322 323 324 325 326
    This->vtbl = &ACLMultiVtbl;
    This->aclVtbl = &ACLMulti_ACListVtbl;
    This->objmgrVtbl = &ACLMulti_ObjMgrVtbl;
    This->refCount = 1;

    TRACE("returning %p\n", This);
    *ppOut = (IUnknown *)This;
    BROWSEUI_refCount++;
    return S_OK;
}