~gmpc-trunk/gmpc/gmpc-libnotify

« back to all changes in this revision

Viewing changes to src/plugin.c

  • Committer: Qball
  • Date: 2010-02-15 21:01:09 UTC
  • Revision ID: git-v1:78b47f1cfa18f78c6735be2576488ebd5ef7ab76
cleaning

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* gmpc-libnotify (GMPC plugin)
2
2
 * Copyright (C) 2007-2009 Qball Cow <qball@sarine.nl>
3
3
 * Project homepage: http://gmpcwiki.sarine.nl/
4
 
 
 
4
 
5
5
 * This program is free software; you can redistribute it and/or modify
6
6
 * it under the terms of the GNU General Public License as published by
7
7
 * the Free Software Foundation; either version 2 of the License, or
15
15
 * You should have received a copy of the GNU General Public License along
16
16
 * with this program; if not, write to the Free Software Foundation, Inc.,
17
17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 
*/
 
18
 */
19
19
 
20
20
#include <stdio.h>
21
21
#include <string.h>
39
39
{
40
40
        if(not)
41
41
                notify_notification_close(not,NULL);
42
 
    if(data_changed) {
43
 
        g_signal_handler_disconnect(G_OBJECT(gmw), data_changed);
44
 
        data_changed = 0;
45
 
    }
46
 
    not = NULL;
 
42
        if(data_changed) {
 
43
                g_signal_handler_disconnect(G_OBJECT(gmw), data_changed);
 
44
                data_changed = 0;
 
45
        }
 
46
        not = NULL;
47
47
}
48
48
static void libnotify_plugin_init(void)
49
49
{
55
55
 
56
56
static const gchar *libnotify_get_translation_domain(void)
57
57
{
58
 
    return GETTEXT_PACKAGE;
 
58
        return GETTEXT_PACKAGE;
59
59
}
60
60
 
61
61
static void libnotify_update_cover(GmpcMetaWatcher *gmv, mpd_Song *song, MetaDataType type, MetaDataResult ret, MetaData *met, gpointer data)
68
68
                return;
69
69
 
70
70
        if(ret == META_DATA_AVAILABLE) {
71
 
        if(met->content_type == META_DATA_CONTENT_URI)
72
 
        {
73
 
            const gchar *path = meta_data_get_uri(met);
74
 
            GdkPixbuf *pb = gdk_pixbuf_new_from_file_at_scale(path,64,64,TRUE,NULL);
75
 
            if(pb)
76
 
            {
77
 
                screenshot_add_border(&pb);
78
 
            }
79
 
            /* Default to gmpc icon when no icon is found */
80
 
            if(!pb) {
81
 
                pb = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),"gmpc" , 64, 0,NULL);
82
 
                if(!pb) return; /* should never happen, bail out */
83
 
            }
84
 
            notify_notification_set_icon_from_pixbuf(not, pb);
85
 
            if(pb)
86
 
                g_object_unref(pb);
87
 
        }
 
71
                if(met->content_type == META_DATA_CONTENT_URI)
 
72
                {
 
73
                        const gchar *path = meta_data_get_uri(met);
 
74
                        GdkPixbuf *pb = gdk_pixbuf_new_from_file_at_scale(path,64,64,TRUE,NULL);
 
75
                        if(pb)
 
76
                        {
 
77
                                screenshot_add_border(&pb);
 
78
                        }
 
79
                        /* Default to gmpc icon when no icon is found */
 
80
                        if(!pb) {
 
81
                                pb = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),"gmpc" , 64, 0,NULL);
 
82
                                if(!pb) return; /* should never happen, bail out */
 
83
                        }
 
84
                        notify_notification_set_icon_from_pixbuf(not, pb);
 
85
                        if(pb)
 
86
                                g_object_unref(pb);
 
87
                }
88
88
        } else if (ret == META_DATA_FETCHING) {
89
89
                GdkPixbuf *pb = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),"gmpc-loading-cover" , 64, 0,NULL);
90
90
                /* Default to gmpc icon when no icon is found */
93
93
                        if(!pb) return; /* should never happen, bail out */
94
94
                }
95
95
                notify_notification_set_icon_from_pixbuf(not, pb);
