BPicture: Fix archive constructor.
[haiku.git] / src / kits / app / MessageRunner.cpp
blob4a982a47bbd1fcab2e7806dd7232171822b706ca
1 /*
2 * Copyright 2001-2010 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Ingo Weinhold, ingo_weinhold@gmx.de
7 */
10 #include <MessageRunner.h>
12 #include <Application.h>
13 #include <AppMisc.h>
14 #include <RegistrarDefs.h>
15 #include <Roster.h>
16 #include <RosterPrivate.h>
19 using namespace BPrivate;
22 BMessageRunner::BMessageRunner(BMessenger target, const BMessage* message,
23 bigtime_t interval, int32 count)
25 fToken(-1)
27 _InitData(target, message, interval, count, be_app_messenger);
31 BMessageRunner::BMessageRunner(BMessenger target, const BMessage& message,
32 bigtime_t interval, int32 count)
34 fToken(-1)
36 _InitData(target, &message, interval, count, be_app_messenger);
40 BMessageRunner::BMessageRunner(BMessenger target, const BMessage* message,
41 bigtime_t interval, int32 count, BMessenger replyTo)
43 fToken(-1)
45 _InitData(target, message, interval, count, replyTo);
49 BMessageRunner::BMessageRunner(BMessenger target, const BMessage& message,
50 bigtime_t interval, int32 count, BMessenger replyTo)
52 fToken(-1)
54 _InitData(target, &message, interval, count, replyTo);
58 BMessageRunner::~BMessageRunner()
60 if (fToken < B_OK)
61 return;
63 // compose the request message
64 BMessage request(B_REG_UNREGISTER_MESSAGE_RUNNER);
65 status_t result = request.AddInt32("token", fToken);
67 // send the request
68 BMessage reply;
69 if (result == B_OK)
70 result = BRoster::Private().SendTo(&request, &reply, false);
72 // ignore the reply, we can't do anything anyway
76 status_t
77 BMessageRunner::InitCheck() const
79 return fToken >= 0 ? B_OK : fToken;
83 status_t
84 BMessageRunner::SetInterval(bigtime_t interval)
86 return _SetParams(true, interval, false, 0);
90 status_t
91 BMessageRunner::SetCount(int32 count)
93 return _SetParams(false, 0, true, count);
97 status_t
98 BMessageRunner::GetInfo(bigtime_t* interval, int32* count) const
100 status_t result = fToken >= 0 ? B_OK : B_BAD_VALUE;
102 // compose the request message
103 BMessage request(B_REG_GET_MESSAGE_RUNNER_INFO);
104 if (result == B_OK)
105 result = request.AddInt32("token", fToken);
107 // send the request
108 BMessage reply;
109 if (result == B_OK)
110 result = BRoster::Private().SendTo(&request, &reply, false);
112 // evaluate the reply
113 if (result == B_OK) {
114 if (reply.what == B_REG_SUCCESS) {
115 // count
116 int32 _count;
117 if (reply.FindInt32("count", &_count) == B_OK) {
118 if (count != 0)
119 *count = _count;
120 } else
121 result = B_ERROR;
123 // interval
124 bigtime_t _interval;
125 if (reply.FindInt64("interval", &_interval) == B_OK) {
126 if (interval != 0)
127 *interval = _interval;
128 } else
129 result = B_ERROR;
130 } else {
131 if (reply.FindInt32("error", &result) != B_OK)
132 result = B_ERROR;
136 return result;
140 /*static*/ status_t
141 BMessageRunner::StartSending(BMessenger target, const BMessage* message,
142 bigtime_t interval, int32 count)
144 int32 token = _RegisterRunner(target, message, interval, count, true,
145 be_app_messenger);
147 return token >= B_OK ? B_OK : token;
151 /*static*/ status_t
152 BMessageRunner::StartSending(BMessenger target, const BMessage* message,
153 bigtime_t interval, int32 count, BMessenger replyTo)
155 int32 token = _RegisterRunner(target, message, interval, count, true,
156 replyTo);
158 return token >= B_OK ? B_OK : token;
162 // FBC
163 void BMessageRunner::_ReservedMessageRunner1() {}
164 void BMessageRunner::_ReservedMessageRunner2() {}
165 void BMessageRunner::_ReservedMessageRunner3() {}
166 void BMessageRunner::_ReservedMessageRunner4() {}
167 void BMessageRunner::_ReservedMessageRunner5() {}
168 void BMessageRunner::_ReservedMessageRunner6() {}
171 //! Privatized copy constructor to prevent usage.
172 BMessageRunner::BMessageRunner(const BMessageRunner &)
174 fToken(-1)
179 //! Privatized assignment operator to prevent usage.
180 BMessageRunner&
181 BMessageRunner::operator=(const BMessageRunner&)
183 return* this;
187 /*! Initializes the BMessageRunner.
189 The success of the initialization can (and should) be asked for via
190 InitCheck().
192 \note As soon as the last message has been sent, the message runner
193 becomes unusable. InitCheck() will still return \c B_OK, but
194 SetInterval(), SetCount() and GetInfo() will fail.
196 \param target Target of the message(s).
197 \param message The message to be sent to the target.
198 \param interval Period of time before the first message is sent and
199 between messages (if more than one shall be sent) in microseconds.
200 \param count Specifies how many times the message shall be sent.
201 A value less than \c 0 for an unlimited number of repetitions.
202 \param replyTo Target replies to the delivered message(s) shall be sent to.
204 void
205 BMessageRunner::_InitData(BMessenger target, const BMessage* message,
206 bigtime_t interval, int32 count, BMessenger replyTo)
208 fToken = _RegisterRunner(target, message, interval, count, false, replyTo);
212 /*! Registers the BMessageRunner in the registrar.
214 \param target Target of the message(s).
215 \param message The message to be sent to the target.
216 \param interval Period of time before the first message is sent and
217 between messages (if more than one shall be sent) in microseconds.
218 \param count Specifies how many times the message shall be sent.
219 A value less than \c 0 for an unlimited number of repetitions.
220 \param replyTo Target replies to the delivered message(s) shall be sent to.
222 \return The token the message runner is registered with, or the error code
223 while trying to register it.
225 /*static*/ int32
226 BMessageRunner::_RegisterRunner(BMessenger target, const BMessage* message,
227 bigtime_t interval, int32 count, bool detach, BMessenger replyTo)
229 status_t result = B_OK;
230 if (message == NULL || count == 0 || (count < 0 && detach))
231 result = B_BAD_VALUE;
233 // compose the request message
234 BMessage request(B_REG_REGISTER_MESSAGE_RUNNER);
235 if (result == B_OK)
236 result = request.AddInt32("team", BPrivate::current_team());
238 if (result == B_OK)
239 result = request.AddMessenger("target", target);
241 if (result == B_OK)
242 result = request.AddMessage("message", message);
244 if (result == B_OK)
245 result = request.AddInt64("interval", interval);
247 if (result == B_OK)
248 result = request.AddInt32("count", count);
250 if (result == B_OK)
251 result = request.AddMessenger("reply_target", replyTo);
253 // send the request
254 BMessage reply;
255 if (result == B_OK)
256 result = BRoster::Private().SendTo(&request, &reply, false);
258 int32 token;
260 // evaluate the reply
261 if (result == B_OK) {
262 if (reply.what == B_REG_SUCCESS) {
263 if (reply.FindInt32("token", &token) != B_OK)
264 result = B_ERROR;
265 } else {
266 if (reply.FindInt32("error", &result) != B_OK)
267 result = B_ERROR;
271 if (result == B_OK)
272 return token;
274 return result;
278 /*! Sets the message runner's interval and count parameters.
280 The parameters \a resetInterval and \a resetCount specify whether
281 the interval or the count parameter respectively shall be reset.
283 At least one parameter must be set, otherwise the methods returns
284 \c B_BAD_VALUE.
286 \param resetInterval \c true, if the interval shall be reset, \c false
287 otherwise -- then \a interval is ignored.
288 \param interval The new interval in microseconds.
289 \param resetCount \c true, if the count shall be reset, \c false
290 otherwise -- then \a count is ignored.
291 \param count Specifies how many times the message shall be sent.
292 A value less than \c 0 for an unlimited number of repetitions.
294 \return A status code.
295 \retval B_OK Everything went fine.
296 \retval B_BAD_VALUE The message runner is not longer valid. All the
297 messages that had to be sent have already been sent. Or both
298 \a resetInterval and \a resetCount are \c false.
300 status_t
301 BMessageRunner::_SetParams(bool resetInterval, bigtime_t interval,
302 bool resetCount, int32 count)
304 if ((!resetInterval && !resetCount) || fToken < 0)
305 return B_BAD_VALUE;
307 // compose the request message
308 BMessage request(B_REG_SET_MESSAGE_RUNNER_PARAMS);
309 status_t result = request.AddInt32("token", fToken);
310 if (result == B_OK && resetInterval)
311 result = request.AddInt64("interval", interval);
313 if (result == B_OK && resetCount)
314 result = request.AddInt32("count", count);
316 // send the request
317 BMessage reply;
318 if (result == B_OK)
319 result = BRoster::Private().SendTo(&request, &reply, false);
321 // evaluate the reply
322 if (result == B_OK) {
323 if (reply.what != B_REG_SUCCESS) {
324 if (reply.FindInt32("error", &result) != B_OK)
325 result = B_ERROR;
329 return result;