aboutsummaryrefslogtreecommitdiff
path: root/libdino/src/service/stream_interactor.vala
blob: 81ddfebd306e09d461e6ad91211a11af19af249a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using Gee;

using Xmpp;
using Dino.Entities;

namespace Dino {

public class StreamInteractor {

    public signal void account_added(Account account);
    public signal void account_removed(Account account);
    public signal void stream_negotiated(Account account);

    public ModuleManager module_manager;
    public ConnectionManager connection_manager;
    private ArrayList<StreamInteractionModule> modules = new ArrayList<StreamInteractionModule>();

    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) {
        module_manager.initialize(account);
        account_added(account);
        connection_manager.connect(account);
    }

    public void disconnect(Account account) {
        connection_manager.disconnect(account);
        account_removed(account);
    }

    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 Core.XmppStream? get_stream(Account account) {
        return connection_manager.get_stream(account);
    }

    public void add_module(StreamInteractionModule module) {
        modules.add(module);
    }

    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);
        }
        return null;
    }

    private void on_stream_opened(Account account, Core.XmppStream stream) {
        stream.stream_negotiated.connect( (stream) => {
            stream_negotiated(account);
        });
    }
}

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;
    }
}

public interface StreamInteractionModule : Object {
    public abstract string id { get; }
}

}