96
 
        if(pb)
97
 
            g_object_unref(pb);
 
96
                if(pb)
 
97
                        g_object_unref(pb);
98
98
        } else {
99
99
                GdkPixbuf *pb = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),"gmpc" , 64, 0,NULL);
100
 
        if(pb == NULL) 
101
 
            g_log(LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Failed to load gmpc icon");
 
100
                if(pb == NULL) 
 
101
                        g_log(LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Failed to load gmpc icon");
102
102
                notify_notification_set_icon_from_pixbuf(not, pb);
103
 
        if(pb)
104
 
            g_object_unref(pb);
 
103
                if(pb)
 
104
                        g_object_unref(pb);
105
105
        }
106
106
}
107
107
 
120
120
        if(!cfg_get_single_value_as_int_with_default(config, "libnotify-plugin", "enable", TRUE))
121
121
                return;
122
122
        song = mpd_playlist_get_current_song(connection);
123
 
        if(song) {
124
 
        MetaDataResult ret;
125
 
        MetaData *met= NULL;
 
123
        if(song) 
 
124
        {
 
125
                MetaDataResult ret;
 
126
                MetaData *met= NULL;
126
127
                gchar buffer[1024];
127
128
                gchar *summary;
128
 
        gchar *version = NULL, *ret_name = NULL, *ret_vendor = NULL, *ret_spec_version = NULL;
129
 
        int *versions;
130
 
        GdkPixbuf *pb=NULL;
131
 
        
132
 
        notify_get_server_info(&ret_name, &ret_vendor, &version, &ret_spec_version);
133
 
        if(version){
134
 
            versions = split_version(version); 
135
 
        }else{
136
 
            versions = g_malloc0(4*sizeof(int));
137
 
        }
138
 
        g_log(LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "libnotify version: %i %i %i\n", versions[0], versions[1], versions[2]);
139
 
        if((versions[0] > 0) || (versions[0] == 0 && versions[1] >= 4))
140
 
            mpd_song_markup(buffer, 1024, C_("Summary markup","%title%|%name%|%shortfile%"), song);
141
 
        else
142
 
            mpd_song_markup_escaped(buffer, 1024, "%title%|%name%|%shortfile%", song);
 
129
                gchar *version = NULL, *ret_name = NULL, *ret_vendor = NULL, *ret_spec_version = NULL;
 
130
                int *versions;
 
131
                GdkPixbuf *pb=NULL;
 
132
 
 
133
                notify_get_server_info(&ret_name, &ret_vendor, &version, &ret_spec_version);
 
134
                if(version){
 
135
                        versions = split_version(version); 
 
136
                }else{
 
137
                        versions = g_malloc0(4*sizeof(int));
 
138
                }
 
139
                g_log(LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "libnotify version: %i %i %i\n", versions[0], versions[1], versions[2]);
 
140
                if((versions[0] > 0) || (versions[0] == 0 && versions[1] >= 4))
 
141
                        mpd_song_markup(buffer, 1024, C_("Summary markup","%title%|%name%|%shortfile%"), song);
 
142
                else
 
143
                        mpd_song_markup_escaped(buffer, 1024, "%title%|%name%|%shortfile%", song);
143
144
                summary = g_strdup(buffer);
144
145
                mpd_song_markup_escaped(buffer, 1024, 
145
 
                (char *)C_("Body markup", "[<b>Artist:</b> %artist%\n][<b>Album:</b> %album% [(%date%)]\n][<b>Genre:</b> %genre%\n]"),
146
 
                song);
147
 
        /* if notification exists update it, else create one */
148
 
        if(not == NULL)
149
 
        {
150
 
//            notify_notification_close(not, NULL);
151
 
        not = notify_notification_new(summary, buffer,NULL, NULL);
152
 
        }
153
 
        else{
154
 
                notify_notification_update(not, summary, buffer, NULL);
155
 
        }
156
 
        notify_notification_set_urgency(not, NOTIFY_URGENCY_LOW);
 
146
                                (char *)C_("Body markup", "[<b>Artist:</b> %artist%\n][<b>Album:</b> %album% [(%date%)]\n][<b>Genre:</b> %genre%\n]"),
 
147
                                song);
 
148
                /* if notification exists update it, else create one */
 
149
                if(not == NULL)
 
150
                {
 
151
                        //            notify_notification_close(not, NULL);
 
152
                        not = notify_notification_new(summary, buffer,NULL, NULL);
 
153
                }
 
154
                else{
 
155
                        notify_notification_update(not, summary, buffer, NULL);
 
156
                }
 
157
                notify_notification_set_urgency(not, NOTIFY_URGENCY_LOW);
157
158
 
158
159
                if(cfg_get_single_value_as_int_with_default(config, "libnotify-plugin", "attach-to-tray", TRUE))
159
160
                        notify_notification_attach_to_status_icon(not, tray_icon2_gsi);
161
162
                g_free(summary);
162
163
                /* Add the song to the widget */
163
164
                g_object_set_data_full(G_OBJECT(not), "mpd-song", mpd_songDup(song),  (GDestroyNotify)mpd_freeSong);
164
 
                
165
 
        pb = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),"gmpc" , 64, 0,NULL);
