Add/Update translations
[gmpc-dynamic-playlist.git] / src / database.c
blob30680016ee632197bbd28e1d2b6ba1c2045e7fde
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 <gmpc/plugin.h>
21 #include "database.h"
22 #include "blacklist.h"
23 #include "played.h"
24 #include <libmpd/libmpd-internal.h>
26 extern GRand* m_rand;
28 static dbList* database_get_songs_fill_list(dbList* l_list, gint* l_out_count)
30 g_assert(l_out_count != NULL && *l_out_count >= 0);
32 MpdData* data;
33 for(data = mpd_database_search_commit(connection); data != NULL; data = mpd_data_get_next(data))
35 if(data->song->artist != NULL && data->song->title != NULL
36 && !is_blacklisted(data->song)
37 && !is_played_song(data->song->artist, data->song->title))
39 dbSong* song = new_dbSong(data->song->artist, data->song->title, data->song->file);
40 l_list = g_list_prepend(l_list, song);
41 ++(*l_out_count);
45 return l_list;
48 dbList* database_get_songs_comment(dbList* l_list, const gchar* l_comment, gint* l_out_count)
50 g_assert(l_comment != NULL);
52 mpd_database_search_start(connection, TRUE);
53 mpd_database_search_add_constraint(connection, MPD_TAG_ITEM_COMMENT, l_comment);
55 return database_get_songs_fill_list(l_list, l_out_count);
58 dbList* database_get_songs_genre(dbList* l_list, const gchar* l_genre, gint* l_out_count)
60 g_assert(l_genre != NULL);
62 if(is_blacklisted_genre(l_genre))
63 return l_list;
65 mpd_database_search_start(connection, TRUE);
66 mpd_database_search_add_constraint(connection, MPD_TAG_ITEM_GENRE, l_genre);
68 return database_get_songs_fill_list(l_list, l_out_count);
71 dbList* database_get_songs(dbList* l_list, const gchar* l_artist, const gchar* l_title, gint* l_out_count)
73 g_assert(l_artist != NULL && l_title != NULL);
75 if(is_blacklisted_artist(l_artist) || is_blacklisted_song(l_artist, l_title))
76 return l_list;
78 mpd_database_search_start(connection, FALSE);
79 gchar** artist_split = g_strsplit(l_artist, " ", -1);
80 gint i;
81 for(i = 0; artist_split != NULL && artist_split[i] != NULL; ++i)
82 mpd_database_search_add_constraint(connection, MPD_TAG_ITEM_ARTIST, artist_split[i]);
83 g_strfreev(artist_split);
84 mpd_database_search_add_constraint(connection, MPD_TAG_ITEM_TITLE, l_title);
86 return database_get_songs_fill_list(l_list, l_out_count);
89 strList* database_get_artists(strList* l_list, const gchar* l_artist, const gchar* l_genre, gint* l_out_count)
91 g_assert(l_out_count != NULL && *l_out_count >= 0);
93 if(is_blacklisted_genre(l_genre) || is_blacklisted_artist(l_artist))
94 return l_list;
96 mpd_database_search_field_start(connection, MPD_TAG_ITEM_ARTIST);
97 if(l_artist != NULL)
98 mpd_database_search_add_constraint(connection, MPD_TAG_ITEM_ARTIST, l_artist);
99 if(l_genre != NULL)
100 mpd_database_search_add_constraint(connection, MPD_TAG_ITEM_GENRE, l_genre);
102 MpdData* data;
103 for(data = mpd_database_search_commit(connection); data != NULL; data = mpd_data_get_next(data))
105 if(data->tag_type == MPD_TAG_ITEM_ARTIST
106 && data->tag != NULL
107 && data->tag[0] != '\0'
108 && !is_blacklisted_artist(data->tag)
109 && !is_played_artist(data->tag))
111 l_list = new_strListItem(l_list, data->tag);
112 ++(*l_out_count);
116 return l_list;
119 gboolean database_tryToAdd_artist(const gchar* l_artist)
121 g_assert(l_artist != NULL);
123 mpd_database_search_start(connection, FALSE);
124 mpd_database_search_add_constraint(connection, MPD_TAG_ITEM_ARTIST, l_artist);
126 gint count = 0;
127 MpdData* prev = NULL;
128 gboolean first = TRUE;
129 MpdData* data = mpd_database_search_commit(connection);
130 while(data != NULL)
132 const gchar* artist = data->song->albumartist == NULL ? data->song->artist : data->song->albumartist;
133 if(data->song->artist == NULL || data->song->title == NULL
134 || is_blacklisted_genre(data->song->genre)
135 || is_blacklisted_album(artist, data->song->album)
136 || is_blacklisted_song(data->song->artist, data->song->title)
137 || is_played_song(data->song->artist, data->song->title))
139 data = mpd_data_delete_item(data);
140 if( data == NULL || (first && ((MpdData_real*) data)->prev == NULL) )
141 continue;
143 else
144 ++count;
146 first = FALSE;
147 prev = data;
148 data = mpd_data_get_next_real(data, FALSE);
151 if(count > 0)
153 g_assert(prev != NULL);
155 gint selected = g_rand_int_range(m_rand, 0, count);
156 g_debug("Artist selected: %s", l_artist);
157 g_debug("Song selected: %d, count: %d", selected, count);
158 gint i = 0;
159 for(data = mpd_data_get_first(prev); i < selected; ++i)
160 data = mpd_data_get_next_real(data, FALSE);
161 g_assert(data != NULL);
163 dbSong* song = new_dbSong(data->song->artist, data->song->title, data->song->file);
164 mpd_playlist_add(connection, song->path);
165 add_played_song(song);
166 g_debug("Added via artist | artist: %s | title: %s | genre: %s",
167 data->song->artist, data->song->title, data->song->genre);
168 mpd_data_free(data);
170 return TRUE;
173 return FALSE;
176 gboolean database_tryToAdd_artists(strList** l_out_list, gint l_count)
178 g_assert(l_out_list != NULL && l_count > 0);
180 gboolean found = FALSE;
183 gint selected = g_rand_int_range(m_rand, 0, l_count);
184 g_debug("Artist selected: %d, count: %d", selected, l_count);
185 gint i = 0;
186 strList* prev = NULL;
187 strList* iter;
188 for(iter = *l_out_list; i < selected; ++i)
190 prev = iter;
191 iter = g_slist_next(iter);
194 --l_count;
195 found = database_tryToAdd_artist( (gchar*) iter->data );
196 if(prev == NULL) // first element will be freed
198 strList* tmp = *l_out_list;
199 *l_out_list = g_slist_next(*l_out_list);
200 clear_strListItem(tmp);
201 g_slist_free_1(tmp);
203 else
204 free_next_strListItem(prev);
206 while(!found && l_count > 0);
208 return found;
211 /* vim:set ts=4 sw=4: */