summaryrefslogtreecommitdiff
path: root/shell/source/win32/shlxthandler/thumbviewer/thumbviewer.cxx
blob: 2c42553ee24324bc9dcdeb8d220943d62be8f2ae (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org 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 version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_shell.hxx"

#include "internal/global.hxx"

#include "internal/thumbviewer.hxx"
#include "internal/shlxthdl.hxx"
#include "internal/registry.hxx"
#include "internal/fileextensions.hxx"
#include "internal/config.hxx"
#include "internal/zipfile.hxx"
#include "internal/utilities.hxx"

#include "internal/resource.h"

#include <stdio.h>
#include <utility>
#include <stdlib.h>

#if defined _MSC_VER
#pragma warning(push, 1)
#endif
#include <shellapi.h>
#if defined _MSC_VER
#pragma warning(pop)
#endif
#include <memory>

extern HINSTANCE g_hModule;

namespace internal
{
    /* The signet.png used for thumbnails of signed documents
       is contained as resource in this module, the resource
       id is 2000 */
    void LoadSignetImageFromResource(ZipFile::ZipContentBuffer_t& buffer)
    {
        HRSRC hrc = FindResource(g_hModule, TEXT("#2000"), RT_RCDATA);
        DWORD size = SizeofResource(g_hModule, hrc);
        HGLOBAL hglob = LoadResource(g_hModule, hrc);
        char* data = reinterpret_cast<char*>(LockResource(hglob));
        buffer = ZipFile::ZipContentBuffer_t(data, data + size);
    }

    bool IsSignedDocument(const ZipFile* zipfile)
    {
        return zipfile->HasContent("META-INF/documentsignatures.xml");
    }

    bool IsWindowsXP()
    {
        OSVERSIONINFO osvi;
        ZeroMemory(&osvi, sizeof(osvi));
        osvi.dwOSVersionInfoSize = sizeof(osvi);
        GetVersionEx(&osvi);

        return ((osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) &&
                ((osvi.dwMajorVersion >= 5) && (osvi.dwMinorVersion >= 1)));
    }

    /* Calculate where to position the signet image.
       On Windows ME we need to shift the signet a
       little bit to the left because Windows ME
       puts an overlay icon to the lower right
       corner of a thumbnail image so that our signet
       we be hidden. */
    Gdiplus::Point CalcSignetPosition(
        const Gdiplus::Rect& canvas, const Gdiplus::Rect& thumbnail_border, const Gdiplus::Rect& signet)
    {
        int x = 0;
        int y = 0;
        int hoffset = canvas.GetRight() - thumbnail_border.GetRight();
        int voffset = canvas.GetBottom() - thumbnail_border.GetBottom();

        if (hoffset > voffset)
        {
            x = thumbnail_border.GetRight() - signet.GetRight() + min(signet.GetRight() / 2, hoffset);
            y = thumbnail_border.GetBottom() - signet.GetBottom();
        }
        else
        {
            x = thumbnail_border.GetRight() - signet.GetRight();
            y = thumbnail_border.GetBottom() - signet.GetBottom() + min(signet.GetBottom() / 2, voffset);
        }

        if (!IsWindowsXP())
            x -= 15;

        return Gdiplus::Point(x,y);
    }
}

class StreamOnZipBuffer : public IStream
{
public:
    StreamOnZipBuffer(const ZipFile::ZipContentBuffer_t& zip_buffer);

    // IUnknown
    virtual ULONG STDMETHODCALLTYPE AddRef();
    virtual ULONG STDMETHODCALLTYPE Release( void);
    virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject);

    // IStream
    virtual HRESULT STDMETHODCALLTYPE Read(void *pv, ULONG cb, ULONG *pcbRead);
    virtual HRESULT STDMETHODCALLTYPE Write(void const *pv, ULONG cb, ULONG *pcbWritten);
    virtual HRESULT STDMETHODCALLTYPE Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition);
    virtual HRESULT STDMETHODCALLTYPE SetSize(ULARGE_INTEGER libNewSize);
    virtual HRESULT STDMETHODCALLTYPE CopyTo(IStream *pstm, ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten);
    virtual HRESULT STDMETHODCALLTYPE Commit(DWORD grfCommitFlags);
    virtual HRESULT STDMETHODCALLTYPE Revert(void);
    virtual HRESULT STDMETHODCALLTYPE LockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
    virtual HRESULT STDMETHODCALLTYPE UnlockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
    virtual HRESULT STDMETHODCALLTYPE Stat(STATSTG *pstatstg, DWORD grfStatFlag);
    virtual HRESULT STDMETHODCALLTYPE Clone(IStream **ppstm);

