]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
api: Add api convenience call for renaming channels. (#4715).
authorE.Smith <31170571+azlm8t@users.noreply.github.com>
Thu, 16 Nov 2017 11:20:52 +0000 (11:20 +0000)
committerJaroslav Kysela <perex@perex.cz>
Thu, 16 Nov 2017 13:53:06 +0000 (14:53 +0100)
Add a convenience function for users to rename all channels
that match a particular name. Previously the caller needed
to parse an api/channel/list, extract each channel that
matched the name and then issue an idnode/save for the nodes.

This change allows people to easily fixup their names such as
updating channels that are broadcast with a lowercase name
(and so sort to the end of the list in many UIs); or prefixing
the channels with identifiers. Since the server does the channel
name lookup, it allows non-programmers to issue simple renames
from a script.

curl http://l:9981/api/channel/rename --data-urlencode 'from=dave' --data-urlencode 'to=Dave'

Issue: #4715.

src/api/api_channel.c
src/channels.c
src/channels.h

index 4183eb8fb1927ca5492b4769aaaa0f7dcc444bfc..4b6a2d94d2cc828bc7de37cf008de813306b6841 100644 (file)
@@ -95,6 +95,23 @@ api_channel_create
   return 0;
 }
 
+static int
+api_channel_rename
+  ( access_t *perm, void *opaque, const char *op, htsmsg_t *args, htsmsg_t **resp )
+{
+  const char *from, *to;
+  if (!(from = htsmsg_get_str(args, "from")))
+    return -EINVAL;
+  if (!(to = htsmsg_get_str(args, "to")))
+    return -EINVAL;
+  /* We need the lock since we are altering details */
+  pthread_mutex_lock(&global_lock);
+  const int num_match = channel_rename_and_save(from, to);
+  pthread_mutex_unlock(&global_lock);
+
+  return num_match > 0 ? 0 : -ENOENT;
+}
+
 static int
 api_channel_tag_list
   ( access_t *perm, void *opaque, const char *op, htsmsg_t *args, htsmsg_t **resp )
@@ -205,6 +222,7 @@ void api_channel_init ( void )
     { "channel/grid",    ACCESS_ANONYMOUS, api_idnode_grid,  api_channel_grid },
     { "channel/list",    ACCESS_ANONYMOUS, api_channel_list, NULL },
     { "channel/create",  ACCESS_ADMIN,     api_channel_create, NULL },
+    { "channel/rename",  ACCESS_ADMIN,     api_channel_rename, NULL }, /* User convenience function */
 
     { "channeltag/class",ACCESS_ANONYMOUS, api_idnode_class, (void*)&channel_tag_class },
     { "channeltag/grid", ACCESS_ANONYMOUS, api_idnode_grid,  api_channel_tag_grid },
index f998a20d1b1f2fbbdc75d8590aa38e8da5803351..d7fb45388dd6a6babaab8b3a58dd6df2b4749fd9 100644 (file)
@@ -783,6 +783,30 @@ channel_set_name ( channel_t *ch, const char *name )
   return save;
 }
 
+int
+channel_rename_and_save ( const char *from, const char *to )
+{
+  channel_t *ch;
+  const char *s;
+  int num_match = 0;
+
+  if (!from || !to || !*from || !*to)
+    return 0;
+
+  CHANNEL_FOREACH(ch) {
+    if (!ch->ch_enabled) continue;
+    s = channel_get_name(ch, NULL);
+    if (s == NULL) continue;
+    if (strcmp(s, from) == 0) {
+      ++num_match;
+      if (channel_set_name(ch, to)) {
+        idnode_changed(&ch->ch_id);
+      }
+    }
+  }
+  return num_match;
+}
+
 int64_t
 channel_get_number ( channel_t *ch )
 {
index 44fd95216547db631fdfab038ebf2131e37a4280..d10d0e7abcb666855a691aca7c4a1dfc025e678f 100644 (file)
@@ -176,6 +176,10 @@ int channel_tag_access(channel_tag_t *ct, struct access *a, int disabled);
 
 const char *channel_get_name ( channel_t *ch, const char *blank );
 int channel_set_name ( channel_t *ch, const char *name );
+/// User API convenience function to rename all channels that
+/// match "from". Lock must be held prior to call.
+/// @return number channels that matched "from".
+int channel_rename_and_save ( const char *from, const char *to );
 
 #define CHANNEL_SPLIT ((int64_t)1000000)