/
ClementineRemote.cpp
1071 lines (955 loc) · 34.9 KB
/
ClementineRemote.cpp
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
//========================================================================
//
// Copyright (C) 2020 Matthieu Bruel <Matthieu.Bruel@gmail.com>
// Code reused from clementine-player project: https://github.com/clementine-player/Clementine
//
// This file is a part of ClementineRemote : https://github.com/mbruel/ClementineRemote
//
// ClementineRemote is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; version 3.0 of the License.
//
// 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 Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
// USA.
//
//========================================================================
#include "ClementineRemote.h"
#include "ConnectionWorker.h"
#include "model/RemoteSongModel.h"
#include "model/PlaylistModel.h"
#include "model/LibraryModel.h"
#include "player/RemotePlaylist.h"
#include <QTcpSocket>
#include <QDataStream>
#include <QStandardPaths>
#include <QDir>
#include <QUrl>
#include <QSqlQuery>
#if defined(Q_OS_ANDROID)
#include <QtAndroid>
#endif
const QString ClementineRemote::sAppName = "ClemRemote";
const QString ClementineRemote::sVersion = "0.3";
const QString ClementineRemote::sAppTitle = tr("Clementine Remote");
const QString ClementineRemote::sProjectUrl = "https://github.com/mbruel/ClementineRemote";
const QString ClementineRemote::sBTCaddress = "3BGbnvnnBCCqrGuq1ytRqUMciAyMXjXAv6";
const QString ClementineRemote::sDonateUrl = "https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=W2C236U6JNTUA&item_name=ClementineRemote¤cy_code=EUR";
const QPair<ushort, ushort> ClementineRemote::sClemFilesSupportMinVersion = {1, 4};
const QString ClementineRemote::sLibrarySQL =
"select artist, album, title, track, filename from songs order by artist, album, track, title";
const QMap<pb::remote::RepeatMode, ushort> ClementineRemote::sQmlRepeatCodes = {
{pb::remote::RepeatMode::Repeat_Off, 0},
{pb::remote::RepeatMode::Repeat_Track, 1},
{pb::remote::RepeatMode::Repeat_Album, 2},
{pb::remote::RepeatMode::Repeat_Playlist, 3},
};
const QMap<pb::remote::ShuffleMode, ushort> ClementineRemote::sQmlShuffleCodes = {
{pb::remote::ShuffleMode::Shuffle_Off, 0},
{pb::remote::ShuffleMode::Shuffle_All, 1},
{pb::remote::ShuffleMode::Shuffle_InsideAlbum, 2},
{pb::remote::ShuffleMode::Shuffle_Albums, 3}
};
ClementineRemote::ClementineRemote(QObject *parent):
QObject(parent), Singleton<ClementineRemote>(),
#ifdef __USE_CONNECTION_THREAD__
_thread(),
#endif
_connection(new ConnectionWorker(this)),
#if defined( Q_OS_WIN )
_settings("clemRemote.ini", QSettings::Format::IniFormat),
#else
_settings(QSettings::UserScope),
#endif
_clemVersion(), _clemState(pb::remote::Idle), _previousClemState(pb::remote::Idle),
_musicExtensions(), _volume(0),
_downloadsAllowed(true), _downloadPath(),
_shuffleMode(pb::remote::Shuffle_Off), _repeatMode(pb::remote::Repeat_Off),
_playlistsOpened(), _playlistsClosed(),
#ifdef __USE_CONNECTION_THREAD__
_securePlaylists(), _playlistData(),
#endif
_dispPlaylist(nullptr), _dispPlaylistId(0), _dispPlaylistIndex(0),
_plOpenedModel(new PlaylistModel(this, false)), _plClosedModel(new PlaylistModel(this, true)),
_songs(), _activeSong(), _activeSongIndex(0),
#ifdef __USE_CONNECTION_THREAD__
_secureSongs(), _songsData(),
#endif
_songsModel(new RemoteSongModel),
_songsProxyModel(new RemoteSongProxyModel),
_songsToRemove(), _songsToDownload(),
_activePlaylistId(1), _trackPostition(0),
_initialized(false),
_clemFilesSupport(false),
_remoteFilesPath("./"), _remoteFilesPathPerHost(),
_remoteFiles(),
#ifdef __USE_CONNECTION_THREAD__
_secureRemoteFilesData(), _remoteFilesData(),
_secureFilesToAppend(),
#endif
_filesToAppend(),
_radioStreams(),
#ifdef __USE_CONNECTION_THREAD__
_secureRadioStreams(), _radioStreamsData(),
#endif
_isDownloading(0x0),
#if defined(Q_OS_ANDROID)
_libraryPath(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)),
#elif defined(Q_OS_IOS)
_libraryPath(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)),
#else
_libraryPath(QFileInfo(_settings.fileName()).absolutePath()),
#endif
_libDB(), _libModel(new LibraryModel), _libProxyModel(new LibraryProxyModel)
{
setObjectName("ClemRemote");
_songsModel->setRemote(this);
_songsProxyModel->setSourceModel(_songsModel);
_libProxyModel->setSourceModel(_libModel);
#ifdef __USE_CONNECTION_THREAD__
connect(this, &ClementineRemote::playlistsOpenedUpdatedByWorker,
this, &ClementineRemote::onPlaylistsOpenedUpdatedByWorker, Qt::QueuedConnection);
connect(this, &ClementineRemote::songsUpdatedByWorker,
this, &ClementineRemote::onSongsUpdatedByWorker, Qt::QueuedConnection);
connect(this, &ClementineRemote::remoteFilesUpdatedByWorker,
this, &ClementineRemote::onRemoteFilesUpdatedByWorker, Qt::QueuedConnection);
_connection->moveToThread(&_thread);
_thread.start();
_thread.setObjectName("ConnectionWorkerThread");
#endif
_settings.beginGroup("remotePath");
for (const QString &host : _settings.childKeys())
_remoteFilesPathPerHost[host] = _settings.value(host).toString();
_settings.endGroup();
_filesToAppend.set_type(pb::remote::APPEND_FILES);
_songsToRemove.set_type(pb::remote::REMOVE_SONGS);
_songsToDownload.set_type(pb::remote::DOWNLOAD_SONGS);
qDebug() << "Settings filename: " << _settings.fileName()
<< " => libraryPath: " << _libraryPath;
connect(this, &ClementineRemote::libraryDownloaded, this, &ClementineRemote::onLibraryDownloaded, Qt::QueuedConnection);
#ifdef Q_OS_IOS
if (!_settings.contains("verticalVolume"))
setVerticalVolumeSlider(true);
#endif
}
ClementineRemote::~ClementineRemote()
{
qDebug() << "[MB_TRACE] destruction ClementineRemote";
close();
}
void ClementineRemote::libraryItemActivated(const QModelIndex &proxyIndex, const QString &newPlaylistName)
{
//TODO: add a setting to add items in playlist on double click
if (!proxyIndex.isValid())
{
qCritical() << "Wrong library index...";
return;
}
int itemType = _libProxyModel->data(proxyIndex, LibraryModel::type).toInt();
if (itemType == LibraryModel::Album)
{
qDebug() << "Library album activated: " << _libProxyModel->data(proxyIndex, LibraryModel::name).toString();
int childCount = _libProxyModel->rowCount(proxyIndex);
if (childCount == 0)
{
qDebug() << "no tracks for the Library album: " << _libProxyModel->data(proxyIndex, LibraryModel::name).toString();
return;
}
QStringList urls;
for (int i = 0; i < childCount; ++i)
{
QModelIndex childIndex = _libProxyModel->index(i, 0, proxyIndex);
QString url = _libProxyModel->data(childIndex, LibraryModel::url).toString();
qDebug() << "Adding Library track from album: " << url;
urls << url;
}
emit insertUrls(_dispPlaylistId, urls, newPlaylistName);
}
else if (itemType == LibraryModel::Track)
{
QString url = _libProxyModel->data(proxyIndex, LibraryModel::url).toString();
qDebug() << "Library track activated: " << url;
emit insertUrls(_dispPlaylistId, {url}, newPlaylistName);
}
}
bool ClementineRemote::verticalVolumeSlider() const
{
return _settings.value("verticalVolume", false).toBool();
}
void ClementineRemote::setVerticalVolumeSlider(bool isVertical)
{
_settings.setValue("verticalVolume", isVertical);
}
bool ClementineRemote::isConnected() const { return _connection->isConnected(); }
void ClementineRemote::cancelDownload() const { _connection->cancelDownload(); }
const QString ClementineRemote::hostname() const
{
if (_connection)
return _connection->hostname();
else
return "nowhere..."; // should never happen
}
void ClementineRemote::close()
{
qDebug() << "[MB_TRACE] close ClementineRemote";
_settings.beginGroup("remotePath");
for (auto it = _remoteFilesPathPerHost.cbegin(), itEnd = _remoteFilesPathPerHost.cend(); it != itEnd ; ++it)
_settings.setValue(it.key(), it.value());
_settings.endGroup();
_settings.sync();
#ifdef __USE_CONNECTION_THREAD__
emit _connection->killSocket();
_thread.quit();
_thread.wait();
#endif
qDeleteAll(_playlistsOpened);
_playlistsOpened.clear();
if (_plOpenedModel)
{
delete _plOpenedModel;
_plOpenedModel = nullptr;
}
qDeleteAll(_playlistsClosed);
_playlistsClosed.clear();
if (_plClosedModel)
{
delete _plClosedModel;
_plClosedModel = nullptr;
}
if (_songsProxyModel)
{
delete _songsProxyModel;
_songsProxyModel = nullptr;
}
if (_songsModel)
{
delete _songsModel;
_songsModel = nullptr;
}
if (_connection)
{
delete _connection;
_connection = nullptr;
}
}
int ClementineRemote::sendSelectedFiles(const QString &newPlaylistName)
{
#ifdef __USE_CONNECTION_THREAD__
_secureFilesToAppend.lock();
#endif
_filesToAppend.clear_response_list_files();
QStringList selectedFiles;
for (const RemoteFile &f : _remoteFiles)
{
if (!f.isDir && f.selected)
selectedFiles << f.filename;
}
if (selectedFiles.isEmpty())
{
#ifdef __USE_CONNECTION_THREAD__
_secureFilesToAppend.unlock();
#endif
return 0;
}
else
{
pb::remote::RequestAppendFiles *files = _filesToAppend.mutable_request_append_files();
files->set_relative_path(_remoteFilesPath.toStdString());
if (!newPlaylistName.isEmpty())
files->set_new_playlist_name(newPlaylistName.toStdString());
else
files->set_playlist_id(_dispPlaylistId);
for (const QString &filename : selectedFiles)
*files->add_files() = filename.toStdString();
emit sendFilesToAppend();
return selectedFiles.size();
}
}
int ClementineRemote::downloadSelectedFiles()
{
#ifdef __USE_CONNECTION_THREAD__
_secureFilesToAppend.lock();
#endif
_songsToDownload.clear_request_download_songs();
QStringList selectedFiles;
for (const RemoteFile &f : _remoteFiles)
{
if (!f.isDir && f.selected)
selectedFiles << f.filename;
}
if (selectedFiles.isEmpty())
{
#ifdef __USE_CONNECTION_THREAD__
_secureFilesToAppend.unlock();
#endif
return 0;
}
else
{
pb::remote::RequestDownloadSongs *files = _songsToDownload.mutable_request_download_songs();
files->set_download_item(pb::remote::DownloadItem::Urls);
files->set_relative_path(_remoteFilesPath.toStdString());
for (const QString &filename : selectedFiles)
*files->add_urls() = filename.toStdString();
#ifdef __USE_CONNECTION_THREAD__
_secureFilesToAppend.unlock();
_secureSongs.lock(); // this one will be unlocked in ClementineRemote::doSendSongsToDownload
#endif
emit sendSongsToDownload(QFileInfo(_remoteFilesPath).fileName());
return selectedFiles.size();
}
}
bool ClementineRemote::allFilesSelected() const
{
for (const RemoteFile &f : _remoteFiles)
{
if (!f.isDir && !f.selected)
return false;
}
return true;
}
void ClementineRemote::doSendFilesToAppend()
{
_connection->sendDataToServer(_filesToAppend);
_filesToAppend.clear_request_append_files();
#ifdef __USE_CONNECTION_THREAD__
_secureFilesToAppend.unlock();
#endif
}
void ClementineRemote::setLibraryFilter(const QString &searchTxt)
{
qDebug() << "[MB_TRACE][ClementineRemote::setSongsFilter] searchTxt: " << searchTxt;
Qt::CaseSensitivity caseSensitivity = Qt::CaseInsensitive;
QRegExp regExp(searchTxt, caseSensitivity);
_libProxyModel->setFilterRegExp(regExp);
}
void ClementineRemote::setSongsFilter(const QString &searchTxt)
{
qDebug() << "[MB_TRACE][ClementineRemote::setSongsFilter] searchTxt: " << searchTxt;
Qt::CaseSensitivity caseSensitivity = Qt::CaseInsensitive;
QRegExp regExp(searchTxt, caseSensitivity);
_songsProxyModel->setFilterRegExp(regExp);
}
#include <QMutexLocker>
bool ClementineRemote::allSongsSelected()
{
#ifdef __USE_CONNECTION_THREAD__
QMutexLocker lock(&_secureSongs);
#endif
return _songsProxyModel->allSongsSelected();
}
void ClementineRemote::selectAllSongsFromProxyModel(bool selectAll)
{
#ifdef __USE_CONNECTION_THREAD__
QMutexLocker lock(&_secureSongs);
#endif
_songsProxyModel->selectAllSongs(selectAll);
}
void ClementineRemote::deleteSelectedSongs()
{
#ifdef __USE_CONNECTION_THREAD__
_secureSongs.lock();
#endif
_songsToRemove.clear_request_remove_songs();
QList<int> selectedSongsIdexes = _songsProxyModel->selectedSongsIdexes();
if (selectedSongsIdexes.isEmpty())
{
#ifdef __USE_CONNECTION_THREAD__
_secureSongs.unlock();
#endif
return ;
}
else
{
pb::remote::RequestRemoveSongs *req = _songsToRemove.mutable_request_remove_songs();
req->set_playlist_id(_dispPlaylistId);
for (int songIndex : selectedSongsIdexes)
req->add_songs(songIndex);
emit sendSongsToRemove();
}
}
void ClementineRemote::downloadSelectedSongs()
{
#ifdef __USE_CONNECTION_THREAD__
_secureSongs.lock();
#endif
_songsToDownload.clear_request_download_songs();
QList<int> selectedSongsIDs = _songsProxyModel->selectedSongsIDs();
if (selectedSongsIDs.isEmpty())
{
#ifdef __USE_CONNECTION_THREAD__
_secureSongs.unlock();
#endif
return ;
}
else
{
pb::remote::RequestDownloadSongs *req = _songsToDownload.mutable_request_download_songs();
req->set_playlist_id(_dispPlaylistId);
req->set_download_item(pb::remote::DownloadItem::APlaylist);
for (int songID : selectedSongsIDs)
req->add_songs_ids(songID);
emit sendSongsToDownload(QString());//playlistName());
}
}
void ClementineRemote::doSendSongsToRemove()
{
_connection->sendDataToServer(_songsToRemove);
_songsToRemove.clear_request_append_files();
#ifdef __USE_CONNECTION_THREAD__
_secureSongs.unlock();
#endif
}
void ClementineRemote::doSendSongsToDownload()
{
_connection->sendDataToServer(_songsToDownload);
_songsToDownload.clear_request_download_songs();
#ifdef __USE_CONNECTION_THREAD__
_secureSongs.unlock();
#endif
}
QString ClementineRemote::playlistName() const
{
return _dispPlaylist ? _dispPlaylist->name : QString("Playlist #%1").arg(_dispPlaylistId);
}
bool ClementineRemote::isCurrentPlaylistSaved() const
{
if (_dispPlaylist)
return _dispPlaylist->favorite;
else
return false;
}
void ClementineRemote::updateCurrentSongIdx(qint32 currentSongIndex)
{
int idx = 0;
for (const RemoteSong &s : _songs)
{
if (s.index == currentSongIndex)
{
_activeSongIndex = idx;
emit currentSongIdx(_songsProxyModel->mapFromSource(_songsModel->index(idx)).row());
break;
}
++idx;
}
}
void ClementineRemote::dumpCurrentPlaylist()
{
for (const RemoteSong &s : _songs)
qDebug() << " - " << s.str();
}
void ClementineRemote::rcvPlaylists(const pb::remote::ResponsePlaylists &playlists)
{
bool includeClosedPlaylists = playlists.has_include_closed() && playlists.include_closed();
if (_playlistsOpened.size())
{
emit _plOpenedModel->preClearPlaylists(_playlistsOpened.size() - 1);
qDeleteAll(_playlistsOpened);
_playlistsOpened.clear();
emit _plOpenedModel->postClearPlaylists();
}
if (includeClosedPlaylists && _playlistsClosed.size())
{
emit _plClosedModel->preClearPlaylists(_playlistsClosed.size() - 1);
qDeleteAll(_playlistsClosed);
_playlistsClosed.clear();
emit _plClosedModel->postClearPlaylists();
}
qint32 nbPlaylists = playlists.playlist_size();
int idxOpened = 0, idxClosed = 0;
if (nbPlaylists)
{
for (const auto& pb_playlist : playlists.playlist())
{
if(pb_playlist.closed())
{
emit _plClosedModel->preAddPlaylist(idxClosed);
_playlistsClosed << new RemotePlaylist(pb_playlist);
emit _plClosedModel->postAddPlaylist(idxClosed++);
}
else
{
emit _plOpenedModel->preAddPlaylist(idxOpened);
_playlistsOpened << new RemotePlaylist(pb_playlist);
emit _plOpenedModel->postAddPlaylist(idxOpened++);
}
}
}
updateCurrentPlaylist();
if (includeClosedPlaylists)
emit closedPlaylistsReceived(idxClosed);
// emit updatePlaylists();
qDebug() << "[MsgType::PLAYLISTS] Nb Playlists: " << idxOpened
<< " (closed ones: " << idxClosed << ")";
dumpPlaylists();
}
void ClementineRemote::updateCurrentPlaylist()
{
_dispPlaylist = nullptr;
_dispPlaylistIndex = 0;
int idx = 0;
for (RemotePlaylist *p : _playlistsOpened)
{
if (p->id == _dispPlaylistId)
{
_dispPlaylist = p;
_dispPlaylistIndex = idx;
qDebug() << "[ClementineRemote::updateCurrentPlaylist] currentPlaylist: #" << idx
<< " : " << p->name;
emit updatePlaylist(idx);
break;
}
++idx;
}
}
void ClementineRemote::updateActivePlaylist()
{
_activePlaylistId = _dispPlaylistId;
int row = 0;
for (RemotePlaylist *p : _playlistsOpened)
{
if (p->id == _activePlaylistId)
{
if (!p->playing)
{
p->playing = true;
QModelIndex index = _plOpenedModel->index(row);
emit _plOpenedModel->dataChanged(index, index, {PlaylistModel::iconSrc});
}
}
else if (p->playing)
{
p->playing = false;
QModelIndex index = _plOpenedModel->index(row);
emit _plOpenedModel->dataChanged(index, index, {PlaylistModel::iconSrc});
}
++row;
}
}
void ClementineRemote::dumpPlaylists()
{
for (RemotePlaylist *p : _playlistsOpened)
qDebug() << " - " << p->str();
}
void ClementineRemote::rcvListOfRemoteFiles(const pb::remote::ResponseListFiles &files)
{
if (files.has_error() && files.error() != pb::remote::ResponseListFiles::NONE)
qDebug() << "[MsgType::LIST_FILES] ERROR: " << files.error();
else
{
if (_remoteFiles.size())
{
emit preClearRemoteFiles(_remoteFiles.size() - 1);
_remoteFiles.clear();
emit postClearRemoteFiles();
}
qint32 nbRemoteFiles = files.files_size();
if (nbRemoteFiles)
{
emit preAddRemoteFiles(nbRemoteFiles -1 );
_remoteFiles.reserve(nbRemoteFiles);
for (const auto& pb_file : files.files())
_remoteFiles << RemoteFile(pb_file.filename(), pb_file.is_dir());
emit postAddRemoteFiles();
}
qDebug() << "[MsgType::LIST_FILES] Nb Files: " << _remoteFiles.size();
_remoteFilesPath = files.relative_path().c_str();
_remoteFilesPathPerHost[_connection->hostname()] = _remoteFilesPath;
emit updateRemoteFilesPath(_remoteFilesPath);
}
}
void ClementineRemote::rcvSavedRadios(const pb::remote::ResponseSavedRadios &radios)
{
if (_radioStreams.size())
{
emit preClearRadioStreams(_radioStreams.size() - 1);
_radioStreams.clear();
emit postClearRadioStreams();
}
qint32 nbStreams = radios.streams_size();
if (nbStreams)
{
emit preAddRadioStreams(nbStreams -1 );
_radioStreams.reserve(nbStreams);
for (const auto &radio: radios.streams())
_radioStreams << Stream(radio.name(), radio.url(), radio.url_logo());
emit postAddRadioStreams();
}
qDebug() << "[MsgType::REQUEST_SAVED_RADIOS] Nb Radio Streams: " << _radioStreams.size();
}
QString ClementineRemote::testDownloadPath()
{
downloadPath();
qDebug() << "Download path: " << _downloadPath;
QFileInfo fi(_downloadPath);
if (!fi.isDir())
return tr("the download folder '%1' is not a directory...").arg(_downloadPath);
else if (!fi.isWritable())
return tr("the download folder '%1' is not writable...").arg(_downloadPath);
else
return QString();
}
QString ClementineRemote::downloadPath()
{
qDebug() << "standard writable app data loc: " << QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
qDebug() << "standard writable app conf loc: " << QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
qDebug() << "standard writable data loc: " << QStandardPaths::writableLocation(QStandardPaths::DataLocation);
qDebug() << "standard writable apps loc: " << QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation);
qDebug() << "standard writable doc loc: " << QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
qDebug() << "standard writable music loc: " << QStandardPaths::writableLocation(QStandardPaths::MusicLocation);
qDebug() << "standard writable down loc: " << QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
if (_settings.contains("downloadPath"))
_downloadPath = _settings.value("downloadPath").toString();
else
{
#if defined(Q_OS_ANDROID)
_downloadPath = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
QtAndroid::PermissionResultMap res = QtAndroid::requestPermissionsSync({"android.permission.WRITE_EXTERNAL_STORAGE"});
if (res["android.permission.WRITE_EXTERNAL_STORAGE"] != QtAndroid::PermissionResult::Granted)
return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
#elif defined(Q_OS_IOS)
_downloadPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
#else
_downloadPath = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
#endif
qDebug() << "Download path: " << _downloadPath;
QFileInfo fi(_downloadPath);
if (!fi.isDir())
qCritical() << tr("the download folder is not a directory...");
else if (!fi.isWritable())
qCritical() << tr("the download folder is not writable...");
else {
#if defined(Q_OS_IOS)
QString downFolder = "Downloads"; // using UIFileSharingEnabled, a ClemRemote folder is already visible in Files
#else
QString downFolder = sAppName;
#endif
fi = QFileInfo(QString("%1/%2").arg(_downloadPath).arg(downFolder));
if (!fi.exists())
{
QDir dir(_downloadPath);
if (!dir.mkdir(downFolder))
qCritical() << tr("error creating default Download folder: %1").arg(fi.absoluteFilePath());
}
_downloadPath = fi.absoluteFilePath();
}
}
_settings.setValue("downloadPath", _downloadPath);
_settings.sync();
qDebug() << "Download path: " << _downloadPath;
return _downloadPath;
}
QUrl ClementineRemote::downloadPathURL() { return QUrl::fromLocalFile(downloadPath()); }
#ifdef __USE_CONNECTION_THREAD__
void ClementineRemote::onPlaylistsOpenedUpdatedByWorker()
{
rcvPlaylists(_playlistData.response_playlists());
_playlistData.clear_response_playlists();
_securePlaylists.unlock();
}
void ClementineRemote::onSongsUpdatedByWorker(bool initialized)
{
rcvPlaylistSongs(_songsData.response_playlist_songs());
_songsData.clear_response_playlist_songs();
_secureSongs.unlock();
if (!initialized)
updateActivePlaylist();
}
void ClementineRemote::onRemoteFilesUpdatedByWorker()
{
rcvListOfRemoteFiles(_remoteFilesData.response_list_files());
_remoteFilesData.clear_response_list_files();
_secureRemoteFilesData.unlock();
}
#endif
void ClementineRemote::onLibraryDownloaded()
{
QString host = _connection->hostname();
_libDB = QSqlDatabase::addDatabase("QSQLITE", host);
_libDB.setDatabaseName(QString("%1/%2.db").arg(_libraryPath).arg(host));
if(!_libDB.open()){
qCritical() << "Can't open sqlite DB... " << _libDB.databaseName();
return;
}
emit _libModel->beginReset();
_libModel->clear();
QElapsedTimer timeStart;
timeStart.start();
int nbArtists = 0, nbAlbums = 0, nbTracks = 0;
QSqlQuery query(_libDB);
if(!query.exec(sLibrarySQL))
qDebug() << "Can't Execute Query !";
else
{
QStandardItem *rootItem = _libModel->invisibleRootItem(),
*artistItem = nullptr, *albumItem = nullptr;
QString currentArtist, currentAlbum;
while(query.next())
{
QString artist = query.value(0).toString();
QString album = query.value(1).toString();
QString title = query.value(2).toString();
int track = query.value(3).toInt();
QString filename = query.value(4).toString();
++nbTracks;
if (!artistItem || artist != currentArtist)
{
artistItem = new QStandardItem();
artistItem->setData(artist.isEmpty() ? tr("unset artist") : artist,
LibraryModel::name);
artistItem->setData(LibraryModel::Artist, LibraryModel::type);
rootItem->appendRow(artistItem);
currentArtist = artist;
++nbArtists;
}
if (!albumItem || album != currentAlbum)
{
albumItem = new QStandardItem();
albumItem->setData(album.isEmpty() ? tr("unset album") : album,
LibraryModel::name);
albumItem->setData(LibraryModel::Album, LibraryModel::type);
artistItem->appendRow(albumItem);
currentAlbum = album;
++nbAlbums;
}
QStandardItem *trackItem = new QStandardItem();
if (track == -1)
trackItem->setData(title, LibraryModel::name);
else
trackItem->setData(QString("%1 - %2").arg(track, 2, 10, QChar('0')).arg(title),
LibraryModel::name);
trackItem->setData(filename.toLower().endsWith("m3u")?LibraryModel::Playlist:LibraryModel::Track,
LibraryModel::type);
trackItem->setData(filename, LibraryModel::url);
albumItem->appendRow(trackItem);
}
}
emit _libModel->endReset();
qint64 durationMS = timeStart.elapsed();
// QTime::fromMSecsSinceStartOfDay(static_cast<int>(duration)).toString("hh:mm:ss.zzz"));
qDebug() << "Library model loaded in: " << durationMS / 1000 << " sec";
qDebug() << "nb Artists: " << nbArtists << ", nb Albums: " << nbAlbums
<< ", nb Tracks: " << nbTracks;
}
void ClementineRemote::rcvPlaylistSongs(const pb::remote::ResponsePlaylistSongs &songs)
{
_dispPlaylistId = songs.requested_playlist().id();
qDebug() << "[MsgType::PLAYLIST_SONGS] playlist ID: " << _dispPlaylistId;
updateCurrentPlaylist();
if (_songs.size())
{
emit preClearSongs(_songs.size() - 1);
_songs.clear();
emit postSongRemoved();
}
qint32 nbSongs = songs.songs_size();
if (nbSongs > 0)
{
emit preAddSongs(songs.songs_size() -1 );
_songs.reserve(songs.songs_size());
qint32 idx = 0;
for (const auto& song : songs.songs())
{
_songs << song;
if (song.index() == _activeSong.index)
{
qDebug() << "[MsgType::PLAYLIST_SONGS] current song id: " << _activeSongIndex;
_activeSongIndex = idx;
}
++idx;
}
emit postSongAppended();
}
qDebug() << "[MsgType::PLAYLIST_SONGS] Nb Songs: " << _songs.size();
// dumpCurrentPlaylist();
}
void ClementineRemote::checkClementineVersion()
{
QRegularExpression regExp(sClemVersionRegExpStr);
QRegularExpressionMatch match = regExp.match(_clemVersion);
if (match.hasMatch())
{
ushort major = match.captured(1).toUShort();
ushort minor = match.captured(2).toUShort();
if (major > sClemFilesSupportMinVersion.first
|| (major == sClemFilesSupportMinVersion.first && minor >= sClemFilesSupportMinVersion.second))
_clemFilesSupport = true;
else
_clemFilesSupport = false;
}
else
qCritical() << "Couldn't parse Clementine version...";
}
void ClementineRemote::parseMessage(const QByteArray &data)
{
pb::remote::Message msg;
if (!msg.ParseFromArray(data.constData(), data.size())) {
qCritical() << "Couldn't parse data";
return;
}
pb::remote::MsgType msgType = msg.type();
switch (msgType) {
case pb::remote::KEEP_ALIVE:
qDebug() << "[MsgType::KEEP_ALIVE]";
break;
case pb::remote::DISCONNECT:
_connection->setDisconnectReason(disconnectReason(msg.response_disconnect().reason_disconnect()));
qDebug() << "[MsgType::DISCONNECT]" << _connection->disconnectReason();
break;
case pb::remote::INFO:
_clemVersion = msg.response_clementine_info().version().c_str();
_clemState = msg.response_clementine_info().state();
_musicExtensions.clear();
for (const auto& ext : msg.response_clementine_info().files_music_extensions())
_musicExtensions << QString(ext.c_str());
if (msg.response_clementine_info().has_allow_downloads())
_downloadsAllowed = msg.response_clementine_info().allow_downloads();
checkClementineVersion();
qDebug() << "[MsgType::INFO] version: " << _clemVersion
<< ", state: " << _clemState
<< ", support files: " << _clemFilesSupport
<< ", music extensions: " << _musicExtensions
<< ", Downloads Allowed: " << _downloadsAllowed;
break;
case pb::remote::MsgType::CURRENT_METAINFO:
_activeSong = RemoteSong(msg.response_current_metadata().song_metadata());
updateCurrentSongIdx(_activeSong.index);
emit currentSongLength(_activeSong.length, _activeSong.pretty_length);
qDebug() << "[MsgType::CURRENT_METAINFO] " << _activeSong.str();
break;
case pb::remote::SET_VOLUME:
_volume = msg.request_set_volume().volume();
qDebug() << "[MsgType::SET_VOLUME] " << _volume;
emit updateVolume(_volume);
break;
case pb::remote::UPDATE_TRACK_POSITION:
_trackPostition = msg.response_update_track_position().position();
emit currentTrackPosition(_trackPostition);
qDebug() << "[MsgType::UPDATE_TRACK_POSITION] " << _trackPostition;
break;
case pb::remote::PLAYLISTS:
#ifdef __USE_CONNECTION_THREAD__
_securePlaylists.lock();
_playlistData = std::move(msg);
emit playlistsOpenedUpdatedByWorker();
#else
rcvPlaylists(msg.response_playlists());
#endif
break;
case pb::remote::PLAYLIST_SONGS:
#ifdef __USE_CONNECTION_THREAD__
_secureSongs.lock();
_songsData = std::move(msg);
emit songsUpdatedByWorker(_initialized);
#else
rcvPlaylistSongs(msg.response_playlist_songs());
if (!_initialized)
updateActivePlaylist();
#endif
break;
case pb::remote::SHUFFLE:
_shuffleMode = msg.shuffle().shuffle_mode();
qDebug() << "[MsgType::SHUFFLE] " << _shuffleMode;
emit updateShuffle(sQmlShuffleCodes.value(_shuffleMode));
break;
case pb::remote::REPEAT:
_repeatMode = msg.repeat().repeat_mode();
qDebug() << "[MsgType::REPEAT] " << _repeatMode;
emit updateRepeat(sQmlRepeatCodes.value(_repeatMode));
break;
case pb::remote::FIRST_DATA_SENT_COMPLETE:
_initialized = true;
qDebug() << "[MsgType::FIRST_DATA_SENT_COMPLETE] fully Initialized \\o/";
emit connected();
if (_clemFilesSupport)
_connection->requestSavedRadios();
if (QFileInfo(QString("%1/%2.db").arg(_libraryPath).arg(_connection->hostname())).exists())
emit libraryDownloaded();
else
emit getLibrary();
break;
case pb::remote::PLAY:
qDebug() << "[MsgType::PLAY]";
_clemState = pb::remote::EngineState::Playing;
emit updateEngineState();
break;
case pb::remote::PAUSE:
qDebug() << "[MsgType::PAUSE]";
_clemState = pb::remote::EngineState::Paused;
emit updateEngineState();
break;
case pb::remote::STOP:
qDebug() << "[MsgType::STOP]";
_clemState = pb::remote::EngineState::Idle;
emit updateEngineState();
break;
case pb::remote::ACTIVE_PLAYLIST_CHANGED: