EpisodeWidget: Add local_size label that shows the amount of bytes downloaded.

This commit is contained in:
Jordan Petridis 2018-01-09 03:58:13 +02:00
parent 193117f579
commit 9dafb0ae9e
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
3 changed files with 90 additions and 31 deletions

View File

@ -120,10 +120,10 @@
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkLabel" id="size_label"> <object class="GtkLabel" id="local_size">
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="no_show_all">True</property> <property name="no_show_all">True</property>
<property name="label" translatable="yes">42 MB</property> <property name="label" translatable="yes">0 MB</property>
<property name="single_line_mode">True</property> <property name="single_line_mode">True</property>
<property name="track_visited_links">False</property> <property name="track_visited_links">False</property>
<style> <style>
@ -137,10 +137,10 @@
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkLabel" id="progress_label"> <object class="GtkLabel" id="prog_separator">
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="no_show_all">True</property> <property name="no_show_all">True</property>
<property name="label" translatable="yes">12 MB / 42 MB</property> <property name="label" translatable="yes">/</property>
<property name="single_line_mode">True</property> <property name="single_line_mode">True</property>
<property name="track_visited_links">False</property> <property name="track_visited_links">False</property>
<style> <style>
@ -153,6 +153,23 @@
<property name="position">5</property> <property name="position">5</property>
</packing> </packing>
</child> </child>
<child>
<object class="GtkLabel" id="total_size">
<property name="can_focus">False</property>
<property name="no_show_all">True</property>
<property name="label" translatable="yes">XX MB</property>
<property name="single_line_mode">True</property>
<property name="track_visited_links">False</property>
<style>
<class name="dim-label"/>
</style>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">6</property>
</packing>
</child>
</object> </object>
<packing> <packing>
<property name="expand">False</property> <property name="expand">False</property>

View File

