fractal/
error_page.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
use adw::{prelude::*, subclass::prelude::*};
use gettextrs::gettext;
use gtk::{self, glib, CompositeTemplate};

use crate::toast;

/// The possible error subpages.
#[derive(Debug, Clone, Copy, strum::AsRefStr)]
#[strum(serialize_all = "kebab-case")]
pub enum ErrorSubpage {
    Secret,
    Session,
}

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

    use super::*;

    #[derive(Debug, Default, CompositeTemplate)]
    #[template(resource = "/org/gnome/Fractal/ui/error_page.ui")]
    pub struct ErrorPage {
        #[template_child]
        pub stack: TemplateChild<gtk::Stack>,
        #[template_child]
        pub secret_error_page: TemplateChild<adw::StatusPage>,
        #[template_child]
        pub linux_secret_instructions: TemplateChild<adw::Clamp>,
        #[template_child]
        pub secret_service_override_command: TemplateChild<gtk::Label>,
        #[template_child]
        pub session_error_page: TemplateChild<adw::StatusPage>,
    }

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

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

            klass.set_accessible_role(gtk::AccessibleRole::Group);
        }

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

    impl ObjectImpl for ErrorPage {}
    impl WidgetImpl for ErrorPage {}
    impl BinImpl for ErrorPage {}
}

glib::wrapper! {
    /// A view displaying an error.
    pub struct ErrorPage(ObjectSubclass<imp::ErrorPage>)
        @extends gtk::Widget, adw::Bin, @implements gtk::Accessible;
}

#[gtk::template_callbacks]
impl ErrorPage {
    pub fn new() -> Self {
        glib::Object::new()
    }

    /// Display the given secret error.
    pub fn display_secret_error(&self, message: &str) {
        let imp = self.imp();

        #[cfg(not(target_os = "linux"))]
        imp.linux_secret_instructions.set_visible(false);

        #[cfg(target_os = "linux")]
        {
            imp.linux_secret_instructions.set_visible(true);

            imp.secret_service_override_command.set_markup(&format!(
                "<tt>flatpak --user override --talk-name=org.freedesktop.secrets {}</tt>",
                crate::config::APP_ID
            ));
        }

        imp.secret_error_page.set_description(Some(message));
        imp.stack
            .set_visible_child_name(ErrorSubpage::Secret.as_ref());
    }

    /// Copy the secret service override command to the clipboard.
    #[template_callback]
    fn copy_secret_service_override_command(&self) {
        let command = self.imp().secret_service_override_command.label();
        self.clipboard()
            .set_text(command.trim_start_matches("<tt>").trim_end_matches("</tt>"));
        toast!(self, gettext("Command copied to clipboard"));
    }

    /// Display the given session error.
    pub fn display_session_error(&self, message: &str) {
        let imp = self.imp();
        imp.session_error_page.set_description(Some(message));
        imp.stack
            .set_visible_child_name(ErrorSubpage::Session.as_ref());
    }
}