aboutsummaryrefslogtreecommitdiff
path: root/main/src/ui/main_window_controller.vala
blob: 2e27066337dec423cbdd2d142806721e281fdbe2 (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
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
using Gee;
using Gdk;
using Gtk;

using Dino.Entities;

namespace Dino.Ui {

public class MainWindowController : Object {

    private StreamInteractor stream_interactor;
    private Conversation? conversation;
    private Application app;
    private Database db;
    private MainWindow window;

    private ConversationViewController conversation_view_controller;

    public MainWindowController(Application application, StreamInteractor stream_interactor, Database db) {
        this.app = application;
        this.stream_interactor = stream_interactor;
        this.db = db;

        stream_interactor.get_module(ConversationManager.IDENTITY).conversation_deactivated.connect(check_unset_conversation);
        stream_interactor.account_removed.connect(check_unset_conversation);

        SimpleAction jump_to_conversatio_message_action = new SimpleAction("jump-to-conversation-message", new VariantType.tuple(new VariantType[]{VariantType.INT32, VariantType.INT32}));
        jump_to_conversatio_message_action.activate.connect((variant) => {
            int conversation_id = variant.get_child_value(0).get_int32();
            Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation_by_id(conversation_id);
            if (conversation == null || !this.conversation.equals(conversation)) return;

            int item_id = variant.get_child_value(1).get_int32();
            ContentItem? content_item = stream_interactor.get_module(ContentItemStore.IDENTITY).get_item_by_id(conversation, item_id);

            select_conversation(conversation, false, false);
            window.conversation_view.conversation_frame.initialize_around_message(conversation, content_item);
        });
        app.add_action(jump_to_conversatio_message_action);

    }

    public void set_window(MainWindow window) {
        this.window = window;

        this.conversation_view_controller = new ConversationViewController(window.conversation_view, window.conversation_titlebar, stream_interactor);

        conversation_view_controller.search_menu_entry.button.bind_property("active", window.search_flap, "reveal-flap", BindingFlags.SYNC_CREATE | BindingFlags.BIDIRECTIONAL);

        window.search_flap.notify["reveal-flap"].connect(() => {
            if (window.search_flap.reveal_flap) {
                if (window.conversation_view.conversation_frame.conversation != null && window.global_search.search_entry.text == "") {
                    reset_search_entry();
                }
                window.global_search.search_entry.grab_focus();
                window.global_search.search_entry.set_position((int)window.global_search.search_entry.text.length);
            }
        });
        window.global_search.selected_item.connect((item) => {
            select_conversation(item.conversation, false, false);
            window.conversation_view.conversation_frame.initialize_around_message(item.conversation, item);
            if (window.search_flap.folded) {
                close_search();
            }
        });

        window.welcome_placeholder.primary_button.clicked.connect(() => {
            ManageAccounts.AddAccountDialog dialog = new ManageAccounts.AddAccountDialog(stream_interactor, db);
            dialog.set_transient_for(app.get_active_window());
            dialog.present();
        });
        window.accounts_placeholder.primary_button.clicked.connect(() => { app.activate_action("preferences", null); });
        window.conversation_selector.conversation_selected.connect((conversation) => select_conversation(conversation));

//        ConversationListModel list_model = new ConversationListModel(stream_interactor);
//        list_model.closed_conversation.connect((conversation) => {
//            print(@"closed $(conversation.counterpart.bare_jid)\n");
//            stream_interactor.get_module(ConversationManager.IDENTITY).close_conversation(conversation);
//        });
//        SingleSelection selection_model = new SingleSelection(list_model) { autoselect=false };
//        selection_model.notify["selected-item"].connect(() => {
//            ConversationViewModel view_model = (ConversationViewModel) selection_model.selected_item;
//            if (view_model.conversation.equals(conversation)) return;
//            print(@"selected conversation $(view_model.conversation.counterpart)\n");
//            select_conversation(view_model.conversation);
//        });
//        window.conversation_list_view.set_model(selection_model);
//        print(list_model.get_n_items().to_string() + " " + selection_model.get_n_items().to_string() + "<<");
//        print(selection_model.get_selected().to_string() + "<<");
//        window.conversation_list_view.realize.connect(() => {
//            selection_model.set_selected(0);
//        });

        Widget window_widget = ((Widget) window);

        EventControllerKey key_event_controller = new EventControllerKey();
        window_widget.add_controller(key_event_controller);
        // TODO GTK4: Why doesn't this work with key_pressed signal
        key_event_controller.key_released.connect((keyval) => {
            if (keyval == Gdk.Key.Escape) {
                close_search();
            }
        });

        EventControllerFocus focus_event_controller = new EventControllerFocus();
        window_widget.add_controller(focus_event_controller);
        focus_event_controller.enter.connect(() => {
            stream_interactor.get_module(ChatInteraction.IDENTITY).on_window_focus_in(conversation);
        });
        focus_event_controller.leave.connect(() => {
            stream_interactor.get_module(ChatInteraction.IDENTITY).on_window_focus_out(conversation);
        });

        window.conversation_selected.connect(conversation => select_conversation(conversation));

        stream_interactor.account_added.connect((account) => { update_stack_state(true); });
        stream_interactor.account_removed.connect((account) => { update_stack_state(); });
        stream_interactor.get_module(ConversationManager.IDENTITY).conversation_activated.connect(() => update_stack_state());
        stream_interactor.get_module(ConversationManager.IDENTITY).conversation_deactivated.connect(() => update_stack_state());
        update_stack_state();
    }

    public void select_conversation(Conversation? conversation, bool do_reset_search = true, bool default_initialize_conversation = true) {
        this.conversation = conversation;

        conversation_view_controller.select_conversation(conversation, default_initialize_conversation);

        stream_interactor.get_module(ChatInteraction.IDENTITY).on_conversation_selected(conversation);
        conversation.active = true; // only for conversation_selected
        window.conversation_selector.on_conversation_selected(conversation); // In case selection was not via ConversationSelector

        if (do_reset_search) {
            reset_search_entry();
        }
    }

    private void check_unset_conversation() {
        if (stream_interactor.get_module(ConversationManager.IDENTITY).get_active_conversations().size == 0) {
            unset_conversation();
        }
    }

    private void unset_conversation() {
        this.conversation = null;

        conversation_view_controller.unset_conversation();

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

    private void update_stack_state(bool know_exists = false) {
        ArrayList<Account> accounts = stream_interactor.get_accounts();
        if (!know_exists && accounts.size == 0) {
            if (db.get_accounts().size == 0) {
                window.set_stack_state(MainWindow.StackState.CLEAN_START);
            } else {
                window.set_stack_state(MainWindow.StackState.NO_ACTIVE_ACCOUNTS);
            }
        } else if (stream_interactor.get_module(ConversationManager.IDENTITY).get_active_conversations().size == 0) {
            window.set_stack_state(MainWindow.StackState.NO_ACTIVE_CONVERSATIONS);
        } else {
            window.set_stack_state(MainWindow.StackState.CONVERSATION);
        }
    }

    private void reset_search_entry() {
        if (window.conversation_view.conversation_frame.conversation != null) {
            switch (conversation.type_) {
                case Conversation.Type.CHAT:
                case Conversation.Type.GROUPCHAT_PM:
                    window.global_search.search_entry.text = @"with:$(conversation.counterpart) ";
                    break;
                case Conversation.Type.GROUPCHAT:
                    window.global_search.search_entry.text = @"in:$(conversation.counterpart) ";
                    break;
            }
        }
    }

    private void close_search() {
        conversation_view_controller.search_menu_entry.button.active = false;
    }
}

}