1 /* Gnome Music Player Client (GMPC)
2 * Copyright (C) 2004-2009 Qball Cow <qball@sarine.nl>
3 * Project homepage: http://gmpc.wikia.com/
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.
28 typedef enum _LabelType {
30 ARTIST_PLAYTIME_SONGS,
41 #include <libmpd/libmpd.h>
44 class Gmpc:Stats:Label from Gtk:Label
46 private LabelType ltype = ARTIST_NUM_SONGS;
47 private mpd_Song *song = {NULL} destroywith mpd_freeSong;
48 private gulong ll_signal = 0;
54 if(self->_priv->ltype == ARTIST_NUM_SONGS || self->_priv->ltype == ALBUM_NUM_SONGS)
56 MpdDBStats *stats = NULL;
57 mpd_database_search_stats_start(connection);
58 mpd_database_search_add_constraint(connection, MPD_TAG_ITEM_ARTIST, self->_priv->song->artist);
59 if(self->_priv->ltype == ALBUM_NUM_SONGS) {
60 mpd_database_search_add_constraint(connection, MPD_TAG_ITEM_ALBUM, self->_priv->song->album);
62 stats = mpd_database_search_stats_commit(connection);
64 char *buffer = g_strdup_printf("%i", stats->numberOfSongs);
65 gtk_label_set_text(GTK_LABEL(self), buffer);
67 mpd_database_search_free_stats(stats);
69 gtk_label_set_text(GTK_LABEL(self), "");
71 }else if (self->_priv->ltype == ARTIST_PLAYTIME_SONGS || self->_priv->ltype == ALBUM_PLAYTIME_SONGS )
73 MpdDBStats *stats = NULL;
74 mpd_database_search_stats_start(connection);
75 mpd_database_search_add_constraint(connection, MPD_TAG_ITEM_ARTIST, self->_priv->song->artist);
76 if(self->_priv->ltype == ALBUM_PLAYTIME_SONGS) {
77 mpd_database_search_add_constraint(connection, MPD_TAG_ITEM_ALBUM, self->_priv->song->album);
79 stats = mpd_database_search_stats_commit(connection);
81 char *buffer = format_time_real(stats->playTime,"");
82 gtk_label_set_text(GTK_LABEL(self), buffer);
84 mpd_database_search_free_stats(stats);
86 gtk_label_set_text(GTK_LABEL(self), "");
88 }else if (self->_priv->ltype == ARTIST_GENRES_SONGS|| self->_priv->ltype == ALBUM_GENRES_SONGS)
90 GString *string = NULL;
92 mpd_database_search_field_start(connection, MPD_TAG_ITEM_GENRE);
93 mpd_database_search_add_constraint(connection, MPD_TAG_ITEM_ARTIST, self->_priv->song->artist);
94 if(self->_priv->ltype == ALBUM_GENRES_SONGS) {
95 mpd_database_search_add_constraint(connection, MPD_TAG_ITEM_ALBUM, self->_priv->song->album);
97 string = g_string_new("");
98 for(data = mpd_database_search_commit(connection);data != NULL ;data= mpd_data_get_next(data))
100 if(strlen(data->tag) > 0)
101 g_string_append_printf(string, "%s%s",data->tag, (mpd_data_is_last(data))?"":", ");
104 gtk_label_set_text(GTK_LABEL(self), string->str);
106 gtk_label_set_text(GTK_LABEL(self),_("n/a"));
108 g_string_free(string, TRUE);
110 else if (self->_priv->ltype == ARTIST_DATES_SONGS || self->_priv->ltype == ALBUM_DATES_SONGS)
112 GString *string = NULL;
113 MpdData *data = NULL;
114 mpd_database_search_field_start(connection, MPD_TAG_ITEM_DATE);
115 mpd_database_search_add_constraint(connection, MPD_TAG_ITEM_ARTIST, self->_priv->song->artist);
116 if(self->_priv->ltype == ALBUM_DATES_SONGS) {
117 mpd_database_search_add_constraint(connection, MPD_TAG_ITEM_ALBUM, self->_priv->song->album);
119 string = g_string_new("");
120 for(data = mpd_database_search_commit(connection);data != NULL ;data= mpd_data_get_next(data))
122 if(strlen(data->tag) > 0)
123 g_string_append_printf(string, "%s%s",data->tag, (mpd_data_is_last(data))?"":", ");
126 gtk_label_set_text(GTK_LABEL(self), string->str);
128 gtk_label_set_text(GTK_LABEL(self), _("n/a"));
130 g_string_free(string, TRUE);
136 /* This implements lazy loading, the data will only be fetched when the widget is visible for the first time. */
139 expose_event(self, GdkEventExpose *event, gpointer data)
141 gchar *string = g_markup_printf_escaped("<i>%s</i>", _("Loading"));
142 gtk_label_set_markup(GTK_LABEL(self),string);
144 self_idle_handler(self);
145 if(self->_priv->ll_signal) {
146 g_signal_handler_disconnect(self, self->_priv->ll_signal);
147 self->_priv->ll_signal = 0;
154 new(LabelType ltype, mpd_Song *song)
156 Self *self = GET_NEW;
157 self->_priv->song = mpd_songDup(song);
158 self->_priv->ltype = ltype;
159 /* Set expose signal, so I know when it is visible for the first time */
160 self->_priv->ll_signal = g_signal_connect(G_OBJECT(self), "expose-event", G_CALLBACK(self_expose_event), NULL);
161 return (GtkWidget *)self;