/*
* The live tile: the newest stories as tagged updates (each a slide, the
* picture sliding up to the headline when there is one) and the unread
* count as the badge, over org.cont8nuum.Tiles1.
*/
#include <gio/gio.h>
#include <string.h>
#include "tiles.h"
#include "news.h"
#define STORIES 5
void news_tile_update(const char *tile) {
GPtrArray *latest = news_latest(STORIES);
for (guint i = 0; i < STORIES; i++) {
char *tag = g_strdup_printf("s%u", i);
if (i >= latest->len) {
c8_tiles_call("Remove", g_variant_new("(ss)", tile, tag));
g_free(tag);
continue;
}
struct story *s = latest->pdata[i];
GVariantBuilder b;
g_variant_builder_init(&b, G_VARIANT_TYPE("a{ss}"));
const char *image = news_settings.images ? news_image_path(s->image_url) :
NULL;
/* Starts the picture coming for the next time */
if (news_settings.images && s->image_url && !image) {
news_image(s->image_url);
}
g_variant_builder_add(&b, "{ss}", "heading", s->title);
g_variant_builder_add(&b, "{ss}", "text1", s->feed->name);
char *age = news_age(s->date);
if (age[0]) {
g_variant_builder_add(&b, "{ss}", "text2", age);
}
g_free(age);
if (image) {
g_variant_builder_add(&b, "{ss}", "image", image);
g_variant_builder_add(&b, "{ss}", "template", "peek");
g_variant_builder_add(&b, "{ss}", "wide.template", "imagetext");
} else {
g_variant_builder_add(&b, "{ss}", "template", "text");
}
c8_tiles_call("Update", g_variant_new("(ssa{ss})", tile, tag, &b));
g_free(tag);
}
g_ptr_array_unref(latest);
int unread = news_unread();
char badge[16] = "";
if (unread > 0) {
g_snprintf(badge, sizeof(badge), "%d", unread > 99 ? 99 : unread);
}
c8_tiles_call("SetBadge", g_variant_new("(ss)", tile, badge));
}