Debian Codegraph @

shell/source/win32/shlxthandler/thumbviewer/thumbviewer.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20 #include <global.hxx>
21
22 #include <thumbviewer.hxx>
23 #include <shlxthdl.hxx>
24 #include <registry.hxx>
25 #include <fileextensions.hxx>
26 #include <config.hxx>
27 #include <zipfile.hxx>
28 #include <utilities.hxx>
29
30 #include <resource.h>
31
32 #include <stdio.h>
33 #include <utility>
34 #include <stdlib.h>
35
36 #include <shellapi.h>
37 #include <objidl.h>
38 #include <shlobj.h>
39 #include <gdiplus.h>
40
41 #include <algorithm>
42 #include <atomic>
43 #include <memory>
44 #include <string>
45
46 namespace
47 {
48 /* The signet.png used for thumbnails of signed documents
49 is contained as resource in this module, the resource
50 id is 2000 */
51 static ZipFile::ZipContentBuffer_t LoadSignetImageFromResource()
52 {
53 HRSRC hrc = FindResourceW(g_hModule, L"#2000", reinterpret_cast<LPWSTR>(RT_RCDATA));
54 DWORD size = SizeofResource(g_hModule, hrc);
55 HGLOBAL hglob = LoadResource(g_hModule, hrc);
56 char* data = static_cast<char*>(LockResource(hglob));
57 return ZipFile::ZipContentBuffer_t(data, data + size);
58 }
59
60 static bool IsSignedDocument(const ZipFile& zipfile)
61 {
62 return zipfile.HasContent("META-INF/documentsignatures.xml");
63 }
64
65 static Gdiplus::Point CalcSignetPosition(
66 const Gdiplus::Rect& canvas, const Gdiplus::Rect& thumbnail_border, const Gdiplus::Rect& signet)
67 {
68 int x = 0;
69 int y = 0;
70 int hoffset = canvas.GetRight() - thumbnail_border.GetRight();
71 int voffset = canvas.GetBottom() - thumbnail_border.GetBottom();
72
73 if (hoffset > voffset)
74 {
75 x = thumbnail_border.GetRight() - signet.GetRight() + min(signet.GetRight() / 2, hoffset);
76 y = thumbnail_border.GetBottom() - signet.GetBottom();
77 }
78 else
79 {
80 x = thumbnail_border.GetRight() - signet.GetRight();
81 y = thumbnail_border.GetBottom() - signet.GetBottom() + min(signet.GetBottom() / 2, voffset);
82 }
83
84 return Gdiplus::Point(x,y);
85 }
86
87 Gdiplus::Rect CalcScaledAspectRatio(const Gdiplus::Rect& src, const Gdiplus::Rect& dest)
88 {
89 Gdiplus::Rect result;
90 if (src.Width >= src.Height)
91 result = Gdiplus::Rect(0, 0, dest.Width, src.Height * dest.Width / src.Width);
92 else
93 result = Gdiplus::Rect(0, 0, src.Width * dest.Height / src.Height, dest.Height);
94
95 return result;
96 }
97
98 class StreamOnZipBuffer final : public IStream
99 {
100 public:
101 struct SmartPtr
102 {
103 ~SmartPtr() { p->Release(); }
104 operator IStream*() { return p; }
105
106 IStream* p;
107 };
108
109 static SmartPtr Create(ZipFile::ZipContentBuffer_t&& zip_buffer)
110 {
111 return { new StreamOnZipBuffer(std::move(zip_buffer)) };
112 }
113
114 // IUnknown
115 virtual ULONG STDMETHODCALLTYPE AddRef() override;
116 virtual ULONG STDMETHODCALLTYPE Release() override;
117 virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject) override;
118
119 // IStream
120 virtual HRESULT STDMETHODCALLTYPE Read(void *pv, ULONG cb, ULONG *pcbRead) override;
121 virtual HRESULT STDMETHODCALLTYPE Write(void const *pv, ULONG cb, ULONG *pcbWritten) override;
122 virtual HRESULT STDMETHODCALLTYPE Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition) override;
123 virtual HRESULT STDMETHODCALLTYPE SetSize(ULARGE_INTEGER libNewSize) override;
124 virtual HRESULT STDMETHODCALLTYPE CopyTo(IStream *pstm, ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten) override;
125 virtual HRESULT STDMETHODCALLTYPE Commit(DWORD grfCommitFlags) override;
126 virtual HRESULT STDMETHODCALLTYPE Revert() override;
127 virtual HRESULT STDMETHODCALLTYPE LockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType) override;
128 virtual HRESULT STDMETHODCALLTYPE UnlockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType) override;
129 virtual HRESULT STDMETHODCALLTYPE Stat(STATSTG *pstatstg, DWORD grfStatFlag) override;
130 virtual HRESULT STDMETHODCALLTYPE Clone(IStream **ppstm) override;
131
132 private:
133 explicit StreamOnZipBuffer(ZipFile::ZipContentBuffer_t&& zip_buffer)
134 : zip_buffer_(std::move(zip_buffer))
135 {
136 }
137
138 std::atomic<ULONG> ref_count_ = 1;
139 const ZipFile::ZipContentBuffer_t zip_buffer_;
140 size_t pos_ = 0;
141 };
142
143 // IUnknown methods
144
145 ULONG STDMETHODCALLTYPE StreamOnZipBuffer::AddRef()
146 {
147 return ++ref_count_;
148 }
149
150 ULONG STDMETHODCALLTYPE StreamOnZipBuffer::Release()
151 {
152 const ULONG refcnt = --ref_count_;
153
154 if (0 == refcnt)
155 delete this;
156
157 return refcnt;
158 }
159
160 HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject)
161 {
162 *ppvObject = nullptr;
163
164 if ((IID_IUnknown == riid) || (IID_IStream == riid))
165 {
166 *ppvObject = this;
167 AddRef();
168 return S_OK;
169 }
170 return E_NOINTERFACE;
171 }
172
173 HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Read(void *pv, ULONG cb, ULONG *pcbRead)
174 {
175 if (pv == nullptr)
176 return STG_E_INVALIDPOINTER;
177
178 char* p = static_cast<char*>(pv);
179 const size_t available = pos_ < zip_buffer_.size() ? zip_buffer_.size() - pos_ : 0;
180 const ULONG read = min(available, cb);
181 if (read > 0)
182 {
183 std::copy_n(zip_buffer_.data() + pos_, read, p);
184 pos_ += read;
185 }
186
187 if (pcbRead)
188 *pcbRead = read;
189
190 return read == cb ? S_OK : S_FALSE;
191 }
192
193 HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin,
194 ULARGE_INTEGER* plibNewPosition)
195 {
196 const __int64 size = zip_buffer_.size();
197 __int64 p;
198
199 switch (dwOrigin)
200 {
201 case STREAM_SEEK_SET:
202 p = 0;
203 break;
204 case STREAM_SEEK_CUR:
205 p = pos_;
206 break;
207 case STREAM_SEEK_END:
208 p = size;
209 break;
210 default:
211 return STG_E_INVALIDFUNCTION;
212 }
213
214 p += dlibMove.QuadPart;
215 if (p < 0)
216 return STG_E_INVALIDFUNCTION;
217
218 if (p > size)
219 p = size;
220
221 pos_ = p;
222 if (plibNewPosition)
223 *plibNewPosition = { .QuadPart = pos_ };
224 return S_OK;
225 }
226
227 HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Stat(STATSTG *pstatstg, DWORD grfStatFlag)
228 {
229 if (pstatstg == nullptr)
230 return STG_E_INVALIDPOINTER;
231 if (grfStatFlag != STATFLAG_DEFAULT && grfStatFlag != STATFLAG_NONAME)
232 return STG_E_INVALIDFLAG;
233
234 ZeroMemory(pstatstg, sizeof(STATSTG));
235
236 if (grfStatFlag != STATFLAG_NONAME)
237 {
238 size_t sz = 4 * sizeof(wchar_t);
239 wchar_t* name = static_cast<wchar_t*>(CoTaskMemAlloc(sz));
240 if (!name)
241 return STG_E_INSUFFICIENTMEMORY;
242 wcscpy(name, L"png");
243 pstatstg->pwcsName = name;
244 }
245
246 pstatstg->type = STGTY_LOCKBYTES;
247
248 static_assert(std::is_integral_v<decltype(pstatstg->cbSize.QuadPart)>);
249 pstatstg->cbSize = { .QuadPart = zip_buffer_.size() };
250
251 return S_OK;
252 }
253
254 HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Write(void const *, ULONG, ULONG *)
255 { return E_NOTIMPL; }
256
257 HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::SetSize(ULARGE_INTEGER)
258 { return E_NOTIMPL; }
259
260 HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::CopyTo(IStream *, ULARGE_INTEGER, ULARGE_INTEGER *, ULARGE_INTEGER *)
261 { return E_NOTIMPL; }
262
263 HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Commit(DWORD)
264 { return E_NOTIMPL; }
265
266 HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Revert()
267 { return E_NOTIMPL; }
268
269 HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::LockRegion(ULARGE_INTEGER, ULARGE_INTEGER, DWORD)
270 { return E_NOTIMPL; }
271
272 HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::UnlockRegion(ULARGE_INTEGER, ULARGE_INTEGER, DWORD)
273 { return E_NOTIMPL; }
274
275 HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Clone(IStream **)
276 { return E_NOTIMPL; }
277
278 class CThumbviewer : public IPersistFile, public IExtractImage
279 {
280 public:
281 CThumbviewer();
282 virtual ~CThumbviewer();
283
284 // IUnknown methods
285
286 virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid,
287 void __RPC_FAR* __RPC_FAR* ppvObject) override;
288
289 virtual ULONG STDMETHODCALLTYPE AddRef() override;
290
291 virtual ULONG STDMETHODCALLTYPE Release() override;
292
293 // IExtractImage methods
294
295 virtual HRESULT STDMETHODCALLTYPE Extract(HBITMAP* phBmpImage) override;
296
297 virtual HRESULT STDMETHODCALLTYPE GetLocation(LPWSTR pszPathBuffer, DWORD cchMax,
298 DWORD* pdwPriority, const SIZE* prgSize,
299 DWORD dwRecClrDepth, DWORD* pdwFlags) override;
300
301 // IPersist methods
302
303 virtual HRESULT STDMETHODCALLTYPE GetClassID(CLSID* pClassID) override;
304
305 // IPersistFile methods
306
307 virtual HRESULT STDMETHODCALLTYPE IsDirty() override;
308
309 virtual HRESULT STDMETHODCALLTYPE Load(
310 /* [in] */ LPCOLESTR pszFileName,
311 /* [in] */ DWORD dwMode) override;
312
313 virtual HRESULT STDMETHODCALLTYPE Save(
314 /* [unique][in] */ LPCOLESTR pszFileName,
315 /* [in] */ BOOL fRemember) override;
316
317 virtual HRESULT STDMETHODCALLTYPE SaveCompleted(
318 /* [unique][in] */ LPCOLESTR pszFileName) override;
319
320 virtual HRESULT STDMETHODCALLTYPE GetCurFile(
321 /* [out] */ LPOLESTR __RPC_FAR* ppszFileName) override;
322
323 private:
324 std::atomic<ULONG> ref_count_ = 1;
325 DWORD color_depth_ = 0;
326 std::wstring filename_;
327 SIZE thumbnail_size_ = { .cx = 0, .cy = 0 };
328 ULONG_PTR gdiplus_token_;
329 Gdiplus::Bitmap* signet_;
330 };
331
332 CThumbviewer::CThumbviewer()
333 {
334 InterlockedIncrement(&g_DllRefCnt);
335
336 Gdiplus::GdiplusStartupInput gdiplusStartupInput;
337 Gdiplus::GdiplusStartup(&gdiplus_token_, &gdiplusStartupInput, nullptr);
338
339 signet_ = new Gdiplus::Bitmap(
340 StreamOnZipBuffer::Create(LoadSignetImageFromResource()), TRUE);
341 }
342
343 CThumbviewer::~CThumbviewer()
344 {
345 delete signet_;
346 Gdiplus::GdiplusShutdown(gdiplus_token_);
347 InterlockedDecrement(&g_DllRefCnt);
348 }
349
350 // IUnknown methods
351
352 HRESULT STDMETHODCALLTYPE CThumbviewer::QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject)
353 {
354 *ppvObject = nullptr;
355
356 if ((IID_IUnknown == riid) || (IID_IPersistFile == riid))
357 {
358 *ppvObject = static_cast<IPersistFile*>(this);
359 AddRef();
360 return S_OK;
361 }
362 else if (IID_IExtractImage == riid)
363 {
364 *ppvObject = static_cast<IExtractImage*>(this);
365 AddRef();
366 return S_OK;
367 }
368 return E_NOINTERFACE;
369 }
370
371 ULONG STDMETHODCALLTYPE CThumbviewer::AddRef()
372 {
373 return ++ref_count_;
374 }
375
376 ULONG STDMETHODCALLTYPE CThumbviewer::Release()
377 {
378 const ULONG refcnt = --ref_count_;
379
380 if (0 == refcnt)
381 delete this;
382
383 return refcnt;
384 }
385
386 // IExtractImage2 methods
387
388 const std::string THUMBNAIL_CONTENT = "Thumbnails/thumbnail.png";
389
390 HRESULT STDMETHODCALLTYPE CThumbviewer::Extract(HBITMAP *phBmpImage)
391 {
392 HRESULT hr = E_FAIL;
393
394 try
395 {
396 std::wstring fname = getShortPathName( filename_ );
397 ZipFile zipfile(fname);
398
399 if (zipfile.HasContent(THUMBNAIL_CONTENT))
400 {
401 ZipFile::ZipContentBuffer_t thumbnail;
402 zipfile.GetUncompressedContent(THUMBNAIL_CONTENT, thumbnail);
403
404 Gdiplus::Bitmap thumbnail_png(StreamOnZipBuffer::Create(std::move(thumbnail)), TRUE);
405
406 if ((thumbnail_png.GetHeight() == 0) || (thumbnail_png.GetWidth() == 0))
407 {
408 return E_FAIL;
409 }
410
411 HWND hwnd = GetDesktopWindow();
412 HDC hdc = GetDC(hwnd);
413 HDC memDC = CreateCompatibleDC(hdc);
414
415 if (memDC)
416 {
417 UINT offset = 3; // reserve a little border space
418
419 Gdiplus::Rect canvas(0, 0, thumbnail_size_.cx, thumbnail_size_.cy);
420 Gdiplus::Rect canvas_thumbnail(offset, offset, thumbnail_size_.cx - 2 * offset, thumbnail_size_.cy - 2 * offset);
421
422 Gdiplus::Rect scaledRect = CalcScaledAspectRatio(
423 Gdiplus::Rect(0, 0, thumbnail_png.GetWidth(), thumbnail_png.GetHeight()), canvas_thumbnail);
424
425 struct {
426 BITMAPINFOHEADER bi;
427 DWORD ct[256];
428 } dib;
429
430 ZeroMemory(&dib, sizeof(dib));
431
432 dib.bi.biSize = sizeof(BITMAPINFOHEADER);
433 dib.bi.biWidth = thumbnail_size_.cx;
434 dib.bi.biHeight = thumbnail_size_.cy;
435 dib.bi.biPlanes = 1;
436 dib.bi.biBitCount = static_cast<WORD>(color_depth_);
437 dib.bi.biCompression = BI_RGB;
438
439 LPVOID lpBits;
440 HBITMAP hMemBmp = CreateDIBSection(memDC, reinterpret_cast<LPBITMAPINFO>(&dib), DIB_RGB_COLORS, &lpBits, nullptr, 0);
441 HGDIOBJ hOldObj = SelectObject(memDC, hMemBmp);
442
443 Gdiplus::Graphics graphics(memDC);
444 Gdiplus::Pen blackPen(Gdiplus::Color(255, 0, 0, 0), 1);
445
446 Gdiplus::SolidBrush whiteBrush(Gdiplus::Color(255, 255, 255, 255));
447 graphics.FillRectangle(&whiteBrush, canvas);
448
449 scaledRect.X = (canvas.Width - scaledRect.Width) / 2;
450 scaledRect.Y = (canvas.Height - scaledRect.Height) / 2;
451
452 Gdiplus::Rect border_rect(scaledRect.X, scaledRect.Y, scaledRect.Width, scaledRect.Height);
453 graphics.DrawRectangle(&blackPen, border_rect);
454
455 scaledRect.X += 1;
456 scaledRect.Y += 1;
457 scaledRect.Width -= 1;
458 scaledRect.Height -= 1;
459
460 graphics.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);
461 Gdiplus::Status stat = graphics.DrawImage(
462 &thumbnail_png, scaledRect, 0 , 0,
463 thumbnail_png.GetWidth(), thumbnail_png.GetHeight(), Gdiplus::UnitPixel);
464
465 /* Add a signet sign to the thumbnail of signed documents */
466 if (IsSignedDocument(zipfile))
467 {
468 double SCALING_FACTOR = 0.6;
469 Gdiplus::Rect signet_scaled(
470 0, 0, static_cast<INT>(signet_->GetWidth() * SCALING_FACTOR), static_cast<INT>(signet_->GetHeight() * SCALING_FACTOR));
471 Gdiplus::Point pos_signet = CalcSignetPosition(canvas_thumbnail, border_rect, signet_scaled);
472 Gdiplus::Rect dest(pos_signet.X, pos_signet.Y, signet_scaled.GetRight(), signet_scaled.GetBottom());
473
474 stat = graphics.DrawImage(
475 signet_, dest,
476 0, 0, signet_->GetWidth(), signet_->GetHeight(),
477 Gdiplus::UnitPixel);
478 }
479
480 if (stat == Gdiplus::Ok)
481 {
482 *phBmpImage = hMemBmp;
483 hr = NOERROR;
484 }
485
486 SelectObject(memDC, hOldObj);
487 DeleteDC(memDC);
488 }
489
490 ReleaseDC(hwnd, hdc);
491 }
492 }
493 catch(std::exception&)
494 {
495 OutputDebugStringFormatW( L"CThumbviewer Extract ERROR!\n" );
496 hr = E_FAIL;
497 }
498 return hr;
499 }
500
501 HRESULT STDMETHODCALLTYPE CThumbviewer::GetLocation(
502 LPWSTR pszPathBuffer, DWORD cchMax, DWORD *pdwPriority, const SIZE *prgSize, DWORD dwRecClrDepth, DWORD *pdwFlags)
503 {
504 if ((prgSize == nullptr) || (pdwFlags == nullptr) || ((*pdwFlags & IEIFLAG_ASYNC) && (pdwPriority == nullptr)))
505 return E_INVALIDARG;
506
507 thumbnail_size_ = *prgSize;
508 color_depth_ = dwRecClrDepth;
509
510 *pdwFlags = IEIFLAG_CACHE; // we don't cache the image
511
512 wcsncpy(pszPathBuffer, filename_.c_str(), cchMax);
513
514 return NOERROR;
515 }
516
517 // IPersist methods
518
519 HRESULT STDMETHODCALLTYPE CThumbviewer::GetClassID(CLSID* pClassID)
520 {
521 *pClassID = CLSID_THUMBVIEWER_HANDLER;
522 return S_OK;
523 }
524
525 // IPersistFile methods
526
527 HRESULT STDMETHODCALLTYPE CThumbviewer::Load(LPCOLESTR pszFileName, DWORD)
528 {
529 filename_ = pszFileName;
530 return S_OK;
531 }
532
533 HRESULT STDMETHODCALLTYPE CThumbviewer::IsDirty()
534 { return E_NOTIMPL; }
535
536 HRESULT STDMETHODCALLTYPE CThumbviewer::Save(LPCOLESTR, BOOL)
537 { return E_NOTIMPL; }
538
539 HRESULT STDMETHODCALLTYPE CThumbviewer::SaveCompleted(LPCOLESTR)
540 { return E_NOTIMPL; }
541
542 HRESULT STDMETHODCALLTYPE CThumbviewer::GetCurFile(LPOLESTR __RPC_FAR*)
543 { return E_NOTIMPL; }
544
545 } // namespace
546
547 IExtractImage* CreateThumbviewer() { return new CThumbviewer; }
548
549 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */