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
use gtk::{glib, glib::closure, prelude::*, subclass::prelude::*};

use crate::{
    session::model::{JoinRule, Member, Membership, Room, RoomAliases, RoomList, RoomType},
    utils::{expression, ExpressionListModel},
};

mod imp {
    use std::marker::PhantomData;

    use super::*;

    #[derive(Debug, Default, glib::Properties)]
    #[properties(wrapper_type = super::CompletionRoomList)]
    pub struct CompletionRoomList {
        /// The rooms used for completion.
        #[property(get = Self::rooms, set = Self::set_rooms, explicit_notify, nullable)]
        rooms: PhantomData<Option<RoomList>>,
        /// The room list with expression watches.
        pub rooms_expr: ExpressionListModel,
        /// The search filter.
        pub search_filter: gtk::StringFilter,
        /// The list of sorted and filtered rooms.
        #[property(get)]
        pub list: gtk::FilterListModel,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for CompletionRoomList {
        const NAME: &'static str = "ContentCompletionRoomList";
        type Type = super::CompletionRoomList;
    }

    #[glib::derived_properties]
    impl ObjectImpl for CompletionRoomList {
        fn constructed(&self) {
            self.parent_constructed();

            // Filter the rooms, the criteria:
            // - not a space or upgraded
            // - joined
            // - anyone can join

            let category_expr = Room::this_expression("category").chain_closure::<bool>(closure!(
                |_obj: Option<glib::Object>, category: RoomType| {
                    !matches!(category, RoomType::Space | RoomType::Outdated)
                }
            ));
            let category_filter = gtk::BoolFilter::new(Some(&category_expr));

            let joined_expr = Room::this_expression("own-member")
                .chain_property::<Member>("membership")
                .chain_closure::<bool>(closure!(
                    |_obj: Option<glib::Object>, membership: Membership| {
                        membership == Membership::Join
                    }
                ));
            let joined_filter = gtk::BoolFilter::new(Some(&joined_expr));

            let anyone_can_join_expr =
                Room::this_expression("join-rule").chain_property::<JoinRule>("anyone-can-join");
            let anyone_can_join_filter = gtk::BoolFilter::builder()
                .expression(&anyone_can_join_expr)
                .build();

            let filter = gtk::EveryFilter::new();
            filter.append(category_filter);
            filter.append(joined_filter);
            filter.append(anyone_can_join_filter);

            let first_model = gtk::FilterListModel::builder()
                .filter(&filter)
                .model(&self.rooms_expr)
                .build();

            // Sort list by display name.
            let display_name_expr = Room::this_expression("display-name");
            let display_name_sorter = gtk::StringSorter::builder()
                .ignore_case(true)
                .expression(&display_name_expr)
                .build();

            let second_model = gtk::SortListModel::builder()
                .sorter(&display_name_sorter)
                .model(&first_model)
                .build();

            // Setup the search filter.
            let alias_expr =
                Room::this_expression("aliases").chain_property::<RoomAliases>("alias-string");
            let room_search_string_expr = gtk::ClosureExpression::new::<String>(
                &[alias_expr.clone(), display_name_expr.clone()],
                closure!(
                    |_: Option<glib::Object>, alias: Option<&str>, display_name: &str| {
                        if let Some(alias) = alias {
                            format!("{display_name} {alias}")
                        } else {
                            display_name.to_owned()
                        }
                    }
                ),
            );
            self.search_filter.set_ignore_case(true);
            self.search_filter
                .set_match_mode(gtk::StringFilterMatchMode::Substring);
            self.search_filter
                .set_expression(Some(expression::normalize_string(room_search_string_expr)));

            self.list.set_filter(Some(&self.search_filter));
            self.list.set_model(Some(&second_model));

            self.rooms_expr.set_expressions(vec![
                category_expr.upcast(),
                joined_expr.upcast(),
                anyone_can_join_expr.upcast(),
                alias_expr.upcast(),
                display_name_expr.upcast(),
            ]);
        }
    }

    impl WidgetImpl for CompletionRoomList {}
    impl PopoverImpl for CompletionRoomList {}

    impl CompletionRoomList {
        /// The rooms used for completion.
        fn rooms(&self) -> Option<RoomList> {
            self.rooms_expr.model().and_downcast()
        }

        /// Set the rooms used for completion.
        fn set_rooms(&self, rooms: Option<RoomList>) {
            if self.rooms() == rooms {
                return;
            }

            self.rooms_expr.set_model(rooms);
            self.obj().notify_rooms();
        }
    }
}

glib::wrapper! {
    /// The filtered and sorted rooms list for completion.
    pub struct CompletionRoomList(ObjectSubclass<imp::CompletionRoomList>);
}

impl CompletionRoomList {
    pub fn new() -> Self {
        glib::Object::new()
    }

    /// Set the search term.
    pub fn set_search_term(&self, term: Option<&str>) {
        self.imp().search_filter.set_search(term);
    }
}

impl Default for CompletionRoomList {
    fn default() -> Self {
        Self::new()
    }
}