fractal/session/view/account_settings/general_page/
log_out_subpage.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
use adw::{prelude::*, subclass::prelude::*};
use gettextrs::gettext;
use gtk::{glib, CompositeTemplate};

use crate::{
    components::LoadingButtonRow,
    session::{
        model::{CryptoIdentityState, RecoveryState, Session, SessionVerificationState},
        view::AccountSettings,
    },
    toast,
};

mod imp {
    use glib::subclass::InitializingObject;

    use super::*;

    #[derive(Debug, Default, CompositeTemplate, glib::Properties)]
    #[template(
        resource = "/org/gnome/Fractal/ui/session/view/account_settings/general_page/log_out_subpage.ui"
    )]
    #[properties(wrapper_type = super::LogOutSubpage)]
    pub struct LogOutSubpage {
        /// The current session.
        #[property(get, set = Self::set_session, nullable)]
        pub session: glib::WeakRef<Session>,
        #[template_child]
        pub stack: TemplateChild<gtk::Stack>,
        #[template_child]
        pub warning_box: TemplateChild<gtk::Box>,
        #[template_child]
        pub warning_description: TemplateChild<gtk::Label>,
        #[template_child]
        pub warning_button: TemplateChild<adw::ButtonRow>,
        #[template_child]
        pub logout_button: TemplateChild<LoadingButtonRow>,
        #[template_child]
        pub try_again_button: TemplateChild<LoadingButtonRow>,
        #[template_child]
        pub remove_button: TemplateChild<LoadingButtonRow>,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for LogOutSubpage {
        const NAME: &'static str = "LogOutSubpage";
        type Type = super::LogOutSubpage;
        type ParentType = adw::NavigationPage;

        fn class_init(klass: &mut Self::Class) {
            Self::bind_template(klass);
            Self::Type::bind_template_callbacks(klass);
        }

        fn instance_init(obj: &InitializingObject<Self>) {
            obj.init_template();
        }
    }

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

    impl WidgetImpl for LogOutSubpage {}
    impl NavigationPageImpl for LogOutSubpage {}

    impl LogOutSubpage {
        /// Set the current session.
        fn set_session(&self, session: Option<&Session>) {
            self.session.set(session);

            self.update_warning();
        }

        /// Update the warning.
        fn update_warning(&self) {
            let Some(session) = self.session.upgrade() else {
                return;
            };

            let security = session.security();
            let verification_state = security.verification_state();
            let recovery_state = security.recovery_state();

            if verification_state != SessionVerificationState::Verified
                || recovery_state != RecoveryState::Enabled
            {
                self.warning_description.set_label(&gettext("The crypto identity and account recovery are not set up properly. If this is your last connected session and you have no recent local backup of your encryption keys, you will not be able to restore your account."));
                self.warning_box.set_visible(true);
                return;
            }

            let crypto_identity_state = security.crypto_identity_state();

            if crypto_identity_state == CryptoIdentityState::LastManStanding {
                self.warning_description.set_label(&gettext("This is your last connected session. Make sure that you can still access your recovery key or passphrase, or to backup your encryption keys before logging out."));
                self.warning_box.set_visible(true);
                return;
            }

            // No particular problem, do not show the warning.
            self.warning_box.set_visible(false);
        }
    }
}

glib::wrapper! {
    /// Subpage allowing a user to log out from their account.
    pub struct LogOutSubpage(ObjectSubclass<imp::LogOutSubpage>)
        @extends gtk::Widget, adw::NavigationPage, @implements gtk::Accessible;
}

#[gtk::template_callbacks]
impl LogOutSubpage {
    pub fn new(session: &Session) -> Self {
        glib::Object::builder().property("session", session).build()
    }

    /// Show the security tab of the settings.
    #[template_callback]
    fn view_security(&self) {
        let Some(dialog) = self
            .ancestor(AccountSettings::static_type())
            .and_downcast::<AccountSettings>()
        else {
            return;
        };

        dialog.pop_subpage();
        dialog.set_visible_page_name("security");
    }

    /// Log out the current session.
    #[template_callback]
    async fn logout(&self) {
        let Some(session) = self.session() else {
            return;
        };

        let imp = self.imp();
        let is_logout_page = imp
            .stack
            .visible_child_name()
            .is_some_and(|name| name == "logout");

        if is_logout_page {
            imp.logout_button.set_is_loading(true);
            imp.warning_button.set_sensitive(false);
        } else {
            imp.try_again_button.set_is_loading(true);
        }

        if let Err(error) = session.log_out().await {
            if is_logout_page {
                imp.stack.set_visible_child_name("failed");
            } else {
                toast!(self, error);
            }
        }

        if is_logout_page {
            imp.logout_button.set_is_loading(false);
            imp.warning_button.set_sensitive(true);
        } else {
            imp.try_again_button.set_is_loading(false);
        }
    }

    /// Remove the current session.
    #[template_callback]
    async fn remove(&self) {
        let Some(session) = self.session() else {
            return;
        };

        let imp = self.imp();
        imp.remove_button.set_is_loading(true);

        session.clean_up().await;

        imp.remove_button.set_is_loading(false);
    }
}