compositemoniker.c 62.2 KB
Newer Older
1
/*
2
 * CompositeMonikers implementation
Noomen Hamza's avatar
Noomen Hamza committed
3
 *
4
 * Copyright 1999  Noomen Hamza
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
 */

Noomen Hamza's avatar
Noomen Hamza committed
21
#include <assert.h>
22
#include <stdarg.h>
23
#include <string.h>
24

25
#define COBJMACROS
26 27
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
28

29
#include "windef.h"
30
#include "winbase.h"
31
#include "winuser.h"
Noomen Hamza's avatar
Noomen Hamza committed
32
#include "winerror.h"
33
#include "wine/debug.h"
34
#include "wine/unicode.h"
Noomen Hamza's avatar
Noomen Hamza committed
35
#include "ole2.h"
36
#include "moniker.h"
Noomen Hamza's avatar
Noomen Hamza committed
37

38
WINE_DEFAULT_DEBUG_CHANNEL(ole);
39

Noomen Hamza's avatar
Noomen Hamza committed
40 41 42 43
#define  BLOCK_TAB_SIZE 5 /* represent the first size table and it's increment block size */

/* CompositeMoniker data structure */
typedef struct CompositeMonikerImpl{
44 45 46 47
    IMoniker IMoniker_iface;
    IROTData IROTData_iface;
    IMarshal IMarshal_iface;
    LONG ref;
Austin English's avatar
Austin English committed
48
    IMoniker** tabMoniker; /* dynamic table containing all components (monikers) of this composite moniker */
Noomen Hamza's avatar
Noomen Hamza committed
49
    ULONG    tabSize;      /* size of tabMoniker */
50 51
    ULONG    tabLastIndex; /* first free index in tabMoniker */
} CompositeMonikerImpl;
Noomen Hamza's avatar
Noomen Hamza committed
52

53 54 55 56
static inline CompositeMonikerImpl *impl_from_IMoniker(IMoniker *iface)
{
    return CONTAINING_RECORD(iface, CompositeMonikerImpl, IMoniker_iface);
}
Noomen Hamza's avatar
Noomen Hamza committed
57

58 59 60 61
static inline CompositeMonikerImpl *impl_from_IROTData(IROTData *iface)
{
    return CONTAINING_RECORD(iface, CompositeMonikerImpl, IROTData_iface);
}
Noomen Hamza's avatar
Noomen Hamza committed
62

63 64 65 66
static inline CompositeMonikerImpl *impl_from_IMarshal(IMarshal *iface)
{
    return CONTAINING_RECORD(iface, CompositeMonikerImpl, IMarshal_iface);
}
Noomen Hamza's avatar
Noomen Hamza committed
67 68 69

/* EnumMoniker data structure */
typedef struct EnumMonikerImpl{
70 71
    IEnumMoniker IEnumMoniker_iface;
    LONG ref;
Noomen Hamza's avatar
Noomen Hamza committed
72 73 74 75 76
    IMoniker** tabMoniker; /* dynamic table containing the enumerated monikers */
    ULONG      tabSize; /* size of tabMoniker */
    ULONG      currentPos;  /* index pointer on the current moniker */
} EnumMonikerImpl;

77
static inline EnumMonikerImpl *impl_from_IEnumMoniker(IEnumMoniker *iface)
78
{
79
    return CONTAINING_RECORD(iface, EnumMonikerImpl, IEnumMoniker_iface);
80 81
}

82
static HRESULT EnumMonikerImpl_CreateEnumMoniker(IMoniker** tabMoniker,ULONG tabSize,ULONG currentPos,BOOL leftToRigth,IEnumMoniker ** ppmk);
Noomen Hamza's avatar
Noomen Hamza committed
83 84 85 86

/*******************************************************************************
 *        CompositeMoniker_QueryInterface
 *******************************************************************************/
87 88
static HRESULT WINAPI
CompositeMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
Noomen Hamza's avatar
Noomen Hamza committed
89
{
90
    CompositeMonikerImpl *This = impl_from_IMoniker(iface);
91

92
    TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
Noomen Hamza's avatar
Noomen Hamza committed
93 94

    /* Perform a sanity check on the parameters.*/
95
    if ( ppvObject==0 )
Noomen Hamza's avatar
Noomen Hamza committed
96
	return E_INVALIDARG;
97

Noomen Hamza's avatar
Noomen Hamza committed
98 99 100 101 102 103 104 105 106 107 108
    /* Initialize the return parameter */
    *ppvObject = 0;

    /* Compare the riid with the interface IDs implemented by this object.*/
    if (IsEqualIID(&IID_IUnknown, riid) ||
        IsEqualIID(&IID_IPersist, riid) ||
        IsEqualIID(&IID_IPersistStream, riid) ||
        IsEqualIID(&IID_IMoniker, riid)
       )
        *ppvObject = iface;
    else if (IsEqualIID(&IID_IROTData, riid))
109
        *ppvObject = &This->IROTData_iface;
110
    else if (IsEqualIID(&IID_IMarshal, riid))
111
        *ppvObject = &This->IMarshal_iface;
Noomen Hamza's avatar
Noomen Hamza committed
112 113 114 115 116 117

    /* Check that we obtained an interface.*/
    if ((*ppvObject)==0)
        return E_NOINTERFACE;

    /* Query Interface always increases the reference count by one when it is successful */
118
    IMoniker_AddRef(iface);
Noomen Hamza's avatar
Noomen Hamza committed
119 120 121 122 123 124 125

    return S_OK;
}

/******************************************************************************
 *        CompositeMoniker_AddRef
 ******************************************************************************/
126 127
static ULONG WINAPI
CompositeMonikerImpl_AddRef(IMoniker* iface)
Noomen Hamza's avatar
Noomen Hamza committed
128
{
129
    CompositeMonikerImpl *This = impl_from_IMoniker(iface);
Noomen Hamza's avatar
Noomen Hamza committed
130

131
    TRACE("(%p)\n",This);
Noomen Hamza's avatar
Noomen Hamza committed
132

133
    return InterlockedIncrement(&This->ref);
Noomen Hamza's avatar
Noomen Hamza committed
134 135
}

136 137 138 139 140 141 142 143 144 145
static void CompositeMonikerImpl_ReleaseMonikersInTable(CompositeMonikerImpl *This)
{
    ULONG i;

    for (i = 0; i < This->tabLastIndex; i++)
        IMoniker_Release(This->tabMoniker[i]);

    This->tabLastIndex = 0;
}

Noomen Hamza's avatar
Noomen Hamza committed
146 147 148
/******************************************************************************
 *        CompositeMoniker_Release
 ******************************************************************************/
149 150
static ULONG WINAPI
CompositeMonikerImpl_Release(IMoniker* iface)
Noomen Hamza's avatar
Noomen Hamza committed
151
{
152
    CompositeMonikerImpl *This = impl_from_IMoniker(iface);
153
    ULONG ref;
154

155
    TRACE("(%p)\n",This);
Noomen Hamza's avatar
Noomen Hamza committed
156

157
    ref = InterlockedDecrement(&This->ref);
Noomen Hamza's avatar
Noomen Hamza committed
158 159

    /* destroy the object if there's no more reference on it */
160
    if (ref == 0){
Noomen Hamza's avatar
Noomen Hamza committed
161 162

        /* release all the components before destroying this object */
163
        CompositeMonikerImpl_ReleaseMonikersInTable(This);
164

165 166
        HeapFree(GetProcessHeap(),0,This->tabMoniker);
        HeapFree(GetProcessHeap(),0,This);
Noomen Hamza's avatar
Noomen Hamza committed
167
    }
168
    return ref;
Noomen Hamza's avatar
Noomen Hamza committed
169 170 171 172 173
}

/******************************************************************************
 *        CompositeMoniker_GetClassID
 ******************************************************************************/
174 175
static HRESULT WINAPI
CompositeMonikerImpl_GetClassID(IMoniker* iface,CLSID *pClassID)
Noomen Hamza's avatar
Noomen Hamza committed
176
{
177
    TRACE("(%p,%p)\n",iface,pClassID);
Noomen Hamza's avatar
Noomen Hamza committed
178 179 180

    if (pClassID==NULL)
        return E_POINTER;
181

Noomen Hamza's avatar
Noomen Hamza committed
182
    *pClassID = CLSID_CompositeMoniker;
183

Noomen Hamza's avatar
Noomen Hamza committed
184 185 186 187 188 189
    return S_OK;
}

/******************************************************************************
 *        CompositeMoniker_IsDirty
 ******************************************************************************/
190 191
static HRESULT WINAPI
CompositeMonikerImpl_IsDirty(IMoniker* iface)
Noomen Hamza's avatar
Noomen Hamza committed
192 193 194 195 196
{
    /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
       method in the OLE-provided moniker interfaces always return S_FALSE because
       their internal state never changes. */

197
    TRACE("(%p)\n",iface);
Noomen Hamza's avatar
Noomen Hamza committed
198 199 200 201 202 203 204

    return S_FALSE;
}

/******************************************************************************
 *        CompositeMoniker_Load
 ******************************************************************************/
205 206
static HRESULT WINAPI
CompositeMonikerImpl_Load(IMoniker* iface,IStream* pStm)
Noomen Hamza's avatar
Noomen Hamza committed
207
{
208
    CompositeMonikerImpl *This = impl_from_IMoniker(iface);
Noomen Hamza's avatar
Noomen Hamza committed
209
    HRESULT res;
210 211
    DWORD moniker_count;
    DWORD i;
212

213
    TRACE("(%p,%p)\n",iface,pStm);
Noomen Hamza's avatar
Noomen Hamza committed
214 215 216

    /* this function call OleLoadFromStream function for each moniker within this object */

217 218 219 220
    res=IStream_Read(pStm,&moniker_count,sizeof(DWORD),NULL);
    if (res != S_OK)
    {
        ERR("couldn't reading moniker count from stream\n");
Noomen Hamza's avatar
Noomen Hamza committed
221
        return E_FAIL;
222
    }
Noomen Hamza's avatar
Noomen Hamza committed
223

224 225
    CompositeMonikerImpl_ReleaseMonikersInTable(This);

226 227
    for (i = 0; i < moniker_count; i++)
    {
228
        res=OleLoadFromStream(pStm,&IID_IMoniker,(void**)&This->tabMoniker[This->tabLastIndex]);
Noomen Hamza's avatar
Noomen Hamza committed
229
        if (FAILED(res))
230
        {
231
            ERR("couldn't load moniker from stream, res = 0x%08x\n", res);
Noomen Hamza's avatar
Noomen Hamza committed
232 233 234 235 236
            break;
        }

        /* resize the table if needed */
        if (++This->tabLastIndex==This->tabSize){
237

Noomen Hamza's avatar
Noomen Hamza committed
238
            This->tabSize+=BLOCK_TAB_SIZE;
239
            This->tabMoniker=HeapReAlloc(GetProcessHeap(),0,This->tabMoniker,This->tabSize*sizeof(IMoniker));
Noomen Hamza's avatar
Noomen Hamza committed
240 241 242 243 244 245 246 247 248 249 250 251

            if (This->tabMoniker==NULL)
            return E_OUTOFMEMORY;
        }
    }

    return res;
}