private:
    LONG ref_count_;
    const ZipFile::ZipContentBuffer_t& ref_zip_buffer_;
    size_t pos_;
};

StreamOnZipBuffer::StreamOnZipBuffer(const ZipFile::ZipContentBuffer_t& zip_buffer) :
    ref_count_(1),
    ref_zip_buffer_(zip_buffer),
    pos_(0)
{
}

// IUnknown methods

ULONG STDMETHODCALLTYPE StreamOnZipBuffer::AddRef(void)
{
    return InterlockedIncrement(&ref_count_);
}

ULONG STDMETHODCALLTYPE StreamOnZipBuffer::Release( void)
{
    long refcnt = InterlockedDecrement(&ref_count_);

    if (0 == ref_count_)
        delete this;

    return refcnt;
}

HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject)
{
    *ppvObject = 0;
    IUnknown* pUnk = 0;

    if ((IID_IUnknown == riid) || (IID_IStream == riid))
    {
        pUnk = static_cast<IStream*>(this);
        pUnk->AddRef();
        *ppvObject = pUnk;
        return S_OK;
    }
    return E_NOINTERFACE;
}

HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Read(void *pv, ULONG cb, ULONG *pcbRead)
{
    if (pv == NULL)
        return STG_E_INVALIDPOINTER;

    size_t size = ref_zip_buffer_.size();

    if (pos_ > size)
        return S_FALSE;

    char* p = reinterpret_cast<char*>(pv);
    ULONG read = 0;

    for ( ;(pos_ < size) && (cb > 0); pos_++, cb--, read++)
        *p++ = ref_zip_buffer_[pos_];

    if (pcbRead)
        *pcbRead = read;

    return S_OK;
}

HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *)
{
    __int64 size = (__int64) ref_zip_buffer_.size();
    __int64 p = 0;

    switch (dwOrigin)
    {
        case STREAM_SEEK_SET:
            break;
        case STREAM_SEEK_CUR:
            p = (__int64) pos_;
            break;
        case STREAM_SEEK_END:
            p = size - 1;
            break;
   }

   HRESULT hr = STG_E_INVALIDFUNCTION;

   p += dlibMove.QuadPart;

   if ( ( p >= 0 ) && (p < size) )
   {
        pos_ = (size_t) p;
        hr = S_OK;
   }
   return hr;
}

HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Stat(STATSTG *pstatstg, DWORD grfStatFlag)
{
    if (pstatstg == NULL)
        return STG_E_INVALIDPOINTER;

    ZeroMemory(pstatstg, sizeof(STATSTG));

    if (grfStatFlag == STATFLAG_DEFAULT)
    {
        size_t sz = 4 * sizeof(wchar_t);
        wchar_t* name = reinterpret_cast<wchar_t*>(CoTaskMemAlloc(sz));
        ZeroMemory(name, sz);
        memcpy(name, L"png", 3 * sizeof(wchar_t));
        pstatstg->pwcsName = name;
    }

    pstatstg->type = STGTY_LOCKBYTES;

    ULARGE_INTEGER uli;
    uli.LowPart = ref_zip_buffer_.size();
    uli.HighPart = 0;

    pstatstg->cbSize = uli;

    return S_OK;
}

HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Write(void const *, ULONG, ULONG *)
{ return E_NOTIMPL; }

HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::SetSize(ULARGE_INTEGER)
{ return E_NOTIMPL; }

HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::CopyTo(IStream *, ULARGE_INTEGER, ULARGE_INTEGER *, ULARGE_INTEGER *)
{ return E_NOTIMPL; }

HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Commit(DWORD)
{ return E_NOTIMPL; }

HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Revert(void)
{ return E_NOTIMPL; }

HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::LockRegion(ULARGE_INTEGER, ULARGE_INTEGER, DWORD)
{ return E_NOTIMPL; }

HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::UnlockRegion(ULARGE_INTEGER, ULARGE_INTEGER, DWORD)
{ return E_NOTIMPL; }

HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Clone(IStream **)
{  return E_NOTIMPL; }


//#########################################


