Modify version string to post-release version 0.18.101
[gmpc.git] / src / gmpc-stats-label.gob
blob9f558be8dcc976cc8ede5bdbbeb668dc470ce257
1 /* Gnome Music Player Client (GMPC)
2  * Copyright (C) 2004-2009 Qball Cow <qball@sarine.nl>
3  * Project homepage: http://gmpc.wikia.com/
4  
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 requires 2.0.10
22 %ph{
23 #include "main.h"
24 #include "misc.h"
27 %h{
28     typedef enum _LabelType {
29         ARTIST_NUM_SONGS,
30         ARTIST_PLAYTIME_SONGS,
31         ARTIST_GENRES_SONGS,
32         ARTIST_DATES_SONGS,
33         ALBUM_NUM_SONGS,
34         ALBUM_PLAYTIME_SONGS,
35         ALBUM_GENRES_SONGS,
36         ALBUM_DATES_SONGS
37     }LabelType;
39 %ht{
40 #include <gtk/gtk.h>
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;
50     private 
51     gboolean
52     idle_handler(self)
53     {
54         if(self->_priv->ltype == ARTIST_NUM_SONGS || self->_priv->ltype == ALBUM_NUM_SONGS)
55         {
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);
61             }
62             stats = mpd_database_search_stats_commit(connection);
63             if(stats) {
64                 char *buffer = g_strdup_printf("%i", stats->numberOfSongs);
65                 gtk_label_set_text(GTK_LABEL(self), buffer); 
66                 q_free(buffer); 
67                 mpd_database_search_free_stats(stats);
68             } else {
69                 gtk_label_set_text(GTK_LABEL(self), ""); 
70             }
71         }else if (self->_priv->ltype == ARTIST_PLAYTIME_SONGS || self->_priv->ltype == ALBUM_PLAYTIME_SONGS )
72         {
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);
78             }
79             stats = mpd_database_search_stats_commit(connection);
80             if(stats) {
81                 char *buffer = format_time_real(stats->playTime,"");
82                 gtk_label_set_text(GTK_LABEL(self), buffer); 
83                 q_free(buffer); 
84                 mpd_database_search_free_stats(stats);
85             } else {
86                 gtk_label_set_text(GTK_LABEL(self), ""); 
87             }
88         }else if (self->_priv->ltype == ARTIST_GENRES_SONGS|| self->_priv->ltype == ALBUM_GENRES_SONGS)
89         {
90             GString *string = NULL;
91             MpdData *data = 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);
96             }
97             string = g_string_new("");
98             for(data = mpd_database_search_commit(connection);data != NULL ;data= mpd_data_get_next(data))
99             {
100                 if(strlen(data->tag) > 0)
101                     g_string_append_printf(string, "%s%s",data->tag, (mpd_data_is_last(data))?"":", "); 
102             }
103             if(string->len >0) {
104                 gtk_label_set_text(GTK_LABEL(self), string->str); 
105             } else {
106                 gtk_label_set_text(GTK_LABEL(self),_("n/a")); 
107             }
108             g_string_free(string, TRUE);
109         }
110         else if (self->_priv->ltype == ARTIST_DATES_SONGS || self->_priv->ltype == ALBUM_DATES_SONGS)
111         {
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);
118             }
119             string = g_string_new("");
120             for(data = mpd_database_search_commit(connection);data != NULL ;data= mpd_data_get_next(data))
121             {
122                 if(strlen(data->tag) > 0)
123                     g_string_append_printf(string, "%s%s",data->tag, (mpd_data_is_last(data))?"":", "); 
124             }
125             if(string->len >0) {
126                 gtk_label_set_text(GTK_LABEL(self), string->str); 
127             } else {
128                 gtk_label_set_text(GTK_LABEL(self), _("n/a")); 
129             }
130             g_string_free(string, TRUE);
131         }
133         return FALSE;
134     }
136     /* This implements lazy loading, the data will only be fetched when the widget is visible for the first time. */
137     private
138     gboolean
139     expose_event(self, GdkEventExpose *event, gpointer data)
140     {
141         gchar *string = g_markup_printf_escaped("<i>%s</i>", _("Loading"));
142         gtk_label_set_markup(GTK_LABEL(self),string);
143         g_free(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;
148         }
149         return FALSE;
150     }
151     /* Constructor */
152     public
153     GtkWidget *
154     new(LabelType ltype, mpd_Song *song)
155     {
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;
162     }