/******************************************************************************
 *        CompositeMoniker_Save
 ******************************************************************************/
252 253
static HRESULT WINAPI
CompositeMonikerImpl_Save(IMoniker* iface,IStream* pStm,BOOL fClearDirty)
Noomen Hamza's avatar
Noomen Hamza committed
254
{
255
    CompositeMonikerImpl *This = impl_from_IMoniker(iface);
Noomen Hamza's avatar
Noomen Hamza committed
256 257 258
    HRESULT res;
    IEnumMoniker *enumMk;
    IMoniker *pmk;
259
    DWORD moniker_count = This->tabLastIndex;
260

261
    TRACE("(%p,%p,%d)\n",iface,pStm,fClearDirty);
Noomen Hamza's avatar
Noomen Hamza committed
262

263
    /* This function calls OleSaveToStream function for each moniker within
264
     * this object.
265 266
     * When I tested this function in windows, I usually found this constant
     * at the beginning of the stream. I don't known why (there's no
267 268
     * indication in the specification) !
     */
269 270
    res=IStream_Write(pStm,&moniker_count,sizeof(moniker_count),NULL);
    if (FAILED(res)) return res;
Noomen Hamza's avatar
Noomen Hamza committed
271 272 273 274 275 276 277 278 279 280 281

    IMoniker_Enum(iface,TRUE,&enumMk);

    while(IEnumMoniker_Next(enumMk,1,&pmk,NULL)==S_OK){

        res=OleSaveToStream((IPersistStream*)pmk,pStm);

        IMoniker_Release(pmk);

        if (FAILED(res)){

282
            IEnumMoniker_Release(enumMk);
Noomen Hamza's avatar
Noomen Hamza committed
283 284 285 286 287 288 289 290 291 292 293 294
            return res;
        }
    }

    IEnumMoniker_Release(enumMk);

    return S_OK;
}

/******************************************************************************
 *        CompositeMoniker_GetSizeMax
 ******************************************************************************/
295 296
static HRESULT WINAPI
CompositeMonikerImpl_GetSizeMax(IMoniker* iface,ULARGE_INTEGER* pcbSize)
Noomen Hamza's avatar
Noomen Hamza committed
297 298 299 300 301
{
    IEnumMoniker *enumMk;
    IMoniker *pmk;
    ULARGE_INTEGER ptmpSize;

302
    /* The sizeMax of this object is calculated by calling  GetSizeMax on
303 304
     * each moniker within this object then summing all returned values
     */
Noomen Hamza's avatar
Noomen Hamza committed
305

306
    TRACE("(%p,%p)\n",iface,pcbSize);
Noomen Hamza's avatar
Noomen Hamza committed
307

308
    if (!pcbSize)
Noomen Hamza's avatar
Noomen Hamza committed
309 310
        return E_POINTER;

311
    pcbSize->QuadPart = sizeof(DWORD);
Noomen Hamza's avatar
Noomen Hamza committed
312 313 314

    IMoniker_Enum(iface,TRUE,&enumMk);

315
    while(IEnumMoniker_Next(enumMk,1,&pmk,NULL)==S_OK){
Noomen Hamza's avatar
Noomen Hamza committed
316 317 318 319 320

        IMoniker_GetSizeMax(pmk,&ptmpSize);

        IMoniker_Release(pmk);

321
        pcbSize->QuadPart = ptmpSize.QuadPart + sizeof(CLSID);
Noomen Hamza's avatar
Noomen Hamza committed
322 323 324 325 326 327 328 329 330 331
    }

    IEnumMoniker_Release(enumMk);

    return S_OK;
}

/******************************************************************************
 *                  CompositeMoniker_BindToObject
 ******************************************************************************/
332 333 334
static HRESULT WINAPI
CompositeMonikerImpl_BindToObject(IMoniker* iface, IBindCtx* pbc,
               IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult)
Noomen Hamza's avatar
Noomen Hamza committed
335 336 337 338 339
{
    HRESULT   res;
    IRunningObjectTable *prot;
    IMoniker *tempMk,*antiMk,*mostRigthMk;
    IEnumMoniker *enumMoniker;
340

341
    TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvResult);
Noomen Hamza's avatar
Noomen Hamza committed
342 343 344

    if (ppvResult==NULL)
        return E_POINTER;
345

Noomen Hamza's avatar
Noomen Hamza committed
346 347 348 349 350 351 352 353 354
    *ppvResult=0;
    /* If pmkToLeft is NULL, this method looks for the moniker in the ROT, and if found, queries the retrieved */
    /* object for the requested interface pointer. */
    if(pmkToLeft==NULL){

        res=IBindCtx_GetRunningObjectTable(pbc,&prot);

        if (SUCCEEDED(res)){

355
            /* if the requested class was loaded before ! we don't need to reload it */
Noomen Hamza's avatar
Noomen Hamza committed
356 357 358 359 360 361 362 363 364 365 366 367 368
            res = IRunningObjectTable_GetObject(prot,iface,(IUnknown**)ppvResult);

            if (res==S_OK)
                return res;
        }
    }
    else{
        /* If pmkToLeft is not NULL, the method recursively calls IMoniker::BindToObject on the rightmost */
        /* component of the composite, passing the rest of the composite as the pmkToLeft parameter for that call */

        IMoniker_Enum(iface,FALSE,&enumMoniker);
        IEnumMoniker_Next(enumMoniker,1,&mostRigthMk,NULL);
        IEnumMoniker_Release(enumMoniker);
369

Noomen Hamza's avatar
Noomen Hamza committed
370 371 372
        res=CreateAntiMoniker(&antiMk);
        res=IMoniker_ComposeWith(iface,antiMk,0,&tempMk);
        IMoniker_Release(antiMk);
373

374
        res=IMoniker_BindToObject(mostRigthMk,pbc,tempMk,riid,ppvResult);
Noomen Hamza's avatar
Noomen Hamza committed
375 376 377 378 379 380 381 382 383 384 385

        IMoniker_Release(tempMk);
        IMoniker_Release(mostRigthMk);
    }

    return res;
}

/******************************************************************************
 *        CompositeMoniker_BindToStorage
 ******************************************************************************/
386 387 388
static HRESULT WINAPI
CompositeMonikerImpl_BindToStorage(IMoniker* iface, IBindCtx* pbc,
               IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult)
Noomen Hamza's avatar
Noomen Hamza committed
389 390
{
    HRESULT   res;
391
    IMoniker *tempMk,*antiMk,*mostRigthMk,*leftMk;
Noomen Hamza's avatar
Noomen Hamza committed
392 393
    IEnumMoniker *enumMoniker;

394
    TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvResult);
Noomen Hamza's avatar
Noomen Hamza committed
395 396 397 398 399 400

    *ppvResult=0;

    /* This method recursively calls BindToStorage on the rightmost component of the composite, */
    /* passing the rest of the composite as the pmkToLeft parameter for that call. */

401 402 403 404 405 406 407
    if (pmkToLeft)
    {
        res = IMoniker_ComposeWith(pmkToLeft, iface, FALSE, &leftMk);
        if (FAILED(res)) return res;
    }
    else
        leftMk = iface;
408

409 410 411
    IMoniker_Enum(iface, FALSE, &enumMoniker);
    IEnumMoniker_Next(enumMoniker, 1, &mostRigthMk, NULL);
    IEnumMoniker_Release(enumMoniker);
412

413 414 415 416 417
    res = CreateAntiMoniker(&antiMk);
    if (FAILED(res)) return res;
    res = IMoniker_ComposeWith(leftMk, antiMk, 0, &tempMk);
    if (FAILED(res)) return res;
    IMoniker_Release(antiMk);
418

419
    res = IMoniker_BindToStorage(mostRigthMk, pbc, tempMk, riid, ppvResult);
Noomen Hamza's avatar
Noomen Hamza committed
420

421
    IMoniker_Release(tempMk);
Noomen Hamza's avatar
Noomen Hamza committed
422

423
    IMoniker_Release(mostRigthMk);
Noomen Hamza's avatar
Noomen Hamza committed
424

425 426 427 428
    if (pmkToLeft)
        IMoniker_Release(leftMk);

    return res;
Noomen Hamza's avatar
Noomen Hamza committed
429 430 431 432 433
}

/******************************************************************************
 *        CompositeMoniker_Reduce
 ******************************************************************************/
434 435 436
static HRESULT WINAPI
CompositeMonikerImpl_Reduce(IMoniker* iface, IBindCtx* pbc, DWORD dwReduceHowFar,
               IMoniker** ppmkToLeft, IMoniker** ppmkReduced)
