1 /*
2  *  Copyright (C) 2012-2018 Team Kodi
3  *  This file is part of Kodi - https://kodi.tv
4  *
5  *  SPDX-License-Identifier: GPL-2.0-or-later
6  *  See LICENSES/README.md for more information.
7  */
8 
9 #include "PVRTimersPath.h"
10 
11 #include "utils/StringUtils.h"
12 #include "utils/URIUtils.h"
13 
14 #include <cstdlib>
15 #include <string>
16 #include <vector>
17 
18 using namespace PVR;
19 
20 const std::string CPVRTimersPath::PATH_ADDTIMER = "pvr://timers/addtimer/";
21 const std::string CPVRTimersPath::PATH_NEW = "pvr://timers/new/";
22 const std::string CPVRTimersPath::PATH_TV_TIMERS = "pvr://timers/tv/timers/";
23 const std::string CPVRTimersPath::PATH_TV_TIMER_RULES = "pvr://timers/radio/timers/";
24 const std::string CPVRTimersPath::PATH_RADIO_TIMERS = "pvr://timers/tv/rules/";
25 const std::string CPVRTimersPath::PATH_RADIO_TIMER_RULES = "pvr://timers/radio/rules/";
26 
CPVRTimersPath(const std::string & strPath)27 CPVRTimersPath::CPVRTimersPath(const std::string& strPath)
28 {
29   Init(strPath);
30 }
31 
CPVRTimersPath(const std::string & strPath,int iClientId,int iParentId)32 CPVRTimersPath::CPVRTimersPath(const std::string& strPath, int iClientId, int iParentId)
33 {
34   if (Init(strPath))
35   {
36     // set/replace client and parent id.
37     m_path = StringUtils::Format("pvr://timers/%s/%s/%d/%d",
38                                  m_bRadio      ? "radio" : "tv",
39                                  m_bTimerRules ? "rules" : "timers",
40                                  iClientId,
41                                  iParentId);
42     m_iClientId = iClientId;
43     m_iParentId = iParentId;
44     m_bRoot = false;
45   }
46 }
47 
CPVRTimersPath(bool bRadio,bool bTimerRules)48 CPVRTimersPath::CPVRTimersPath(bool bRadio, bool bTimerRules)
49   : m_path(StringUtils::Format(
50         "pvr://timers/%s/%s", bRadio ? "radio" : "tv", bTimerRules ? "rules" : "timers")),
51     m_bValid(true),
52     m_bRoot(true),
53     m_bRadio(bRadio),
54     m_bTimerRules(bTimerRules)
55 {
56 }
57 
Init(const std::string & strPath)58 bool CPVRTimersPath::Init(const std::string& strPath)
59 {
60   std::string strVarPath(strPath);
61   URIUtils::RemoveSlashAtEnd(strVarPath);
62 
63   m_path = strVarPath;
64   const std::vector<std::string> segments = URIUtils::SplitPath(m_path);
65 
66   m_bValid = (((segments.size() == 4) || (segments.size() == 6)) &&
67                 (segments.at(1) == "timers") &&
68                 ((segments.at(2) == "radio") || (segments.at(2) == "tv")) &&
69                 ((segments.at(3) == "rules") || (segments.at(3) == "timers")));
70   m_bRoot = (m_bValid && (segments.size() == 4));
71   m_bRadio = (m_bValid && (segments.at(2) == "radio"));
72   m_bTimerRules = (m_bValid && (segments.at(3) == "rules"));
73 
74   if (!m_bValid || m_bRoot)
75   {
76     m_iClientId = -1;
77     m_iParentId = 0;
78   }
79   else
80   {
81     m_iClientId = std::stoi(segments.at(4));
82     m_iParentId = std::stoi(segments.at(5));
83   }
84 
85   return m_bValid;
86 }
87