Convert to using gmpc_get_metadata_filename
[gmpc-lyricwiki.git] / src / plugin.c
blob772d9dcbb47925673c25fc27f315895de395c500
1 #include <stdio.h>
2 #include <string.h>
3 #include <gtk/gtk.h>
4 #include <libxml/parser.h>
5 #include <libxml/tree.h>
6 #include <libmpd/debug_printf.h>
7 #include <gmpc/plugin.h>
8 #include <glib.h>
9 #include <gio/gio.h>
10 #include <config.h>
12 /**
13 * Get/Set enabled
15 static int lyricwiki_get_enabled()
17 return cfg_get_single_value_as_int_with_default(config, "lyricwiki-plugin", "enable", TRUE);
19 static void lyricwiki_set_enabled(int enabled)
21 cfg_set_single_value_as_int(config, "lyricwiki-plugin", "enable", enabled);
24 /* Get priority */
25 static int lyricwiki_fetch_cover_priority(void){
26 return cfg_get_single_value_as_int_with_default(config, "lyricwiki-plugin", "priority", 80);
29 static int lyricwiki_fetch_get_lyric(mpd_Song *song,MetaDataType type, char **path)
31 int result = 0;
32 /* Check if enabled */
33 if(lyricwiki_get_enabled() == FALSE)
35 return META_DATA_UNAVAILABLE;
37 /* Can I fetch this type? */
38 if(type != META_SONG_TXT)
40 return META_DATA_UNAVAILABLE;
42 /* Check available fields */
43 if(song->artist == NULL || song->title == NULL)
45 return META_DATA_UNAVAILABLE;
47 gchar *artist, *title;
48 int retv = META_DATA_UNAVAILABLE;
49 /* escape, for html query */
50 artist = g_uri_escape_string(song->artist, NULL, TRUE);
51 title = g_uri_escape_string(song->title, NULL, TRUE);
53 if(artist && title)
55 GFile *file = NULL;
56 GInputStream *stream = NULL;
57 /* create the uri */
58 gchar *uri_path = g_strdup_printf("http://lyricwiki.org/api.php?artist=%s&song=%s&fmt=xml",
59 artist, title);
60 printf("%s\n", uri_path);
61 file = g_file_new_for_uri(uri_path);
62 GError *error = NULL;
63 stream = (GInputStream *)g_file_read(file,NULL,&error);
64 if(stream)
66 char buffer[1024];
67 gssize size;
68 xmlParserCtxtPtr ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL);
70 printf("trying to read stream\n");
71 while((size = g_input_stream_read(stream, buffer, 1024, NULL, NULL)) > 0)
73 printf("read chunk of %i bytes\n", size);
74 xmlParseChunk(ctxt, buffer, (int)size, FALSE);
76 /* Parse last chunk */
77 if(xmlParseChunk(ctxt, "",0,TRUE)==0)
79 xmlDocPtr doc = ctxt->myDoc;
80 if(doc)
82 xmlNodePtr root = xmlDocGetRootElement(doc);
83 xmlNodePtr cur = root->xmlChildrenNode;
84 for(;cur;cur = cur->next){
85 if(xmlStrEqual(cur->name, (xmlChar *)"lyrics")){
86 xmlChar *lyric = xmlNodeGetContent(cur);
87 if(lyric && strcmp(lyric,"Not found")!= 0)
89 /* now we want to store it somewhere */
90 gchar *full_path = gmpc_get_metadata_filename(META_SONG_TXT, song, NULL);
91 /* don't need fancy writers/loaders, just dump the file. */
92 if(g_file_set_contents(full_path, lyric, -1, NULL))
94 *path = full_path;
95 retv = META_DATA_AVAILABLE;
97 else
98 g_free(full_path);
100 xmlFree(lyric);
104 xmlFreeDoc(doc);
108 g_input_stream_close(stream,NULL, NULL);
109 xmlFreeParserCtxt(ctxt);
111 else if (error)
113 printf("Failed to create stream: %s\n", error->message);
114 g_error_free(error);
115 error = NULL;
117 g_object_unref(file);
118 g_free(uri_path);
119 xmlCleanupParser();
121 else
122 printf("Failed to create stream\n");
124 if(artist) g_free(artist);
125 if(title) g_free(title);
126 return retv;
130 * Preferences
132 static void preferences_enable_cb_callback(GtkToggleButton *button, gpointer data)
134 lyricwiki_set_enabled(gtk_toggle_button_get_active(button));
136 static void preferences_construct(GtkWidget *container)
138 GtkWidget *vbox = gtk_vbox_new(FALSE, 6);
139 GtkWidget *checkbox = gtk_check_button_new_with_mnemonic("_Enable lyricwiki.org as lyric source.");
140 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox), lyricwiki_get_enabled());
142 g_signal_connect(G_OBJECT(checkbox), "toggled", G_CALLBACK(preferences_enable_cb_callback), NULL);
143 gtk_box_pack_start(GTK_BOX(vbox), checkbox, FALSE, FALSE,0);
145 gtk_widget_show_all(vbox);
146 gtk_container_add(GTK_CONTAINER(container), vbox);
150 * Generic de-construct function
152 static void preferences_destroy(GtkWidget *container)
154 GtkWidget *widget = gtk_bin_get_child(GTK_BIN(container));
155 if(widget)
157 gtk_container_remove(GTK_CONTAINER(container),widget);
161 gmpcMetaDataPlugin lw_cover = {
162 .get_priority = lyricwiki_fetch_cover_priority,
163 .get_image = lyricwiki_fetch_get_lyric
166 gmpcPrefPlugin lw_pref = {
167 .construct = preferences_construct,
168 .destroy = preferences_destroy
171 int plugin_api_version = PLUGIN_API_VERSION;
173 gmpcPlugin plugin = {
174 .name = "LyricWiki.org lyric source",
175 .version = {PLUGIN_MAJOR_VERSION,PLUGIN_MINOR_VERSION,PLUGIN_MICRO_VERSION},
176 .plugin_type = GMPC_PLUGIN_META_DATA,
177 .metadata = &lw_cover,
178 .get_enabled = lyricwiki_get_enabled,
179 .set_enabled = lyricwiki_set_enabled,
180 .pref = &lw_pref