4 #include <libnotify/notify.h>
5 #include <gmpc/plugin.h>
6 #include <gmpc/metadata.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)
19 g_source_remove(timeout
);
22 notify_notification_close(not,NULL
);
25 static void libnotify_plugin_init(void)
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
)
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
))
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
);
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
);
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
);
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
)
71 notify_notification_close(not,NULL
);
78 static void libnotify_song_changed(MpdObj
*mi
)
81 mpd_Song
*song
= NULL
;
82 if(!cfg_get_single_value_as_int_with_default(config
, "libnotify-plugin", "enable", TRUE
))
84 song
= mpd_playlist_get_current_song(connection
);
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 */
96 not = notify_notification_new(summary
, buffer
,NULL
, NULL
);
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
);
107 /* Add the song to the widget */
108 g_object_set_data_full(G_OBJECT(not), "mpd-song", mpd_songDup(song
), (GDestroyNotify
)mpd_freeSong
);
112 notify_notification_set_timeout(not, NOTIFY_EXPIRES_NEVER
);
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
);
123 if(!notify_notification_show(not, NULL
))
125 g_source_remove(timeout
);
127 notify_notification_close(not,NULL
);
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