MythTV  master
backendselect.cpp
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 #include "backendselect.h"
3 
4 #include <utility>
5 
6 #include <QEventLoop>
7 
10 #include "libmythbase/mythversion.h"
13 #include "libmythui/mythuibutton.h"
17 
19  MythScreenStack *parent,
20  DatabaseParams *params,
21  QString config_filename,
22  bool exitOnFinish
23  ) :
24  MythScreenType(parent, "BackEnd Selection"),
25  m_dbParams(params),
26  m_configFilename(std::move(config_filename)),
27  m_exitOnFinish(exitOnFinish)
28 {
29  if (exitOnFinish)
30  {
31  m_loop = new QEventLoop();
32  }
33 }
34 
36 {
38 
39  ItemMap::iterator it;
40  for (it = m_devices.begin(); it != m_devices.end(); ++it)
41  {
42  if (*it)
43  (*it)->DecrRef();
44  }
45 
46  m_devices.clear();
47 
48  if (m_exitOnFinish)
49  {
50  delete m_loop;
51  }
52 }
53 
55  DatabaseParams *dbParams, const QString& config_filename)
56 {
59  if (!mainStack)
60  return ret;
61 
62  auto *backendSettings =
63  new BackendSelection(mainStack, dbParams, config_filename, true);
64 
65  if (backendSettings->Create())
66  {
67  mainStack->AddScreen(backendSettings, false);
68  backendSettings->m_loop->exec();
69  ret = backendSettings->m_backendDecision;
70  mainStack->PopScreen(backendSettings, false);
71  }
72  else
73  delete backendSettings;
74 
75  return ret;
76 }
77 
79 {
80  if (!LoadWindowFromXML("config-ui.xml", "backendselection", this))
81  return false;
82 
83  m_backendList = dynamic_cast<MythUIButtonList*>(GetChild("backends"));
84  m_saveButton = dynamic_cast<MythUIButton*>(GetChild("save"));
85  m_cancelButton = dynamic_cast<MythUIButton*>(GetChild("cancel"));
86  m_manualButton = dynamic_cast<MythUIButton*>(GetChild("manual"));
87 
89  this, qOverload<MythUIButtonListItem *>(&BackendSelection::Accept));
90 
94  this, qOverload<>(&BackendSelection::Accept));
95 
98 
99  return true;
100 }
101 
103 {
104  if (!item)
105  return;
106 
107  auto *dev = item->GetData().value<DeviceLocation *>();
108  if (!dev)
109  {
110  Cancel();
111  LOG(VB_GENERAL, LOG_ERR,
112  "Could not get device details from UI element?");
113  return;
114  }
115 
116  if (ConnectBackend(dev))
117  {
118  {
119  auto config = XmlConfiguration(m_configFilename);
120  if (!m_pinCode.isEmpty())
121  config.SetValue(kDefaultPIN, m_pinCode);
122  config.SetValue(kDefaultUSN, m_usn);
123  config.Save();
124  }
126  }
127 }
128 
130 {
132 
133  if (!item)
134  return;
135 
136  Accept(item);
137 }
138 
139 
141 {
142  if (!dev)
143  return;
144 
145  QString usn = dev->m_sUSN;
146 
147  m_mutex.lock();
148 
149  // The devices' USN should be unique. Don't add if it is already there:
150  if (m_devices.find(usn) == m_devices.end())
151  {
152  dev->IncrRef();
153  m_devices.insert(usn, dev);
154 
155  m_mutex.unlock();
156 
157  InfoMap infomap;
158  dev->GetDeviceDetail(infomap);
159 
160  // We only want the version number, not the library version info
161  infomap["version"] = infomap["modelnumber"].section('.', 0, 1);
162 
163  auto *item = new MythUIButtonListItem(m_backendList, infomap["modelname"],
164  QVariant::fromValue(dev));
165  item->SetTextFromMap(infomap);
166 
167  bool protoMatch = (infomap["protocolversion"] == MYTH_PROTO_VERSION);
168 
169  QString status = "good";
170  if (!protoMatch)
171  status = "protocolmismatch";
172 
173  // TODO: Not foolproof but if we can't get device details then it's
174  // probably because we could not connect to port 6544 - firewall?
175  // Maybe we can replace this with a more specific check
176  if (infomap["modelname"].isEmpty())
177  status = "blocked";
178 
179  item->DisplayState(status, "connection");
180 
181  bool needPin = dev->NeedSecurityPin();
182  item->DisplayState(needPin ? "yes" : "no", "securitypin");
183  }
184  else
185  m_mutex.unlock();
186 }
187 
193 {
194  QString message;
195 
196  m_usn = dev->m_sUSN;
197 
198  MythXMLClient client( dev->m_sLocation );
199 
200  UPnPResultCode stat = client.GetConnectionInfo(m_pinCode, m_dbParams, message);
201 
202  QString backendName = dev->GetFriendlyName();
203 
204  if (backendName == "<Unknown>")
205  backendName = dev->m_sLocation;
206 
207  switch (stat)
208  {
209  case UPnPResult_Success:
210  LOG(VB_UPNP, LOG_INFO,
211  QString("ConnectBackend() - success. New hostname: %1")
212  .arg(m_dbParams->m_dbHostName));
213  return true;
214 
216  LOG(VB_GENERAL, LOG_ERR, QString("Need Human: %1").arg(message));
217  ShowOkPopup(message);
218 
219  if (TryDBfromURL("", dev->m_sLocation))
220  return true;
221 
222  break;
223 
225  LOG(VB_GENERAL, LOG_ERR,
226  QString("Access denied for %1. Wrong PIN?")
227  .arg(backendName));
229  break;
230 
231  default:
232  LOG(VB_GENERAL, LOG_ERR,
233  QString("GetConnectionInfo() failed for %1 : %2")
234  .arg(backendName, message));
235  ShowOkPopup(message);
236  }
237 
238  // Back to the list, so the user can choose a different backend:
240  return false;
241 }
242 
244 {
246 }
247 
249 {
250  SSDP::AddListener(this);
252 }
253 
255 {
257  if (pEntries)
258  {
259  EntryMap ourMap;
260  pEntries->GetEntryMap(ourMap);
261  pEntries->DecrRef();
262 
263  for (auto * devLoc : std::as_const(ourMap))
264  {
265  AddItem(devLoc);
266  devLoc->DecrRef();
267  }
268  }
269 }
270 
272 {
274 }
275 
276 void BackendSelection::RemoveItem(const QString& USN_)
277 {
278  m_mutex.lock();
279 
280  ItemMap::iterator it = m_devices.find(USN_);
281 
282  if (it != m_devices.end())
283  {
284  if (*it)
285  (*it)->DecrRef();
286  m_devices.erase(it);
287  }
288 
289  m_mutex.unlock();
290 }
291 
292 bool BackendSelection::TryDBfromURL(const QString &error, const QString& URL)
293 {
294  if (ShowOkPopup(error + tr("Shall I attempt to connect to this"
295  " host with default database parameters?")))
296  {
297  static const QRegularExpression re {"http[s]?://([^:/]+)",
298  QRegularExpression::CaseInsensitiveOption};
299  QRegularExpressionMatch match = re.match(URL);
300  if (match.hasMatch())
301  {
302  m_dbParams->m_dbHostName = match.captured(1);
303  return true;
304  }
305  }
306 
307  return false;
308 }
309 
310 void BackendSelection::customEvent(QEvent *event)
311 {
312  if (event->type() == MythEvent::kMythEventMessage)
313  {
314  auto *me = dynamic_cast<MythEvent *>(event);
315  if (me == nullptr)
316  return;
317 
318  const QString& message = me->Message();
319  const QString& URI = me->ExtraData(0);
320  const QString& URN = me->ExtraData(1);
321  const QString& URL = me->ExtraData(2);
322 
323 
324  LOG(VB_UPNP, LOG_DEBUG,
325  QString("BackendSelection::customEvent(%1, %2, %3, %4)")
326  .arg(message, URI, URN, URL));
327 
328  if (message.startsWith("SSDP_ADD") &&
329  URI.startsWith("urn:schemas-mythtv-org:device:MasterMediaServer:"))
330  {
331  DeviceLocation *devLoc = SSDP::Find(URI, URN);
332  if (devLoc)
333  {
334  AddItem(devLoc);
335  devLoc->DecrRef();
336  }
337  }
338  else if (message.startsWith("SSDP_REMOVE"))
339  {
340  //-=>Note: This code will never get executed until
341  // SSDPCache is changed to handle NotifyRemove correctly
342  RemoveItem(URN);
343  }
344  }
345  else if (event->type() == DialogCompletionEvent::kEventType)
346  {
347  auto *dce = dynamic_cast<DialogCompletionEvent*>(event);
348  if (!dce)
349  return;
350 
351  QString resultid = dce->GetId();
352 
353  if (resultid == "password")
354  {
355  m_pinCode = dce->GetResultText();
356  Accept();
357  }
358  }
359 }
360 
362 {
363  QString message = tr("Please enter the backend access PIN");
364 
365  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
366 
367  auto *pwDialog = new MythTextInputDialog(popupStack, message,
368  FilterNone, true);
369 
370  if (pwDialog->Create())
371  {
372  pwDialog->SetReturnEvent(this, "password");
373  popupStack->AddScreen(pwDialog);
374  }
375  else
376  delete pwDialog;
377 }
378 
380 {
382 }
383 
385 {
387 
388  if (m_exitOnFinish)
389  m_loop->quit();
390  else
392 }
MythUIButton::Clicked
void Clicked()
BackendSelection::m_dbParams
DatabaseParams * m_dbParams
Definition: backendselect.h:73
MythScreenType::LoadInBackground
void LoadInBackground(const QString &message="")
Definition: mythscreentype.cpp:286
MythMainWindow::GetMainStack
MythScreenStack * GetMainStack()
Definition: mythmainwindow.cpp:318
BackendSelection::Cancel
void Cancel(void)
Linked to 'Cancel' button.
Definition: backendselect.cpp:243
BackendSelection::Close
void Close(void) override
Definition: backendselect.cpp:379
MythUIButtonList::GetItemCurrent
MythUIButtonListItem * GetItemCurrent() const
Definition: mythuibuttonlist.cpp:1587
DatabaseParams::m_dbHostName
QString m_dbHostName
database server
Definition: mythdbparams.h:22
MythEvent::kMythEventMessage
static const Type kMythEventMessage
Definition: mythevent.h:79
FilterNone
@ FilterNone
Definition: mythuitextedit.h:21
DialogCompletionEvent::GetId
QString GetId()
Definition: mythdialogbox.h:52
error
static void error(const char *str,...)
Definition: vbi.cpp:36
ReferenceCounter::DecrRef
virtual int DecrRef(void)
Decrements reference count and deletes on 0.
Definition: referencecounter.cpp:125
SSDP::PerformSearch
void PerformSearch(const QString &sST, std::chrono::seconds timeout=2s)
Definition: ssdp.cpp:204
BackendSelection::Init
void Init(void) override
Used after calling Load() to assign data to widgets and other UI initilisation which is prohibited in...
Definition: backendselect.cpp:254
BackendSelection::m_mutex
QMutex m_mutex
Definition: backendselect.h:86
MythScreenType::Close
virtual void Close()
Definition: mythscreentype.cpp:386
BackendSelection::customEvent
void customEvent(QEvent *event) override
Definition: backendselect.cpp:310
DatabaseParams
Structure containing the basic Database parameters.
Definition: mythdbparams.h:10
MythUIType::GetChild
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:133
MythXMLClient
Definition: mythxmlclient.h:31
MythEvent
This class is used as a container for messages.
Definition: mythevent.h:16
mythxmlclient.h
BackendSelection::AddItem
void AddItem(DeviceLocation *dev)
Definition: backendselect.cpp:140
mythdialogbox.h
MythScreenStack
Definition: mythscreenstack.h:16
SSDP::Find
static SSDPCacheEntries * Find(const QString &sURI)
Definition: ssdp.h:132
DeviceLocation::GetFriendlyName
QString GetFriendlyName(void)
Definition: upnpdevice.h:278
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MythScreenType
Screen in which all other widgets are contained and rendered.
Definition: mythscreentype.h:45
mythuistatetype.h
SSDPCacheEntries::GetEntryMap
void GetEntryMap(EntryMap &map)
Returns a copy of the EntryMap.
Definition: ssdpcache.cpp:85
DeviceLocation
Definition: upnpdevice.h:210
SSDPCache::Instance
static SSDPCache * Instance()
Definition: ssdpcache.cpp:237
BackendSelection::m_exitOnFinish
bool m_exitOnFinish
Definition: backendselect.h:75
mythuibuttonlist.h
MythEvent::Message
const QString & Message() const
Definition: mythevent.h:65
XmlConfiguration
Definition: configuration.h:38
BackendSelection::m_devices
ItemMap m_devices
Definition: backendselect.h:76
UPnPResult_HumanInterventionRequired
@ UPnPResult_HumanInterventionRequired
Definition: upnp.h:46
InfoMap
QHash< QString, QString > InfoMap
Definition: mythtypes.h:15
DeviceLocation::NeedSecurityPin
bool NeedSecurityPin(void)
Definition: upnpdevice.h:316
SSDP::Instance
static SSDP * Instance()
Definition: ssdp.cpp:55
MythUIButtonListItem
Definition: mythuibuttonlist.h:41
mythlogging.h
DeviceLocation::m_sUSN
QString m_sUSN
Definition: upnpdevice.h:235
BackendSelection::BackendSelection
BackendSelection(MythScreenStack *parent, DatabaseParams *params, QString config_filename, bool exitOnFinish=false)
Definition: backendselect.cpp:18
MythUIButtonList::itemClicked
void itemClicked(MythUIButtonListItem *item)
BackendSelection::CloseWithDecision
void CloseWithDecision(Decision d)
Definition: backendselect.cpp:384
MythScreenType::SetFocusWidget
bool SetFocusWidget(MythUIType *widget=nullptr)
Definition: mythscreentype.cpp:118
SSDPCache::Find
SSDPCacheEntries * Find(const QString &sURI)
Finds the SSDPCacheEntries in the cache, returns nullptr when absent.
Definition: ssdpcache.cpp:293
BackendSelection::kCancelConfigure
@ kCancelConfigure
Definition: backendselect.h:44
BackendSelection::RemoveItem
void RemoveItem(const QString &USN)
Definition: backendselect.cpp:276
MythScreenType::BuildFocusList
void BuildFocusList(void)
Definition: mythscreentype.cpp:206
BackendSelection::Manual
void Manual(void)
Linked to 'Configure Manually' button.
Definition: backendselect.cpp:271
DeviceLocation::m_sLocation
QString m_sLocation
Definition: upnpdevice.h:236
MythUIButton
A single button widget.
Definition: mythuibutton.h:21
BackendSelection::kAcceptConfigure
@ kAcceptConfigure
Definition: backendselect.h:45
EntryMap
QMap< QString, DeviceLocation * > EntryMap
Key == Unique Service Name (USN)
Definition: ssdpcache.h:29
BackendSelection::m_configFilename
QString m_configFilename
Definition: backendselect.h:74
BackendSelection::m_pinCode
QString m_pinCode
Definition: backendselect.h:83
BackendSelection::Accept
void Accept(void)
Definition: backendselect.cpp:129
BackendSelection::m_saveButton
MythUIButton * m_saveButton
Definition: backendselect.h:80
kBackendURI
const QString kBackendURI
Definition: backendselect.h:20
MythUIButtonListItem::GetData
QVariant GetData()
Definition: mythuibuttonlist.cpp:3656
SSDPCacheEntries
Definition: ssdpcache.h:35
UPnPResult_ActionNotAuthorized
@ UPnPResult_ActionNotAuthorized
Definition: upnp.h:48
BackendSelection::ConnectBackend
bool ConnectBackend(DeviceLocation *dev)
Attempt UPnP connection to a backend device, get its DB details.
Definition: backendselect.cpp:192
BackendSelection::~BackendSelection
~BackendSelection() override
Definition: backendselect.cpp:35
BackendSelection::m_backendList
MythUIButtonList * m_backendList
Definition: backendselect.h:78
BackendSelection::m_usn
QString m_usn
Definition: backendselect.h:84
BackendSelection::Prompt
static Decision Prompt(DatabaseParams *dbParams, const QString &config_filename)
Definition: backendselect.cpp:54
BackendSelection::TryDBfromURL
bool TryDBfromURL(const QString &error, const QString &URL)
Definition: backendselect.cpp:292
BackendSelection::m_backendDecision
Decision m_backendDecision
Definition: backendselect.h:88
BackendSelection::Create
bool Create(void) override
Definition: backendselect.cpp:78
XMLParseBase::LoadWindowFromXML
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
Definition: xmlparsebase.cpp:687
BackendSelection::PromptForPassword
void PromptForPassword(void)
Definition: backendselect.cpp:361
BackendSelection::kManualConfigure
@ kManualConfigure
Definition: backendselect.h:43
std
Definition: mythchrono.h:23
DialogCompletionEvent
Event dispatched from MythUI modal dialogs to a listening class containing a result of some form.
Definition: mythdialogbox.h:41
BackendSelection::Decision
Decision
Definition: backendselect.h:41
configuration.h
MythScreenStack::PopScreen
virtual void PopScreen(MythScreenType *screen=nullptr, bool allowFade=true, bool deleteScreen=true)
Definition: mythscreenstack.cpp:86
kDefaultUSN
const QString kDefaultUSN
Definition: backendselect.h:25
DialogCompletionEvent::kEventType
static const Type kEventType
Definition: mythdialogbox.h:57
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:104
MythXMLClient::GetConnectionInfo
UPnPResultCode GetConnectionInfo(const QString &sPin, DatabaseParams *pParams, QString &sMsg)
Definition: mythxmlclient.cpp:35
mythuibutton.h
BackendSelection::Load
void Load(void) override
Load data which will ultimately be displayed on-screen or used to determine what appears on-screen (S...
Definition: backendselect.cpp:248
MythMainWindow::GetStack
MythScreenStack * GetStack(const QString &Stackname)
Definition: mythmainwindow.cpp:323
BackendSelection::m_loop
QEventLoop * m_loop
Definition: backendselect.h:89
kDefaultPIN
const QString kDefaultPIN
Definition: backendselect.h:24
BackendSelection::m_cancelButton
MythUIButton * m_cancelButton
Definition: backendselect.h:81
SSDP::AddListener
static void AddListener(QObject *listener)
Definition: ssdp.h:127
BackendSelection::m_manualButton
MythUIButton * m_manualButton
Definition: backendselect.h:79
UPnPResult_Success
@ UPnPResult_Success
Definition: upnp.h:37
backendselect.h
d
static const iso6937table * d
Definition: iso6937tables.cpp:1025
MythTextInputDialog
Dialog prompting the user to enter a text string.
Definition: mythdialogbox.h:314
MythUIButtonList
List widget, displays list items in a variety of themeable arrangements and can trigger signals when ...
Definition: mythuibuttonlist.h:191
ReferenceCounter::IncrRef
virtual int IncrRef(void)
Increments reference count.
Definition: referencecounter.cpp:101
mythmainwindow.h
MythScreenStack::AddScreen
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
Definition: mythscreenstack.cpp:52
ShowOkPopup
MythConfirmationDialog * ShowOkPopup(const QString &message, bool showCancel)
Non-blocking version of MythPopupBox::showOkPopup()
Definition: mythdialogbox.cpp:562
SSDP::RemoveListener
static void RemoveListener(QObject *listener)
Definition: ssdp.h:129
UPnPResultCode
UPnPResultCode
Definition: upnp.h:35
DeviceLocation::GetDeviceDetail
void GetDeviceDetail(InfoMap &map)
Definition: upnpdevice.h:305