Allow multiple protocols per add-on

Now an add-on can contain multiple protocols, and the protocol API has
changed. An add-on must now export protocol_count() and protocol_at(),
with the latter replacing protocol(). protocol_count() returning the
amount of protocols in a given add-on, and protocol_at(i) giving a
new CayaProtocol* "at" the given index.

CayaProtocol has also been changed, adding Signature(),
FriendlySignature(), Icon(), Path(), and SetPath(). The reasoning is
that different protocols (even within a single add-on) will have
different signatures and icons, so this data should be accessible from
the protocol itself.

CayaProtocolAddOn now has CountProtocols() and ProtocolAt(i), allowing
the accessing of multiple protocols. A CayaProtocolAddOn can be given a
default protocol index in the constructor, whose protocol will be
returned with Protocol(). Version() was also moved from CayaProtocol to
CayaProtocolAddOn.
This commit is contained in:
Jaidyn Ann 2021-05-21 13:33:43 -05:00
parent 53fe03d2c8
commit 61a1f0baf1
23 changed files with 296 additions and 189 deletions

View File

@ -8,6 +8,8 @@
#include <Messenger.h>
class BBitmap;
// Caya protocol interface version
#define CAYA_VERSION_1_PRE_ALPHA_1 0x00000001
#define CAYA_VERSION_1_ALPHA_1 0x00000100
@ -42,14 +44,18 @@ public:
//! Protocol name
virtual const char* FriendlySignature() const = 0;
//! Protocol icon
virtual BBitmap* Icon() const = 0;
//! Add-on's path
virtual void SetPath(BPath path) = 0;
virtual BPath Path() = 0;
//! Preferred encoding of messages
virtual uint32 GetEncoding() = 0;
//! Messenger interface used
virtual CayaProtocolMessengerInterface* MessengerInterface() const = 0;
//! Caya interface version
virtual uint32 Version() const = 0;
};
#endif // _CAYA_PROTOCOL_H

View File

