Add translation support
[gmpc-lyricwiki.git] / src / plugin.c
blob00c1ff95112e79a74df5273f479a35bc758ed3a3
1 /* gmpc-lyricwiki (GMPC plugin)
2 * Copyright (C) 2008-2009 Qball Cow <qball@sarine.nl>
3 * Project homepage: http://gmpcwiki.sarine.nl/
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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <stdio.h>
21 #include <config.h>
22 #include <glib.h>
23 #include <glib/gi18n-lib.h>
24 #include <string.h>
25 #include <gtk/gtk.h>
26 #include <libxml/parser.h>
27 #include <libxml/tree.h>
28 #include <libmpd/debug_printf.h>
29 #include <gmpc/gmpc_easy_download.h>
30 #include <gmpc/plugin.h>
32 /**
33 * Get/Set enabled
35 static int lyricwiki_get_enabled()
37 return cfg_get_single_value_as_int_with_default(config, "lyricwiki-plugin", "enable", TRUE);
39 static void lyricwiki_set_enabled(int enabled)
41 cfg_set_single_value_as_int(config, "lyricwiki-plugin", "enable", enabled);
44 /* Get priority */
45 static int lyricwiki_fetch_cover_priority(void){
46 return cfg_get_single_value_as_int_with_default(config, "lyricwiki-plugin", "priority", 80);
48 static void lyricwiki_fetch_cover_priority_set(int priority){
49 cfg_set_single_value_as_int(config, "lyricwiki-plugin", "priority", priority);
51 static int lyricwiki_fetch_get_lyric(mpd_Song *song,MetaDataType type, char **path)
53 int result = 0;
54 gchar *artist, *title;
55 int retv = META_DATA_UNAVAILABLE;
57 /* Check if enabled */
58 if(lyricwiki_get_enabled() == FALSE)
60 return META_DATA_UNAVAILABLE;
62 /* Can I fetch this type? */
63 if(type != META_SONG_TXT)
65 return META_DATA_UNAVAILABLE;
67 /* Check available fields */
68 if(song->artist == NULL || song->title == NULL)
70 return META_DATA_UNAVAILABLE;
72 /* escape, for html query */
73 artist = gmpc_easy_download_uri_escape(song->artist);
74 title = gmpc_easy_download_uri_escape(song->title);
76 if(artist && title)
78 gmpc_easy_download_struct dld = {NULL, 0, -1, NULL, NULL};
79 /* create the uri */
80 gchar *uri_path = g_strdup_printf("http://lyricwiki.org/api.php?artist=%s&song=%s&fmt=xml",
81 artist, title);
82 debug_printf(DEBUG_INFO, "Uri-path: '%s'\n", uri_path);
83 if(gmpc_easy_download(uri_path, &dld))
85 xmlDocPtr doc = xmlParseMemory(dld.data,dld.size);
86 if(doc)
88 xmlNodePtr root = xmlDocGetRootElement(doc);
89 xmlNodePtr cur = root->xmlChildrenNode;
90 for(;cur;cur = cur->next){
91 if(xmlStrEqual(cur->name, (xmlChar *)"lyrics")){
92 xmlChar *lyric = xmlNodeGetContent(cur);
93 if(lyric && strcmp(lyric,"Not found")!= 0)
95 /* now we want to store it somewhere */
96 gchar *full_path = gmpc_get_metadata_filename(META_SONG_TXT, song, NULL);
97 /* don't need fancy writers/loaders, just dump the file. */
98 if(g_file_set_contents(full_path, lyric, -1, NULL))
100 *path = full_path;
101 retv = META_DATA_AVAILABLE;
103 else
104 g_free(full_path);
106 xmlFree(lyric);
110 xmlFreeDoc(doc);
114 gmpc_easy_download_clean(&dld);
115 g_free(uri_path);
118 if(artist) g_free(artist);
119 if(title) g_free(title);
120 return retv;
123 gmpcMetaDataPlugin lw_cover = {
124 .get_priority = lyricwiki_fetch_cover_priority,
125 .set_priority = lyricwiki_fetch_cover_priority_set,
126 .get_image = lyricwiki_fetch_get_lyric
129 int plugin_api_version = PLUGIN_API_VERSION;
131 void lw_init(void)
133 bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
134 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
136 static const gchar *lw_get_translation_domain(void)
138 return GETTEXT_PACKAGE;
140 gmpcPlugin plugin = {
141 .name = N_("LyricWiki.org lyric source"),
142 .version = {PLUGIN_MAJOR_VERSION,PLUGIN_MINOR_VERSION,PLUGIN_MICRO_VERSION},
143 .plugin_type = GMPC_PLUGIN_META_DATA,
144 .init = lw_init,
145 .metadata = &lw_cover,
146 .get_enabled = lyricwiki_get_enabled,
147 .set_enabled = lyricwiki_set_enabled,
148 .get_translation_domain = lw_get_translation_domain