Noomen Hamza's avatar
Noomen Hamza committed
437 438 439 440
{
    IMoniker *tempMk,*antiMk,*mostRigthMk,*leftReducedComposedMk,*mostRigthReducedMk;
    IEnumMoniker *enumMoniker;

441
    TRACE("(%p,%p,%d,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
Noomen Hamza's avatar
Noomen Hamza committed
442 443 444 445 446 447 448 449 450 451 452

    if (ppmkReduced==NULL)
        return E_POINTER;

    /* This method recursively calls Reduce for each of its component monikers. */

    if (ppmkToLeft==NULL){

        IMoniker_Enum(iface,FALSE,&enumMoniker);
        IEnumMoniker_Next(enumMoniker,1,&mostRigthMk,NULL);
        IEnumMoniker_Release(enumMoniker);
453

454 455
        CreateAntiMoniker(&antiMk);
        IMoniker_ComposeWith(iface,antiMk,0,&tempMk);
Noomen Hamza's avatar
Noomen Hamza committed
456 457
        IMoniker_Release(antiMk);

458
        return IMoniker_Reduce(mostRigthMk,pbc,dwReduceHowFar,&tempMk, ppmkReduced);
Noomen Hamza's avatar
Noomen Hamza committed
459 460 461 462 463 464 465
    }
    else if (*ppmkToLeft==NULL)

        return IMoniker_Reduce(iface,pbc,dwReduceHowFar,NULL,ppmkReduced);

    else{

Andreas Mohr's avatar
Andreas Mohr committed
466
        /* separate the composite moniker in to left and right moniker */
Noomen Hamza's avatar
Noomen Hamza committed
467 468 469
        IMoniker_Enum(iface,FALSE,&enumMoniker);
        IEnumMoniker_Next(enumMoniker,1,&mostRigthMk,NULL);
        IEnumMoniker_Release(enumMoniker);
470

471 472
        CreateAntiMoniker(&antiMk);
        IMoniker_ComposeWith(iface,antiMk,0,&tempMk);
Noomen Hamza's avatar
Noomen Hamza committed
473 474 475 476 477
        IMoniker_Release(antiMk);

        /* If any of the components  reduces itself, the method returns S_OK and passes back a composite */
        /* of the reduced components */
        if (IMoniker_Reduce(mostRigthMk,pbc,dwReduceHowFar,NULL,&mostRigthReducedMk) &&
478
            IMoniker_Reduce(mostRigthMk,pbc,dwReduceHowFar,&tempMk,&leftReducedComposedMk)
Noomen Hamza's avatar
Noomen Hamza committed
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
           )

            return CreateGenericComposite(leftReducedComposedMk,mostRigthReducedMk,ppmkReduced);

        else{
            /* If no reduction occurred, the method passes back the same moniker and returns MK_S_REDUCED_TO_SELF.*/

            IMoniker_AddRef(iface);

            *ppmkReduced=iface;

            return MK_S_REDUCED_TO_SELF;
        }
    }
}

/******************************************************************************
 *        CompositeMoniker_ComposeWith
 ******************************************************************************/
498 499 500
static HRESULT WINAPI
CompositeMonikerImpl_ComposeWith(IMoniker* iface, IMoniker* pmkRight,
               BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite)
Noomen Hamza's avatar
Noomen Hamza committed
501
{
502
    TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
Noomen Hamza's avatar
Noomen Hamza committed
503 504 505 506 507 508 509 510 511

    if ((ppmkComposite==NULL)||(pmkRight==NULL))
	return E_POINTER;

    *ppmkComposite=0;

    /* If fOnlyIfNotGeneric is TRUE, this method sets *pmkComposite to NULL and returns MK_E_NEEDGENERIC; */
    /* otherwise, the method returns the result of combining the two monikers by calling the */
    /* CreateGenericComposite function */
512

Noomen Hamza's avatar
Noomen Hamza committed
513 514
    if (fOnlyIfNotGeneric)
        return MK_E_NEEDGENERIC;
515

Noomen Hamza's avatar
Noomen Hamza committed
516 517 518 519 520 521
    return CreateGenericComposite(iface,pmkRight,ppmkComposite);
}

/******************************************************************************
 *        CompositeMoniker_Enum
 ******************************************************************************/
522 523
static HRESULT WINAPI
CompositeMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
Noomen Hamza's avatar
Noomen Hamza committed
524
{
525
    CompositeMonikerImpl *This = impl_from_IMoniker(iface);
Noomen Hamza's avatar
Noomen Hamza committed
526

527
    TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
Noomen Hamza's avatar
Noomen Hamza committed
528 529 530

    if (ppenumMoniker == NULL)
        return E_POINTER;
531

Noomen Hamza's avatar
Noomen Hamza committed
532 533 534 535 536 537
    return EnumMonikerImpl_CreateEnumMoniker(This->tabMoniker,This->tabLastIndex,0,fForward,ppenumMoniker);
}

/******************************************************************************
 *        CompositeMoniker_IsEqual
 ******************************************************************************/
538 539
static HRESULT WINAPI
CompositeMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
Noomen Hamza's avatar
Noomen Hamza committed
540 541 542 543
{
    IEnumMoniker *enumMoniker1,*enumMoniker2;
    IMoniker *tempMk1,*tempMk2;
    HRESULT res1,res2,res;
544
    BOOL done;
545

546
    TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
Noomen Hamza's avatar
Noomen Hamza committed
547 548 549 550 551 552 553 554 555 556

    if (pmkOtherMoniker==NULL)
        return S_FALSE;

    /* This method returns S_OK if the components of both monikers are equal when compared in the */
    /* left-to-right order.*/
    IMoniker_Enum(pmkOtherMoniker,TRUE,&enumMoniker1);

    if (enumMoniker1==NULL)
        return S_FALSE;
557

Noomen Hamza's avatar
Noomen Hamza committed
558 559
    IMoniker_Enum(iface,TRUE,&enumMoniker2);

560
    do {
Noomen Hamza's avatar
Noomen Hamza committed
561 562 563

        res1=IEnumMoniker_Next(enumMoniker1,1,&tempMk1,NULL);
        res2=IEnumMoniker_Next(enumMoniker2,1,&tempMk2,NULL);
564

Noomen Hamza's avatar
Noomen Hamza committed
565
        if((res1==S_OK)&&(res2==S_OK)){
566
            done = (res = IMoniker_IsEqual(tempMk1,tempMk2)) == S_FALSE;
Noomen Hamza's avatar
Noomen Hamza committed
567
        }
568 569 570 571
        else
        {
            res = (res1==S_FALSE) && (res2==S_FALSE);
            done = TRUE;
Noomen Hamza's avatar
Noomen Hamza committed
572 573 574 575 576 577 578
        }

        if (res1==S_OK)
            IMoniker_Release(tempMk1);

        if (res2==S_OK)
            IMoniker_Release(tempMk2);
579
    } while (!done);
Noomen Hamza's avatar
Noomen Hamza committed
580 581 582 583 584 585 586 587 588

    IEnumMoniker_Release(enumMoniker1);
    IEnumMoniker_Release(enumMoniker2);

    return res;
}
/******************************************************************************
 *        CompositeMoniker_Hash
 ******************************************************************************/
589 590
static HRESULT WINAPI
CompositeMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
Noomen Hamza's avatar
Noomen Hamza committed
591
{
592 593 594 595 596 597 598 599 600 601 602 603 604
    IEnumMoniker *enumMoniker;
    IMoniker *tempMk;
    HRESULT res;
    DWORD tempHash;

    TRACE("(%p,%p)\n",iface,pdwHash);

    if (pdwHash==NULL)
        return E_POINTER;

    res = IMoniker_Enum(iface,TRUE,&enumMoniker);
    if(FAILED(res))
        return res;
Noomen Hamza's avatar
Noomen Hamza committed
605

606 607
    *pdwHash = 0;

608
    while(IEnumMoniker_Next(enumMoniker,1,&tempMk,NULL)==S_OK){
609 610 611
        res = IMoniker_Hash(tempMk, &tempHash);
        if(FAILED(res))
            break;
612
        *pdwHash = *pdwHash ^ tempHash;
613 614 615 616 617 618 619
        
        IMoniker_Release(tempMk);
    }

    IEnumMoniker_Release(enumMoniker);

    return res;
Noomen Hamza's avatar
Noomen Hamza committed
620 621 622 623 624
}

/******************************************************************************
 *        CompositeMoniker_IsRunning
 ******************************************************************************/
625 626 627
static HRESULT WINAPI
CompositeMonikerImpl_IsRunning(IMoniker* iface, IBindCtx* pbc,
               IMoniker* pmkToLeft, IMoniker* pmkNewlyRunning)
Noomen Hamza's avatar
Noomen Hamza committed
628 629 630 631 632 633
{
    IRunningObjectTable* rot;
    HRESULT res;
    IMoniker *tempMk,*antiMk,*mostRigthMk;
    IEnumMoniker *enumMoniker;

634
    TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
Noomen Hamza's avatar
Noomen Hamza committed
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649

    /* If pmkToLeft is non-NULL, this method composes pmkToLeft with this moniker and calls IsRunning on the result.*/
    if (pmkToLeft!=NULL){

        CreateGenericComposite(pmkToLeft,iface,&tempMk);

        res = IMoniker_IsRunning(tempMk,pbc,NULL,pmkNewlyRunning);

        IMoniker_Release(tempMk);

        return res;
    }
    else
        /* If pmkToLeft is NULL, this method returns S_OK if pmkNewlyRunning is non-NULL and is equal */
        /* to this moniker */
650

Noomen Hamza's avatar
Noomen Hamza committed
651 652 653 654 655 656 657 658 659 660 661
        if (pmkNewlyRunning!=NULL)

            if (IMoniker_IsEqual(iface,pmkNewlyRunning)==S_OK)
                return S_OK;

            else
                return S_FALSE;

        else{

            if (pbc==NULL)
662
                return E_INVALIDARG;
Noomen Hamza's avatar
Noomen Hamza committed
663 664 665 666 667

            /* If pmkToLeft and pmkNewlyRunning are both NULL, this method checks the ROT to see whether */
            /* the moniker is running. If so, the method returns S_OK; otherwise, it recursively calls   */
            /* IMoniker::IsRunning on the rightmost component of the composite, passing the remainder of */
            /* the composite as the pmkToLeft parameter for that call.                                   */
668

Noomen Hamza's avatar
Noomen Hamza committed
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
             res=IBindCtx_GetRunningObjectTable(pbc,&rot);

            if (FAILED(res))
                return res;

            res = IRunningObjectTable_IsRunning(rot,iface);
            IRunningObjectTable_Release(rot);

            if(res==S_OK)
                return S_OK;

            else{

                IMoniker_Enum(iface,FALSE,&enumMoniker);
                IEnumMoniker_Next(enumMoniker,1,&mostRigthMk,NULL);
                IEnumMoniker_Release(enumMoniker);

                res=CreateAntiMoniker(&antiMk);
                res=IMoniker_ComposeWith(iface,antiMk,0,&tempMk);
                IMoniker_Release(antiMk);

                res=IMoniker_IsRunning(mostRigthMk,pbc,tempMk,pmkNewlyRunning);

                IMoniker_Release(tempMk);
                IMoniker_Release(mostRigthMk);

                return res;
            }
        }
}

/******************************************************************************
 *        CompositeMoniker_GetTimeOfLastChange
 ******************************************************************************/
703 704 705
static HRESULT WINAPI
CompositeMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc,
               IMoniker* pmkToLeft, FILETIME* pCompositeTime)
Noomen Hamza's avatar
Noomen Hamza committed
706 707
{
    HRESULT res;
708
    IMoniker *tempMk,*antiMk,*mostRigthMk,*leftMk;
Noomen Hamza's avatar
Noomen Hamza committed
709
    IEnumMoniker *enumMoniker;
710

711
    TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pCompositeTime);
Noomen Hamza's avatar
Noomen Hamza committed
712 713 714 715 716 717 718 719

    if (pCompositeTime==NULL)
        return E_INVALIDARG;

    /* This method creates a composite of pmkToLeft (if non-NULL) and this moniker and uses the ROT to  */
    /* retrieve the time of last change. If the object is not in the ROT, the method recursively calls  */
    /* IMoniker::GetTimeOfLastChange on the rightmost component of the composite, passing the remainder */
    /* of the composite as the pmkToLeft parameter for that call.                                       */
720 721 722
    if (pmkToLeft)
    {
        IRunningObjectTable* rot;
Noomen Hamza's avatar
Noomen Hamza committed
723

724
        res = IMoniker_ComposeWith(pmkToLeft, iface, FALSE, &leftMk);
Noomen Hamza's avatar
Noomen Hamza committed
725

726
        res = IBindCtx_GetRunningObjectTable(pbc,&rot);
Noomen Hamza's avatar
Noomen Hamza committed
727
        if (FAILED(res))
728 729
        {
            IMoniker_Release(leftMk);
Noomen Hamza's avatar
Noomen Hamza committed
730
            return res;
731
        }
Noomen Hamza's avatar
Noomen Hamza committed
732

733 734 735
        if (IRunningObjectTable_GetTimeOfLastChange(rot,leftMk,pCompositeTime)==S_OK)
        {
            IMoniker_Release(leftMk);
Noomen Hamza's avatar
Noomen Hamza committed
736
            return res;
737 738 739 740
        }
    }
    else
        leftMk = iface;
