1 /***************************************************************************
2 * Copyright (C) 2008-2013 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
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. *
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. *
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 ***************************************************************************/
23 #ifdef HAVE_CURL_CURL_H
28 # include <sys/stat.h>
34 #include <boost/locale/conversion.hpp>
42 #include "statusbar.h"
44 #include "screen_switcher.h"
45 #include "utility/string.h"
47 using Global::MainHeight
;
48 using Global::MainStartY
;
53 : Screen(NC::Scrollpad(0, MainStartY
, COLS
, MainHeight
, "", Config
.main_color
, NC::Border::None
))
54 , isReadyToTake(0), isDownloadInProgress(0)
59 size_t x_offset
, width
;
60 getWindowResizeParams(x_offset
, width
);
61 w
.resize(width
, MainHeight
);
62 w
.moveTo(x_offset
, MainStartY
);
66 std::wstring
Lastfm::title()
79 assert(isReadyToTake
);
80 pthread_join(itsDownloader
, 0);
83 isDownloadInProgress
= 0;
87 void Lastfm::switchTo()
89 using Global::myScreen
;
92 SwitchTo::execute(this);
93 // get an old info if it waits
100 switchToPreviousScreen();
105 if (isDownloadInProgress
)
108 assert(itsService
.get());
109 assert(itsService
->checkArgs(itsArgs
));
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()
128 std::ifstream
input(itsFilename
.c_str());
133 while (std::getline(input
, line
))
137 w
<< Charset::utf8ToLocale(line
);
141 itsService
->colorizeOutput(w
);
145 w
<< "Fetching informations... ";
146 pthread_create(&itsDownloader
, 0, DownloadWrapper
, this);
147 isDownloadInProgress
= 1;
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();
168 void Lastfm::Download()
170 LastfmService::Result result
= itsService
->fetch(itsArgs
);
176 w
<< Charset::utf8ToLocale(result
.second
);
177 itsService
->colorizeOutput(w
);
180 w
<< NC::Color::Red
<< result
.second
<< NC::Color::End
;
185 void Lastfm::Save(const std::string
&data
)
187 std::ofstream
output(itsFilename
.c_str());
188 if (output
.is_open())
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
));
208 bool Lastfm::SetArtistInfoArgs(const std::string
&artist
, const std::string
&lang
)
213 itsService
.reset(new ArtistInfo
);
215 itsArgs
["artist"] = artist
;
217 itsArgs
["lang"] = lang
;
222 #endif // HVAE_CURL_CURL_H