Fix libnotify config group (libnotify -> libnotify-plugin)
[gmpc-libnotify.git] / src / plugin.c
bloba1b4a699e87bddc5093ed5f11abb9c1b2870276d
1 #include <stdio.h>
2 #include <string.h>
3 #include <glib.h>
4 #include <libnotify/notify.h>
5 #include <gmpc/plugin.h>
6 #include <gmpc/metadata.h>
7 #include <gmpc/misc.h>
8 #include <config.h>
10 extern GtkStatusIcon *tray_icon2_gsi;
11 static NotifyNotification *not = NULL;
12 static guint timeout = 0;
14 static void libnotify_update_cover(GmpcMetaWatcher *gmv, mpd_Song *song, MetaDataType type, MetaDataResult ret, char *path, gpointer data);
16 static void libnotify_plugin_destroy(void)
18 if(timeout)
19 g_source_remove(timeout);
20 timeout = 0;
21 if(not)
22 notify_notification_close(not,NULL);
23 not = NULL;
25 static void libnotify_plugin_init(void)
27 notify_init("gmpc");
28 g_signal_connect(G_OBJECT(gmw), "data-changed", G_CALLBACK(libnotify_update_cover), NULL);
31 static void libnotify_update_cover(GmpcMetaWatcher *gmv, mpd_Song *song, MetaDataType type, MetaDataResult ret, char *path, gpointer data)
33 mpd_Song *song2;
34 if(!not) return;
36 song2 = g_object_get_data(G_OBJECT(not), "mpd-song");
37 if(!song2 || type != META_ALBUM_ART || !gmpc_meta_watcher_match_data(META_ALBUM_ART, song2, song))
38 return;
40 if(ret == META_DATA_AVAILABLE) {
41 GdkPixbuf *pb = gdk_pixbuf_new_from_file_at_scale(path,64,64,TRUE,NULL);
42 screenshot_add_border(&pb);
43 notify_notification_set_icon_from_pixbuf(not, pb);
44 g_object_unref(pb);
45 if(!notify_notification_show(not, NULL))printf("crap\n");
46 } else if (ret == META_DATA_FETCHING) {
47 GdkPixbuf *pb = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),"gmpc-loading-cover" , 64, 0,NULL);
48 notify_notification_set_icon_from_pixbuf(not, pb);
49 g_object_unref(pb);
50 if(!notify_notification_show(not, NULL))printf("crap\n");
51 } else if (ret == META_DATA_UNAVAILABLE) {
52 GdkPixbuf *pb = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),"gmpc" , 64, 0,NULL);
53 notify_notification_set_icon_from_pixbuf(not, pb);
54 g_object_unref(pb);
55 if(!notify_notification_show(not, NULL))printf("crap\n");
59 static int libnotify_get_enabled(void)
61 return cfg_get_single_value_as_int_with_default(config, "libnotify-plugin", "enable", TRUE);
63 static void libnotify_set_enabled(int enabled)
65 cfg_set_single_value_as_int(config, "libnotify-plugin", "enable", enabled);
68 static gboolean timeout_callback(gpointer data)
70 if(not) {
71 notify_notification_close(not,NULL);
72 not = NULL;
73 timeout = 0;
74 return FALSE;
76 return TRUE;
78 static void libnotify_song_changed(MpdObj *mi)
80 int tm = 5000;
81 mpd_Song *song = NULL;
82 if(!cfg_get_single_value_as_int_with_default(config, "libnotify-plugin", "enable", TRUE))
83 return;
84 song = mpd_playlist_get_current_song(connection);
85 if(song) {
86 MetaDataResult ret;
87 gchar *path = NULL;
88 gchar buffer[1024];
89 gchar *summary;
91 mpd_song_markup_escaped(buffer, 1024, "%title%|%name%|%shortfile%", song);
92 summary = g_strdup(buffer);
93 mpd_song_markup_escaped(buffer, 1024, "[<b>Artist:</b> %artist%\n][<b>Album:</b> %album% [(%date%)]\n][<b>Genre:</b> %genre%\n]", song);
94 /* if notification exists update it, else create one */
95 if(not == NULL)
96 not = notify_notification_new(summary, buffer,NULL, NULL);
97 else
99 notify_notification_close(not, NULL);
100 not = notify_notification_new(summary, buffer,NULL, NULL);
103 if(cfg_get_single_value_as_int_with_default(config, "libnotify-plugin", "attach-to-tray", TRUE))
104 notify_notification_attach_to_status_icon(not, tray_icon2_gsi);
106 g_free(summary);
107 /* Add the song to the widget */
108 g_object_set_data_full(G_OBJECT(not), "mpd-song", mpd_songDup(song), (GDestroyNotify)mpd_freeSong);
111 /* set timeout */
112 notify_notification_set_timeout(not, NOTIFY_EXPIRES_NEVER);
113 if(timeout)
114 g_source_remove(timeout);
115 tm = cfg_get_single_value_as_int_with_default(config, "libnotify-plugin", "timeout", 5000);
116 timeout = g_timeout_add(tm,timeout_callback, not);
118 ret = gmpc_meta_watcher_get_meta_path(gmw,song, META_ALBUM_ART,&path);
119 libnotify_update_cover(gmw, song, META_ALBUM_ART, ret, path, NULL);
120 if(path)
121 g_free(path);
123 if(!notify_notification_show(not, NULL))
125 g_source_remove(timeout);
126 timeout = 0;
127 notify_notification_close(not,NULL);
128 not = NULL;
133 /* mpd changed */
134 static void libnotify_mpd_status_changed(MpdObj *mi, ChangedStatusType what, void *data)
136 if(what&(MPD_CST_SONGID))
137 libnotify_song_changed(mi);
140 int plugin_api_version = PLUGIN_API_VERSION;
141 /* main plugin_osd info */
142 gmpcPlugin plugin = {
143 .name = "Libnotify Plugin",
144 .version = {PLUGIN_MAJOR_VERSION,PLUGIN_MINOR_VERSION,PLUGIN_MICRO_VERSION},
145 .plugin_type = GMPC_PLUGIN_NO_GUI,
146 .init = libnotify_plugin_init,
147 .destroy = libnotify_plugin_destroy,
148 .mpd_status_changed = &libnotify_mpd_status_changed,
149 .get_enabled = libnotify_get_enabled,
150 .set_enabled = libnotify_set_enabled