|
| 1 | +use gettextrs::gettext; |
| 2 | +use glib::clone; |
| 3 | +use gtk::glib; |
| 4 | +use gtk::prelude::*; |
| 5 | +use gtk::subclass::prelude::*; |
| 6 | + |
| 7 | +use super::UserStatusString; |
| 8 | +use crate::i18n::*; |
| 9 | +use crate::strings; |
| 10 | +use crate::tdlib::Chat; |
| 11 | +use crate::tdlib::ChatType; |
| 12 | + |
| 13 | +mod imp { |
| 14 | + use once_cell::sync::Lazy; |
| 15 | + use once_cell::unsync::OnceCell; |
| 16 | + |
| 17 | + use super::*; |
| 18 | + |
| 19 | + #[derive(Debug, Default)] |
| 20 | + pub(crate) struct ChatSubtitleString { |
| 21 | + pub(super) chat: OnceCell<Chat>, |
| 22 | + pub(super) user_status_string: OnceCell<UserStatusString>, |
| 23 | + } |
| 24 | + |
| 25 | + #[glib::object_subclass] |
| 26 | + impl ObjectSubclass for ChatSubtitleString { |
| 27 | + const NAME: &'static str = "ChatSubtitleString"; |
| 28 | + type Type = super::ChatSubtitleString; |
| 29 | + } |
| 30 | + |
| 31 | + impl ObjectImpl for ChatSubtitleString { |
| 32 | + fn properties() -> &'static [glib::ParamSpec] { |
| 33 | + static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| { |
| 34 | + vec![glib::ParamSpecString::builder("subtitle") |
| 35 | + .read_only() |
| 36 | + .build()] |
| 37 | + }); |
| 38 | + PROPERTIES.as_ref() |
| 39 | + } |
| 40 | + |
| 41 | + fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value { |
| 42 | + let obj = self.obj(); |
| 43 | + |
| 44 | + match pspec.name() { |
| 45 | + "subtitle" => obj.subtitle().to_value(), |
| 46 | + _ => unimplemented!(), |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +glib::wrapper! { |
| 53 | + pub(crate) struct ChatSubtitleString(ObjectSubclass<imp::ChatSubtitleString>); |
| 54 | +} |
| 55 | + |
| 56 | +impl ChatSubtitleString { |
| 57 | + pub(crate) fn new(chat: Chat) -> ChatSubtitleString { |
| 58 | + let obj: ChatSubtitleString = glib::Object::builder().build(); |
| 59 | + |
| 60 | + chat.actions() |
| 61 | + .connect_items_changed(clone!(@weak obj => move |_, _, _, _| { |
| 62 | + obj.notify("subtitle"); |
| 63 | + })); |
| 64 | + |
| 65 | + chat.connect_notify_local( |
| 66 | + Some("online-member-count"), |
| 67 | + clone!(@weak obj => move |_, _| { |
| 68 | + obj.notify("subtitle"); |
| 69 | + }), |
| 70 | + ); |
| 71 | + match chat.type_() { |
| 72 | + ChatType::BasicGroup(basic) => { |
| 73 | + basic.connect_notify_local( |
| 74 | + Some("member-count"), |
| 75 | + clone!(@weak obj => move |_, _| { |
| 76 | + obj.notify("subtitle"); |
| 77 | + }), |
| 78 | + ); |
| 79 | + } |
| 80 | + ChatType::Supergroup(group) => { |
| 81 | + group.connect_notify_local( |
| 82 | + Some("member-count"), |
| 83 | + clone!(@weak obj => move |_, _| { |
| 84 | + obj.notify("subtitle"); |
| 85 | + }), |
| 86 | + ); |
| 87 | + } |
| 88 | + ChatType::Private(user) if !chat.is_own_chat() => { |
| 89 | + let user_status_string = UserStatusString::new(user.clone()); |
| 90 | + user_status_string.connect_notify_local( |
| 91 | + Some("string"), |
| 92 | + clone!(@weak obj => move |_, _| { |
| 93 | + obj.notify("subtitle"); |
| 94 | + }), |
| 95 | + ); |
| 96 | + obj.imp() |
| 97 | + .user_status_string |
| 98 | + .set(user_status_string) |
| 99 | + .unwrap(); |
| 100 | + } |
| 101 | + ChatType::Secret(secret) => { |
| 102 | + let user = secret.user(); |
| 103 | + let user_status_string = UserStatusString::new(user.clone()); |
| 104 | + user_status_string.connect_notify_local( |
| 105 | + Some("string"), |
| 106 | + clone!(@weak obj => move |_, _| { |
| 107 | + obj.notify("subtitle"); |
| 108 | + }), |
| 109 | + ); |
| 110 | + obj.imp() |
| 111 | + .user_status_string |
| 112 | + .set(user_status_string) |
| 113 | + .unwrap(); |
| 114 | + } |
| 115 | + _ => (), |
| 116 | + } |
| 117 | + |
| 118 | + obj.imp().chat.set(chat).unwrap(); |
| 119 | + obj |
| 120 | + } |
| 121 | + |
| 122 | + pub(crate) fn subtitle(&self) -> String { |
| 123 | + let chat = self.imp().chat.get().unwrap(); |
| 124 | + let action = chat.actions().last(); |
| 125 | + if let Some(action) = action { |
| 126 | + strings::chat_action(&action) |
| 127 | + } else { |
| 128 | + format!( |
| 129 | + "{}{}", |
| 130 | + if !chat.is_own_chat() { |
| 131 | + match chat.type_() { |
| 132 | + ChatType::Private(_) | ChatType::Secret(_) => { |
| 133 | + if !chat.is_own_chat() { |
| 134 | + self.imp().user_status_string.get().unwrap().string() |
| 135 | + } else { |
| 136 | + String::new() |
| 137 | + } |
| 138 | + } |
| 139 | + ChatType::BasicGroup(basic) => { |
| 140 | + let m_count = basic.member_count(); |
| 141 | + match m_count { |
| 142 | + 0 => gettext("group"), |
| 143 | + _ => ngettext_f( |
| 144 | + "{num} member", |
| 145 | + "{num} members", |
| 146 | + m_count as u32, |
| 147 | + &[("num", &m_count.to_string())], |
| 148 | + ), |
| 149 | + } |
| 150 | + } |
| 151 | + ChatType::Supergroup(data) if data.is_channel() => { |
| 152 | + let m_count = data.member_count(); |
| 153 | + match m_count { |
| 154 | + 0 => gettext("channel"), |
| 155 | + _ => ngettext_f( |
| 156 | + "{num} subscriber", |
| 157 | + "{num} subscribers", |
| 158 | + m_count as u32, |
| 159 | + &[("num", &m_count.to_string())], |
| 160 | + ), |
| 161 | + } |
| 162 | + } |
| 163 | + ChatType::Supergroup(data) => { |
| 164 | + let m_count = data.member_count(); |
| 165 | + match m_count { |
| 166 | + 0 => gettext("group"), |
| 167 | + _ => ngettext_f( |
| 168 | + "{num} member", |
| 169 | + "{num} members", |
| 170 | + m_count as u32, |
| 171 | + &[("num", &m_count.to_string())], |
| 172 | + ), |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + } else { |
| 177 | + String::new() |
| 178 | + }, |
| 179 | + if chat.online_member_count() > 1 { |
| 180 | + format!( |
| 181 | + ", {}", |
| 182 | + ngettext_f( |
| 183 | + "{num} online", |
| 184 | + "{num} online", |
| 185 | + chat.online_member_count() as u32, |
| 186 | + &[("num", &chat.online_member_count().to_string())] |
| 187 | + ) |
| 188 | + ) |
| 189 | + } else { |
| 190 | + String::new() |
| 191 | + } |
| 192 | + ) |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments