aboutsummaryrefslogtreecommitdiff
path: root/plugins/http-files/src/manager.vala
blob: f398b70034cb2a2ee35a0eb56ed07982fe32acd1 (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
using Dino.Entities;
using Xmpp;
using Gee;

namespace Dino.Plugins.HttpFiles {

public class Manager : StreamInteractionModule, Object {
    public static ModuleIdentity<Manager> IDENTITY = new ModuleIdentity<Manager>("http_files");
    public string id { get { return IDENTITY.id; } }

    public signal void upload_available(Account account);

    private StreamInteractor stream_interactor;
    private HashMap<Account, int?> max_file_sizes = new HashMap<Account, int?>(Account.hash_func, Account.equals_func);

    private Manager(StreamInteractor stream_interactor) {
        this.stream_interactor = stream_interactor;

        stream_interactor.stream_negotiated.connect(on_stream_negotiated);
    }

    public void send(Conversation conversation, string file_uri) {
        Xmpp.Core.XmppStream? stream = stream_interactor.get_stream(conversation.account);
        if (stream != null) {
            stream_interactor.module_manager.get_module(conversation.account, UploadStreamModule.IDENTITY).upload(stream, file_uri,
                (stream, url_down) => {
                    stream_interactor.get_module(MessageProcessor.IDENTITY).send_message(url_down, conversation);
                },
                () => {}
            );

        }
    }

    public bool is_upload_available(Account account) {
        lock (max_file_sizes) {
            return max_file_sizes.has_key(account);
        }
    }

    public int? get_max_file_size(Account account) {
        lock (max_file_sizes) {
            return max_file_sizes[account];
        }
    }

    private void on_stream_negotiated(Account account, Core.XmppStream stream) {
        stream_interactor.module_manager.get_module(account, UploadStreamModule.IDENTITY).feature_available.connect((stream, max_file_size) => {
            lock (max_file_sizes) {
                max_file_sizes[account] = max_file_size;
            }
            upload_available(account);
        });
    }

    public static void start(StreamInteractor stream_interactor) {
        Manager m = new Manager(stream_interactor);
        stream_interactor.add_module(m);
    }
}

}