1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#![deny(missing_docs)]
#![allow(unused_doc_comments)]

//! This library contains some widgets for relm.
//!
//! These widgets got extracted from OMMUI-related projects so they can
//! be used in several of them.

#[macro_use]
extern crate log;
#[macro_use]
extern crate relm_derive;
#[macro_use]
extern crate relm;
#[macro_use]
extern crate snafu;

extern crate dbus;
extern crate dbus_udisks2;
extern crate gdk_pixbuf;
extern crate gio;
extern crate glib;
extern crate gtk;
extern crate humansize;
extern crate indexmap;
extern crate ommui_data;
extern crate pango;
extern crate uuid;

pub mod boolean_parameter_entry;
pub mod description_item;
pub mod description_listbox;
pub mod enumeration_parameter_entry;
pub mod error;
pub mod menu;
pub mod menu_page;
pub mod message;
pub mod numeric_parameter_entry;
pub mod parameter_editor;
pub mod parameter_query;
pub mod profile_list;
pub mod progress;
pub mod removable_drives_selector;
pub mod text_entry;

pub use error::Error;

use indexmap::IndexMap;
use ommui_data::samplic;
use snafu::ResultExt;
use std::path::Path;

/// The result returned by functions in this crate.
pub type Result<T, E = Error> = std::result::Result<T, E>;

/// A translation trait.
pub trait XTr: std::fmt::Debug {
    /// Translate a string.
    fn xtr(&self, s: &str) -> String;
}

impl XTr for indexmap::IndexMap<String, String> {
    fn xtr(&self, s: &str) -> String {
        if let Some(t) = self.get(&s.to_string()) {
            t.to_string()
        } else {
            s.to_string()
        }
    }
}

impl<T: XTr> XTr for std::sync::RwLock<T> {
    fn xtr(&self, s: &str) -> String {
        self.read().unwrap().xtr(s)
    }
}

impl<T: XTr> XTr for std::sync::Arc<T> {
    fn xtr(&self, s: &str) -> String {
        (**self).xtr(s)
    }
}

fn tr<S: AsRef<str>>(s: S) -> String {
    s.as_ref().to_string()
}

/// A trait for providing pixbufs.
pub trait Pixbufs {
    /// Get a pixbuf.
    fn get_pixbuf(&self, k: &str) -> Option<gdk_pixbuf::Pixbuf>;

    /// Get a pixbuf, or if not present, a default pixbuf.
    fn get_pixbuf_or_default(&self, k: &str) -> gdk_pixbuf::Pixbuf;
}

impl Pixbufs for indexmap::IndexMap<String, gdk_pixbuf::Pixbuf> {
    fn get_pixbuf(&self, k: &str) -> Option<gdk_pixbuf::Pixbuf> {
        self.get(&k.to_string()).cloned()
    }

    fn get_pixbuf_or_default(&self, k: &str) -> gdk_pixbuf::Pixbuf {
        self.get_pixbuf(k).unwrap_or_else(|| {
            gdk_pixbuf::Pixbuf::load_from_bytes_with_size(
                include_bytes!("../assets/default.svg").as_ref(),
                64u16,
            )
            .unwrap()
        })
    }
}

/// A trait for loading pixbufs from the icons of a source.
pub trait LoadPixbufs {
    /// The pixbufs type which gets loaded.
    type Loaded: Pixbufs;

    /// Load the pixbufs.
    fn load_pixbufs(&self) -> Result<Self::Loaded>;
}

impl<T: samplic::LoadableDevice> LoadPixbufs for T {
    type Loaded = IndexMap<String, gdk_pixbuf::Pixbuf>;

    fn load_pixbufs(&self) -> Result<Self::Loaded> {
        self.load_icon_index()
            .context(error::OmmuiData)?
            .into_iter()
            .map(|k| {
                let raw = self
                    .load_icon(k.as_str())
                    .context(error::OmmuiData)?;
                Ok((
                    k,
                    gdk_pixbuf::Pixbuf::load_from_bytes_with_size(
                        raw.as_bytes(),
                        64u16,
                    )
                    .unwrap(),
                ))
            })
            .collect::<Result<_>>()
    }
}

/// A trait for items loadable from in-memory byte arrays.
pub trait LoadFromBytes: Sized {
    /// Load the item with a maximum width and height.
    fn load_from_bytes_at_scale<B: Into<glib::Bytes>>(
        data: B,
        max_width: u16,
        max_height: u16,
    ) -> Result<Self>;

    /// Load the item with a defined pixel size.
    fn load_from_bytes_with_size<B: Into<glib::Bytes>>(
        data: B,
        pixelsize: u16,
    ) -> Result<Self> {
        Self::load_from_bytes_at_scale(data, pixelsize, pixelsize)
    }
}

impl LoadFromBytes for gdk_pixbuf::Pixbuf {
    fn load_from_bytes_at_scale<B: Into<glib::Bytes>>(
        data: B,
        max_width: u16,
        max_height: u16,
    ) -> Result<Self> {
        let b: glib::Bytes = data.into();
        let stream = gio::MemoryInputStream::new_from_bytes(&b);
        Ok(gdk_pixbuf::Pixbuf::new_from_stream_at_scale(
            &stream,
            i32::from(max_width),
            i32::from(max_height),
            true,
            None::<&gio::Cancellable>,
        )
        .context(error::GLib)?)
    }
}

/// A trait for items loadable from file.
pub trait LoadFromFile: Sized {
    /// Load the item with a maximum width and height.
    fn load_from_file_at_scale<P: AsRef<Path>>(
        filename: P,
        max_width: u16,
        max_height: u16,
    ) -> Result<Self>;

    /// Load the item with a defined pixel size.
    fn load_from_file_with_size<P: AsRef<Path>>(
        filename: P,
        pixelsize: u16,
    ) -> Result<Self> {
        Self::load_from_file_at_scale(filename, pixelsize, pixelsize)
    }
}

impl LoadFromFile for gdk_pixbuf::Pixbuf {
    fn load_from_file_at_scale<P: AsRef<Path>>(
        filename: P,
        max_width: u16,
        max_height: u16,
    ) -> Result<Self> {
        let file = gio::File::new_for_path(filename);
        use gio::FileExt;
        let stream =
            file.read(None::<&gio::Cancellable>).context(error::GLib)?;
        Ok(gdk_pixbuf::Pixbuf::new_from_stream_at_scale(
            &stream,
            i32::from(max_width),
            i32::from(max_height),
            true,
            None::<&gio::Cancellable>,
        )
        .context(error::GLib)?)
    }
}