Replace ClipboardTree by Clipboard, a class just representing a clipboard.

The clipboards are managed in a simple string->Clipboard map.


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@10703 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold
2005-01-13 00:45:55 +00:00
parent 697ba202b7
commit 58b5124e46
7 changed files with 268 additions and 265 deletions
+111
View File
@@ -0,0 +1,111 @@
// Clipboard.cpp
#include <app/Clipboard.h>
#include "Clipboard.h"
/*!
\class Clipboard
\brief Server-side representation of a clipboard.
*/
// constructor
/*! \brief Creates and initializes a Clipboard.
\param name The name of the clipboard.
*/
Clipboard::Clipboard(const char *name)
: fName(name),
fData(B_SIMPLE_DATA),
fDataSource(),
fCount(0),
fWatchingService()
{
}
// destructor
/*! \brief Frees all resources associate with this object.
*/
Clipboard::~Clipboard()
{
}
// SetData
/*! \brief Sets the clipboard's data.
Also notifies all watchers that the clipboard data have changed.
\param data The new clipboard data.
\param dataSource The clipboards new data source.
*/
void
Clipboard::SetData(const BMessage *data, BMessenger dataSource)
{
fData = *data;
fDataSource = dataSource;
fCount++;
NotifyWatchers();
}
// Data
/*! \brief Returns the clipboard's data.
\return The clipboard's data.
*/
const BMessage *
Clipboard::Data() const
{
return &fData;
}
// DataSource
/*! \brief Returns the clipboard's data source.
\return The clipboard's data source.
*/
BMessenger
Clipboard::DataSource() const
{
return fDataSource;
}
// Count
int32
Clipboard::Count() const
{
return fCount;
}
// AddWatcher
/*! \brief Adds a new watcher for this clipboard.
\param watcher The messenger referring to the new watcher.
\return \c true, if the watcher could be added successfully,
\c false otherwise.
*/
bool
Clipboard::AddWatcher(BMessenger watcher)
{
return fWatchingService.AddWatcher(watcher);
}
// RemoveWatcher
/*! \brief Removes a watcher from this clipboard.
\param watcher The watcher to be removed.
\return \c true, if the supplied watcher was watching the clipboard,
\c false otherwise.
*/
bool
Clipboard::RemoveWatcher(BMessenger watcher)
{
return fWatchingService.RemoveWatcher(watcher);
}
// NotifyWatchers
/*! \brief Sends a notification message that the clipboard data have changed
to all associated watchers.
*/
void
Clipboard::NotifyWatchers()
{
BMessage message(B_CLIPBOARD_CHANGED);
fWatchingService.NotifyWatchers(&message, NULL);
}
+34
View File
@@ -0,0 +1,34 @@
// Clipboard.h
#ifndef CLIPBOARD_H
#define CLIPBOARD_H
#include <String.h>
#include <Message.h>
#include <Messenger.h>
#include "WatchingService.h"
class Clipboard {
public:
Clipboard(const char *name);
~Clipboard();
void SetData(const BMessage *data, BMessenger dataSource);
const BMessage *Data() const;
BMessenger DataSource() const;
int32 Count() const;
bool AddWatcher(BMessenger watcher);
bool RemoveWatcher(BMessenger watcher);
void NotifyWatchers();
private:
BString fName;
BMessage fData;
BMessenger fDataSource;
int32 fCount;
WatchingService fWatchingService;
};
#endif // CLIPBOARD_H
+114 -105
View File
@@ -1,27 +1,27 @@
// ClipboardHandler.cpp
#include <map>
#include <string>
#include <Message.h>
#include <RegistrarDefs.h>
#include "Clipboard.h"
#include "ClipboardHandler.h"
// bonefish: TODO: Cleanup/reimplement! The ClipboardTree doesn't need to be a
// tree at all. Strip it off fLeftChild, fRightChild, and fCount and we have a
// server-side clipboard representation. Simply use a name->clipboard map to
// manage the clipboards. The clipboard count (which doesn't work at the
// moment) will be the size of the map.
/*!
\class ClipboardHandler
\brief Handles all clipboard related requests.
*/
struct ClipboardHandler::ClipboardMap : map<string, Clipboard*> {};
// constructor
/*! \brief Creates and initializes a ClipboardHandler.
*/
ClipboardHandler::ClipboardHandler()
: BHandler()
: BHandler(),
fClipboards(new ClipboardMap)
{
}
@@ -30,6 +30,11 @@ ClipboardHandler::ClipboardHandler()
*/
ClipboardHandler::~ClipboardHandler()
{
for (ClipboardMap::iterator it = fClipboards->begin();
it != fClipboards->end();
++it) {
delete it->second;
}
}
// MessageReceived
@@ -40,143 +45,147 @@ ClipboardHandler::~ClipboardHandler()
void
ClipboardHandler::MessageReceived(BMessage *message)
{
BString name;
const char *name;
BMessage reply;
switch (message->what) {
case B_REG_ADD_CLIPBOARD:
{
if ( message->FindString("name",&name) != B_OK )
{
reply.AddInt32("result",B_BAD_VALUE);
}
else
{
fClipboardTree.AddNode(name);
reply.AddInt32("result",B_OK);
}
status_t result = B_BAD_VALUE;
if (message->FindString("name", &name) == B_OK) {
if (_GetClipboard(name))
result = B_OK;
}
reply.what = B_REG_RESULT;
reply.AddInt32("result", result);
message->SendReply(&reply);
break;
}
break;
case B_REG_GET_CLIPBOARD_COUNT:
{
if ( message->FindString("name",&name) != B_OK )
{
reply.AddInt32("result",B_BAD_VALUE);
}
else
{
ClipboardTree *node = fClipboardTree.GetNode(name);
if ( node )
{
reply.AddInt32("count",(uint32)(node->GetCount()));
reply.AddInt32("result",B_OK);
}
else
reply.AddInt32("result",B_BAD_VALUE);
}
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);
reply.what = B_REG_RESULT;
message->SendReply(&reply);
break;
}
break;
case B_REG_CLIPBOARD_START_WATCHING:
{
status_t result = B_BAD_VALUE;
BMessenger target;
if ( (message->FindString("name",&name) != B_OK) ||
(message->FindMessenger("target",&target) != B_OK) )
{
reply.AddInt32("result",B_BAD_VALUE);
}
else
{
ClipboardTree *node = fClipboardTree.GetNode(name);
if ( node && node->AddWatcher(&target) )
reply.AddInt32("result",B_OK);
else
reply.AddInt32("result",B_BAD_VALUE);
}
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;
}
reply.what = B_REG_RESULT;
reply.AddInt32("result", result);
message->SendReply(&reply);
break;
}
break;
case B_REG_CLIPBOARD_STOP_WATCHING:
{
status_t result = B_BAD_VALUE;
BMessenger target;
if ( (message->FindString("name",&name) != B_OK) ||
(message->FindMessenger("target",&target) != B_OK) )
{
reply.AddInt32("result",B_BAD_VALUE);
}
else
{
ClipboardTree *node = fClipboardTree.GetNode(name);
if ( node && node->RemoveWatcher(&target) )
reply.AddInt32("result",B_OK);
else
reply.AddInt32("result",B_BAD_VALUE);
}
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;
}
reply.what = B_REG_RESULT;
reply.AddInt32("result", result);
message->SendReply(&reply);
break;
}
break;
case B_REG_DOWNLOAD_CLIPBOARD:
{
if ( message->FindString("name",&name) != B_OK )
{
reply.AddInt32("result",B_BAD_VALUE);
}
else
{
ClipboardTree *node = fClipboardTree.GetNode(name);
if ( node )
{
reply.AddMessage("data",node->GetData());
reply.AddMessenger("data source",*node->GetDataSource());
reply.AddInt32("count",(uint32)(node->GetCount()));
reply.AddInt32("result",B_OK);
}
else
reply.AddInt32("result",B_BAD_VALUE);
}
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;
}
}
reply.what = B_REG_RESULT;
reply.AddInt32("result", result);
message->SendReply(&reply);
break;
}
break;
case B_REG_UPLOAD_CLIPBOARD:
{
status_t result = B_BAD_VALUE;
BMessage data;
BMessenger dataSource;
ClipboardTree *node = NULL;
if ( (message->FindString("name",&name) != B_OK) ||
(message->FindMessage("data",&data) != B_OK) ||
(message->FindMessenger("data source",&dataSource) != B_OK) )
{
reply.AddInt32("result",B_BAD_VALUE);
}
else
{
node = fClipboardTree.GetNode(name);
if ( node )
{
node->SetData(&data);
node->SetDataSource(&dataSource);
reply.AddInt32("count",(uint32)(node->IncrementCount()));
reply.AddInt32("result",B_OK);
}
else
reply.AddInt32("result",B_BAD_VALUE);
}
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;
}
}
reply.what = B_REG_RESULT;
reply.AddInt32("result", result);
message->SendReply(&reply);
if ( node )
node->NotifyWatchers();
break;
}
break;
default:
BHandler::MessageReceived(message);
break;
}
}
/*! \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";
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;
}
+8 -2
View File
@@ -5,7 +5,8 @@
#include <Handler.h>
#include <Message.h>
#include "ClipboardTree.h"
class Clipboard;
class ClipboardHandler : public BHandler {
public:
@@ -13,8 +14,13 @@ public:
virtual ~ClipboardHandler();
virtual void MessageReceived(BMessage *message);
private:
ClipboardTree fClipboardTree;
Clipboard *_GetClipboard(const char *name);
struct ClipboardMap;
ClipboardMap *fClipboards;
};
#endif // CLIPBOARD_HANDLER_H
-120
View File
@@ -1,120 +0,0 @@
// ClipboardTree.cpp
#include <Message.h>
#include <Clipboard.h>
#include "ClipboardTree.h"
/*!
\class ClipboardTree
\brief Implements a tree containing clipboards and their data
*/
// constructor
/*! \brief Creates and initializes a ClipboardTree.
*/
ClipboardTree::ClipboardTree()
{
fName = "";
fCount = 0;
fLeftChild = NULL;
fRightChild = NULL;
}
// destructor
/*! \brief Frees all resources associate with this object.
*/
ClipboardTree::~ClipboardTree()
{
if ( fLeftChild )
delete fLeftChild;
if ( fRightChild )
delete fRightChild;
}
void ClipboardTree::AddNode(BString name)
{
if ( fName == "" )
{
fName = name;
return;
}
if ( fName == name )
return;
if ( name < fName )
{
if ( !fLeftChild )
fLeftChild = new ClipboardTree;
fLeftChild->AddNode(name);
return;
}
if ( !fRightChild )
fRightChild = new ClipboardTree;
fRightChild->AddNode(name);
}
ClipboardTree* ClipboardTree::GetNode(BString name)
{
if ( fName == "" )
return NULL;
if ( fName == name )
return this;
if ( name < fName )
{
if ( !fLeftChild )
return NULL;
return fLeftChild->GetNode(name);
}
if ( !fRightChild )
return NULL;
return fRightChild->GetNode(name);
}
uint32 ClipboardTree::GetCount()
{
return fCount;
}
uint32 ClipboardTree::IncrementCount()
{
fCount++;
return fCount;
}
BMessage* ClipboardTree::GetData()
{
return &fData;
}
void ClipboardTree::SetData(BMessage *data)
{
fData = *data;
}
BMessenger* ClipboardTree::GetDataSource()
{
return &fDataSource;
}
void ClipboardTree::SetDataSource(BMessenger *dataSource)
{
fDataSource = *dataSource;
}
bool ClipboardTree::AddWatcher(BMessenger *watcher)
{
return fWatchingService.AddWatcher(*watcher);
}
bool ClipboardTree::RemoveWatcher(BMessenger *watcher)
{
return fWatchingService.RemoveWatcher(*watcher,false);
}
void ClipboardTree::NotifyWatchers()
{
BMessage message(B_CLIPBOARD_CHANGED);
fWatchingService.NotifyWatchers(&message,NULL);
}
-37
View File
@@ -1,37 +0,0 @@
// ClipboardTree.h
#ifndef CLIPBOARD_TREE_H
#define CLIPBOARD_TREE_H
#include <String.h>
#include <Message.h>
#include <Messenger.h>
#include "WatchingService.h"
class ClipboardTree {
public:
ClipboardTree();
~ClipboardTree();
void AddNode(BString name);
ClipboardTree* GetNode(BString name);
uint32 GetCount();
uint32 IncrementCount();
BMessage *GetData();
void SetData(BMessage *data);
BMessenger *GetDataSource();
void SetDataSource(BMessenger *dataSource);
bool AddWatcher(BMessenger *watcher);
bool RemoveWatcher(BMessenger *watcher);
void NotifyWatchers();
private:
BString fName;
BMessage fData;
BMessenger fDataSource;
ClipboardTree *fLeftChild;
ClipboardTree *fRightChild;
uint32 fCount;
WatchingService fWatchingService;
};
#endif // CLIPBOARD_TREE_H
+1 -1
View File
@@ -8,8 +8,8 @@ AddResources obos_registrar : registrar.rdef ;
local registrar_sources =
AppInfoList.cpp
Clipboard.cpp
ClipboardHandler.cpp
ClipboardTree.cpp
Event.cpp
EventMaskWatcher.cpp
EventQueue.cpp