diff options
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | 2022-02-09 22:47:38 +0100 |
---|---|---|
committer | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | 2022-02-10 01:45:37 +0100 |
commit | d35c0564b3f5dd10f6e23bf462bbfabd3084e486 (patch) | |
tree | 4354ccef627717e05ea4cf8abea50ab1f105b58a /tools/sticker-picker/src/sticker.rs | |
parent | 9735b6d6dc5ff6b5d577d2ba7511d74a9de99f38 (diff) | |
download | poezio-d35c0564b3f5dd10f6e23bf462bbfabd3084e486.tar.gz poezio-d35c0564b3f5dd10f6e23bf462bbfabd3084e486.tar.bz2 poezio-d35c0564b3f5dd10f6e23bf462bbfabd3084e486.tar.xz poezio-d35c0564b3f5dd10f6e23bf462bbfabd3084e486.zip |
Add a /sticker plugin
This plugin currently uploads the selected sticker every time, to the
HTTP File Upload service of the server (see XEP-0363), a future
optimisation would be to use XEP-0231 instead, for better caching on the
recipient side.
It relies on a helper tool to select the wanted sticker inside the pack,
a sample one is provided in tools/sticker-picker/, but it is not built
by default.
Diffstat (limited to 'tools/sticker-picker/src/sticker.rs')
-rw-r--r-- | tools/sticker-picker/src/sticker.rs | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/tools/sticker-picker/src/sticker.rs b/tools/sticker-picker/src/sticker.rs new file mode 100644 index 00000000..7fb44e8e --- /dev/null +++ b/tools/sticker-picker/src/sticker.rs @@ -0,0 +1,106 @@ +// This file is part of Poezio. +// +// Poezio is free software: you can redistribute it and/or modify +// it under the terms of the zlib license. See the COPYING file. + +use gtk::prelude::*; +use gtk::subclass::prelude::*; +use std::cell::RefCell; +use std::path::Path; + +#[derive(Debug, Default)] +pub struct Sticker { + filename: RefCell<Option<String>>, + texture: RefCell<Option<gdk::Texture>>, +} + +#[glib::object_subclass] +impl ObjectSubclass for Sticker { + const NAME: &'static str = "Sticker"; + type Type = StickerType; +} + +impl ObjectImpl for Sticker { + fn properties() -> &'static [glib::ParamSpec] { + use once_cell::sync::Lazy; + static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| { + vec![ + glib::ParamSpecString::new( + "filename", + "Filename", + "Filename", + None, + glib::ParamFlags::READWRITE | glib::ParamFlags::CONSTRUCT_ONLY, + ), + glib::ParamSpecObject::new( + "texture", + "Texture", + "Texture", + gdk::Texture::static_type(), + glib::ParamFlags::READWRITE | glib::ParamFlags::CONSTRUCT_ONLY, + ), + ] + }); + PROPERTIES.as_ref() + } + + fn set_property( + &self, + _obj: &StickerType, + _id: usize, + value: &glib::Value, + pspec: &glib::ParamSpec, + ) { + match pspec.name() { + "filename" => { + let filename = value.get().unwrap(); + self.filename.replace(filename); + } + "texture" => { + let texture = value.get().unwrap(); + self.texture.replace(texture); + } + _ => unimplemented!(), + } + } + + fn property(&self, _obj: &StickerType, _id: usize, pspec: &glib::ParamSpec) -> glib::Value { + match pspec.name() { + "filename" => self.filename.borrow().to_value(), + "texture" => self.texture.borrow().to_value(), + _ => unimplemented!(), + } + } +} + +glib::wrapper! { + pub struct StickerType(ObjectSubclass<Sticker>); +} + +impl StickerType { + pub fn new(filename: String, path: &Path) -> StickerType { + let texture = gdk::Texture::from_filename(path).unwrap(); + glib::Object::new(&[("filename", &filename), ("texture", &texture)]) + .expect("Failed to create Sticker") + } + + pub fn filename(&self) -> Option<String> { + let imp = self.imp(); + let filename = imp.filename.borrow(); + if let Some(filename) = filename.as_ref() { + Some(filename.clone()) + } else { + None + } + } + + pub fn texture(&self) -> Option<gdk::Texture> { + let imp = self.imp(); + let texture = imp.texture.borrow(); + if let Some(texture) = texture.as_ref() { + Some(texture.clone()) + } else { + None + } + } +} |