xref: /haiku/src/kits/app/Invoker.cpp (revision 7120e97489acbf17d86d3f33e3b2e68974fd4b23)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2001-2002, OpenBeOS
3 //
4 //	Permission is hereby granted, free of charge, to any person obtaining a
5 //	copy of this software and associated documentation files (the "Software"),
6 //	to deal in the Software without restriction, including without limitation
7 //	the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 //	and/or sell copies of the Software, and to permit persons to whom the
9 //	Software is furnished to do so, subject to the following conditions:
10 //
11 //	The above copyright notice and this permission notice shall be included in
12 //	all copies or substantial portions of the Software.
13 //
14 //	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 //	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 //	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 //	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 //	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 //	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 //	DEALINGS IN THE SOFTWARE.
21 //
22 //	File Name:		Invoker.cpp
23 //	Author:			Frans van Nispen (xlr8@tref.nl)
24 //	Description:	BMessageFilter class creates objects that filter
25 //					in-coming BMessages.
26 //------------------------------------------------------------------------------
27 
28 // Standard Includes -----------------------------------------------------------
29 #include <stdio.h>
30 
31 // System Includes -------------------------------------------------------------
32 #include <Application.h>
33 #include <Handler.h>
34 #include <Invoker.h>
35 #include <Message.h>
36 #include <Messenger.h>
37 
38 // Project Includes ------------------------------------------------------------
39 
40 // Local Includes --------------------------------------------------------------
41 
42 // Local Defines ---------------------------------------------------------------
43 
44 // Globals ---------------------------------------------------------------------
45 
46 //------------------------------------------------------------------------------
47 BInvoker::BInvoker()
48 	:	fMessage(NULL),
49 		fMessenger(be_app),
50 		fReplyTo(NULL),
51 		fTimeout(B_INFINITE_TIMEOUT)
52 {
53 }
54 //------------------------------------------------------------------------------
55 BInvoker::BInvoker(BMessage* message, const BHandler* handler,
56 				   const BLooper* looper)
57 	:	fMessage(message),
58 		fMessenger(BMessenger(handler, looper)),
59 		fReplyTo(NULL),
60 		fTimeout(B_INFINITE_TIMEOUT)
61 {
62 }
63 //------------------------------------------------------------------------------
64 BInvoker::BInvoker(BMessage* message, BMessenger target)
65 	:	fMessage(message),
66 		fMessenger(BMessenger(target)),
67 		fReplyTo(NULL),
68 		fTimeout(B_INFINITE_TIMEOUT)
69 {
70 }
71 //------------------------------------------------------------------------------
72 BInvoker::~BInvoker()
73 {
74 	delete fMessage;
75 }
76 //------------------------------------------------------------------------------
77 status_t BInvoker::SetMessage(BMessage* message)
78 {
79 	delete fMessage;
80 	fMessage = message;
81 	return B_OK;
82 }
83 //------------------------------------------------------------------------------
84 BMessage* BInvoker::Message() const
85 {
86 	return fMessage;
87 }
88 //------------------------------------------------------------------------------
89 uint32 BInvoker::Command() const
90 {
91 	if (fMessage)
92 	{
93 		return fMessage->what;
94 	}
95 	else
96 	{
97 		return (uint32)NULL;
98 	}
99 }
100 //------------------------------------------------------------------------------
101 status_t BInvoker::SetTarget(const BHandler* handler, const BLooper* looper)
102 {
103 	fMessenger = BMessenger(handler, looper);
104 	return fMessenger.IsValid();
105 }
106 //------------------------------------------------------------------------------
107 status_t BInvoker::SetTarget(BMessenger messenger)
108 {
109 	fMessenger = messenger;
110 	return fMessenger.IsValid();
111 }
112 //------------------------------------------------------------------------------
113 bool BInvoker::IsTargetLocal() const
114 {
115 	return fMessenger.IsTargetLocal();
116 }
117 //------------------------------------------------------------------------------
118 BHandler* BInvoker::Target(BLooper** looper) const
119 {
120 	return fMessenger.Target(looper);
121 }
122 //------------------------------------------------------------------------------
123 BMessenger BInvoker::Messenger() const
124 {
125 	return fMessenger;
126 }
127 //------------------------------------------------------------------------------
128 status_t BInvoker::SetHandlerForReply(BHandler* handler)
129 {
130 	fReplyTo = handler;
131 	return B_OK;
132 }
133 //------------------------------------------------------------------------------
134 BHandler* BInvoker::HandlerForReply() const
135 {
136 	return fReplyTo;
137 }
138 //------------------------------------------------------------------------------
139 status_t BInvoker::Invoke(BMessage* msg)
140 {
141 	return InvokeNotify( msg );
142 }
143 //------------------------------------------------------------------------------
144 status_t BInvoker::InvokeNotify(BMessage* msg, uint32 kind)
145 {
146 	fNotifyKind = kind;
147 
148 	BMessage clone(kind);
149 	status_t err = B_BAD_VALUE;
150 
151 	if (!msg && fNotifyKind == B_CONTROL_INVOKED)
152 	{
153 		msg = fMessage;
154 	}
155 
156 	if (!msg)
157 	{
158 		if (!Target()->IsWatched())
159 			return err;
160 	}
161 	else
162 	{
163 		clone = *msg;
164 	}
165 
166 	clone.AddInt64("when", system_time());
167 	clone.AddPointer("source", this);
168 
169 	if (msg)
170 	{
171 		err = fMessenger.SendMessage( &clone, fReplyTo, fTimeout);
172 	}
173 
174 	// Also send invocation to any observers of this handler.
175 	Target()->SendNotices(kind, &clone);
176 	return err;
177 }
178 //------------------------------------------------------------------------------
179 status_t BInvoker::SetTimeout(bigtime_t timeout)
180 {
181 	fTimeout = timeout;
182 	return B_OK;
183 }
184 //------------------------------------------------------------------------------
185 bigtime_t BInvoker::Timeout() const
186 {
187 	return fTimeout;
188 }
189 //------------------------------------------------------------------------------
190 uint32 BInvoker::InvokeKind(bool* notify)
191 {
192 	if (fNotifyKind == B_CONTROL_INVOKED)
193 	{
194 		*notify = false;
195 	}
196 	else
197 	{
198 		*notify = true;
199 	}
200 
201 	return fNotifyKind;
202 }
203 //------------------------------------------------------------------------------
204 void BInvoker::BeginInvokeNotify(uint32 kind)
205 {
206 	fNotifyKind = kind;
207 }
208 //------------------------------------------------------------------------------
209 void BInvoker::EndInvokeNotify()
210 {
211 	fNotifyKind = B_CONTROL_INVOKED;
212 }
213 //------------------------------------------------------------------------------
214 void BInvoker::_ReservedInvoker1()
215 {
216 }
217 //------------------------------------------------------------------------------
218 void BInvoker::_ReservedInvoker2()
219 {
220 }
221 //------------------------------------------------------------------------------
222 void BInvoker::_ReservedInvoker3()
223 {
224 }
225 //------------------------------------------------------------------------------
226 BInvoker::BInvoker(const BInvoker &)
227 {
228 	// No copy construction allowed!
229 }
230 //------------------------------------------------------------------------------
231 BInvoker& BInvoker::operator=(const BInvoker&)
232 {
233 	// No assignment allowed!
234 	return *this;
235 }
236 //------------------------------------------------------------------------------
237 
238 /*
239  * $Log $
240  *
241  * $Id  $
242  *
243  */
244 
245