CThumbviewer::CThumbviewer(long RefCnt) :
    ref_count_(RefCnt)
{
    InterlockedIncrement(&g_DllRefCnt);

    thumbnail_size_.cx = 0;
    thumbnail_size_.cy = 0;

    Gdiplus::GdiplusStartupInput gdiplusStartupInput;
    Gdiplus::GdiplusStartup(&gdiplus_token_, &gdiplusStartupInput, NULL);

    ZipFile::ZipContentBuffer_t img_data;
    internal::LoadSignetImageFromResource(img_data);
    IStream* stream = new StreamOnZipBuffer(img_data);
    signet_ = new Gdiplus::Bitmap(stream, TRUE);
    stream->Release();
}

CThumbviewer::~CThumbviewer()
{
    delete signet_;
    Gdiplus::GdiplusShutdown(gdiplus_token_);
    InterlockedDecrement(&g_DllRefCnt);
}

// IUnknown methods

HRESULT STDMETHODCALLTYPE CThumbviewer::QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject)
{
    *ppvObject = 0;
    IUnknown* pUnk = 0;

    if ((IID_IUnknown == riid) || (IID_IPersistFile == riid))
    {
        pUnk = static_cast<IPersistFile*>(this);
        pUnk->AddRef();
        *ppvObject = pUnk;
        return S_OK;
    }
    else if (IID_IExtractImage == riid)
    {
        pUnk = static_cast<IExtractImage*>(this);
        pUnk->AddRef();
        *ppvObject = pUnk;
        return S_OK;
    }
    return E_NOINTERFACE;
}

ULONG STDMETHODCALLTYPE CThumbviewer::AddRef(void)
{
    return InterlockedIncrement(&ref_count_);
}

ULONG STDMETHODCALLTYPE CThumbviewer::Release( void)
{
    long refcnt = InterlockedDecrement(&ref_count_);

    if (0 == ref_count_)
        delete this;

    return refcnt;
}

// IExtractImage2 methods

const std::string THUMBNAIL_CONTENT = "Thumbnails/thumbnail.png";

HRESULT STDMETHODCALLTYPE CThumbviewer::Extract(HBITMAP *phBmpImage)
{
    HRESULT hr = E_FAIL;

    try
    {
        std::auto_ptr<ZipFile> zipfile(new ZipFile(WStringToString(filename_)));

        if (zipfile->HasContent(THUMBNAIL_CONTENT))
        {
            ZipFile::ZipContentBuffer_t thumbnail;
            zipfile->GetUncompressedContent(THUMBNAIL_CONTENT, thumbnail);
            IStream* stream = new StreamOnZipBuffer(thumbnail);

            Gdiplus::Bitmap thumbnail_png(stream, TRUE);

            if ((thumbnail_png.GetHeight() == 0) || (thumbnail_png.GetWidth() == 0))
            {
                stream->Release();
                return E_FAIL;
            }

            HWND hwnd = GetDesktopWindow();
            HDC hdc = GetDC(hwnd);
            HDC memDC = CreateCompatibleDC(hdc);

            if (memDC)
            {
                UINT offset = 3; // reserve a little border space

                Gdiplus::Rect canvas(0, 0, thumbnail_size_.cx, thumbnail_size_.cy);
                Gdiplus::Rect canvas_thumbnail(offset, offset, thumbnail_size_.cx - 2 * offset, thumbnail_size_.cy - 2 * offset);

                Gdiplus::Rect scaledRect = CalcScaledAspectRatio(
                    Gdiplus::Rect(0, 0, thumbnail_png.GetWidth(), thumbnail_png.GetHeight()), canvas_thumbnail);

                struct {
                    BITMAPINFOHEADER bi;
                    DWORD ct[256];
                } dib;

                ZeroMemory(&dib, sizeof(dib));

                dib.bi.biSize = sizeof(BITMAPINFOHEADER);
                dib.bi.biWidth = thumbnail_size_.cx;
                dib.bi.biHeight = thumbnail_size_.cy;
                dib.bi.biPlanes = 1;
                dib.bi.biBitCount = static_cast<WORD>(color_depth_);
                dib.bi.biCompression = BI_RGB;

                LPVOID lpBits;
                HBITMAP hMemBmp = CreateDIBSection(memDC, (LPBITMAPINFO)&dib, DIB_RGB_COLORS, &lpBits, NULL, 0);
                HGDIOBJ hOldObj = SelectObject(memDC, hMemBmp);

                Gdiplus::Graphics graphics(memDC);
                Gdiplus::Pen blackPen(Gdiplus::Color(255, 0, 0, 0), 1);

                Gdiplus::SolidBrush whiteBrush(Gdiplus::Color(255, 255, 255, 255));
                graphics.FillRectangle(&whiteBrush, canvas);

                scaledRect.X = (canvas.Width - scaledRect.Width) / 2;
                scaledRect.Y = (canvas.Height - scaledRect.Height) / 2;

                Gdiplus::Rect border_rect(scaledRect.X, scaledRect.Y, scaledRect.Width, scaledRect.Height);
                graphics.DrawRectangle(&blackPen, border_rect);

                scaledRect.X += 1;
                scaledRect.Y += 1;
                scaledRect.Width -= 1;
                scaledRect.Height -= 1;

                graphics.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);
                Gdiplus::Status stat = graphics.DrawImage(
                    &thumbnail_png, scaledRect, 0 , 0,
                    thumbnail_png.GetWidth(), thumbnail_png.GetHeight(), Gdiplus::UnitPixel);

                /* Add a signet sign to the thumbnail of signed documents */
                if (internal::IsSignedDocument(zipfile.get()))
                {
                    double SCALING_FACTOR = 0.6;
                    Gdiplus::Rect signet_scaled(
                        0, 0, static_cast<INT>(signet_->GetWidth() * SCALING_FACTOR), static_cast<INT>(signet_->GetHeight() * SCALING_FACTOR));
                    Gdiplus::Point pos_signet = internal::CalcSignetPosition(canvas_thumbnail, border_rect, signet_scaled);
                    Gdiplus::Rect dest(pos_signet.X, pos_signet.Y, signet_scaled.GetRight(), signet_scaled.GetBottom());

                    stat = graphics.DrawImage(
                        signet_, dest,
                        0, 0, signet_->GetWidth(), signet_->GetHeight(),
                        Gdiplus::UnitPixel);
                }

                if (stat == Gdiplus::Ok)
                {
                    *phBmpImage = hMemBmp;
                    hr = NOERROR;
                }

                SelectObject(memDC, hOldObj);
                DeleteDC(memDC);
            }

            ReleaseDC(hwnd, hdc);
            stream->Release();
        }
    }
    catch(std::exception&)
    {
        OutputDebugStringFormat( "CThumbviewer Extract ERROR!\n" );
        hr = E_FAIL;
    }
    return hr;
}