@ -28,6 +28,10 @@ impl Progress {
}; };
ratio ratio
} }
pub fn get_total_size(&self) -> u64 {
self.total_bytes
}
} }
impl Default for Progress { impl Default for Progress {

View File

@ -17,8 +17,27 @@ use app::Action;
use manager; use manager;
use std::sync::mpsc::Sender; use std::sync::mpsc::Sender;
use std::sync::Arc;
use std::path::Path; use std::path::Path;
lazy_static! {
static ref SIZE_OPTS: Arc<size_opts::FileSizeOpts> = {
// Declare a custom humansize option struct
// See: https://docs.rs/humansize/1.0.2/humansize/file_size_opts/struct.FileSizeOpts.html
Arc::new(size_opts::FileSizeOpts {
divider: size_opts::Kilo::Binary,
units: size_opts::Kilo::Decimal,
decimal_places: 0,
decimal_zeroes: 0,
fixed_at: size_opts::FixedAt::No,
long_units: false,
space: true,
suffix: "",
allow_negative: false,
})
};
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct EpisodeWidget { pub struct EpisodeWidget {
pub container: gtk::Box, pub container: gtk::Box,
@ -28,11 +47,12 @@ pub struct EpisodeWidget {
title: gtk::Label, title: gtk::Label,
date: gtk::Label, date: gtk::Label,
duration: gtk::Label, duration: gtk::Label,
size: gtk::Label,
progress: gtk::ProgressBar, progress: gtk::ProgressBar,
progress_label: gtk::Label, total_size: gtk::Label,
local_size: gtk::Label,
separator1: gtk::Label, separator1: gtk::Label,
separator2: gtk::Label, separator2: gtk::Label,
prog_separator: gtk::Label,
} }
impl Default for EpisodeWidget { impl Default for EpisodeWidget {
@ -49,11 +69,12 @@ impl Default for EpisodeWidget {
let title: gtk::Label = builder.get_object("title_label").unwrap(); let title: gtk::Label = builder.get_object("title_label").unwrap();
let date: gtk::Label = builder.get_object("date_label").unwrap(); let date: gtk::Label = builder.get_object("date_label").unwrap();
let duration: gtk::Label = builder.get_object("duration_label").unwrap(); let duration: gtk::Label = builder.get_object("duration_label").unwrap();
let size: gtk::Label = builder.get_object("size_label").unwrap(); let local_size: gtk::Label = builder.get_object("local_size").unwrap();
let progress_label: gtk::Label = builder.get_object("progress_label").unwrap(); let total_size: gtk::Label = builder.get_object("total_size").unwrap();
let separator1: gtk::Label = builder.get_object("separator1").unwrap(); let separator1: gtk::Label = builder.get_object("separator1").unwrap();
let separator2: gtk::Label = builder.get_object("separator2").unwrap(); let separator2: gtk::Label = builder.get_object("separator2").unwrap();
let prog_separator: gtk::Label = builder.get_object("prog_separator").unwrap();
EpisodeWidget { EpisodeWidget {
container, container,
@ -63,11 +84,12 @@ impl Default for EpisodeWidget {
cancel, cancel,
title, title,
duration, duration,
size,
date, date,
progress_label, total_size,
local_size,
separator1, separator1,
separator2, separator2,
prog_separator,
} }
} }
} }
@ -87,7 +109,7 @@ impl EpisodeWidget {
self.set_title(episode); self.set_title(episode);
// Set the size label. // Set the size label.
self.set_size(episode.length()); self.set_total_size(episode.length());
// Set the duaration label. // Set the duaration label.
self.set_duration(episode.duration()); self.set_duration(episode.duration());
@ -164,33 +186,20 @@ impl EpisodeWidget {
} }
/// Set the Episode label dependings on its size /// Set the Episode label dependings on its size
fn set_size(&self, bytes: Option<i32>) { fn set_total_size(&self, bytes: Option<i32>) {
// Declare a custom humansize option struct
// See: https://docs.rs/humansize/1.0.2/humansize/file_size_opts/struct.FileSizeOpts.html
let custom_options = size_opts::FileSizeOpts {
divider: size_opts::Kilo::Binary,
units: size_opts::Kilo::Decimal,
decimal_places: 0,
decimal_zeroes: 0,
fixed_at: size_opts::FixedAt::No,
long_units: false,
space: true,
suffix: "",
allow_negative: false,
};
if let Some(size) = bytes { if let Some(size) = bytes {
if size != 0 { if size != 0 {
let s = size.file_size(custom_options); let s = size.file_size(SIZE_OPTS.clone());
if let Ok(s) = s { if let Ok(s) = s {
self.size.set_text(&s); self.total_size.set_text(&s);
self.size.show(); self.total_size.show();
self.separator2.show(); self.separator2.show();
} }
} }
}; };
} }
// FIXME: REFACTOR ME
fn determine_progess_bar(&self) { fn determine_progess_bar(&self) {
let id = WidgetExt::get_name(&self.container) let id = WidgetExt::get_name(&self.container)
.unwrap() .unwrap()
@ -204,10 +213,14 @@ impl EpisodeWidget {
let progress_bar = self.progress.clone(); let progress_bar = self.progress.clone();
if let Some(prog) = m.get(&id) { if let Some(prog) = m.get(&id) {
self.progress.show();
self.download.hide(); self.download.hide();
self.progress.show();
self.local_size.show();
self.total_size.show();
self.prog_separator.show();
self.cancel.show(); self.cancel.show();
// Setup a callback that will update the progress bar.
timeout_add( timeout_add(
400, 400,
clone!(prog => move || { clone!(prog => move || {
@ -236,6 +249,31 @@ impl EpisodeWidget {
} }
}), }),
); );
let total_size = self.total_size.clone();
// Setup a callback that will update the total_size label
// with the http ContentLength header number rather than
// relying to the RSS feed.
timeout_add(
500,
clone!(prog, total_size => move || {
let total_bytes = {
let m = prog.lock().unwrap();
m.get_total_size()
};
debug!("Total Size: {}", total_bytes);
if total_bytes != 0 {
let size = total_bytes.file_size(SIZE_OPTS.clone());
if let Ok(s) = size {
total_size.set_text(&s);
}
glib::Continue(false)
} else {
glib::Continue(true)
}
}),
);
} }
} }
} }