From 8de7df4ede9410465af3c47625d4380fec391804 Mon Sep 17 00:00:00 2001 From: Patrick Ulbrich Date: Thu, 6 Oct 2016 19:31:10 +0200 Subject: [PATCH] Improved avatar aggregation. Further fixes for #37 --- aggregate-avatars.vala | 95 +++++++++++++++++++++++++++--------------- 1 file changed, 62 insertions(+), 33 deletions(-) diff --git a/aggregate-avatars.vala b/aggregate-avatars.vala index bd779a9..d73ee32 100644 --- a/aggregate-avatars.vala +++ b/aggregate-avatars.vala @@ -25,48 +25,77 @@ using Folks; -MainLoop mainloop = null; - -void main() +static int main(string args[]) { - mainloop = new MainLoop(); - + MainLoop mainloop = new MainLoop(); var aggregator = IndividualAggregator.dup(); - aggregator.prepare(); - + + aggregator.prepare.begin(); + Idle.add(() => { if (!aggregator.is_quiescent) return true; - - var sb = new StringBuilder(); - - foreach (var e in aggregator.individuals.entries) + + aggregate_async.begin(mainloop, aggregator); + return false; + }); + + mainloop.run(); + return 0; +} + +async void aggregate_async(MainLoop mainloop, IndividualAggregator aggregator) +{ + File cache_dir = File.new_for_path( + Environment.get_user_runtime_dir()).get_child("aggregate-avatars-cache"); + + if (!cache_dir.query_exists()) + cache_dir.make_directory_with_parents(); + + var sb = new StringBuilder(); + + foreach (var e in aggregator.individuals.entries) + { + OutputStream dst_stream = null; + InputStream src_stream = null; + + try { Individual individual = e.value; - FileIcon file_icon = individual.avatar as FileIcon; - - if (file_icon != null) + File dst_file = cache_dir.get_child(individual.id); + + if (dst_file.query_exists()) + dst_file.delete(); + + if ((individual.avatar != null) && !individual.email_addresses.is_empty) { - File file = file_icon.get_file(); - - if (file.query_exists()) - { - foreach (var email in individual.email_addresses) - sb.append_printf("%s;%s;", email.value.strip(), file.get_path()); - } - } + src_stream = individual.avatar.load(-1, null, null); + dst_stream = dst_file.replace(null, false, FileCreateFlags.PRIVATE); + dst_stream.splice(src_stream, OutputStreamSpliceFlags.NONE); + + foreach (var email in individual.email_addresses) + sb.append_printf("%s;%s;", email.value.strip(), dst_file.get_path()); + } } - - if (sb.len > 0) + catch (Error e) { - stdout.printf(sb.truncate(sb.len - 1).str); - stdout.flush(); + stderr.printf("Error: %s\n", e.message); } - - mainloop.quit(); - - return false; - }); - - mainloop.run(); + finally + { + try + { + if (src_stream != null) src_stream.close(); + if (dst_stream != null) dst_stream.close(); + } catch (IOError e) {} + } + } + + if (sb.len > 0) + { + stdout.printf(sb.truncate(sb.len - 1).str); + stdout.flush(); + } + + mainloop.quit(); }