2 * Copyright 2001-2009, Haiku Inc.
3 * Distributed under the terms of the MIT License.
6 * Gabe Yoder (gyoder@stny.rr.com)
10 #include <Clipboard.h>
12 #include <Application.h>
13 #include <RegistrarDefs.h>
14 #include <RosterPrivate.h>
20 #ifdef RUN_WITHOUT_REGISTRAR
21 static BClipboard
sClipboard(NULL
);
22 BClipboard
*be_clipboard
= &sClipboard
;
24 BClipboard
*be_clipboard
= NULL
;
28 using namespace BPrivate
;
31 BClipboard::BClipboard(const char *name
, bool transient
)
38 fName
= strdup("system");
40 fData
= new BMessage();
43 BMessage
message(B_REG_GET_CLIPBOARD_MESSENGER
), reply
;
44 if (BRoster::Private().SendTo(&message
, &reply
, false) == B_OK
45 && reply
.what
== B_REG_SUCCESS
46 && reply
.FindMessenger("messenger", &fClipHandler
) == B_OK
) {
47 BMessage
handlerMessage(B_REG_ADD_CLIPBOARD
), handlerReply
;
49 if (handlerMessage
.AddString("name", fName
) == B_OK
50 && fClipHandler
.SendMessage(&handlerMessage
, &handlerReply
) == B_OK
)
51 handlerReply
.FindInt32("result", &result
);
56 BClipboard::~BClipboard()
64 BClipboard::Name() const
66 return (const char *)fName
;
70 /*! \brief Returns the (locally cached) number of commits to the clipboard.
72 The returned value is the number of successful Commit() invocations for
73 the clipboard represented by this object, either invoked on this object
74 or another (even from another application). This method returns a locally
75 cached value, which might already be obsolete. For an up-to-date value
76 SystemCount() can be invoked.
78 \return The number of commits to the clipboard.
81 BClipboard::LocalCount() const
87 /*! \brief Returns the number of commits to the clipboard.
89 The returned value is the number of successful Commit() invocations for
90 the clipboard represented by this object, either invoked on this object
91 or another (even from another application). This method retrieves the
92 value directly from the system service managing the clipboards, so it is
93 more expensive, but more up-to-date than LocalCount(), which returns a
96 \return The number of commits to the clipboard.
99 BClipboard::SystemCount() const
102 BMessage
message(B_REG_GET_CLIPBOARD_COUNT
), reply
;
103 if (message
.AddString("name", fName
) == B_OK
104 && fClipHandler
.SendMessage(&message
, &reply
) == B_OK
105 && reply
.FindInt32("count", &value
) == B_OK
)
106 return (uint32
)value
;
113 BClipboard::StartWatching(BMessenger target
)
115 if (!target
.IsValid())
118 BMessage
message(B_REG_CLIPBOARD_START_WATCHING
), reply
;
119 if (message
.AddString("name", fName
) == B_OK
120 && message
.AddMessenger("target", target
) == B_OK
121 && fClipHandler
.SendMessage(&message
, &reply
) == B_OK
) {
123 reply
.FindInt32("result", &result
);
131 BClipboard::StopWatching(BMessenger target
)
133 if (!target
.IsValid())
136 BMessage
message(B_REG_CLIPBOARD_STOP_WATCHING
), reply
;
137 if (message
.AddString("name", fName
) == B_OK
138 && message
.AddMessenger("target", target
) == B_OK
139 && fClipHandler
.SendMessage(&message
, &reply
) == B_OK
) {
141 if (reply
.FindInt32("result", &result
) == B_OK
)
151 // Will this work correctly if clipboard is deleted while still waiting on
153 bool locked
= fLock
.Lock();
155 #ifndef RUN_WITHOUT_REGISTRAR
156 if (locked
&& _DownloadFromSystem() != B_OK
) {
174 BClipboard::IsLocked() const
176 return fLock
.IsLocked();
183 if (!_AssertLocked())
184 return B_NOT_ALLOWED
;
186 return fData
->MakeEmpty();
193 return Commit(false);
198 BClipboard::Commit(bool failIfChanged
)
200 if (!_AssertLocked())
201 return B_NOT_ALLOWED
;
203 status_t status
= B_ERROR
;
204 BMessage
message(B_REG_UPLOAD_CLIPBOARD
), reply
;
205 if (message
.AddString("name", fName
) == B_OK
206 && message
.AddMessage("data", fData
) == B_OK
207 && message
.AddMessenger("data source", be_app_messenger
) == B_OK
208 && message
.AddInt32("count", fCount
) == B_OK
209 && message
.AddBool("fail if changed", failIfChanged
) == B_OK
)
210 status
= fClipHandler
.SendMessage(&message
, &reply
);
212 if (status
== B_OK
) {
214 if (reply
.FindInt32("count", &count
) == B_OK
)
225 if (!_AssertLocked())
226 return B_NOT_ALLOWED
;
228 status_t status
= fData
->MakeEmpty();
230 status
= _DownloadFromSystem();
237 BClipboard::DataSource() const
244 BClipboard::Data() const
246 if (!_AssertLocked())
253 // #pragma mark - Private methods
256 BClipboard::BClipboard(const BClipboard
&)
258 // This is private, and I don't use it, so I'm not going to implement it
262 BClipboard
& BClipboard::operator=(const BClipboard
&)
264 // This is private, and I don't use it, so I'm not going to implement it
269 void BClipboard::_ReservedClipboard1() {}
270 void BClipboard::_ReservedClipboard2() {}
271 void BClipboard::_ReservedClipboard3() {}
275 BClipboard::_AssertLocked() const
277 // This function is for jumping to the debugger if not locked
278 if (!fLock
.IsLocked()) {
279 debugger("The clipboard must be locked before proceeding.");
287 BClipboard::_DownloadFromSystem(bool force
)
289 // Apparently, the force paramater was used in some sort of
290 // optimization in R5. Currently, we ignore it.
291 BMessage
message(B_REG_DOWNLOAD_CLIPBOARD
), reply
;
292 if (message
.AddString("name", fName
) == B_OK
293 && fClipHandler
.SendMessage(&message
, &reply
) == B_OK
294 && reply
.FindMessage("data", fData
) == B_OK
295 && reply
.FindMessenger("data source", &fDataSource
) == B_OK
296 && reply
.FindInt32("count", (int32
*)&fCount
) == B_OK
)