@ -8,6 +8,7 @@
*/
#include <Bitmap.h>
#include <Path.h>
#include <libinterface/BitmapUtils.h>
@ -15,11 +16,12 @@
#include "CayaProtocolAddOn.h"
CayaProtocolAddOn::CayaProtocolAddOn(image_id image, const char* path)
CayaProtocolAddOn::CayaProtocolAddOn(image_id image, const char* path, int32 subProto)
:
fImage(image),
fPath(path),
fIcon(NULL)
fIcon(NULL),
fProtoIndex(subProto)
{
_Init();
}
@ -42,7 +44,23 @@ CayaProtocolAddOn::Path() const
CayaProtocol*
CayaProtocolAddOn::Protocol() const
{
return fGetProtocol();
return ProtocolAt(fProtoIndex);
}
CayaProtocol*
CayaProtocolAddOn::ProtocolAt(int32 i) const
{
CayaProtocol* proto = fGetProtocol(i);
proto->SetPath(BPath(fPath.String()));
return proto;
}
int32
CayaProtocolAddOn::CountProtocols() const
{
return fCountProtocols();
}
@ -60,6 +78,13 @@ CayaProtocolAddOn::FriendlySignature() const
}
uint32
CayaProtocolAddOn::Version() const
{
return fVersion;
}
BBitmap*
CayaProtocolAddOn::Icon() const
{
@ -72,16 +97,24 @@ CayaProtocolAddOn::_Init()
{
const char* (*signature)();
const char* (*friendly_signature)();
uint32 (*version)();
fStatus = B_OK;
if (get_image_symbol(fImage, "protocol", B_SYMBOL_TYPE_TEXT,
if (get_image_symbol(fImage, "protocol_at", B_SYMBOL_TYPE_TEXT,
(void**)&fGetProtocol) != B_OK) {
unload_add_on(fImage);
fStatus = B_ERROR;
return;
}
if (get_image_symbol(fImage, "protocol_count", B_SYMBOL_TYPE_TEXT,
(void**)&fCountProtocols) != B_OK) {
unload_add_on(fImage);
fStatus = B_ERROR;
return;
}
if (get_image_symbol(fImage, "signature", B_SYMBOL_TYPE_TEXT,
(void**)&signature) != B_OK) {
unload_add_on(fImage);
@ -96,6 +129,14 @@ CayaProtocolAddOn::_Init()
return;
}
if (get_image_symbol(fImage, "version", B_SYMBOL_TYPE_TEXT,
(void**)&version) != B_OK) {
unload_add_on(fImage);
fStatus = B_ERROR;
return;
}
fSignature = signature();
fFriendlySignature = friendly_signature();
fVersion = version();
}

View File

@ -10,27 +10,36 @@
#include <String.h>
class BBitmap;
class CayaProtocol;
class CayaProtocolAddOn {
public:
CayaProtocolAddOn(image_id image, const char* path);
CayaProtocolAddOn(image_id image, const char* path,
int32 subProto=0);
status_t InitCheck() const;
const char* Path() const;
CayaProtocol* Protocol() const;
CayaProtocol* ProtocolAt(int32 i) const;
int32 CountProtocols() const;
const char* Signature() const;
const char* FriendlySignature() const;
uint32 Version() const;
BBitmap* Icon() const;
private:
image_id fImage;
BString fPath;
CayaProtocol* (*fGetProtocol)();
CayaProtocol* (*fGetProtocol)(int32 i);
int32 (*fCountProtocols)();
int32 fProtoIndex;
uint32 fVersion;
BString fSignature;
BString fFriendlySignature;
BBitmap* fIcon;

View File

@ -93,6 +93,23 @@ CayaAccountPath(const char* signature)
}
const char*
CayaAccountPath(const char* signature, const char* subsignature)
{
if (BString(signature) == BString(subsignature)
|| BString(subsignature).IsEmpty() == true)
return CayaAccountPath(signature);
BPath path(CayaAccountPath(signature));
path.Append(subsignature);
if (create_directory(path.Path(), 0755) != B_OK)
return NULL;
return path.Path();
}
extern "C" {
status_t

View File

@ -18,6 +18,7 @@ BResources* CayaResources();
const char* CayaAccountsPath();
const char* CayaAccountPath(const char* signature);
const char* CayaAccountPath(const char* signature, const char* subsignature);
extern "C" status_t our_image(image_info& image);

View File

@ -42,9 +42,21 @@ ProtocolManager::Init(BDirectory dir, BHandler* target)
// If add-on's API version fits then load accounts...
CayaProtocolAddOn* addOn = new CayaProtocolAddOn(id, path.Path());
if (addOn->Protocol()->Version() == CAYA_VERSION) {
fAddOnMap.AddItem(addOn->Signature(), addOn);
_GetAccounts(addOn, target);
if (addOn->Version() != CAYA_VERSION)
continue;
fAddOnMap.AddItem(addOn->Signature(), addOn);
_GetAccounts(addOn, addOn->Signature(), target);
// If add-on has multiple protocols, also load them
for (int32 i = 1; i < addOn->CountProtocols(); i++) {
CayaProtocolAddOn* subAddOn =
new CayaProtocolAddOn(id,path.Path(), i);
CayaProtocol* proto = subAddOn->Protocol();
fAddOnMap.AddItem(proto->Signature(), subAddOn);
_GetAccounts(subAddOn, proto->Signature(), target);
delete proto;
}
}
}
@ -122,10 +134,11 @@ ProtocolManager::AddAccount(CayaProtocolAddOn* addOn, const char* account,
void
ProtocolManager::_GetAccounts(CayaProtocolAddOn* addOn, BHandler* target)
ProtocolManager::_GetAccounts(CayaProtocolAddOn* addOn, const char* subProtocol,
BHandler* target)
{
// Find accounts path for this protocol
BPath path(CayaAccountPath(addOn->Signature()));
BPath path(CayaAccountPath(addOn->Signature(), subProtocol));
if (path.InitCheck() != B_OK)
return;
@ -144,3 +157,5 @@ ProtocolManager::_GetAccounts(CayaProtocolAddOn* addOn, BHandler* target)
}
}
}

View File

@ -42,6 +42,7 @@ private:
ProtocolManager();
void _GetAccounts(CayaProtocolAddOn* addOn,
const char* subProtocol,
BHandler* target);
AddOnMap fAddOnMap;

View File

@ -75,9 +75,12 @@ ProtocolSettings::Accounts() const
{
BObjectList<BString> list(true);
BPath path(CayaAccountPath(fAddOn->Signature()));
CayaProtocol* proto = fAddOn->Protocol();
BPath path(CayaAccountPath(fAddOn->Signature(), proto->Signature()));
if (path.InitCheck() != B_OK)
return list;
delete proto;
BDirectory dir(path.Path());
BEntry entry;

View File

@ -57,6 +57,7 @@ PreferencesAccounts::PreferencesAccounts()
fProtosMenu = new BPopUpMenu(NULL, true);
for (uint32 i = 0; i < pm->CountProtocolAddOns(); i++) {
CayaProtocolAddOn* addOn = pm->ProtocolAddOnAt(i);
CayaProtocol* proto = addOn->Protocol();
ProtocolSettings* settings = new ProtocolSettings(addOn);
// Add accounts to list view
@ -67,8 +68,9 @@ PreferencesAccounts::PreferencesAccounts()
msg->AddPointer("settings", settings);
BitmapMenuItem* item = new BitmapMenuItem(
addOn->FriendlySignature(), msg, addOn->Icon());
proto->FriendlySignature(), msg, proto->Icon());
fProtosMenu->AddItem(item);
delete proto;
}
ToolButton* proto = new ToolButton("Add", NULL);

View File

@ -1,36 +0,0 @@
/*
* Copyright 2010, Pier Luigi Fiorini. All rights reserved.
* Distributed under the terms of the GPL v2 License.
*
* Authors:
* Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
*/
#include <BeBuild.h>
#include "FacebookProtocol.h"
extern "C" _EXPORT CayaProtocol* protocol();
extern "C" _EXPORT const char* signature();
extern "C" _EXPORT const char* friendly_signature();
CayaProtocol*
protocol()
{
return (CayaProtocol*)new FacebookProtocol();
}
const char*
signature()
{
return kProtocolSignature;
}
const char*
friendly_signature()
{
return kProtocolName;
}

View File

@ -8,8 +8,9 @@
#include "FacebookProtocol.h"
const char* kProtocolSignature = "facebook";
const char* kProtocolName = "Facebook";
#include <Resources.h>
#include <libinterface/BitmapUtils.h>
FacebookProtocol::FacebookProtocol()
@ -23,10 +24,25 @@ FacebookProtocol::~FacebookProtocol()
}
void
FacebookProtocol::OverrideSettings()
const char*
FacebookProtocol::Signature() const
{
fServer = "chat.facebook.com";
return "facebook";
}
const char*
FacebookProtocol::FriendlySignature() const
{
return "Facebook";
}
BBitmap*
FacebookProtocol::Icon() const
{
BResources res(fPath.Path());
return IconFromResources(&res, 1, B_LARGE_ICON);
}
@ -37,6 +53,13 @@ FacebookProtocol::SettingsTemplate()
}
void
FacebookProtocol::OverrideSettings()
{
fServer = "chat.facebook.com";
}
BString
FacebookProtocol::ComposeJID() const
{

View File

@ -12,7 +12,14 @@ public:
FacebookProtocol();
virtual ~FacebookProtocol();
virtual const char* Signature() const;
virtual const char* FriendlySignature() const;
virtual BBitmap* Icon() const;
virtual BMessage SettingsTemplate();
virtual void OverrideSettings();
virtual BString ComposeJID() const;
};

