configure: explicitly link with boost.system
[ncmpcpp/stream.git] / src / lastfm.cpp
blobf6ff500496d72303974633c2e2552fe2624da6f9
1 /***************************************************************************
2 * Copyright (C) 2008-2013 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "lastfm.h"
23 #ifdef HAVE_CURL_CURL_H
25 #ifdef WIN32
26 # include <io.h>
27 #else
28 # include <sys/stat.h>
29 #endif // WIN32
31 #include <cassert>
32 #include <cerrno>
33 #include <cstring>
34 #include <boost/locale/conversion.hpp>
35 #include <fstream>
36 #include <iostream>
38 #include "error.h"
39 #include "helpers.h"
40 #include "charset.h"
41 #include "global.h"
42 #include "statusbar.h"
43 #include "title.h"
44 #include "screen_switcher.h"
45 #include "utility/string.h"
47 using Global::MainHeight;
48 using Global::MainStartY;
50 Lastfm *myLastfm;
52 Lastfm::Lastfm()
53 : Screen(NC::Scrollpad(0, MainStartY, COLS, MainHeight, "", Config.main_color, NC::Border::None))
54 , isReadyToTake(0), isDownloadInProgress(0)
55 { }
57 void Lastfm::resize()
59 size_t x_offset, width;
60 getWindowResizeParams(x_offset, width);
61 w.resize(width, MainHeight);
62 w.moveTo(x_offset, MainStartY);
63 hasToBeResized = 0;
66 std::wstring Lastfm::title()
68 return itsTitle;
71 void Lastfm::update()
73 if (isReadyToTake)
74 Take();
77 void Lastfm::Take()
79 assert(isReadyToTake);
80 pthread_join(itsDownloader, 0);
81 w.flush();
82 w.refresh();
83 isDownloadInProgress = 0;
84 isReadyToTake = 0;
87 void Lastfm::switchTo()
89 using Global::myScreen;
90 if (myScreen != this)
92 SwitchTo::execute(this);
93 // get an old info if it waits
94 if (isReadyToTake)
95 Take();
96 Load();
97 drawHeader();
99 else
100 switchToPreviousScreen();
103 void Lastfm::Load()
105 if (isDownloadInProgress)
106 return;
108 assert(itsService.get());
109 assert(itsService->checkArgs(itsArgs));
111 SetTitleAndFolder();
113 w.clear();
114 w.reset();
116 std::string artist = itsArgs.find("artist")->second;
117 std::string file = boost::locale::to_lower(artist + ".txt");
118 removeInvalidCharsFromFilename(file, Config.generate_win32_compatible_filenames);
120 itsFilename = itsFolder + "/" + file;
122 mkdir(itsFolder.c_str()
123 # ifndef WIN32
124 , 0755
125 # endif // !WIN32
128 std::ifstream input(itsFilename.c_str());
129 if (input.is_open())
131 bool first = 1;
132 std::string line;
133 while (std::getline(input, line))
135 if (!first)
136 w << '\n';
137 w << Charset::utf8ToLocale(line);
138 first = 0;
140 input.close();
141 itsService->colorizeOutput(w);
143 else
145 w << "Fetching informations... ";
146 pthread_create(&itsDownloader, 0, DownloadWrapper, this);
147 isDownloadInProgress = 1;
149 w.flush();
152 void Lastfm::SetTitleAndFolder()
154 if (dynamic_cast<ArtistInfo *>(itsService.get()))
156 itsTitle = L"Artist info - ";
157 itsTitle += ToWString(itsArgs.find("artist")->second);
158 itsFolder = Config.ncmpcpp_directory + "artists";
162 void *Lastfm::DownloadWrapper(void *this_ptr)
164 static_cast<Lastfm *>(this_ptr)->Download();
165 pthread_exit(0);
168 void Lastfm::Download()
170 LastfmService::Result result = itsService->fetch(itsArgs);
172 if (result.first)
174 Save(result.second);
175 w.clear();
176 w << Charset::utf8ToLocale(result.second);
177 itsService->colorizeOutput(w);
179 else
180 w << NC::Color::Red << result.second << NC::Color::End;
182 isReadyToTake = 1;
185 void Lastfm::Save(const std::string &data)
187 std::ofstream output(itsFilename.c_str());
188 if (output.is_open())
190 output << data;
191 output.close();
193 else
194 std::cerr << "ncmpcpp: couldn't save file \"" << itsFilename << "\"\n";
197 void Lastfm::Refetch()
199 if (remove(itsFilename.c_str()) && errno != ENOENT)
201 const char msg[] = "Couldn't remove \"%ls\": %s";
202 Statusbar::msg(msg, wideShorten(ToWString(itsFilename), COLS-const_strlen(msg)-25).c_str(), strerror(errno));
203 return;
205 Load();
208 bool Lastfm::SetArtistInfoArgs(const std::string &artist, const std::string &lang)
210 if (isDownloading())
211 return false;
213 itsService.reset(new ArtistInfo);
214 itsArgs.clear();
215 itsArgs["artist"] = artist;
216 if (!lang.empty())
217 itsArgs["lang"] = lang;
219 return true;
222 #endif // HVAE_CURL_CURL_H