2002-07-22 02:40:07 +04:00
|
|
|
// ClipboardHandler.cpp
|
|
|
|
|
2005-01-13 03:45:55 +03:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
|
2002-07-22 02:40:07 +04:00
|
|
|
#include <Message.h>
|
2002-10-22 04:08:45 +04:00
|
|
|
#include <RegistrarDefs.h>
|
2002-07-22 02:40:07 +04:00
|
|
|
|
2005-01-13 03:45:55 +03:00
|
|
|
#include "Clipboard.h"
|
2002-07-22 02:40:07 +04:00
|
|
|
#include "ClipboardHandler.h"
|
|
|
|
|
2005-11-13 02:27:14 +03:00
|
|
|
using std::map;
|
|
|
|
using std::string;
|
2005-01-18 04:14:50 +03:00
|
|
|
using namespace BPrivate;
|
|
|
|
|
2002-07-28 17:37:26 +04:00
|
|
|
/*!
|
|
|
|
\class ClipboardHandler
|
|
|
|
\brief Handles all clipboard related requests.
|
|
|
|
*/
|
|
|
|
|
2005-01-13 03:45:55 +03:00
|
|
|
struct ClipboardHandler::ClipboardMap : map<string, Clipboard*> {};
|
|
|
|
|
2002-07-22 02:40:07 +04:00
|
|
|
// constructor
|
2002-07-28 17:37:26 +04:00
|
|
|
/*! \brief Creates and initializes a ClipboardHandler.
|
|
|
|
*/
|
2002-07-22 02:40:07 +04:00
|
|
|
ClipboardHandler::ClipboardHandler()
|
2005-01-13 03:45:55 +03:00
|
|
|
: BHandler(),
|
|
|
|
fClipboards(new ClipboardMap)
|
2002-07-22 02:40:07 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// destructor
|
2002-07-28 17:37:26 +04:00
|
|
|
/*! \brief Frees all resources associate with this object.
|
|
|
|
*/
|
2002-07-22 02:40:07 +04:00
|
|
|
ClipboardHandler::~ClipboardHandler()
|
|
|
|
{
|
2005-01-13 03:45:55 +03:00
|
|
|
for (ClipboardMap::iterator it = fClipboards->begin();
|
|
|
|
it != fClipboards->end();
|
|
|
|
++it) {
|
|
|
|
delete it->second;
|
|
|
|
}
|
2002-07-22 02:40:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// MessageReceived
|
2002-07-28 17:37:26 +04:00
|
|
|
/*! \brief Overrides the super class version to handle the clipboard specific
|
|
|
|
messages.
|
|
|
|
\param message The message to be handled
|
|
|
|
*/
|
2002-07-22 02:40:07 +04:00
|
|
|
void
|
|
|
|
ClipboardHandler::MessageReceived(BMessage *message)
|
|
|
|
{
|
2005-01-13 03:45:55 +03:00
|
|
|
const char *name;
|
2002-10-22 04:08:45 +04:00
|
|
|
BMessage reply;
|
2002-07-22 02:40:07 +04:00
|
|
|
switch (message->what) {
|
2002-11-10 00:19:49 +03:00
|
|
|
case B_REG_ADD_CLIPBOARD:
|
2002-10-22 04:08:45 +04:00
|
|
|
{
|
2005-01-13 03:45:55 +03:00
|
|
|
status_t result = B_BAD_VALUE;
|
|
|
|
|
|
|
|
if (message->FindString("name", &name) == B_OK) {
|
|
|
|
if (_GetClipboard(name))
|
|
|
|
result = B_OK;
|
|
|
|
}
|
|
|
|
|
2002-10-22 04:08:45 +04:00
|
|
|
reply.what = B_REG_RESULT;
|
2005-01-13 03:45:55 +03:00
|
|
|
reply.AddInt32("result", result);
|
2002-10-22 04:08:45 +04:00
|
|
|
message->SendReply(&reply);
|
2005-01-13 03:45:55 +03:00
|
|
|
break;
|
2002-10-22 04:08:45 +04:00
|
|
|
}
|
2005-01-13 03:45:55 +03:00
|
|
|
|
2002-11-10 00:19:49 +03:00
|
|
|
case B_REG_GET_CLIPBOARD_COUNT:
|
2002-10-22 04:08:45 +04:00
|
|
|
{
|
2005-01-13 03:45:55 +03:00
|
|
|
status_t result = B_BAD_VALUE;
|
|
|
|
|
|
|
|
if (message->FindString("name", &name) == B_OK) {
|
|
|
|
if (Clipboard *clipboard = _GetClipboard(name)) {
|
|
|
|
reply.AddInt32("count", clipboard->Count());
|
|
|
|
result = B_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.AddInt32("result", result);
|
2002-10-22 04:08:45 +04:00
|
|
|
reply.what = B_REG_RESULT;
|
|
|
|
message->SendReply(&reply);
|
2005-01-13 03:45:55 +03:00
|
|
|
break;
|
2002-10-22 04:08:45 +04:00
|
|
|
}
|
2005-01-13 03:45:55 +03:00
|
|
|
|
2002-11-10 00:19:49 +03:00
|
|
|
case B_REG_CLIPBOARD_START_WATCHING:
|
2002-10-22 04:08:45 +04:00
|
|
|
{
|
2005-01-13 03:45:55 +03:00
|
|
|
status_t result = B_BAD_VALUE;
|
|
|
|
|
2002-10-22 04:08:45 +04:00
|
|
|
BMessenger target;
|
2005-01-13 03:45:55 +03:00
|
|
|
if (message->FindString("name", &name) == B_OK
|
|
|
|
&& message->FindMessenger("target", &target) == B_OK) {
|
|
|
|
Clipboard *clipboard = _GetClipboard(name);
|
|
|
|
if (clipboard && clipboard->AddWatcher(target))
|
|
|
|
result = B_OK;
|
|
|
|
}
|
|
|
|
|
2002-10-22 04:08:45 +04:00
|
|
|
reply.what = B_REG_RESULT;
|
2005-01-13 03:45:55 +03:00
|
|
|
reply.AddInt32("result", result);
|
2002-10-22 04:08:45 +04:00
|
|
|
message->SendReply(&reply);
|
2005-01-13 03:45:55 +03:00
|
|
|
break;
|
2002-10-22 04:08:45 +04:00
|
|
|
}
|
2005-01-13 03:45:55 +03:00
|
|
|
|
2002-11-10 00:19:49 +03:00
|
|
|
case B_REG_CLIPBOARD_STOP_WATCHING:
|
2002-10-22 04:08:45 +04:00
|
|
|
{
|
2005-01-13 03:45:55 +03:00
|
|
|
status_t result = B_BAD_VALUE;
|
|
|
|
|
2002-10-22 04:08:45 +04:00
|
|
|
BMessenger target;
|
2005-01-13 03:45:55 +03:00
|
|
|
if (message->FindString("name", &name) == B_OK
|
|
|
|
&& message->FindMessenger("target", &target) == B_OK) {
|
|
|
|
Clipboard *clipboard = _GetClipboard(name);
|
|
|
|
if (clipboard && clipboard->RemoveWatcher(target))
|
|
|
|
result = B_OK;
|
|
|
|
}
|
|
|
|
|
2002-10-22 04:08:45 +04:00
|
|
|
reply.what = B_REG_RESULT;
|
2005-01-13 03:45:55 +03:00
|
|
|
reply.AddInt32("result", result);
|
2002-10-22 04:08:45 +04:00
|
|
|
message->SendReply(&reply);
|
2005-01-13 03:45:55 +03:00
|
|
|
break;
|
2002-10-22 04:08:45 +04:00
|
|
|
}
|
2005-01-13 03:45:55 +03:00
|
|
|
|
2002-11-10 00:19:49 +03:00
|
|
|
case B_REG_DOWNLOAD_CLIPBOARD:
|
2002-10-22 04:08:45 +04:00
|
|
|
{
|
2005-01-13 03:45:55 +03:00
|
|
|
status_t result = B_BAD_VALUE;
|
|
|
|
|
|
|
|
if (message->FindString("name", &name) == B_OK) {
|
|
|
|
Clipboard *clipboard = _GetClipboard(name);
|
|
|
|
if (clipboard) {
|
|
|
|
reply.AddMessage("data", clipboard->Data());
|
|
|
|
reply.AddMessenger("data source", clipboard->DataSource());
|
|
|
|
reply.AddInt32("count", clipboard->Count());
|
|
|
|
result = B_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-22 04:08:45 +04:00
|
|
|
reply.what = B_REG_RESULT;
|
2005-01-13 03:45:55 +03:00
|
|
|
reply.AddInt32("result", result);
|
2002-10-22 04:08:45 +04:00
|
|
|
message->SendReply(&reply);
|
2005-01-13 03:45:55 +03:00
|
|
|
break;
|
2002-10-22 04:08:45 +04:00
|
|
|
}
|
2005-01-13 03:45:55 +03:00
|
|
|
|
2002-11-10 00:19:49 +03:00
|
|
|
case B_REG_UPLOAD_CLIPBOARD:
|
2002-10-22 04:08:45 +04:00
|
|
|
{
|
2005-01-13 03:45:55 +03:00
|
|
|
status_t result = B_BAD_VALUE;
|
|
|
|
|
2002-10-22 04:08:45 +04:00
|
|
|
BMessage data;
|
2005-01-13 03:45:55 +03:00
|
|
|
BMessenger source;
|
|
|
|
if (message->FindString("name", &name) == B_OK
|
|
|
|
&& message->FindMessenger("data source", &source) == B_OK
|
|
|
|
&& message->FindMessage("data", &data) == B_OK) {
|
|
|
|
Clipboard *clipboard = _GetClipboard(name);
|
|
|
|
if (clipboard) {
|
|
|
|
clipboard->SetData(&data, source);
|
|
|
|
reply.AddInt32("count", clipboard->Count());
|
|
|
|
result = B_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-22 04:08:45 +04:00
|
|
|
reply.what = B_REG_RESULT;
|
2005-01-13 03:45:55 +03:00
|
|
|
reply.AddInt32("result", result);
|
2002-10-22 04:08:45 +04:00
|
|
|
message->SendReply(&reply);
|
2005-01-13 03:45:55 +03:00
|
|
|
break;
|
2002-10-22 04:08:45 +04:00
|
|
|
}
|
2005-01-13 03:45:55 +03:00
|
|
|
|
2002-07-22 02:40:07 +04:00
|
|
|
default:
|
|
|
|
BHandler::MessageReceived(message);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-13 03:45:55 +03:00
|
|
|
/*! \brief Gets the clipboard with the specified name, or adds it, if not yet
|
|
|
|
existent.
|
|
|
|
|
|
|
|
\param name The name of the clipboard to be returned.
|
|
|
|
\return The clipboard with the respective name.
|
|
|
|
*/
|
|
|
|
Clipboard*
|
|
|
|
ClipboardHandler::_GetClipboard(const char *name)
|
|
|
|
{
|
|
|
|
if (!name)
|
|
|
|
name = "system";
|
2002-10-22 04:08:45 +04:00
|
|
|
|
2005-01-13 03:45:55 +03:00
|
|
|
Clipboard *clipboard = NULL;
|
|
|
|
ClipboardMap::iterator it = fClipboards->find(name);
|
|
|
|
if (it != fClipboards->end()) {
|
|
|
|
clipboard = it->second;
|
|
|
|
} else {
|
|
|
|
clipboard = new Clipboard(name);
|
|
|
|
(*fClipboards)[name] = clipboard;
|
|
|
|
}
|
|
|
|
|
|
|
|
return clipboard;
|
|
|
|
}
|
2002-11-03 02:33:33 +03:00
|
|
|
|