2017-03-02 15:37:32 +01:00
|
|
|
using Gee;
|
|
|
|
|
|
|
|
using Xmpp;
|
|
|
|
using Dino.Entities;
|
|
|
|
|
|
|
|
namespace Dino {
|
2017-03-09 15:34:32 +01:00
|
|
|
|
2018-11-16 16:27:31 +01:00
|
|
|
public class StreamInteractor : Object {
|
2017-03-02 15:37:32 +01:00
|
|
|
|
|
|
|
public signal void account_added(Account account);
|
2017-03-17 23:21:23 +01:00
|
|
|
public signal void account_removed(Account account);
|
2020-08-14 16:42:56 +02:00
|
|
|
public signal void stream_resumed(Account account, XmppStream stream);
|
2018-01-12 21:03:09 +01:00
|
|
|
public signal void stream_negotiated(Account account, XmppStream stream);
|
2019-12-28 03:11:51 +01:00
|
|
|
public signal void stream_attached_modules(Account account, XmppStream stream);
|
2017-03-02 15:37:32 +01:00
|
|
|
|
|
|
|
public ModuleManager module_manager;
|
|
|
|
public ConnectionManager connection_manager;
|
2017-03-19 12:55:36 +01:00
|
|
|
private ArrayList<StreamInteractionModule> modules = new ArrayList<StreamInteractionModule>();
|
2017-03-02 15:37:32 +01:00
|
|
|
|
|
|
|
public StreamInteractor(Database db) {
|
2020-03-29 20:23:47 +02:00
|
|
|
module_manager = new ModuleManager();
|
2017-03-02 15:37:32 +01:00
|
|
|
connection_manager = new ConnectionManager(module_manager);
|
|
|
|
|
|
|
|
connection_manager.stream_opened.connect(on_stream_opened);
|
2019-12-28 03:11:51 +01:00
|
|
|
connection_manager.stream_attached_modules.connect((account, stream) => {
|
|
|
|
stream_attached_modules(account, stream);
|
|
|
|
});
|
2017-03-02 15:37:32 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 16:27:31 +01:00
|
|
|
public void connect_account(Account account) {
|
2017-03-10 21:45:56 +01:00
|
|
|
module_manager.initialize(account);
|
2017-03-02 15:37:32 +01:00
|
|
|
account_added(account);
|
2019-03-14 17:38:48 +01:00
|
|
|
connection_manager.connect_account(account);
|
2017-03-02 15:37:32 +01:00
|
|
|
}
|
|
|
|
|
2019-11-27 18:46:29 +01:00
|
|
|
public async void disconnect_account(Account account) {
|
|
|
|
yield connection_manager.disconnect_account(account);
|
2017-03-17 23:21:23 +01:00
|
|
|
account_removed(account);
|
2017-03-02 15:37:32 +01: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;
|
|
|
|
}
|
|
|
|
|
2018-01-12 21:03:09 +01:00
|
|
|
public XmppStream? get_stream(Account account) {
|
2017-03-02 15:37:32 +01:00
|
|
|
return connection_manager.get_stream(account);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void add_module(StreamInteractionModule module) {
|
2017-03-19 12:55:36 +01:00
|
|
|
modules.add(module);
|
2017-03-02 15:37:32 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 12:55:36 +01: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 15:37:32 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-11-18 22:45:18 +01:00
|
|
|
public new T? get<T>() {
|
2019-10-18 16:52:29 +02:00
|
|
|
foreach (StreamInteractionModule module in modules) {
|
|
|
|
if (module.get_type() == typeof(T)) return (T?) module;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-12 21:03:09 +01:00
|
|
|
private void on_stream_opened(Account account, XmppStream stream) {
|
2017-03-02 15:37:32 +01:00
|
|
|
stream.stream_negotiated.connect( (stream) => {
|
2020-07-15 15:08:56 +02:00
|
|
|
var flag = stream.get_flag(Xep.StreamManagement.Flag.IDENTITY);
|
|
|
|
if (flag == null || flag.resumed == false) {
|
|
|
|
stream_negotiated(account, stream);
|
2020-08-14 16:42:56 +02:00
|
|
|
} else if (flag != null && flag.resumed == true) {
|
|
|
|
stream_resumed(account, stream);
|
2020-07-15 15:08:56 +02:00
|
|
|
}
|
2017-03-02 15:37:32 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 12:55:36 +01:00
|
|
|
public class ModuleIdentity<T> : Object {
|
|
|
|
public string id { get; private set; }
|
|
|
|
|
|
|
|
public ModuleIdentity(string id) {
|
|
|
|
this.id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public T? cast(StreamInteractionModule module) {
|
2017-03-20 22:12:20 +01:00
|
|
|
return module.get_type().is_a(typeof(T)) ? (T?) module : null;
|
2017-03-19 12:55:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool matches(StreamInteractionModule module) {
|
2019-06-01 17:09:26 +02:00
|
|
|
return module.id == id;
|
2017-03-19 12:55:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 15:37:32 +01:00
|
|
|
public interface StreamInteractionModule : Object {
|
2017-03-19 12:55:36 +01:00
|
|
|
public abstract string id { get; }
|
2017-03-02 15:37:32 +01:00
|
|
|
}
|
2017-03-09 15:34:32 +01:00
|
|
|
|
2017-08-12 23:14:50 +02:00
|
|
|
}
|