Noomen Hamza's avatar
Noomen Hamza committed
741

742 743 744
    IMoniker_Enum(iface, FALSE, &enumMoniker);
    IEnumMoniker_Next(enumMoniker, 1, &mostRigthMk, NULL);
    IEnumMoniker_Release(enumMoniker);
Noomen Hamza's avatar
Noomen Hamza committed
745

746 747 748
    res = CreateAntiMoniker(&antiMk);
    res = IMoniker_ComposeWith(leftMk, antiMk, 0, &tempMk);
    IMoniker_Release(antiMk);
Noomen Hamza's avatar
Noomen Hamza committed
749

750
    res = IMoniker_GetTimeOfLastChange(mostRigthMk, pbc, tempMk, pCompositeTime);
Noomen Hamza's avatar
Noomen Hamza committed
751

752 753
    IMoniker_Release(tempMk);
    IMoniker_Release(mostRigthMk);
Noomen Hamza's avatar
Noomen Hamza committed
754

755 756 757 758
    if (pmkToLeft)
        IMoniker_Release(leftMk);

    return res;
Noomen Hamza's avatar
Noomen Hamza committed
759 760 761 762 763
}

/******************************************************************************
 *        CompositeMoniker_Inverse
 ******************************************************************************/
764 765
static HRESULT WINAPI
CompositeMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
Noomen Hamza's avatar
Noomen Hamza committed
766 767 768 769 770
{
    HRESULT res;
    IMoniker *tempMk,*antiMk,*mostRigthMk,*tempInvMk,*mostRigthInvMk;
    IEnumMoniker *enumMoniker;

771
    TRACE("(%p,%p)\n",iface,ppmk);
Noomen Hamza's avatar
Noomen Hamza committed
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809

    if (ppmk==NULL)
        return E_POINTER;

    /* This method returns a composite moniker that consists of the inverses of each of the components */
    /* of the original composite, stored in reverse order */

    res=CreateAntiMoniker(&antiMk);
    res=IMoniker_ComposeWith(iface,antiMk,0,&tempMk);
    IMoniker_Release(antiMk);

    if (tempMk==NULL)

        return IMoniker_Inverse(iface,ppmk);

    else{

        IMoniker_Enum(iface,FALSE,&enumMoniker);
        IEnumMoniker_Next(enumMoniker,1,&mostRigthMk,NULL);
        IEnumMoniker_Release(enumMoniker);

        IMoniker_Inverse(mostRigthMk,&mostRigthInvMk);
        CompositeMonikerImpl_Inverse(tempMk,&tempInvMk);

        res=CreateGenericComposite(mostRigthInvMk,tempInvMk,ppmk);

        IMoniker_Release(tempMk);
        IMoniker_Release(mostRigthMk);
        IMoniker_Release(tempInvMk);
        IMoniker_Release(mostRigthInvMk);

        return res;
    }
}

/******************************************************************************
 *        CompositeMoniker_CommonPrefixWith
 ******************************************************************************/
810 811 812
static HRESULT WINAPI
CompositeMonikerImpl_CommonPrefixWith(IMoniker* iface, IMoniker* pmkOther,
               IMoniker** ppmkPrefix)
Noomen Hamza's avatar
Noomen Hamza committed
813 814 815 816 817 818
{
    DWORD mkSys;
    HRESULT res1,res2;
    IMoniker *tempMk1,*tempMk2,*mostLeftMk1,*mostLeftMk2;
    IEnumMoniker *enumMoniker1,*enumMoniker2;
    ULONG i,nbCommonMk=0;
819

Noomen Hamza's avatar
Noomen Hamza committed
820 821 822 823 824 825
    /* If the other moniker is a composite, this method compares the components of each composite from left  */
    /* to right. The returned common prefix moniker might also be a composite moniker, depending on how many */
    /* of the leftmost components were common to both monikers.                                              */

    if (ppmkPrefix==NULL)
        return E_POINTER;
826

Noomen Hamza's avatar
Noomen Hamza committed
827 828 829 830
    *ppmkPrefix=0;

    if (pmkOther==NULL)
        return MK_E_NOPREFIX;
831

Noomen Hamza's avatar
Noomen Hamza committed
832 833
    IMoniker_IsSystemMoniker(pmkOther,&mkSys);

834
    if(mkSys==MKSYS_GENERICCOMPOSITE){
Noomen Hamza's avatar
Noomen Hamza committed
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886

        IMoniker_Enum(iface,TRUE,&enumMoniker1);
        IMoniker_Enum(pmkOther,TRUE,&enumMoniker2);

        while(1){

            res1=IEnumMoniker_Next(enumMoniker1,1,&mostLeftMk1,NULL);
            res2=IEnumMoniker_Next(enumMoniker2,1,&mostLeftMk2,NULL);

            if ((res1==S_FALSE) && (res2==S_FALSE)){

                /* If the monikers are equal, the method returns MK_S_US and sets ppmkPrefix to this moniker.*/
                *ppmkPrefix=iface;
                IMoniker_AddRef(iface);
                return  MK_S_US;
            }
            else if ((res1==S_OK) && (res2==S_OK)){

                if (IMoniker_IsEqual(mostLeftMk1,mostLeftMk2)==S_OK)

                    nbCommonMk++;

                else
                    break;

            }
            else if (res1==S_OK){

                /* If the other moniker is a prefix of this moniker, the method returns MK_S_HIM and sets */
                /* ppmkPrefix to the other moniker.                                                       */
                *ppmkPrefix=pmkOther;
                return MK_S_HIM;
            }
            else{
                /* If this moniker is a prefix of the other, this method returns MK_S_ME and sets ppmkPrefix */
                /* to this moniker.                                                                          */
                *ppmkPrefix=iface;
                return MK_S_ME;
            }
        }

        IEnumMoniker_Release(enumMoniker1);
        IEnumMoniker_Release(enumMoniker2);

        /* If there is no common prefix, this method returns MK_E_NOPREFIX and sets ppmkPrefix to NULL. */
        if (nbCommonMk==0)
            return MK_E_NOPREFIX;

        IEnumMoniker_Reset(enumMoniker1);

        IEnumMoniker_Next(enumMoniker1,1,&tempMk1,NULL);

Austin English's avatar
Austin English committed
887
        /* if we have more than one common moniker the result will be a composite moniker */
Noomen Hamza's avatar
Noomen Hamza committed
888 889 890 891 892 893 894
        if (nbCommonMk>1){

            /* initialize the common prefix moniker with the composite of two first moniker (from the left)*/
            IEnumMoniker_Next(enumMoniker1,1,&tempMk2,NULL);
            CreateGenericComposite(tempMk1,tempMk2,ppmkPrefix);
            IMoniker_Release(tempMk1);
            IMoniker_Release(tempMk2);
895

Noomen Hamza's avatar
Noomen Hamza committed
896 897 898 899 900 901 902 903 904 905
            /* compose all common monikers in a composite moniker */
            for(i=0;i<nbCommonMk;i++){

                IEnumMoniker_Next(enumMoniker1,1,&tempMk1,NULL);

                CreateGenericComposite(*ppmkPrefix,tempMk1,&tempMk2);

                IMoniker_Release(*ppmkPrefix);

                IMoniker_Release(tempMk1);
906

Noomen Hamza's avatar
Noomen Hamza committed
907 908 909 910 911
                *ppmkPrefix=tempMk2;
            }
            return S_OK;
        }
        else{
Austin English's avatar
Austin English committed
912
            /* if we have only one common moniker the result will be a simple moniker which is the most-left one*/
Noomen Hamza's avatar
Noomen Hamza committed
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
            *ppmkPrefix=tempMk1;

            return S_OK;
        }
    }
    else{
        /* If the other moniker is not a composite, the method simply compares it to the leftmost component
         of this moniker.*/

        IMoniker_Enum(iface,TRUE,&enumMoniker1);

        IEnumMoniker_Next(enumMoniker1,1,&mostLeftMk1,NULL);

        if (IMoniker_IsEqual(pmkOther,mostLeftMk1)==S_OK){

            *ppmkPrefix=pmkOther;

            return MK_S_HIM;
        }
        else
            return MK_E_NOPREFIX;
    }
}
936

Noomen Hamza's avatar
Noomen Hamza committed
937 938 939 940
/***************************************************************************************************
 *        GetAfterCommonPrefix (local function)
 *  This function returns a moniker that consist of the remainder when the common prefix is removed
 ***************************************************************************************************/
941
static VOID GetAfterCommonPrefix(IMoniker* pGenMk,IMoniker* commonMk,IMoniker** restMk)
Noomen Hamza's avatar
Noomen Hamza committed
942 943 944 945 946 947
{
    IMoniker *tempMk,*tempMk1,*tempMk2;
    IEnumMoniker *enumMoniker1,*enumMoniker2,*enumMoniker3;
    ULONG nbRestMk=0;
    DWORD mkSys;
    HRESULT res1,res2;
948

Noomen Hamza's avatar
Noomen Hamza committed
949 950 951
    *restMk=0;

    /* to create an enumerator for pGenMk with current position pointed on the first element after common  */
Andreas Mohr's avatar
Andreas Mohr committed
952
    /* prefix: enum the two monikers (left-right) then compare these enumerations (left-right) and stop  */
Noomen Hamza's avatar
Noomen Hamza committed
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
    /* on the first difference. */
    IMoniker_Enum(pGenMk,TRUE,&enumMoniker1);

    IMoniker_IsSystemMoniker(commonMk,&mkSys);

    if (mkSys==MKSYS_GENERICCOMPOSITE){

        IMoniker_Enum(commonMk,TRUE,&enumMoniker2);
        while(1){

            res1=IEnumMoniker_Next(enumMoniker1,1,&tempMk1,NULL);
            res2=IEnumMoniker_Next(enumMoniker2,1,&tempMk2,NULL);

            if ((res1==S_FALSE)||(res2==S_FALSE)){

                if (res1==S_OK)

                    nbRestMk++;

                IMoniker_Release(tempMk1);
973
                IMoniker_Release(tempMk2);
Noomen Hamza's avatar
Noomen Hamza committed
974 975 976 977

                break;
            }
            IMoniker_Release(tempMk1);
978
            IMoniker_Release(tempMk2);
Noomen Hamza's avatar
Noomen Hamza committed
979 980 981 982 983 984 985 986 987 988 989 990
        }
    }
    else{
        IEnumMoniker_Next(enumMoniker1,1,&tempMk1,NULL);
        IMoniker_Release(tempMk1);
    }

    /* count the number of elements in the enumerator after the common prefix */
    IEnumMoniker_Clone(enumMoniker1,&enumMoniker3);

    for(;IEnumMoniker_Next(enumMoniker3,1,&tempMk,NULL)==S_OK;nbRestMk++)

991
        IMoniker_Release(tempMk);
Noomen Hamza's avatar
Noomen Hamza committed
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025

    if (nbRestMk==0)
        return;

    /* create a generic composite moniker with monikers located after the common prefix */
    IEnumMoniker_Next(enumMoniker1,1,&tempMk1,NULL);

    if (nbRestMk==1){

        *restMk= tempMk1;
        return;
    }
    else {

        IEnumMoniker_Next(enumMoniker1,1,&tempMk2,NULL);

        CreateGenericComposite(tempMk1,tempMk2,restMk);

        IMoniker_Release(tempMk1);

        IMoniker_Release(tempMk2);

        while(IEnumMoniker_Next(enumMoniker1,1,&tempMk1,NULL)==S_OK){

            CreateGenericComposite(*restMk,tempMk1,&tempMk2);

            IMoniker_Release(tempMk1);

            IMoniker_Release(*restMk);

            *restMk=tempMk2;
        }
    }
}
1026

