[SCM] kodi-pvr-hts/master: Refactor settings, step 2 (Coverity, refactor defaults, data encapsulation, remove settings global vars).

tiber-guest at users.alioth.debian.org tiber-guest at users.alioth.debian.org
Wed Mar 2 23:01:46 UTC 2016


The following commit has been merged in the master branch:
commit d7a59dd0339982d7633f8a59326e66b480db1799
Author: Kai Sommerfeld <kai.sommerfeld at gmx.com>
Date:   Thu Oct 15 17:12:10 2015 +0200

    Refactor settings, step 2 (Coverity, refactor defaults, data encapsulation, remove settings global vars).

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 697c5f3..e6f56bb 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -33,6 +33,7 @@ set(HTS_SOURCES src/AsyncState.cpp
                 src/xbmc_codec_descriptor.hpp)
 
 set(HTS_SOURCES_TVHEADEND
+                src/tvheadend/Settings.cpp
                 src/tvheadend/Settings.h)
 
 set(HTS_SOURCES_TVHEADEND_ENTITY
diff --git a/src/AutoRecordings.cpp b/src/AutoRecordings.cpp
index dd9f9ca..87e12e2 100644
--- a/src/AutoRecordings.cpp
+++ b/src/AutoRecordings.cpp
@@ -182,14 +182,14 @@ PVR_ERROR AutoRecordings::SendAutorecAdd(const PVR_TIMER &timer)
   /*                              => end time in kodi   = end of starting window in tvh     */
   const Settings &settings = Settings::GetInstance();
 
-  if (settings.bAutorecApproxTime)
+  if (settings.GetAutorecApproxTime())
   {
     /* Not sending causes server to set start and startWindow to any time */
     if (timer.startTime > 0 && !timer.bStartAnyTime)
     {
       struct tm *tm_start = localtime(&timer.startTime);
-      int32_t startWindowBegin = tm_start->tm_hour * 60 + tm_start->tm_min - settings.iAutorecMaxDiff;
-      int32_t startWindowEnd = tm_start->tm_hour * 60 + tm_start->tm_min + settings.iAutorecMaxDiff;
+      int32_t startWindowBegin = tm_start->tm_hour * 60 + tm_start->tm_min - settings.GetAutorecMaxDiff();
+      int32_t startWindowEnd = tm_start->tm_hour * 60 + tm_start->tm_min + settings.GetAutorecMaxDiff();
 
       /* Past midnight correction */
       if (startWindowBegin < 0)
diff --git a/src/HTSPConnection.cpp b/src/HTSPConnection.cpp
index 958f538..c2ba412 100644
--- a/src/HTSPConnection.cpp
+++ b/src/HTSPConnection.cpp
@@ -112,13 +112,13 @@ std::string CHTSPConnection::GetWebURL ( const char *fmt, ... )
   const Settings &settings = Settings::GetInstance();
 
   // Generate the authentication string (user:pass@)
-  std::string auth = settings.strUsername;
-  if (!(auth.empty() || settings.strPassword.empty()))
-    auth += ":" + settings.strPassword;
+  std::string auth = settings.GetUsername();
+  if (!(auth.empty() || settings.GetPassword().empty()))
+    auth += ":" + settings.GetPassword();
   if (!auth.empty())
     auth += "@";
 
-  std::string url = StringUtils::Format("http://%s%s:%d", auth.c_str(), settings.strHostname.c_str(), settings.iPortHTTP);
+  std::string url = StringUtils::Format("http://%s%s:%d", auth.c_str(), settings.GetHostname().c_str(), settings.GetPortHTTP());
 
   CLockObject lock(m_mutex);
   va_start(va, fmt);
@@ -133,7 +133,7 @@ bool CHTSPConnection::WaitForConnection ( void )
 {
   if (!m_ready) {
     tvhtrace("waiting for registration...");
-    m_regCond.Wait(m_mutex, m_ready, Settings::GetInstance().iConnectTimeout);
+    m_regCond.Wait(m_mutex, m_ready, Settings::GetInstance().GetConnectTimeout());
   }
   return m_ready;
 }
@@ -155,7 +155,7 @@ std::string CHTSPConnection::GetServerString ( void )
   const Settings &settings = Settings::GetInstance();
 
   CLockObject lock(m_mutex);
-  return StringUtils::Format("%s:%d [%s]", settings.strHostname.c_str(), settings.iPortHTSP,
+  return StringUtils::Format("%s:%d [%s]", settings.GetHostname().c_str(), settings.GetPortHTSP(),
              m_ready ? "connected" : "disconnected");
 }
 
@@ -228,7 +228,7 @@ bool CHTSPConnection::ReadMessage ( void )
   cnt = 0;
   while (cnt < len)
   {
-    r = m_socket->Read((char*)buf + cnt, len - cnt, Settings::GetInstance().iResponseTimeout);
+    r = m_socket->Read((char*)buf + cnt, len - cnt, Settings::GetInstance().GetResponseTimeout());
     if (r < 0)
     {
       tvherror("failed to read packet (%s)",
@@ -325,7 +325,7 @@ bool CHTSPConnection::SendMessage0 ( const char *method, htsmsg_t *msg )
 htsmsg_t *CHTSPConnection::SendAndWait0 ( const char *method, htsmsg_t *msg, int iResponseTimeout )
 {
   if (iResponseTimeout == -1)
-    iResponseTimeout = Settings::GetInstance().iResponseTimeout;
+    iResponseTimeout = Settings::GetInstance().GetResponseTimeout();
   
   uint32_t seq;
 
@@ -386,7 +386,7 @@ htsmsg_t *CHTSPConnection::SendAndWait0 ( const char *method, htsmsg_t *msg, int
 htsmsg_t *CHTSPConnection::SendAndWait ( const char *method, htsmsg_t *msg, int iResponseTimeout )
 {
   if (iResponseTimeout == -1)
-    iResponseTimeout = Settings::GetInstance().iResponseTimeout;
+    iResponseTimeout = Settings::GetInstance().GetResponseTimeout();
   
   if (!WaitForConnection())
     return NULL;
@@ -473,8 +473,8 @@ bool CHTSPConnection::SendAuth
  */
 void CHTSPConnection::Register ( void )
 {
-  std::string user = Settings::GetInstance().strUsername;
-  std::string pass = Settings::GetInstance().strPassword;
+  std::string user = Settings::GetInstance().GetUsername();
+  std::string pass = Settings::GetInstance().GetPassword();
 
   {
     CLockObject lock(m_mutex);
@@ -531,10 +531,10 @@ void* CHTSPConnection::Process ( void )
   {
     tvhdebug("new connection requested");
 
-    std::string host = settings.strHostname;
+    std::string host = settings.GetHostname();
     int port, timeout;
-    port = settings.iPortHTSP;
-    timeout = settings.iConnectTimeout;
+    port = settings.GetPortHTSP();
+    timeout = settings.GetConnectTimeout();
 
     /* Create socket (ensure mutex protection) */
     {
diff --git a/src/HTSPDemuxer.cpp b/src/HTSPDemuxer.cpp
index b4ced4a..5de5e0b 100644
--- a/src/HTSPDemuxer.cpp
+++ b/src/HTSPDemuxer.cpp
@@ -172,7 +172,7 @@ bool CHTSPDemuxer::Seek
   htsmsg_destroy(m);
 
   /* Wait for time */
-  if (!m_seekCond.Wait(m_conn.Mutex(), m_seekTime, Settings::GetInstance().iResponseTimeout))
+  if (!m_seekCond.Wait(m_conn.Mutex(), m_seekTime, Settings::GetInstance().GetResponseTimeout()))
   {
     tvherror("failed to get subscriptionSeek response");
     return false;
diff --git a/src/Tvheadend.cpp b/src/Tvheadend.cpp
index af60b74..971a441 100644
--- a/src/Tvheadend.cpp
+++ b/src/Tvheadend.cpp
@@ -33,10 +33,10 @@ using namespace tvheadend::entity;
 
 CTvheadend::CTvheadend()
   : m_streamchange(false), m_vfs(m_conn),
-    m_queue((size_t)-1), m_asyncState(Settings::GetInstance().iResponseTimeout),
+    m_queue((size_t)-1), m_asyncState(Settings::GetInstance().GetResponseTimeout()),
     m_timeRecordings(m_conn), m_autoRecordings(m_conn)
 {
-  for (int i = 0; i < 1 || i < Settings::GetInstance().iTotalTuners; i++)
+  for (int i = 0; i < 1 || i < Settings::GetInstance().GetTotalTuners(); i++)
   {
     m_dmx.push_back(new CHTSPDemuxer(m_conn));
   }
@@ -279,7 +279,7 @@ PVR_ERROR CTvheadend::SendDvrDelete ( uint32_t id, const char *method )
 
   /* Send and wait a bit longer than usual */
   if ((m = m_conn.SendAndWait(method, m,
-            std::max(30000, Settings::GetInstance().iResponseTimeout))) == NULL)
+            std::max(30000, Settings::GetInstance().GetResponseTimeout()))) == NULL)
     return PVR_ERROR_SERVER_ERROR;
 
   /* Check for error */
@@ -704,7 +704,7 @@ PVR_ERROR CTvheadend::GetTimerTypes ( PVR_TIMER_TYPE types[], int *size )
       TIMER_REPEATING_EPG_ATTRIBS |= PVR_TIMER_TYPE_SUPPORTS_RECORD_ONLY_NEW_EPISODES;
     }
 
-    if (!Settings::GetInstance().bAutorecApproxTime)
+    if (!Settings::GetInstance().GetAutorecApproxTime())
     {
       /* We need the end time to represent the end of the tvh starting window */
       TIMER_REPEATING_EPG_ATTRIBS |= PVR_TIMER_TYPE_SUPPORTS_END_TIME;
@@ -1065,7 +1065,7 @@ PVR_ERROR CTvheadend::GetEpg
            (long long)start, (long long)end);
 
   /* Async transfer */
-  if (Settings::GetInstance().bAsyncEpg)
+  if (Settings::GetInstance().GetAsyncEpg())
   {
     if (!m_asyncState.WaitForState(ASYNC_DONE))
       return PVR_ERROR_FAILED;
@@ -1167,7 +1167,7 @@ bool CTvheadend::Connected ( void )
   m_asyncState.SetState(ASYNC_NONE);
   
   msg = htsmsg_create_map();
-  htsmsg_add_u32(msg, "epg", Settings::GetInstance().bAsyncEpg);
+  htsmsg_add_u32(msg, "epg", Settings::GetInstance().GetAsyncEpg());
   //htsmsg_add_u32(msg, "epgMaxTime", 0);
   //htsmsg_add_s64(msg, "lastUpdate", 0);
   if ((msg = m_conn.SendAndWait0("enableAsyncMetadata", msg)) == NULL)
@@ -1401,7 +1401,7 @@ void CTvheadend::SyncDvrCompleted ( void )
 void CTvheadend::SyncEpgCompleted ( void )
 {
   /* Done */
-  if (!Settings::GetInstance().bAsyncEpg || m_asyncState.GetState() > ASYNC_EPG)
+  if (!Settings::GetInstance().GetAsyncEpg() || m_asyncState.GetState() > ASYNC_EPG)
     return;
 
   /* Schedules */
@@ -2056,8 +2056,8 @@ DemuxPacket* CTvheadend::DemuxRead ( void )
       pkt = dmx->Read();
     else
     {
-      if (dmx->GetChannelId() && Settings::GetInstance().iPreTuneCloseDelay &&
-          dmx->GetLastUse() + Settings::GetInstance().iPreTuneCloseDelay < time(NULL))
+      if (dmx->GetChannelId() && Settings::GetInstance().GetPreTunerCloseDelay() &&
+          dmx->GetLastUse() + Settings::GetInstance().GetPreTunerCloseDelay() < time(NULL))
       {
         tvhtrace("untuning channel %u on subscription %u",
                  m_channels[dmx->GetChannelId()].GetNum(), dmx->GetSubscriptionId());
diff --git a/src/Tvheadend.h b/src/Tvheadend.h
index 25a61fb..f41004e 100644
--- a/src/Tvheadend.h
+++ b/src/Tvheadend.h
@@ -80,7 +80,7 @@ extern "C" {
 #define tvhdebug(...) tvhlog(ADDON::LOG_DEBUG, ##__VA_ARGS__)
 #define tvhinfo(...)  tvhlog(ADDON::LOG_INFO,  ##__VA_ARGS__)
 #define tvherror(...) tvhlog(ADDON::LOG_ERROR, ##__VA_ARGS__)
-#define tvhtrace(...) if (tvheadend::Settings::GetInstance().bTraceDebug) tvhlog(ADDON::LOG_DEBUG, ##__VA_ARGS__)
+#define tvhtrace(...) if (tvheadend::Settings::GetInstance().GetTraceDebug()) tvhlog(ADDON::LOG_DEBUG, ##__VA_ARGS__)
 static inline void tvhlog ( ADDON::addon_log_t lvl, const char *fmt, ... )
 {
   char buf[16384];
diff --git a/src/client.cpp b/src/client.cpp
index 28fb172..8af1b45 100644
--- a/src/client.cpp
+++ b/src/client.cpp
@@ -29,6 +29,7 @@
 using namespace std;
 using namespace ADDON;
 using namespace PLATFORM;
+using namespace tvheadend;
 
 /* **************************************************************************
  * Global variables
@@ -41,27 +42,9 @@ ADDON_STATUS m_CurStatus = ADDON_STATUS_UNKNOWN;
 bool         m_bAlertHtspVersionMismatch = true;
 
 /*
- * Global configuration
- */
-CMutex     g_mutex;
-string     g_strHostname         = DEFAULT_HOST;
-int        g_iPortHTSP           = DEFAULT_HTSP_PORT;
-int        g_iPortHTTP           = DEFAULT_HTTP_PORT;
-int        g_iConnectTimeout     = DEFAULT_CONNECT_TIMEOUT;
-int        g_iResponseTimeout    = DEFAULT_RESPONSE_TIMEOUT;
-string     g_strUsername         = "";
-string     g_strPassword         = "";
-bool       g_bTraceDebug         = false;
-bool       g_bAsyncEpg           = false;
-bool       g_bPreTunerEnabled    = false;
-int        g_iTotalTuners        = DEFAULT_TOTAL_TUNERS;
-int        g_iPreTunerCloseDelay = DEFAULT_PRETUNER_CLOSEDELAY;
-int        g_iAutorecApproxTime  = DEFAULT_APPROX_TIME;
-int        g_iAutorecMaxDiff     = DEFAULT_AUTOREC_MAXDIFF;
-
-/*
- * Global state
+ * Globals
  */
+CMutex g_mutex;
 CHelper_libXBMC_addon *XBMC      = NULL;
 CHelper_libXBMC_pvr   *PVR       = NULL;
 CHelper_libXBMC_codec *CODEC     = NULL;
@@ -76,44 +59,7 @@ extern "C" {
 
 void ADDON_ReadSettings(void)
 {
-#define UPDATE_INT(var, key, def)\
-  if (!XBMC->GetSetting(key, &var))\
-    var = def;
-
-#define UPDATE_STR(var, key, tmp, def)\
-  if (XBMC->GetSetting(key, tmp))\
-    var = tmp;\
-  else\
-    var = def;
-
-  char buffer[1024];
-
-  /* Connection */
-  UPDATE_STR(g_strHostname, "host", buffer, DEFAULT_HOST);
-  UPDATE_STR(g_strUsername, "user", buffer, "");
-  UPDATE_STR(g_strPassword, "pass", buffer, "");
-  UPDATE_INT(g_iPortHTSP,   "htsp_port", DEFAULT_HTSP_PORT);
-  UPDATE_INT(g_iPortHTTP,   "http_port", DEFAULT_HTTP_PORT);
-  UPDATE_INT(g_iConnectTimeout,  "connect_timeout",  DEFAULT_CONNECT_TIMEOUT);
-  UPDATE_INT(g_iResponseTimeout, "response_timeout", DEFAULT_RESPONSE_TIMEOUT);
-  UPDATE_INT(g_iTotalTuners,  "total_tuners",  DEFAULT_TOTAL_TUNERS);
-  UPDATE_INT(g_iPreTunerCloseDelay, "pretuner_closedelay",  DEFAULT_PRETUNER_CLOSEDELAY);
-  UPDATE_INT(g_bPreTunerEnabled, "pretuner_enabled", false);
-
-  /* Data Transfer */
-  UPDATE_INT(g_bAsyncEpg,   "epg_async", false);
-
-  /* Debug */
-  UPDATE_INT(g_bTraceDebug, "trace_debug", false);
-
-  /* Auto recordings */
-  UPDATE_INT(g_iAutorecApproxTime, "autorec_approxtime", DEFAULT_APPROX_TIME);
-  UPDATE_INT(g_iAutorecMaxDiff, "autorec_maxdiff", DEFAULT_AUTOREC_MAXDIFF);
-
-  /* TODO: Transcoding */
-
-#undef UPDATE_INT
-#undef UPDATE_STR
+  Settings::GetInstance().ReadSettings();
 }
 
 ADDON_STATUS ADDON_Create(void* hdl, void* _unused(props))
@@ -138,35 +84,6 @@ ADDON_STATUS ADDON_Create(void* hdl, void* _unused(props))
   tvhinfo("starting PVR client");
 
   ADDON_ReadSettings();
-  
-  /* Create a settings object that can be used without locks */
-  tvheadend::Settings &settings = tvheadend::Settings::GetInstance();
-  settings.strHostname = g_strHostname;
-  settings.iPortHTSP = g_iPortHTSP;
-  settings.iPortHTTP = g_iPortHTTP;
-  settings.strUsername = g_strUsername;
-  settings.strPassword = g_strPassword;
-  settings.bTraceDebug = g_bTraceDebug;
-  settings.bAsyncEpg = g_bAsyncEpg;
-
-  /* Timeouts are defined in seconds but we expect them to be in milliseconds. */
-  settings.iConnectTimeout = (g_iConnectTimeout * 1000);
-  settings.iResponseTimeout = (g_iResponseTimeout * 1000);
-
-  if (g_bPreTunerEnabled)
-  {
-    settings.iTotalTuners = g_iTotalTuners;
-    settings.iPreTuneCloseDelay = g_iPreTunerCloseDelay;
-  }
-  else
-  {
-    /* When we don't want to use predictive tuning */
-    settings.iTotalTuners = 1;
-    settings.iPreTuneCloseDelay = 0;
-  }
-
-  settings.bAutorecApproxTime = (g_iAutorecApproxTime > 0);
-  settings.iAutorecMaxDiff = g_iAutorecMaxDiff;
 
   tvh = new CTvheadend();
   tvh->Start();
@@ -234,58 +151,7 @@ unsigned int ADDON_GetSettings
 ADDON_STATUS ADDON_SetSetting
   (const char *settingName, const void *settingValue)
 {
-#define UPDATE_STR(key, var)\
-  if (!strcmp(settingName, key))\
-  {\
-    if (strcmp(var.c_str(), (const char*)settingValue) != 0)\
-    {\
-      tvhdebug("update %s from '%s' to '%s'",\
-               settingName, var.c_str(), settingValue);\
-      return ADDON_STATUS_NEED_RESTART;\
-    }\
-    return ADDON_STATUS_OK;\
-  }
-
-#define UPDATE_INT(key, type, var)\
-  if (!strcmp(settingName, key))\
-  {\
-    if (var != *(type*)settingValue)\
-    {\
-      tvhdebug("update %s from '%d' to '%d'",\
-               settingName, var, (int)*(type*)settingValue);\
-      return ADDON_STATUS_NEED_RESTART;\
-    }\
-    return ADDON_STATUS_OK;\
-  }
-
-  /* Connection */
-  UPDATE_STR("host", g_strHostname);
-  UPDATE_STR("user", g_strUsername);
-  UPDATE_STR("pass", g_strPassword);
-  UPDATE_INT("htsp_port", int, g_iPortHTSP);
-  UPDATE_INT("http_port", int, g_iPortHTTP);
-  UPDATE_INT("connect_timeout", int, g_iConnectTimeout);
-  UPDATE_INT("response_timeout", int, g_iResponseTimeout);
-  
-  /* Data transfer */
-  UPDATE_INT("epg_async", bool, g_bAsyncEpg);
-
-  /* Debug */
-  UPDATE_INT("trace_debug", bool, g_bTraceDebug);
-
-  /* Predictive Tuning */
-  UPDATE_INT("total_tuners", int, g_iTotalTuners);
-  UPDATE_INT("pretuner_closedelay", int, g_iPreTunerCloseDelay);
-  UPDATE_INT("pretuner_enabled", bool, g_bPreTunerEnabled);
-
-  /* Auto Recordings */
-  UPDATE_INT("autorec_approxtime", int, g_iAutorecApproxTime);
-  UPDATE_INT("autorec_maxdiff", int, g_iAutorecMaxDiff);
-
-  return ADDON_STATUS_OK;
-
-#undef UPDATE_INT
-#undef UPDATE_STR
+  return Settings::GetInstance().SetSetting(settingName, settingValue);
 }
 
 void ADDON_Stop()
@@ -368,7 +234,7 @@ const char *GetConnectionString(void)
 
 const char *GetBackendHostname(void)
 {
-  return g_strHostname.c_str();
+  return Settings::GetInstance().GetConstCharHostname();
 }
 
 PVR_ERROR GetDriveSpace(long long *iTotal, long long *iUsed)
diff --git a/src/client.h b/src/client.h
index 3271f1a..3a5f950 100644
--- a/src/client.h
+++ b/src/client.h
@@ -30,18 +30,6 @@ extern ADDON::CHelper_libXBMC_addon*  XBMC;
 extern CHelper_libXBMC_pvr*           PVR;
 extern CHelper_libXBMC_codec*         CODEC;
 
-#define DEFAULT_HOST                "127.0.0.1"
-#define DEFAULT_HTTP_PORT           9981
-#define DEFAULT_HTSP_PORT           9982
-#define DEFAULT_CONNECT_TIMEOUT     10
-#define DEFAULT_RESPONSE_TIMEOUT    5
-#define DEFAULT_TOTAL_TUNERS        1
-#define DEFAULT_PRETUNER_CLOSEDELAY 10
-
-/* Maximum difference between real and approximate start time for auto recordings (minutes) */
-#define DEFAULT_AUTOREC_MAXDIFF      15
-#define DEFAULT_APPROX_TIME          0
-
 /* timer type ids */
 #define TIMER_ONCE_MANUAL             (PVR_TIMER_TYPE_NONE + 1)
 #define TIMER_ONCE_EPG                (PVR_TIMER_TYPE_NONE + 2)
diff --git a/src/tvheadend/Settings.cpp b/src/tvheadend/Settings.cpp
new file mode 100644
index 0000000..1f5a019
--- /dev/null
+++ b/src/tvheadend/Settings.cpp
@@ -0,0 +1,169 @@
+/*
+ *      Copyright (C) 2005-2011 Team XBMC
+ *      http://www.xbmc.org
+ *
+ *  This Program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2, or (at your option)
+ *  any later version.
+ *
+ *  This Program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with XBMC; see the file COPYING.  If not, write to
+ *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  http://www.gnu.org/copyleft/gpl.html
+ *
+ */
+
+#include "../Tvheadend.h"
+
+#include "Settings.h"
+
+namespace tvheadend {
+
+const std::string Settings::DEFAULT_HOST                = "127.0.0.1";
+const int         Settings::DEFAULT_HTTP_PORT           = 9981;
+const int         Settings::DEFAULT_HTSP_PORT           = 9982;
+const std::string Settings::DEFAULT_USERNAME            = "";
+const std::string Settings::DEFAULT_PASSWORD            = "";
+const int         Settings::DEFAULT_CONNECT_TIMEOUT     = 10000; // millisecs
+const int         Settings::DEFAULT_RESPONSE_TIMEOUT    = 5000;  // millisecs
+const bool        Settings::DEFAULT_TRACE_DEBUG         = false;
+const bool        Settings::DEFAULT_ASYNC_EPG           = false;
+const int         Settings::DEFAULT_TOTAL_TUNERS        = 1;  // total tuners > 1 => predictive tuning active
+const int         Settings::DEFAULT_PRETUNER_CLOSEDELAY = 10; // secs
+const int         Settings::DEFAULT_AUTOREC_MAXDIFF     = 15; // mins. Maximum difference between real and approximate start time for auto recordings
+const int         Settings::DEFAULT_APPROX_TIME         = 0;  // mins
+
+void Settings::ReadSettings()
+{
+  /* Connection */
+  SetHostname(ReadStringSetting("host", DEFAULT_HOST));
+  SetPortHTSP(ReadIntSetting("htsp_port", DEFAULT_HTSP_PORT));
+  SetPortHTTP(ReadIntSetting("http_port", DEFAULT_HTTP_PORT));
+  SetUsername(ReadStringSetting("user", DEFAULT_USERNAME));
+  SetPassword(ReadStringSetting("pass", DEFAULT_PASSWORD));
+
+  /* Note: Timeouts in settings UI are defined in seconds but we expect them to be in milliseconds. */
+  SetConnectTimeout(ReadIntSetting("connect_timeout", DEFAULT_CONNECT_TIMEOUT / 1000) * 1000);
+  SetResponseTimeout(ReadIntSetting("response_timeout", DEFAULT_RESPONSE_TIMEOUT / 1000) * 1000);
+
+  /* Debug */
+  SetTraceDebug(ReadBoolSetting("trace_debug", DEFAULT_TRACE_DEBUG));
+
+  /* Data Transfer */
+  SetAsyncEpg(ReadBoolSetting("epg_async", DEFAULT_ASYNC_EPG));
+
+  /* Predictive Tuning */
+  bool bPretunerEnabled = ReadBoolSetting("pretuner_enabled", false);
+  SetTotalTuners(bPretunerEnabled ? ReadIntSetting("total_tuners", DEFAULT_TOTAL_TUNERS) : 1);
+  SetPreTunerCloseDelay(bPretunerEnabled ? ReadIntSetting("pretuner_closedelay", DEFAULT_PRETUNER_CLOSEDELAY) : 0);
+
+  /* Auto recordings */
+  SetAutorecApproxTime(ReadIntSetting("autorec_approxtime", DEFAULT_APPROX_TIME));
+  SetAutorecMaxDiff(ReadIntSetting("autorec_maxdiff", DEFAULT_AUTOREC_MAXDIFF));
+}
+
+ADDON_STATUS Settings::SetSetting(const std::string &key, const void *value)
+{
+  /* Connection */
+  if (key == "host")
+    return SetStringSetting(GetHostname(), value);
+  else if (key == "htsp_port")
+    return SetIntSetting(GetPortHTSP(), value);
+  else if (key == "http_port")
+    return SetIntSetting(GetPortHTTP(), value);
+  else if (key == "user")
+    return SetStringSetting(GetUsername(), value);
+  else if (key == "pass")
+    return SetStringSetting(GetPassword(), value);
+  else if (key == "connect_timeout")
+    return SetIntSetting(GetConnectTimeout(), value);
+  else if (key == "response_timeout")
+    return SetIntSetting(GetResponseTimeout(), value);
+  /* Debug */
+  else if (key == "trace_debug")
+    return SetBoolSetting(GetTraceDebug(), value);
+  /* Data Transfer */
+  else if (key == "epg_async")
+    return SetBoolSetting(GetAsyncEpg(), value);
+  /* Predictive Tuning */
+  else if (key == "pretuner_enabled")
+  {
+    if (GetTotalTuners() > 1 && *(reinterpret_cast<const bool*>(value)))
+      return ADDON_STATUS_OK;
+    else
+      return ADDON_STATUS_NEED_RESTART;
+  }
+  else if (key == "total_tuners")
+    return SetIntSetting(GetTotalTuners(), value);
+  else if (key == "pretuner_closedelay")
+    return SetIntSetting(GetPreTunerCloseDelay(), value);
+  /* Auto recordings */
+  else if (key == "autorec_approxtime")
+    return SetIntSetting(GetAutorecApproxTime(), value);
+  else if (key == "autorec_maxdiff")
+    return SetIntSetting(GetAutorecMaxDiff(), value);
+  else
+  {
+    tvherror("Settings::SetSetting - unknown setting '%s'", key.c_str());
+    return ADDON_STATUS_UNKNOWN;
+  }
+}
+
+std::string Settings::ReadStringSetting(const std::string &key, const std::string &def)
+{
+  char value[1024];
+  if (XBMC->GetSetting(key.c_str(), value))
+    return value;
+
+  return def;
+}
+
+int Settings::ReadIntSetting(const std::string &key, int def)
+{
+  int value;
+  if (XBMC->GetSetting(key.c_str(), &value))
+    return value;
+
+  return def;
+}
+
+bool Settings::ReadBoolSetting(const std::string &key, bool def)
+{
+  bool value;
+  if (XBMC->GetSetting(key.c_str(), &value))
+    return value;
+
+  return def;
+}
+
+ADDON_STATUS Settings::SetStringSetting(const std::string &oldValue, const void *newValue)
+{
+  if (oldValue == std::string(reinterpret_cast<const char *>(newValue)))
+    return ADDON_STATUS_OK;
+
+  return ADDON_STATUS_NEED_RESTART;
+}
+
+ADDON_STATUS Settings::SetIntSetting(int oldValue, const void *newValue)
+{
+  if (oldValue == *(reinterpret_cast<const int *>(newValue)))
+    return ADDON_STATUS_OK;
+
+  return ADDON_STATUS_NEED_RESTART;
+}
+
+ADDON_STATUS Settings::SetBoolSetting(int oldValue, const void *newValue)
+{
+  if (oldValue == *(reinterpret_cast<const bool *>(newValue)))
+    return ADDON_STATUS_OK;
+
+  return ADDON_STATUS_NEED_RESTART;
+}
+
+} // namespace tvheadend
diff --git a/src/tvheadend/Settings.h b/src/tvheadend/Settings.h
index 2b8d090..f234010 100644
--- a/src/tvheadend/Settings.h
+++ b/src/tvheadend/Settings.h
@@ -23,6 +23,8 @@
 
 #include <string>
 
+#include "kodi/xbmc_addon_types.h"
+
 namespace tvheadend {
 
   /**
@@ -31,6 +33,21 @@ namespace tvheadend {
   class Settings {
   public:
 
+    // Default values.
+    static const std::string DEFAULT_HOST;
+    static const int         DEFAULT_HTTP_PORT;
+    static const int         DEFAULT_HTSP_PORT;
+    static const std::string DEFAULT_USERNAME;
+    static const std::string DEFAULT_PASSWORD;
+    static const int         DEFAULT_CONNECT_TIMEOUT;  // millisecs
+    static const int         DEFAULT_RESPONSE_TIMEOUT; // millisecs
+    static const bool        DEFAULT_TRACE_DEBUG;
+    static const bool        DEFAULT_ASYNC_EPG;
+    static const int         DEFAULT_TOTAL_TUNERS;
+    static const int         DEFAULT_PRETUNER_CLOSEDELAY; // secs
+    static const int         DEFAULT_AUTOREC_MAXDIFF; // mins. Maximum difference between real and approximate start time for auto recordings
+    static const int         DEFAULT_APPROX_TIME;     // mins
+
     /**
      * Singleton getter for the instance
      */
@@ -40,24 +57,95 @@ namespace tvheadend {
       return settings;
     }
 
-    std::string strHostname;
-    int         iPortHTSP;
-    int         iPortHTTP;
-    std::string strUsername;
-    std::string strPassword;
-    int         iConnectTimeout;
-    int         iResponseTimeout;
-    bool        bTraceDebug;
-    bool        bAsyncEpg;
-    int         iTotalTuners;
-    int         iPreTuneCloseDelay;
-    bool        bAutorecApproxTime;
-    int         iAutorecMaxDiff;
+    /**
+     * Read all settings defined in settings.xml
+     */
+    void ReadSettings();
+
+    /**
+     * Set a value according to key definition in settings.xml
+     */
+    ADDON_STATUS SetSetting(const std::string &key, const void *value);
+
+    /**
+     * Getters for the settings values
+     */
+    std::string GetHostname() const { return m_strHostname; }
+    const char *GetConstCharHostname() const { return m_strHostname.c_str(); }
+    int         GetPortHTSP() const { return m_iPortHTSP; }
+    int         GetPortHTTP() const { return m_iPortHTTP; }
+    std::string GetUsername() const { return m_strUsername; }
+    std::string GetPassword() const { return m_strPassword; }
+    int         GetConnectTimeout() const { return m_iConnectTimeout; }
+    int         GetResponseTimeout() const { return m_iResponseTimeout; }
+    bool        GetTraceDebug() const { return m_bTraceDebug; }
+    bool        GetAsyncEpg() const { return m_bAsyncEpg; }
+    int         GetTotalTuners() const { return m_iTotalTuners; }
+    int         GetPreTunerCloseDelay() const { return m_iPreTunerCloseDelay; }
+    bool        GetAutorecApproxTime() const { return m_bAutorecApproxTime; }
+    int         GetAutorecMaxDiff() const { return m_iPreTunerCloseDelay; }
 
   private:
-    Settings() { }
+    Settings()
+    : m_strHostname(DEFAULT_HOST),
+      m_iPortHTSP(DEFAULT_HTTP_PORT),
+      m_iPortHTTP(DEFAULT_HTSP_PORT),
+      m_strUsername(DEFAULT_USERNAME),
+      m_strPassword(DEFAULT_PASSWORD),
+      m_iConnectTimeout(DEFAULT_CONNECT_TIMEOUT),
+      m_iResponseTimeout(DEFAULT_RESPONSE_TIMEOUT),
+      m_bTraceDebug(DEFAULT_TRACE_DEBUG),
+      m_bAsyncEpg(DEFAULT_ASYNC_EPG),
+      m_iTotalTuners(DEFAULT_TOTAL_TUNERS),
+      m_iPreTunerCloseDelay(DEFAULT_PRETUNER_CLOSEDELAY),
+      m_bAutorecApproxTime(DEFAULT_APPROX_TIME),
+      m_iAutorecMaxDiff(DEFAULT_AUTOREC_MAXDIFF) {}
+
     Settings(Settings const &) = delete;
     void operator=(Settings const &) = delete;
+
+    /**
+     * Setters
+     */
+    void SetHostname(const std::string& value) { m_strHostname = value; }
+    void SetPortHTSP(int value) { m_iPortHTSP = value; }
+    void SetPortHTTP(int value) { m_iPortHTTP = value; }
+    void SetUsername(const std::string& value) { m_strUsername = value; }
+    void SetPassword(const std::string& value) { m_strPassword = value; }
+    void SetConnectTimeout(int value) { m_iConnectTimeout = value; }
+    void SetResponseTimeout(int value) { m_iResponseTimeout = value; }
+    void SetTraceDebug(bool value) { m_bTraceDebug = value; }
+    void SetAsyncEpg(bool value) { m_bAsyncEpg = value; }
+    void SetTotalTuners(int value) { m_iTotalTuners = value; }
+    void SetPreTunerCloseDelay(int value) { m_iPreTunerCloseDelay = value; }
+    void SetAutorecApproxTime(bool value) { m_bAutorecApproxTime = value; }
+    void SetAutorecMaxDiff(int value) { m_iAutorecMaxDiff = value; }
+
+    /**
+     * Read/Set values according to definition in settings.xml
+     */
+    static std::string ReadStringSetting(const std::string &key, const std::string &def);
+    static int         ReadIntSetting(const std::string &key, int def);
+    static bool        ReadBoolSetting(const std::string &key, bool def);
+
+    // @return ADDON_STATUS_OK if value has not changed, ADDON_STATUS_NEED_RESTART otherwise
+    static ADDON_STATUS SetStringSetting(const std::string &oldValue, const void *newValue);
+    static ADDON_STATUS SetIntSetting(int oldValue, const void *newValue);
+    static ADDON_STATUS SetBoolSetting(int oldValue, const void *newValue);
+
+    std::string m_strHostname;
+    int         m_iPortHTSP;
+    int         m_iPortHTTP;
+    std::string m_strUsername;
+    std::string m_strPassword;
+    int         m_iConnectTimeout;
+    int         m_iResponseTimeout;
+    bool        m_bTraceDebug;
+    bool        m_bAsyncEpg;
+    int         m_iTotalTuners;
+    int         m_iPreTunerCloseDelay;
+    bool        m_bAutorecApproxTime;
+    int         m_iAutorecMaxDiff;
   };
 
-}
\ No newline at end of file
+}
diff --git a/src/tvheadend/entity/AutoRecording.cpp b/src/tvheadend/entity/AutoRecording.cpp
index 1fcbe11..9bb7518 100644
--- a/src/tvheadend/entity/AutoRecording.cpp
+++ b/src/tvheadend/entity/AutoRecording.cpp
@@ -56,7 +56,7 @@ bool AutoRecording::operator!=(const AutoRecording &right)
 
 time_t AutoRecording::GetStart() const
 {
-  if (Settings::GetInstance().bAutorecApproxTime)
+  if (Settings::GetInstance().GetAutorecApproxTime())
   {
     /* Calculate the approximate start time from the starting window */
     if ((m_startWindowBegin == -1) ||
@@ -92,7 +92,7 @@ void AutoRecording::SetStartWindowBegin(int32_t start)
 
 time_t AutoRecording::GetStop() const
 {
-  if (Settings::GetInstance().bAutorecApproxTime)
+  if (Settings::GetInstance().GetAutorecApproxTime())
   {
     /* Tvh doesn't have an approximate stop time => "any time" */
     return 0;

-- 
kodi-pvr-hts packaging



More information about the pkg-multimedia-commits mailing list