2014-05-13 12:31:05 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2014 - 2016 Symless Ltd
|
|
|
|
|
* SPDX-FileCopyrightText: (C) 2014 Ryan Chapman
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2014-05-13 12:31:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "platform/OSXClipboardHTMLConverter.h"
|
|
|
|
|
|
|
|
|
|
#include "base/Unicode.h"
|
|
|
|
|
|
2025-07-30 12:19:15 +00:00
|
|
|
IClipboard::Format OSXClipboardHTMLConverter::getFormat() const
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-07-30 12:19:15 +00:00
|
|
|
return IClipboard::Format::HTML;
|
2014-05-13 12:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
CFStringRef OSXClipboardHTMLConverter::getOSXFormat() const
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
return CFSTR("public.html");
|
2014-05-13 12:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-06 00:55:07 +00:00
|
|
|
std::string OSXClipboardHTMLConverter::convertString(
|
|
|
|
|
const std::string &data, CFStringEncoding fromEncoding, CFStringEncoding toEncoding
|
|
|
|
|
)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
|
|
|
|
CFStringRef stringRef = CFStringCreateWithCString(kCFAllocatorDefault, data.c_str(), fromEncoding);
|
2014-05-13 12:31:05 +00:00
|
|
|
|
2025-04-27 19:39:23 +00:00
|
|
|
if (stringRef == nullptr) {
|
2025-01-06 00:55:07 +00:00
|
|
|
return std::string();
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2014-05-13 12:31:05 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
CFIndex buffSize;
|
|
|
|
|
CFRange entireString = CFRangeMake(0, CFStringGetLength(stringRef));
|
2014-05-13 12:31:05 +00:00
|
|
|
|
2025-04-27 19:39:23 +00:00
|
|
|
CFStringGetBytes(stringRef, entireString, toEncoding, 0, false, nullptr, 0, &buffSize);
|
2014-05-13 12:31:05 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
char *buffer = new char[buffSize];
|
2014-05-13 12:31:05 +00:00
|
|
|
|
2025-04-27 19:39:23 +00:00
|
|
|
if (buffer == nullptr) {
|
2024-07-02 19:07:06 +00:00
|
|
|
CFRelease(stringRef);
|
2025-01-06 00:55:07 +00:00
|
|
|
return std::string();
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2014-05-13 12:31:05 +00:00
|
|
|
|
2025-04-27 19:39:23 +00:00
|
|
|
CFStringGetBytes(stringRef, entireString, toEncoding, 0, false, (uint8_t *)buffer, buffSize, nullptr);
|
2014-05-13 12:31:05 +00:00
|
|
|
|
2025-01-06 00:55:07 +00:00
|
|
|
std::string result(buffer, buffSize);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
delete[] buffer;
|
|
|
|
|
CFRelease(stringRef);
|
2014-05-13 12:31:05 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
return result;
|
2014-05-13 12:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-06 00:55:07 +00:00
|
|
|
std::string OSXClipboardHTMLConverter::doFromIClipboard(const std::string &data) const
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
return data;
|
2014-05-13 12:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-06 00:55:07 +00:00
|
|
|
std::string OSXClipboardHTMLConverter::doToIClipboard(const std::string &data) const
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
if (Unicode::isUTF8(data)) {
|
|
|
|
|
return data;
|
|
|
|
|
} else {
|
2024-10-17 18:04:35 +00:00
|
|
|
return convertString(data, CFStringGetSystemEncoding(), kCFStringEncodingUTF8);
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2014-05-13 12:31:05 +00:00
|
|
|
}
|