Fix wrong use of screenshot_add_border.
[gmpc-libnotify.git] / src / plugin.c
blob76f1faef7acb1f3ad2ef63e9b93e480d06898327
1 /* gmpc-libnotify (GMPC plugin)
2 * Copyright (C) 2007-2009 Qball Cow <qball@sarine.nl>
3 * Project homepage: http://gmpcwiki.sarine.nl/
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 <stdio.h>
21 #include <string.h>
22 #include <config.h>
23 #include <glib.h>
24 #include <glib/gi18n-lib.h>
25 #include <libnotify/notify.h>
26 #include <gmpc/plugin.h>
27 #include <gmpc/metadata.h>
28 #include <gmpc/misc.h>
29 #include <config.h>
31 #define LOG_DOMAIN "LibNotifyPlugin"
33 extern GtkStatusIcon *tray_icon2_gsi;
34 static NotifyNotification *not = NULL;
35 static gulong data_changed = 0;
36 static void libnotify_update_cover(GmpcMetaWatcher *gmv, mpd_Song *song, MetaDataType type, MetaDataResult ret, MetaData *met, gpointer data);
38 static void libnotify_plugin_destroy(void)
40 if(not)
41 notify_notification_close(not,NULL);
42 if(data_changed) {
43 g_signal_handler_disconnect(G_OBJECT(gmw), data_changed);
44 data_changed = 0;
46 not = NULL;
48 static void libnotify_plugin_init(void)
50 bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
51 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
52 notify_init("gmpc");
53 data_changed = g_signal_connect(G_OBJECT(gmw), "data-changed", G_CALLBACK(libnotify_update_cover), NULL);
56 static const gchar *libnotify_get_translation_domain(void)
58 return GETTEXT_PACKAGE;
61 static void libnotify_update_cover(GmpcMetaWatcher *gmv, mpd_Song *song, MetaDataType type, MetaDataResult ret, MetaData *met, gpointer data)
63 mpd_Song *song2;
64 if(!not) return;
66 song2 = g_object_get_data(G_OBJECT(not), "mpd-song");
67 if(!song2 || type != META_ALBUM_ART || !gmpc_meta_watcher_match_data(META_ALBUM_ART, song2, song))
68 return;
70 if(ret == META_DATA_AVAILABLE) {
71 if(met->content_type == META_DATA_CONTENT_URI)
73 const gchar *path = meta_data_get_uri(met);
74 GError *error = NULL;
75 GdkPixbuf *pb = gdk_pixbuf_new_from_file_at_scale(path,64,64,TRUE,&error);
76 if(error == NULL && pb != NULL)
78 screenshot_add_border(pb);
79 }else{
80 if(pb) g_object_unref(pb);
81 pb = NULL;
83 /* Default to gmpc icon when no icon is found */
84 if(!pb) {
85 pb = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),"gmpc" , 64, 0,NULL);
86 if(!pb) return; /* should never happen, bail out */
88 notify_notification_set_icon_from_pixbuf(not, pb);
89 if(pb)
90 g_object_unref(pb);
92 } else if (ret == META_DATA_FETCHING) {
93 GdkPixbuf *pb = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),"gmpc-loading-cover" , 64, 0,NULL);
94 /* Default to gmpc icon when no icon is found */
95 if(!pb) {
96 pb = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),"gmpc" , 64, 0,NULL);
97 if(!pb) return; /* should never happen, bail out */
99 notify_notification_set_icon_from_pixbuf(not, pb);
100 if(pb)
101 g_object_unref(pb);
102 } else {
103 GdkPixbuf *pb = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),"gmpc" , 64, 0,NULL);
104 if(pb == NULL)
105 g_log(LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Failed to load gmpc icon");
106 notify_notification_set_icon_from_pixbuf(not, pb);
107 if(pb)
108 g_object_unref(pb);
112 static int libnotify_get_enabled(void)
114 return cfg_get_single_value_as_int_with_default(config, "libnotify-plugin", "enable", TRUE);
116 static void libnotify_set_enabled(int enabled)
118 cfg_set_single_value_as_int(config, "libnotify-plugin", "enable", enabled);
121 static void libnotify_song_changed(MpdObj *mi)
123 mpd_Song *song = NULL;
124 if(!cfg_get_single_value_as_int_with_default(config, "libnotify-plugin", "enable", TRUE))
125 return;
126 song = mpd_playlist_get_current_song(connection);
127 if(song)
129 MetaDataResult ret;
130 MetaData *met= NULL;
131 gchar buffer[1024];
132 gchar *summary;
133 gchar *version = NULL, *ret_name = NULL, *ret_vendor = NULL, *ret_spec_version = NULL;
134 int *versions;
135 GdkPixbuf *pb=NULL;
137 notify_get_server_info(&ret_name, &ret_vendor, &version, &ret_spec_version);
138 if(version){
139 versions = split_version(version);
140 }else{
141 versions = g_malloc0(4*sizeof(int));
143 g_log(LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "libnotify version: %i %i %i\n", versions[0], versions[1], versions[2]);
144 if((versions[0] > 0) || (versions[0] == 0 && versions[1] >= 4))
145 mpd_song_markup(buffer, 1024, C_("Summary markup","%title%|%name%|%shortfile%"), song);
146 else
147 mpd_song_markup_escaped(buffer, 1024, "%title%|%name%|%shortfile%", song);
148 summary = g_strdup(buffer);
149 mpd_song_markup_escaped(buffer, 1024,
150 (char *)C_("Body markup", "[<b>Artist:</b> %artist%\n][<b>Album:</b> %album% [(%date%)]\n][<b>Genre:</b> %genre%\n]"),
151 song);
152 /* if notification exists update it, else create one */
153 if(not == NULL)
155 // notify_notification_close(not, NULL);
156 not = notify_notification_new(summary, buffer,NULL, NULL);
158 else{
159 notify_notification_update(not, summary, buffer, NULL);
161 notify_notification_set_urgency(not, NOTIFY_URGENCY_LOW);
163 if(cfg_get_single_value_as_int_with_default(config, "libnotify-plugin", "attach-to-tray", TRUE))
164 notify_notification_attach_to_status_icon(not, tray_icon2_gsi);
166 g_free(summary);
167 /* Add the song to the widget */
168 g_object_set_data_full(G_OBJECT(not), "mpd-song", mpd_songDup(song), (GDestroyNotify)mpd_freeSong);
170 pb = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),"gmpc" , 64, 0,NULL);
171 if(pb){
172 notify_notification_set_icon_from_pixbuf(not, pb);
173 g_object_unref(pb);
175 ret = gmpc_meta_watcher_get_meta_path(gmw,song, META_ALBUM_ART,&met);
176 libnotify_update_cover(gmw, song, META_ALBUM_ART, ret, met, NULL);
177 if(met)
178 meta_data_free(met);
180 if(!notify_notification_show(not, NULL))
182 notify_notification_close(not,NULL);
183 not = NULL;
185 if(ret_name)
186 g_free(ret_name);
187 if(ret_vendor)
188 g_free(ret_vendor);
189 if(ret_spec_version)
190 g_free(ret_spec_version);
191 if(version)
192 g_free(version);
193 g_free(versions);
197 /* mpd changed */
198 static void libnotify_mpd_status_changed(MpdObj *mi, ChangedStatusType what, void *data)
200 if(what&(MPD_CST_SONGID))
201 libnotify_song_changed(mi);
204 int plugin_api_version = PLUGIN_API_VERSION;
205 /* main plugin_osd info */
206 gmpcPlugin plugin = {
207 .name = "Libnotify Plugin",
208 .version = {PLUGIN_MAJOR_VERSION,PLUGIN_MINOR_VERSION,PLUGIN_MICRO_VERSION},
209 .plugin_type = GMPC_PLUGIN_NO_GUI,
210 .init = libnotify_plugin_init,
211 .destroy = libnotify_plugin_destroy,
212 .mpd_status_changed = &libnotify_mpd_status_changed,
213 .get_enabled = libnotify_get_enabled,
214 .set_enabled = libnotify_set_enabled,
216 .get_translation_domain = libnotify_get_translation_domain