MythTV  master
v2config.cpp
Go to the documentation of this file.
1 // qt
2 #include <QHostAddress>
3 #include <QNetworkInterface>
4 
5 // MythTV
6 #include "libmyth/mythcontext.h"
9 #include "libmythbase/iso3166.h"
10 #include "libmythbase/iso639.h"
12 #include "libmythbase/mythlocale.h"
14 #include "libmythbase/mythversion.h"
15 #include "libmythbase/version.h"
16 
17 // MythBackend
18 #include "v2config.h"
19 #include "v2countryList.h"
20 #include "v2databaseInfo.h"
21 #include "v2databaseStatus.h"
22 #include "v2languageList.h"
23 
24 // Only endpoints that don't require a fully configured mythbackend (eg a new
25 // setup with no database or tuners for example) should be put here.
26 
27 // This will be initialised in a thread safe manner on first use
29  (CONFIG_HANDLE, V2Config::staticMetaObject, &V2Config::RegisterCustomTypes))
30 
32 {
33  qRegisterMetaType<V2ConnectionInfo*>("V2ConnectionInfo");
34  qRegisterMetaType<V2CountryList*>("V2CountryList");
35  qRegisterMetaType<V2Country*>("V2Country");
36  qRegisterMetaType<V2LanguageList*>("V2LanguageList");
37  qRegisterMetaType<V2Language*>("V2Language");
38  qRegisterMetaType<V2DatabaseStatus*>("V2DatabaseStatus");
39  qRegisterMetaType<V2SystemEvent*>("V2SystemEvent");
40  qRegisterMetaType<V2SystemEventList*>("V2SystemEventList");
41 }
42 
43 
45  : MythHTTPService(s_service)
46 {
47 }
48 
50 //
52 bool V2Config::SetDatabaseCredentials(const QString &Host, const QString &UserName,
53  const QString &Password, const QString &Name, int Port, bool DoTest,
54  bool LocalEnabled, const QString &LocalHostName, bool WOLEnabled,
55  int WOLReconnect, int WOLRetry, const QString &WOLCommand)
56 {
57  bool bResult = false;
58 
59  QString db("mythconverg");
60  int port = 3306;
61 
62  if (!Name.isEmpty())
63  db = Name;
64 
65  if (Port != 0)
66  port = Port;
67 
68  if (DoTest && !TestDatabase(Host, UserName, Password, db, port))
69  return false;
70 
71  DatabaseParams dbparms;
72  dbparms.m_dbName = db;
73  dbparms.m_dbUserName = UserName;
74  dbparms.m_dbPassword = Password;
75  dbparms.m_dbHostName = Host;
76  dbparms.m_dbPort = port;
77  dbparms.m_localEnabled = LocalEnabled;
78  if (LocalEnabled)
79  dbparms.m_localHostName = LocalHostName;
80  else
81  dbparms.m_localHostName = "my-unique-identifier-goes-here";
82  dbparms.m_wolEnabled = WOLEnabled;
83  dbparms.m_wolReconnect = std::chrono::seconds(WOLReconnect);
84  dbparms.m_wolRetry = WOLRetry;
85  dbparms.m_wolCommand = WOLCommand;
86  // We need the force parameter set to true here, otherwise if you accept the
87  // default values, it does not save the file and theus does not create
88  // config.xml.
89  bResult = gContext->SaveDatabaseParams(dbparms, true);
90 
91  return bResult;
92 }
93 
95 //
98 {
99  const DatabaseParams params = GetMythDB()->GetDatabaseParams();
100 
101  auto *pInfo = new V2DatabaseStatus();
102 
103  pInfo->setHost(params.m_dbHostName);
104  pInfo->setPing(params.m_dbHostPing);
105  pInfo->setPort(params.m_dbPort);
106  pInfo->setUserName(params.m_dbUserName);
107  pInfo->setPassword(params.m_dbPassword);
108  pInfo->setName(params.m_dbName);
109  pInfo->setType(params.m_dbType);
110  pInfo->setLocalEnabled(params.m_localEnabled);
111  pInfo->setLocalHostName(params.m_localHostName);
112  pInfo->setWOLEnabled(params.m_wolEnabled);
113  pInfo->setWOLReconnect(params.m_wolReconnect.count());
114  pInfo->setWOLRetry(params.m_wolRetry);
115  pInfo->setWOLCommand(params.m_wolCommand);
116 
117  // are we connected to the database?
118  bool connected = TestDatabase(params.m_dbHostName, params.m_dbUserName, params.m_dbPassword, params.m_dbName, params.m_dbPort);
119  pInfo->setConnected(connected);
120 
121  // do we have a mythconverg database?
122  if (connected)
123  {
124  bool haveSchema = GetMythDB()->HaveSchema();
125  pInfo->setHaveDatabase(haveSchema);
126 
127  if (haveSchema)
128  {
129  // get schema version
130  pInfo->setSchemaVersion(QString(MYTH_DATABASE_VERSION).toInt());
131  }
132  else
133  pInfo->setSchemaVersion(0);
134  }
135  else
136  {
137  pInfo->setHaveDatabase(false);
138  pInfo->setSchemaVersion(0);
139  }
140 
141  return pInfo;
142 }
143 
145 //
148 {
149 
151  QStringList locales = localesMap.values();
152  locales.sort();
153 
154  auto* pList = new V2CountryList();
155 
156  for (const auto& country : std::as_const(locales))
157  {
158  const QString code = localesMap.key(country);
159  const QString nativeCountry = GetISO3166CountryName(code);
160 
161  V2Country *pCountry = pList->AddNewCountry();
162  pCountry->setCode(code);
163  pCountry->setCountry(country);
164  pCountry->setNativeCountry(nativeCountry);
165  pCountry->setImage(QString("%1.png").arg(code.toLower()));
166  }
167 
168  return pList;
169 }
170 
172 //
175 {
176  QMap<QString,QString> langMap = MythTranslation::getLanguages();
177  QStringList langs = langMap.values();
178  langs.sort();
179 
180  auto* pList = new V2LanguageList();
181 
182  for (const auto& nativeLang : std::as_const(langs))
183  {
184  const QString code = langMap.key(nativeLang);
185  const QString language = GetISO639EnglishLanguageName(code);
186 
187  V2Language *pLanguage = pList->AddNewLanguage();
188  pLanguage->setCode(code);
189  pLanguage->setLanguage(language);
190  pLanguage->setNativeLanguage(nativeLang);
191  pLanguage->setImage(QString("%1.png").arg(code));
192  }
193 
194  return pList;
195 }
196 
198 //
200 
201 QStringList V2Config::GetIPAddresses( const QString &Protocol )
202 {
203  QString protocol = Protocol;
204 
205  if (protocol != "IPv4" && protocol != "IPv6")
206  protocol = "All";
207 
208  QStringList oList;
209 
210  QList<QHostAddress> list = QNetworkInterface::allAddresses();
211  QList<QHostAddress>::iterator it;
212 
213  for (it = list.begin(); it != list.end(); ++it)
214  {
215  if (((*it).protocol() == QAbstractSocket::IPv4Protocol && protocol == "IPv4") ||
216  ((*it).protocol() == QAbstractSocket::IPv6Protocol && protocol == "IPv6") || protocol == "All")
217  {
218  it->setScopeId(QString());
219  oList.append((*it).toString());
220  }
221  }
222 
223  return oList;
224 }
225 
227 {
228  QString theHost;
229  if (Host.isEmpty())
230  theHost = gCoreContext->GetHostName();
231  else
232  theHost = Host;
233  auto* pList = new V2SystemEventList();
234  QMap <QString, QString> settings;
236  QMap<QString, QString>::const_iterator it;
237  for (it = settings.constBegin(); it != settings.constEnd(); ++it)
238  {
239  V2SystemEvent *event = pList->AddNewSystemEvent();
240  event->setKey(it.key());
241  event->setLocalizedName(it.value());
242  event->setValue(gCoreContext->GetSettingOnHost(it.key(), theHost, QString()));
243  }
244  return pList;
245 }
Password
static StandardSetting * Password(bool enabled)
Setting for changing password.
Definition: galleryconfig.cpp:245
DatabaseParams::m_dbHostName
QString m_dbHostName
database server
Definition: mythdbparams.h:22
GetISO639EnglishLanguageName
QString GetISO639EnglishLanguageName(const QString &iso639_1)
Definition: iso639.cpp:962
DatabaseParams
Structure containing the basic Database parameters.
Definition: mythdbparams.h:10
V2Config::RegisterCustomTypes
static void RegisterCustomTypes()
TestDatabase
bool TestDatabase(const QString &dbHostName, const QString &dbUserName, QString dbPassword, QString dbName, int dbPort)
Definition: mythdbcon.cpp:41
mythhttpmetaservice.h
V2Config::GetDatabaseStatus
static V2DatabaseStatus * GetDatabaseStatus(void)
Definition: v2config.cpp:97
MythContext::SaveDatabaseParams
bool SaveDatabaseParams(const DatabaseParams &params, bool force=false)
Definition: mythcontext.cpp:1727
DatabaseParams::m_dbType
QString m_dbType
database type (MySQL, Postgres, etc.)
Definition: mythdbparams.h:28
GetMythDB
MythDB * GetMythDB(void)
Definition: mythdb.cpp:50
mythsystemevent.h
V2CountryList
Definition: v2countryList.h:9
V2Config::GetCountries
static V2CountryList * GetCountries(void)
Definition: v2config.cpp:147
GetISO3166CountryName
QString GetISO3166CountryName(const QString &iso3166Code)
Definition: iso3166.cpp:368
DatabaseParams::m_wolReconnect
std::chrono::seconds m_wolReconnect
seconds to wait for reconnect
Definition: mythdbparams.h:35
MythTranslation::getLanguages
static QMap< QString, QString > getLanguages(void)
Definition: mythtranslation.cpp:118
Q_GLOBAL_STATIC_WITH_ARGS
Q_GLOBAL_STATIC_WITH_ARGS(MythHTTPMetaService, s_service,(CONFIG_HANDLE, V2Config::staticMetaObject, &V2Config::RegisterCustomTypes)) void V2Config
Definition: v2config.cpp:28
DatabaseParams::m_dbPort
int m_dbPort
database port
Definition: mythdbparams.h:24
v2languageList.h
DatabaseParams::m_dbHostPing
bool m_dbHostPing
Can we test connectivity using ping?
Definition: mythdbparams.h:23
GetISO3166EnglishCountryMap
ISO3166ToNameMap GetISO3166EnglishCountryMap(void)
Returns a map of ISO-3166 country codes mapped to the country name in English.
Definition: iso3166.cpp:344
V2Config::GetLanguages
static V2LanguageList * GetLanguages(void)
Definition: v2config.cpp:174
V2Country
Definition: v2country.h:6
mythtranslation.h
V2SystemEvent
Definition: v2systemEventList.h:8
mythlocale.h
V2LanguageList
Definition: v2languageList.h:9
DatabaseParams::m_dbPassword
QString m_dbPassword
DB password.
Definition: mythdbparams.h:26
V2Config::V2Config
V2Config()
Definition: v2config.cpp:44
DatabaseParams::m_wolRetry
int m_wolRetry
times to retry to reconnect
Definition: mythdbparams.h:36
v2databaseInfo.h
DatabaseParams::m_dbName
QString m_dbName
database name
Definition: mythdbparams.h:27
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:55
v2config.h
MythHTTPService
Definition: mythhttpservice.h:19
V2Config::GetSystemEvents
static V2SystemEventList * GetSystemEvents(const QString &Host)
Definition: v2config.cpp:226
MythHTTPService::Name
QString & Name()
Definition: mythhttpservice.cpp:240
v2databaseStatus.h
mythcorecontext.h
DatabaseParams::m_wolCommand
QString m_wolCommand
command to use for wake-on-lan
Definition: mythdbparams.h:37
Name
Definition: channelsettings.cpp:71
MythCoreContext::GetSettingOnHost
QString GetSettingOnHost(const QString &key, const QString &host, const QString &defaultval="")
Definition: mythcorecontext.cpp:926
V2SystemEventList
Definition: v2systemEventList.h:30
DatabaseParams::m_dbUserName
QString m_dbUserName
DB user name.
Definition: mythdbparams.h:25
iso3166.h
ISO 3166-1 support functions.
V2DatabaseStatus
Definition: v2databaseStatus.h:7
MythSystemEventEditor::createSettingList
static void createSettingList(QMap< QString, QString > &settings)
Definition: mythsystemevent.cpp:390
mythcontext.h
V2Config::SetDatabaseCredentials
static bool SetDatabaseCredentials(const QString &Host, const QString &UserName, const QString &Password, const QString &Name, int Port, bool DoTest, bool LocalEnabled, const QString &LocalHostName, bool WOLEnabled, int WOLReconnect, int WOLRetry, const QString &WOLCommand)
Definition: v2config.cpp:52
V2Language
Definition: v2language.h:6
iso639.h
ISO 639-1 and ISO 639-2 support functions.
v2countryList.h
MythCoreContext::GetHostName
QString GetHostName(void)
Definition: mythcorecontext.cpp:838
DatabaseParams::m_localEnabled
bool m_localEnabled
true if localHostName is not default
Definition: mythdbparams.h:30
DatabaseParams::m_wolEnabled
bool m_wolEnabled
true if wake-on-lan params are used
Definition: mythdbparams.h:34
CONFIG_HANDLE
#define CONFIG_HANDLE
Definition: v2config.h:12
DatabaseParams::m_localHostName
QString m_localHostName
name used for loading/saving settings
Definition: mythdbparams.h:31
ISO3166ToNameMap
QMap< QString, QString > ISO3166ToNameMap
Definition: iso3166.h:23
gContext
MythContext * gContext
This global variable contains the MythContext instance for the application.
Definition: mythcontext.cpp:64
V2Config::GetIPAddresses
static QStringList GetIPAddresses(const QString &Protocol)
Definition: v2config.cpp:201
MythHTTPMetaService
Definition: mythhttpmetaservice.h:10