aboutsummaryrefslogtreecommitdiff
path: root/main/src/ui/conversation_view_controller.vala
blob: 9626a39d355c6db501c82c45801765757183b9b9 (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using Gee;
using Gdk;
using Gtk;

using Dino.Entities;

namespace Dino.Ui {

public class ConversationViewController : Object {

    private Application app;
    private MainWindow main_window;
    private ConversationView view;
    private Widget? overlay_dialog;
    public SearchMenuEntry search_menu_entry = new SearchMenuEntry();
    public ListView list_view = new ListView(null, null);
    private DropTarget drop_event_controller = new DropTarget(typeof(FileList), DragAction.COPY );

    private ChatInputController chat_input_controller;
    private StreamInteractor stream_interactor;
    private Conversation? conversation;

    private Binding? display_name_binding = null;

    private const string[] KEY_COMBINATION_CLOSE_CONVERSATION = {"<Ctrl>W", null};

    public ConversationViewController(MainWindow main_window, ConversationView view, StreamInteractor stream_interactor) {
        this.main_window = main_window;
        this.view = view;
        this.stream_interactor = stream_interactor;
        this.app = GLib.Application.get_default() as Application;

        this.chat_input_controller = new ChatInputController(view.chat_input, stream_interactor);
        chat_input_controller.activate_last_message_correction.connect(view.conversation_frame.activate_last_message_correction);
        chat_input_controller.file_picker_selected.connect(open_file_picker);
        chat_input_controller.clipboard_pasted.connect(on_clipboard_paste);

        view.conversation_frame.init(stream_interactor);

        // drag 'n drop file upload
        drop_event_controller.on_drop.connect(this.on_drag_data_received);

        // forward key presses
        var key_controller = new EventControllerKey() { name = "dino-forward-to-input-key-events-1" };
        key_controller.key_pressed.connect(forward_key_press_to_chat_input);
        view.conversation_frame.add_controller(key_controller);

        var key_controller2 = new EventControllerKey() { name = "dino-forward-to-input-key-events-2" };
        key_controller2.key_pressed.connect(forward_key_press_to_chat_input);
        view.chat_input.add_controller(key_controller2);

        var key_controller3 = new EventControllerKey() { name = "dino-forward-to-input-key-events-3" };
        key_controller3.key_pressed.connect(forward_key_press_to_chat_input);
        main_window.conversation_headerbar.add_controller(key_controller3);

//      goto-end floating button
        var vadjustment = view.conversation_frame.scrolled.vadjustment;
        vadjustment.notify["value"].connect(() => {
            debug_here();
            bool scrolled_up = vadjustment.value <  vadjustment.upper - vadjustment.page_size;
            view.goto_end_revealer.reveal_child = scrolled_up;
            view.goto_end_revealer.visible = scrolled_up;
            stream_interactor.get_module(ChatInteraction.IDENTITY).on_scrolled_down_changed(!scrolled_up && view.conversation_frame.at_current_content, conversation);
        });
        view.goto_end_button.clicked.connect(() => {
            view.conversation_frame.initialize_for_conversation(conversation, true);
        });

        // Update conversation topic
        stream_interactor.get_module(MucManager.IDENTITY).subject_set.connect((account, jid, subject) => {
            if (conversation != null && conversation.counterpart.equals_bare(jid) && conversation.account.equals(account)) {
                update_conversation_topic(subject);
            }
        });

        stream_interactor.get_module(FileManager.IDENTITY).upload_available.connect(update_file_upload_status);

        // Headerbar plugins
        app.plugin_registry.register_contact_titlebar_entry(new MenuEntry(stream_interactor));
        app.plugin_registry.register_contact_titlebar_entry(search_menu_entry);
        app.plugin_registry.register_contact_titlebar_entry(new OccupantsEntry(stream_interactor));
        app.plugin_registry.register_contact_titlebar_entry(new CallTitlebarEntry(stream_interactor));
        foreach(var entry in app.plugin_registry.conversation_titlebar_entries) {
            Widget? button = entry.get_widget(Plugins.WidgetType.GTK4) as Widget;
            if (button == null) {
                continue;
            }
            main_window.conversation_headerbar.pack_end(button);
        }

        Shortcut shortcut = new Shortcut(new KeyvalTrigger(Key.U, ModifierType.CONTROL_MASK), new CallbackAction(() => {
            if (conversation == null) return false;
            stream_interactor.get_module(FileManager.IDENTITY).is_upload_available.begin(conversation, (_, res) => {
                if (stream_interactor.get_module(FileManager.IDENTITY).is_upload_available.end(res)) {
                    open_file_picker();
                }
            });
            return false;
        }));
        ((Gtk.Window)view.get_root()).add_shortcut(shortcut);

        SimpleAction close_conversation_action = new SimpleAction("close-current-conversation", null);
        close_conversation_action.activate.connect(() => {
            stream_interactor.get_module(ConversationManager.IDENTITY).close_conversation(conversation);
        });
        app.add_action(close_conversation_action);
        app.set_accels_for_action("app.close-current-conversation", KEY_COMBINATION_CLOSE_CONVERSATION);
    }

    public void select_conversation(Conversation? conversation, bool default_initialize_conversation) {
        if (this.conversation != null) {
            conversation.notify["encryption"].disconnect(update_file_upload_status);
        }

        this.conversation = conversation;

        // Set list model onto list view
//        Dino.Application app = GLib.Application.get_default() as Dino.Application;
//        var map_list_model = get_conversation_content_model(new ContentItemMetaModel(app.db, conversation, stream_interactor), stream_interactor);
//        NoSelection selection_model = new NoSelection(map_list_model);
//        view.list_view.set_model(selection_model);
//        view.at_current_content = true;

        conversation.notify["encryption"].connect(update_file_upload_status);

        chat_input_controller.set_conversation(conversation);

        if (display_name_binding != null) display_name_binding.unbind();
        var display_name_model = stream_interactor.get_module(ContactModels.IDENTITY).get_display_name_model(conversation);
        display_name_binding = display_name_model.bind_property("display-name", main_window.conversation_window_title, "title", BindingFlags.SYNC_CREATE);

        update_conversation_topic();

        foreach(Plugins.ConversationTitlebarEntry e in this.app.plugin_registry.conversation_titlebar_entries) {
            e.set_conversation(conversation);
        }

        if (default_initialize_conversation) {
            view.conversation_frame.initialize_for_conversation(conversation);
        }

        update_file_upload_status.begin();
    }

    public void unset_conversation() {
        main_window.conversation_window_title.title = "";
        main_window.conversation_window_title.subtitle = "";
    }

    private async void update_file_upload_status() {
        if (conversation == null) return;

        bool upload_available = yield stream_interactor.get_module(FileManager.IDENTITY).is_upload_available(conversation);
        chat_input_controller.set_file_upload_active(upload_available);

        if (upload_available) {
            if (drop_event_controller.widget == null) {
                view.add_controller(drop_event_controller);
            }
        } else {
            if (drop_event_controller.widget != null) {
                view.remove_controller(drop_event_controller);
            }
        }
    }

    private void update_conversation_topic(string? subtitle = null) {
        string str = "";
        if (subtitle != null) {
            str = Util.summarize_whitespaces_to_space(subtitle);
        } else if (conversation.type_ == Conversation.Type.GROUPCHAT) {
            string? subject = stream_interactor.get_module(MucManager.IDENTITY).get_groupchat_subject(conversation.counterpart, conversation.account);
            if (subject != null) {
                str = Util.summarize_whitespaces_to_space(subject);
            }
        }

        main_window.conversation_window_title.subtitle = str;
    }

    private async void on_clipboard_paste() {
        try {
            Clipboard clipboard = view.get_clipboard();
            Gdk.Texture? texture = yield clipboard.read_texture_async(null); // TODO critical
            var file_name = Path.build_filename(FileManager.get_storage_dir(), Xmpp.random_uuid() + ".png");
            texture.save_to_png(file_name);
            chat_input_controller.add_file_transfer(File.new_for_path(file_name));
        } catch (IOError.NOT_SUPPORTED e) {
            // Format not supported, ignore
        }
    }

    private bool on_drag_data_received(DropTarget target, Value val, double x, double y) {
        if (val.type() == typeof(FileList)) {
            foreach (File file in ((FileList)val).get_files()) {
                chat_input_controller.add_file_transfer(file);
            }
            return true;
        }
        return false;
    }

    private void open_file_picker() {
        FileChooserNative chooser = new FileChooserNative(_("Select file"), view.get_root() as Gtk.Window, FileChooserAction.OPEN, _("Select"), _("Cancel")) {
            select_multiple = true
        };
        chooser.response.connect((response) => {
            if (response == ResponseType.ACCEPT) {
                var files_model = chooser.get_files();
                for (int i = 0; i < files_model.get_n_items(); i++) {
                    File file = (File) files_model.get_item(i);
                    chat_input_controller.add_file_transfer(file);
                }
            }
        });
        chooser.show();
    }

    private bool forward_key_press_to_chat_input(EventControllerKey key_controller, uint keyval, uint keycode, Gdk.ModifierType state) {
        if (view.get_root().get_focus() is TextView) {
            return false;
        }

        // Don't forward / change focus on control keys
        if (keyval == Gdk.Key.Control_L || keyval == Gdk.Key.Control_R ||
                keyval == Gdk.Key.Alt_L || keyval == Gdk.Key.Alt_R ||
                keyval == Gdk.Key.Meta_L || keyval == Gdk.Key.Meta_R ||
                keyval == Gdk.Key.Super_L || keyval == Gdk.Key.Super_R) {
            return false;
        }
        // Don't forward / change focus on keyboard shortcuts (which include control keys)
        if ((state & (ModifierType.CONTROL_MASK | ModifierType.ALT_MASK |
                ModifierType.META_MASK | ModifierType.SUPER_MASK)) > 0) {
            return false;
        }

        return key_controller.forward(view.chat_input.chat_text_view.text_view);
    }
}
}