fractal/session_list/
mod.rs

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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
use std::{cmp::Ordering, ffi::OsString};

use gettextrs::gettext;
use gtk::{gio, glib, glib::clone, prelude::*, subclass::prelude::*};
use indexmap::map::IndexMap;
use tracing::{error, info};

mod failed_session;
mod new_session;
mod session_info;
mod session_list_settings;

pub use self::{failed_session::*, new_session::*, session_info::*, session_list_settings::*};
use crate::{
    prelude::*,
    secret::{self, StoredSession},
    session::model::{Session, SessionState},
    spawn, spawn_tokio,
    utils::{data_dir_path, DataType, LoadingState},
};

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

    use super::*;

    #[derive(Debug, Default, glib::Properties)]
    #[properties(wrapper_type = super::SessionList)]
    pub struct SessionList {
        /// The map of session ID to session.
        pub(super) list: RefCell<IndexMap<String, SessionInfo>>,
        /// The loading state of the list.
        #[property(get, builder(LoadingState::default()))]
        state: Cell<LoadingState>,
        /// The error message, if state is set to `LoadingState::Error`.
        #[property(get, nullable)]
        error: RefCell<Option<String>>,
        /// The settings of the sessions.
        #[property(get)]
        settings: SessionListSettings,
        /// Whether this list is empty.
        #[property(get = Self::is_empty)]
        is_empty: PhantomData<bool>,
    }

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

    #[glib::derived_properties]
    impl ObjectImpl for SessionList {}

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

        fn n_items(&self) -> u32 {
            self.list.borrow().len() as u32
        }

        fn item(&self, position: u32) -> Option<glib::Object> {
            self.list
                .borrow()
                .get_index(position as usize)
                .map(|(_, v)| v.clone().upcast())
        }
    }

    impl SessionList {
        /// Whether this list is empty.
        fn is_empty(&self) -> bool {
            self.list.borrow().is_empty()
        }

        /// Set the loading state of this list.
        fn set_state(&self, state: LoadingState) {
            if self.state.get() == state {
                return;
            }

            self.state.set(state);
            self.obj().notify_state();
        }

        /// Set the error message.
        fn set_error(&self, message: String) {
            self.error.replace(Some(message));
            self.obj().notify_error();
        }

        /// Insert the given session into the list.
        ///
        /// If a session with the same ID already exists, it is replaced.
        ///
        /// Returns the index of the session.
        pub(super) fn insert(&self, session: impl IsA<SessionInfo>) -> usize {
            let session = session.upcast();

            if let Some(session) = session.downcast_ref::<Session>() {
                session.connect_logged_out(clone!(
                    #[weak(rename_to = imp)]
                    self,
                    move |session| imp.remove(session.session_id())
                ));
            }

            let was_empty = self.is_empty();

            let (index, replaced) = self
                .list
                .borrow_mut()
                .insert_full(session.session_id(), session);

            let removed = replaced.is_some().into();

            let obj = self.obj();
            obj.items_changed(index as u32, removed, 1);

            if was_empty {
                obj.notify_is_empty();
            }

            index
        }

        /// Remove the session with the given ID from the list.
        fn remove(&self, session_id: &str) {
            let removed = self.list.borrow_mut().shift_remove_full(session_id);

            if let Some((position, ..)) = removed {
                let obj = self.obj();
                obj.items_changed(position as u32, 1, 0);

                if self.is_empty() {
                    obj.notify_is_empty();
                }
            }
        }

        /// Restore the logged-in sessions.
        pub(super) async fn restore_sessions(&self) {
            if self.state.get() >= LoadingState::Loading {
                return;
            }

            self.set_state(LoadingState::Loading);

            let handle = spawn_tokio!(secret::restore_sessions());
            let mut sessions = match handle.await.expect("task was not aborted") {
                Ok(sessions) => sessions,
                Err(error) => {
                    let message = format!(
                        "{}\n\n{}",
                        gettext("Could not restore previous sessions"),
                        error.to_user_facing(),
                    );

                    self.set_error(message);
                    self.set_state(LoadingState::Error);
                    return;
                }
            };

            self.settings.load();
            let session_ids = self.settings.session_ids();

            // Keep the order from the settings.
            sessions.sort_by(|a, b| {
                let pos_a = session_ids.get_index_of(&a.id);
                let pos_b = session_ids.get_index_of(&b.id);

                match (pos_a, pos_b) {
                    (Some(pos_a), Some(pos_b)) => pos_a.cmp(&pos_b),
                    // Keep unknown sessions at the end.
                    (Some(_), None) => Ordering::Greater,
                    (None, Some(_)) => Ordering::Less,
                    _ => Ordering::Equal,
                }
            });

            // Get the directories present in the data path to only restore sessions with
            // data on the system. This is necessary for users sharing their secrets between
            // devices.
            let mut directories = match self.data_directories(sessions.len()).await {
                Ok(directories) => directories,
                Err(error) => {
                    error!("Could not access data directory: {error}");
                    let message = format!(
                        "{}\n\n{}",
                        gettext("Could not restore previous sessions"),
                        gettext("An unexpected error happened while accessing the data directory"),
                    );

                    self.set_error(message);
                    self.set_state(LoadingState::Error);
                    return;
                }
            };

            for stored_session in sessions {
                if let Some(pos) = directories
                    .iter()
                    .position(|dir_name| dir_name == stored_session.id.as_str())
                {
                    directories.swap_remove(pos);
                    info!(
                        "Restoring previous session {} for user {}",
                        stored_session.id, stored_session.user_id,
                    );
                    self.insert(NewSession::new(&stored_session));

                    spawn!(
                        glib::Priority::DEFAULT_IDLE,
                        clone!(
                            #[weak(rename_to = obj)]
                            self,
                            async move {
                                obj.restore_stored_session(&stored_session).await;
                            }
                        )
                    );
                } else {
                    info!(
                        "Ignoring session {} for user {}: no data directory",
                        stored_session.id, stored_session.user_id,
                    );
                }
            }

            self.set_state(LoadingState::Ready);
        }

        /// The list of directories in the data directory.
        async fn data_directories(&self, capacity: usize) -> std::io::Result<Vec<OsString>> {
            let data_path = data_dir_path(DataType::Persistent);

            if !data_path.try_exists()? {
                return Ok(Vec::new());
            }

            spawn_tokio!(async move {
                let mut read_dir = tokio::fs::read_dir(data_path).await?;
                let mut directories = Vec::with_capacity(capacity);

                loop {
                    let Some(entry) = read_dir.next_entry().await? else {
                        // We are at the end of the list.
                        break;
                    };

                    if !entry.file_type().await?.is_dir() {
                        // We are only interested in directories.
                        continue;
                    }

                    directories.push(entry.file_name());
                }

                std::io::Result::Ok(directories)
            })
            .await
            .expect("task was not aborted")
        }

        /// Restore a stored session.
        async fn restore_stored_session(&self, session_info: &StoredSession) {
            let settings = self.settings.get_or_create(&session_info.id);
            match Session::restore(session_info.clone(), settings).await {
                Ok(session) => {
                    session.prepare().await;
                    self.insert(session);
                }
                Err(error) => {
                    error!("Could not restore previous session: {error}");
                    self.insert(FailedSession::new(session_info, error));
                }
            }
        }
    }
}

