Skip to content

Instantly share code, notes, and snippets.

@adhadse
Last active October 7, 2022 01:33
Show Gist options
  • Save adhadse/2546e471044fa4b79a40cb921a31ff82 to your computer and use it in GitHub Desktop.
Save adhadse/2546e471044fa4b79a40cb921a31ff82 to your computer and use it in GitHub Desktop.
use once_cell::sync::Lazy;
use std::collections::{HashMap, HashSet};
use anyhow::Result;
use gtk::glib::{
self, clone, Enum, ObjectExt, ParamFlags, ParamSpec, ParamSpecEnum, ParamSpecObject, Sender, ToValue,
};
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use std::cell::Cell;
use std::collections::hash_map::Values;
use std::rc::Rc;
use std::str::FromStr;
use adw::gdk::{self, gdk_pixbuf};
use adw::glib::{List, OptionArg::String};
use gio::ffi::GInputStream;
use gio::ListStore;
use log::{debug, error};
use crate::settings::{settings_manager, Key};
use crate::library::utils;
use crate::database;
use crate::library::utils::EBook;
mod imp {
use adw::glib::{ParamSpecInt, ParamSpecString};
use super::*;
#[derive(Insertable, Eq, PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct Book {
pub identifier: Cell<i32>,
pub _type: EBook,
pub uri: Option<String>,
pub annotations_map: HashMap<String, String>,
pub bookmarks_set: HashSet<String>,
}
// checking if this works
// https://raw.githubusercontent.com/gtk-rs/examples/master/src/bin/listbox_model.rs
#[glib::object_subclass]
impl ObjectSubclass for Book {
const NAME: &'static str = "Book";
type ParentType = glib::Object;
type Class = glib::Class<Self>;
glib_object_subclass!();
}
impl ObjectImpl for Book {
fn properties() -> &'static [ParamSpec] {
static PROPERTIES: Lazy<Vec<ParamSpec>> = Lazy::new(|| {
vec![ParamSpecInt::new(
"identifier",
"Identifier",
"Identifier",
i32::MIN,
i32::MAX,
i32::default(),
ParamFlags::READABLE
),
ParamSpecEnum::new(
"_type",
"Type",
"Type",
EBook::static_type(),
EBook::default() as i32,
ParamFlags::READABLE,
),
ParamSpecString::new(
"uri",
"URI",
"URI",
Some(String::default().as_str()),
ParamFlags::READABLE
),
ParamSpecObject::new( // ParamSpecObject for HashMap and HashSet?
"annotations_map",
"AnnotationsMap",
"AnnotationsMap",
HashMap::, // what to write here?
ParamFlags::READWRITE
),
ParamSpecObject::new(
"bookmarks_set",
"BookmarksSet",
"BookmarksSet",
HashSet::, // What to write here?
ParamFlags::READWRITE
)
]
});
PROPERTIES.as_ref()
}
fn property(&self, obj: &Self::Type, _id: usize, pspec: &ParamSpec) -> glib::Value {
match pspec.name() {
"identifier" => self.identifier.to_value(),
"_type" => self._type.to_value(),
"uri" => self.uri.to_value(),
_ => unimplemented!(),
}
}
}
}
glib::wrapper! {
pub struct Book(ObjectSubclass<imp::Book>)
@extends glib::Object;
}
impl Book {
pub fn new(identifier: i32, _type: EBook, uri: String) -> Self {
let book = glib::Object::new::<Self>(
&[identifier, _type, uri, ]).unwrap();
book.storage
// book.set_property("identifier", identifier);
// book.set_property("_type", _type);
// book.set_property("uri", uri);
book
}
fn load_data(&self) {
self.imp().annotations_map.clear(); // Cannot borrow immutable local variable `self.imp().annotations_map` as mutable
// self.imp().bookmarks_set.replace(HashSet::default());
self.imp().bookmarks_set.clear(); // Cannot borrow immutable local variable `self.imp().annotations_map` as mutable
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment