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  qVariantFromValue(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")
207  .arg(m_DBparams->dbHostName));
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, QString URL)
289 {
290  if (ShowOkPopup(error + tr("Shall I attempt to connect to this"
291  " host with default database parameters?")))
292  {
293  URL.remove("http://");
294  URL.remove(QRegExp("[:/].*"));
296  return true;
297  }
298 
299  return false;
300 }
301 
302 void BackendSelection::customEvent(QEvent *event)
303 {
304  if (event->type() == MythEvent::MythEventMessage)
305  {
306  auto *me = dynamic_cast<MythEvent *>(event);
307  if (me == nullptr)
308  return;
309 
310  const QString& message = me->Message();
311  const QString& URI = me->ExtraData(0);
312  const QString& URN = me->ExtraData(1);
313  const QString& URL = me->ExtraData(2);
314 
315 
316  LOG(VB_UPNP, LOG_DEBUG,
317  QString("BackendSelection::customEvent(%1, %2, %3, %4)")
318  .arg(message).arg(URI).arg(URN).arg(URL));
319 
320  if (message.startsWith("SSDP_ADD") &&
321  URI.startsWith("urn:schemas-mythtv-org:device:MasterMediaServer:"))
322  {
323  DeviceLocation *devLoc = SSDP::Find(URI, URN);
324  if (devLoc)
325  {
326  AddItem(devLoc);
327  devLoc->DecrRef();
328  }
329  }
330  else if (message.startsWith("SSDP_REMOVE"))
331  {
332  //-=>Note: This code will never get executed until
333  // SSDPCache is changed to handle NotifyRemove correctly
334  RemoveItem(URN);
335  }
336  }
337  else if (event->type() == DialogCompletionEvent::kEventType)
338  {
339  auto *dce = dynamic_cast<DialogCompletionEvent*>(event);
340  if (!dce)
341  return;
342 
343  QString resultid = dce->GetId();
344 
345  if (resultid == "password")
346  {
347  m_pinCode = dce->GetResultText();
348  Accept();
349  }
350  }
351 }
352 
354 {
355  QString message = tr("Please enter the backend access PIN");
356 
357  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
358 
359  auto *pwDialog = new MythTextInputDialog(popupStack, message,
360  FilterNone, true);
361 
362  if (pwDialog->Create())
363  {
364  pwDialog->SetReturnEvent(this, "password");
365  popupStack->AddScreen(pwDialog);
366  }
367  else
368  delete pwDialog;
369 }
370 
372 {
374 }
375 
377 {
379 
380  if (m_exitOnFinish)
381  m_loop->quit();
382  else
384 }
MythUIButton * m_saveButton
Definition: backendselect.h:82
static SSDPCache * Instance()
Definition: ssdpcache.cpp:240
void RemoveItem(const QString &USN)
MythConfirmationDialog * ShowOkPopup(const QString &message, QObject *parent, const char *slot, bool showCancel)
Non-blocking version of MythPopupBox::showOkPopup()
QString m_sUSN
Definition: upnpdevice.h:228
UPnPResultCode
Definition: upnp.h:31
QHash< QString, QString > InfoMap
Definition: mythtypes.h:15
static Type MythEventMessage
Definition: mythevent.h:66
bool ConnectBackend(DeviceLocation *dev)
Attempt UPnP connection to a backend device, get its DB details.
static void error(const char *str,...)
Definition: vbi.c:42
bool Create(void) override
MythUIButtonList * m_backendList
Definition: backendselect.h:80
static void AddListener(QObject *listener)
Definition: ssdp.h:121
const QString gBackendURI
Service type for the backend's UPnP server.
Definition: backendselect.h:21
QString GetFriendlyName(void)
Definition: upnpdevice.h:274
static const char URL[]
Definition: cddb.cpp:29
MythScreenStack * GetStack(const QString &stackname)
Decision m_backendDecision
Definition: backendselect.h:91
static Decision Prompt(DatabaseParams *dbParams, Configuration *pConfig)
MythScreenStack * GetMainStack()
void PromptForPassword(void)
QEventLoop * m_loop
Definition: backendselect.h:92
MythUIButton * m_cancelButton
Definition: backendselect.h:83
MythUIButton * m_manualButton
Definition: backendselect.h:81
void BuildFocusList(void)
static Type kEventType
Definition: mythdialogbox.h:50
BackendSelection(MythScreenStack *parent, DatabaseParams *params, Configuration *pConfig, bool exitOnFinish=false)
void Load(void) override
Load data which will ultimately be displayed on-screen or used to determine what appears on-screen (S...
virtual void Close()
void GetEntryMap(EntryMap &)
Returns a copy of the EntryMap.
Definition: ssdpcache.cpp:85
void LoadInBackground(const QString &message="")
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
void PerformSearch(const QString &sST, uint timeout_secs=2)
Definition: ssdp.cpp:206
virtual int IncrRef(void)
Increments reference count.
virtual ~BackendSelection()
void Cancel(void)
Linked to 'Cancel' button.
static const uint16_t * d
void Close(void) override
virtual int DecrRef(void)
Decrements reference count and deletes on 0.
virtual bool Save(void)=0
#define MYTH_PROTO_VERSION
Increment this whenever the MythTV network protocol changes.
Definition: mythversion.h:48
void customEvent(QEvent *event) override
DatabaseParams * m_DBparams
Definition: backendselect.h:75
static void RemoveListener(QObject *listener)
Definition: ssdp.h:123
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
MythMainWindow * GetMythMainWindow(void)
bool NeedSecurityPin(void)
Definition: upnpdevice.h:312
QMap< QString, DeviceLocation * > EntryMap
Key == Unique Service Name (USN)
Definition: ssdpcache.h:28
void Init(void) override
Used after calling Load() to assign data to widgets and other UI initilisation which is prohibited in...
Dialog prompting the user to enter a text string.
Configuration * m_pConfig
Definition: backendselect.h:76
Structure containing the basic Database parameters.
Definition: mythdbparams.h:9
QString m_sLocation
Definition: upnpdevice.h:229
void GetDeviceDetail(InfoMap &map)
Definition: upnpdevice.h:301
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
QString dbHostName
database server
Definition: mythdbparams.h:21
static SSDP * Instance()
Definition: ssdp.cpp:54
virtual void PopScreen(MythScreenType *screen=nullptr, bool allowFade=true, bool deleteScreen=true)
void AddItem(DeviceLocation *dev)
UPnPResultCode GetConnectionInfo(const QString &sPin, DatabaseParams *pParams, QString &sMsg)
const QString kDefaultPIN
Definition: backendselect.h:25
bool SetFocusWidget(MythUIType *widget=nullptr)
const QString kDefaultUSN
Definition: backendselect.h:26
bool TryDBfromURL(const QString &error, QString URL)
static SSDPCacheEntries * Find(const QString &sURI)
Definition: ssdp.h:126
Screen in which all other widgets are contained and rendered.
SSDPCacheEntries * Find(const QString &sURI)
Finds the SSDPCacheEntries in the cache, returns nullptr when absent.
Definition: ssdpcache.cpp:297
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:132
void Manual(void)
Linked to 'Configure Manually' button.
void CloseWithDecision(Decision)
MythUIButtonListItem * GetItemCurrent() const
virtual void SetValue(const QString &sSetting, int value)=0