Noomen Hamza's avatar
Noomen Hamza committed
1027 1028 1029
/******************************************************************************
 *        CompositeMoniker_RelativePathTo
 ******************************************************************************/
1030 1031 1032
static HRESULT WINAPI
CompositeMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmkOther,
               IMoniker** ppmkRelPath)
Noomen Hamza's avatar
Noomen Hamza committed
1033 1034 1035 1036
{
    HRESULT res;
    IMoniker *restOtherMk=0,*restThisMk=0,*invRestThisMk=0,*commonMk=0;

1037
    TRACE("(%p,%p,%p)\n",iface,pmkOther,ppmkRelPath);
Noomen Hamza's avatar
Noomen Hamza committed
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048

    if (ppmkRelPath==NULL)
        return E_POINTER;

    *ppmkRelPath=0;

    /* This method finds the common prefix of the two monikers and creates two monikers that consist     */
    /* of the remainder when the common prefix is removed. Then it creates the inverse for the remainder */
    /* of this moniker and composes the remainder of the other moniker on the right of it.               */

    /* finds the common prefix of the two monikers */
1049
    res=IMoniker_CommonPrefixWith(iface,pmkOther,&commonMk);
Noomen Hamza's avatar
Noomen Hamza committed
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 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091

    /* if there's no common prefix or the two moniker are equal the relative is the other moniker */
    if ((res== MK_E_NOPREFIX)||(res==MK_S_US)){

        *ppmkRelPath=pmkOther;
        IMoniker_AddRef(pmkOther);
        return MK_S_HIM;
    }

    GetAfterCommonPrefix(iface,commonMk,&restThisMk);
    GetAfterCommonPrefix(pmkOther,commonMk,&restOtherMk);

    /* if other is a prefix of this moniker the relative path is the inverse of the remainder path of this */
    /* moniker when the common prefix is removed                                                           */
    if (res==MK_S_HIM){

        IMoniker_Inverse(restThisMk,ppmkRelPath);
        IMoniker_Release(restThisMk);
    }
    /* if this moniker is a prefix of other moniker the relative path is the remainder path of other moniker */
    /* when the common prefix is removed                                                                     */
    else if (res==MK_S_ME){

        *ppmkRelPath=restOtherMk;
        IMoniker_AddRef(restOtherMk);
    }
    /* the relative path is the inverse for the remainder of this moniker and the remainder of the other  */
    /* moniker on the right of it.                                                                        */
    else if (res==S_OK){

        IMoniker_Inverse(restThisMk,&invRestThisMk);
        IMoniker_Release(restThisMk);
        CreateGenericComposite(invRestThisMk,restOtherMk,ppmkRelPath);
        IMoniker_Release(invRestThisMk);
        IMoniker_Release(restOtherMk);
    }
    return S_OK;
}

/******************************************************************************
 *        CompositeMoniker_GetDisplayName
 ******************************************************************************/
1092 1093 1094
static HRESULT WINAPI
CompositeMonikerImpl_GetDisplayName(IMoniker* iface, IBindCtx* pbc,
               IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName)
Noomen Hamza's avatar
Noomen Hamza committed
1095 1096 1097 1098 1099 1100
{
    ULONG lengthStr=1;
    IEnumMoniker *enumMoniker;
    IMoniker* tempMk;
    LPOLESTR tempStr;

1101
    TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
Noomen Hamza's avatar
Noomen Hamza committed
1102 1103 1104

    if (ppszDisplayName==NULL)
        return E_POINTER;
1105

Noomen Hamza's avatar
Noomen Hamza committed
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
    *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR));

    if (*ppszDisplayName==NULL)
        return E_OUTOFMEMORY;

    /* This method returns the concatenation of the display names returned by each component moniker of */
    /* the composite */

    **ppszDisplayName=0;

    IMoniker_Enum(iface,TRUE,&enumMoniker);
1117

Noomen Hamza's avatar
Noomen Hamza committed
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
    while(IEnumMoniker_Next(enumMoniker,1,&tempMk,NULL)==S_OK){

        IMoniker_GetDisplayName(tempMk,pbc,NULL,&tempStr);

        lengthStr+=lstrlenW(tempStr);

        *ppszDisplayName=CoTaskMemRealloc(*ppszDisplayName,lengthStr * sizeof(WCHAR));

        if (*ppszDisplayName==NULL)
            return E_OUTOFMEMORY;

1129
        strcatW(*ppszDisplayName,tempStr);
Noomen Hamza's avatar
Noomen Hamza committed
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142

        CoTaskMemFree(tempStr);
        IMoniker_Release(tempMk);
    }

    IEnumMoniker_Release(enumMoniker);

    return S_OK;
}

/******************************************************************************
 *        CompositeMoniker_ParseDisplayName
 ******************************************************************************/
1143 1144 1145 1146
static HRESULT WINAPI
CompositeMonikerImpl_ParseDisplayName(IMoniker* iface, IBindCtx* pbc,
               IMoniker* pmkToLeft, LPOLESTR pszDisplayName, ULONG* pchEaten,
               IMoniker** ppmkOut)
Noomen Hamza's avatar
Noomen Hamza committed
1147 1148 1149 1150 1151 1152
{
    IEnumMoniker *enumMoniker;
    IMoniker *tempMk,*mostRigthMk,*antiMk;
    /* This method recursively calls IMoniker::ParseDisplayName on the rightmost component of the composite,*/
    /* passing everything else as the pmkToLeft parameter for that call. */

Andreas Mohr's avatar
Andreas Mohr committed
1153
    /* get the most right moniker */
Noomen Hamza's avatar
Noomen Hamza committed
1154 1155 1156 1157
    IMoniker_Enum(iface,FALSE,&enumMoniker);
    IEnumMoniker_Next(enumMoniker,1,&mostRigthMk,NULL);
    IEnumMoniker_Release(enumMoniker);

Andreas Mohr's avatar
Andreas Mohr committed
1158
    /* get the left moniker */
Noomen Hamza's avatar
Noomen Hamza committed
1159 1160 1161 1162 1163 1164 1165 1166
    CreateAntiMoniker(&antiMk);
    IMoniker_ComposeWith(iface,antiMk,0,&tempMk);
    IMoniker_Release(antiMk);

    return IMoniker_ParseDisplayName(mostRigthMk,pbc,tempMk,pszDisplayName,pchEaten,ppmkOut);
}

/******************************************************************************
Andreas Mohr's avatar
Andreas Mohr committed
1167
 *        CompositeMoniker_IsSystemMoniker
Noomen Hamza's avatar
Noomen Hamza committed
1168
 ******************************************************************************/
1169 1170
static HRESULT WINAPI
CompositeMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
Noomen Hamza's avatar
Noomen Hamza committed
1171
{
1172
    TRACE("(%p,%p)\n",iface,pwdMksys);
Noomen Hamza's avatar
Noomen Hamza committed
1173 1174 1175

    if (!pwdMksys)
        return E_POINTER;
1176

Noomen Hamza's avatar
Noomen Hamza committed
1177 1178 1179 1180 1181 1182 1183 1184
    (*pwdMksys)=MKSYS_GENERICCOMPOSITE;

    return S_OK;
}

/*******************************************************************************
 *        CompositeMonikerIROTData_QueryInterface
 *******************************************************************************/
1185 1186 1187
static HRESULT WINAPI
CompositeMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,
               VOID** ppvObject)
Noomen Hamza's avatar
Noomen Hamza committed
1188
{
1189
    CompositeMonikerImpl *This = impl_from_IROTData(iface);
Noomen Hamza's avatar
Noomen Hamza committed
1190

1191
    TRACE("(%p,%p,%p)\n",iface,riid,ppvObject);
Noomen Hamza's avatar
Noomen Hamza committed
1192

1193
    return CompositeMonikerImpl_QueryInterface(&This->IMoniker_iface, riid, ppvObject);
Noomen Hamza's avatar
Noomen Hamza committed
1194 1195 1196 1197 1198
}

/***********************************************************************
 *        CompositeMonikerIROTData_AddRef
 */
1199 1200
static ULONG WINAPI
CompositeMonikerROTDataImpl_AddRef(IROTData *iface)
Noomen Hamza's avatar
Noomen Hamza committed
1201
{
1202
    CompositeMonikerImpl *This = impl_from_IROTData(iface);
Noomen Hamza's avatar
Noomen Hamza committed
1203

1204
    TRACE("(%p)\n",iface);
Noomen Hamza's avatar
Noomen Hamza committed
1205

1206
    return IMoniker_AddRef(&This->IMoniker_iface);
Noomen Hamza's avatar
Noomen Hamza committed
1207 1208 1209 1210 1211
}

/***********************************************************************
 *        CompositeMonikerIROTData_Release
 */
1212
static ULONG WINAPI CompositeMonikerROTDataImpl_Release(IROTData* iface)
Noomen Hamza's avatar
Noomen Hamza committed
1213
{
1214
    CompositeMonikerImpl *This = impl_from_IROTData(iface);
1215

1216
    TRACE("(%p)\n",iface);
Noomen Hamza's avatar
Noomen Hamza committed
1217

1218
    return IMoniker_Release(&This->IMoniker_iface);
Noomen Hamza's avatar
Noomen Hamza committed
1219 1220 1221
}

/******************************************************************************
1222
 *        CompositeMonikerIROTData_GetComparisonData
Noomen Hamza's avatar
Noomen Hamza committed
1223
 ******************************************************************************/
1224
static HRESULT WINAPI
1225
CompositeMonikerROTDataImpl_GetComparisonData(IROTData* iface,
1226
               BYTE* pbData, ULONG cbMax, ULONG* pcbData)
