forked from mixxxdj/mixxx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wfindonwebmenu.cpp
177 lines (156 loc) · 6.39 KB
/
wfindonwebmenu.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
#include "widget/wfindonwebmenu.h"
#include <QDesktopServices>
#include <QScreen>
#include <QUrl>
#include <QUrlQuery>
#include <QtDebug>
#include "track/track.h"
#include "util/math.h"
#include "util/parented_ptr.h"
#include "util/qt.h"
#include "util/widgethelper.h"
namespace {
QString composeActionText(const QString& prefix, const QString& trackProperty) {
return prefix + QStringLiteral(" | ") + trackProperty;
}
QString composeSearchQuery(const QString& trackAlbumOrTitle, const QString& artist) {
return trackAlbumOrTitle + QStringLiteral(" ") + artist;
}
QString composePrefixAction(WFindOnWebMenu::TrackSearchProperties trackSearchProperties) {
switch (trackSearchProperties) {
case WFindOnWebMenu::TrackSearchProperties::Title:
return QObject::tr("Title");
case WFindOnWebMenu::TrackSearchProperties::Artist:
return QObject::tr("Artist");
case WFindOnWebMenu::TrackSearchProperties::ArtistAndTitle:
return QObject::tr("Title + Artist");
case WFindOnWebMenu::TrackSearchProperties::ArtistAndAlbum:
return QObject::tr("Album + Artist");
default:
return QObject::tr("Album");
}
}
const QString kSearchUrlSoundCloudArtist = QStringLiteral("https://soundcloud.com/search/people?");
const QString kSearchUrlSoundCloudTitle = QStringLiteral("https://soundcloud.com/search/sounds?");
const QString kSearchUrlSoundCloudAlbum = QStringLiteral("https://soundcloud.com/search/albums?");
const QString kSearchUrlDiscogsGen = QStringLiteral("https://www.discogs.com/search/?");
const QString kSearchUrlLastFmArtist = QStringLiteral("https://www.last.fm/search/artists?");
const QString kSearchUrlLastFmTitle = QStringLiteral("https://www.last.fm/search/tracks?");
const QString kSearchUrlLastFmAlbum = QStringLiteral("https://www.last.fm/search/albums?");
const QString kSearchUrlDefault = QStringLiteral("https://soundcloud.com/search?");
QString getServiceUrl(WFindOnWebMenu::Service service,
WFindOnWebMenu::TrackSearchProperties trackSearchProperties) {
switch (service) {
case WFindOnWebMenu::Service::Discogs:
return kSearchUrlDiscogsGen;
case WFindOnWebMenu::Service::LastFm:
if (trackSearchProperties == WFindOnWebMenu::TrackSearchProperties::Title ||
trackSearchProperties == WFindOnWebMenu::TrackSearchProperties::ArtistAndTitle) {
return kSearchUrlLastFmTitle;
} else if (trackSearchProperties == WFindOnWebMenu::TrackSearchProperties::Artist) {
return kSearchUrlLastFmArtist;
} else {
return kSearchUrlLastFmAlbum;
}
case WFindOnWebMenu::Service::SoundCloud:
if (trackSearchProperties == WFindOnWebMenu::TrackSearchProperties::Title ||
trackSearchProperties == WFindOnWebMenu::TrackSearchProperties::ArtistAndTitle) {
return kSearchUrlSoundCloudTitle;
} else if (trackSearchProperties == WFindOnWebMenu::TrackSearchProperties::Artist) {
return kSearchUrlSoundCloudArtist;
} else {
return kSearchUrlSoundCloudAlbum;
}
default:
return kSearchUrlDefault;
}
}
} // namespace
WFindOnWebMenu::WFindOnWebMenu(QWidget* parent)
: QMenu(tr("Find on Web"), parent) {
}
void WFindOnWebMenu::addSubmenusForServices(const Track& track) {
WFindOnWebMenu::populateFromTrackProperties(
track,
QStringLiteral("SoundCloud"),
Service::SoundCloud);
WFindOnWebMenu::populateFromTrackProperties(
track, QStringLiteral("LastFm"), Service::LastFm);
WFindOnWebMenu::populateFromTrackProperties(
track, QStringLiteral("Discogs"), Service::Discogs);
}
void WFindOnWebMenu::addActions(Service service,
const QString& queryValue,
QMenu* pServiceMenu,
TrackSearchProperties trackSearchProperties) {
const QString prefixAction = composePrefixAction(trackSearchProperties);
pServiceMenu->addAction(
composeActionText(prefixAction, queryValue),
this,
[this, service, trackSearchProperties, queryValue]() {
openInBrowser(service, trackSearchProperties, queryValue);
});
}
void WFindOnWebMenu::openInBrowser(Service service,
TrackSearchProperties trackSearchProperties,
const QString& queryValue) {
const QString serviceUrl = getServiceUrl(service, trackSearchProperties);
QUrlQuery urlQuery;
urlQuery.addQueryItem("q", queryValue);
QUrl url(serviceUrl);
url.setQuery(urlQuery);
if (!QDesktopServices::openUrl(url)) {
qWarning() << "QDesktopServices::openUrl() failed for " << url;
DEBUG_ASSERT(false);
}
}
bool WFindOnWebMenu::hasEntriesForTrack(const Track& track) {
return !(track.getArtist().isEmpty() &&
track.getAlbum().isEmpty() &&
track.getTitle().isEmpty());
}
void WFindOnWebMenu::populateFromTrackProperties(
const Track& track,
const QString& serviceTitle,
Service service) {
const QString artist = track.getArtist();
const QString trackTitle = track.getTitle();
const QString album = track.getAlbum();
auto pServiceMenu = make_parented<QMenu>(this);
pServiceMenu->setTitle(serviceTitle);
addMenu(pServiceMenu);
addSeparator();
if (!artist.isEmpty()) {
addActions(service,
artist,
pServiceMenu,
WFindOnWebMenu::TrackSearchProperties::Artist);
}
if (!trackTitle.isEmpty()) {
if (!artist.isEmpty()) {
const QString artistWithTrackTitle = composeSearchQuery(trackTitle, artist);
addActions(service,
artistWithTrackTitle,
pServiceMenu,
WFindOnWebMenu::TrackSearchProperties::ArtistAndTitle);
}
addActions(service,
trackTitle,
pServiceMenu,
WFindOnWebMenu::TrackSearchProperties::Title);
}
if (!album.isEmpty()) {
if (!artist.isEmpty()) {
const QString artistWithAlbum = composeSearchQuery(album, artist);
addActions(service,
artistWithAlbum,
pServiceMenu,
WFindOnWebMenu::TrackSearchProperties::ArtistAndAlbum);
} else {
addActions(service,
album,
pServiceMenu,
WFindOnWebMenu::TrackSearchProperties::Album);
}
}
}