fractal/session/view/account_settings/user_sessions_page/user_session_row.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
use adw::prelude::*;
use gettextrs::gettext;
use gtk::{glib, glib::clone, subclass::prelude::*, CompositeTemplate};
use crate::{
components::{AuthError, LoadingButton},
gettext_f,
session::{model::UserSession, view::account_settings::AccountSettingsSubpage},
system_settings::ClockFormat,
toast, Application,
};
mod imp {
use std::cell::RefCell;
use glib::subclass::InitializingObject;
use super::*;
#[derive(Debug, Default, CompositeTemplate, glib::Properties)]
#[template(
resource = "/org/gnome/Fractal/ui/session/view/account_settings/user_sessions_page/user_session_row.ui"
)]
#[properties(wrapper_type = super::UserSessionRow)]
pub struct UserSessionRow {
#[template_child]
display_name: TemplateChild<gtk::Label>,
#[template_child]
verified_icon: TemplateChild<gtk::Image>,
#[template_child]
last_seen_ip: TemplateChild<gtk::Label>,
#[template_child]
last_seen_ts: TemplateChild<gtk::Label>,
#[template_child]
pub(super) disconnect_button: TemplateChild<LoadingButton>,
#[template_child]
verify_button: TemplateChild<LoadingButton>,
/// The user session displayed by this row.
#[property(get, set = Self::set_user_session, construct_only)]
user_session: RefCell<Option<UserSession>>,
system_settings_handler: RefCell<Option<glib::SignalHandlerId>>,
}
#[glib::object_subclass]
impl ObjectSubclass for UserSessionRow {
const NAME: &'static str = "AccountSettingsUserSessionRow";
type Type = super::UserSessionRow;
type ParentType = gtk::ListBoxRow;
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 UserSessionRow {
fn constructed(&self) {
self.parent_constructed();
let system_settings = Application::default().system_settings();
let system_settings_handler = system_settings.connect_clock_format_notify(clone!(
#[weak(rename_to = imp)]
self,
move |_| {
imp.update_last_seen_ts();
}
));
self.system_settings_handler
.replace(Some(system_settings_handler));
}
fn dispose(&self) {
if let Some(handler) = self.system_settings_handler.take() {
Application::default().system_settings().disconnect(handler);
}
}
}
impl WidgetImpl for UserSessionRow {}
impl ListBoxRowImpl for UserSessionRow {}
impl UserSessionRow {
/// Set the user session displayed by this row.
fn set_user_session(&self, user_session: UserSession) {
let obj = self.obj();
let session_name = user_session.display_name();
self.display_name.set_label(&session_name);
obj.set_tooltip_text(Some(user_session.device_id().as_str()));
self.verified_icon.set_visible(user_session.verified());
// TODO: Implement verification
// imp.verify_button.set_visible(!device.is_verified());
let last_seen_ip = user_session.last_seen_ip();
if let Some(last_seen_ip) = &last_seen_ip {
self.last_seen_ip.set_label(last_seen_ip);
}
self.last_seen_ip.set_visible(last_seen_ip.is_some());
self.last_seen_ts
.set_visible(user_session.last_seen_ts().is_some());
let disconnect_label = if user_session.is_current() {
gettext("Log Out")
} else {
gettext("Disconnect Session")
};
self.disconnect_button.set_content_label(disconnect_label);
self.user_session.replace(Some(user_session));
obj.notify_user_session();
self.update_last_seen_ts();
}
/// Update the last seen timestamp according to the current user session
/// and clock format setting.
fn update_last_seen_ts(&self) {
let Some(datetime) = self
.user_session
.borrow()
.as_ref()
.and_then(|s| s.last_seen_ts())
else {
return;
};
let clock_format = Application::default().system_settings().clock_format();
let use_24 = clock_format == ClockFormat::TwentyFourHours;
// This was ported from Nautilus and simplified for our use case.
// See: https://gitlab.gnome.org/GNOME/nautilus/-/blob/1c5bd3614a35cfbb49de087bc10381cdef5a218f/src/nautilus-file.c#L5001
let now = glib::DateTime::now_local().unwrap();
let format;
let days_ago = {
let today_midnight = glib::DateTime::from_local(
now.year(),
now.month(),
now.day_of_month(),
0,
0,
0f64,
)
.expect("constructing GDateTime works");
let date = glib::DateTime::from_local(
datetime.year(),
datetime.month(),
datetime.day_of_month(),
0,
0,
0f64,
)
.expect("constructing GDateTime works");
today_midnight.difference(&date).as_days()
};
// Show only the time if date is on today
if days_ago == 0 {
if use_24 {
// Translators: Time in 24h format, i.e. "23:04".
// Do not change the time format as it will follow the system settings.
// See `man strftime` or the documentation of g_date_time_format for the available specifiers: <https://docs.gtk.org/glib/method.DateTime.format.html>
format = gettext("Last seen at %H:%M");
} else {
// Translators: Time in 12h format, i.e. "11:04 PM".
// Do not change the time format as it will follow the system settings.
// See `man strftime` or the documentation of g_date_time_format for the available specifiers: <https://docs.gtk.org/glib/method.DateTime.format.html>
format = gettext("Last seen at %I:%M %p");
}
}
// Show the word "Yesterday" and time if date is on yesterday
else if days_ago == 1 {
if use_24 {
// Translators: this a time in 24h format, i.e. "Last seen yesterday at 23:04".
// Do not change the time format as it will follow the system settings.
// See `man strftime` or the documentation of g_date_time_format for the available specifiers: <https://docs.gtk.org/glib/method.DateTime.format.html>
// xgettext:no-c-format
format = gettext("Last seen yesterday at %H:%M");
} else {
// Translators: this is a time in 12h format, i.e. "Last seen Yesterday at 11:04
// PM".
// Do not change the time format as it will follow the system settings.
// See `man strftime` or the documentation of g_date_time_format for the available specifiers: <https://docs.gtk.org/glib/method.DateTime.format.html>
// xgettext:no-c-format
format = gettext("Last seen yesterday at %I:%M %p");
}
}
// Show a week day and time if date is in the last week
else if days_ago > 1 && days_ago < 7 {
if use_24 {
// Translators: this is the name of the week day followed by a time in 24h
// format, i.e. "Last seen Monday at 23:04".
// Do not change the time format as it will follow the system settings.
// See `man strftime` or the documentation of g_date_time_format for the available specifiers: <https://docs.gtk.org/glib/method.DateTime.format.html>
// xgettext:no-c-format
format = gettext("Last seen %A at %H:%M");
} else {
// Translators: this is the week day name followed by a time in 12h format, i.e.
// "Last seen Monday at 11:04 PM".
// Do not change the time format as it will follow the system settings.
// See `man strftime` or the documentation of g_date_time_format for the available specifiers: <https://docs.gtk.org/glib/method.DateTime.format.html>
// xgettext:no-c-format
format = gettext("Last seen %A at %I:%M %p");
}
} else if datetime.year() == now.year() {
if use_24 {
// Translators: this is the month and day and the time in 24h format, i.e. "Last
// seen February 3 at 23:04".
// Do not change the time format as it will follow the system settings.
// See `man strftime` or the documentation of g_date_time_format for the available specifiers: <https://docs.gtk.org/glib/method.DateTime.format.html>
// xgettext:no-c-format
format = gettext("Last seen %B %-e at %H:%M");
} else {
// Translators: this is the month and day and the time in 12h format, i.e. "Last
// seen February 3 at 11:04 PM".
// Do not change the time format as it will follow the system settings.
// See `man strftime` or the documentation of g_date_time_format for the available specifiers: <https://docs.gtk.org/glib/method.DateTime.format.html>
// xgettext:no-c-format
format = gettext("Last seen %B %-e at %I:%M %p");
}
} else if use_24 {
// Translators: this is the full date and the time in 24h format, i.e. "Last
// seen February 3 2015 at 23:04".
// Do not change the time format as it will follow the system settings.
// See `man strftime` or the documentation of g_date_time_format for the available specifiers: <https://docs.gtk.org/glib/method.DateTime.format.html>
// xgettext:no-c-format
format = gettext("Last seen %B %-e %Y at %H:%M");
} else {
// Translators: this is the full date and the time in 12h format, i.e. "Last
// seen February 3 2015 at 11:04 PM".
// Do not change the time format as it will follow the system settings.
// See `man strftime` or the documentation of g_date_time_format for the available specifiers: <https://docs.gtk.org/glib/method.DateTime.format.html>
// xgettext:no-c-format
format = gettext("Last seen %B %-e %Y at %I:%M %p");
}
let label = datetime
.format(&format)
.expect("formatting GDateTime works");
self.last_seen_ts.set_label(&label);
}
}
}
glib::wrapper! {
/// A row presenting a user session.
pub struct UserSessionRow(ObjectSubclass<imp::UserSessionRow>)
@extends gtk::Widget, gtk::ListBoxRow, @implements gtk::Accessible;
}
#[gtk::template_callbacks]
impl UserSessionRow {
pub fn new(user_session: &UserSession) -> Self {
glib::Object::builder()
.property("user-session", user_session)
.build()
}
/// Disconnect the user session.
#[template_callback]
async fn disconnect(&self) {
let Some(user_session) = self.user_session() else {
return;
};
if user_session.is_current() {
self.activate_action(
"account-settings.show-subpage",
Some(&AccountSettingsSubpage::LogOut.to_variant()),
)
.unwrap();
return;
}
let imp = self.imp();
imp.disconnect_button.set_is_loading(true);
match user_session.delete(self).await {
Ok(_) => self.set_visible(false),
Err(AuthError::UserCancelled) => {}
Err(_) => {
let device_name = user_session.display_name();
// Translators: Do NOT translate the content between '{' and '}', this is a
// variable name.
let error_message = gettext_f(
"Could not disconnect device “{device_name}”",
&[("device_name", &device_name)],
);
toast!(self, error_message);
}
}
imp.disconnect_button.set_is_loading(false);
}
}