anotherim-desktop/libdino/src/service/stream_interactor.vala

89 lines
2.5 KiB
Vala
Raw Normal View History

2017-03-02 14:37:32 +00:00
using Gee;
using Xmpp;
using Dino.Entities;
namespace Dino {
public class StreamInteractor : Object {
2017-03-02 14:37:32 +00:00
public signal void account_added(Account account);
public signal void account_removed(Account account);
public signal void stream_negotiated(Account account, XmppStream stream);
public signal void attached_modules(Account account, XmppStream stream);
2017-03-02 14:37:32 +00:00
public ModuleManager module_manager;
public ConnectionManager connection_manager;
private ArrayList<StreamInteractionModule> modules = new ArrayList<StreamInteractionModule>();
2017-03-02 14:37:32 +00:00
public StreamInteractor(Database db) {
module_manager = new ModuleManager(db);
connection_manager = new ConnectionManager(module_manager);
connection_manager.stream_opened.connect(on_stream_opened);
}
public void connect_account(Account account) {
2017-03-10 20:45:56 +00:00
module_manager.initialize(account);
2017-03-02 14:37:32 +00:00
account_added(account);
connection_manager.connect_account(account);
2017-03-02 14:37:32 +00:00
}
public void disconnect_account(Account account) {
connection_manager.disconnect_account(account);
account_removed(account);
2017-03-02 14:37:32 +00:00
}
public ArrayList<Account> get_accounts() {
ArrayList<Account> ret = new ArrayList<Account>(Account.equals_func);
foreach (Account account in connection_manager.get_managed_accounts()) {
ret.add(account);
}
return ret;
}
public XmppStream? get_stream(Account account) {
2017-03-02 14:37:32 +00:00
return connection_manager.get_stream(account);
}
public void add_module(StreamInteractionModule module) {
modules.add(module);
2017-03-02 14:37:32 +00:00
}
public T? get_module<T>(ModuleIdentity<T>? identity) {
if (identity == null) return null;
foreach (StreamInteractionModule module in modules) {
if (identity.matches(module)) return identity.cast(module);
2017-03-02 14:37:32 +00:00
}
return null;
}
private void on_stream_opened(Account account, XmppStream stream) {
2017-03-02 14:37:32 +00:00
stream.stream_negotiated.connect( (stream) => {
stream_negotiated(account, stream);
2017-03-02 14:37:32 +00:00
});
}
}
public class ModuleIdentity<T> : Object {
public string id { get; private set; }
public ModuleIdentity(string id) {
this.id = id;
}
public T? cast(StreamInteractionModule module) {
return module.get_type().is_a(typeof(T)) ? (T?) module : null;
}
public bool matches(StreamInteractionModule module) {
return module.id == id;
}
}
2017-03-02 14:37:32 +00:00
public interface StreamInteractionModule : Object {
public abstract string id { get; }
2017-03-02 14:37:32 +00:00
}
2017-08-12 21:14:50 +00:00
}