From 7c5965e915f84aaafe59bf0627bbbc3079f3b159 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andreas=20=C3=96man?= <andreas@lonelycoder.com>
Date: Sun, 27 Apr 2008 13:43:50 +0000
Subject: [PATCH] Use better locking and set channel icon

---
 channels.c  |  5 ++++-
 epg.c       |  8 ++++++--
 epg_xmltv.c | 17 ++++++++++++++---
 3 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/channels.c b/channels.c
index 394b4ab3..83996220 100644
--- a/channels.c
+++ b/channels.c
@@ -308,6 +308,9 @@ channels_load(void)
 	if(v > 1)
 	  ch->ch_commercial_detection = v;
       }
+
+      if((x = config_get_str_sub(&cl, "icon", NULL)) != NULL)
+	ch->ch_icon = strdup(x);
     }
     config_free0(&cl);
   }
@@ -421,7 +424,7 @@ channel_settings_write(th_channel_t *ch)
 
   fprintf(fp, "name = %s\n", ch->ch_name);
   fprintf(fp, "channel-group = %s\n", ch->ch_group->tcg_name);
-
+  fprintf(fp, "icon = %s\n", ch->ch_icon);
   fprintf(fp, "commercial-detect = %s\n", 
 	  val2str(ch->ch_commercial_detection, commercial_detect_tab) ?: "?");
   fclose(fp);
diff --git a/epg.c b/epg.c
index 50ae86c2..d3e6a10c 100644
--- a/epg.c
+++ b/epg.c
@@ -477,8 +477,12 @@ epg_transfer_events(th_channel_t *ch, struct event_queue *src,
 
   epg_lock();
 
-  free(ch->ch_icon);
-  ch->ch_icon = icon ? strdup(icon) : NULL;
+
+  if(strcmp(icon ?: "", ch->ch_icon ?: "")) {
+    free(ch->ch_icon);
+    ch->ch_icon = icon ? strdup(icon) : NULL;
+    channel_settings_write(ch);
+  }
 
   TAILQ_FOREACH(e, src, e_link) {
 
diff --git a/epg_xmltv.c b/epg_xmltv.c
index 52142b3c..de43f203 100644
--- a/epg_xmltv.c
+++ b/epg_xmltv.c
@@ -17,6 +17,7 @@
  */
 
 #define _GNU_SOURCE
+#include <errno.h>
 #include <string.h>
 
 #include <sys/types.h>
@@ -548,7 +549,6 @@ static void
 regrab(void *aux, int64_t now)
 {
   xmltv_grabber_t *xg = aux;
-
   xmltv_grabber_enqueue(xg);
 }
 
@@ -559,10 +559,21 @@ static void
 xmltv_xfer(void *aux, int64_t now)
 {
   xmltv_grabber_t *xg = aux;
+  int t;
 
-  xmltv_transfer_events(xg);
+  /* We don't want to stall waiting for the xml decoding which
+     can take quite some time, instead retry in a second if we fail
+     to obtain mutex */
 
-  dtimer_arm(&xg->xg_xfer_timer, xmltv_xfer, xg, 60);
+  if(pthread_mutex_trylock(&xg->xg_mutex) == EBUSY) {
+    t = 1;
+  } else {
+    xmltv_transfer_events(xg);
+    pthread_mutex_unlock(&xg->xg_mutex);
+    t = 60;
+  }
+
+  dtimer_arm(&xg->xg_xfer_timer, xmltv_xfer, xg, t);
 }
 
 /**