glib::wrapper! {
    /// List of all logged in sessions.
    pub struct SessionList(ObjectSubclass<imp::SessionList>)
        @implements gio::ListModel;
}

impl SessionList {
    /// Create a new empty `SessionList`.
    pub fn new() -> Self {
        glib::Object::new()
    }

    /// Whether at least one session is ready.
    pub(crate) fn has_session_ready(&self) -> bool {
        self.imp()
            .list
            .borrow()
            .values()
            .filter_map(|s| s.downcast_ref::<Session>())
            .any(|s| s.state() == SessionState::Ready)
    }

    /// The session with the given ID, if any.
    pub(crate) fn get(&self, session_id: &str) -> Option<SessionInfo> {
        self.imp().list.borrow().get(session_id).cloned()
    }

    /// The index of the session with the given ID, if any.
    pub(crate) fn index(&self, session_id: &str) -> Option<usize> {
        self.imp().list.borrow().get_index_of(session_id)
    }

    /// The first session in the list, if any.
    pub(crate) fn first(&self) -> Option<SessionInfo> {
        self.imp().list.borrow().first().map(|(_, v)| v.clone())
    }

    /// Insert the given session into the list.
    ///
    /// If a session with the same ID already exists, it is replaced.
    ///
    /// Returns the index of the session.
    pub(crate) fn insert(&self, session: impl IsA<SessionInfo>) -> usize {
        self.imp().insert(session)
    }

    /// Restore the logged-in sessions.
    pub(crate) async fn restore_sessions(&self) {
        self.imp().restore_sessions().await;
    }
}

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