2012-06-10 16:50:54 +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) 2012 - 2016 Symless Ltd.
|
|
|
|
|
* SPDX-FileCopyrightText: (C) 2004 Chris Schoeneman
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2012-06-10 16:50:54 +00:00
|
|
|
*/
|
|
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "platform/OSXClipboardTextConverter.h"
|
|
|
|
|
|
|
|
|
|
#include "base/Unicode.h"
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// OSXClipboardTextConverter
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
OSXClipboardTextConverter::OSXClipboardTextConverter()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
// do nothing
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
OSXClipboardTextConverter::~OSXClipboardTextConverter()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
// do nothing
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
CFStringRef OSXClipboardTextConverter::getOSXFormat() const
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
return CFSTR("public.plain-text");
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-06 00:55:07 +00:00
|
|
|
std::string OSXClipboardTextConverter::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);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
if (stringRef == NULL) {
|
2025-01-06 00:55:07 +00:00
|
|
|
return std::string();
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CFIndex buffSize;
|
|
|
|
|
CFRange entireString = CFRangeMake(0, CFStringGetLength(stringRef));
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
CFStringGetBytes(stringRef, entireString, toEncoding, 0, false, NULL, 0, &buffSize);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
char *buffer = new char[buffSize];
|
|
|
|
|
|
|
|
|
|
if (buffer == NULL) {
|
2016-12-28 11:50:32 +00:00
|
|
|
CFRelease(stringRef);
|
2025-01-06 00:55:07 +00:00
|
|
|
return std::string();
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-11 00:09:43 +00:00
|
|
|
CFStringGetBytes(stringRef, entireString, toEncoding, 0, false, (uint8_t *)buffer, buffSize, NULL);
|
2024-07-02 19:07:06 +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);
|
|
|
|
|
|
|
|
|
|
return result;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-06 00:55:07 +00:00
|
|
|
std::string OSXClipboardTextConverter::doFromIClipboard(const std::string &data) const
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
|
|
|
|
return convertString(data, kCFStringEncodingUTF8, CFStringGetSystemEncoding());
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-06 00:55:07 +00:00
|
|
|
std::string OSXClipboardTextConverter::doToIClipboard(const std::string &data) const
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
|
|
|
|
return convertString(data, CFStringGetSystemEncoding(), kCFStringEncodingUTF8);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|