PowerToys/src/modules/previewpane/PdfThumbnailProviderCpp/PdfThumbnailProvider.cpp

182 lines
4.9 KiB
C++
Raw Normal View History

Self-contained .NET (#22217) * dotnet sc * MD preview - C# app - working self-contained * Gcode preview - C# app * DevFiles preview - C# app * Fix passing path with spaces as cmd arg and monacocpp proj file * Pdf preview - C# app * Svg preview - C# app * Fix comment * Gcode thumbnail - C# app TODO: - installer - why IThumbnailProvider and IIntializeWithFile doesn't work? * Pdf thumbnail - C# app TODO: - installer - why IThumbnailProvider and IIntializeWithFile doesn't work? * Pdf thumbnail - C# app TODO: - installer - why IThumbnailProvider and IIntializeWithFile doesn't work? * Fix GcodeThumbnailProviderCpp.vcxproj * Svg thumbnail - C# app TODO: - installer - why IThumbnailProvider and IIntializeWithFile doesn't work? * Fix Svg tests * Thumbnail providers - installer * Self-contained Hosts and FileLocksmith * Fix hardcoded <RuntimeIdentifier> * Remove unneeded files * Try to fix Nuget in PR CI * Prefix new dlls with PowerToys. Sign new dlls and exes * Add new .exe files to ProcessList * ci: debug by listing all env vars * ci: try setting variable in the right ci file * Bring back hardcoded RuntimeIdentifier * ci: Add comment and remove debug action * Remove unneeded lib * [WIP] Platform conditional dotnet files & hardlinks * Cleanup * Update expect.txt * Test fix - ARM installer * Fix uninstall bug * Update docs * Fix failing test * Add dll details * Minor cleanup * Improve resizing * Add some logs * Test fix - release build * Remove InvokeOnControlThread * Test fix: logger initialization * Fix arm64 installer Co-authored-by: Jaime Bernardo <jaime@janeasystems.com> Co-authored-by: Dustin L. Howett <dustin@howett.net>
2022-12-14 20:37:23 +08:00
#include "pch.h"
#include "PdfThumbnailProvider.h"
#include <filesystem>
#include <fstream>
#include <shellapi.h>
#include <Shlwapi.h>
#include <string>
#include <wil/com.h>
#include <common/interop/shared_constants.h>
#include <common/logger/logger.h>
#include <common/SettingsAPI/settings_helpers.h>
#include <common/utils/process_path.h>
extern HINSTANCE g_hInst;
extern long g_cDllRef;
PdfThumbnailProvider::PdfThumbnailProvider() :
m_cRef(1), m_pStream(NULL), m_process(NULL)
{
std::filesystem::path logFilePath(PTSettingsHelper::get_local_low_folder_location());
logFilePath.append(LogSettings::pdfThumbLogPath);
Logger::init(LogSettings::pdfThumbLoggerName, logFilePath.wstring(), PTSettingsHelper::get_log_settings_file_location());
InterlockedIncrement(&g_cDllRef);
}
PdfThumbnailProvider::~PdfThumbnailProvider()
{
InterlockedDecrement(&g_cDllRef);
}
#pragma region IUnknown
IFACEMETHODIMP PdfThumbnailProvider::QueryInterface(REFIID riid, void** ppv)
{
static const QITAB qit[] = {
QITABENT(PdfThumbnailProvider, IThumbnailProvider),
QITABENT(PdfThumbnailProvider, IInitializeWithStream),
{ 0 },
};
return QISearch(this, qit, riid, ppv);
}
IFACEMETHODIMP_(ULONG)
PdfThumbnailProvider::AddRef()
{
return InterlockedIncrement(&m_cRef);
}
IFACEMETHODIMP_(ULONG)
PdfThumbnailProvider::Release()
{
ULONG cRef = InterlockedDecrement(&m_cRef);
if (0 == cRef)
{
delete this;
}
return cRef;
}
#pragma endregion
#pragma region IInitializationWithStream
IFACEMETHODIMP PdfThumbnailProvider::Initialize(IStream* pStream, DWORD grfMode)
{
HRESULT hr = E_INVALIDARG;
if (pStream)
{
// Initialize can be called more than once, so release existing valid
// m_pStream.
if (m_pStream)
{
m_pStream->Release();
m_pStream = NULL;
}
m_pStream = pStream;
m_pStream->AddRef();
hr = S_OK;
}
return hr;
}
#pragma endregion
#pragma region IThumbnailProvider
IFACEMETHODIMP PdfThumbnailProvider::GetThumbnail(UINT cx, HBITMAP* phbmp, WTS_ALPHATYPE* pdwAlpha)
{
// Read stream into the buffer
char buffer[4096];
ULONG cbRead;
Logger::trace(L"Begin");
GUID guid;
if (CoCreateGuid(&guid) == S_OK)
{
wil::unique_cotaskmem_string guidString;
if (SUCCEEDED(StringFromCLSID(guid, &guidString)))
{
Logger::info(L"Read stream and save to tmp file.");
// {CLSID} -> CLSID
std::wstring guid = std::wstring(guidString.get()).substr(1, std::wstring(guidString.get()).size() - 2);
std::wstring filePath = PTSettingsHelper::get_local_low_folder_location() + L"\\PdfThumbnail-Temp\\";
if (!std::filesystem::exists(filePath))
{
std::filesystem::create_directories(filePath);
}
std::wstring fileName = filePath + guid + L".pdf";
// Write data to tmp file
std::fstream file;
file.open(fileName, std::ios_base::out | std::ios_base::binary);
if (!file.is_open())
{
return 0;
}
while (true)
{
auto result = m_pStream->Read(buffer, 4096, &cbRead);
file.write(buffer, cbRead);
if (result == S_FALSE)
{
break;
}
}
file.close();
try
{
Logger::info(L"Start PdfThumbnailProvider.exe");
STARTUPINFO info = { sizeof(info) };
std::wstring cmdLine{ L"\"" + fileName + L"\"" };
cmdLine += L" ";
cmdLine += std::to_wstring(cx);
std::wstring appPath = get_module_folderpath(g_hInst) + L"\\PowerToys.PdfThumbnailProvider.exe";
SHELLEXECUTEINFO sei{ sizeof(sei) };
sei.fMask = { SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI };
sei.lpFile = appPath.c_str();
sei.lpParameters = cmdLine.c_str();
sei.nShow = SW_SHOWDEFAULT;
ShellExecuteEx(&sei);
m_process = sei.hProcess;
WaitForSingleObject(m_process, INFINITE);
std::filesystem::remove(fileName);
std::wstring fileNameBmp = filePath + guid + L".bmp";
*phbmp = (HBITMAP)LoadImage(NULL, fileNameBmp.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
*pdwAlpha = WTS_ALPHATYPE::WTSAT_ARGB;
std::filesystem::remove(fileNameBmp);
}
catch (std::exception& e)
{
std::wstring errorMessage = std::wstring{ winrt::to_hstring(e.what()) };
Logger::error(L"Failed to start PdfThumbnailProvider.exe. Error: {}", errorMessage);
}
}
}
return S_OK;
}
#pragma endregion
#pragma region Helper Functions
#pragma endregion