Add/Update translations
[gmpc-dynamic-playlist.git] / src / played.c
blob0c92a3318b5fa227f3f16e912f2af7af9869453e
1 /* gmpc-dynamic-playlist (GMPC plugin)
2 * Copyright (C) 2009 Andre Klitzing <andre@incubo.de>
3 * Homepage: http://www.incubo.de
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 "played.h"
21 #include "fuzzy.h"
22 #include <gmpc/plugin.h>
24 static gint m_song = -1;
25 static gint m_artist = -1;
26 static dbQueue m_list = G_QUEUE_INIT;
28 static void flush_played_list(gint l_max)
30 g_assert(l_max >= 0);
32 const gint max = (gint)g_queue_get_length(&m_list) - l_max;
33 gint i;
34 for(i = 0; i < max; ++i)
35 free_dbSong( (dbSong*) g_queue_pop_tail(&m_list) );
38 void add_played_song(dbSong* l_song)
40 g_assert(l_song != NULL);
41 g_assert(l_song->artist != NULL);
42 g_assert(l_song->title != NULL);
43 g_assert(l_song->artist[0] != '\0');
44 g_assert(l_song->title[0] != '\0');
46 g_queue_push_head(&m_list, l_song);
47 flush_played_list( MAX(m_song, m_artist) );
50 static gboolean is_played_song_current(const gchar* l_artist, const gchar* l_title)
52 mpd_Song* curSong = mpd_playlist_get_current_song(connection);
53 if(curSong != NULL && curSong->artist != NULL && fuzzy_match_artist(l_artist, curSong->artist))
55 if(m_artist > 0 || (l_title != NULL && curSong->title != NULL && fuzzy_match_title(l_title, curSong->title)))
56 return TRUE;
59 return FALSE;
62 gboolean is_played_song(const gchar* l_artist, const gchar* l_title)
64 g_assert(l_artist != NULL);
65 g_assert(l_artist[0] != '\0');
66 if(l_title != NULL)
67 g_assert(l_title[0] != '\0');
69 if(is_played_song_current(l_artist, l_title))
70 return TRUE;
72 if(!g_queue_is_empty(&m_list))
74 dbList* iter = g_queue_peek_head_link(&m_list);
75 gint i;
76 for(i = 0; iter != NULL; iter = g_list_next(iter), ++i)
78 dbSong* song = (dbSong*) iter->data;
79 if(!fuzzy_match_artist(l_artist, song->artist))
80 continue;
82 if(i < m_artist)
84 g_debug("Artist blocked: %s", l_artist);
85 return TRUE;
88 if(i < m_song && l_title != NULL && fuzzy_match_title(l_title, song->title))
90 g_debug("Song blocked: %s::%s", l_artist, l_title);
91 return TRUE;
96 return FALSE;
99 gboolean is_played_artist(const gchar* l_artist)
101 return is_played_song(l_artist, NULL);
104 void set_played_limit_song(gint l_song)
106 g_assert(l_song >= 0);
107 m_song = l_song;
108 cfg_set_single_value_as_int(config, "dynamic-playlist", "block", m_song);
111 void set_played_limit_artist(gint l_artist)
113 g_assert(l_artist >= 0);
114 m_artist = l_artist;
115 cfg_set_single_value_as_int(config, "dynamic-playlist", "block_artist", m_artist);
118 gint get_played_limit_song()
120 return m_song;
123 gint get_played_limit_artist()
125 return m_artist;
128 void init_played_list()
130 m_song = cfg_get_single_value_as_int_with_default(config, "dynamic-playlist", "block", 100);
131 m_artist = cfg_get_single_value_as_int_with_default(config, "dynamic-playlist", "block_artist", 0);
134 void free_played_list()
136 if(!g_queue_is_empty(&m_list))
137 clear_dbQueue(&m_list);
140 /* vim:set ts=4 sw=4: */