Files
hrynco-kswitcher/src/storage.h
T

77 lines
3.0 KiB
C++

#pragma once
#include "platform.h"
#include <mutex>
#include <functional>
namespace ks
{
struct Pixels;
struct HistoryImageWorkers;
Settings loadSettings(const std::filesystem::path &, const std::vector<Layout> &);
Settings decodeSettings(const Bytes &, const std::vector<Layout> &);
Bytes encodeSettings(const Settings &);
void saveSettings(const std::filesystem::path &, const Settings &);
void validateSettings(const Settings &);
struct Entry
{
std::wstring id, preview, app;
uint64_t bytes{}, at{};
bool pinned{}, image{}, files{};
int ocrState{}; // 0: legacy/not applicable, 1: pending, 2: saved, 3: failed
std::wstring ocrProfile = L"windows";
std::string contentHash, imageHash, pixelHash;
std::string computer;
std::wstring computerName;
};
class History
{
std::filesystem::path folder_;
std::vector<Entry> entries_;
mutable std::mutex mutex_;
std::unique_ptr<HistoryImageWorkers> imageWorkers_;
std::mutex indexWriter_; // Serializes resident mutations with the image capture helper.
void saveIndex();
void loadIndex();
void prune(const Settings &);
public:
explicit History(const std::filesystem::path &);
~History();
void reload();
std::vector<Entry> entries() const;
Clip load(const std::wstring &id) const;
void add(const Clip &, const std::wstring &app, const Settings &, const std::string &computer = {}, const std::wstring &computerName = {});
bool captureImage(DWORD sequence, const std::wstring &app, const Settings &);
std::wstring recognizeImage(const std::wstring &id, const Settings &,
const std::function<bool()> &cancelled = {}) const;
Pixels thumbnail(const std::wstring &id, const std::function<bool()> &cancelled = {}) const;
bool saveOcr(const std::wstring &id, const std::optional<std::wstring> &, const Settings &);
std::optional<std::wstring> ocrText(const std::wstring &id, const std::wstring &profile = L"") const;
void remove(const std::wstring &);
void pin(const std::wstring &);
void clear();
std::vector<Entry> search(const std::wstring &, bool pinnedOnly = false) const;
};
Bytes encodeClip(const Clip &);
Bytes selectionClipboardIdentity(const Clip &);
Clip decodeClip(const Bytes &);
bool sameClipboardImage(const Clip &, const Clip &);
int runImageHistoryCapture(const std::filesystem::path &request);
int runHistoryOcr(const std::filesystem::path &request);
int runHistoryImageWorker(HANDLE mapping, HANDLE requestReady, HANDLE responseReady);
// Image backups are held in memory by a helper for the lifetime of the transaction.
class ClipboardSnapshot
{
struct Helper;
Clip clip_;
std::unique_ptr<Helper> helper_;
public:
ClipboardSnapshot();
ClipboardSnapshot(const ClipboardSnapshot &) = delete;
ClipboardSnapshot &operator=(const ClipboardSnapshot &) = delete;
~ClipboardSnapshot();
DWORD capture(HWND, DWORD sequence);
DWORD restore(HWND, DWORD sequence);
};
int runClipboardSnapshot(HANDLE mapping, HANDLE requestReady, HANDLE responseReady, HANDLE parent);
} // namespace ks