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
|
/***************************************************************************
* Copyright (C) 2008-2009 by Andrzej Rybczak *
* electricityispower@gmail.com *
* *
* 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., *
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include "info.h"
#ifdef HAVE_CURL_CURL_H
# include <fstream>
# include <sys/stat.h>
# include <pthread.h>
# include "curl/curl.h"
# include "helpers.h"
#endif
#include "charset.h"
#include "global.h"
#include "media_library.h"
#include "playlist_editor.h"
#include "status_checker.h"
#include "tag_editor.h"
using namespace Global;
using std::string;
using std::vector;
Scrollpad *Global::sInfo;
const string artists_folder = home_folder + "/.ncmpcpp/artists";
namespace
{
# ifdef HAVE_CURL_CURL_H
pthread_t artist_info_downloader;
bool artist_info_ready = 0;
void *GetArtistInfo(void *);
# endif
void GetSongInfo(MPD::Song &, Scrollpad &);
const basic_buffer<my_char_t> &ShowTagInInfoScreen(const string &);
}
void Info::Init()
{
sInfo = new Scrollpad(0, main_start_y, COLS, main_height, "", Config.main_color, brNone);
sInfo->SetTimeout(ncmpcpp_window_timeout);
}
void Info::Resize()
{
sInfo->Resize(COLS, main_height);
}
void Info::GetSong()
{
if (wCurrent == sInfo)
{
wCurrent->Hide();
current_screen = prev_screen;
wCurrent = wPrev;
// redraw_screen = 1;
redraw_header = 1;
if (current_screen == csLibrary)
{
MediaLibrary::Refresh();
}
else if (current_screen == csPlaylistEditor)
{
PlaylistEditor::Refresh();
}
# ifdef HAVE_TAGLIB_H
else if (current_screen == csTagEditor)
{
TagEditor::Refresh();
}
# endif // HAVE_TAGLIB_H
}
else if (
(wCurrent == mPlaylist && !mPlaylist->Empty())
|| (wCurrent == mBrowser && mBrowser->Current().type == MPD::itSong)
|| (wCurrent == mSearcher && !mSearcher->Current().first)
|| (wCurrent == mLibSongs && !mLibSongs->Empty())
|| (wCurrent == mPlaylistEditor && !mPlaylistEditor->Empty())
# ifdef HAVE_TAGLIB_H
|| (wCurrent == mEditorTags && !mEditorTags->Empty())
# endif // HAVE_TAGLIB_H
)
{
MPD::Song *s = 0;
size_t id = ((Menu<MPD::Song> *)wCurrent)->Choice();
switch (current_screen)
{
case csPlaylist:
s = &mPlaylist->at(id);
break;
case csBrowser:
s = mBrowser->at(id).song;
break;
case csSearcher:
s = mSearcher->at(id).second;
break;
case csLibrary:
s = &mLibSongs->at(id);
break;
case csPlaylistEditor:
s = &mPlaylistEditor->at(id);
break;
# ifdef HAVE_TAGLIB_H
case csTagEditor:
s = &mEditorTags->at(id);
break;
# endif // HAVE_TAGLIB_H
default:
break;
}
wPrev = wCurrent;
wCurrent = sInfo;
prev_screen = current_screen;
current_screen = csInfo;
redraw_header = 1;
info_title = "Song info";
sInfo->Clear();
GetSongInfo(*s, *sInfo);
sInfo->Flush();
sInfo->Hide();
}
}
#ifdef HAVE_CURL_CURL_H
bool Info::Ready()
{
if (!artist_info_ready)
return false;
pthread_join(artist_info_downloader, NULL);
sInfo->Flush();
artist_info_downloader = 0;
artist_info_ready = 0;
return true;
}
void Info::GetArtist()
{
if (wCurrent == sInfo)
{
wCurrent->Hide();
current_screen = prev_screen;
wCurrent = wPrev;
// redraw_screen = 1;
redraw_header = 1;
if (current_screen == csLibrary)
{
MediaLibrary::Refresh();
}
else if (current_screen == csPlaylistEditor)
{
PlaylistEditor::Refresh();
}
# ifdef HAVE_TAGLIB_H
else if (current_screen == csTagEditor)
{
TagEditor::Refresh();
}
# endif // HAVE_TAGLIB_H
}
else if (
(wCurrent == mPlaylist && !mPlaylist->Empty())
|| (wCurrent == mBrowser && mBrowser->Current().type == MPD::itSong)
|| (wCurrent == mSearcher && !mSearcher->Current().first)
|| (wCurrent == mLibArtists && !mLibArtists->Empty())
|| (wCurrent == mLibSongs && !mLibSongs->Empty())
|| (wCurrent == mPlaylistEditor && !mPlaylistEditor->Empty())
# ifdef HAVE_TAGLIB_H
|| (wCurrent == mEditorTags && !mEditorTags->Empty())
# endif // HAVE_TAGLIB_H
)
{
if (artist_info_downloader)
{
ShowMessage("Artist's info is being downloaded...");
return;
}
string *artist = new string();
int id = ((Menu<MPD::Song> *)wCurrent)->Choice();
switch (current_screen)
{
case csPlaylist:
*artist = mPlaylist->at(id).GetArtist();
break;
case csBrowser:
*artist = mBrowser->at(id).song->GetArtist();
break;
case csSearcher:
*artist = mSearcher->at(id).second->GetArtist();
break;
case csLibrary:
*artist = mLibArtists->at(id);
break;
case csPlaylistEditor:
*artist = mPlaylistEditor->at(id).GetArtist();
break;
# ifdef HAVE_TAGLIB_H
case csTagEditor:
*artist = mEditorTags->at(id).GetArtist();
break;
# endif // HAVE_TAGLIB_H
default:
break;
}
if (!artist->empty())
{
wPrev = wCurrent;
wCurrent = sInfo;
prev_screen = current_screen;
current_screen = csInfo;
redraw_header = 1;
info_title = "Artist's info - " + *artist;
sInfo->Clear();
sInfo->WriteXY(0, 0, 0, "Fetching artist's info...");
sInfo->Refresh();
if (!artist_info_downloader)
{
pthread_create(&artist_info_downloader, NULL, GetArtistInfo, artist);
}
}
else
delete artist;
}
}
#endif // HVAE_CURL_CURL_H
namespace {
#ifdef HAVE_CURL_CURL_H
void *GetArtistInfo(void *ptr)
{
string *strptr = static_cast<string *>(ptr);
string artist = *strptr;
delete strptr;
locale_to_utf(artist);
string filename = artist + ".txt";
ToLower(filename);
EscapeUnallowedChars(filename);
const string fullpath = artists_folder + "/" + filename;
mkdir(artists_folder.c_str(), 0755);
string result;
std::ifstream input(fullpath.c_str());
if (input.is_open())
{
bool first = 1;
string line;
while (getline(input, line))
{
if (!first)
*sInfo << "\n";
utf_to_locale(line);
*sInfo << line;
first = 0;
}
input.close();
sInfo->SetFormatting(fmtBold, "\n\nSimilar artists:\n", fmtBoldEnd, 0);
sInfo->SetFormatting(Config.color2, "\n * ", clEnd);
artist_info_ready = 1;
pthread_exit(NULL);
}
CURLcode code;
char *c_artist = curl_easy_escape(0, artist.c_str(), artist.length());
string url = "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=";
url += c_artist;
url += "&api_key=d94e5b6e26469a2d1ffae8ef20131b79";
pthread_mutex_lock(&curl);
CURL *info = curl_easy_init();
curl_easy_setopt(info, CURLOPT_URL, url.c_str());
curl_easy_setopt(info, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(info, CURLOPT_WRITEDATA, &result);
curl_easy_setopt(info, CURLOPT_CONNECTTIMEOUT, 10);
curl_easy_setopt(info, CURLOPT_NOSIGNAL, 1);
code = curl_easy_perform(info);
curl_easy_cleanup(info);
pthread_mutex_unlock(&curl);
curl_free(c_artist);
if (code != CURLE_OK)
{
*sInfo << "Error while fetching artist's info: " << curl_easy_strerror(code);
artist_info_ready = 1;
pthread_exit(NULL);
}
size_t a, b;
bool save = 1;
a = result.find("status=\"failed\"");
if (a != string::npos)
{
EscapeHtml(result);
*sInfo << "Last.fm returned an error message: " << result;
artist_info_ready = 1;
pthread_exit(NULL);
}
vector<string> similar;
for (size_t i = result.find("<name>"); i != string::npos; i = result.find("<name>"))
{
result[i] = '.';
size_t j = result.find("</name>");
result[j] = '.';
i += 6;
similar.push_back(result.substr(i, j-i));
EscapeHtml(similar.back());
}
vector<string> urls;
for (size_t i = result.find("<url>"); i != string::npos; i = result.find("<url>"))
{
result[i] = '.';
size_t j = result.find("</url>");
result[j] = '.';
i += 5;
urls.push_back(result.substr(i, j-i));
}
a = result.find("<content>")+9;
b = result.find("</content>");
if (a == b)
{
result = "No description available for this artist.";
save = 0;
}
else
{
a += 9; // for <![CDATA[
b -= 3; // for ]]>
result = result.substr(a, b-a);
}
EscapeHtml(result);
Trim(result);
Buffer filebuffer;
if (save)
filebuffer << result;
utf_to_locale(result);
*sInfo << result;
if (save)
filebuffer << "\n\nSimilar artists:\n";
*sInfo << fmtBold << "\n\nSimilar artists:\n" << fmtBoldEnd;
for (size_t i = 1; i < similar.size(); i++)
{
if (save)
filebuffer << "\n * " << similar[i] << " (" << urls[i] << ")";
utf_to_locale(similar[i]);
utf_to_locale(urls[i]);
*sInfo << "\n" << Config.color2 << " * " << clEnd << similar[i] << " (" << urls[i] << ")";
}
if (save)
filebuffer << "\n\n" << urls.front();
utf_to_locale(urls.front());
*sInfo << "\n\n" << urls.front();
if (save)
{
std::ofstream output(fullpath.c_str());
if (output.is_open())
{
output << filebuffer.Str();
output.close();
}
}
artist_info_ready = 1;
pthread_exit(NULL);
}
#endif // HVAE_CURL_CURL_H
void GetSongInfo(MPD::Song &s, Scrollpad &info)
{
# ifdef HAVE_TAGLIB_H
string path_to_file;
if (s.IsFromDB())
path_to_file += Config.mpd_music_dir;
path_to_file += s.GetFile();
TagLib::FileRef f(path_to_file.c_str());
if (!f.isNull())
s.SetComment(f.tag()->comment().to8Bit(1));
# endif // HAVE_TAGLIB_H
info << fmtBold << Config.color1 << "Filename: " << fmtBoldEnd << Config.color2 << s.GetName() << "\n" << clEnd;
info << fmtBold << "Directory: " << fmtBoldEnd << Config.color2 << ShowTagInInfoScreen(s.GetDirectory()) << "\n\n" << clEnd;
info << fmtBold << "Length: " << fmtBoldEnd << Config.color2 << s.GetLength() << "\n" << clEnd;
# ifdef HAVE_TAGLIB_H
if (!f.isNull())
{
info << fmtBold << "Bitrate: " << fmtBoldEnd << Config.color2 << f.audioProperties()->bitrate() << " kbps\n" << clEnd;
info << fmtBold << "Sample rate: " << fmtBoldEnd << Config.color2 << f.audioProperties()->sampleRate() << " Hz\n" << clEnd;
info << fmtBold << "Channels: " << fmtBoldEnd << Config.color2 << (f.audioProperties()->channels() == 1 ? "Mono" : "Stereo") << "\n" << clDefault;
}
else
info << clDefault;
# endif // HAVE_TAGLIB_H
info << fmtBold << "\nTitle: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetTitle());
info << fmtBold << "\nArtist: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetArtist());
info << fmtBold << "\nAlbum: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetAlbum());
info << fmtBold << "\nYear: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetYear());
info << fmtBold << "\nTrack: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetTrack());
info << fmtBold << "\nGenre: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetGenre());
info << fmtBold << "\nComposer: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetComposer());
info << fmtBold << "\nPerformer: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetPerformer());
info << fmtBold << "\nDisc: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetDisc());
info << fmtBold << "\nComment: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetComment());
}
const basic_buffer<my_char_t> &ShowTagInInfoScreen(const string &tag)
{
# ifdef _UTF8
static WBuffer result;
result.Clear();
if (tag.empty())
result << Config.empty_tags_color << ToWString(Config.empty_tag) << clEnd;
else
result << ToWString(tag);
return result;
# else
return ShowTag(tag);
# endif
}
}
|