View File

@ -1,36 +0,0 @@
/*
* Copyright 2010, Pier Luigi Fiorini. All rights reserved.
* Distributed under the terms of the GPL v2 License.
*
* Authors:
* Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
*/
#include <BeBuild.h>
#include "GoogleTalkProtocol.h"
extern "C" _EXPORT CayaProtocol* protocol();
extern "C" _EXPORT const char* signature();
extern "C" _EXPORT const char* friendly_signature();
CayaProtocol*
protocol()
{
return (CayaProtocol*)new GoogleTalkProtocol();
}
const char*
signature()
{
return kProtocolSignature;
}
const char*
friendly_signature()
{
return kProtocolName;
}

View File

@ -8,8 +8,9 @@
#include "GoogleTalkProtocol.h"
const char* kProtocolSignature = "gtalk";
const char* kProtocolName = "GoogleTalk";
#include <Resources.h>
#include <libinterface/BitmapUtils.h>
GoogleTalkProtocol::GoogleTalkProtocol()
@ -23,11 +24,25 @@ GoogleTalkProtocol::~GoogleTalkProtocol()
}
void
GoogleTalkProtocol::OverrideSettings()
const char*
GoogleTalkProtocol::Signature() const
{
fServer = "talk.google.com";
fPort = 0;
return "gtalk";
}
const char*
GoogleTalkProtocol::FriendlySignature() const
{
return "GTalk";
}
BBitmap*
GoogleTalkProtocol::Icon() const
{
BResources res(fPath.Path());
return IconFromResources(&res, 2, B_LARGE_ICON);
}
@ -38,6 +53,14 @@ GoogleTalkProtocol::SettingsTemplate()
}
void
GoogleTalkProtocol::OverrideSettings()
{
fServer = "talk.google.com";
fPort = 0;
}
BString
GoogleTalkProtocol::ComposeJID() const
{

View File

@ -12,8 +12,13 @@ public:
GoogleTalkProtocol();
virtual ~GoogleTalkProtocol();
virtual void OverrideSettings();
virtual const char* Signature() const;
virtual const char* FriendlySignature() const;
virtual BBitmap* Icon() const;
virtual BMessage SettingsTemplate();
virtual void OverrideSettings();
virtual BString ComposeJID() const;
};

