2013-04-30 08:10:25 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include <string>
|
2014-03-16 01:13:06 +00:00
|
|
|
#include <vector>
|
2013-04-30 08:10:25 +00:00
|
|
|
|
2014-04-16 07:31:59 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
2013-04-30 08:10:25 +00:00
|
|
|
#include "ui/base/clipboard/clipboard.h"
|
2013-12-11 07:48:19 +00:00
|
|
|
|
2014-04-17 05:45:14 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
2013-04-30 08:10:25 +00:00
|
|
|
|
2014-04-15 07:50:00 +00:00
|
|
|
namespace {
|
2013-04-30 08:10:25 +00:00
|
|
|
|
2014-04-15 07:50:00 +00:00
|
|
|
bool Has(const std::string& format_string) {
|
2013-04-30 08:10:25 +00:00
|
|
|
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
|
|
|
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
|
2014-04-15 07:50:00 +00:00
|
|
|
return clipboard->IsFormatAvailable(format, ui::Clipboard::BUFFER_STANDARD);
|
2013-04-30 08:10:25 +00:00
|
|
|
}
|
|
|
|
|
2014-04-15 07:50:00 +00:00
|
|
|
std::string Read(const std::string& format_string) {
|
2013-04-30 08:10:25 +00:00
|
|
|
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
|
|
|
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
|
|
|
|
|
|
|
|
std::string data;
|
|
|
|
clipboard->ReadData(format, &data);
|
2014-04-15 07:50:00 +00:00
|
|
|
return data;
|
2013-04-30 08:10:25 +00:00
|
|
|
}
|
|
|
|
|
2014-04-15 07:50:00 +00:00
|
|
|
std::string ReadText() {
|
2013-04-30 08:10:25 +00:00
|
|
|
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
|
|
|
|
|
|
|
std::string data;
|
|
|
|
clipboard->ReadAsciiText(ui::Clipboard::BUFFER_STANDARD, &data);
|
2014-04-15 07:50:00 +00:00
|
|
|
return data;
|
2013-04-30 08:10:25 +00:00
|
|
|
}
|
|
|
|
|
2014-04-15 07:50:00 +00:00
|
|
|
void WriteText(const std::string text) {
|
2013-04-30 08:10:25 +00:00
|
|
|
ui::Clipboard::ObjectMap object_map;
|
|
|
|
object_map[ui::Clipboard::CBF_TEXT].push_back(
|
|
|
|
std::vector<char>(text.begin(), text.end()));
|
|
|
|
|
2013-12-11 07:48:19 +00:00
|
|
|
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
|
|
|
clipboard->WriteObjects(ui::Clipboard::BUFFER_STANDARD, object_map);
|
2013-04-30 08:10:25 +00:00
|
|
|
}
|
|
|
|
|
2014-04-15 07:50:00 +00:00
|
|
|
void Clear() {
|
2013-12-11 07:48:19 +00:00
|
|
|
ui::Clipboard::GetForCurrentThread()->Clear(ui::Clipboard::BUFFER_STANDARD);
|
2013-04-30 08:10:25 +00:00
|
|
|
}
|
|
|
|
|
2014-04-15 07:50:00 +00:00
|
|
|
void Initialize(v8::Handle<v8::Object> exports) {
|
2014-04-16 07:31:59 +00:00
|
|
|
mate::Dictionary dict(v8::Isolate::GetCurrent(), exports);
|
|
|
|
dict.SetMethod("has", &Has);
|
|
|
|
dict.SetMethod("read", &Read);
|
|
|
|
dict.SetMethod("readText", &ReadText);
|
|
|
|
dict.SetMethod("writeText", &WriteText);
|
|
|
|
dict.SetMethod("clear", &Clear);
|
2013-04-30 08:10:25 +00:00
|
|
|
}
|
|
|
|
|
2014-04-15 07:50:00 +00:00
|
|
|
} // namespace
|
2013-04-30 08:10:25 +00:00
|
|
|
|
2014-04-15 07:50:00 +00:00
|
|
|
NODE_MODULE(atom_common_clipboard, Initialize)
|