From 9cbcf56218832f092eb311c85b92b9cf79fbaab3 Mon Sep 17 00:00:00 2001 From: "E.Smith" <31170571+azlm8t@users.noreply.github.com> Date: Thu, 16 Nov 2017 11:20:52 +0000 Subject: [PATCH] api: Add api convenience call for renaming channels. (#4715). 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 | 18 ++++++++++++++++++ src/channels.c | 24 ++++++++++++++++++++++++ src/channels.h | 4 ++++ 3 files changed, 46 insertions(+) diff --git a/src/api/api_channel.c b/src/api/api_channel.c index 4183eb8fb..4b6a2d94d 100644 --- a/src/api/api_channel.c +++ b/src/api/api_channel.c @@ -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 }, diff --git a/src/channels.c b/src/channels.c index f998a20d1..d7fb45388 100644 --- a/src/channels.c +++ b/src/channels.c @@ -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 ) { diff --git a/src/channels.h b/src/channels.h index 44fd95216..d10d0e7ab 100644 --- a/src/channels.h +++ b/src/channels.h @@ -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) -- 2.47.3