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  {
134  pInfo->setSchemaVersion(0);
135  }
136  }
137  else
138  {
139  pInfo->setHaveDatabase(false);
140  pInfo->setSchemaVersion(0);
141  }
142 
143  return pInfo;
144 }
145 
147 //
150 {
151 
153  QStringList locales = localesMap.values();
154  locales.sort();
155 
156  auto* pList = new V2CountryList();
157 
158  for (const auto& country : std::as_const(locales))
159  {
160  const QString code = localesMap.key(country);
161  const QString nativeCountry = GetISO3166CountryName(code);
162 
163  V2Country *pCountry = pList->AddNewCountry();
164  pCountry->setCode(code);
165  pCountry->setCountry(country);
166  pCountry->setNativeCountry(nativeCountry);
167  pCountry->setImage(QString("%1.png").arg(code.toLower()));
168  }
169 
170  return pList;
171 }
172 
174 //
177 {
178  QMap<QString,QString> langMap = MythTranslation::getLanguages();
179  QStringList langs = langMap.values();
180  langs.sort();
181 
182  auto* pList = new V2LanguageList();
183 
184  for (const auto& nativeLang : std::as_const(langs))
185  {
186  const QString code = langMap.key(nativeLang);
187  const QString language = GetISO639EnglishLanguageName(code);
188 
189  V2Language *pLanguage = pList->AddNewLanguage();
190  pLanguage->setCode(code);
191  pLanguage->setLanguage(language);
192  pLanguage->setNativeLanguage(nativeLang);
193  pLanguage->setImage(QString("%1.png").arg(code));
194  }
195 
196  return pList;
197 }
198 
200 //
202 
203 QStringList V2Config::GetIPAddresses( const QString &Protocol )
204 {
205  QString protocol = Protocol;
206 
207  if (protocol != "IPv4" && protocol != "IPv6")
208  protocol = "All";
209 
210  QStringList oList;
211 
212  QList<QHostAddress> list = QNetworkInterface::allAddresses();
213  QList<QHostAddress>::iterator it;
214 
215  for (it = list.begin(); it != list.end(); ++it)
216  {
217  if (((*it).protocol() == QAbstractSocket::IPv4Protocol && protocol == "IPv4") ||
218  ((*it).protocol() == QAbstractSocket::IPv6Protocol && protocol == "IPv6") || protocol == "All")
219  {
220  it->setScopeId(QString());
221  oList.append((*it).toString());
222  }
223  }
224 
225  return oList;
226 }
227 
229 {
230  QString theHost;
231  if (Host.isEmpty())
232  theHost = gCoreContext->GetHostName();
233  else
234  theHost = Host;
235  auto* pList = new V2SystemEventList();
236  QMap <QString, QString> settings;
238  QMap<QString, QString>::const_iterator it;
239  for (it = settings.constBegin(); it != settings.constEnd(); ++it)
240  {
241  V2SystemEvent *event = pList->AddNewSystemEvent();
242  event->setKey(it.key());
243  event->setLocalizedName(it.value());
244  event->setValue(gCoreContext->GetSettingOnHost(it.key(), theHost, QString()));
245  }
246  return pList;
247 }
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:1732
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:149
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
No longer used.
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:176
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:228
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:930
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:842
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:203
MythHTTPMetaService
Definition: mythhttpmetaservice.h:10