BPicture: Fix archive constructor.
[haiku.git] / src / kits / app / LaunchRoster.cpp
blob7770534acebfab4c873ac9e4fe8c793ec9f079b9
1 /*
2 * Copyright 2015 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Axel Dörfler, axeld@pinc-software.de
7 */
10 #include <LaunchRoster.h>
12 #include <Application.h>
13 #include <String.h>
15 #include <launch.h>
16 #include <LaunchDaemonDefs.h>
17 #include <LaunchRosterPrivate.h>
18 #include <MessengerPrivate.h>
21 using namespace BPrivate;
24 BLaunchRoster::Private::Private(BLaunchRoster* roster)
26 fRoster(roster)
31 BLaunchRoster::Private::Private(BLaunchRoster& roster)
33 fRoster(&roster)
38 status_t
39 BLaunchRoster::Private::RegisterSessionDaemon(const BMessenger& daemon)
41 BMessage request(B_REGISTER_SESSION_DAEMON);
42 status_t status = request.AddInt32("user", getuid());
43 if (status == B_OK)
44 status = request.AddMessenger("daemon", daemon);
45 if (status != B_OK)
46 return status;
48 // send the request
49 BMessage result;
50 status = fRoster->fMessenger.SendMessage(&request, &result);
52 // evaluate the reply
53 if (status == B_OK)
54 status = result.what;
56 return status;
60 // #pragma mark -
63 BLaunchRoster::BLaunchRoster()
65 _InitMessenger();
69 BLaunchRoster::~BLaunchRoster()
74 status_t
75 BLaunchRoster::InitCheck() const
77 return fMessenger.Team() >= 0 ? B_OK : B_ERROR;
81 status_t
82 BLaunchRoster::GetData(BMessage& data)
84 if (be_app == NULL)
85 return B_BAD_VALUE;
87 return GetData(be_app->Signature(), data);
91 status_t
92 BLaunchRoster::GetData(const char* signature, BMessage& data)
94 if (signature == NULL || signature[0] == '\0')
95 return B_BAD_VALUE;
97 BMessage request(B_GET_LAUNCH_DATA);
98 status_t status = request.AddString("name", signature);
99 if (status == B_OK)
100 status = request.AddInt32("user", getuid());
101 if (status != B_OK)
102 return status;
104 // send the request
105 status = fMessenger.SendMessage(&request, &data);
107 // evaluate the reply
108 if (status == B_OK)
109 status = data.what;
111 return status;
115 port_id
116 BLaunchRoster::GetPort(const char* name)
118 if (be_app == NULL)
119 return B_BAD_VALUE;
121 return GetPort(be_app->Signature(), name);
125 port_id
126 BLaunchRoster::GetPort(const char* signature, const char* name)
128 BMessage data;
129 status_t status = GetData(signature, data);
130 if (status == B_OK) {
131 BString fieldName;
132 if (name != NULL)
133 fieldName << name << "_";
134 fieldName << "port";
136 port_id port = data.GetInt32(fieldName.String(), B_NAME_NOT_FOUND);
137 if (port >= 0)
138 return port;
141 return -1;
145 status_t
146 BLaunchRoster::Target(const char* name, const BMessage& data,
147 const char* baseName)
149 return Target(name, &data, baseName);
153 status_t
154 BLaunchRoster::Target(const char* name, const BMessage* data,
155 const char* baseName)
157 if (name == NULL)
158 return B_BAD_VALUE;
160 BMessage request(B_LAUNCH_TARGET);
161 status_t status = request.AddInt32("user", getuid());
162 if (status == B_OK)
163 status = request.AddString("target", name);
164 if (status == B_OK && data != NULL && !data->IsEmpty())
165 status = request.AddMessage("data", data);
166 if (status == B_OK && baseName != NULL)
167 status = request.AddString("base target", baseName);
168 if (status != B_OK)
169 return status;
171 // send the request
172 BMessage result;
173 status = fMessenger.SendMessage(&request, &result);
175 // evaluate the reply
176 if (status == B_OK)
177 status = result.what;
179 return status;
183 status_t
184 BLaunchRoster::StartSession(const char* login)
186 if (login == NULL)
187 return B_BAD_VALUE;
189 BMessage request(B_LAUNCH_SESSION);
190 status_t status = request.AddInt32("user", getuid());
191 if (status == B_OK)
192 status = request.AddString("login", login);
193 if (status != B_OK)
194 return status;
196 // send the request
197 BMessage result;
198 status = fMessenger.SendMessage(&request, &result);
200 // evaluate the reply
201 if (status == B_OK)
202 status = result.what;
204 return status;
208 status_t
209 BLaunchRoster::RegisterEvent(const BMessenger& source, const char* name)
211 return _UpdateEvent(B_REGISTER_LAUNCH_EVENT, source, name);
215 status_t
216 BLaunchRoster::UnregisterEvent(const BMessenger& source, const char* name)
218 return _UpdateEvent(B_UNREGISTER_LAUNCH_EVENT, source, name);
222 status_t
223 BLaunchRoster::NotifyEvent(const BMessenger& source, const char* name)
225 return _UpdateEvent(B_NOTIFY_LAUNCH_EVENT, source, name);
229 void
230 BLaunchRoster::_InitMessenger()
232 // find the launch_daemon port
233 port_id daemonPort = BPrivate::get_launch_daemon_port();
234 port_info info;
235 if (daemonPort >= 0 && get_port_info(daemonPort, &info) == B_OK) {
236 BMessenger::Private(fMessenger).SetTo(info.team, daemonPort,
237 B_PREFERRED_TOKEN);
242 status_t
243 BLaunchRoster::_UpdateEvent(uint32 what, const BMessenger& source,
244 const char* name)
246 if (be_app == NULL || name == NULL || name[0] == '\0')
247 return B_BAD_VALUE;
249 BMessage request(what);
250 status_t status = request.AddInt32("user", getuid());
251 if (status == B_OK)
252 status = request.AddMessenger("source", source);
253 if (status == B_OK)
254 status = request.AddString("owner", be_app->Signature());
255 if (status == B_OK)
256 status = request.AddString("name", name);
257 if (status != B_OK)
258 return status;
260 // send the request
261 BMessage result;
262 status = fMessenger.SendMessage(&request, &result);
264 // evaluate the reply
265 if (status == B_OK)
266 status = result.what;
268 return status;