(purple) Loading from arbitary add-on directory

This commit is contained in:
Jaidyn Ann 2021-08-15 01:08:32 -05:00
parent ed7fe730cb
commit a11215ebf2
2 changed files with 28 additions and 9 deletions

View File

@ -19,12 +19,15 @@
#include "PurpleProtocol.h" #include "PurpleProtocol.h"
#include <iostream>
#include <Application.h> #include <Application.h>
#include <Catalog.h> #include <Catalog.h>
#include <Resources.h> #include <Resources.h>
#include <Roster.h> #include <Roster.h>
#include <TranslationUtils.h> #include <TranslationUtils.h>
#include <Cardie.h>
#include <ChatProtocolMessages.h> #include <ChatProtocolMessages.h>
#include "Purple.h" #include "Purple.h"
@ -42,6 +45,8 @@ ChatProtocol*
protocol_at(int32 i) protocol_at(int32 i)
{ {
BMessenger* msgr = ensure_app_messenger(); BMessenger* msgr = ensure_app_messenger();
if (msgr == NULL)
return NULL;
BMessage* msg = new BMessage(PURPLE_REQUEST_PROTOCOL_INFO); BMessage* msg = new BMessage(PURPLE_REQUEST_PROTOCOL_INFO);
msg->AddInt64("thread_id", find_thread(NULL)); msg->AddInt64("thread_id", find_thread(NULL));
@ -59,9 +64,13 @@ protocol_at(int32 i)
int32 int32
protocol_count() protocol_count()
{ {
BMessenger* msgr = ensure_app_messenger();
if (msgr == NULL)
return 0;
BMessage* msg = new BMessage(PURPLE_REQUEST_PROTOCOL_COUNT); BMessage* msg = new BMessage(PURPLE_REQUEST_PROTOCOL_COUNT);
msg->AddInt64("thread_id", find_thread(NULL)); msg->AddInt64("thread_id", find_thread(NULL));
ensure_app_messenger()->SendMessage(msg); msgr->SendMessage(msg);
thread_id sender; thread_id sender;
return receive_data(&sender, NULL, 0); return receive_data(&sender, NULL, 0);
@ -93,30 +102,40 @@ BMessenger*
ensure_app_messenger() ensure_app_messenger()
{ {
if (kAppMessenger == NULL || kAppMessenger->IsValid() == false) { if (kAppMessenger == NULL || kAppMessenger->IsValid() == false) {
ensure_app(); if (ensure_app() == true)
kAppMessenger = new BMessenger(PURPLE_SIGNATURE); kAppMessenger = new BMessenger(PURPLE_SIGNATURE);
} }
return kAppMessenger; return kAppMessenger;
} }
void bool
ensure_app() ensure_app()
{ {
BRoster roster; BRoster roster;
if (roster.IsRunning(PURPLE_SIGNATURE) == true) if (roster.IsRunning(PURPLE_SIGNATURE) == true)
return; return true;
if (roster.Launch(PURPLE_SIGNATURE) == B_OK) {
snooze(100000);
return true;
}
app_info aInfo; app_info aInfo;
be_app->GetAppInfo(&aInfo); be_app->GetAppInfo(&aInfo);
BPath protoPath(&aInfo.ref); BPath protoPath(&aInfo.ref);
protoPath.GetParent(&protoPath); protoPath.GetParent(&protoPath);
protoPath.Append("protocols/purple"); protoPath.Append(BString(APP_NAME).ToLower().Append("/purple"));
entry_ref protoRef; entry_ref protoRef;
BEntry(protoPath.Path()).GetRef(&protoRef); BEntry(protoPath.Path()).GetRef(&protoRef);
roster.Launch(&protoRef); if (roster.Launch(&protoRef) == B_OK) {
snooze(100000); snooze(100000);
return true;
}
std::cerr << "libpurple add-on could not find its binary! All is lost!\n";
return false;
} }

View File

@ -35,7 +35,7 @@ extern "C" _EXPORT const char* friendly_signature();
extern "C" _EXPORT uint32 version(); extern "C" _EXPORT uint32 version();
BMessenger* ensure_app_messenger(); BMessenger* ensure_app_messenger();
void ensure_app(); bool ensure_app();
status_t connect_thread(void* data); status_t connect_thread(void* data);
BMessage receive_message(); BMessage receive_message();