Skip to content

Commit

Permalink
Improved avatar aggregation. Further fixes for #37
Browse files Browse the repository at this point in the history
  • Loading branch information
pulb committed Oct 6, 2016
1 parent c163793 commit 8de7df4
Showing 1 changed file with 62 additions and 33 deletions.
95 changes: 62 additions & 33 deletions aggregate-avatars.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit 8de7df4

Please sign in to comment.