Skip to main content

fractal/session/sidebar_data/section/
name.rs

1use std::fmt;
2
3use gettextrs::gettext;
4use gtk::glib;
5use serde::{Deserialize, Serialize};
6
7use crate::session::{RoomCategory, TargetRoomCategory};
8
9/// The possible names of the sections in the sidebar.
10#[derive(
11    Debug, Default, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, glib::Enum, Serialize, Deserialize,
12)]
13#[enum_type(name = "SidebarSectionName")]
14#[serde(rename_all = "kebab-case")]
15pub enum SidebarSectionName {
16    /// The section for verification requests.
17    VerificationRequest,
18    /// The section for invite requests.
19    InviteRequest,
20    /// The section for room invites.
21    Invited,
22    /// The section for favorite rooms.
23    Favorite,
24    /// The section for joined rooms without a tag.
25    #[default]
26    Normal,
27    /// The section for low-priority rooms.
28    LowPriority,
29    /// The section for room that were left.
30    Left,
31}
32
33impl SidebarSectionName {
34    /// Convert the given `RoomCategory` to a `SidebarSectionName`, if possible.
35    pub(crate) fn from_room_category(category: RoomCategory) -> Option<Self> {
36        let name = match category {
37            RoomCategory::Knocked => Self::InviteRequest,
38            RoomCategory::Invited => Self::Invited,
39            RoomCategory::Favorite => Self::Favorite,
40            RoomCategory::Normal => Self::Normal,
41            RoomCategory::LowPriority => Self::LowPriority,
42            RoomCategory::Left => Self::Left,
43            RoomCategory::Outdated | RoomCategory::Space | RoomCategory::Ignored => return None,
44        };
45
46        Some(name)
47    }
48
49    /// Convert this `SidebarSectionName` to a `RoomCategory`, if possible.
50    pub(crate) fn into_room_category(self) -> Option<RoomCategory> {
51        let category = match self {
52            Self::VerificationRequest => return None,
53            Self::InviteRequest => RoomCategory::Knocked,
54            Self::Invited => RoomCategory::Invited,
55            Self::Favorite => RoomCategory::Favorite,
56            Self::Normal => RoomCategory::Normal,
57            Self::LowPriority => RoomCategory::LowPriority,
58            Self::Left => RoomCategory::Left,
59        };
60
61        Some(category)
62    }
63
64    /// Convert this `SidebarSectionName` to a `TargetRoomCategory`, if
65    /// possible.
66    pub(crate) fn into_target_room_category(self) -> Option<TargetRoomCategory> {
67        let category = match self {
68            Self::VerificationRequest | Self::InviteRequest | Self::Invited => return None,
69            Self::Favorite => TargetRoomCategory::Favorite,
70            Self::Normal => TargetRoomCategory::Normal,
71            Self::LowPriority => TargetRoomCategory::LowPriority,
72            Self::Left => TargetRoomCategory::Left,
73        };
74
75        Some(category)
76    }
77}
78
79impl fmt::Display for SidebarSectionName {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        let label = match self {
82            SidebarSectionName::VerificationRequest => gettext("Verifications"),
83            SidebarSectionName::InviteRequest => gettext("Invite Requests"),
84            SidebarSectionName::Invited => gettext("Invited"),
85            SidebarSectionName::Favorite => gettext("Favorites"),
86            SidebarSectionName::Normal => gettext("Rooms"),
87            SidebarSectionName::LowPriority => gettext("Low Priority"),
88            SidebarSectionName::Left => gettext("Historical"),
89        };
90        f.write_str(&label)
91    }
92}