Noomen Hamza's avatar
Noomen Hamza committed
1227
{
1228
    CompositeMonikerImpl *This = impl_from_IROTData(iface);
1229 1230 1231 1232
    IEnumMoniker *pEnumMk;
    IMoniker *pmk;
    HRESULT hr;

1233
    TRACE("(%p, %u, %p)\n", pbData, cbMax, pcbData);
1234 1235 1236

    *pcbData = sizeof(CLSID);

1237
    hr = IMoniker_Enum(&This->IMoniker_iface, TRUE, &pEnumMk);
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
    if (FAILED(hr)) return hr;

    while(IEnumMoniker_Next(pEnumMk, 1, &pmk, NULL) == S_OK)
    {
        IROTData *pROTData;
        hr = IMoniker_QueryInterface(pmk, &IID_IROTData, (void **)&pROTData);
        if (FAILED(hr))
            ERR("moniker doesn't support IROTData interface\n");

        if (SUCCEEDED(hr))
        {
            ULONG cbData;
            hr = IROTData_GetComparisonData(pROTData, NULL, 0, &cbData);
            IROTData_Release(pROTData);
            if (SUCCEEDED(hr) || (hr == E_OUTOFMEMORY))
            {
                *pcbData += cbData;
                hr = S_OK;
            }
            else
1258
                ERR("IROTData_GetComparisonData failed with error 0x%08x\n", hr);
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
        }

        IMoniker_Release(pmk);

        if (FAILED(hr))
        {
            IEnumMoniker_Release(pEnumMk);
            return hr;
        }
    }
    if (cbMax < *pcbData)
        return E_OUTOFMEMORY;

    IEnumMoniker_Reset(pEnumMk);

    memcpy(pbData, &CLSID_CompositeMoniker, sizeof(CLSID));
    pbData += sizeof(CLSID);
    cbMax -= sizeof(CLSID);

    while (IEnumMoniker_Next(pEnumMk, 1, &pmk, NULL) == S_OK)
    {
        IROTData *pROTData;
        hr = IMoniker_QueryInterface(pmk, &IID_IROTData, (void **)&pROTData);
        if (FAILED(hr))
            ERR("moniker doesn't support IROTData interface\n");

        if (SUCCEEDED(hr))
        {
            ULONG cbData;
            hr = IROTData_GetComparisonData(pROTData, pbData, cbMax, &cbData);
            IROTData_Release(pROTData);
            if (SUCCEEDED(hr))
            {
                pbData += cbData;
                cbMax -= cbData;
            }
            else
1296
                ERR("IROTData_GetComparisonData failed with error 0x%08x\n", hr);
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
        }

        IMoniker_Release(pmk);

        if (FAILED(hr))
        {
            IEnumMoniker_Release(pEnumMk);
            return hr;
        }
    }

    IEnumMoniker_Release(pEnumMk);

    return S_OK;
Noomen Hamza's avatar
Noomen Hamza committed
1311 1312
}

1313 1314
static HRESULT WINAPI CompositeMonikerMarshalImpl_QueryInterface(IMarshal *iface, REFIID riid, LPVOID *ppv)
{
1315
    CompositeMonikerImpl *This = impl_from_IMarshal(iface);
1316 1317 1318

    TRACE("(%p,%s,%p)\n",iface,debugstr_guid(riid),ppv);

1319
    return CompositeMonikerImpl_QueryInterface(&This->IMoniker_iface, riid, ppv);
1320 1321 1322 1323
}

static ULONG WINAPI CompositeMonikerMarshalImpl_AddRef(IMarshal *iface)
{
1324
    CompositeMonikerImpl *This = impl_from_IMarshal(iface);
1325 1326 1327

    TRACE("(%p)\n",iface);

1328
    return CompositeMonikerImpl_AddRef(&This->IMoniker_iface);
1329 1330 1331 1332
}

static ULONG WINAPI CompositeMonikerMarshalImpl_Release(IMarshal *iface)
{
1333
    CompositeMonikerImpl *This = impl_from_IMarshal(iface);
1334 1335 1336

    TRACE("(%p)\n",iface);

1337
    return CompositeMonikerImpl_Release(&This->IMoniker_iface);
1338 1339 1340
}

static HRESULT WINAPI CompositeMonikerMarshalImpl_GetUnmarshalClass(
1341
  IMarshal *iface, REFIID riid, void *pv, DWORD dwDestContext,
1342 1343
  void* pvDestContext, DWORD mshlflags, CLSID* pCid)
{
1344
    CompositeMonikerImpl *This = impl_from_IMarshal(iface);
1345

1346
    TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1347 1348
        dwDestContext, pvDestContext, mshlflags, pCid);

1349
    return IMoniker_GetClassID(&This->IMoniker_iface, pCid);
1350 1351 1352
}

static HRESULT WINAPI CompositeMonikerMarshalImpl_GetMarshalSizeMax(
1353
  IMarshal *iface, REFIID riid, void *pv, DWORD dwDestContext,
1354 1355
  void* pvDestContext, DWORD mshlflags, DWORD* pSize)
{
1356
    CompositeMonikerImpl *This = impl_from_IMarshal(iface);
1357 1358 1359 1360 1361
    IEnumMoniker *pEnumMk;
    IMoniker *pmk;
    HRESULT hr;
    ULARGE_INTEGER size;

1362
    TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1363 1364
        dwDestContext, pvDestContext, mshlflags, pSize);

1365
    *pSize = 0x10; /* to match native */
1366

1367
    hr = IMoniker_Enum(&This->IMoniker_iface, TRUE, &pEnumMk);
1368 1369
    if (FAILED(hr)) return hr;

1370
    hr = IMoniker_GetSizeMax(&This->IMoniker_iface, &size);
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393

    while (IEnumMoniker_Next(pEnumMk, 1, &pmk, NULL) == S_OK)
    {
        ULONG size;

        hr = CoGetMarshalSizeMax(&size, &IID_IMoniker, (IUnknown *)pmk, dwDestContext, pvDestContext, mshlflags);
        if (SUCCEEDED(hr))
            *pSize += size;

        IMoniker_Release(pmk);

        if (FAILED(hr))
        {
            IEnumMoniker_Release(pEnumMk);
            return hr;
        }
    }

    IEnumMoniker_Release(pEnumMk);

    return S_OK;
}

1394
static HRESULT WINAPI CompositeMonikerMarshalImpl_MarshalInterface(IMarshal *iface, IStream *pStm,
1395 1396 1397
    REFIID riid, void* pv, DWORD dwDestContext,
    void* pvDestContext, DWORD mshlflags)
{
1398
    CompositeMonikerImpl *This = impl_from_IMarshal(iface);
1399 1400 1401 1402 1403
    IEnumMoniker *pEnumMk;
    IMoniker *pmk;
    HRESULT hr;
    ULONG i = 0;

1404
    TRACE("(%p, %s, %p, %x, %p, %x)\n", pStm, debugstr_guid(riid), pv,
1405 1406
        dwDestContext, pvDestContext, mshlflags);

1407
    hr = IMoniker_Enum(&This->IMoniker_iface, TRUE, &pEnumMk);
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
    if (FAILED(hr)) return hr;

    while (IEnumMoniker_Next(pEnumMk, 1, &pmk, NULL) == S_OK)
    {
        hr = CoMarshalInterface(pStm, &IID_IMoniker, (IUnknown *)pmk, dwDestContext, pvDestContext, mshlflags);

        IMoniker_Release(pmk);

        if (FAILED(hr))
        {
            IEnumMoniker_Release(pEnumMk);
            return hr;
        }
        i++;
    }

    if (i != 2)
1425
        FIXME("moniker count of %d not supported\n", i);
1426 1427 1428 1429 1430 1431

    IEnumMoniker_Release(pEnumMk);

    return S_OK;
}

1432 1433
static HRESULT WINAPI CompositeMonikerMarshalImpl_UnmarshalInterface(IMarshal *iface, IStream *pStm,
    REFIID riid, void **ppv)
1434
{
1435
    CompositeMonikerImpl *This = impl_from_IMarshal(iface);
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
    HRESULT hr;

    TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);

    CompositeMonikerImpl_ReleaseMonikersInTable(This);

    /* resize the table if needed */
    if (This->tabLastIndex + 2 > This->tabSize)
    {
        This->tabSize += max(BLOCK_TAB_SIZE, 2);
        This->tabMoniker=HeapReAlloc(GetProcessHeap(),0,This->tabMoniker,This->tabSize*sizeof(IMoniker));

        if (This->tabMoniker==NULL)
            return E_OUTOFMEMORY;
    }

    hr = CoUnmarshalInterface(pStm, &IID_IMoniker, (void**)&This->tabMoniker[This->tabLastIndex]);
    if (FAILED(hr))
    {
1455
        ERR("couldn't unmarshal moniker, hr = 0x%08x\n", hr);
1456 1457 1458 1459 1460 1461
        return hr;
    }
    This->tabLastIndex++;
    hr = CoUnmarshalInterface(pStm, &IID_IMoniker, (void**)&This->tabMoniker[This->tabLastIndex]);
    if (FAILED(hr))
    {
1462
        ERR("couldn't unmarshal moniker, hr = 0x%08x\n", hr);
1463 1464 1465 1466
        return hr;
    }
    This->tabLastIndex++;

1467
    return IMoniker_QueryInterface(&This->IMoniker_iface, riid, ppv);
1468 1469
}

1470
static HRESULT WINAPI CompositeMonikerMarshalImpl_ReleaseMarshalData(IMarshal *iface, IStream *pStm)
1471 1472 1473 1474 1475 1476 1477
{
    TRACE("(%p)\n", pStm);
    /* can't release a state-based marshal as nothing on server side to
     * release */
    return S_OK;
}

1478 1479
static HRESULT WINAPI CompositeMonikerMarshalImpl_DisconnectObject(IMarshal *iface,
    DWORD dwReserved)
1480
{
1481
    TRACE("(0x%x)\n", dwReserved);
1482 1483 1484 1485 1486
    /* can't disconnect a state-based marshal as nothing on server side to
     * disconnect from */
    return S_OK;
}

Noomen Hamza's avatar
Noomen Hamza committed
1487 1488 1489
/******************************************************************************
 *        EnumMonikerImpl_QueryInterface
 ******************************************************************************/
1490 1491
static HRESULT WINAPI
EnumMonikerImpl_QueryInterface(IEnumMoniker* iface,REFIID riid,void** ppvObject)
Noomen Hamza's avatar
Noomen Hamza committed
1492
{
1493
    EnumMonikerImpl *This = impl_from_IEnumMoniker(iface);
1494

1495
    TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
Noomen Hamza's avatar
Noomen Hamza committed
1496 1497

    /* Perform a sanity check on the parameters.*/
1498
    if ( ppvObject==0 )
Noomen Hamza's avatar
Noomen Hamza committed
1499
	return E_INVALIDARG;
1500

Noomen Hamza's avatar
Noomen Hamza committed
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
    /* Initialize the return parameter */
    *ppvObject = 0;

    /* Compare the riid with the interface IDs implemented by this object.*/
    if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IEnumMoniker, riid))
        *ppvObject = iface;

    /* Check that we obtained an interface.*/
    if ((*ppvObject)==0)
        return E_NOINTERFACE;

    /* Query Interface always increases the reference count by one when it is successful */
1513
    IEnumMoniker_AddRef(iface);
Noomen Hamza's avatar
Noomen Hamza committed
1514 1515 1516 1517 1518 1519 1520

    return S_OK;
}

/******************************************************************************
 *        EnumMonikerImpl_AddRef
 ******************************************************************************/
