4 #include <libxml/parser.h>
5 #include <libxml/tree.h>
6 #include <libmpd/debug_printf.h>
7 #include <gmpc/plugin.h>
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
);
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
)
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
);
56 GInputStream
*stream
= NULL
;
58 gchar
*uri_path
= g_strdup_printf("http://lyricwiki.org/api.php?artist=%s&song=%s&fmt=xml",
60 printf("%s\n", uri_path
);
61 file
= g_file_new_for_uri(uri_path
);
63 stream
= (GInputStream
*)g_file_read(file
,NULL
,&error
);
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
;
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
))
95 retv
= META_DATA_AVAILABLE
;
108 g_input_stream_close(stream
,NULL
, NULL
);
109 xmlFreeParserCtxt(ctxt
);
113 printf("Failed to create stream: %s\n", error
->message
);
117 g_object_unref(file
);
122 printf("Failed to create stream\n");
124 if(artist
) g_free(artist
);
125 if(title
) g_free(title
);
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
));
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
,