aboutsummaryrefslogtreecommitdiff
path: root/main/src/ui/conversation_titlebar/file_entry.vala
blob: cb7d0807278a8fcffd6ce61ca45262bc4cd56df5 (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
using Gtk;

using Dino.Entities;

namespace Dino.Ui {

public class FileEntry : Plugins.ConversationTitlebarEntry, Object {
    public string id { get { return "send_files"; } }

    StreamInteractor stream_interactor;

    public FileEntry(StreamInteractor stream_interactor) {
        this.stream_interactor = stream_interactor;
    }

    public double order { get { return 4; } }
    public Plugins.ConversationTitlebarWidget? get_widget(Plugins.WidgetType type) {
        if (type == Plugins.WidgetType.GTK) {
            return new FileWidget(stream_interactor) { visible=true };
        }
        return null;
    }
}

public class FileWidget : Button, Plugins.ConversationTitlebarWidget {

    private Conversation? conversation;
    private StreamInteractor stream_interactor;

    public FileWidget(StreamInteractor stream_interactor) {
        this.stream_interactor = stream_interactor;
        image = new Image.from_icon_name("mail-attachment-symbolic", IconSize.MENU);
        clicked.connect(on_clicked);
        stream_interactor.get_module(FileManager.IDENTITY).upload_available.connect(on_upload_available);
    }

    public void on_clicked() {
        FileChooserNative chooser = new FileChooserNative (
                "Select file", get_toplevel() as Window, FileChooserAction.OPEN,
                "Select", "Cancel");
//        long max_file_size = stream_interactor.get_module(Manager.IDENTITY).get_max_file_size(conversation.account);
//        if (max_file_size != -1) {
//            FileFilter filter = new FileFilter();
//            filter.add_custom(FileFilterFlags.URI, (filter_info) => {
//                File file = File.new_for_uri(filter_info.uri);
//                FileInfo file_info = file.query_info("*", FileQueryInfoFlags.NONE);
//                return file_info.get_size() <= max_file_size;
//            });
//            chooser.set_filter(filter);
//        }
        if (chooser.run() == Gtk.ResponseType.ACCEPT) {
            string uri = chooser.get_filename();
            stream_interactor.get_module(FileManager.IDENTITY).send_file(uri, conversation);
        }
    }

    public void on_upload_available(Account account) {
        Idle.add(() => {
            if (conversation != null && conversation.account.equals(account)) {
                visible = true;
            }
            return false;
        });
    }

    public new void set_conversation(Conversation conversation) {
        this.conversation = conversation;
        visible = stream_interactor.get_module(FileManager.IDENTITY).is_upload_available(conversation);
    }
}

}