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