1c2f641f2SAxel Dörfler /*
20690387cSAxel Dörfler * Copyright 2001-2015, Haiku, Inc. All Rights Reserved.
3c2f641f2SAxel Dörfler * Distributed under the terms of the MIT License.
4c2f641f2SAxel Dörfler *
5c2f641f2SAxel Dörfler * Authors:
64701b640SIngo Weinhold * Ingo Weinhold, ingo_weinhold@gmx.de
7c2f641f2SAxel Dörfler */
8c2f641f2SAxel Dörfler
94b8a7115SIngo Weinhold #include "Registrar.h"
10f2096210SIngo Weinhold
11f2096210SIngo Weinhold #include <stdio.h>
12f2096210SIngo Weinhold #include <string.h>
13f2096210SIngo Weinhold
14f2096210SIngo Weinhold #include <exception>
154b8a7115SIngo Weinhold
16c2f641f2SAxel Dörfler #include <Application.h>
174701b640SIngo Weinhold #include <Clipboard.h>
18c2f641f2SAxel Dörfler #include <Message.h>
199edb207fSFredrik Holmqvist #include <MessengerPrivate.h>
20c2f641f2SAxel Dörfler #include <OS.h>
21c2f641f2SAxel Dörfler #include <RegistrarDefs.h>
22c2f641f2SAxel Dörfler #include <RosterPrivate.h>
239edb207fSFredrik Holmqvist #include <system_info.h>
24c2f641f2SAxel Dörfler
25a94ce1c9SIngo Weinhold #include "AuthenticationManager.h"
26f2096210SIngo Weinhold #include "ClipboardHandler.h"
27f2096210SIngo Weinhold #include "Debug.h"
28f2096210SIngo Weinhold #include "EventQueue.h"
29f2096210SIngo Weinhold #include "MessageDeliverer.h"
30f2096210SIngo Weinhold #include "MessageEvent.h"
31f2096210SIngo Weinhold #include "MessageRunnerManager.h"
32f2096210SIngo Weinhold #include "MessagingService.h"
33f2096210SIngo Weinhold #include "MIMEManager.h"
345c9672edSIngo Weinhold #include "PackageWatchingManager.h"
35f2096210SIngo Weinhold #include "ShutdownProcess.h"
36f2096210SIngo Weinhold #include "TRoster.h"
37c2f641f2SAxel Dörfler
38c2f641f2SAxel Dörfler
392ad75c74SIngo Weinhold /*!
402ad75c74SIngo Weinhold \class Registrar
412ad75c74SIngo Weinhold \brief The application class of the registrar.
422ad75c74SIngo Weinhold
432ad75c74SIngo Weinhold Glues the registrar services together and dispatches the roster messages.
442ad75c74SIngo Weinhold */
452ad75c74SIngo Weinhold
46758b1d0eSIngo Weinhold using std::nothrow;
47769ba00eSIngo Weinhold using namespace BPrivate;
48769ba00eSIngo Weinhold
49463322c2SIngo Weinhold //! Name of the event queue.
50463322c2SIngo Weinhold static const char *kEventQueueName = "timer_thread";
51463322c2SIngo Weinhold
52c2f641f2SAxel Dörfler
532ad75c74SIngo Weinhold /*! \brief Creates the registrar application class.
54dca399f4SIngo Weinhold \param error Passed to the BApplication constructor for returning an
55dca399f4SIngo Weinhold error code.
562ad75c74SIngo Weinhold */
Registrar(status_t * _error)570690387cSAxel Dörfler Registrar::Registrar(status_t* _error)
58a0439d88SAxel Dörfler :
59e3d73948SJulian Harnath BServer(B_REGISTRAR_SIGNATURE, B_REGISTRAR_PORT_NAME, -1, false, _error),
6051d6b22eSIngo Weinhold fRoster(NULL),
614b8a7115SIngo Weinhold fClipboardHandler(NULL),
6244c3726bSIngo Weinhold fMIMEManager(NULL),
6344c3726bSIngo Weinhold fEventQueue(NULL),
64463322c2SIngo Weinhold fMessageRunnerManager(NULL),
65a94ce1c9SIngo Weinhold fShutdownProcess(NULL),
665c9672edSIngo Weinhold fAuthenticationManager(NULL),
675c9672edSIngo Weinhold fPackageWatchingManager(NULL)
684b8a7115SIngo Weinhold {
694b8a7115SIngo Weinhold FUNCTION_START();
70a0439d88SAxel Dörfler
71a0439d88SAxel Dörfler set_thread_priority(find_thread(NULL), B_NORMAL_PRIORITY + 1);
724b8a7115SIngo Weinhold }
734b8a7115SIngo Weinhold
74a0439d88SAxel Dörfler
752ad75c74SIngo Weinhold /*! \brief Frees all resources associated with the registrar.
762ad75c74SIngo Weinhold
772ad75c74SIngo Weinhold All registrar services, that haven't been shut down earlier, are
782ad75c74SIngo Weinhold terminated.
792ad75c74SIngo Weinhold */
~Registrar()804b8a7115SIngo Weinhold Registrar::~Registrar()
814b8a7115SIngo Weinhold {
824b8a7115SIngo Weinhold FUNCTION_START();
8360aa4feaSIngo Weinhold Lock();
8444c3726bSIngo Weinhold fEventQueue->Die();
85a94ce1c9SIngo Weinhold delete fAuthenticationManager;
865c9672edSIngo Weinhold delete fPackageWatchingManager;
8744c3726bSIngo Weinhold delete fMessageRunnerManager;
8844c3726bSIngo Weinhold delete fEventQueue;
894b8a7115SIngo Weinhold fMIMEManager->Lock();
904b8a7115SIngo Weinhold fMIMEManager->Quit();
914b8a7115SIngo Weinhold RemoveHandler(fClipboardHandler);
924b8a7115SIngo Weinhold delete fClipboardHandler;
9351d6b22eSIngo Weinhold delete fRoster;
94463322c2SIngo Weinhold // Invalidate the global be_roster, so that the BApplication destructor
95463322c2SIngo Weinhold // won't dead-lock when sending a message to itself.
962ed834e3SIngo Weinhold BRoster::Private().SetTo(BMessenger(), BMessenger());
97c39e4532SIngo Weinhold FUNCTION_END();
984b8a7115SIngo Weinhold }
994b8a7115SIngo Weinhold
100a0439d88SAxel Dörfler
1012ad75c74SIngo Weinhold /*! \brief Overrides the super class version to dispatch roster specific
1022ad75c74SIngo Weinhold messages.
1032ad75c74SIngo Weinhold \param message The message to be handled
1042ad75c74SIngo Weinhold */
1054b8a7115SIngo Weinhold void
MessageReceived(BMessage * message)1064b8a7115SIngo Weinhold Registrar::MessageReceived(BMessage *message)
1074b8a7115SIngo Weinhold {
108f2096210SIngo Weinhold try {
109f2096210SIngo Weinhold _MessageReceived(message);
110f2096210SIngo Weinhold } catch (std::exception& exception) {
111f2096210SIngo Weinhold char buffer[1024];
112f2096210SIngo Weinhold snprintf(buffer, sizeof(buffer),
113f2096210SIngo Weinhold "Registrar::MessageReceived() caught exception: %s",
114f2096210SIngo Weinhold exception.what());
115f2096210SIngo Weinhold debugger(buffer);
116f2096210SIngo Weinhold } catch (...) {
117f2096210SIngo Weinhold debugger("Registrar::MessageReceived() caught unknown exception");
118f2096210SIngo Weinhold }
119f2096210SIngo Weinhold }
120f2096210SIngo Weinhold
121a0439d88SAxel Dörfler
122f2096210SIngo Weinhold /*! \brief Overrides the super class version to initialize the registrar
123f2096210SIngo Weinhold services.
124f2096210SIngo Weinhold */
125f2096210SIngo Weinhold void
ReadyToRun()126f2096210SIngo Weinhold Registrar::ReadyToRun()
127f2096210SIngo Weinhold {
128f2096210SIngo Weinhold FUNCTION_START();
129f2096210SIngo Weinhold
130f2096210SIngo Weinhold // create message deliverer
131f2096210SIngo Weinhold status_t error = MessageDeliverer::CreateDefault();
132f2096210SIngo Weinhold if (error != B_OK) {
133e09045d4SMichael Lotz FATAL("Registrar::ReadyToRun(): Failed to create the message "
134e09045d4SMichael Lotz "deliverer: %s\n", strerror(error));
135f2096210SIngo Weinhold }
136f2096210SIngo Weinhold
137f2096210SIngo Weinhold // create event queue
138f2096210SIngo Weinhold fEventQueue = new EventQueue(kEventQueueName);
139f2096210SIngo Weinhold
140a94ce1c9SIngo Weinhold // create authentication manager
141a94ce1c9SIngo Weinhold fAuthenticationManager = new AuthenticationManager;
142a94ce1c9SIngo Weinhold fAuthenticationManager->Init();
143a94ce1c9SIngo Weinhold
144f2096210SIngo Weinhold // create roster
145f2096210SIngo Weinhold fRoster = new TRoster;
146f2096210SIngo Weinhold fRoster->Init();
147f2096210SIngo Weinhold
148f2096210SIngo Weinhold // create clipboard handler
149f2096210SIngo Weinhold fClipboardHandler = new ClipboardHandler;
150f2096210SIngo Weinhold AddHandler(fClipboardHandler);
151f2096210SIngo Weinhold
152f2096210SIngo Weinhold // create MIME manager
153f2096210SIngo Weinhold fMIMEManager = new MIMEManager;
154f2096210SIngo Weinhold fMIMEManager->Run();
155f2096210SIngo Weinhold
156f2096210SIngo Weinhold // create message runner manager
157f2096210SIngo Weinhold fMessageRunnerManager = new MessageRunnerManager(fEventQueue);
158f2096210SIngo Weinhold
159f2096210SIngo Weinhold // init the global be_roster
160f2096210SIngo Weinhold BRoster::Private().SetTo(be_app_messenger, BMessenger(NULL, fMIMEManager));
161f2096210SIngo Weinhold
162f2096210SIngo Weinhold // create the messaging service
163f2096210SIngo Weinhold error = MessagingService::CreateDefault();
164f2096210SIngo Weinhold if (error != B_OK) {
165e09045d4SMichael Lotz ERROR("Registrar::ReadyToRun(): Failed to init messaging service "
166e09045d4SMichael Lotz "(that's by design when running under R5): %s\n", strerror(error));
167f2096210SIngo Weinhold }
168f2096210SIngo Weinhold
1695c9672edSIngo Weinhold // create the package watching manager
1705c9672edSIngo Weinhold fPackageWatchingManager = new PackageWatchingManager;
1715c9672edSIngo Weinhold
1729edb207fSFredrik Holmqvist // Sanity check roster after team deletion
1739edb207fSFredrik Holmqvist BMessenger target(this);
1749edb207fSFredrik Holmqvist BMessenger::Private messengerPrivate(target);
1759edb207fSFredrik Holmqvist
1769edb207fSFredrik Holmqvist port_id port = messengerPrivate.Port();
1779edb207fSFredrik Holmqvist int32 token = messengerPrivate.Token();
1789edb207fSFredrik Holmqvist __start_watching_system(-1, B_WATCH_SYSTEM_TEAM_DELETION, port, token);
1799edb207fSFredrik Holmqvist fRoster->CheckSanity();
1809edb207fSFredrik Holmqvist // Clean up any teams that exited before we started watching
181f2096210SIngo Weinhold
182f2096210SIngo Weinhold FUNCTION_END();
183f2096210SIngo Weinhold }
184f2096210SIngo Weinhold
185a0439d88SAxel Dörfler
186f2096210SIngo Weinhold /*! \brief Overrides the super class version to avoid termination of the
187f2096210SIngo Weinhold registrar until the system shutdown.
188f2096210SIngo Weinhold */
189f2096210SIngo Weinhold bool
QuitRequested()190f2096210SIngo Weinhold Registrar::QuitRequested()
191f2096210SIngo Weinhold {
192f2096210SIngo Weinhold FUNCTION_START();
193f2096210SIngo Weinhold // The final registrar must not quit. At least not that easily. ;-)
194f2096210SIngo Weinhold return BApplication::QuitRequested();
195f2096210SIngo Weinhold }
196f2096210SIngo Weinhold
197a0439d88SAxel Dörfler
198f2096210SIngo Weinhold /*! \brief Returns the registrar's event queue.
199f2096210SIngo Weinhold \return The registrar's event queue.
200f2096210SIngo Weinhold */
201f2096210SIngo Weinhold EventQueue*
GetEventQueue() const202f2096210SIngo Weinhold Registrar::GetEventQueue() const
203f2096210SIngo Weinhold {
204f2096210SIngo Weinhold return fEventQueue;
205f2096210SIngo Weinhold }
206f2096210SIngo Weinhold
207a0439d88SAxel Dörfler
208f2096210SIngo Weinhold /*! \brief Returns the Registrar application object.
209f2096210SIngo Weinhold \return The Registrar application object.
210f2096210SIngo Weinhold */
211f2096210SIngo Weinhold Registrar*
App()212f2096210SIngo Weinhold Registrar::App()
213f2096210SIngo Weinhold {
214f2096210SIngo Weinhold return dynamic_cast<Registrar*>(be_app);
215f2096210SIngo Weinhold }
216f2096210SIngo Weinhold
217a0439d88SAxel Dörfler
218f2096210SIngo Weinhold void
_MessageReceived(BMessage * message)219f2096210SIngo Weinhold Registrar::_MessageReceived(BMessage *message)
220f2096210SIngo Weinhold {
2214b8a7115SIngo Weinhold switch (message->what) {
22244c3726bSIngo Weinhold // general requests
2234b8a7115SIngo Weinhold case B_REG_GET_MIME_MESSENGER:
2244b8a7115SIngo Weinhold {
225e09045d4SMichael Lotz PRINT("B_REG_GET_MIME_MESSENGER\n");
2264b8a7115SIngo Weinhold BMessenger messenger(NULL, fMIMEManager);
2274b8a7115SIngo Weinhold BMessage reply(B_REG_SUCCESS);
2284b8a7115SIngo Weinhold reply.AddMessenger("messenger", messenger);
2294b8a7115SIngo Weinhold message->SendReply(&reply);
2304b8a7115SIngo Weinhold break;
2314b8a7115SIngo Weinhold }
232553ea301SIngo Weinhold
2334b8a7115SIngo Weinhold case B_REG_GET_CLIPBOARD_MESSENGER:
2344b8a7115SIngo Weinhold {
235e09045d4SMichael Lotz PRINT("B_REG_GET_CLIPBOARD_MESSENGER\n");
2364b8a7115SIngo Weinhold BMessenger messenger(fClipboardHandler);
2374b8a7115SIngo Weinhold BMessage reply(B_REG_SUCCESS);
2384b8a7115SIngo Weinhold reply.AddMessenger("messenger", messenger);
2394b8a7115SIngo Weinhold message->SendReply(&reply);
2404b8a7115SIngo Weinhold break;
2414b8a7115SIngo Weinhold }
242553ea301SIngo Weinhold
2437301a7fbSAxel Dörfler // shutdown process
244553ea301SIngo Weinhold case B_REG_SHUT_DOWN:
245553ea301SIngo Weinhold {
246e09045d4SMichael Lotz PRINT("B_REG_SHUT_DOWN\n");
247553ea301SIngo Weinhold
248553ea301SIngo Weinhold _HandleShutDown(message);
24926ffd9e2SIngo Weinhold break;
250553ea301SIngo Weinhold }
251*feeb4b5dSJérôme Duval case B_REG_IS_SHUT_DOWN_IN_PROGRESS:
252*feeb4b5dSJérôme Duval {
253*feeb4b5dSJérôme Duval PRINT("B_REG_IS_SHUT_DOWN_IN_PROGRESS\n");
254*feeb4b5dSJérôme Duval
255*feeb4b5dSJérôme Duval _HandleIsShutDownInProgress(message);
256*feeb4b5dSJérôme Duval break;
257*feeb4b5dSJérôme Duval }
2587301a7fbSAxel Dörfler case B_REG_TEAM_DEBUGGER_ALERT:
2597301a7fbSAxel Dörfler {
2607301a7fbSAxel Dörfler if (fShutdownProcess != NULL)
2617301a7fbSAxel Dörfler fShutdownProcess->PostMessage(message);
2627301a7fbSAxel Dörfler break;
2637301a7fbSAxel Dörfler }
264553ea301SIngo Weinhold
26544c3726bSIngo Weinhold // roster requests
26651d6b22eSIngo Weinhold case B_REG_ADD_APP:
26751d6b22eSIngo Weinhold fRoster->HandleAddApplication(message);
26851d6b22eSIngo Weinhold break;
26951d6b22eSIngo Weinhold case B_REG_COMPLETE_REGISTRATION:
27051d6b22eSIngo Weinhold fRoster->HandleCompleteRegistration(message);
27151d6b22eSIngo Weinhold break;
2726bfd06d1SIngo Weinhold case B_REG_IS_APP_REGISTERED:
2736bfd06d1SIngo Weinhold fRoster->HandleIsAppRegistered(message);
27451d6b22eSIngo Weinhold break;
27551d6b22eSIngo Weinhold case B_REG_REMOVE_PRE_REGISTERED_APP:
27651d6b22eSIngo Weinhold fRoster->HandleRemovePreRegApp(message);
27751d6b22eSIngo Weinhold break;
27851d6b22eSIngo Weinhold case B_REG_REMOVE_APP:
27951d6b22eSIngo Weinhold fRoster->HandleRemoveApp(message);
28051d6b22eSIngo Weinhold break;
281c39e4532SIngo Weinhold case B_REG_SET_THREAD_AND_TEAM:
282c39e4532SIngo Weinhold fRoster->HandleSetThreadAndTeam(message);
283c39e4532SIngo Weinhold break;
284af8eec40SIngo Weinhold case B_REG_SET_SIGNATURE:
285af8eec40SIngo Weinhold fRoster->HandleSetSignature(message);
286af8eec40SIngo Weinhold break;
2872e042baaSIngo Weinhold case B_REG_GET_APP_INFO:
2882e042baaSIngo Weinhold fRoster->HandleGetAppInfo(message);
2892e042baaSIngo Weinhold break;
2902e042baaSIngo Weinhold case B_REG_GET_APP_LIST:
2912e042baaSIngo Weinhold fRoster->HandleGetAppList(message);
2922e042baaSIngo Weinhold break;
293c2f641f2SAxel Dörfler case B_REG_UPDATE_ACTIVE_APP:
294c2f641f2SAxel Dörfler fRoster->HandleUpdateActiveApp(message);
295c39e4532SIngo Weinhold break;
29656d4d637SIngo Weinhold case B_REG_BROADCAST:
29756d4d637SIngo Weinhold fRoster->HandleBroadcast(message);
29856d4d637SIngo Weinhold break;
2997a0335c8SIngo Weinhold case B_REG_START_WATCHING:
3007a0335c8SIngo Weinhold fRoster->HandleStartWatching(message);
3017a0335c8SIngo Weinhold break;
3027a0335c8SIngo Weinhold case B_REG_STOP_WATCHING:
3037a0335c8SIngo Weinhold fRoster->HandleStopWatching(message);
3047a0335c8SIngo Weinhold break;
3057c827740STyler Dauwalder case B_REG_GET_RECENT_DOCUMENTS:
3067c827740STyler Dauwalder fRoster->HandleGetRecentDocuments(message);
3077c827740STyler Dauwalder break;
3087c827740STyler Dauwalder case B_REG_GET_RECENT_FOLDERS:
3097c827740STyler Dauwalder fRoster->HandleGetRecentFolders(message);
3107c827740STyler Dauwalder break;
3117c827740STyler Dauwalder case B_REG_GET_RECENT_APPS:
3127c827740STyler Dauwalder fRoster->HandleGetRecentApps(message);
3137c827740STyler Dauwalder break;
3147c827740STyler Dauwalder case B_REG_ADD_TO_RECENT_DOCUMENTS:
3157c827740STyler Dauwalder fRoster->HandleAddToRecentDocuments(message);
3167c827740STyler Dauwalder break;
3177c827740STyler Dauwalder case B_REG_ADD_TO_RECENT_FOLDERS:
3187c827740STyler Dauwalder fRoster->HandleAddToRecentFolders(message);
3197c827740STyler Dauwalder break;
3207c827740STyler Dauwalder case B_REG_ADD_TO_RECENT_APPS:
3217c827740STyler Dauwalder fRoster->HandleAddToRecentApps(message);
3227c827740STyler Dauwalder break;
3237c827740STyler Dauwalder case B_REG_CLEAR_RECENT_DOCUMENTS:
3241bde02aeSTyler Dauwalder fRoster->ClearRecentDocuments();
3257c827740STyler Dauwalder break;
3267c827740STyler Dauwalder case B_REG_CLEAR_RECENT_FOLDERS:
3271bde02aeSTyler Dauwalder fRoster->ClearRecentFolders();
3287c827740STyler Dauwalder break;
3297c827740STyler Dauwalder case B_REG_CLEAR_RECENT_APPS:
3301bde02aeSTyler Dauwalder fRoster->ClearRecentApps();
3311bde02aeSTyler Dauwalder break;
3321bde02aeSTyler Dauwalder case B_REG_LOAD_RECENT_LISTS:
3331bde02aeSTyler Dauwalder fRoster->HandleLoadRecentLists(message);
3341bde02aeSTyler Dauwalder break;
3351bde02aeSTyler Dauwalder case B_REG_SAVE_RECENT_LISTS:
3361bde02aeSTyler Dauwalder fRoster->HandleSaveRecentLists(message);
3377c827740STyler Dauwalder break;
338553ea301SIngo Weinhold
33944c3726bSIngo Weinhold // message runner requests
34044c3726bSIngo Weinhold case B_REG_REGISTER_MESSAGE_RUNNER:
34144c3726bSIngo Weinhold fMessageRunnerManager->HandleRegisterRunner(message);
34244c3726bSIngo Weinhold break;
34344c3726bSIngo Weinhold case B_REG_UNREGISTER_MESSAGE_RUNNER:
34444c3726bSIngo Weinhold fMessageRunnerManager->HandleUnregisterRunner(message);
34544c3726bSIngo Weinhold break;
34644c3726bSIngo Weinhold case B_REG_SET_MESSAGE_RUNNER_PARAMS:
34744c3726bSIngo Weinhold fMessageRunnerManager->HandleSetRunnerParams(message);
34844c3726bSIngo Weinhold break;
34944c3726bSIngo Weinhold case B_REG_GET_MESSAGE_RUNNER_INFO:
35044c3726bSIngo Weinhold fMessageRunnerManager->HandleGetRunnerInfo(message);
35144c3726bSIngo Weinhold break;
352553ea301SIngo Weinhold
3535c9672edSIngo Weinhold // package watching requests
3545c9672edSIngo Weinhold case B_REG_PACKAGE_START_WATCHING:
3555c9672edSIngo Weinhold case B_REG_PACKAGE_STOP_WATCHING:
3565c9672edSIngo Weinhold fPackageWatchingManager->HandleStartStopWatching(message);
3575c9672edSIngo Weinhold break;
3585c9672edSIngo Weinhold case B_PACKAGE_UPDATE:
3595c9672edSIngo Weinhold fPackageWatchingManager->NotifyWatchers(message);
3605c9672edSIngo Weinhold break;
3615c9672edSIngo Weinhold
362463322c2SIngo Weinhold // internal messages
3639edb207fSFredrik Holmqvist case B_SYSTEM_OBJECT_UPDATE:
3649edb207fSFredrik Holmqvist {
3659edb207fSFredrik Holmqvist team_id team = (team_id)message->GetInt32("team", -1);
3669edb207fSFredrik Holmqvist if (team >= 0 && message->GetInt32("opcode", 0) == B_TEAM_DELETED)
3679edb207fSFredrik Holmqvist fRoster->HandleRemoveApp(message);
368463322c2SIngo Weinhold break;
3699edb207fSFredrik Holmqvist }
370553ea301SIngo Weinhold case B_REG_SHUTDOWN_FINISHED:
371553ea301SIngo Weinhold if (fShutdownProcess) {
3726b454d97SIngo Weinhold fShutdownProcess->PostMessage(B_QUIT_REQUESTED,
3736b454d97SIngo Weinhold fShutdownProcess);
374553ea301SIngo Weinhold fShutdownProcess = NULL;
375553ea301SIngo Weinhold }
376553ea301SIngo Weinhold break;
377553ea301SIngo Weinhold
37804209cddSczeidler case kMsgRestartAppServer:
37904209cddSczeidler {
38004209cddSczeidler fRoster->HandleRestartAppServer(message);
38104209cddSczeidler break;
38204209cddSczeidler }
38304209cddSczeidler
3844b8a7115SIngo Weinhold default:
3854b8a7115SIngo Weinhold BApplication::MessageReceived(message);
3864b8a7115SIngo Weinhold break;
3874b8a7115SIngo Weinhold }
3884b8a7115SIngo Weinhold }
3894b8a7115SIngo Weinhold
390a0439d88SAxel Dörfler
391553ea301SIngo Weinhold /*! \brief Handle a shut down request message.
392553ea301SIngo Weinhold \param request The request to be handled.
393553ea301SIngo Weinhold */
394553ea301SIngo Weinhold void
_HandleShutDown(BMessage * request)395553ea301SIngo Weinhold Registrar::_HandleShutDown(BMessage *request)
396553ea301SIngo Weinhold {
397553ea301SIngo Weinhold status_t error = B_OK;
398553ea301SIngo Weinhold
399553ea301SIngo Weinhold // check, whether we're already shutting down
400553ea301SIngo Weinhold if (fShutdownProcess)
401553ea301SIngo Weinhold error = B_SHUTTING_DOWN;
402553ea301SIngo Weinhold
403553ea301SIngo Weinhold bool needsReply = true;
404553ea301SIngo Weinhold if (error == B_OK) {
405553ea301SIngo Weinhold // create a ShutdownProcess
406553ea301SIngo Weinhold fShutdownProcess = new(nothrow) ShutdownProcess(fRoster, fEventQueue);
407553ea301SIngo Weinhold if (fShutdownProcess) {
408553ea301SIngo Weinhold error = fShutdownProcess->Init(request);
409553ea301SIngo Weinhold if (error == B_OK) {
410553ea301SIngo Weinhold DetachCurrentMessage();
411553ea301SIngo Weinhold fShutdownProcess->Run();
412553ea301SIngo Weinhold needsReply = false;
413553ea301SIngo Weinhold } else {
414553ea301SIngo Weinhold delete fShutdownProcess;
415553ea301SIngo Weinhold fShutdownProcess = NULL;
416553ea301SIngo Weinhold }
417553ea301SIngo Weinhold } else
418553ea301SIngo Weinhold error = B_NO_MEMORY;
419553ea301SIngo Weinhold }
420553ea301SIngo Weinhold
421553ea301SIngo Weinhold if (needsReply)
422553ea301SIngo Weinhold ShutdownProcess::SendReply(request, error);
423553ea301SIngo Weinhold }
424463322c2SIngo Weinhold
425c2f641f2SAxel Dörfler
426*feeb4b5dSJérôme Duval /*! \brief Handle a is shut down in progress request message.
427*feeb4b5dSJérôme Duval \param request The request to be handled.
428*feeb4b5dSJérôme Duval */
429*feeb4b5dSJérôme Duval void
_HandleIsShutDownInProgress(BMessage * request)430*feeb4b5dSJérôme Duval Registrar::_HandleIsShutDownInProgress(BMessage *request)
431*feeb4b5dSJérôme Duval {
432*feeb4b5dSJérôme Duval BMessage reply(B_REG_SUCCESS);
433*feeb4b5dSJérôme Duval reply.AddBool("in-progress", fShutdownProcess != NULL);
434*feeb4b5dSJérôme Duval request->SendReply(&reply);
435*feeb4b5dSJérôme Duval }
436*feeb4b5dSJérôme Duval
437*feeb4b5dSJérôme Duval
438c2f641f2SAxel Dörfler // #pragma mark -
439c2f641f2SAxel Dörfler
440c2f641f2SAxel Dörfler
4412ad75c74SIngo Weinhold /*! \brief Creates and runs the registrar application.
4422ad75c74SIngo Weinhold
4432ad75c74SIngo Weinhold The main thread is renamed.
4442ad75c74SIngo Weinhold
4452ad75c74SIngo Weinhold \return 0.
4462ad75c74SIngo Weinhold */
4474b8a7115SIngo Weinhold int
main()4484b8a7115SIngo Weinhold main()
4494b8a7115SIngo Weinhold {
4504b8a7115SIngo Weinhold FUNCTION_START();
451dca399f4SIngo Weinhold
4524701b640SIngo Weinhold // Create the global be_clipboard manually -- it will not work, since it
4534701b640SIngo Weinhold // wants to talk to the registrar in its constructor, but it doesn't have
4544701b640SIngo Weinhold // to and we would otherwise deadlock when initializing our GUI in the
4554701b640SIngo Weinhold // app thread.
4564701b640SIngo Weinhold be_clipboard = new BClipboard(NULL);
4574701b640SIngo Weinhold
4584b8a7115SIngo Weinhold // create and run the registrar application
459dca399f4SIngo Weinhold status_t error;
460dca399f4SIngo Weinhold Registrar *app = new Registrar(&error);
461dca399f4SIngo Weinhold if (error != B_OK) {
462310c813fSIngo Weinhold fprintf(stderr, "REG: Failed to create the BApplication: %s\n",
463dca399f4SIngo Weinhold strerror(error));
464dca399f4SIngo Weinhold return 1;
465dca399f4SIngo Weinhold }
466dca399f4SIngo Weinhold
467769ba00eSIngo Weinhold // rename the main thread
468bea38cb7SAxel Dörfler rename_thread(find_thread(NULL), "roster");
469769ba00eSIngo Weinhold
470e09045d4SMichael Lotz PRINT("app->Run()...\n");
471f2096210SIngo Weinhold
472f2096210SIngo Weinhold try {
4734b8a7115SIngo Weinhold app->Run();
474f2096210SIngo Weinhold } catch (std::exception& exception) {
475f2096210SIngo Weinhold char buffer[1024];
476f2096210SIngo Weinhold snprintf(buffer, sizeof(buffer),
477f2096210SIngo Weinhold "registrar main() caught exception: %s", exception.what());
478f2096210SIngo Weinhold debugger(buffer);
479f2096210SIngo Weinhold } catch (...) {
480f2096210SIngo Weinhold debugger("registrar main() caught unknown exception");
481f2096210SIngo Weinhold }
482dca399f4SIngo Weinhold
483e09045d4SMichael Lotz PRINT("delete app...\n");
4844b8a7115SIngo Weinhold delete app;
485dca399f4SIngo Weinhold
4864b8a7115SIngo Weinhold FUNCTION_END();
4874b8a7115SIngo Weinhold return 0;
4884b8a7115SIngo Weinhold }
4894b8a7115SIngo Weinhold
490