summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrzej Rybczak <electricityispower@gmail.com>2010-08-09 09:31:47 +0200
committerAndrzej Rybczak <electricityispower@gmail.com>2010-08-09 09:31:47 +0200
commitd3cb0ea6f6ab7be363baf0d395fb4aee91457a85 (patch)
treebbaee2049b9c95c67158227139777ee11420db73
parent8b109d07366829550e02d43df14e8552b75c2af9 (diff)
put CURL related functions into a separate file
-rw-r--r--src/Makefile.am17
-rw-r--r--src/curl_handle.cpp67
-rw-r--r--src/curl_handle.h47
-rw-r--r--src/helpers.cpp9
-rw-r--r--src/helpers.h4
-rw-r--r--src/info.cpp21
-rw-r--r--src/lyrics.cpp26
-rw-r--r--src/lyrics.h4
-rw-r--r--src/ncmpcpp.h2
9 files changed, 131 insertions, 66 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 31d52363..ad3bf3ad 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,17 +1,18 @@
bin_PROGRAMS = ncmpcpp
-ncmpcpp_SOURCES = browser.cpp charset.cpp clock.cpp conv.cpp display.cpp \
- error.cpp help.cpp helpers.cpp info.cpp lyrics.cpp media_library.cpp menu.cpp \
- mpdpp.cpp ncmpcpp.cpp outputs.cpp playlist.cpp playlist_editor.cpp scrollpad.cpp \
- search_engine.cpp sel_items_adder.cpp server_info.cpp settings.cpp song.cpp status.cpp \
- tag_editor.cpp tiny_tag_editor.cpp tolower.cpp visualizer.cpp window.cpp
+ncmpcpp_SOURCES = browser.cpp charset.cpp clock.cpp conv.cpp curl_handle.cpp \
+ display.cpp error.cpp help.cpp helpers.cpp info.cpp lyrics.cpp media_library.cpp \
+ menu.cpp mpdpp.cpp ncmpcpp.cpp outputs.cpp playlist.cpp playlist_editor.cpp \
+ scrollpad.cpp search_engine.cpp sel_items_adder.cpp server_info.cpp settings.cpp \
+ song.cpp status.cpp tag_editor.cpp tiny_tag_editor.cpp tolower.cpp \
+ visualizer.cpp window.cpp
# set the include path found by configure
INCLUDES= $(all_includes)
# the library search path.
ncmpcpp_LDFLAGS = $(all_libraries)
-noinst_HEADERS = browser.h charset.h clock.h conv.h display.h error.h global.h \
- help.h helpers.h home.h info.h lyrics.h media_library.h menu.h mpdpp.h \
- outputs.h playlist_editor.h screen.h scrollpad.h search_engine.h \
+noinst_HEADERS = browser.h charset.h clock.h conv.h curl_handle.h display.h \
+ error.h global.h help.h helpers.h home.h info.h lyrics.h media_library.h menu.h \
+ mpdpp.h outputs.h playlist_editor.h screen.h scrollpad.h search_engine.h \
sel_items_adder.h server_info.h settings.h song.h tag_editor.h tiny_tag_editor.h \
tolower.h visualizer.h window.h
diff --git a/src/curl_handle.cpp b/src/curl_handle.cpp
new file mode 100644
index 00000000..0b476645
--- /dev/null
+++ b/src/curl_handle.cpp
@@ -0,0 +1,67 @@
+/***************************************************************************
+ * 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 "curl_handle.h"
+
+#ifdef HAVE_CURL_CURL_H
+
+#include <cstdlib>
+
+namespace
+{
+ size_t write_data(char *buffer, size_t size, size_t nmemb, void *data)
+ {
+ size_t result = size*nmemb;
+ static_cast<std::string *>(data)->append(buffer, result);
+ return result;
+ }
+}
+
+CURLcode Curl::perform(const std::string &URL, std::string &data, unsigned timeout)
+{
+# ifdef HAVE_PTHREAD_H
+ static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
+ pthread_mutex_lock(&lock);
+# endif
+ CURLcode result;
+ CURL *c = curl_easy_init();
+ curl_easy_setopt(c, CURLOPT_URL, URL.c_str());
+ curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, write_data);
+ curl_easy_setopt(c, CURLOPT_WRITEDATA, &data);
+ curl_easy_setopt(c, CURLOPT_CONNECTTIMEOUT, timeout);
+ curl_easy_setopt(c, CURLOPT_NOSIGNAL, 1);
+ result = curl_easy_perform(c);
+ curl_easy_cleanup(c);
+# ifdef HAVE_PTHREAD_H
+ pthread_mutex_unlock(&lock);
+# endif
+ return result;
+}
+
+std::string Curl::escape(const std::string &s)
+{
+ char *cs = curl_easy_escape(0, s.c_str(), s.length());
+ std::string result(cs);
+ curl_free(cs);
+ return result;
+}
+
+#endif // HAVE_CURL_CURL_H
+
diff --git a/src/curl_handle.h b/src/curl_handle.h
new file mode 100644
index 00000000..96f938e4
--- /dev/null
+++ b/src/curl_handle.h
@@ -0,0 +1,47 @@
+/***************************************************************************
+ * 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. *
+ ***************************************************************************/
+
+#ifndef _CURL_HANDLE_H
+#define _CURL_HANDLE_H
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#ifdef HAVE_CURL_CURL_H
+
+#ifdef HAVE_PTHREAD_H
+# include <pthread.h>
+#endif // HAVE_PTHREAD_H
+
+#include <string>
+#include "curl/curl.h"
+
+namespace Curl
+{
+ CURLcode perform(const std::string &URL, std::string &data, unsigned timeout = 10);
+
+ std::string escape(const std::string &s);
+}
+
+#endif // HAVE_CURL_CURL_H
+
+#endif
+
diff --git a/src/helpers.cpp b/src/helpers.cpp
index 8c29e6f8..e295a84b 100644
--- a/src/helpers.cpp
+++ b/src/helpers.cpp
@@ -408,12 +408,3 @@ std::basic_string<my_char_t> Scroller(const std::basic_string<my_char_t> &str, s
return result;
}
-#ifdef HAVE_CURL_CURL_H
-size_t write_data(char *buffer, size_t size, size_t nmemb, void *data)
-{
- size_t result = size*nmemb;
- static_cast<std::string *>(data)->append(buffer, result);
- return result;
-}
-#endif // HAVE_CURL_CURL_H
-
diff --git a/src/helpers.h b/src/helpers.h
index fc9d20a9..bb320c5d 100644
--- a/src/helpers.h
+++ b/src/helpers.h
@@ -218,9 +218,5 @@ std::string GetLineValue(std::string &, char = '"', char = '"', bool = 0);
std::basic_string<my_char_t> Scroller(const std::basic_string<my_char_t> &str, size_t &pos, size_t width);
-#ifdef HAVE_CURL_CURL_H
-size_t write_data(char *, size_t, size_t, void *);
-#endif
-
#endif
diff --git a/src/info.cpp b/src/info.cpp
index 22d7bb9a..d47efa49 100644
--- a/src/info.cpp
+++ b/src/info.cpp
@@ -19,6 +19,7 @@
***************************************************************************/
#include "info.h"
+#include "curl_handle.h"
#ifdef HAVE_CURL_CURL_H
# include <fstream>
@@ -27,8 +28,6 @@
# else
# include <sys/stat.h>
# endif // WIN32
-# include "curl/curl.h"
-# include "helpers.h"
#endif
#include "browser.h"
@@ -236,26 +235,12 @@ void *Info::PrepareArtist(void *screen_void_ptr)
{
Info *screen = static_cast<Info *>(screen_void_ptr);
- char *c_artist = curl_easy_escape(0, screen->itsArtist.c_str(), screen->itsArtist.length());
std::string url = "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=";
- url += c_artist;
+ url += Curl::escape(screen->itsArtist);
url += "&api_key=d94e5b6e26469a2d1ffae8ef20131b79";
std::string result;
- CURLcode code;
-
- pthread_mutex_lock(&Global::CurlLock);
- 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(&Global::CurlLock);
-
- curl_free(c_artist);
+ CURLcode code = Curl::perform(url, result);
if (code != CURLE_OK)
{
diff --git a/src/lyrics.cpp b/src/lyrics.cpp
index eacc20f0..fd4c976c 100644
--- a/src/lyrics.cpp
+++ b/src/lyrics.cpp
@@ -31,6 +31,7 @@
#include "browser.h"
#include "charset.h"
+#include "curl_handle.h"
#include "global.h"
#include "helpers.h"
@@ -63,7 +64,6 @@ bool Lyrics::Ready = 0;
#ifdef HAVE_PTHREAD_H
pthread_t *Lyrics::Downloader = 0;
-pthread_mutex_t Global::CurlLock = PTHREAD_MUTEX_INITIALIZER;
#endif // HAVE_PTHREAD_H
#endif // HAVE_CURL_CURL_H
@@ -211,27 +211,11 @@ void *Lyrics::Get(void *screen_void_ptr)
std::string artist = locale_to_utf_cpy(screen->itsSong.GetArtist());
std::string title = locale_to_utf_cpy(screen->itsSong.GetTitle());
- char *c_artist = curl_easy_escape(0, artist.c_str(), artist.length());
- char *c_title = curl_easy_escape(0, title.c_str(), title.length());
-
std::string url = my_lyrics->url;
- Replace(url, "%artist%", c_artist);
- Replace(url, "%title%", c_title);
-
- CURLcode code;
- pthread_mutex_lock(&Global::CurlLock);
- CURL *lyrics = curl_easy_init();
- curl_easy_setopt(lyrics, CURLOPT_URL, url.c_str());
- curl_easy_setopt(lyrics, CURLOPT_WRITEFUNCTION, write_data);
- curl_easy_setopt(lyrics, CURLOPT_WRITEDATA, &result);
- curl_easy_setopt(lyrics, CURLOPT_CONNECTTIMEOUT, 10);
- curl_easy_setopt(lyrics, CURLOPT_NOSIGNAL, 1);
- code = curl_easy_perform(lyrics);
- curl_easy_cleanup(lyrics);
- pthread_mutex_unlock(&Global::CurlLock);
+ Replace(url, "%artist%", Curl::escape(artist).c_str());
+ Replace(url, "%title%", Curl::escape(title).c_str());
- curl_free(c_artist);
- curl_free(c_title);
+ CURLcode code = Curl::perform(url, result);
if (code != CURLE_OK)
{
@@ -372,7 +356,7 @@ const Lyrics::Plugin Lyrics::LyricsPlugin =
LyricsPlugin_NotFound
};*/
-bool Lyrics::Generic_NotFound(const std::string &s)
+bool Lyrics::Generic_NotFound(const std::string &)
{
// it should never fail as open_tag and close_tag
// are not present if lyrics are not found
diff --git a/src/lyrics.h b/src/lyrics.h
index 360540b5..30bf837f 100644
--- a/src/lyrics.h
+++ b/src/lyrics.h
@@ -25,10 +25,6 @@
#include "mpdpp.h"
#include "screen.h"
-#ifdef HAVE_CURL_CURL_H
-# include "curl/curl.h"
-#endif
-
class Lyrics : public Screen<Scrollpad>
{
struct Plugin
diff --git a/src/ncmpcpp.h b/src/ncmpcpp.h
index 12987546..82e66eb7 100644
--- a/src/ncmpcpp.h
+++ b/src/ncmpcpp.h
@@ -28,8 +28,6 @@
#ifdef HAVE_PTHREAD_H
# include <pthread.h>
#else
-# define pthread_mutex_lock(x);
-# define pthread_mutex_unlock(x);
# define pthread_exit(x) return (x)
#endif // HAVE_PTHREAD_H