1 /*
2  *  API - Wizard interface
3  *
4  *  Copyright (C) 2015 Jaroslav Kysela
5  *
6  *  This program is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; withm even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "tvheadend.h"
21 #include "channels.h"
22 #include "access.h"
23 #include "api.h"
24 #include "config.h"
25 #include "input.h"
26 #include "wizard.h"
27 
28 static int
wizard_page(const char * page)29 wizard_page ( const char *page )
30 {
31   pthread_mutex_lock(&global_lock);
32   if (strcmp(page, config.wizard ?: "")) {
33     free(config.wizard);
34     config.wizard = page[0] ? strdup(page) : NULL;
35     idnode_changed(&config.idnode);
36   }
37   pthread_mutex_unlock(&global_lock);
38   return 0;
39 }
40 
41 static int
wizard_idnode_load_simple(access_t * perm,void * opaque,const char * op,htsmsg_t * args,htsmsg_t ** resp)42 wizard_idnode_load_simple
43   ( access_t *perm, void *opaque, const char *op, htsmsg_t *args, htsmsg_t **resp )
44 {
45   int r;
46   wizard_build_fcn_t fcn = opaque;
47   wizard_page_t *page = fcn(perm->aa_lang_ui);
48   r = api_idnode_load_simple(perm, &page->idnode, op, args, resp);
49   wizard_page(page->name);
50   page->free(page);
51   return r;
52 }
53 
54 static int
wizard_idnode_save_simple(access_t * perm,void * opaque,const char * op,htsmsg_t * args,htsmsg_t ** resp)55 wizard_idnode_save_simple
56   ( access_t *perm, void *opaque, const char *op, htsmsg_t *args, htsmsg_t **resp )
57 {
58   int r;
59   wizard_build_fcn_t fcn = opaque;
60   wizard_page_t *page = fcn(perm->aa_lang_ui);
61   r = api_idnode_save_simple(perm, &page->idnode, op, args, resp);
62   idnode_save_check(&page->idnode, 1);
63   page->free(page);
64   return r;
65 }
66 
67 static int
wizard_cancel(access_t * perm,void * opaque,const char * op,htsmsg_t * args,htsmsg_t ** resp)68 wizard_cancel
69   ( access_t *perm, void *opaque, const char *op, htsmsg_t *args, htsmsg_t **resp )
70 {
71   return wizard_page("");
72 }
73 
74 static int
wizard_start(access_t * perm,void * opaque,const char * op,htsmsg_t * args,htsmsg_t ** resp)75 wizard_start
76   ( access_t *perm, void *opaque, const char *op, htsmsg_t *args, htsmsg_t **resp )
77 {
78   return wizard_page("hello");
79 }
80 
81 static int
wizard_status_progress(access_t * perm,void * opaque,const char * op,htsmsg_t * args,htsmsg_t ** resp)82 wizard_status_progress
83   ( access_t *perm, void *opaque, const char *op, htsmsg_t *args, htsmsg_t **resp )
84 {
85   int64_t active = 0, total = 0, services = 0;
86   mpegts_service_t *s;
87   mpegts_mux_t *mm;
88   mpegts_network_t *mn;
89 
90   LIST_FOREACH(mn, &mpegts_network_all, mn_global_link) {
91     if (!mn->mn_wizard) continue;
92     LIST_FOREACH(mm, &mn->mn_muxes, mm_network_link) {
93       total++;
94       if (mm->mm_scan_state != MM_SCAN_STATE_IDLE)
95         active++;
96       LIST_FOREACH(s, &mm->mm_services, s_dvb_mux_link)
97         services++;
98     }
99   }
100   *resp = htsmsg_create_map();
101   htsmsg_add_dbl(*resp, "progress", total ? ((double)1.0 - ((double)active / (double)total)) : 1);
102   htsmsg_add_s64(*resp, "muxes", total);
103   htsmsg_add_s64(*resp, "services", services);
104   return 0;
105 }
106 
107 void
api_wizard_init(void)108 api_wizard_init ( void )
109 {
110   static api_hook_t ah[] = {
111     { "wizard/hello/load",   ACCESS_ADMIN, wizard_idnode_load_simple, wizard_hello },
112     { "wizard/hello/save",   ACCESS_ADMIN, wizard_idnode_save_simple, wizard_hello },
113     { "wizard/login/load",   ACCESS_ADMIN, wizard_idnode_load_simple, wizard_login },
114     { "wizard/login/save",   ACCESS_ADMIN, wizard_idnode_save_simple, wizard_login },
115     { "wizard/network/load", ACCESS_ADMIN, wizard_idnode_load_simple, wizard_network },
116     { "wizard/network/save", ACCESS_ADMIN, wizard_idnode_save_simple, wizard_network },
117     { "wizard/muxes/load",   ACCESS_ADMIN, wizard_idnode_load_simple, wizard_muxes },
118     { "wizard/muxes/save",   ACCESS_ADMIN, wizard_idnode_save_simple, wizard_muxes },
119     { "wizard/status/load",  ACCESS_ADMIN, wizard_idnode_load_simple, wizard_status },
120     { "wizard/status/save",  ACCESS_ADMIN, wizard_idnode_save_simple, wizard_status },
121     { "wizard/status/progress", ACCESS_ADMIN, wizard_status_progress, NULL },
122     { "wizard/mapping/load", ACCESS_ADMIN, wizard_idnode_load_simple, wizard_mapping },
123     { "wizard/mapping/save", ACCESS_ADMIN, wizard_idnode_save_simple, wizard_mapping },
124     { "wizard/channels/load", ACCESS_ADMIN, wizard_idnode_load_simple, wizard_channels },
125     { "wizard/channels/save", ACCESS_ADMIN, wizard_idnode_save_simple, wizard_channels },
126     { "wizard/start",        ACCESS_ADMIN, wizard_start, NULL },
127     { "wizard/cancel",       ACCESS_ADMIN, wizard_cancel, NULL },
128     { NULL },
129   };
130 
131   api_register_all(ah);
132 }
133