View File

@ -142,17 +142,17 @@ JabberHandler::Shutdown()
}
const char*
JabberHandler::Signature() const
void
JabberHandler::SetPath(BPath path)
{
return kProtocolSignature;
fPath = path;
}
const char*
JabberHandler::FriendlySignature() const
BPath
JabberHandler::Path()
{
return kProtocolName;
return fPath;
}
@ -224,13 +224,6 @@ JabberHandler::MessengerInterface() const
}
uint32
JabberHandler::Version() const
{
return CAYA_VERSION_1_PRE_ALPHA_1;
}
gloox::Client*
JabberHandler::Client() const
{
@ -499,7 +492,7 @@ JabberHandler::_SendMessage(BMessage* msg)
if (!msg)
return;
msg->AddString("protocol", kProtocolSignature);
msg->AddString("protocol", Signature());
fServerMessenger->SendMessage(msg);
}
@ -554,7 +547,7 @@ JabberHandler::_SetupAvatarCache()
path.Append("Caya");
path.Append("Cache");
path.Append(kProtocolSignature);
path.Append(Signature());
if (create_directory(path.Path(), 0755) != B_OK)
return B_ERROR;
@ -983,7 +976,7 @@ JabberHandler::handleSelfPresence(const gloox::RosterItem& item, const std::stri
{
BMessage msg(IM_MESSAGE);
msg.AddInt32("im_what", IM_OWN_CONTACT_INFO);
msg.AddString("protocol", kProtocolSignature);
msg.AddString("protocol", Signature());
msg.AddString("id", item.jidJID().full().c_str());
msg.AddString("name", item.name().c_str());
msg.AddInt32("subscription", item.subscription());

View File

@ -47,8 +47,11 @@ public:
virtual status_t Shutdown();
virtual const char* Signature() const;
virtual const char* FriendlySignature() const;
virtual const char* Signature() const = 0;
virtual const char* FriendlySignature() const = 0;
virtual void SetPath(BPath path);
virtual BPath Path();
virtual status_t UpdateSettings(BMessage* msg);
@ -57,8 +60,6 @@ public:
virtual CayaProtocolMessengerInterface*
MessengerInterface() const;
virtual uint32 Version() const;
// Functions for gloox
gloox::Client* Client() const;
void HandleError(gloox::ConnectionError& e);
@ -74,6 +75,8 @@ protected:
BString fResource;
uint16 fPort;
BPath fPath;
BMessage _SettingsTemplate(const char* username, bool serverOption);
private:
CayaProtocolMessengerInterface*
@ -139,7 +142,4 @@ private:
gloox::StanzaError);
};
extern const char* kProtocolSignature;
extern const char* kProtocolName;
#endif // _JABBER_HANDLER_H

