From 8c012917f5530d947c1a31adfc6aba4b0cf3853c Mon Sep 17 00:00:00 2001 From: Viktor Semykin Date: Tue, 25 May 2010 03:37:44 +0300 Subject: Misc plugin actions --- plugins/lastfm/lastfm.c | 62 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 6 deletions(-) (limited to 'plugins/lastfm/lastfm.c') diff --git a/plugins/lastfm/lastfm.c b/plugins/lastfm/lastfm.c index 3ba8def8..02dff618 100644 --- a/plugins/lastfm/lastfm.c +++ b/plugins/lastfm/lastfm.c @@ -334,26 +334,31 @@ lfm_fetch_song_info (DB_playItem_t *song, const char **a, const char **t, const static int lfm_uri_encode (char *out, int outl, const char *str) { int l = outl; - static const char echars[] = " ;/?:@=#&+"; //trace ("lfm_uri_encode %p %d %s\n", out, outl, str); while (*str) { if (outl <= 1) { //trace ("no space left for 1 byte in buffer\n"); return -1; } - if (strchr (echars, *str)) { + + if (!( + (*str >= '0' && *str <= '9') || + (*str >= 'a' && *str <= 'z') || + (*str >= 'A' && *str <= 'Z') || + (*str == ' ') + )) + { if (outl <= 3) { //trace ("no space left for 3 bytes in the buffer\n"); return -1; } - //trace ("adding escaped value for %c\n", *str); - snprintf (out, outl, "%%%02x", (int)*str); + snprintf (out, outl, "%%%02x", (uint8_t)*str); outl -= 3; str++; out += 3; } else { - *out = *str; + *out = *str == ' ' ? '+' : *str; out++; str++; outl--; @@ -819,6 +824,50 @@ lastfm_stop (void) { return 0; } +static int +lfm_action_lookup (DB_playItem_t *it, void *data) +{ + const char *artist = deadbeef->pl_find_meta (it, "artist"); + const char *title = deadbeef->pl_find_meta (it, "title"); + + if (!title || !artist) + return 0; + + char eartist [strlen (artist) * 3 + 1]; + char etitle [strlen (title) * 3 + 1]; + + if (-1 == lfm_uri_encode (eartist, sizeof (eartist), artist)) + return 0; + + if (-1 == lfm_uri_encode (etitle, sizeof (etitle), title)) + return 0; + + char *command = NULL; + if (-1 == asprintf (&command, "xdg-open http://www.last.fm/music/%s/_/%s", eartist, etitle)) + return 0; + system (command); + free (command); +} + +static DB_single_action_t lookup_action = { + .title = "Lookup at Last.fm", + .callback = lfm_action_lookup +}; + +static int +lfm_get_single_actions (DB_playItem_t *it, DB_single_action_t *actions[], int *size) +{ + if (deadbeef->pl_find_meta (it, "artist") && + deadbeef->pl_find_meta (it, "title")) + { + actions[0] = &lookup_action; + *size = 1; + } + else + *size = 0; + return 1; +} + static const char settings_dlg[] = "property \"Enable scrobbler\" checkbox lastfm.enable 0;" "property \"Disable nowplaying\" checkbox lastfm.disable_np 0;" @@ -840,5 +889,6 @@ static DB_misc_t plugin = { .plugin.website = "http://deadbeef.sf.net", .plugin.start = lastfm_start, .plugin.stop = lastfm_stop, - .plugin.configdialog = settings_dlg + .plugin.configdialog = settings_dlg, + .get_single_actions = lfm_get_single_actions }; -- cgit v1.2.3