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

mod category_filter;
mod category_type;

use self::category_filter::CategoryFilter;
pub use self::category_type::CategoryType;
use crate::{
    session::model::{Room, RoomList, RoomType, SessionSettings, VerificationList},
    utils::ExpressionListModel,
};

mod imp {
    use std::{
        cell::{Cell, OnceCell},
        marker::PhantomData,
    };

    use super::*;

    #[derive(Debug, Default, glib::Properties)]
    #[properties(wrapper_type = super::Category)]
    pub struct Category {
        /// The source model of this category.
        #[property(get, set = Self::set_model, construct_only)]
        pub model: OnceCell<gio::ListModel>,
        /// The inner model of this category.
        inner_model: OnceCell<gio::ListModel>,
        /// The filter of this category.
        pub filter: CategoryFilter,
        /// The type of this category.
        #[property(get = Self::category_type, set = Self::set_category_type, construct_only, builder(CategoryType::default()))]
        pub category_type: PhantomData<CategoryType>,
        /// Whether this category is empty.
        #[property(get)]
        pub empty: Cell<bool>,
        /// The display name of this category.
        #[property(get = Self::display_name)]
        pub display_name: PhantomData<String>,
        /// Whether this category is expanded.
        #[property(get, set = Self::set_is_expanded, explicit_notify)]
        pub is_expanded: Cell<bool>,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for Category {
        const NAME: &'static str = "Category";
        type Type = super::Category;
        type Interfaces = (gio::ListModel,);
    }

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

            let Some(settings) = self.session_settings() else {
                return;
            };

            let is_expanded = settings.is_category_expanded(self.category_type());
            self.set_is_expanded(is_expanded);
        }
    }

    impl ListModelImpl for Category {
        fn item_type(&self) -> glib::Type {
            glib::Object::static_type()
        }

        fn n_items(&self) -> u32 {
            self.inner_model().n_items()
        }

        fn item(&self, position: u32) -> Option<glib::Object> {
            self.inner_model().item(position)
        }
    }

    impl Category {
        /// The source model of this category.
        fn model(&self) -> &gio::ListModel {
            self.model.get().unwrap()
        }

        /// Set the source model of this category.
        fn set_model(&self, model: gio::ListModel) {
            let model = self.model.get_or_init(|| model).clone();
            let obj = self.obj();

            // Special-case room lists so that they are sorted and in the right category.
            let inner_model = if model.is::<RoomList>() {
                let room_category_type = Room::this_expression("category")
                    .chain_closure::<CategoryType>(closure!(
                        |_: Option<glib::Object>, room_type: RoomType| {
                            CategoryType::from(room_type)
                        }
                    ));
                self.filter
                    .set_expression(Some(room_category_type.clone().upcast()));

                let category_type_expr_model = ExpressionListModel::new();
                category_type_expr_model.set_expressions(vec![room_category_type.upcast()]);
                category_type_expr_model.set_model(Some(model));

                let filter_model = gtk::FilterListModel::new(
                    Some(category_type_expr_model),
                    Some(self.filter.clone()),
                );

                let room_latest_activity = Room::this_expression("latest-activity");
                let sorter = gtk::NumericSorter::builder()
                    .expression(&room_latest_activity)
                    .sort_order(gtk::SortType::Descending)
                    .build();

                let latest_activity_expr_model = ExpressionListModel::new();
                latest_activity_expr_model.set_expressions(vec![room_latest_activity.upcast()]);
                latest_activity_expr_model.set_model(Some(filter_model));

                let sort_model =
                    gtk::SortListModel::new(Some(latest_activity_expr_model), Some(sorter));
                sort_model.upcast()
            } else {
                model
            };

            inner_model.connect_items_changed(clone!(
                #[weak]
                obj,
                move |model, pos, removed, added| {
                    obj.items_changed(pos, removed, added);
                    obj.imp().set_empty(model.n_items() == 0);
                }
            ));

            self.set_empty(inner_model.n_items() == 0);
            self.inner_model.set(inner_model).unwrap();
        }

        /// The inner model of this category.
        fn inner_model(&self) -> &gio::ListModel {
            self.inner_model.get().unwrap()
        }

        /// The type of this category.
        fn category_type(&self) -> CategoryType {
            self.filter.category_type()
        }

        /// Set the type of this category.
        fn set_category_type(&self, type_: CategoryType) {
            self.filter.set_category_type(type_);
        }

        /// Set whether this category is empty.
        fn set_empty(&self, empty: bool) {
            if empty == self.empty.get() {
                return;
            }

            self.empty.set(empty);
            self.obj().notify_empty();
        }

        /// The display name of this category.
        fn display_name(&self) -> String {
            self.category_type().to_string()
        }

        /// Set whether this category is expanded.
        fn set_is_expanded(&self, expanded: bool) {
            if self.is_expanded.get() == expanded {
                return;
            }

            self.is_expanded.set(expanded);
            self.obj().notify_is_expanded();

            if let Some(settings) = self.session_settings() {
                settings.set_category_expanded(self.category_type(), expanded);
            }
        }

        /// The settings of the current session.
        fn session_settings(&self) -> Option<SessionSettings> {
            let model = self.model();
            let session = model
                .downcast_ref::<RoomList>()
                .and_then(|l| l.session())
                .or_else(|| {
                    model
                        .downcast_ref::<VerificationList>()
                        .and_then(|l| l.session())
                })?;
            Some(session.settings())
        }
    }
}

glib::wrapper! {
    /// A list of items in the same category.
    pub struct Category(ObjectSubclass<imp::Category>)
        @implements gio::ListModel;
}

impl Category {
    /// Constructs a new `Category` with the given category type and source
    /// model.
    pub fn new(category_type: CategoryType, model: &impl IsA<gio::ListModel>) -> Self {
        glib::Object::builder()
            .property("category-type", category_type)
            .property("model", model)
            .build()
    }

    /// Whether this category should be shown for a drag-n-drop from the given
    /// category.
    pub fn visible_for_category(&self, for_category: CategoryType) -> bool {
        if !self.empty() {
            return true;
        }

        let room_types = RoomType::try_from(for_category)
            .ok()
            .zip(RoomType::try_from(self.category_type()).ok());

        room_types.is_some_and(|(source_room_type, target_room_type)| {
            source_room_type.can_change_to(target_room_type)
        })
    }
}