1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
// -*- Mode: c++ -*-
#include <QCoreApplication>
#include "mythuistatetype.h"
#include "mythmainwindow.h"
#include "mythdialogbox.h"
#include "backendselect.h"
#include "configuration.h"
#include "mythxmlclient.h"
#include "mythuibutton.h"
#include "mythlogging.h"
#include "mythversion.h"
BackendSelection::BackendSelection(
MythScreenStack *parent, DatabaseParams *params,
Configuration *conf, bool exitOnFinish) :
MythScreenType(parent, "BackEnd Selection"),
m_DBparams(params), m_pConfig(conf), m_exitOnFinish(exitOnFinish),
m_backendList(NULL), m_manualButton(NULL), m_saveButton(NULL),
m_cancelButton(NULL), m_backendDecision(kCancelConfigure)
{
}
BackendSelection::~BackendSelection()
{
SSDP::RemoveListener(this);
ItemMap::iterator it;
for (it = m_devices.begin(); it != m_devices.end(); ++it)
{
if (*it)
(*it)->DecrRef();
}
m_devices.clear();
}
BackendSelection::Decision BackendSelection::Prompt(
DatabaseParams *dbParams, Configuration *pConfig)
{
Decision ret = kCancelConfigure;
MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
if (!mainStack)
return ret;
BackendSelection *backendSettings =
new BackendSelection(mainStack, dbParams, pConfig, true);
if (backendSettings->Create())
{
mainStack->AddScreen(backendSettings, false);
qApp->exec();
ret = backendSettings->m_backendDecision;
mainStack->PopScreen(backendSettings, false);
}
else
delete backendSettings;
return ret;
}
bool BackendSelection::Create(void)
{
if (!LoadWindowFromXML("config-ui.xml", "backendselection", this))
return false;
m_backendList = dynamic_cast<MythUIButtonList*>(GetChild("backends"));
m_saveButton = dynamic_cast<MythUIButton*>(GetChild("save"));
m_cancelButton = dynamic_cast<MythUIButton*>(GetChild("cancel"));
m_manualButton = dynamic_cast<MythUIButton*>(GetChild("manual"));
//m_searchButton = dynamic_cast<MythUIButton*>(GetChild("search"));
connect(m_backendList, SIGNAL(itemClicked(MythUIButtonListItem *)),
SLOT(Accept(MythUIButtonListItem *)));
// connect(m_searchButton, SIGNAL(clicked()), SLOT(Search()));
connect(m_manualButton, SIGNAL(Clicked()), SLOT(Manual()));
connect(m_cancelButton, SIGNAL(Clicked()), SLOT(Cancel()));
connect(m_saveButton, SIGNAL(Clicked()), SLOT(Accept()));
BuildFocusList();
LoadInBackground();
return true;
}
void BackendSelection::Accept(MythUIButtonListItem *item)
{
if (!item)
return;
DeviceLocation *dev = qVariantValue<DeviceLocation *>(item->GetData());
if (!dev)
Cancel();
if (ConnectBackend(dev)) // this does a Release()
{
if (m_pConfig)
{
if (m_pinCode.length())
m_pConfig->SetValue(kDefaultPIN, m_pinCode);
m_pConfig->SetValue(kDefaultUSN, m_USN);
m_pConfig->Save();
}
CloseWithDecision(kAcceptConfigure);
}
}
void BackendSelection::Accept(void)
{
MythUIButtonListItem *item = m_backendList->GetItemCurrent();
if (!item)
return;
Accept(item);
}
void BackendSelection::AddItem(DeviceLocation *dev)
{
if (!dev)
return;
QString USN = dev->m_sUSN;
m_mutex.lock();
// The devices' USN should be unique. Don't add if it is already there:
if (m_devices.find(USN) == m_devices.end())
{
dev->IncrRef();
m_devices.insert(USN, dev);
m_mutex.unlock();
InfoMap infomap;
dev->GetDeviceDetail(infomap);
// We only want the version number, not the library version info
infomap["version"] = infomap["modelnumber"].section('.', 0, 1);
MythUIButtonListItem *item;
item = new MythUIButtonListItem(m_backendList, infomap["modelname"],
qVariantFromValue(dev));
item->SetTextFromMap(infomap);
bool protoMatch = (infomap["protocolversion"] == MYTH_PROTO_VERSION);
QString status = "good";
if (!protoMatch)
status = "protocolmismatch";
// TODO: Not foolproof but if we can't get device details then it's
// probably because we could not connect to port 6544 - firewall?
// Maybe we can replace this with a more specific check
if (infomap["modelname"].isEmpty())
status = "blocked";
item->DisplayState(status, "connection");
bool needPin = dev->NeedSecurityPin();
item->DisplayState(needPin ? "yes" : "no", "securitypin");
}
else
m_mutex.unlock();
}
/**
* Attempt UPnP connection to a backend device, get its DB details.
* Will loop until a valid PIN is entered.
*/
bool BackendSelection::ConnectBackend(DeviceLocation *dev)
{
QString error;
QString message;
UPnPResultCode stat;
m_USN = dev->m_sUSN;
MythXMLClient client( dev->m_sLocation );
stat = client.GetConnectionInfo(m_pinCode, m_DBparams, message);
QString backendName = dev->GetFriendlyName();
if (backendName == "<Unknown>")
backendName = dev->m_sLocation;
switch (stat)
{
case UPnPResult_Success:
LOG(VB_UPNP, LOG_INFO,
QString("ConnectBackend() - success. New hostname: %1")
.arg(m_DBparams->dbHostName));
return true;
case UPnPResult_HumanInterventionRequired:
LOG(VB_GENERAL, LOG_ERR, QString("Need Human: %1").arg(message));
ShowOkPopup(message);
if (TryDBfromURL("", dev->m_sLocation))
return true;
break;
case UPnPResult_ActionNotAuthorized:
LOG(VB_GENERAL, LOG_ERR,
QString("Access denied for %1. Wrong PIN?")
.arg(backendName));
PromptForPassword();
break;
default:
LOG(VB_GENERAL, LOG_ERR,
QString("GetConnectionInfo() failed for %1 : %2")
.arg(backendName).arg(message));
ShowOkPopup(message);
}
// Back to the list, so the user can choose a different backend:
SetFocusWidget(m_backendList);
return false;
}
void BackendSelection::Cancel(void)
{
CloseWithDecision(kCancelConfigure);
}
void BackendSelection::Load(void)
{
SSDP::Instance()->AddListener(this);
SSDP::Instance()->PerformSearch(gBackendURI);
}
void BackendSelection::Init(void)
{
SSDPCacheEntries *pEntries = SSDPCache::Instance()->Find(gBackendURI);
if (pEntries)
{
EntryMap ourMap;
pEntries->GetEntryMap(ourMap);
pEntries->DecrRef();
EntryMap::const_iterator it;
for (it = ourMap.begin(); it != ourMap.end(); ++it)
{
AddItem(*it);
(*it)->DecrRef();
}
}
}
void BackendSelection::Manual(void)
{
CloseWithDecision(kManualConfigure);
}
void BackendSelection::RemoveItem(QString USN)
{
m_mutex.lock();
ItemMap::iterator it = m_devices.find(USN);
if (it != m_devices.end())
{
if (*it)
(*it)->DecrRef();
m_devices.erase(it);
}
m_mutex.unlock();
}
bool BackendSelection::TryDBfromURL(const QString &error, QString URL)
{
if (ShowOkPopup(error + tr("Shall I attempt to connect to this"
" host with default database parameters?")))
{
URL.remove("http://");
URL.remove(QRegExp("[:/].*"));
m_DBparams->dbHostName = URL;
return true;
}
return false;
}
void BackendSelection::customEvent(QEvent *event)
{
if (((MythEvent::Type)(event->type())) == MythEvent::MythEventMessage)
{
MythEvent *me = (MythEvent *)event;
QString message = me->Message();
QString URI = me->ExtraData(0);
QString URN = me->ExtraData(1);
QString URL = me->ExtraData(2);
LOG(VB_UPNP, LOG_DEBUG,
QString("BackendSelection::customEvent(%1, %2, %3, %4)")
.arg(message).arg(URI).arg(URN).arg(URL));
if (message.startsWith("SSDP_ADD") &&
URI.startsWith("urn:schemas-mythtv-org:device:MasterMediaServer:"))
{
DeviceLocation *devLoc = SSDP::Instance()->Find(URI, URN);
if (devLoc)
{
AddItem(devLoc);
devLoc->DecrRef();
}
}
else if (message.startsWith("SSDP_REMOVE"))
{
//-=>Note: This code will never get executed until
// SSDPCache is changed to handle NotifyRemove correctly
RemoveItem(URN);
}
}
else if (event->type() == DialogCompletionEvent::kEventType)
{
DialogCompletionEvent *dce = dynamic_cast<DialogCompletionEvent*>(event);
if (!dce)
return;
QString resultid = dce->GetId();
if (resultid == "password")
{
m_pinCode = dce->GetResultText();
Accept();
}
}
}
void BackendSelection::PromptForPassword(void)
{
QString message = tr("Please enter the backend access PIN");
MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
MythTextInputDialog *pwDialog = new MythTextInputDialog(popupStack,
message,
FilterNone,
true);
if (pwDialog->Create())
{
pwDialog->SetReturnEvent(this, "password");
popupStack->AddScreen(pwDialog);
}
else
delete pwDialog;
}
void BackendSelection::Close(void)
{
CloseWithDecision(kCancelConfigure);
}
void BackendSelection::CloseWithDecision(Decision d)
{
m_backendDecision = d;
if (m_exitOnFinish)
qApp->quit();
else
MythScreenType::Close();
}
|