1 /*
2  * Cantata
3  *
4  * Copyright (c) 2011-2020 Craig Drummond <craig.p.drummond@gmail.com>
5  *
6  * ----
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; see the file COPYING.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 #include "onlineservice.h"
25 #include "mpd-interface/song.h"
26 #include <QUrl>
27 #include <QJsonDocument>
28 #include <QJsonObject>
29 #include <QMap>
30 
31 static const QString constSongDetails=QLatin1String("SongDetails");
32 
add(QJsonObject & json,const QString & key,const QString & val)33 static inline void add(QJsonObject &json, const QString &key, const QString &val)
34 {
35     if (!val.isEmpty()) {
36         json.insert(key, val);
37     }
38 }
39 
add(QJsonObject & json,const QString & key,int val)40 static inline void add(QJsonObject &json, const QString &key, int val)
41 {
42     if (0!=val) {
43         json.insert(key, val);
44     }
45 }
46 
encode(Song & song)47 Song & OnlineService::encode(Song &song)
48 {
49     QJsonObject json;
50     add(json, "artist", song.artist);
51     add(json, "albumartist", song.albumartist);
52     add(json, "album", song.album);
53     add(json, "title", song.title);
54     add(json, "genre", song.genres[0]);
55     add(json, "time", song.time);
56     add(json, "year", song.year);
57     add(json, "track", song.track);
58     add(json, "disc", song.disc);
59     add(json, "service", song.onlineService());
60     add(json, "imgcache", song.extraField(Song::OnlineImageCacheName));
61     add(json, "imgurl", song.extraField(Song::OnlineImageUrl));
62     song.file=Utils::addHashParam(QUrl(song.file).toEncoded(), constSongDetails, QJsonDocument(json).toJson(QJsonDocument::Compact));
63     return song;
64 }
65 
decode(Song & song)66 bool OnlineService::decode(Song &song)
67 {
68     if (!song.file.startsWith(QLatin1String("http://")) && !song.file.startsWith(QLatin1String("https://"))) {
69         return false;
70     }
71 
72     QMap<QString, QString> kv = Utils::hashParams(song.file);
73     QMap<QString, QString>::ConstIterator details = kv.constFind(constSongDetails);
74 
75     if (kv.constEnd()!=details) {
76         QJsonDocument doc = QJsonDocument::fromJson(details.value().toUtf8());
77         if (!doc.isNull()) {
78             QJsonObject obj = doc.object();
79             song.artist=obj["artist"].toString();
80             song.albumartist=obj["albumartist"].toString();
81             song.album=obj["album"].toString();
82             song.title=obj["title"].toString();
83             song.genres[0]=obj["genre"].toString();
84             if (obj.contains("time")) song.time=obj["time"].toInt();
85             if (obj.contains("year")) song.year=obj["year"].toInt();
86             if (obj.contains("track")) song.track=obj["track"].toInt();
87             if (obj.contains("disc")) song.disc=obj["disc"].toInt();
88             song.setIsFromOnlineService(obj["service"].toString());
89             if (obj.contains("imgcache")) song.setExtraField(Song::OnlineImageCacheName, obj["imgcache"].toString());
90             if (obj.contains("imgurl")) song.setExtraField(Song::OnlineImageUrl, obj["imgurl"].toString());
91             song.type=Song::OnlineSvrTrack;
92             song.file=Utils::removeHash(song.file);
93             return true;
94         }
95     }
96     return false;
97 }
98 
99 static QSet<QString> servicesWithCovers;
100 static QSet<QString> servicesWithCoversIfCached;
101 
iconPath(const QString & srv)102 QString OnlineService::iconPath(const QString &srv)
103 {
104     QString file=QString(CANTATA_SYS_ICONS_DIR+srv+".png");
105     if (!QFile::exists(file)) {
106         file=QString(CANTATA_SYS_ICONS_DIR+srv+".svg");
107     }
108     return file;
109 }
110 
isPodcasts(const QString & srv)111 bool OnlineService::isPodcasts(const QString &srv)
112 {
113     return QLatin1String("podcasts")==srv;
114 }
115 
showLogoAsCover(const Song & s)116 bool OnlineService::showLogoAsCover(const Song &s)
117 {
118     return s.isFromOnlineService() && !servicesWithCovers.contains(s.onlineService())
119            && (!servicesWithCoversIfCached.contains(s.onlineService()) || s.extraField(Song::OnlineImageCacheName).isEmpty());
120 }
121 
useCovers(const QString & name,bool onlyIfCache)122 void OnlineService::useCovers(const QString &name, bool onlyIfCache)
123 {
124     if (onlyIfCache) {
125         servicesWithCoversIfCached.insert(name);
126     } else {
127         servicesWithCovers.insert(name);
128     }
129 }
130