2 * @file ClipboardHistory.cpp
4 * @brief Implementation for Clipboard history functions
8 #include "ClipboardHistory.h"
10 #include "Concurrent.h"
13 #if __has_include(<winrt/windows.applicationmodel.datatransfer.h>) && _MSC_VER >= 1930
15 #include <winrt/windows.foundation.collections.h>
16 #include <winrt/windows.applicationmodel.datatransfer.h>
17 #include <winrt/windows.graphics.imaging.h>
18 #include <winrt/windows.storage.streams.h>
20 #if !__has_include(<winrt/windows.applicationmodel.holographic.h>)
21 #pragma comment(lib, "windowsapp.lib") // < Windows SDK 10.0.22000.0
24 using namespace winrt::Windows::ApplicationModel::DataTransfer;
25 using namespace winrt::Windows::Graphics::Imaging;
26 using namespace winrt::Windows::Storage;
32 namespace ClipboardHistory
36 std::shared_ptr<TempFile> CreateTempTextFile(const String& text)
38 std::shared_ptr<TempFile> pTempFile(new TempFile());
39 pTempFile->Create(_T("CLP"), L".txt");
40 String path = pTempFile->GetPath();
42 if (file.OpenCreateUtf8(path))
44 file.WriteString(text);
50 String GetClipboardText()
53 GetFromClipboard(text, nullptr);
58 std::shared_ptr<TempFile> CreateTempBitmapFile(const DataPackageView& dataPackageView)
60 std::shared_ptr<TempFile> pTempFile(new TempFile());
61 pTempFile->Create(_T("CLP"), _T(".png"));
63 auto streamReference = dataPackageView.GetBitmapAsync().get();
64 auto inputStream = streamReference.OpenReadAsync().get();
65 auto decoder = BitmapDecoder::CreateAsync(inputStream).get();
66 auto bitmap = decoder.GetSoftwareBitmapAsync().get();
68 auto outputFile = StorageFile::GetFileFromPathAsync(pTempFile->GetPath()).get();
69 auto outputStream = outputFile.OpenAsync(FileAccessMode::ReadWrite).get();
70 auto encoder = BitmapEncoder::CreateAsync(BitmapEncoder::PngEncoderId(), outputStream).get();
71 encoder.SetSoftwareBitmap(bitmap);
72 encoder.FlushAsync().get();
78 std::vector<Item> GetItems(unsigned num)
80 std::vector<Item> result;
84 auto historyItems = Clipboard::GetHistoryItemsAsync().get();
85 auto items = historyItems.Items();
86 for (unsigned int i = 0; i < num; ++i)
88 result.emplace_back();
89 auto& item = result.back();
94 auto dataPackageView = items.GetAt(i).Content();
95 item.timestamp = winrt::clock::to_time_t(items.GetAt(i).Timestamp());
96 if (dataPackageView.Contains(StandardDataFormats::Text()))
98 item.pTextTempFile = CreateTempTextFile(dataPackageView.GetTextAsync().get().c_str());
100 if (dataPackageView.Contains(StandardDataFormats::Bitmap()))
102 item.pBitmapTempFile = CreateTempBitmapFile(dataPackageView);
104 if (!item.pTextTempFile && !item.pBitmapTempFile)
106 item.pTextTempFile = CreateTempTextFile(_T(""));
109 catch (const winrt::hresult_error& e)
111 item.pTextTempFile = CreateTempTextFile(e.message().c_str());
117 time(&item.timestamp);
118 item.pTextTempFile = CreateTempTextFile(i == 0 ?
120 (!Clipboard::IsHistoryEnabled() ? _("Clipboard history is disabled.\r\nTo enable clipboard history, press Windows logo key + V and then click the Turn on button.") : _T("")));
124 catch (const winrt::hresult_error&)
126 for (unsigned int i = 0; i < num; ++i)
128 result.emplace_back();
129 auto& item = result.back();
131 time(&item.timestamp);
132 item.pTextTempFile = CreateTempTextFile(
133 i == 0 ? GetClipboardText() : _("This system does not support clipboard history."));
137 for (unsigned int i = 0; i < num; ++i)
139 result.emplace_back();
140 auto& item = result.back();
142 time(&item.timestamp);
143 item.pTextTempFile = CreateTempTextFile(
144 i == 0 ? GetClipboardText() : _("The 32-bit version of WinMerge does not support Clipboard Compare"));
151 std::vector<Item> GetItems(unsigned num)
153 auto task = Concurrent::CreateTask([num] {
154 return impl::GetItems(num);