Parent: [60454b] (diff)

Child: [5addbf] (diff)

Download this file

cdbrowser.cpp    1188 lines (1066 with data), 38.0 kB

   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
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
/* Copyright (C) 2005 J.F.Dockes
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "cdbrowser.h"
#include <iostream>
#include <functional>
#include "libupnpp/config.h"
#include <QUrl>
#ifdef USING_WEBENGINE
// Notes for WebEngine
// - All links must begin with http:// for acceptNavigationRequest to be
// called.
// - The links passed to acceptNav.. have the host part
// lowercased -> we change S0 to http://h/S0, not http://S0
// As far as I can see, 2016-09, two relatively small issues remaining:
// - No way to know when page load is done (see mySetHtml())
// - No way to save/restore the scroll position, all the rest works ok.
#include <QWebEnginePage>
#include <QWebEngineSettings>
#include <QtWebEngineWidgets>
#include <QLoggingCategory>
#else
#include <QWebFrame>
#include <QWebSettings>
#include <QWebElement>
#include <QScriptEngine>
#endif
#include <QMessageBox>
#include <QMenu>
#include <QApplication>
#include <QByteArray>
#include <QProgressDialog>
#include <QClipboard>
#include "HelperStructs/Helper.h"
#include "HelperStructs/Style.h"
#include "HelperStructs/CSettingsStorage.h"
#include "libupnpp/log.hxx"
#include "libupnpp/control/discovery.hxx"
#include "upqo/cdirectory_qo.h"
#include "upadapt/upputils.h"
#include "upadapt/md5.hxx"
#include "dirbrowser.h"
#include "rreaper.h"
using namespace std;
using namespace std::placeholders;
using namespace UPnPP;
using namespace UPnPClient;
static const string minimFoldersViewPrefix("0$folders");
void CDWebPage::javaScriptConsoleMessage(
#ifdef USING_WEBENGINE
JavaScriptConsoleMessageLevel,
#endif
const QString& msg, int lineNum, const QString&)
{
Q_UNUSED(msg);
Q_UNUSED(lineNum);
LOGDEB("JAVASCRIPT: "<< qs2utf8s(msg) << " at line " << lineNum << endl);
}
CDBrowser::CDBrowser(QWidget* parent)
: QWEBVIEW(parent), m_reader(0), m_reaper(0), m_progressD(0),
m_browsers(0), m_lastbutton(Qt::LeftButton), m_sysUpdId(0)
{
setPage(new CDWebPage(this));
#ifndef USING_WEBENGINE
connect(this, SIGNAL(linkClicked(const QUrl &)),
this, SLOT(onLinkClicked(const QUrl &)));
// Not available and not sure that this is needed with webengine ?
connect(page()->mainFrame(), SIGNAL(contentsSizeChanged(const QSize&)),
this, SLOT(onContentsSizeChanged(const QSize&)));
page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
#endif
settings()->setAttribute(QWEBSETTINGS::JavascriptEnabled, true);
if (parent) {
settings()->setFontSize(QWEBSETTINGS::DefaultFontSize,
parent->font().pointSize()+4);
settings()->setFontFamily(QWEBSETTINGS::StandardFont,
parent->font().family());
}
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
this, SLOT(createPopupMenu(const QPoint&)));
m_timer.setSingleShot(1);
connect(&m_timer, SIGNAL(timeout()), this, SLOT(initialPage()));
m_timer.start(0);
}
CDBrowser::~CDBrowser()
{
deleteReaders();
}
void CDBrowser::mySetHtml(const QString& html)
{
// Tried to wait using an event loop, but it seems that the
// loadFinished() signal is only ever triggered once, even if we
// replace with a new page ???
// QEventLoop pause;
// setPage(new CDWebPage(this));
// connect(page(), SIGNAL(loadFinished(bool)), &pause, SLOT(quit()));
setHtml(html);
Helper::msleep(100);
//pause.exec();
}
void CDBrowser::mouseReleaseEvent(QMouseEvent *event)
{
//qDebug() << "CDBrowser::mouseReleaseEvent";
m_lastbutton = event->button();
QWEBVIEW::mouseReleaseEvent(event);
}
void CDBrowser::onContentsSizeChanged(const QSize&)
{
//qDebug() << "CDBrowser::onContentsSizeChanged: scrollpos " <<
// page()->mainFrame()->scrollPosition();
#ifndef USING_WEBENGINE
// No way to do this with webengine ?
page()->mainFrame()->setScrollPosition(m_savedscrollpos);
#endif
}
#ifdef USING_WEBENGINE
static QString base64_encode(QString string)
{
QByteArray ba;
ba.append(string);
return ba.toBase64();
}
#endif
void CDBrowser::runJS(const QString& js)
{
#ifdef USING_WEBENGINE
page()->runJavaScript(js, [] (QVariant res) {
Q_UNUSED(res);
});
#else
page()->mainFrame()->evaluateJavaScript(js);
#endif
}
void CDBrowser::appendHtml(const QString& elt_id, const QString& html)
{
LOGDEB1("CDBrowser::appendHtml: elt_id [" << qs2utf8s(elt_id) <<
"] html "<< qs2utf8s(html) << endl);
#ifdef USING_WEBENGINE
// With webengine, we can't access the qt object from the page, so
// we do everything in js. The data is encoded as base64 to avoid
// quoting issues
QString js;
js = "var morehtml = '" + base64_encode(html) + "';\n";
if (elt_id.isEmpty()) {
js += QString("document.body.innerHTML += window.atob(morehtml);\n");
} else {
js += QString("document.getElementById(\"%1\").innerHTML += "
"window.atob(morehtml);\n").arg(elt_id);
}
page()->runJavaScript(js, [] (QVariant res) {
Q_UNUSED(res);
});
#else
QWebFrame *mainframe = page()->mainFrame();
StringObj morehtml(html);
mainframe->addToJavaScriptWindowObject("morehtml", &morehtml,
SCRIPTOWNERSHIP);
QString js;
if (elt_id.isEmpty()) {
js = QString("document.body.innerHTML += morehtml.text");
} else {
js = QString("document.getElementById(\"%1\").innerHTML += "
"morehtml.text").arg(elt_id);
}
mainframe->evaluateJavaScript(js);
#endif
}
bool CDBrowser::newCds(int cdsidx)
{
if (cdsidx > int(m_msdescs.size())) {
LOGERR("CDBrowser::newCds: bad link index: " << cdsidx
<< " cd count: " << m_msdescs.size() << endl);
return false;
}
MSRH ms = MSRH(new MediaServer(m_msdescs[cdsidx]));
if (!ms) {
qDebug() << "MediaServer connect failed";
return false;
}
CDSH cds = ms->cds();
if (!cds) {
LOGERR("CDBrowser::newCds: null cds" << endl);
return false;
}
m_cds = std::shared_ptr<ContentDirectoryQO>(new ContentDirectoryQO(cds));
m_sysUpdId = 0;
connect(m_cds.get(), SIGNAL(systemUpdateIDChanged(int)),
this, SLOT(onSysUpdIdChanged(int)));
return true;
}
void CDBrowser::onSysUpdIdChanged(int id)
{
qDebug() << "CDBrowser::onSysUpdIdChanged: mine " << m_sysUpdId <<
"server" << id;
if (!QSettings().value("monitorupdateid").toBool()) {
return;
}
// 1st time is free
if (!m_sysUpdId) {
m_sysUpdId = id;
return;
}
// We should try to use the containerUpdateIDs to make sure of
// what needs to be done, instead of dumping the problem on the
// user.
if (m_sysUpdId != id) {
m_sysUpdId = id;
// CDSKIND_BUBBLE, CDSKIND_MEDIATOMB,
// CDSKIND_MINIDLNA, CDSKIND_MINIM, CDSKIND_TWONKY
ContentDirectory::ServiceKind kind = m_cds->srv()->getKind();
switch (kind) {
// Not too sure which actually invalidate their
// tree. Pretty sure that Minim does not (we might just
// want to reload the current dir).
case ContentDirectory::CDSKIND_MINIDLNA: break;
case ContentDirectory::CDSKIND_MEDIATOMB: break;
// By default, don't do anything by default because some
// cds keep changing their global updateid. We'd need to
// check the containerUpdateID.
case ContentDirectory::CDSKIND_MINIM:
default: return;
}
QMessageBox::Button rep =
QMessageBox::question(0, "Upplay",
tr("Content Directory Server state changed, "
"some references may be invalid. (some "
"playlist elements may be invalid too). "
"<br>Reset browse state ?"),
QMessageBox::Ok | QMessageBox::Cancel);
if (rep == QMessageBox::Ok) {
curpathClicked(1);
m_sysUpdId = 0;
}
}
}
void CDBrowser::onLinkClicked(const QUrl &url)
{
m_timer.stop();
string scurl = qs2utf8s(url.toString());
qDebug() << "CDBrowser::onLinkClicked: " << url.toString() <<
" button " << m_lastbutton << " mid " << Qt::MidButton;
// Get rid of http://
if (scurl.find("http://h/") != 0) {
qDebug() << "CDBrowser::onLinkClicked: bad link ! : " << url.toString();
return;
}
scurl = scurl.substr(9);
LOGDEB("CDBrowser::onLinkClicked: corrected url: [" << scurl << "]" <<endl);
int what = scurl[0];
switch (what) {
case 'a':
{
string initial = scurl.substr(1,1);
qDebug() << "INITIAL: " << initial.c_str();
auto it = m_alphamap.find(initial);
if (it != m_alphamap.end()) {
m_browsers->doSearch(u8s2qs(it->second), false);
}
}
break;
case 'S':
{
// Servers page server link click: browse clicked server root
unsigned int cdsidx = atoi(scurl.c_str()+1);
m_curpath.clear();
m_curpath.push_back(CtPathElt("0", "(servers)"));
if (!newCds(cdsidx)) {
return;
}
m_cds->srv()->getSearchCapabilities(m_searchcaps);
emit sig_searchcaps_changed();
browseContainer("0", m_msdescs[cdsidx].friendlyName);
}
break;
case 'I':
{
// Directory listing item link clicked: add to playlist
unsigned int i = atoi(scurl.c_str()+1);
if (i > m_entries.size()) {
LOGERR("CDBrowser::onLinkClicked: bad objid index: " << i
<< " id count: " << m_entries.size() << endl);
return;
}
MetaDataList mdl;
mdl.resize(1);
udirentToMetadata(&m_entries[i], &mdl[0]);
emit sig_tracks_to_playlist(mdl);
}
break;
case 'C':
{
// Directory listing container link clicked: browse subdir.
m_curinitial = 0;
#ifdef USING_WEBENGINE
#warning tobedone
#else
m_curpath.back().scrollpos = page()->mainFrame()->scrollPosition();
#endif
unsigned int i = atoi(scurl.c_str()+1);
if (i > m_entries.size()) {
LOGERR("CDBrowser::onLinkClicked: bad objid index: " << i
<< " id count: " << m_entries.size() << endl);
return;
}
if (m_lastbutton == Qt::MidButton) {
// Open in new tab
vector<CtPathElt> npath(m_curpath);
npath.push_back(CtPathElt(m_entries[i].m_id, m_entries[i].m_title));
emit sig_browse_in_new_tab(u8s2qs(m_cds->srv()->getDeviceId()),
npath);
} else {
browseContainer(m_entries[i].m_id, m_entries[i].m_title);
}
return;
}
break;
case 'L':
{
// Click in curpath section.
unsigned int i = atoi(scurl.c_str()+1);
curpathClicked(i);
return;
}
break;
default:
LOGERR("CDBrowser::onLinkClicked: bad link type: " << what << endl);
return;
}
}
void CDBrowser::curpathClicked(unsigned int i)
{
//qDebug() << "CDBrowser::curpathClicked: " << i << " pathsize " <<
// m_curpath.size();
if (i >= m_curpath.size()) {
LOGERR("CDBrowser::curPathClicked: bad curpath index: " << i
<< " path count: " << m_curpath.size() << endl);
return;
}
if (i == 0) {
m_curpath.clear();
m_msdescs.clear();
m_cds.reset();
initialPage();
} else {
string objid = m_curpath[i].objid;
string title = m_curpath[i].title;
string ss = m_curpath[i].searchStr;
QPoint scrollpos = m_curpath[i].scrollpos;
m_curpath.erase(m_curpath.begin() + i, m_curpath.end());
if (m_lastbutton == Qt::MidButton) {
vector<CtPathElt> npath(m_curpath);
npath.push_back(CtPathElt(objid, title, ss));
emit sig_browse_in_new_tab(u8s2qs(m_cds->srv()->getDeviceId()),
npath);
} else {
if (ss.empty()) {
browseContainer(objid, title, scrollpos);
} else {
search(objid, ss, scrollpos);
}
}
}
}
void CDBrowser::browseIn(QString UDN, vector<CtPathElt> path)
{
//qDebug() << "CDBrowser::browsein: " << UDN;
m_curpath = path;
if (m_msdescs.size() == 0) {
// If the servers list is not ready yet, just set m_initUDN to tell
// initialPage() to do the right thing later when it gets the servers
// qDebug() << "CDBrowser::browsein: no servers";
m_initUDN = UDN;
return;
}
m_lastbutton = Qt::LeftButton;
// Find UDN in msdescs, create a MediaServer object, and read the
// specified container (last in input curpath)
string sudn = qs2utf8s(UDN);
for (unsigned int i = 0; i < m_msdescs.size(); i++) {
if (m_msdescs[i].UDN == sudn) {
newCds(i);
curpathClicked(path.size() - 1);
return;
}
}
qDebug() << "CDBrowser::browsein: " << UDN << " not found";
}
// Re-browse (because sort criteria changed probably)
void CDBrowser::refresh()
{
if (m_curpath.size() >= 1) {
curpathClicked(m_curpath.size() - 1);
}
}
void CDBrowser::browseContainer(string ctid, string cttitle, QPoint scrollpos)
{
LOGDEB("CDBrowser::browseContainer: " << " ctid " << ctid << endl);
deleteReaders();
emit sig_now_in(this, QString::fromUtf8(cttitle.c_str()));
m_savedscrollpos = scrollpos;
if (!m_cds) {
LOGERR("CDBrowser::browseContainer: server not set" << endl);
return;
}
m_entries.clear();
m_curpath.push_back(CtPathElt(ctid, cttitle));
initContainerHtml();
m_reader = new CDBrowseQO(m_cds->srv(), ctid, string(), this);
connect(m_reader, SIGNAL(sliceAvailable(UPnPClient::UPnPDirContent*)),
this, SLOT(onSliceAvailable(UPnPClient::UPnPDirContent *)));
connect(m_reader, SIGNAL(done(int)), this, SLOT(onBrowseDone(int)));
m_reader->start();
}
void CDBrowser::search(const string& iss)
{
search(m_curpath.back().objid, iss);
}
void CDBrowser::search(const string& objid, const string& iss, QPoint scrollpos)
{
deleteReaders();
if (iss.empty())
return;
if (!m_cds) {
LOGERR("CDBrowser::search: server not set" << endl);
return;
}
m_savedscrollpos = scrollpos;
m_entries.clear();
m_curpath.push_back(CtPathElt(objid, "(search)", iss));
initContainerHtml(iss);
m_reader = new CDBrowseQO(m_cds->srv(), m_curpath.back().objid, iss, this);
connect(m_reader, SIGNAL(sliceAvailable(UPnPClient::UPnPDirContent*)),
this, SLOT(onSliceAvailable(UPnPClient::UPnPDirContent *)));
connect(m_reader, SIGNAL(done(int)), this, SLOT(onBrowseDone(int)));
m_reader->start();
}
void CDBrowser::deleteReaders()
{
//qDebug() << "deleteReaders()";
if (m_reader) {
m_reader->setCancel();
m_reader->wait();
delete m_reader;
m_reader = 0;
}
if (m_reaper) {
m_reaper->setCancel();
m_reaper->wait();
delete m_reaper;
m_reaper = 0;
}
// Give a chance to queued signals (a thread could have queued
// something before/while we're cancelling it). Else, typically
// onSliceAvailable could be called at a later time and mess the
// display.
qApp->processEvents();
}
void CDBrowser::updateAlphamap(const string& tt)
{
char ninit = ::toupper(tt[0]);
if (ninit != m_curinitial) {
m_alphamap[string(1, ninit)] = CTTitleStartMarker + tt;
m_curinitial = ninit;
}
}
void CDBrowser::onSliceAvailable(UPnPDirContent *dc)
{
//qDebug() << "CDBrowser::onSliceAvailable";
if (!m_reader) {
qDebug() << "CDBrowser::onSliceAvailable: no reader";
// Cancelled.
delete dc;
return;
}
QString html;
m_entries.reserve(m_entries.size() + dc->m_containers.size() +
dc->m_items.size());
for (auto& entry: dc->m_containers) {
//qDebug() << "Container: " << it->dump().c_str();;
updateAlphamap(entry.m_title);
m_entries.push_back(entry);
html += CTToHtml(m_entries.size()-1, entry);
}
if (m_entries.size() < 40) {
runJS("document.getElementById('alphalist').style.visibility = 'hidden';");
} else {
runJS("document.getElementById('alphalist').style.visibility = 'visible';");
}
QSettings settings;
int maxartlen = 0;
if (settings.value("truncateartistindir").toBool()) {
maxartlen = settings.value("truncateartistlen").toInt();
}
for (std::vector<UPnPDirObject>::iterator it = dc->m_items.begin();
it != dc->m_items.end(); it++) {
//qDebug() << "Item: " << it->dump().c_str();;
m_entries.push_back(*it);
html += ItemToHtml(m_entries.size()-1, *it, maxartlen);
}
appendHtml("entrylist", html);
delete dc;
// On very big lists (thousands), this can be quite late against
// the dir reading threads, with a long queue of slice events
// accumulated. The UI is frozen while we get called for each
// event in the queue. We'd like to give a chance to user
// interaction (e.g. scrolling)here, but it seems that there is no
// way to process only the user events, so processEvents does not help.
// Probably the only workable approach would be to do the
// appending from another thread?
// (by the way, processEvent if it worked must be called at the
// bottom of the func else we risk onBrowseDone getting called and
// deleting the reader before we can use it).
//qApp->processEvents();
}
class DirObjCmp {
public:
DirObjCmp(const vector<string>& crits)
: m_crits(crits) {}
bool operator()(const UPnPDirObject& o1, const UPnPDirObject& o2) {
int rel;
string s1, s2;
for (unsigned int i = 0; i < m_crits.size(); i++) {
const string& crit = m_crits[i];
rel = dirobgetprop(o1, crit, s1).compare(
dirobgetprop(o2, crit, s2));
if (rel < 0)
return true;
else if (rel > 0)
return false;
}
return false;
}
const string& dirobgetprop(const UPnPDirObject& o, const string& nm,
string& storage) {
if (!nm.compare("dc:title")) {
return o.m_title;
} else if (!nm.compare("uri")) {
if (o.m_resources.size() == 0)
return nullstr;
return o.m_resources[0].m_uri;
}
const string& prop = o.getprop(nm);
if (!nm.compare("upnp:originalTrackNumber")) {
char num[30];
int i = atoi(prop.c_str());
sprintf(num, "%010d", i);
storage = num;
return storage;
} else if (!nm.compare("upplay:ctpath")) {
//qDebug() << "TTPATH: " <<
// RecursiveReaper::ttpathPrintable(prop).c_str();
return prop;
} else {
return prop;
}
}
vector<string> m_crits;
static string nullstr;
};
string DirObjCmp::nullstr;
void CDBrowser::onBrowseDone(int)
{
//qDebug() <<"CDBrowser::onBrowseDone";
if (!m_reader) {
qDebug() << "CDBrowser::onBrowseDone(int) no reader: cancelled";
return;
}
vector<string> sortcrits;
int sortkind = CSettingsStorage::getInstance()->getSortKind();
if (sortkind == CSettingsStorage::SK_MINIMFNORDER &&
m_reader->getKind() == ContentDirectory::CDSKIND_MINIM &&
m_curpath.back().searchStr.empty() &&
m_reader->getObjid().compare(0, minimFoldersViewPrefix.size(),
minimFoldersViewPrefix) == 0) {
sortcrits.push_back("uri");
} else if (sortkind == CSettingsStorage::SK_CUSTOM) {
QStringList qcrits = CSettingsStorage::getInstance()->getSortCrits();
for (int i = 0; i < qcrits.size(); i++) {
sortcrits.push_back(qs2utf8s(qcrits[i]));
}
}
if (m_entries.size() < 40) {
runJS("document.getElementById('alphalist').style.visibility = 'hidden';");
} else {
runJS("document.getElementById('alphalist').style.visibility = 'visible';");
}
if (!sortcrits.empty()) {
DirObjCmp cmpo(sortcrits);
sort(m_entries.begin(), m_entries.end(), cmpo);
initContainerHtml();
QString html;
int maxartlen = 0;
QSettings settings;
if (settings.value("truncateartistindir").toBool()) {
maxartlen = settings.value("truncateartistlen").toInt();
}
m_curinitial = 0;
for (unsigned i = 0; i < m_entries.size(); i++) {
if (m_entries[i].m_type == UPnPDirObject::container) {
updateAlphamap(m_entries[i].m_title);
html += CTToHtml(i, m_entries[i]);
} else {
html += ItemToHtml(i, m_entries[i], maxartlen);
}
}
appendHtml("entrylist", html);
}
m_reader->wait();
#ifdef USING_WEBENGINE
#warning tobedone
#else
page()->mainFrame()->setScrollPosition(m_savedscrollpos);
#endif
deleteReaders();
//qDebug() << "CDBrowser::onBrowseDone done";
}
void CDBrowser::initialPage(bool redisplay)
{
deleteReaders();
emit sig_now_in(this, tr("Servers"));
vector<UPnPDeviceDesc> msdescs;
int secs = UPnPDeviceDirectory::getTheDir()->getRemainingDelay();
if (secs > 1) {
mySetHtml(initialServersPage());
qDebug() << "CDBrowser::initialPage: waiting " << secs;
m_timer.start(secs * 1000);
return;
}
m_searchcaps.clear();
emit sig_searchcaps_changed();
if (!MediaServer::getDeviceDescs(msdescs)) {
LOGERR("CDBrowser::initialPage: getDeviceDescs failed" << endl);
return;
}
//qDebug() << "CDBrowser::initialPage: " << msdescs.size() << " servers";
bool same = msdescs.size() == m_msdescs.size();
if (same) {
for (unsigned i = 0; i < msdescs.size(); i++) {
if (msdescs[i].UDN.compare(m_msdescs[i].UDN)) {
same = false;
break;
}
}
}
if (same && !redisplay) {
//LOGDEB("CDBrowser::initialPage: no change" << endl);
m_timer.start(5000);
return;
}
m_msdescs = msdescs;
if (!m_initUDN.isEmpty()) {
// "Browse in new tab": show appropriate container
QString s = m_initUDN;
m_initUDN = "";
browseIn(s, m_curpath);
} else {
// Show servers list
QString html = emptyServersPage();
mySetHtml(html);
for (unsigned i = 0; i < msdescs.size(); i++) {
appendHtml("", DSToHtml(i, msdescs[i]));
}
m_timer.start(5000);
}
}
enum PopupMode {
PUP_ADD,
PUP_ADD_ALL,
PUP_ADD_FROMHERE,
PUP_BACK,
PUP_OPEN_IN_NEW_TAB,
PUP_RAND_PLAY_TRACKS,
PUP_RAND_PLAY_GROUPS,
PUP_RAND_STOP,
PUP_SORT_ORDER,
PUP_COPY_URL,
};
void CDBrowser::onLoadFinished(bool)
{
LOGDEB("CDBrowser::onLoadFinished\n");
}
void CDBrowser::onPopupJsDone(const QVariant &jr)
{
QString qs(jr.toString());
LOGDEB("onPopupJsDone: parameter: " << qs2utf8s(qs) << endl);
QStringList qsl = qs.split("\n", QString::SkipEmptyParts);
for (int i = 0 ; i < qsl.size(); i++) {
int eq = qsl[i].indexOf("=");
if (eq > 0) {
QString nm = qsl[i].left(eq).trimmed();
QString value = qsl[i].right(qsl[i].size() - (eq+1)).trimmed();
if (!nm.compare("objid")) {
m_popupobjid = qs2utf8s(value);
} else if (!nm.compare("title")) {
m_popupobjtitle = qs2utf8s(value);
} else if (!nm.compare("objidx")) {
m_popupidx = value.toInt();
} else if (!nm.compare("otype")) {
m_popupotype = qs2utf8s(value);
} else {
LOGERR("onPopupJsDone: unknown key: " << qs2utf8s(nm) << endl);
}
}
}
LOGDEB1("onPopupJsDone: class [" << m_popupotype <<
"] objid [" << m_popupobjid <<
"] title [" << m_popupobjtitle <<
"] objidx [" << m_popupidx << "]\n");
doCreatePopupMenu();
}
void CDBrowser::createPopupMenu(const QPoint& pos)
{
if (!m_browsers || m_browsers->insertActive()) {
LOGDEB("CDBrowser::createPopupMenu: no popup: insert active\n");
return;
}
LOGDEB("CDBrowser::createPopupMenu\n");
m_popupobjid = m_popupobjtitle = m_popupotype = "";
m_popupidx = -1;
m_popupos = pos;
#ifdef USING_WEBENGINE
Q_UNUSED(pos);
QString js("window.locDetails;");
CDWebPage *mypage = dynamic_cast<CDWebPage*>(page());
QEventLoop pause;
connect(this, SIGNAL(sig_js_done()), &pause, SLOT(quit()));
mypage->runJavaScript(js, bind(&CDBrowser::onPopupJsDone, this, _1));
#else
QWebHitTestResult htr = page()->mainFrame()->hitTestContent(pos);
if (htr.isNull()) {
qDebug() << "CDBrowser::createPopupMenu: no popup: no hit";
return;
}
QWebElement el = htr.enclosingBlockElement();
while (!el.isNull() && !el.hasAttribute("objid"))
el = el.parent();
if (!el.isNull()) {
m_popupobjid = qs2utf8s(el.attribute("objid"));
m_popupobjtitle = qs2utf8s(el.toPlainText());
if (el.hasAttribute("objidx"))
m_popupidx = el.attribute("objidx").toInt();
else
m_popupidx = -1;
m_popupotype = qs2utf8s(el.attribute("class"));
LOGDEB("Popup: " << " class " << m_popupotype << " objid " <<
m_popupobjid << endl);
}
doCreatePopupMenu();
#endif
}
void CDBrowser::doCreatePopupMenu()
{
QMenu *popup = new QMenu(this);
QAction *act;
QVariant v;
if (m_curpath.size() != 0) {
// Back action
act = new QAction(tr("Back"), this);
v = QVariant(int(PUP_BACK));
act->setData(v);
popup->addAction(act);
}
// Click in blank area, or no playlist the only entry is Back
if (m_popupobjid.empty() || (m_browsers && !m_browsers->have_playlist())) {
popup->connect(popup, SIGNAL(triggered(QAction *)),
this, SLOT(back(QAction *)));
popup->popup(mapToGlobal(m_popupos));
return;
}
if (m_popupidx == -1 && m_popupotype.compare("container")) {
// All path elements should be containers !
qDebug() << "Not container and no objidx??";
return;
}
act = new QAction(tr("Send to playlist"), this);
v = QVariant(int(PUP_ADD));
act->setData(v);
popup->addAction(act);
if (!m_popupotype.compare("item")) {
act = new QAction(tr("Send all to playlist"), this);
v = QVariant(int(PUP_ADD_ALL));
act->setData(v);
popup->addAction(act);
act = new QAction(tr("Send all from here to playlist"), this);
v = QVariant(int(PUP_ADD_FROMHERE));
act->setData(v);
popup->addAction(act);
act = new QAction(tr("Copy URL"), this);
v = QVariant(int(PUP_COPY_URL));
act->setData(v);
popup->addAction(act);
}
// Connect to either recursive add or simpleAdd depending on entry type.
if (!m_popupotype.compare("container")) {
act = new QAction(tr("Open in new tab"), this);
v = QVariant(int(PUP_OPEN_IN_NEW_TAB));
act->setData(v);
popup->addAction(act);
act = new QAction(tr("Random play by tracks"), this);
v = QVariant(int(PUP_RAND_PLAY_TRACKS));
act->setData(v);
popup->addAction(act);
act = new QAction(tr("Random play by groups"), this);
v = QVariant(int(PUP_RAND_PLAY_GROUPS));
act->setData(v);
popup->addAction(act);
popup->connect(popup, SIGNAL(triggered(QAction *)), this,
SLOT(recursiveAdd(QAction *)));
} else if (!m_popupotype.compare("item")) {
popup->connect(popup, SIGNAL(triggered(QAction *)), this,
SLOT(simpleAdd(QAction *)));
} else {
// ??
LOGDEB("CDBrowser::createPopup: obj type neither ct nor it: " <<
m_popupotype << endl);
return;
}
// Like BACK, RAND_STOP must be processed by both simple and
// recursiveAdd()
act = new QAction(tr("Stop random play"), this);
v = QVariant(int(PUP_RAND_STOP));
act->setData(v);
popup->addAction(act);
act->setEnabled(m_browsers->randPlayActive());
act = new QAction(tr("Sort order"), this);
v = QVariant(int(PUP_SORT_ORDER));
act->setData(v);
popup->addAction(act);
popup->popup(mapToGlobal(m_popupos));
}
void CDBrowser::back(QAction *)
{
if (m_curpath.size() >= 2)
curpathClicked(m_curpath.size() - 2);
}
bool CDBrowser::popupOther(QAction *act)
{
m_popupmode = act->data().toInt();
switch (m_popupmode) {
case PUP_BACK:
back(0);
break;
case PUP_RAND_STOP:
emit sig_rand_stop();
break;
case PUP_SORT_ORDER:
emit sig_sort_order();
break;
default:
return false;
}
return true;
}
// Add a single track or a section of the current container. This
// maybe triggered by a link click or a popup on an item entry
void CDBrowser::simpleAdd(QAction *act)
{
//qDebug() << "CDBrowser::simpleAdd";
if (popupOther(act)) {
// Not for us
return;
}
if (m_popupidx < 0 || m_popupidx > int(m_entries.size())) {
LOGERR("CDBrowser::simpleAdd: bad obj index: " << m_popupidx
<< " id count: " << m_entries.size() << endl);
return;
}
MetaDataList mdl;
unsigned int starti = 0;
switch (m_popupmode) {
case PUP_COPY_URL:
{
UPnPDirObject& e(m_entries[m_popupidx]);
if (e.m_resources.empty()) {
}
QString uri = QString::fromLocal8Bit(e.m_resources[0].m_uri.c_str());
QApplication::clipboard()->setText(uri, QClipboard::Selection);
QApplication::clipboard()->setText(uri, QClipboard::Clipboard);
return;
}
case PUP_ADD_FROMHERE:
starti = m_popupidx;
/* FALLTHROUGH */
case PUP_ADD_ALL:
for (unsigned int i = starti; i < m_entries.size(); i++) {
if (m_entries[i].m_type == UPnPDirObject::item &&
m_entries[i].m_iclass ==
UPnPDirObject::ITC_audioItem_musicTrack) {
mdl.resize(mdl.size()+1);
udirentToMetadata(&m_entries[i], &mdl[mdl.size()-1]);
}
}
break;
default:
mdl.resize(1);
udirentToMetadata(&m_entries[m_popupidx], &mdl[0]);
}
emit sig_tracks_to_playlist(mdl);
}
// Recursive add. This is triggered popup on a container
void CDBrowser::recursiveAdd(QAction *act)
{
//qDebug() << "CDBrowser::recursiveAdd";
deleteReaders();
if (popupOther(act)) {
// Not for us
return;
}
if (!m_cds) {
qDebug() << "CDBrowser::recursiveAdd: server not set" ;
return;
}
if (m_popupmode == PUP_OPEN_IN_NEW_TAB) {
vector<CtPathElt> npath(m_curpath);
npath.push_back(CtPathElt(m_popupobjid, m_popupobjtitle));
emit sig_browse_in_new_tab(u8s2qs(m_cds->srv()->getDeviceId()), npath);
return;
}
if (m_browsers)
m_browsers->setInsertActive(true);
ContentDirectory::ServiceKind kind = m_cds->srv()->getKind();
if (kind == ContentDirectory::CDSKIND_MINIM &&
m_popupmode != PUP_RAND_PLAY_GROUPS) {
// Use search() rather than a tree walk for Minim, it is much
// more efficient, except for rand play albums, where we want
// to preserve the paths (for discrimination)
UPnPDirContent dirbuf;
string ss("upnp:class = \"object.item.audioItem.musicTrack\"");
int err = m_cds->srv()->search(m_popupobjid, ss, dirbuf);
if (err != 0) {
LOGERR("CDBrowser::recursiveAdd: search failed, code " << err);
return;
}
m_recwalkentries = dirbuf.m_items;
// If we are inside the folders view tree, and the option is
// set, sort by url (minim sorts by tracknum tag even inside
// folder view)
if (CSettingsStorage::getInstance()->getSortKind() ==
CSettingsStorage::SK_MINIMFNORDER &&
m_popupobjid.compare(0, minimFoldersViewPrefix.size(),
minimFoldersViewPrefix) == 0) {
vector<string> crits;
crits.push_back("uri");
DirObjCmp cmpo(crits);
sort(m_recwalkentries.begin(), m_recwalkentries.end(), cmpo);
}
rreaperDone(0);
} else {
delete m_progressD;
m_progressD = new QProgressDialog("Exploring Directory ",
tr("Cancel"), 0, 0, this);
// can't get setMinimumDuration to work
m_progressD->setMinimumDuration(2000);
time(&m_progstart);
m_recwalkentries.clear();
m_recwalkdedup.clear();
m_reaper = new RecursiveReaper(m_cds->srv(), m_popupobjid, this);
connect(m_reaper,
SIGNAL(sliceAvailable(UPnPClient::UPnPDirContent *)),
this,
SLOT(onReaperSliceAvailable(UPnPClient::UPnPDirContent *)));
connect(m_reaper, SIGNAL(done(int)), this, SLOT(rreaperDone(int)));
m_reaper->start();
}
}
void CDBrowser::onReaperSliceAvailable(UPnPClient::UPnPDirContent *dc)
{
qDebug() << "CDBrowser::onReaperSliceAvailable: got " <<
dc->m_items.size() << " items";
if (!m_reaper) {
qDebug() << "CDBrowser::onReaperSliceAvailable: cancelled";
delete dc;
return;
}
for (unsigned int i = 0; i < dc->m_items.size(); i++) {
if (dc->m_items[i].m_resources.empty()) {
LOGDEB("CDBrowser::onReaperSlice: entry has no resources??"<< endl);
continue;
}
//LOGDEB1("CDBrowser::onReaperSlice: uri: " <<
// dc->m_items[i].m_resources[0].m_uri << endl);
string md5;
MD5String(dc->m_items[i].m_resources[0].m_uri, md5);
pair<std::unordered_set<std::string>::iterator, bool> res =
m_recwalkdedup.insert(md5);
if (res.second) {
m_recwalkentries.push_back(dc->m_items[i]);
} else {
LOGDEB("CDBrowser::onReaperSlice: dup found" << endl);
}
}
delete dc;
if (m_progressD) {
if (m_progressD->wasCanceled()) {
delete m_progressD;
m_progressD = 0;
m_reaper->setCancel();
} else {
if (time(0) - m_progstart >= 1) {
m_progressD->show();
}
m_progressD->setLabelText(
tr("Exploring Directory: %1 tracks").
arg(m_recwalkentries.size()));
}
}
}
void CDBrowser::rreaperDone(int status)
{
LOGDEB("CDBrowser::rreaperDone: status: " << status << ". Entries: " <<
m_recwalkentries.size() << endl);
deleteReaders();
delete m_progressD;
m_progressD = 0;
if (m_popupmode == PUP_RAND_PLAY_TRACKS ||
m_popupmode == PUP_RAND_PLAY_GROUPS) {
if (m_browsers)
m_browsers->setInsertActive(false);
// Sort list
if (m_popupmode == PUP_RAND_PLAY_GROUPS) {
vector<string> sortcrits;
sortcrits.push_back("upplay:ctpath");
sortcrits.push_back("upnp:album");
sortcrits.push_back("upnp:originalTrackNumber");
DirObjCmp cmpo(sortcrits);
sort(m_recwalkentries.begin(), m_recwalkentries.end(), cmpo);
}
RandPlayer::PlayMode mode = m_popupmode == PUP_RAND_PLAY_TRACKS ?
RandPlayer::PM_TRACKS : RandPlayer::PM_GROUPS;
LOGDEB("CDBrowser::rreaperDone: emitting sig_tracks_to_randplay, mode "
<< mode << endl);
emit sig_tracks_to_randplay(mode, m_recwalkentries);
} else {
int sortkind = CSettingsStorage::getInstance()->getSortKind();
if (sortkind == CSettingsStorage::SK_CUSTOM) {
QStringList qcrits =
CSettingsStorage::getInstance()->getSortCrits();
vector<string> sortcrits;
for (int i = 0; i < qcrits.size(); i++) {
sortcrits.push_back(qs2utf8s(qcrits[i]));
}
DirObjCmp cmpo(sortcrits);
sort(m_recwalkentries.begin(), m_recwalkentries.end(), cmpo);
}
MetaDataList mdl;
mdl.resize(m_recwalkentries.size());
for (unsigned int i = 0; i < m_recwalkentries.size(); i++) {
udirentToMetadata(&m_recwalkentries[i], &mdl[i]);
}
emit sig_tracks_to_playlist(mdl);
}
}