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