2002-07-22 02:40:07 +04:00
|
|
|
// Registrar.cpp
|
|
|
|
|
|
|
|
#include "Debug.h"
|
|
|
|
|
2005-01-14 18:33:57 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2002-07-22 02:40:07 +04:00
|
|
|
#include <Application.h>
|
|
|
|
#include <Message.h>
|
|
|
|
#include <OS.h>
|
|
|
|
#include <RegistrarDefs.h>
|
2003-02-09 02:29:21 +03:00
|
|
|
#include <RosterPrivate.h>
|
2002-07-22 02:40:07 +04:00
|
|
|
|
|
|
|
#include "ClipboardHandler.h"
|
2002-10-13 02:37:07 +04:00
|
|
|
#include "EventQueue.h"
|
2005-01-25 18:06:42 +03:00
|
|
|
#include "MessageDeliverer.h"
|
2002-10-15 01:19:43 +04:00
|
|
|
#include "MessageEvent.h"
|
2002-10-13 02:37:07 +04:00
|
|
|
#include "MessageRunnerManager.h"
|
2005-01-24 04:31:02 +03:00
|
|
|
#include "MessagingService.h"
|
2002-07-22 02:40:07 +04:00
|
|
|
#include "MIMEManager.h"
|
|
|
|
#include "Registrar.h"
|
2005-07-03 21:17:40 +04:00
|
|
|
#include "ShutdownProcess.h"
|
2002-07-24 03:40:12 +04:00
|
|
|
#include "TRoster.h"
|
2002-07-22 02:40:07 +04:00
|
|
|
|
2002-07-28 17:39:26 +04:00
|
|
|
/*!
|
|
|
|
\class Registrar
|
|
|
|
\brief The application class of the registrar.
|
|
|
|
|
|
|
|
Glues the registrar services together and dispatches the roster messages.
|
|
|
|
*/
|
|
|
|
|
2005-01-18 04:17:35 +03:00
|
|
|
using namespace BPrivate;
|
|
|
|
|
2002-10-15 01:19:43 +04:00
|
|
|
//! Name of the event queue.
|
|
|
|
static const char *kEventQueueName = "timer_thread";
|
|
|
|
|
|
|
|
//! Time interval between two roster sanity checks (1 s).
|
|
|
|
static const bigtime_t kRosterSanityEventInterval = 1000000LL;
|
|
|
|
|
2002-07-22 02:40:07 +04:00
|
|
|
// constructor
|
2002-07-28 17:39:26 +04:00
|
|
|
/*! \brief Creates the registrar application class.
|
2005-01-14 18:33:57 +03:00
|
|
|
\param error Passed to the BApplication constructor for returning an
|
|
|
|
error code.
|
2002-07-28 17:39:26 +04:00
|
|
|
*/
|
2005-01-14 18:33:57 +03:00
|
|
|
Registrar::Registrar(status_t *error)
|
2005-06-28 18:55:57 +04:00
|
|
|
: BServer(kRegistrarSignature, false, error),
|
2002-07-24 03:40:12 +04:00
|
|
|
fRoster(NULL),
|
2002-07-22 02:40:07 +04:00
|
|
|
fClipboardHandler(NULL),
|
2002-10-13 02:37:07 +04:00
|
|
|
fMIMEManager(NULL),
|
|
|
|
fEventQueue(NULL),
|
2002-10-15 01:19:43 +04:00
|
|
|
fMessageRunnerManager(NULL),
|
2005-01-25 18:06:42 +03:00
|
|
|
fSanityEvent(NULL)
|
2002-07-22 02:40:07 +04:00
|
|
|
{
|
2002-07-27 00:02:10 +04:00
|
|
|
FUNCTION_START();
|
2002-07-22 02:40:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// destructor
|
2002-07-28 17:39:26 +04:00
|
|
|
/*! \brief Frees all resources associated with the registrar.
|
|
|
|
|
|
|
|
All registrar services, that haven't been shut down earlier, are
|
|
|
|
terminated.
|
|
|
|
*/
|
2002-07-22 02:40:07 +04:00
|
|
|
Registrar::~Registrar()
|
|
|
|
{
|
2002-07-27 00:02:10 +04:00
|
|
|
FUNCTION_START();
|
2002-07-28 23:36:05 +04:00
|
|
|
Lock();
|
2002-10-13 02:37:07 +04:00
|
|
|
fEventQueue->Die();
|
|
|
|
delete fMessageRunnerManager;
|
|
|
|
delete fEventQueue;
|
2002-10-15 01:19:43 +04:00
|
|
|
delete fSanityEvent;
|
2002-07-22 02:40:07 +04:00
|
|
|
fMIMEManager->Lock();
|
|
|
|
fMIMEManager->Quit();
|
|
|
|
RemoveHandler(fClipboardHandler);
|
|
|
|
delete fClipboardHandler;
|
2002-07-24 03:40:12 +04:00
|
|
|
delete fRoster;
|
2002-10-15 01:19:43 +04:00
|
|
|
// Invalidate the global be_roster, so that the BApplication destructor
|
|
|
|
// won't dead-lock when sending a message to itself.
|
2003-02-09 02:29:21 +03:00
|
|
|
BRoster::Private().SetTo(BMessenger(), BMessenger());
|
2002-07-27 00:02:10 +04:00
|
|
|
FUNCTION_END();
|
2002-07-22 02:40:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// MessageReceived
|
2002-07-28 17:39:26 +04:00
|
|
|
/*! \brief Overrides the super class version to dispatch roster specific
|
|
|
|
messages.
|
|
|
|
\param message The message to be handled
|
|
|
|
*/
|
2002-07-22 02:40:07 +04:00
|
|
|
void
|
|
|
|
Registrar::MessageReceived(BMessage *message)
|
|
|
|
{
|
2002-10-15 01:19:43 +04:00
|
|
|
// FUNCTION_START();
|
2002-07-22 02:40:07 +04:00
|
|
|
switch (message->what) {
|
2002-10-13 02:37:07 +04:00
|
|
|
// general requests
|
2002-07-22 02:40:07 +04:00
|
|
|
case B_REG_GET_MIME_MESSENGER:
|
|
|
|
{
|
|
|
|
PRINT(("B_REG_GET_MIME_MESSENGER\n"));
|
|
|
|
BMessenger messenger(NULL, fMIMEManager);
|
|
|
|
BMessage reply(B_REG_SUCCESS);
|
|
|
|
reply.AddMessenger("messenger", messenger);
|
|
|
|
message->SendReply(&reply);
|
|
|
|
break;
|
|
|
|
}
|
2005-07-03 21:17:40 +04:00
|
|
|
|
2002-07-22 02:40:07 +04:00
|
|
|
case B_REG_GET_CLIPBOARD_MESSENGER:
|
|
|
|
{
|
|
|
|
PRINT(("B_REG_GET_CLIPBOARD_MESSENGER\n"));
|
|
|
|
BMessenger messenger(fClipboardHandler);
|
|
|
|
BMessage reply(B_REG_SUCCESS);
|
|
|
|
reply.AddMessenger("messenger", messenger);
|
|
|
|
message->SendReply(&reply);
|
|
|
|
break;
|
|
|
|
}
|
2005-07-03 21:17:40 +04:00
|
|
|
|
|
|
|
case B_REG_SHUT_DOWN:
|
|
|
|
{
|
|
|
|
PRINT(("B_REG_SHUT_DOWN\n"));
|
|
|
|
|
|
|
|
_HandleShutDown(message);
|
2005-07-06 14:25:50 +04:00
|
|
|
break;
|
2005-07-03 21:17:40 +04:00
|
|
|
}
|
|
|
|
|
2002-10-13 02:37:07 +04:00
|
|
|
// roster requests
|
2002-07-24 03:40:12 +04:00
|
|
|
case B_REG_ADD_APP:
|
|
|
|
fRoster->HandleAddApplication(message);
|
|
|
|
break;
|
|
|
|
case B_REG_COMPLETE_REGISTRATION:
|
|
|
|
fRoster->HandleCompleteRegistration(message);
|
|
|
|
break;
|
2002-07-28 04:26:39 +04:00
|
|
|
case B_REG_IS_APP_PRE_REGISTERED:
|
2002-07-24 03:40:12 +04:00
|
|
|
fRoster->HandleIsAppPreRegistered(message);
|
|
|
|
break;
|
|
|
|
case B_REG_REMOVE_PRE_REGISTERED_APP:
|
|
|
|
fRoster->HandleRemovePreRegApp(message);
|
|
|
|
break;
|
|
|
|
case B_REG_REMOVE_APP:
|
|
|
|
fRoster->HandleRemoveApp(message);
|
|
|
|
break;
|
2002-07-27 00:02:10 +04:00
|
|
|
case B_REG_SET_THREAD_AND_TEAM:
|
|
|
|
fRoster->HandleSetThreadAndTeam(message);
|
|
|
|
break;
|
2002-10-08 02:03:11 +04:00
|
|
|
case B_REG_SET_SIGNATURE:
|
|
|
|
fRoster->HandleSetSignature(message);
|
|
|
|
break;
|
2002-07-28 04:26:39 +04:00
|
|
|
case B_REG_GET_APP_INFO:
|
|
|
|
fRoster->HandleGetAppInfo(message);
|
|
|
|
break;
|
|
|
|
case B_REG_GET_APP_LIST:
|
|
|
|
fRoster->HandleGetAppList(message);
|
|
|
|
break;
|
|
|
|
case B_REG_ACTIVATE_APP:
|
|
|
|
fRoster->HandleActivateApp(message);
|
2002-07-27 00:02:10 +04:00
|
|
|
break;
|
2002-10-16 02:31:55 +04:00
|
|
|
case B_REG_BROADCAST:
|
|
|
|
fRoster->HandleBroadcast(message);
|
|
|
|
break;
|
2002-10-18 18:05:25 +04:00
|
|
|
case B_REG_START_WATCHING:
|
|
|
|
fRoster->HandleStartWatching(message);
|
|
|
|
break;
|
|
|
|
case B_REG_STOP_WATCHING:
|
|
|
|
fRoster->HandleStopWatching(message);
|
2002-10-27 10:50:27 +03:00
|
|
|
break;
|
|
|
|
case B_REG_GET_RECENT_DOCUMENTS:
|
|
|
|
fRoster->HandleGetRecentDocuments(message);
|
|
|
|
break;
|
|
|
|
case B_REG_GET_RECENT_FOLDERS:
|
|
|
|
fRoster->HandleGetRecentFolders(message);
|
|
|
|
break;
|
|
|
|
case B_REG_GET_RECENT_APPS:
|
|
|
|
fRoster->HandleGetRecentApps(message);
|
|
|
|
break;
|
|
|
|
case B_REG_ADD_TO_RECENT_DOCUMENTS:
|
|
|
|
fRoster->HandleAddToRecentDocuments(message);
|
|
|
|
break;
|
|
|
|
case B_REG_ADD_TO_RECENT_FOLDERS:
|
|
|
|
fRoster->HandleAddToRecentFolders(message);
|
|
|
|
break;
|
|
|
|
case B_REG_ADD_TO_RECENT_APPS:
|
|
|
|
fRoster->HandleAddToRecentApps(message);
|
|
|
|
break;
|
|
|
|
case B_REG_CLEAR_RECENT_DOCUMENTS:
|
2002-11-08 11:36:08 +03:00
|
|
|
fRoster->ClearRecentDocuments();
|
2002-10-27 10:50:27 +03:00
|
|
|
break;
|
|
|
|
case B_REG_CLEAR_RECENT_FOLDERS:
|
2002-11-08 11:36:08 +03:00
|
|
|
fRoster->ClearRecentFolders();
|
2002-10-27 10:50:27 +03:00
|
|
|
break;
|
|
|
|
case B_REG_CLEAR_RECENT_APPS:
|
2002-11-08 11:36:08 +03:00
|
|
|
fRoster->ClearRecentApps();
|
|
|
|
break;
|
|
|
|
case B_REG_LOAD_RECENT_LISTS:
|
|
|
|
fRoster->HandleLoadRecentLists(message);
|
|
|
|
break;
|
|
|
|
case B_REG_SAVE_RECENT_LISTS:
|
|
|
|
fRoster->HandleSaveRecentLists(message);
|
2002-10-18 18:05:25 +04:00
|
|
|
break;
|
2005-07-03 21:17:40 +04:00
|
|
|
|
2002-10-13 02:37:07 +04:00
|
|
|
// message runner requests
|
|
|
|
case B_REG_REGISTER_MESSAGE_RUNNER:
|
|
|
|
fMessageRunnerManager->HandleRegisterRunner(message);
|
|
|
|
break;
|
|
|
|
case B_REG_UNREGISTER_MESSAGE_RUNNER:
|
|
|
|
fMessageRunnerManager->HandleUnregisterRunner(message);
|
|
|
|
break;
|
|
|
|
case B_REG_SET_MESSAGE_RUNNER_PARAMS:
|
|
|
|
fMessageRunnerManager->HandleSetRunnerParams(message);
|
|
|
|
break;
|
|
|
|
case B_REG_GET_MESSAGE_RUNNER_INFO:
|
|
|
|
fMessageRunnerManager->HandleGetRunnerInfo(message);
|
|
|
|
break;
|
2005-07-03 21:17:40 +04:00
|
|
|
|
2002-10-15 01:19:43 +04:00
|
|
|
// internal messages
|
|
|
|
case B_REG_ROSTER_SANITY_EVENT:
|
|
|
|
fRoster->CheckSanity();
|
|
|
|
fSanityEvent->SetTime(system_time() + kRosterSanityEventInterval);
|
|
|
|
fEventQueue->AddEvent(fSanityEvent);
|
|
|
|
break;
|
2005-07-03 21:17:40 +04:00
|
|
|
case B_REG_SHUTDOWN_FINISHED:
|
|
|
|
if (fShutdownProcess) {
|
|
|
|
if (fShutdownProcess->Lock()) {
|
|
|
|
fShutdownProcess->Quit();
|
|
|
|
fShutdownProcess = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2002-07-22 02:40:07 +04:00
|
|
|
default:
|
|
|
|
BApplication::MessageReceived(message);
|
|
|
|
break;
|
|
|
|
}
|
2002-10-15 01:19:43 +04:00
|
|
|
// FUNCTION_END();
|
2002-07-22 02:40:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadyToRun
|
2002-07-28 17:39:26 +04:00
|
|
|
/*! \brief Overrides the super class version to initialize the registrar
|
|
|
|
services.
|
|
|
|
*/
|
2002-07-22 02:40:07 +04:00
|
|
|
void
|
|
|
|
Registrar::ReadyToRun()
|
|
|
|
{
|
2002-07-27 00:02:10 +04:00
|
|
|
FUNCTION_START();
|
2005-01-24 04:31:02 +03:00
|
|
|
|
2005-01-25 18:06:42 +03:00
|
|
|
// create message deliverer
|
|
|
|
status_t error = MessageDeliverer::CreateDefault();
|
|
|
|
if (error != B_OK) {
|
|
|
|
FATAL(("Registrar::ReadyToRun(): Failed to create the message "
|
|
|
|
"deliverer: %s\n", strerror(error)));
|
|
|
|
}
|
|
|
|
|
2003-02-12 02:37:48 +03:00
|
|
|
// create event queue
|
|
|
|
fEventQueue = new EventQueue(kEventQueueName);
|
2005-01-24 04:31:02 +03:00
|
|
|
|
2002-07-28 23:36:05 +04:00
|
|
|
// create roster
|
2002-07-28 17:39:26 +04:00
|
|
|
fRoster = new TRoster;
|
2002-07-28 23:36:05 +04:00
|
|
|
fRoster->Init();
|
2005-01-24 04:31:02 +03:00
|
|
|
|
2002-07-28 23:36:05 +04:00
|
|
|
// create clipboard handler
|
2002-07-28 17:39:26 +04:00
|
|
|
fClipboardHandler = new ClipboardHandler;
|
|
|
|
AddHandler(fClipboardHandler);
|
2005-01-24 04:31:02 +03:00
|
|
|
|
2002-07-28 23:36:05 +04:00
|
|
|
// create MIME manager
|
2002-07-28 17:39:26 +04:00
|
|
|
fMIMEManager = new MIMEManager;
|
|
|
|
fMIMEManager->Run();
|
2005-01-24 04:31:02 +03:00
|
|
|
|
2002-10-13 02:37:07 +04:00
|
|
|
// create message runner manager
|
|
|
|
fMessageRunnerManager = new MessageRunnerManager(fEventQueue);
|
2005-01-24 04:31:02 +03:00
|
|
|
|
2002-09-23 23:46:15 +04:00
|
|
|
// init the global be_roster
|
2003-02-09 02:29:21 +03:00
|
|
|
BRoster::Private().SetTo(be_app_messenger, BMessenger(NULL, fMIMEManager));
|
2005-01-24 04:31:02 +03:00
|
|
|
|
|
|
|
// create the messaging service
|
2005-01-25 18:06:42 +03:00
|
|
|
error = MessagingService::CreateDefault();
|
2005-01-24 04:31:02 +03:00
|
|
|
if (error != B_OK) {
|
|
|
|
ERROR(("Registrar::ReadyToRun(): Failed to init messaging service "
|
|
|
|
"(that's by design when running under R5): %s\n", strerror(error)));
|
|
|
|
}
|
|
|
|
|
2002-10-15 01:19:43 +04:00
|
|
|
// create and schedule the sanity message event
|
|
|
|
fSanityEvent = new MessageEvent(system_time() + kRosterSanityEventInterval,
|
|
|
|
this, B_REG_ROSTER_SANITY_EVENT);
|
|
|
|
fSanityEvent->SetAutoDelete(false);
|
|
|
|
fEventQueue->AddEvent(fSanityEvent);
|
2005-01-24 04:31:02 +03:00
|
|
|
|
2002-07-28 17:39:26 +04:00
|
|
|
FUNCTION_END();
|
2002-07-22 02:40:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// QuitRequested
|
2002-07-28 17:39:26 +04:00
|
|
|
/*! \brief Overrides the super class version to avoid termination of the
|
|
|
|
registrar until the system shutdown.
|
|
|
|
*/
|
2002-07-22 02:40:07 +04:00
|
|
|
bool
|
|
|
|
Registrar::QuitRequested()
|
|
|
|
{
|
2002-07-27 00:02:10 +04:00
|
|
|
FUNCTION_START();
|
2002-07-22 02:40:07 +04:00
|
|
|
// The final registrar must not quit. At least not that easily. ;-)
|
|
|
|
return BApplication::QuitRequested();
|
|
|
|
}
|
|
|
|
|
2002-10-15 01:19:43 +04:00
|
|
|
// GetEventQueue
|
|
|
|
/*! \brief Returns the registrar's event queue.
|
|
|
|
\return The registrar's event queue.
|
|
|
|
*/
|
|
|
|
EventQueue*
|
|
|
|
Registrar::GetEventQueue() const
|
|
|
|
{
|
|
|
|
return fEventQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// App
|
|
|
|
/*! \brief Returns the Registrar application object.
|
|
|
|
\return The Registrar application object.
|
|
|
|
*/
|
|
|
|
Registrar*
|
|
|
|
Registrar::App()
|
|
|
|
{
|
|
|
|
return dynamic_cast<Registrar*>(be_app);
|
|
|
|
}
|
|
|
|
|
2005-07-03 21:17:40 +04:00
|
|
|
// _HandleShutDown
|
|
|
|
/*! \brief Handle a shut down request message.
|
|
|
|
\param request The request to be handled.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Registrar::_HandleShutDown(BMessage *request)
|
|
|
|
{
|
|
|
|
status_t error = B_OK;
|
|
|
|
|
|
|
|
// check, whether we're already shutting down
|
|
|
|
if (fShutdownProcess)
|
|
|
|
error = B_SHUTTING_DOWN;
|
|
|
|
|
|
|
|
bool needsReply = true;
|
|
|
|
if (error == B_OK) {
|
|
|
|
// create a ShutdownProcess
|
|
|
|
fShutdownProcess = new(nothrow) ShutdownProcess(fRoster, fEventQueue);
|
|
|
|
if (fShutdownProcess) {
|
|
|
|
error = fShutdownProcess->Init(request);
|
|
|
|
if (error == B_OK) {
|
|
|
|
DetachCurrentMessage();
|
|
|
|
fShutdownProcess->Run();
|
|
|
|
needsReply = false;
|
|
|
|
} else {
|
|
|
|
delete fShutdownProcess;
|
|
|
|
fShutdownProcess = NULL;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
error = B_NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needsReply)
|
|
|
|
ShutdownProcess::SendReply(request, error);
|
|
|
|
}
|
2002-10-15 01:19:43 +04:00
|
|
|
|
2002-07-22 02:40:07 +04:00
|
|
|
// main
|
2002-07-28 17:39:26 +04:00
|
|
|
/*! \brief Creates and runs the registrar application.
|
|
|
|
|
|
|
|
The main thread is renamed.
|
|
|
|
|
|
|
|
\return 0.
|
|
|
|
*/
|
2002-07-22 02:40:07 +04:00
|
|
|
int
|
|
|
|
main()
|
|
|
|
{
|
2002-07-27 00:02:10 +04:00
|
|
|
FUNCTION_START();
|
2005-01-14 18:33:57 +03:00
|
|
|
|
2002-07-22 02:40:07 +04:00
|
|
|
// create and run the registrar application
|
2005-01-14 18:33:57 +03:00
|
|
|
status_t error;
|
|
|
|
Registrar *app = new Registrar(&error);
|
|
|
|
if (error != B_OK) {
|
|
|
|
fprintf(stderr, "Failed to create the BApplication: %s\n",
|
|
|
|
strerror(error));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-01-18 04:17:35 +03:00
|
|
|
// rename the main thread
|
|
|
|
rename_thread(find_thread(NULL), kRosterThreadName);
|
|
|
|
|
2002-07-22 02:40:07 +04:00
|
|
|
PRINT(("app->Run()...\n"));
|
|
|
|
app->Run();
|
2005-01-14 18:33:57 +03:00
|
|
|
|
2002-07-22 02:40:07 +04:00
|
|
|
PRINT(("delete app...\n"));
|
|
|
|
delete app;
|
2005-01-14 18:33:57 +03:00
|
|
|
|
2002-07-27 00:02:10 +04:00
|
|
|
FUNCTION_END();
|
2002-07-22 02:40:07 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|