View File

@ -8,29 +8,58 @@
#include <BeBuild.h>
#include "FacebookProtocol.h"
#include "GoogleTalkProtocol.h"
#include "JabberProtocol.h"
extern "C" _EXPORT CayaProtocol* protocol();
extern "C" _EXPORT CayaProtocol* protocol_at(int32 i);
extern "C" _EXPORT int32 protocol_count();
extern "C" _EXPORT const char* signature();
extern "C" _EXPORT const char* friendly_signature();
extern "C" _EXPORT uint32 version();
CayaProtocol*
protocol()
protocol_at(int32 i)
{
return (CayaProtocol*)new JabberProtocol();
switch(i) {
case 0:
return (CayaProtocol*)new JabberProtocol();
case 1:
return (CayaProtocol*)new FacebookProtocol();
case 2:
return (CayaProtocol*)new GoogleTalkProtocol();
}
return NULL;
}
int32
protocol_count()
{
return 3;
}
const char*
signature()
{
return kProtocolSignature;
return "jabber";
}
const char*
friendly_signature()
{
return kProtocolName;
return "Jabber";
}
uint32
version()
{
return CAYA_VERSION_1_PRE_ALPHA_1;
}

View File

@ -8,9 +8,9 @@
#include "JabberProtocol.h"
#include <Resources.h>
const char* kProtocolSignature = "jabber";
const char* kProtocolName = "Jabber";
#include <libinterface/BitmapUtils.h>
JabberProtocol::JabberProtocol()
@ -24,9 +24,24 @@ JabberProtocol::~JabberProtocol()
}
void
JabberProtocol::OverrideSettings()
const char*
JabberProtocol::Signature() const
{
return "jabber";
}
const char*
JabberProtocol::FriendlySignature() const
{
return "Jabber";
}
BBitmap*
JabberProtocol::Icon() const
{
return ReadNodeIcon(fPath.Path(), B_LARGE_ICON, true);
}
@ -37,6 +52,12 @@ JabberProtocol::SettingsTemplate()
}
void
JabberProtocol::OverrideSettings()
{
}
BString
JabberProtocol::ComposeJID() const
{

View File

@ -12,8 +12,14 @@ public:
JabberProtocol();
virtual ~JabberProtocol();
virtual void OverrideSettings();
virtual const char* Signature() const;
virtual const char* FriendlySignature() const;
virtual BBitmap* Icon() const;
virtual BMessage SettingsTemplate();
virtual void OverrideSettings();
virtual BString ComposeJID() const;
};

View File

@ -1,21 +0,0 @@
resource app_version {
major = 0,
middle = 0,
minor = 1,
variety = B_APPV_ALPHA,
internal = 0,
short_info = "Facebook Protocol AddOn for Caya",
long_info = "©2010 Pier Luigi Fiorini"
};
resource vector_icon {
$"6E6369660202000602000000C000004000000000009220244C000067003C7FFF"
$"1A5EA805FF0206111B1111BD01C0A72B4828BF72B8033FBBBA3C39463F5C4A46"
$"523C4ABC4D4ABB7A4ABC4DC3D234C59634C51C34C659BB0E54352C0208242824"
$"2624BA4724C94924C528245AB6245C265CBA355CC94B5CC5285C5A5C5CC9565C"
$"5A5CC5485CB64A5CBA7B5C26C951245A24C55D242824BA53242624020A000101"
$"000A01010000"
};

View File

