112
116
return !lastfm::ws::SessionKey.isEmpty();
115
RadioItem* LastFMService::CreateRootItem(RadioItem* parent) {
116
RadioItem* item = new RadioItem(this, RadioItem::Type_Service, kServiceName, parent);
117
item->icon = QIcon(":last.fm/as.png");
119
QStandardItem* LastFMService::CreateRootItem() {
120
QStandardItem* item = new QStandardItem(QIcon(":last.fm/as.png"), kServiceName);
121
item->setData(true, RadioModel::Role_CanLazyLoad);
121
void LastFMService::LazyPopulate(RadioItem *item) {
122
switch (item->type) {
123
case RadioItem::Type_Service:
125
void LastFMService::LazyPopulate(QStandardItem* parent) {
126
switch (parent->data(RadioModel::Role_Type).toInt()) {
127
case RadioModel::Type_Service:
124
128
// Normal radio types
125
CreateStationItem(Type_MyRecommendations, tr("My Recommendations"), ":last.fm/recommended_radio.png", item);
126
CreateStationItem(Type_MyRadio, tr("My Radio Station"), ":last.fm/personal_radio.png", item);
127
CreateStationItem(Type_MyMix, tr("My Mix Radio"), ":last.fm/loved_radio.png", item);
128
CreateStationItem(Type_MyNeighbourhood, tr("My Neighborhood"), ":last.fm/neighbour_radio.png", item);
129
CreateStationItem(parent,
130
tr("My Recommendations"),
131
":last.fm/recommended_radio.png",
132
"lastfm://user/USERNAME/recommended",
133
tr("My Last.fm Recommended Radio"));
134
CreateStationItem(parent,
135
tr("My Radio Station"),
136
":last.fm/personal_radio.png",
137
"lastfm://user/USERNAME/library",
138
tr("My Last.fm Library"));
139
CreateStationItem(parent,
141
":last.fm/loved_radio.png",
142
"lastfm://user/USERNAME/mix",
143
tr("My Last.fm Mix Radio"));
144
CreateStationItem(parent,
145
tr("My Neighborhood"),
146
":last.fm/neighbour_radio.png",
147
"lastfm://user/USERNAME/neighbours",
148
tr("My Last.fm Neighborhood"));
130
150
// Types that have children
131
artist_list_ = new RadioItem(this, Type_ArtistRadio, tr("Artist radio"), item);
132
artist_list_->icon = QIcon(":last.fm/icon_radio.png");
133
artist_list_->lazy_loaded = true;
135
tag_list_ = new RadioItem(this, Type_TagRadio, tr("Tag radio"), item);
136
tag_list_->icon = QIcon(":last.fm/icon_tag.png");
137
tag_list_->lazy_loaded = true;
139
custom_list_ = new RadioItem(this, Type_CustomRadio, tr("Custom radio"), item);
140
custom_list_->icon = QIcon(":last.fm/icon_radio.png");
141
custom_list_->lazy_loaded = true;
143
RestoreList("artists", Type_Artist, QIcon(":last.fm/icon_radio.png"), artist_list_);
144
RestoreList("tags", Type_Tag, QIcon(":last.fm/icon_tag.png"), tag_list_);
145
RestoreList("custom", Type_Custom, QIcon(":last.fm/icon_radio.png"), custom_list_);
147
friends_list_ = new RadioItem(this, Type_MyFriends, tr("Friends"), item);
148
friends_list_->icon = QIcon(":last.fm/my_friends.png");
150
neighbours_list_ = new RadioItem(this, Type_MyNeighbours, tr("Neighbors"), item);
151
neighbours_list_->icon = QIcon(":last.fm/my_neighbours.png");
151
artist_list_ = new QStandardItem(QIcon(":last.fm/icon_radio.png"), tr("Artist radio"));
152
artist_list_->setData(Type_Artists, RadioModel::Role_Type);
153
parent->appendRow(artist_list_);
155
tag_list_ = new QStandardItem(QIcon(":last.fm/icon_tag.png"), tr("Tag radio"));
156
tag_list_->setData(Type_Tags, RadioModel::Role_Type);
157
parent->appendRow(tag_list_);
159
custom_list_ = new QStandardItem(QIcon(":last.fm/icon_radio.png"), tr("Custom radio"));
160
custom_list_->setData(Type_Custom, RadioModel::Role_Type);
161
parent->appendRow(custom_list_);
163
RestoreList("artists", kUrlArtist, tr(kTitleArtist), QIcon(":last.fm/icon_radio.png"), artist_list_);
164
RestoreList("tags", kUrlTag, tr(kTitleTag), QIcon(":last.fm/icon_tag.png"), tag_list_);
165
RestoreList("custom", kUrlCustom, tr(kTitleCustom), QIcon(":last.fm/icon_radio.png"), custom_list_);
167
friends_list_ = new QStandardItem(QIcon(":last.fm/my_friends.png"), tr("Friends"));
168
friends_list_->setData(Type_Friends, RadioModel::Role_Type);
169
friends_list_->setData(true, RadioModel::Role_CanLazyLoad);
170
parent->appendRow(friends_list_);
172
neighbours_list_ = new QStandardItem(QIcon(":last.fm/my_neighbours.png"), tr("Neighbors"));
173
neighbours_list_->setData(Type_Neighbours, RadioModel::Role_Type);
174
neighbours_list_->setData(true, RadioModel::Role_CanLazyLoad);
175
parent->appendRow(neighbours_list_);
153
177
if (!IsAuthenticated())
158
182
add_custom_action_->setEnabled(true);
162
186
RefreshFriends();
165
case Type_MyNeighbours:
189
case Type_Neighbours:
166
190
RefreshNeighbours();
169
193
case Type_OtherUser:
170
CreateStationItem(Type_OtherUserRadio, item->key, ":last.fm/recommended_radio.png", item)
171
->display_text = tr("Last.fm Radio Station - %1").arg(item->key);
172
CreateStationItem(Type_OtherUserMix, item->key, ":last.fm/loved_radio.png", item)
173
->display_text = tr("Last.fm Mix Radio - %1").arg(item->key);
174
CreateStationItem(Type_OtherUserNeighbourhood, item->key, ":last.fm/neighbour_radio.png", item)
175
->display_text = tr("Last.fm Neighbor Radio - %1").arg(item->key);
194
CreateStationItem(parent,
195
tr("Last.fm Radio Station - %1").arg(parent->text()),
196
":last.fm/personal_radio.png",
197
"lastfm://user/" + parent->text() + "/library",
198
tr("Last.fm Library - %1").arg(parent->text()));
199
CreateStationItem(parent,
200
tr("Last.fm Mix Radio - %1").arg(parent->text()),
201
":last.fm/loved_radio.png",
202
"lastfm://user/" + parent->text() + "/mix",
203
tr("Last.fm Mix Radio - %1").arg(parent->text()));
204
CreateStationItem(parent,
205
tr("Last.fm Neighbor Radio - %1").arg(parent->text()),
206
":last.fm/neighbour_radio.png",
207
"lastfm://user/" + parent->text() + "/neighbours",
208
tr("Last.fm Neighbor Radio - %1").arg(parent->text()));
182
item->lazy_loaded = true;
185
RadioItem* LastFMService::CreateStationItem(ItemType type, const QString& name,
186
const QString& icon, RadioItem* parent) {
187
RadioItem* ret = new RadioItem(this, type, name, parent);
188
ret->lazy_loaded = true;
189
ret->icon = QIcon(icon);
190
ret->playable = true;
216
QStandardItem* LastFMService::CreateStationItem(
217
QStandardItem* parent, const QString& name, const QString& icon,
218
const QString& url, const QString& title) {
219
QStandardItem* ret = new QStandardItem(QIcon(icon), name);
220
ret->setData(url, RadioModel::Role_Url);
221
ret->setData(title, RadioModel::Role_Title);
222
ret->setData(RadioModel::PlayBehaviour_SingleItem, RadioModel::Role_PlayBehaviour);
223
parent->appendRow(ret);
248
280
emit AuthenticationComplete(true);
251
QUrl LastFMService::UrlForItem(const RadioItem* item) const {
252
switch (item->type) {
253
case Type_MyRecommendations:
254
return "lastfm://user/" + lastfm::ws::Username + "/recommended";
257
return QString("lastfm://rql/") + QString("adv:" + lastfm::ws::Username).toUtf8().toBase64();
259
case Type_MyNeighbourhood:
260
return "lastfm://user/" + lastfm::ws::Username + "/neighbours";
263
return "lastfm://user/" + lastfm::ws::Username + "/library";
266
case Type_OtherUserRadio:
267
return "lastfm://user/" + item->key + "/library";
269
case Type_OtherUserMix:
270
return QString("lastfm://rql/") + QString("adv:" + item->key).toUtf8().toBase64();
272
case Type_OtherUserNeighbourhood:
273
return "lastfm://user/" + item->key + "/neighbours";
276
return "lastfm://artist/" + item->key + "/similarartists";
279
return "lastfm://globaltags/" + item->key;
282
return QString("lastfm://rql/" + item->key.toUtf8().toBase64());
287
QString LastFMService::TitleForItem(const RadioItem* item) const {
288
const QString me(lastfm::ws::Username);
290
switch (item->type) {
291
case Type_MyRecommendations: return tr("Last.fm Recommended Radio - %1").arg(me);
292
case Type_MyMix: return tr("Last.fm Mix Radio - %1").arg(me);
293
case Type_MyNeighbourhood: return tr("Last.fm Neighbor Radio - %1").arg(me);
294
case Type_MyRadio: return tr("Last.fm Library - %1").arg(me);
296
case Type_OtherUserRadio: return tr("Last.fm Library - %1").arg(item->key);
297
case Type_OtherUserMix: return tr("Last.fm Mix Radio - %1").arg(item->key);
298
case Type_OtherUserNeighbourhood: return tr("Last.fm Neighbor Radio - %1").arg(item->key);
299
case Type_Artist: return tr("Last.fm Similar Artists to %1").arg(item->key);
300
case Type_Tag: return tr("Last.fm Tag Radio: %1").arg(item->key);
301
case Type_Custom: return tr("Last.fm Custom Radio: %1").arg(item->key);
283
QUrl LastFMService::FixupUrl(const QUrl& url) {
285
ret.setEncodedUrl(url.toEncoded().replace(
286
"USERNAME", QUrl::toPercentEncoding(lastfm::ws::Username)));
306
290
PlaylistItem::SpecialLoadResult LastFMService::StartLoading(const QUrl& url) {
554
544
foreach (const lastfm::User& n, neighbours) {
555
RadioItem* item = new RadioItem(this, Type_OtherUser, n);
556
item->icon = QIcon(":last.fm/user_purple.png");
557
item->playable = true;
558
item->InsertNotify(neighbours_list_);
545
QStandardItem* item = new QStandardItem(QIcon(":last.fm/user_purple.png"), n.name());
546
item->setData(QUrl("lastfm://user/" + n.name() + "/library"), RadioModel::Role_Url);
547
item->setData(tr("Last.fm Library - %1").arg(n.name()), RadioModel::Role_Title);
548
item->setData(true, RadioModel::Role_CanLazyLoad);
549
item->setData(Type_OtherUser, RadioModel::Role_Type);
550
item->setData(RadioModel::PlayBehaviour_SingleItem, RadioModel::Role_PlayBehaviour);
551
neighbours_list_->appendRow(item);
562
void LastFMService::AddToPlaylist() {
563
emit AddItemToPlaylist(context_item_, false);
566
void LastFMService::LoadToPlaylist() {
567
emit AddItemToPlaylist(context_item_, true);
555
QModelIndex LastFMService::GetCurrentIndex() {
556
return context_item_->index();
570
559
void LastFMService::AddArtistRadio() {
571
AddArtistOrTag("artists", LastFMStationDialog::Artist, Type_Artist, QIcon(":last.fm/icon_radio.png"), artist_list_);
560
AddArtistOrTag("artists", LastFMStationDialog::Artist,
561
kUrlArtist, tr(kTitleArtist),
562
":last.fm/icon_radio.png", artist_list_);
574
565
void LastFMService::AddTagRadio() {
575
AddArtistOrTag("tags", LastFMStationDialog::Tag, Type_Tag, QIcon(":last.fm/icon_tag.png"), tag_list_);
566
AddArtistOrTag("tags", LastFMStationDialog::Tag,
567
kUrlTag, tr(kTitleTag),
568
":last.fm/icon_tag.png", tag_list_);
578
571
void LastFMService::AddCustomRadio() {
579
AddArtistOrTag("custom", LastFMStationDialog::Custom, Type_Custom, QIcon(":last.fm/icon_radio.png"), custom_list_);
572
AddArtistOrTag("custom", LastFMStationDialog::Custom,
573
kUrlCustom, tr(kTitleCustom),
574
":last.fm/icon_radio.png", custom_list_);
582
577
void LastFMService::AddArtistOrTag(const QString& name,
583
LastFMStationDialog::Type dialog_type, ItemType item_type,
584
const QIcon& icon, RadioItem* list) {
578
LastFMStationDialog::Type dialog_type,
579
const QString& url_pattern,
580
const QString& title_pattern,
581
const QString& icon, QStandardItem* list) {
585
582
station_dialog_->SetType(dialog_type);
586
583
if (station_dialog_->exec() == QDialog::Rejected)
589
586
if (station_dialog_->content().isEmpty())
592
RadioItem* item = new RadioItem(this, item_type, station_dialog_->content());
594
item->playable = true;
595
item->lazy_loaded = true;
596
item->InsertNotify(list);
597
emit AddItemToPlaylist(item, false);
589
QString content = station_dialog_->content();
591
if (name == "custom")
592
url_content = content.toUtf8().toBase64();
594
url_content = content;
596
QStandardItem* item = new QStandardItem(QIcon(icon), content);
597
item->setData(url_pattern.arg(url_content), RadioModel::Role_Url);
598
item->setData(title_pattern.arg(content), RadioModel::Role_Title);
599
item->setData(RadioModel::PlayBehaviour_SingleItem, RadioModel::Role_PlayBehaviour);
600
list->appendRow(item);
601
emit AddItemToPlaylist(item->index(), AddMode_Append);
599
603
SaveList(name, list);
602
void LastFMService::SaveList(const QString& name, RadioItem* list) const {
606
void LastFMService::SaveList(const QString& name, QStandardItem* list) const {
603
607
QSettings settings;
604
608
settings.beginGroup(kSettingsGroup);
606
settings.beginWriteArray(name, list->children.count());
607
for (int i=0 ; i<list->children.count() ; ++i) {
610
settings.beginWriteArray(name, list->rowCount());
611
for (int i=0 ; i<list->rowCount() ; ++i) {
608
612
settings.setArrayIndex(i);
609
settings.setValue("key", list->children[i]->key);
613
settings.setValue("key", list->child(i)->text());
611
615
settings.endArray();
614
void LastFMService::RestoreList(const QString &name, ItemType item_type,
615
const QIcon& icon, RadioItem *list) {
618
void LastFMService::RestoreList(const QString& name,
619
const QString& url_pattern,
620
const QString& title_pattern,
621
const QIcon& icon, QStandardItem* parent) {
616
622
QSettings settings;
617
623
settings.beginGroup(kSettingsGroup);
625
if (parent->hasChildren())
626
parent->removeRows(0, parent->rowCount());
621
628
int count = settings.beginReadArray(name);
622
629
for (int i=0 ; i<count ; ++i) {
623
630
settings.setArrayIndex(i);
624
RadioItem* item = new RadioItem(this, item_type,
625
settings.value("key").toString(), list);
627
item->playable = true;
628
item->lazy_loaded = true;
631
QString content = settings.value("key").toString();
633
if (name == "custom")
634
url_content = content.toUtf8().toBase64();
636
url_content = content;
638
QStandardItem* item = new QStandardItem(icon, content);
639
item->setData(url_pattern.arg(url_content), RadioModel::Role_Url);
640
item->setData(title_pattern.arg(content), RadioModel::Role_Title);
641
item->setData(RadioModel::PlayBehaviour_SingleItem, RadioModel::Role_PlayBehaviour);
642
parent->appendRow(item);
630
644
settings.endArray();
633
647
void LastFMService::Remove() {
634
int type = context_item_->type;
636
context_item_->parent->DeleteNotify(context_item_->row);
638
if (type == Type_Artist)
648
int type = context_item_->parent()->data(RadioModel::Role_Type).toInt();
650
context_item_->parent()->removeRow(context_item_->row());
652
if (type == Type_Artists)
639
653
SaveList("artists", artist_list_);
640
else if (type == Type_Tag)
654
else if (type == Type_Tags)
641
655
SaveList("tags", tag_list_);
642
656
else if (type == Type_Custom)
643
657
SaveList("custom", custom_list_);