166
 
        if(pb){
167
 
            notify_notification_set_icon_from_pixbuf(not, pb);
168
 
            g_object_unref(pb);
169
 
            }
 
165
 
 
166
                pb = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),"gmpc" , 64, 0,NULL);
 
167
                if(pb){
 
168
                        notify_notification_set_icon_from_pixbuf(not, pb);
 
169
                        g_object_unref(pb);
 
170
                }
170
171
                ret = gmpc_meta_watcher_get_meta_path(gmw,song, META_ALBUM_ART,&met);
171
172
                libnotify_update_cover(gmw, song, META_ALBUM_ART, ret, met, NULL);
172
173
                if(met)
173
174
                        meta_data_free(met);
174
175
 
175
176
                if(!notify_notification_show(not, NULL))
176
 
        {
177
 
            notify_notification_close(not,NULL);
178
 
            not = NULL;
179
 
        }
180
 
        if(ret_name)
181
 
            g_free(ret_name);
182
 
        if(ret_vendor)
183
 
            g_free(ret_vendor);
184
 
        if(ret_spec_version)
185
 
            g_free(ret_spec_version);
186
 
        if(version)
187
 
            g_free(version);
188
 
        g_free(versions);
189
 
    }
 
177
                {
 
178
                        notify_notification_close(not,NULL);
 
179
                        not = NULL;
 
180
                }
 
181
                if(ret_name)
 
182
                        g_free(ret_name);
 
183
                if(ret_vendor)
 
184
                        g_free(ret_vendor);
 
185
                if(ret_spec_version)
 
186
                        g_free(ret_spec_version);
 
187
                if(version)
 
188
                        g_free(version);
 
189
                g_free(versions);
 
190
        }
190
191
}
191
192
 
192
193
/* mpd changed */
199
200
int plugin_api_version = PLUGIN_API_VERSION;
200
201
/* main plugin_osd info */
201
202
gmpcPlugin plugin = {
202
 
        .name                                   = "Libnotify Plugin",
 
203
        .name                   = "Libnotify Plugin",
203
204
        .version                = {PLUGIN_MAJOR_VERSION,PLUGIN_MINOR_VERSION,PLUGIN_MICRO_VERSION},
204
 
        .plugin_type                    = GMPC_PLUGIN_NO_GUI,
205
 
        .init                                   = libnotify_plugin_init,
206
 
        .destroy                                = libnotify_plugin_destroy,
207
 
        .mpd_status_changed     = &libnotify_mpd_status_changed,
208
 
        .get_enabled                    = libnotify_get_enabled,
209
 
        .set_enabled                    = libnotify_set_enabled, 
 
205
        .plugin_type            = GMPC_PLUGIN_NO_GUI,
 
206
        .init                   = libnotify_plugin_init,
 
207
        .destroy                = libnotify_plugin_destroy,
 
208
        .mpd_status_changed     = &libnotify_mpd_status_changed,
 
209
        .get_enabled            = libnotify_get_enabled,
 
210
        .set_enabled            = libnotify_set_enabled, 
210
211
 
211
 
    .get_translation_domain = libnotify_get_translation_domain
 
212
        .get_translation_domain = libnotify_get_translation_domain
212
213
};