@ -1,34 +0,0 @@
resource app_version {
major = 0,
middle = 0,
minor = 1,
variety = B_APPV_ALPHA,
internal = 0,
short_info = "GoogleTalk Protocol AddOn for Caya",
long_info = "©2010 Pier Luigi Fiorini"
};
resource vector_icon {
$"6E636966080501040046020106023E40000000000000003D4000494000470000"
$"7EFFFFFFFFE5E1DA02000602000000BBC0004000000000009220244AF0000000"
$"33CCFC3366FF02000602000000BA000040000000000092202448800000336699"
$"FF6699CC02000602000000B9000040000000000092202448E00000CC0000FFFF"
$"000002000602000000BA000040000000000092202448800000FF9900FFFBFF00"
$"02000602000000BA000040000000000092202448800000006600FF00CC000A02"
$"06C22622C7562239222E342E2B2E3D4146364441483C50404C3C504A444A4E55"
$"44CBB634CBB83E5E2A0206C22622C7562239222E342E2B2E3D4146364441483C"
$"50404C3C504C464A505744CBB634CBB83E5E2A02024C265928532A583B59335D"
$"350610CAFFFEAF375335543B3B5A3B5A395D325D355D2C5D274F275627483241"
$"2C413541BDA7C2A83942BDA7C2A8394A3F463F463C40324036402A40234F2346"
$"2358325E2A5E395EBF5C5A3F5CBF5C5A3F544053080234313C310404FE372C37"
$"393739373A393B383B3A3B3B393B3A3B390406FE0B4536403640363F363E383E"
$"373E383E393E393E3A403B3F3B413B453A0405FE03453C453445344533433244"
$"324332403240323F323E343E333E3408024D2C4D3C0803553C4F3655300D0A00"
$"01001001178400040A020101000A010102000A0101032021210A010204053021"
$"2101178200040A0102070630212101178200040A010108301D2101178200040A"
$"0102090830212101178200040A030103000A040204051001178200040A050207"
$"061001178200040A060108301C2001178200040A07020908100117820004"
};

View File

@ -54,3 +54,35 @@ resource vector_icon {
$"4640053FFFFB01178000040A030110023EECA00000000000003EECA04640053F"
$"FFFB0A010111023EECA00000000000003EECA04640053FFFFB"
};
resource(1, "facebook") #'VICN' array {
$"6E6369660202000602000000C000004000000000009220244C000067003C7FFF"
$"1A5EA805FF0206111B1111BD01C0A72B4828BF72B8033FBBBA3C39463F5C4A46"
$"523C4ABC4D4ABB7A4ABC4DC3D234C59634C51C34C659BB0E54352C0208242824"
$"2624BA4724C94924C528245AB6245C265CBA355CC94B5CC5285C5A5C5CC9565C"
$"5A5CC5485CB64A5CBA7B5C26C951245A24C55D242824BA53242624020A000101"
$"000A01010000"
};
resource(2, "gtalk") #'VICN' array {
$"6E636966080501040046020106023E40000000000000003D4000494000470000"
$"7EFFFFFFFFE5E1DA02000602000000BBC0004000000000009220244AF0000000"
$"33CCFC3366FF02000602000000BA000040000000000092202448800000336699"
$"FF6699CC02000602000000B9000040000000000092202448E00000CC0000FFFF"
$"000002000602000000BA000040000000000092202448800000FF9900FFFBFF00"
$"02000602000000BA000040000000000092202448800000006600FF00CC000A02"
$"06C22622C7562239222E342E2B2E3D4146364441483C50404C3C504A444A4E55"
$"44CBB634CBB83E5E2A0206C22622C7562239222E342E2B2E3D4146364441483C"
$"50404C3C504C464A505744CBB634CBB83E5E2A02024C265928532A583B59335D"
$"350610CAFFFEAF375335543B3B5A3B5A395D325D355D2C5D274F275627483241"
$"2C413541BDA7C2A83942BDA7C2A8394A3F463F463C40324036402A40234F2346"
$"2358325E2A5E395EBF5C5A3F5CBF5C5A3F544053080234313C310404FE372C37"
$"393739373A393B383B3A3B3B393B3A3B390406FE0B4536403640363F363E383E"
$"373E383E393E393E3A403B3F3B413B453A0405FE03453C453445344533433244"
$"324332403240323F323E343E333E3408024D2C4D3C0803553C4F3655300D0A00"
$"01001001178400040A020101000A010102000A0101032021210A010204053021"
$"2101178200040A0102070630212101178200040A010108301D2101178200040A"
$"0102090830212101178200040A030103000A040204051001178200040A050207"
$"061001178200040A060108301C2001178200040A07020908100117820004"
};