1521 1522
static ULONG WINAPI
EnumMonikerImpl_AddRef(IEnumMoniker* iface)
Noomen Hamza's avatar
Noomen Hamza committed
1523
{
1524
    EnumMonikerImpl *This = impl_from_IEnumMoniker(iface);
Noomen Hamza's avatar
Noomen Hamza committed
1525

1526
    TRACE("(%p)\n",This);
Noomen Hamza's avatar
Noomen Hamza committed
1527

1528
    return InterlockedIncrement(&This->ref);
Noomen Hamza's avatar
Noomen Hamza committed
1529 1530 1531 1532 1533 1534

}

/******************************************************************************
 *        EnumMonikerImpl_Release
 ******************************************************************************/
1535 1536
static ULONG WINAPI
EnumMonikerImpl_Release(IEnumMoniker* iface)
Noomen Hamza's avatar
Noomen Hamza committed
1537
{
1538
    EnumMonikerImpl *This = impl_from_IEnumMoniker(iface);
1539 1540
    ULONG i;
    ULONG ref;
1541
    TRACE("(%p)\n",This);
Noomen Hamza's avatar
Noomen Hamza committed
1542

1543
    ref = InterlockedDecrement(&This->ref);
Noomen Hamza's avatar
Noomen Hamza committed
1544 1545

    /* destroy the object if there's no more reference on it */
1546
    if (ref == 0) {
1547

Noomen Hamza's avatar
Noomen Hamza committed
1548 1549 1550 1551 1552 1553
        for(i=0;i<This->tabSize;i++)
            IMoniker_Release(This->tabMoniker[i]);

        HeapFree(GetProcessHeap(),0,This->tabMoniker);
        HeapFree(GetProcessHeap(),0,This);
    }
1554
    return ref;
Noomen Hamza's avatar
Noomen Hamza committed
1555 1556 1557 1558 1559
}

/******************************************************************************
 *        EnumMonikerImpl_Next
 ******************************************************************************/
1560 1561 1562 1563
static HRESULT WINAPI
EnumMonikerImpl_Next(IEnumMoniker* iface,ULONG celt, IMoniker** rgelt,
               ULONG* pceltFethed)
{
1564
    EnumMonikerImpl *This = impl_from_IEnumMoniker(iface);
Noomen Hamza's avatar
Noomen Hamza committed
1565 1566 1567 1568
    ULONG i;

    /* retrieve the requested number of moniker from the current position */
    for(i=0;((This->currentPos < This->tabSize) && (i < celt));i++)
1569
    {
Noomen Hamza's avatar
Noomen Hamza committed
1570
        rgelt[i]=This->tabMoniker[This->currentPos++];
1571 1572
        IMoniker_AddRef(rgelt[i]);
    }
Noomen Hamza's avatar
Noomen Hamza committed
1573 1574 1575

    if (pceltFethed!=NULL)
        *pceltFethed= i;
1576

Noomen Hamza's avatar
Noomen Hamza committed
1577 1578 1579 1580 1581 1582 1583 1584 1585
    if (i==celt)
        return S_OK;
    else
        return S_FALSE;
}

/******************************************************************************
 *        EnumMonikerImpl_Skip
 ******************************************************************************/
1586 1587 1588
static HRESULT WINAPI
EnumMonikerImpl_Skip(IEnumMoniker* iface,ULONG celt)
{
1589
    EnumMonikerImpl *This = impl_from_IEnumMoniker(iface);
Noomen Hamza's avatar
Noomen Hamza committed
1590 1591 1592 1593 1594

    if ((This->currentPos+celt) >= This->tabSize)
        return S_FALSE;

    This->currentPos+=celt;
1595

Noomen Hamza's avatar
Noomen Hamza committed
1596 1597 1598 1599 1600 1601
    return S_OK;
}

/******************************************************************************
 *        EnumMonikerImpl_Reset
 ******************************************************************************/
1602 1603 1604
static HRESULT WINAPI
EnumMonikerImpl_Reset(IEnumMoniker* iface)
{
1605
    EnumMonikerImpl *This = impl_from_IEnumMoniker(iface);
Noomen Hamza's avatar
Noomen Hamza committed
1606 1607 1608 1609 1610 1611 1612 1613 1614

    This->currentPos=0;

    return S_OK;
}

/******************************************************************************
 *        EnumMonikerImpl_Clone
 ******************************************************************************/
1615 1616 1617
static HRESULT WINAPI
EnumMonikerImpl_Clone(IEnumMoniker* iface,IEnumMoniker** ppenum)
{
1618
    EnumMonikerImpl *This = impl_from_IEnumMoniker(iface);
Noomen Hamza's avatar
Noomen Hamza committed
1619 1620 1621 1622

    return EnumMonikerImpl_CreateEnumMoniker(This->tabMoniker,This->tabSize,This->currentPos,TRUE,ppenum);
}

1623 1624
/********************************************************************************/
/* Virtual function table for the IROTData class                                */
1625
static const IEnumMonikerVtbl VT_EnumMonikerImpl =
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
{
    EnumMonikerImpl_QueryInterface,
    EnumMonikerImpl_AddRef,
    EnumMonikerImpl_Release,
    EnumMonikerImpl_Next,
    EnumMonikerImpl_Skip,
    EnumMonikerImpl_Reset,
    EnumMonikerImpl_Clone
};

Noomen Hamza's avatar
Noomen Hamza committed
1636 1637 1638
/******************************************************************************
 *        EnumMonikerImpl_CreateEnumMoniker
 ******************************************************************************/
1639 1640 1641
static HRESULT
EnumMonikerImpl_CreateEnumMoniker(IMoniker** tabMoniker, ULONG tabSize,
               ULONG currentPos, BOOL leftToRigth, IEnumMoniker ** ppmk)
Noomen Hamza's avatar
Noomen Hamza committed
1642 1643 1644 1645
{
    EnumMonikerImpl* newEnumMoniker;
    int i;

1646 1647 1648
    if (currentPos > tabSize)
        return E_INVALIDARG;

Noomen Hamza's avatar
Noomen Hamza committed
1649 1650 1651 1652 1653 1654
    newEnumMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(EnumMonikerImpl));

    if (newEnumMoniker == 0)
        return STG_E_INSUFFICIENTMEMORY;

    /* Initialize the virtual function table. */
1655
    newEnumMoniker->IEnumMoniker_iface.lpVtbl = &VT_EnumMonikerImpl;
1656
    newEnumMoniker->ref          = 1;
Noomen Hamza's avatar
Noomen Hamza committed
1657 1658 1659 1660

    newEnumMoniker->tabSize=tabSize;
    newEnumMoniker->currentPos=currentPos;

1661
    newEnumMoniker->tabMoniker=HeapAlloc(GetProcessHeap(),0,tabSize*sizeof(IMoniker));
Noomen Hamza's avatar
Noomen Hamza committed
1662

1663 1664
    if (newEnumMoniker->tabMoniker==NULL) {
        HeapFree(GetProcessHeap(), 0, newEnumMoniker);
Noomen Hamza's avatar
Noomen Hamza committed
1665
        return E_OUTOFMEMORY;
1666
    }
Noomen Hamza's avatar
Noomen Hamza committed
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680

    if (leftToRigth)
        for (i=0;i<tabSize;i++){

            newEnumMoniker->tabMoniker[i]=tabMoniker[i];
            IMoniker_AddRef(tabMoniker[i]);
        }
    else
        for (i=tabSize-1;i>=0;i--){

            newEnumMoniker->tabMoniker[tabSize-i-1]=tabMoniker[i];
            IMoniker_AddRef(tabMoniker[i]);
        }

1681
    *ppmk=&newEnumMoniker->IEnumMoniker_iface;
Noomen Hamza's avatar
Noomen Hamza committed
1682 1683 1684 1685

    return S_OK;
}

1686 1687 1688 1689
/********************************************************************************/
/* Virtual function table for the CompositeMonikerImpl class which includes     */
/* IPersist, IPersistStream and IMoniker functions.                             */

1690
static const IMonikerVtbl VT_CompositeMonikerImpl =
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
{
    CompositeMonikerImpl_QueryInterface,
    CompositeMonikerImpl_AddRef,
    CompositeMonikerImpl_Release,
    CompositeMonikerImpl_GetClassID,
    CompositeMonikerImpl_IsDirty,
    CompositeMonikerImpl_Load,
    CompositeMonikerImpl_Save,
    CompositeMonikerImpl_GetSizeMax,
    CompositeMonikerImpl_BindToObject,
    CompositeMonikerImpl_BindToStorage,
    CompositeMonikerImpl_Reduce,
    CompositeMonikerImpl_ComposeWith,
    CompositeMonikerImpl_Enum,
    CompositeMonikerImpl_IsEqual,
    CompositeMonikerImpl_Hash,
    CompositeMonikerImpl_IsRunning,
    CompositeMonikerImpl_GetTimeOfLastChange,
    CompositeMonikerImpl_Inverse,
    CompositeMonikerImpl_CommonPrefixWith,
    CompositeMonikerImpl_RelativePathTo,
    CompositeMonikerImpl_GetDisplayName,
    CompositeMonikerImpl_ParseDisplayName,
    CompositeMonikerImpl_IsSystemMoniker
};

/********************************************************************************/
/* Virtual function table for the IROTData class.                               */
1719
static const IROTDataVtbl VT_ROTDataImpl =
1720 1721 1722 1723
{
    CompositeMonikerROTDataImpl_QueryInterface,
    CompositeMonikerROTDataImpl_AddRef,
    CompositeMonikerROTDataImpl_Release,
1724
    CompositeMonikerROTDataImpl_GetComparisonData
1725 1726
};

1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739
static const IMarshalVtbl VT_MarshalImpl =
{
    CompositeMonikerMarshalImpl_QueryInterface,
    CompositeMonikerMarshalImpl_AddRef,
    CompositeMonikerMarshalImpl_Release,
    CompositeMonikerMarshalImpl_GetUnmarshalClass,
    CompositeMonikerMarshalImpl_GetMarshalSizeMax,
    CompositeMonikerMarshalImpl_MarshalInterface,
    CompositeMonikerMarshalImpl_UnmarshalInterface,
    CompositeMonikerMarshalImpl_ReleaseMarshalData,
    CompositeMonikerMarshalImpl_DisconnectObject
};

1740 1741 1742 1743
/******************************************************************************
 *         Composite-Moniker_Construct (local function)
 *******************************************************************************/