HRESULT STDMETHODCALLTYPE CThumbviewer::GetLocation(
    LPWSTR pszPathBuffer, DWORD cchMax, DWORD *pdwPriority, const SIZE *prgSize, DWORD dwRecClrDepth, DWORD *pdwFlags)
{
    if ((prgSize == NULL) || (pdwFlags == NULL) || ((*pdwFlags & IEIFLAG_ASYNC) && (pdwPriority == NULL)))
        return E_INVALIDARG;

    thumbnail_size_ = *prgSize;
    color_depth_ = dwRecClrDepth;

    *pdwFlags = IEIFLAG_CACHE; // we don't cache the image

    wcsncpy(pszPathBuffer, filename_.c_str(), cchMax);

    return NOERROR;
}

// IPersist methods

HRESULT STDMETHODCALLTYPE CThumbviewer::GetClassID(CLSID* pClassID)
{
    pClassID = const_cast<CLSID*>(&CLSID_THUMBVIEWER_HANDLER);
    return S_OK;
}

// IPersistFile methods

HRESULT STDMETHODCALLTYPE CThumbviewer::Load(LPCOLESTR pszFileName, DWORD)
{
    filename_ = pszFileName;
    return S_OK;
}

HRESULT STDMETHODCALLTYPE CThumbviewer::IsDirty()
{ return E_NOTIMPL; }

HRESULT STDMETHODCALLTYPE CThumbviewer::Save(LPCOLESTR, BOOL)
{ return E_NOTIMPL; }

HRESULT STDMETHODCALLTYPE CThumbviewer::SaveCompleted(LPCOLESTR)
{ return E_NOTIMPL; }

HRESULT STDMETHODCALLTYPE CThumbviewer::GetCurFile(LPOLESTR __RPC_FAR*)
{ return E_NOTIMPL; }


Gdiplus::Rect CThumbviewer::CalcScaledAspectRatio(Gdiplus::Rect src, Gdiplus::Rect dest)
{
    Gdiplus::Rect result;
    if (src.Width >= src.Height)
        result = Gdiplus::Rect(0, 0, dest.Width, src.Height * dest.Width / src.Width);
    else
        result = Gdiplus::Rect(0, 0, src.Width * dest.Height / src.Height, dest.Height);

    return result;
}