Use typed identity in module manager

This commit is contained in:
Marvin W 2017-03-10 21:45:56 +01:00
parent 9cdc3619bd
commit d8e102a160
No known key found for this signature in database
GPG key ID: 072E9235DB996F2A
10 changed files with 79 additions and 90 deletions

View file

@ -89,10 +89,10 @@ public class AvatarManager : StreamInteractionModule, Object {
} }
private void on_account_added(Account account) { private void on_account_added(Account account) {
stream_interactor.module_manager.user_avatars_modules[account].received_avatar.connect((stream, jid, id) => stream_interactor.module_manager.get_module(account, Xep.UserAvatars.Module.IDENTITY).received_avatar.connect((stream, jid, id) =>
on_user_avatar_received(account, new Jid(jid), id) on_user_avatar_received(account, new Jid(jid), id)
); );
stream_interactor.module_manager.vcard_modules[account].received_avatar.connect((stream, jid, id) => stream_interactor.module_manager.get_module(account, Xep.VCard.Module.IDENTITY).received_avatar.connect((stream, jid, id) =>
on_vcard_avatar_received(account, new Jid(jid), id) on_vcard_avatar_received(account, new Jid(jid), id)
); );

View file

@ -44,13 +44,13 @@ public class CounterpartInteractionManager : StreamInteractionModule, Object {
} }
private void on_account_added(Account account) { private void on_account_added(Account account) {
stream_interactor.module_manager.chat_markers_modules[account].marker_received.connect( (stream, jid, marker, id) => { stream_interactor.module_manager.get_module(account, Xep.ChatMarkers.Module.IDENTITY).marker_received.connect( (stream, jid, marker, id) => {
on_chat_marker_received(account, new Jid(jid), marker, id); on_chat_marker_received(account, new Jid(jid), marker, id);
}); });
stream_interactor.module_manager.message_delivery_receipts_modules[account].receipt_received.connect((stream, jid, id) => { stream_interactor.module_manager.get_module(account, Xep.MessageDeliveryReceipts.Module.IDENTITY).receipt_received.connect((stream, jid, id) => {
on_receipt_received(account, new Jid(jid), id); on_receipt_received(account, new Jid(jid), id);
}); });
stream_interactor.module_manager.chat_state_notifications_modules[account].chat_state_received.connect((stream, jid, state) => { stream_interactor.module_manager.get_module(account, Xep.ChatStateNotifications.Module.IDENTITY).chat_state_received.connect((stream, jid, state) => {
on_chat_state_received(account, new Jid(jid), state); on_chat_state_received(account, new Jid(jid), state);
}); });
} }

View file

@ -75,7 +75,7 @@ public class MessageManager : StreamInteractionModule, Object {
} }
private void on_account_added(Account account) { private void on_account_added(Account account) {
stream_interactor.module_manager.message_modules[account].received_message.connect( (stream, message) => { stream_interactor.module_manager.get_module(account, Xmpp.Message.Module.IDENTITY).received_message.connect( (stream, message) => {
on_message_received(account, message); on_message_received(account, message);
}); });
stream_interactor.stream_negotiated.connect(send_unsent_messages); stream_interactor.stream_negotiated.connect(send_unsent_messages);

View file

@ -6,90 +6,76 @@ using Xmpp;
namespace Dino { namespace Dino {
public class ModuleManager { public class ModuleManager {
private HashMap<Account, ArrayList<Core.XmppStreamModule>> module_map = new HashMap<Account, ArrayList<Core.XmppStreamModule>>();
public HashMap<Account, Tls.Module> tls_modules = new HashMap<Account, Tls.Module>();
public HashMap<Account, PlainSasl.Module> plain_sasl_modules = new HashMap<Account, PlainSasl.Module>();
public HashMap<Account, Bind.Module> bind_modules = new HashMap<Account, Bind.Module>();
public HashMap<Account, Roster.Module> roster_modules = new HashMap<Account, Roster.Module>();
public HashMap<Account, Xep.ServiceDiscovery.Module> service_discovery_modules = new HashMap<Account, Xep.ServiceDiscovery.Module>();
public HashMap<Account, Xep.PrivateXmlStorage.Module> private_xmp_storage_modules = new HashMap<Account, Xep.PrivateXmlStorage.Module>();
public HashMap<Account, Xep.Bookmarks.Module> bookmarks_module = new HashMap<Account, Xep.Bookmarks.Module>();
public HashMap<Account, Presence.Module> presence_modules = new HashMap<Account, Presence.Module>();
public HashMap<Account, Xmpp.Message.Module> message_modules = new HashMap<Account, Xmpp.Message.Module>();
public HashMap<Account, Xep.MessageCarbons.Module> message_carbons_modules = new HashMap<Account, Xep.MessageCarbons.Module>();
public HashMap<Account, Xep.Muc.Module> muc_modules = new HashMap<Account, Xep.Muc.Module>();
public HashMap<Account, Xep.Pgp.Module> pgp_modules = new HashMap<Account, Xep.Pgp.Module>();
public HashMap<Account, Xep.Pubsub.Module> pubsub_modules = new HashMap<Account, Xep.Pubsub.Module>();
public HashMap<Account, Xep.EntityCapabilities.Module> entity_capabilities_modules = new HashMap<Account, Xep.EntityCapabilities.Module>();
public HashMap<Account, Xep.UserAvatars.Module> user_avatars_modules = new HashMap<Account, Xep.UserAvatars.Module>();
public HashMap<Account, Xep.VCard.Module> vcard_modules = new HashMap<Account, Xep.VCard.Module>();
public HashMap<Account, Xep.MessageDeliveryReceipts.Module> message_delivery_receipts_modules = new HashMap<Account, Xep.MessageDeliveryReceipts.Module>();
public HashMap<Account, Xep.ChatStateNotifications.Module> chat_state_notifications_modules = new HashMap<Account, Xep.ChatStateNotifications.Module>();
public HashMap<Account, Xep.ChatMarkers.Module> chat_markers_modules = new HashMap<Account, Xep.ChatMarkers.Module>();
public HashMap<Account, Xep.Ping.Module> ping_modules = new HashMap<Account, Xep.Ping.Module>();
public HashMap<Account, Xep.DelayedDelivery.Module> delayed_delivery_module = new HashMap<Account, Xep.DelayedDelivery.Module>();
public HashMap<Account, StreamError.Module> stream_error_modules = new HashMap<Account, StreamError.Module>();
private AvatarStorage avatar_storage = new AvatarStorage("./"); private AvatarStorage avatar_storage = new AvatarStorage("./");
private EntityCapabilitiesStorage entity_capabilities_storage; private EntityCapabilitiesStorage entity_capabilities_storage;
public signal void initialize_account_modules(Account account, ArrayList<Core.XmppStreamModule> modules);
public ModuleManager(Database db) { public ModuleManager(Database db) {
entity_capabilities_storage = new EntityCapabilitiesStorage(db); entity_capabilities_storage = new EntityCapabilitiesStorage(db);
} }
public T? get_module<T>(Account account, Core.ModuleIdentity<T> identity) {
if (identity == null) return null;
lock (module_map) {
if (!module_map.has_key(account)) {
initialize(account);
}
var res = module_map[account].filter((module) => identity.matches(module));
if (res != null && res.next()) {
return identity.cast(res.get());
}
}
return null;
}
public ArrayList<Core.XmppStreamModule> get_modules(Account account, string? resource = null) { public ArrayList<Core.XmppStreamModule> get_modules(Account account, string? resource = null) {
ArrayList<Core.XmppStreamModule> modules = new ArrayList<Core.XmppStreamModule>(); ArrayList<Core.XmppStreamModule> modules = new ArrayList<Core.XmppStreamModule>();
lock (module_map) {
if (!tls_modules.has_key(account)) add_account(account); if (!module_map.has_key(account)) {
initialize(account);
modules.add(tls_modules[account]); }
modules.add(plain_sasl_modules[account]); foreach (Core.XmppStreamModule module in module_map[account]) {
modules.add(new Bind.Module(resource == null ? account.resourcepart : resource)); if (Bind.Module.IDENTITY.matches(module)) {
modules.add(roster_modules[account]); // TODO: argh?!
modules.add(service_discovery_modules[account]); modules.add(new Bind.Module(resource == null ? account.resourcepart : resource));
modules.add(private_xmp_storage_modules[account]); } else {
modules.add(bookmarks_module[account]); modules.add(module);
modules.add(presence_modules[account]); }
modules.add(message_modules[account]); }
modules.add(message_carbons_modules[account]); }
modules.add(muc_modules[account]);
modules.add(pgp_modules[account]);
modules.add(pubsub_modules[account]);
modules.add(entity_capabilities_modules[account]);
modules.add(user_avatars_modules[account]);
modules.add(vcard_modules[account]);
modules.add(message_delivery_receipts_modules[account]);
modules.add(chat_state_notifications_modules[account]);
modules.add(chat_markers_modules[account]);
modules.add(ping_modules[account]);
modules.add(delayed_delivery_module[account]);
modules.add(stream_error_modules[account]);
return modules; return modules;
} }
public void add_account(Account account) { public void initialize(Account account) {
tls_modules[account] = new Tls.Module(); lock(module_map) {
plain_sasl_modules[account] = new PlainSasl.Module(account.bare_jid.to_string(), account.password); module_map[account] = new ArrayList<Core.XmppStreamModule>();
bind_modules[account] = new Bind.Module(account.resourcepart); module_map[account].add(new Tls.Module());
roster_modules[account] = new Roster.Module(); module_map[account].add(new PlainSasl.Module(account.bare_jid.to_string(), account.password));
service_discovery_modules[account] = new Xep.ServiceDiscovery.Module.with_identity("client", "pc"); module_map[account].add(new Bind.Module(account.resourcepart));
private_xmp_storage_modules[account] = new Xep.PrivateXmlStorage.Module(); module_map[account].add(new Roster.Module());
bookmarks_module[account] = new Xep.Bookmarks.Module(); module_map[account].add(new Xep.ServiceDiscovery.Module.with_identity("client", "pc"));
presence_modules[account] = new Presence.Module(); module_map[account].add(new Xep.PrivateXmlStorage.Module());
message_modules[account] = new Xmpp.Message.Module(); module_map[account].add(new Xep.Bookmarks.Module());
message_carbons_modules[account] = new Xep.MessageCarbons.Module(); module_map[account].add(new Presence.Module());
muc_modules[account] = new Xep.Muc.Module(); module_map[account].add(new Xmpp.Message.Module());
pgp_modules[account] = new Xep.Pgp.Module(); module_map[account].add(new Xep.MessageCarbons.Module());
pubsub_modules[account] = new Xep.Pubsub.Module(); module_map[account].add(new Xep.Muc.Module());
entity_capabilities_modules[account] = new Xep.EntityCapabilities.Module(entity_capabilities_storage); module_map[account].add(new Xep.Pgp.Module());
user_avatars_modules[account] = new Xep.UserAvatars.Module(avatar_storage); module_map[account].add(new Xep.Pubsub.Module());
vcard_modules[account] = new Xep.VCard.Module(avatar_storage); module_map[account].add(new Xep.EntityCapabilities.Module(entity_capabilities_storage));
message_delivery_receipts_modules[account] = new Xep.MessageDeliveryReceipts.Module(); module_map[account].add(new Xep.UserAvatars.Module(avatar_storage));
chat_state_notifications_modules[account] = new Xep.ChatStateNotifications.Module(); module_map[account].add(new Xep.VCard.Module(avatar_storage));
chat_markers_modules[account] = new Xep.ChatMarkers.Module(); module_map[account].add(new Xep.MessageDeliveryReceipts.Module());
ping_modules[account] = new Xep.Ping.Module(); module_map[account].add(new Xep.ChatStateNotifications.Module());
delayed_delivery_module[account] = new Xep.DelayedDelivery.Module(); module_map[account].add(new Xep.ChatMarkers.Module());
stream_error_modules[account] = new StreamError.Module(); module_map[account].add(new Xep.Ping.Module());
module_map[account].add(new Xep.DelayedDelivery.Module());
module_map[account].add(new StreamError.Module());
initialize_account_modules(account, module_map[account]);
}
} }
} }

View file

@ -144,10 +144,10 @@ public class MucManager : StreamInteractionModule, Object {
} }
private void on_account_added(Account account) { private void on_account_added(Account account) {
stream_interactor.module_manager.muc_modules[account].subject_set.connect( (stream, subject, jid) => { stream_interactor.module_manager.get_module(account, Xep.Muc.Module.IDENTITY).subject_set.connect( (stream, subject, jid) => {
on_subject_set(account, new Jid(jid), subject); on_subject_set(account, new Jid(jid), subject);
}); });
stream_interactor.module_manager.bookmarks_module[account].conferences_updated.connect( (stream, conferences) => { stream_interactor.module_manager.get_module(account, Xep.Bookmarks.Module.IDENTITY).conferences_updated.connect( (stream, conferences) => {
bookmarks_updated(account, conferences); bookmarks_updated(account, conferences);
}); });
} }

View file

@ -1,5 +1,6 @@
using Gee; using Gee;
using Xmpp;
using Dino.Entities; using Dino.Entities;
namespace Dino { namespace Dino {
@ -37,7 +38,7 @@ namespace Dino {
} }
private void on_account_added(Account account) { private void on_account_added(Account account) {
stream_interactor.module_manager.pgp_modules[account].received_jid_key_id.connect((stream, jid, key_id) => { stream_interactor.module_manager.get_module(account, Xep.Pgp.Module.IDENTITY).received_jid_key_id.connect((stream, jid, key_id) => {
on_jid_key_received(account, new Jid(jid), key_id); on_jid_key_received(account, new Jid(jid), key_id);
}); });
} }

View file

@ -79,13 +79,13 @@ public class PresenceManager : StreamInteractionModule, Object {
} }
private void on_account_added(Account account) { private void on_account_added(Account account) {
stream_interactor.module_manager.presence_modules[account].received_available_show.connect((stream, jid, show) => stream_interactor.module_manager.get_module(account, Presence.Module.IDENTITY).received_available_show.connect((stream, jid, show) =>
on_received_available_show(account, new Jid(jid), show) on_received_available_show(account, new Jid(jid), show)
); );
stream_interactor.module_manager.presence_modules[account].received_unavailable.connect((stream, jid) => stream_interactor.module_manager.get_module(account, Presence.Module.IDENTITY).received_unavailable.connect((stream, jid) =>
on_received_unavailable(account, new Jid(jid)) on_received_unavailable(account, new Jid(jid))
); );
stream_interactor.module_manager.presence_modules[account].received_subscription_request.connect((stream, jid) => stream_interactor.module_manager.get_module(account, Presence.Module.IDENTITY).received_subscription_request.connect((stream, jid) =>
received_subscription_request(new Jid(jid), account) received_subscription_request(new Jid(jid), account)
); );
} }

View file

@ -58,13 +58,13 @@ namespace Dino {
} }
private void on_account_added(Account account) { private void on_account_added(Account account) {
stream_interactor.module_manager.roster_modules[account].received_roster.connect( (stream, roster) => { stream_interactor.module_manager.get_module(account, Roster.Module.IDENTITY).received_roster.connect( (stream, roster) => {
on_roster_received(account, roster); on_roster_received(account, roster);
}); });
stream_interactor.module_manager.roster_modules[account].item_removed.connect( (stream, roster_item) => { stream_interactor.module_manager.get_module(account, Roster.Module.IDENTITY).item_removed.connect( (stream, roster_item) => {
removed_roster_item(account, new Jid(roster_item.jid), roster_item); removed_roster_item(account, new Jid(roster_item.jid), roster_item);
}); });
stream_interactor.module_manager.roster_modules[account].item_updated.connect( (stream, roster_item) => { stream_interactor.module_manager.get_module(account, Roster.Module.IDENTITY).item_updated.connect( (stream, roster_item) => {
on_roster_item_updated(account, roster_item); on_roster_item_updated(account, roster_item);
}); });
} }

View file

@ -22,7 +22,7 @@ public class StreamInteractor {
} }
public void connect(Account account) { public void connect(Account account) {
module_manager.add_account(account); module_manager.initialize(account);
account_added(account); account_added(account);
connection_manager.connect(account); connection_manager.connect(account);
} }

View file

@ -129,9 +129,7 @@ public class XmppStream {
public T? get_module<T>(ModuleIdentity<T>? identity) { public T? get_module<T>(ModuleIdentity<T>? identity) {
if (identity == null) return null; if (identity == null) return null;
foreach (var module in modules) { foreach (var module in modules) {
if (module.get_ns() == identity.ns && module.get_id() == identity.id) { if (identity.matches(module)) return identity.cast(module);
return module;
}
} }
return null; return null;
} }
@ -244,6 +242,10 @@ public class ModuleIdentity<T> : Object {
public T? cast(XmppStreamModule module) { public T? cast(XmppStreamModule module) {
return (T?) module; return (T?) module;
} }
public bool matches(XmppStreamModule module) {
return module.get_ns() == ns && module.get_id() == id;
}
} }
public abstract class XmppStreamModule : Object { public abstract class XmppStreamModule : Object {