Compare commits
3 Commits
81080238af
...
fe09a79d2c
Author | SHA1 | Date | |
---|---|---|---|
fe09a79d2c | |||
c77141db1a | |||
786d9c462b |
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
|||
/target
|
||||
Cargo.lock
|
||||
/data
|
||||
|
|
45
src/main.rs
45
src/main.rs
|
@ -15,6 +15,7 @@
|
|||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use adw::prelude::*;
|
||||
use adw::subclass::prelude::ObjectSubclassIsExt;
|
||||
use gtk::{gio, glib};
|
||||
|
||||
mod message;
|
||||
|
@ -40,31 +41,40 @@ fn xep_0392(input: &str) -> String {
|
|||
format!("#{:02x}{:02x}{:02x}", r, g, b)
|
||||
}
|
||||
|
||||
fn on_login_pressed(win: &window::MainWindow) {
|
||||
fn on_login_pressed(win: window::MainWindow) {
|
||||
let jid = win.jid().text();
|
||||
let password = win.password().text();
|
||||
println!("jid={jid:?} password={password:?}");
|
||||
|
||||
if !jid.is_empty() && !password.is_empty() {
|
||||
win.stack().set_visible_child(win.spinner());
|
||||
|
||||
let (xmpp_receiver, cmd_sender) = xmpp_client::client(&jid, &password);
|
||||
let win2 = win.clone();
|
||||
// let win2 = win.clone();
|
||||
glib::spawn_future_local(async move {
|
||||
while let Ok(event) = xmpp_receiver.recv().await {
|
||||
match event {
|
||||
xmpp::Event::Online => {
|
||||
win2.stack().set_visible_child(win2.split_view());
|
||||
xmpp_client::XMPPEvent::Online => {
|
||||
win.stack().set_visible_child(win.split_view());
|
||||
}
|
||||
xmpp::Event::ContactAdded(jid) => {
|
||||
let tab = Tab::new(jid.jid.as_str(), jid.jid.as_str());
|
||||
win2.tabs_store().append(&tab);
|
||||
xmpp_client::XMPPEvent::Avatar(jid, avatar) => {
|
||||
if let Some(tab) = win.tabs_store().iter().find(|tab| {
|
||||
let tab: &glib::object::Object = tab.as_ref().unwrap();
|
||||
let tab: &Tab = tab.downcast_ref().unwrap();
|
||||
tab.jid() == jid.as_str()
|
||||
}) {
|
||||
let tab: &Tab = tab.as_ref().unwrap().downcast_ref().unwrap();
|
||||
tab.set_avatar_hash(avatar.clone());
|
||||
}
|
||||
}
|
||||
xmpp::Event::ChatMessage(_id, _from, _body, _time) => {
|
||||
// TODO: Insert message into tab history
|
||||
continue;
|
||||
xmpp_client::XMPPEvent::Contact(jid, avatar) => {
|
||||
let tab = Tab::new(jid.as_str(), jid.as_str());
|
||||
|
||||
if let Some(avatar) = avatar {
|
||||
tab.set_avatar_hash(avatar);
|
||||
}
|
||||
|
||||
win.tabs_store().append(&tab);
|
||||
}
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -108,7 +118,7 @@ fn main() {
|
|||
|
||||
let win2 = win.clone();
|
||||
win.login().connect_clicked(move |_| {
|
||||
on_login_pressed(&win2);
|
||||
on_login_pressed(win2.clone());
|
||||
});
|
||||
|
||||
assert!(Tab::static_type().is_valid());
|
||||
|
@ -154,9 +164,18 @@ fn main() {
|
|||
list_item.set_child(Some(&tab_widget));
|
||||
});
|
||||
tabs_factory.connect_bind(move |_, list_item| {
|
||||
// executed when switch tab, one time for previous tab and one time for new tab
|
||||
// but also when create new tab
|
||||
let list_item: >k::ListItem = list_item.downcast_ref().unwrap();
|
||||
let tab: Tab = list_item.item().and_downcast().unwrap();
|
||||
let tab_widget: widgets::ChatTab = list_item.child().and_downcast().unwrap();
|
||||
tab.bind_property(
|
||||
"avatar_hash",
|
||||
&tab_widget.imp().avatar.try_get().unwrap(),
|
||||
"file",
|
||||
)
|
||||
.sync_create()
|
||||
.build();
|
||||
|
||||
tab_widget.set_name(&tab.name());
|
||||
tab_widget.set_jid(&tab.jid());
|
||||
|
|
|
@ -25,8 +25,8 @@ mod imp {
|
|||
#[derive(glib::Properties)]
|
||||
#[properties(wrapper_type = super::Tab)]
|
||||
pub struct Tab {
|
||||
//#[property(get, set)]
|
||||
//avatar: RefCell<String>,
|
||||
#[property(get, set)]
|
||||
avatar_hash: RefCell<Option<String>>,
|
||||
#[property(get, construct_only)]
|
||||
jid: RefCell<String>,
|
||||
#[property(get, set)]
|
||||
|
@ -36,6 +36,7 @@ mod imp {
|
|||
impl Default for Tab {
|
||||
fn default() -> Self {
|
||||
Tab {
|
||||
avatar_hash: RefCell::new(None),
|
||||
jid: RefCell::new(String::new()),
|
||||
name: RefCell::new(String::new()),
|
||||
}
|
||||
|
|
|
@ -68,13 +68,21 @@ impl ChatTab {
|
|||
}
|
||||
|
||||
pub fn set_jid(&self, jid: &str) {
|
||||
let hash = "123456789abcdef123456789abcdef123456789a";
|
||||
self.imp().avatar.set_from_file(Some(format!(
|
||||
"/home/linkmauve/cache/poezio/avatars/{jid}/{hash}"
|
||||
)));
|
||||
self.set_tooltip_text(Some(jid));
|
||||
}
|
||||
|
||||
pub fn set_avatar_hash(&self, hash: &str) {
|
||||
// let xdg_cache = std::env::var("XDG_CACHE_HOME").unwrap_or(format!(
|
||||
// "{}/.cache",
|
||||
// std::env::var("HOME").expect("NO $HOME?!")
|
||||
// ));
|
||||
// let xdg_cache = PathBuf::from(xdg_cache);
|
||||
// let entry = xdg_cache.join("poezio/avatars").join(self.tooltip_text().unwrap()).join(hash);
|
||||
|
||||
// self.imp().avatar.set_from_file(Some(entry.to_str().unwrap().to_string()));
|
||||
self.imp().avatar.set_from_file(Some(hash));
|
||||
}
|
||||
|
||||
pub fn set_name(&self, name: &str) {
|
||||
self.imp().name.set_label(name);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
use async_channel::{Receiver, Sender};
|
||||
use tokio::runtime::Runtime;
|
||||
use xmpp::{BareJid, ClientBuilder, ClientFeature, Event};
|
||||
use xmpp::{BareJid, ClientBuilder, ClientFeature};
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
|
@ -9,13 +10,78 @@ pub enum XMPPCommand {
|
|||
SendPM(BareJid, String),
|
||||
}
|
||||
|
||||
fn tokio_runtime() -> &'static Runtime {
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum XMPPEvent {
|
||||
Online,
|
||||
Avatar(BareJid, String),
|
||||
Contact(BareJid, Option<String>),
|
||||
}
|
||||
|
||||
pub fn tokio_runtime() -> &'static Runtime {
|
||||
static RUNTIME: OnceLock<Runtime> = OnceLock::new();
|
||||
RUNTIME.get_or_init(|| Runtime::new().expect("Setting up tokio runtime needs to succeed."))
|
||||
}
|
||||
|
||||
pub(crate) fn client(jid: &str, password: &str) -> (Receiver<xmpp::Event>, Sender<XMPPCommand>) {
|
||||
let (event_sender, event_receiver) = async_channel::bounded::<xmpp::Event>(1);
|
||||
pub async fn handle_xmpp_event(event: xmpp::Event) -> Option<XMPPEvent> {
|
||||
match event {
|
||||
xmpp::Event::Online => {
|
||||
Some(XMPPEvent::Online)
|
||||
}
|
||||
xmpp::Event::AvatarRetrieved(jid, hash) => {
|
||||
// need update latest symlink?
|
||||
// xmpp-rs stores avatar in data/JID/HASH... we create data/JID/latest symlink points to relative HASH
|
||||
|
||||
let mut latest_link = PathBuf::from(&hash);
|
||||
let hash_name = latest_link
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_str()
|
||||
.unwrap()
|
||||
.to_string();
|
||||
latest_link.set_file_name("latest");
|
||||
|
||||
if let Ok(metadata) = tokio::fs::metadata(&latest_link).await {
|
||||
if metadata.is_symlink() {
|
||||
if let Ok(previous_latest) = tokio::fs::read_link(&latest_link).await {
|
||||
if previous_latest == PathBuf::from(&hash_name) {
|
||||
// We already have latest symlink
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Setting new latest symlink
|
||||
let _ = tokio::fs::symlink(hash_name, latest_link).await;
|
||||
|
||||
if jid.is_bare() {
|
||||
Some(XMPPEvent::Avatar(jid.to_bare(), hash))
|
||||
} else {
|
||||
// no avatar for FullJid (yet)
|
||||
None
|
||||
}
|
||||
}
|
||||
xmpp::Event::ContactAdded(jid) => {
|
||||
let avatar_jid = jid.jid.clone();
|
||||
if let Ok(hash_name) =
|
||||
tokio::fs::read_link(&format!("data/{}/latest", &avatar_jid)).await
|
||||
{
|
||||
let avatar = format!("data/{}/{}", &avatar_jid, hash_name.display());
|
||||
Some(XMPPEvent::Contact(avatar_jid, Some(avatar)))
|
||||
} else {
|
||||
Some(XMPPEvent::Contact(avatar_jid, None))
|
||||
}
|
||||
}
|
||||
xmpp::Event::ChatMessage(_id, _from, _body, _time) => {
|
||||
// TODO: Insert message into tab history
|
||||
None
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn client(jid: &str, password: &str) -> (Receiver<XMPPEvent>, Sender<XMPPCommand>) {
|
||||
let (event_sender, event_receiver) = async_channel::bounded::<XMPPEvent>(1);
|
||||
let (cmd_sender, cmd_receiver) = async_channel::bounded::<XMPPCommand>(1);
|
||||
|
||||
let jid = jid.to_string();
|
||||
|
@ -24,6 +90,7 @@ pub(crate) fn client(jid: &str, password: &str) -> (Receiver<xmpp::Event>, Sende
|
|||
tokio_runtime().spawn(async move {
|
||||
let mut client = ClientBuilder::new(BareJid::new(&jid).unwrap(), &password)
|
||||
.set_default_nick("xmpp-client")
|
||||
.enable_feature(ClientFeature::Avatars)
|
||||
.enable_feature(ClientFeature::ContactList)
|
||||
.build();
|
||||
|
||||
|
@ -31,19 +98,8 @@ pub(crate) fn client(jid: &str, password: &str) -> (Receiver<xmpp::Event>, Sende
|
|||
tokio::select! {
|
||||
Some(events) = client.wait_for_events() => {
|
||||
for event in events {
|
||||
match event {
|
||||
Event::Online => {
|
||||
event_sender.send(event).await.expect("BOOOOOOHOOOO");
|
||||
}
|
||||
Event::ContactAdded(_) => {
|
||||
event_sender.send(event).await.expect("BOOOOOOHOOOO");
|
||||
}
|
||||
Event::ChatMessage(_, _, _, _) => {
|
||||
event_sender.send(event).await.expect("BOOOHOOOO");
|
||||
}
|
||||
_ => {
|
||||
continue;
|
||||
}
|
||||
if let Some(parsed_event) = handle_xmpp_event(event).await {
|
||||
let _ = event_sender.send(parsed_event).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user