static HRESULT
1744
CompositeMonikerImpl_Construct(IMoniker **ppMoniker, IMoniker *pmkFirst, IMoniker *pmkRest)
1745 1746 1747 1748 1749
{
    DWORD mkSys;
    IEnumMoniker *enumMoniker;
    IMoniker *tempMk;
    HRESULT res;
1750 1751
    CompositeMonikerImpl *This;

1752
    This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1753 1754 1755

    if (!This)
        return E_OUTOFMEMORY;
1756 1757 1758 1759

    TRACE("(%p,%p,%p)\n",This,pmkFirst,pmkRest);

    /* Initialize the virtual function table. */
1760 1761 1762
    This->IMoniker_iface.lpVtbl = &VT_CompositeMonikerImpl;
    This->IROTData_iface.lpVtbl = &VT_ROTDataImpl;
    This->IMarshal_iface.lpVtbl = &VT_MarshalImpl;
1763
    This->ref          = 1;
1764 1765 1766 1767 1768

    This->tabSize=BLOCK_TAB_SIZE;
    This->tabLastIndex=0;

    This->tabMoniker=HeapAlloc(GetProcessHeap(),0,This->tabSize*sizeof(IMoniker));
1769 1770
    if (This->tabMoniker==NULL) {
        HeapFree(GetProcessHeap(), 0, This);
1771
        return E_OUTOFMEMORY;
1772
    }
1773

1774
    if (!pmkFirst && !pmkRest)
1775
    {
1776
        *ppMoniker = &This->IMoniker_iface;
1777
        return S_OK;
1778
    }
1779

1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
    IMoniker_IsSystemMoniker(pmkFirst,&mkSys);

    /* put the first moniker contents in the beginning of the table */
    if (mkSys!=MKSYS_GENERICCOMPOSITE){

        This->tabMoniker[(This->tabLastIndex)++]=pmkFirst;
        IMoniker_AddRef(pmkFirst);
    }
    else{

        IMoniker_Enum(pmkFirst,TRUE,&enumMoniker);

        while(IEnumMoniker_Next(enumMoniker,1,&This->tabMoniker[This->tabLastIndex],NULL)==S_OK){


            if (++This->tabLastIndex==This->tabSize){
1796
                LPVOID tab_moniker = This->tabMoniker;
1797 1798 1799 1800

                This->tabSize+=BLOCK_TAB_SIZE;
                This->tabMoniker=HeapReAlloc(GetProcessHeap(),0,This->tabMoniker,This->tabSize*sizeof(IMoniker));

1801 1802 1803
                if (This->tabMoniker==NULL){
                    HeapFree(GetProcessHeap(), 0, tab_moniker);
                    HeapFree(GetProcessHeap(), 0, This);
1804
                    return E_OUTOFMEMORY;
1805
                }
1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848
            }
        }

        IEnumMoniker_Release(enumMoniker);
    }

    /* put the rest moniker contents after the first one and make simplification if needed */

    IMoniker_IsSystemMoniker(pmkRest,&mkSys);

    if (mkSys!=MKSYS_GENERICCOMPOSITE){

        /* add a simple moniker to the moniker table */

        res=IMoniker_ComposeWith(This->tabMoniker[This->tabLastIndex-1],pmkRest,TRUE,&tempMk);

        if (res==MK_E_NEEDGENERIC){

            /* there's no simplification in this case */
            This->tabMoniker[This->tabLastIndex]=pmkRest;

            This->tabLastIndex++;

            IMoniker_AddRef(pmkRest);
        }
        else if (tempMk==NULL){

            /* we have an antimoniker after a simple moniker so we can make a simplification in this case */
            IMoniker_Release(This->tabMoniker[This->tabLastIndex-1]);

            This->tabLastIndex--;
        }
        else if (SUCCEEDED(res)){

            /* the non-generic composition was successful so we can make a simplification in this case */
            IMoniker_Release(This->tabMoniker[This->tabLastIndex-1]);

            This->tabMoniker[This->tabLastIndex-1]=tempMk;
        } else
            return res;

        /* resize tabMoniker if needed */
        if (This->tabLastIndex==This->tabSize){
1849
            LPVOID tab_moniker = This->tabMoniker;
1850 1851 1852 1853 1854

            This->tabSize+=BLOCK_TAB_SIZE;

            This->tabMoniker=HeapReAlloc(GetProcessHeap(),0,This->tabMoniker,This->tabSize*sizeof(IMoniker));

1855 1856 1857 1858 1859
            if (This->tabMoniker==NULL){
                HeapFree(GetProcessHeap(), 0, tab_moniker);
                HeapFree(GetProcessHeap(), 0, This);
                return E_OUTOFMEMORY;
            }
1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
        }
    }
    else{

        /* add a composite moniker to the moniker table (do the same thing
         * for each moniker within the composite moniker as a simple moniker
         * (see above for how to add a simple moniker case) )
         */
        IMoniker_Enum(pmkRest,TRUE,&enumMoniker);

        while(IEnumMoniker_Next(enumMoniker,1,&This->tabMoniker[This->tabLastIndex],NULL)==S_OK){

            res=IMoniker_ComposeWith(This->tabMoniker[This->tabLastIndex-1],This->tabMoniker[This->tabLastIndex],TRUE,&tempMk);

            if (res==MK_E_NEEDGENERIC){

                This->tabLastIndex++;
            }
            else if (tempMk==NULL){

                IMoniker_Release(This->tabMoniker[This->tabLastIndex-1]);
                IMoniker_Release(This->tabMoniker[This->tabLastIndex]);
                This->tabLastIndex--;
            }
            else{

                IMoniker_Release(This->tabMoniker[This->tabLastIndex-1]);

                This->tabMoniker[This->tabLastIndex-1]=tempMk;
            }

            if (This->tabLastIndex==This->tabSize){
1892
                LPVOID tab_moniker = This->tabMoniker;
1893 1894 1895 1896 1897

                This->tabSize+=BLOCK_TAB_SIZE;

                This->tabMoniker=HeapReAlloc(GetProcessHeap(),0,This->tabMoniker,This->tabSize*sizeof(IMoniker));

1898 1899 1900
                if (This->tabMoniker==NULL){
                    HeapFree(GetProcessHeap(), 0, tab_moniker);
                    HeapFree(GetProcessHeap(), 0, This);
1901
                    return E_OUTOFMEMORY;
1902
                }
1903 1904 1905 1906 1907 1908
            }
        }

        IEnumMoniker_Release(enumMoniker);
    }

1909 1910 1911 1912 1913
    /* only one moniker, then just return it */
    if (This->tabLastIndex == 1)
    {
        *ppMoniker = This->tabMoniker[0];
        IMoniker_AddRef(*ppMoniker);
1914
        IMoniker_Release(&This->IMoniker_iface);
1915 1916
    }
    else
1917
        *ppMoniker = &This->IMoniker_iface;
1918

1919 1920 1921
    return S_OK;
}

Noomen Hamza's avatar
Noomen Hamza committed
1922
/******************************************************************************
1923
 *        CreateGenericComposite	[OLE32.@]
Noomen Hamza's avatar
Noomen Hamza committed
1924
 ******************************************************************************/
1925
HRESULT WINAPI
1926
CreateGenericComposite(IMoniker *pmkFirst, IMoniker *pmkRest, IMoniker **ppmkComposite)
Noomen Hamza's avatar
Noomen Hamza committed
1927
{
1928
    IMoniker* moniker = 0;
Noomen Hamza's avatar
Noomen Hamza committed
1929 1930
    HRESULT        hr = S_OK;

1931
    TRACE("(%p,%p,%p)\n",pmkFirst,pmkRest,ppmkComposite);
Noomen Hamza's avatar
Noomen Hamza committed
1932 1933 1934

    if (ppmkComposite==NULL)
        return E_POINTER;
1935

Noomen Hamza's avatar
Noomen Hamza committed
1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949
    *ppmkComposite=0;

    if (pmkFirst==NULL && pmkRest!=NULL){

        *ppmkComposite=pmkRest;
        return S_OK;
    }
    else if (pmkFirst!=NULL && pmkRest==NULL){
        *ppmkComposite=pmkFirst;
        return S_OK;
    }
    else  if (pmkFirst==NULL && pmkRest==NULL)
        return S_OK;

1950
    hr = CompositeMonikerImpl_Construct(&moniker,pmkFirst,pmkRest);
Noomen Hamza's avatar
Noomen Hamza committed
1951

1952
    if (FAILED(hr))
Noomen Hamza's avatar
Noomen Hamza committed
1953 1954
        return hr;

1955 1956 1957 1958
    hr = IMoniker_QueryInterface(moniker,&IID_IMoniker,(void**)ppmkComposite);
    IMoniker_Release(moniker);

    return hr;
Noomen Hamza's avatar
Noomen Hamza committed
1959 1960 1961
}

/******************************************************************************
1962
 *        MonikerCommonPrefixWith	[OLE32.@]
Noomen Hamza's avatar
Noomen Hamza committed
1963
 ******************************************************************************/
1964 1965
HRESULT WINAPI
MonikerCommonPrefixWith(IMoniker* pmkThis,IMoniker* pmkOther,IMoniker** ppmkCommon)
Noomen Hamza's avatar
Noomen Hamza committed
1966
{
1967
    FIXME("(),stub!\n");
Noomen Hamza's avatar
Noomen Hamza committed
1968 1969
    return E_NOTIMPL;
}
1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996

static HRESULT WINAPI CompositeMonikerCF_QueryInterface(LPCLASSFACTORY iface,
                                                  REFIID riid, LPVOID *ppv)
{
    *ppv = NULL;
    if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
    {
        *ppv = iface;
        IUnknown_AddRef(iface);
        return S_OK;
    }
    return E_NOINTERFACE;
}

static ULONG WINAPI CompositeMonikerCF_AddRef(LPCLASSFACTORY iface)
{
    return 2; /* non-heap based object */
}

static ULONG WINAPI CompositeMonikerCF_Release(LPCLASSFACTORY iface)
{
    return 1; /* non-heap based object */
}

static HRESULT WINAPI CompositeMonikerCF_CreateInstance(LPCLASSFACTORY iface,
    LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
{
1997
    IMoniker* pMoniker;
1998 1999 2000 2001 2002 2003 2004 2005 2006
    HRESULT  hr;

    TRACE("(%p, %s, %p)\n", pUnk, debugstr_guid(riid), ppv);

    *ppv = NULL;

    if (pUnk)
        return CLASS_E_NOAGGREGATION;

2007
    hr = CompositeMonikerImpl_Construct(&pMoniker, NULL, NULL);
2008 2009

    if (SUCCEEDED(hr))
2010 2011 2012 2013
    {
        hr = IMoniker_QueryInterface(pMoniker, riid, ppv);
        IMoniker_Release(pMoniker);
    }
2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037

    return hr;
}

static HRESULT WINAPI CompositeMonikerCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
{
    FIXME("(%d), stub!\n",fLock);
    return S_OK;
}

static const IClassFactoryVtbl CompositeMonikerCFVtbl =
{
    CompositeMonikerCF_QueryInterface,
    CompositeMonikerCF_AddRef,
    CompositeMonikerCF_Release,
    CompositeMonikerCF_CreateInstance,
    CompositeMonikerCF_LockServer
};
static const IClassFactoryVtbl *CompositeMonikerCF = &CompositeMonikerCFVtbl;

HRESULT CompositeMonikerCF_Create(REFIID riid, LPVOID *ppv)
{
    return IClassFactory_QueryInterface((IClassFactory *)&CompositeMonikerCF, riid, ppv);
}