Skip to content

Commit

Permalink
fix unicode strings copy to clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
X547 committed Mar 26, 2023
1 parent 61872cb commit 1e3eb35
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
47 changes: 46 additions & 1 deletion HaikuDataDeviceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <Clipboard.h>
#include <AutoLocker.h>
#include <AutoDeleter.h>
#include <String.h>

#include <string.h>

Expand All @@ -19,6 +20,48 @@ enum {

//#pragma mark - HaikuDataSource

void HaikuDataSource::ConvertToHaikuMessage(BMessage &dstMsg, const BMessage &srcMsg)
{
char *name;
type_code type;
int32 count;
const void *val;
ssize_t size;
for (int32 i = 0; srcMsg.GetInfo(B_MIME_TYPE, i, &name, &type, &count) == B_OK; i++) {
srcMsg.FindData(name, B_MIME_TYPE, 0, &val, &size);
if (strcmp(name, "text/plain;charset=utf-8") == 0) {
dstMsg.AddData("text/plain", B_MIME_TYPE, val, size);
} else if (strcmp(name, "text/plain") == 0) {
// drop
} else if (strcmp(name, "text/uri-list") == 0) {
BString str((const char*)val, size);
int32 pos = 0;
while (pos < str.Length()) {
int32 nextPos = str.FindFirst("\r\n", pos);
if (nextPos < 0) {
nextPos = str.Length();
}
if (str[pos] != '#') {
if (str.FindFirst("file://", pos) == pos) {
BString path;
str.CopyInto(path, pos + strlen("file://"), nextPos - pos - strlen("file://"));
printf("path: \"%s\"\n", path.String());
BEntry entry(path);
if (entry.InitCheck() >= B_OK) {
entry_ref ref;
entry.GetRef(&ref);
dstMsg.AddRef("refs", &ref);
}
}
}
pos = nextPos + strlen("\r\n");
}

} else {
dstMsg.AddData(name, B_MIME_TYPE, val, size);
}
}
}
HaikuDataSource::~HaikuDataSource()
{
if (fDataDevice != NULL && fDataDevice->fDataSource == this)
Expand Down Expand Up @@ -61,7 +104,9 @@ BMessage *HaikuDataSource::ToMessage()
ReadData(data, mimeType.c_str());
msg->AddData(mimeType.c_str(), B_MIME_TYPE, &data[0], data.size());
}
return msg.Detach();
ObjectDeleter<BMessage> dstMsg(new BMessage(B_SIMPLE_DATA));
ConvertToHaikuMessage(*dstMsg.Get(), *msg.Get());
return dstMsg.Detach();
}

void HaikuDataSource::HandleOffer(const char *mimeType)
Expand Down
3 changes: 3 additions & 0 deletions HaikuDataDeviceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class HaikuDataSource: public WlDataSource {
HaikuDataDevice *fDataDevice{};
std::set<std::string> fMimeTypes;

static void ConvertToHaikuMessage(BMessage &dstMsg, const BMessage &srcMsg);


public:
static HaikuDataSource *FromResource(struct wl_resource *resource) {return (HaikuDataSource*)WlResource::FromResource(resource);}
virtual ~HaikuDataSource();
Expand Down

0 comments on commit 1e3eb35

Please sign in to comment.