Project

General

Profile

gitdiff.patch

Jean-Alexandre Anglès d'Auriac, November 28, 2012 18:04

View differences:

src/notify/event.c
24 24
#include <audacious/drct.h>
25 25
#include <audacious/i18n.h>
26 26
#include <audacious/playlist.h>
27
#include <audacious/misc.h>
27 28
#include <libaudcore/hook.h>
28 29
#include <libaudgui/libaudgui-gtk.h>
29 30

  
......
31 32
#include "event.h"
32 33
#include "osd.h"
33 34

  
34
static gchar * last_title = NULL, * last_message = NULL;
35

  
36
static void clear (void)
37
{
38
    g_free (last_title);
39
    last_title = NULL;
40
    g_free (last_message);
41
    last_message = NULL;
42
}
43

  
44
static void update (void * unused, void * explicit)
35
static void update (void * unused, void * notif)
45 36
{
46 37
    if (! aud_drct_get_playing () || ! aud_drct_get_ready ())
47 38
    {
48
        if (GPOINTER_TO_INT (explicit))
49
            osd_show (_("Stopped"), _("Audacious is not playing."), NULL, NULL);
39
        if (aud_get_bool ("notify", "actions"))
40
            osd_show (_("Stopped"), _("Audacious is not playing."), NULL, notif, TRUE);
50 41

  
51 42
        return;
52 43
    }
......
70 61
    else
71 62
        message = g_strdup ("");
72 63

  
73
    if (! GPOINTER_TO_INT (explicit) && last_title && last_message && ! strcmp
74
     (title, last_title) && ! strcmp (message, last_message))
75
    {
76
        g_free (message);
77
        goto FREE;
78
    }
79

  
80
    GdkPixbuf * pb = audgui_pixbuf_for_current ();
81
    if (pb)
82
        audgui_pixbuf_scale_within (& pb, 96);
64
    gchar *coverfile = NULL;
65
    gchar *filename = aud_playlist_entry_get_filename(list,entry);
66
    const gchar *coverfileuri = aud_art_request_file(filename);
83 67

  
84
    osd_show (title, message, "audio-x-generic", pb);
68
    if (coverfileuri){
69
        if(g_str_has_prefix (coverfileuri, "file://"))
70
            coverfile = g_uri_unescape_string(coverfileuri + 7,"");
71
        else
72
            coverfile = g_uri_unescape_string(coverfileuri,"");
73
        }
85 74

  
86
    if (pb)
87
        g_object_unref (pb);
75
    osd_show (title, message, coverfile, notif, FALSE);
88 76

  
89
    clear ();
90
    last_title = g_strdup (title);
91
    last_message = message;
77
    if (filename)
78
        str_unref (filename);
79
    if (coverfile)
80
        g_free (coverfile);
92 81

  
93
FREE:
82
    if(message)
83
        g_free(message);
94 84
    str_unref (title);
95 85
    str_unref (artist);
96 86
    str_unref (album);
97 87
}
98 88

  
99
void event_init (void)
89
static void update_explicit (void * unused, void * notif)
90
{
91
    if (! aud_drct_get_playing () || ! aud_drct_get_ready ())
92
        osd_show (_("Stopped"), _("Audacious is not playing."), NULL, notif, FALSE);
93
    else
94
        update(unused, notif);
95
    return;
96
}
97

  
98
void event_init (void* notif)
100 99
{
101
    update (NULL, GINT_TO_POINTER (FALSE));
102
    hook_associate ("aosd toggle", (HookFunction) update, GINT_TO_POINTER (TRUE));
103
    hook_associate ("playback ready", (HookFunction) update, GINT_TO_POINTER (FALSE));
104
    hook_associate ("playlist update", (HookFunction) update, GINT_TO_POINTER (FALSE));
105
    hook_associate ("playback stop", (HookFunction) clear, NULL);
100
    hook_associate ("aosd toggle", (HookFunction) update_explicit, notif);
101
    hook_associate ("playback ready", (HookFunction) update, notif);
102
    hook_associate ("playlist update", (HookFunction) update, notif);
103
    hook_associate ("playback pause", (HookFunction) update, notif);
104
    hook_associate ("playback unpause", (HookFunction) update, notif);
106 105
}
107 106

  
108 107
void event_uninit (void)
109 108
{
110
    hook_dissociate ("aosd toggle", (HookFunction) update);
109
    hook_dissociate ("aosd toggle", (HookFunction) update_explicit);
111 110
    hook_dissociate ("playback ready", (HookFunction) update);
112 111
    hook_dissociate ("playlist update", (HookFunction) update);
113
    hook_dissociate ("playback stop", (HookFunction) clear);
114
    clear ();
112
    hook_dissociate ("playback pause", (HookFunction) update);
113
    hook_dissociate ("playback unpause", (HookFunction) update);
115 114
}
src/notify/notify.c
23 23
#include <audacious/i18n.h>
24 24
#include <audacious/plugin.h>
25 25
#include <audacious/debug.h>
26
#include <audacious/preferences.h>
27
#include <audacious/misc.h>
26 28

  
27 29
#include "config.h"
28 30
#include "event.h"
......
47 49
    "You should have received a copy of the GNU General Public License "
48 50
    "along with this program.  If not, see <http://www.gnu.org/licenses/>.");
49 51

  
52
static const char * const notify_defaults[] = {
53
 "actions", "TRUE",
54
 "silent", "FALSE",
55
 NULL};
56

  
57

  
58
static const PreferencesWidget notify_widgets[] = {
59
 {WIDGET_CHK_BTN, N_("Activate controls inside of the notification"),
60
 .cfg_type = VALUE_BOOLEAN, .csect = "notify", .cname = "actions"},
61
};
62

  
63
static const PluginPreferences notify_prefs = {
64
 .widgets = notify_widgets,
65
 .n_widgets = G_N_ELEMENTS (notify_widgets)};
66

  
67

  
68

  
50 69
AUD_GENERAL_PLUGIN
51 70
(
52 71
    .name = N_("Desktop Notifications"),
53 72
    .domain = PACKAGE,
54 73
    .about_text = plugin_about,
55 74
    .init = plugin_init,
75
    .prefs = & notify_prefs,
56 76
    .cleanup = plugin_cleanup
57 77
)
58 78

  
......
61 81
gboolean plugin_init (void)
62 82
{
63 83
    AUDDBG("started!\n");
64
    if(!osd_init()) {
84
    aud_config_set_defaults ("notify", notify_defaults);
85
    void* notif = osd_init();
86
    if(!notif) {
65 87
        AUDDBG("osd_init failed!\n");
66 88
        return FALSE;
67 89
    }
68
    event_init();
90
    event_init(notif);
69 91

  
70 92
    plugin_active = 1;
71 93
    return TRUE;
src/notify/osd.c
18 18
 */
19 19

  
20 20
#include <glib.h>
21
#include <gtk/gtk.h>
22
#include <libaudcore/hook.h>
21 23
#include <audacious/debug.h>
24
#include <audacious/i18n.h>
25
#include <audacious/drct.h>
26
#include <audacious/misc.h>
22 27
#include <libnotify/notify.h>
23

  
24 28
#include "osd.h"
25 29

  
26
NotifyNotification *notification = NULL;
27

  
28
gboolean osd_init() {
29
    notification = NULL;
30
    return notify_init ("Audacious");
31
}
32

  
33
void osd_uninit (void)
30
static void clean (void * unused, void * notif)
34 31
{
32
    NotifyNotification *notification = (NotifyNotification *) notif;
35 33
    if (notification)
36 34
    {
35
        GError *error = NULL;
36
        if (notify_notification_close (notification, &error) == FALSE)
37
            AUDDBG("%s!\n", error->message);
37 38
        g_object_unref (notification);
38
        notification = NULL;
39 39
    }
40 40

  
41 41
    notify_uninit();
42 42
}
43 43

  
44
void osd_closed_handler(NotifyNotification *notification2, gpointer data) {
45
    if(notification != NULL) {
46
        g_object_unref(notification);
47
        notification = NULL;
48
        AUDDBG("notification unrefed!\n");
44
void* osd_init() {
45
    if (notify_is_initted())
46
        return NULL;
47
    if (notify_init("Audacious"))
48
    {
49
        NotifyNotification *notif = notify_notification_new("Notification", NULL, NULL);
50

  
51
        hook_associate ("notify shutdown", (HookFunction) clean, notif);
52

  
53
        notify_notification_set_urgency (notif, NOTIFY_URGENCY_LOW);
54
        notify_notification_set_hint (notif, "desktop-entry", g_variant_new_string ("audacious"));
55

  
56
        GList *server_caps;
57
        server_caps = notify_get_server_caps ();
58
        if (g_list_find_custom (server_caps, "actions", (GCompareFunc)g_strcmp0))
59
        {
60
            if (g_list_find_custom (server_caps, "action-icons", (GCompareFunc)g_strcmp0))
61
                notify_notification_set_hint (notif, "action-icons", g_variant_new_boolean (TRUE));
62
        }
63
        g_list_free_full(server_caps, g_free);
64
        return notif;
49 65
    }
66

  
67
    return NULL;
68
}
69

  
70
void osd_uninit (void)
71
{
72
    hook_call("notify shutdown", NULL);
73
    hook_dissociate ("notify shutdown", (HookFunction) clean);
74
}
75

  
76

  
77
static void
78
goprevious (NotifyNotification *notification,
79
            const char *action,
80
            gpointer user_data)
81
{
82
    aud_drct_pl_prev();
83
}
84

  
85
static void
86
gonext (NotifyNotification *notification,
87
        const char *action,
88
        gpointer user_data)
89
{
90
    aud_drct_pl_next();
91
}
92

  
93
static void
94
show_audacious (NotifyNotification *notification,
95
        const char *action,
96
        gpointer user_data)
97
{
98
    aud_interface_show(TRUE);
99
}
100

  
101
static void
102
playpause (NotifyNotification *notification,
103
           const char *action,
104
           gpointer user_data)
105
{
106
    if(aud_drct_get_playing())
107
    {aud_drct_pause();}
108
    else
109
    {aud_drct_play();}
50 110
}
51 111

  
52
void osd_show (const gchar * title, const gchar * _message, const gchar * icon,
53
 GdkPixbuf * pb)
112

  
113
void osd_show (const gchar * title, const gchar * _message, const gchar * coverfile, void *notif, gboolean actiononly)
54 114
{
115
    NotifyNotification *notification = (NotifyNotification *) notif;
55 116
    gchar * message = g_markup_escape_text (_message, -1);
56 117
    GError *error = NULL;
57 118

  
58
    if(notification == NULL) {
59
        notification = notify_notification_new(title, message, pb == NULL ? icon : NULL);
60
        g_signal_connect(notification, "closed", G_CALLBACK(osd_closed_handler), NULL);
61
        AUDDBG("new osd created! (notification=%p)\n", (void *) notification);
62
    } else {
63
        if(notify_notification_update(notification, title, message, pb == NULL ? icon : NULL)) {
64
            AUDDBG("old osd updated! (notification=%p)\n", (void *) notification);
65
        } else {
66
            AUDDBG("could not update old osd! (notification=%p)\n", (void *) notification);
119
    GList *server_caps;
120
    server_caps = notify_get_server_caps ();
121

  
122
    if (g_list_find_custom (server_caps, "persistence", (GCompareFunc)g_strcmp0))
123
        {
124
            AUDDBG("Notification server supports persistence\n");
125
            if (aud_get_bool ("notify", "actions"))
126
                notify_notification_set_hint (notif, "resident", g_variant_new_boolean (TRUE));
127
            else
128
                notify_notification_set_hint (notif, "resident", g_variant_new_boolean (FALSE));
129
        }
130

  
131
    if (g_list_find_custom (server_caps, "actions", (GCompareFunc)g_strcmp0))
132
    {
133
        gboolean playing = FALSE;
134
        if (aud_drct_get_playing()) {playing = !aud_drct_get_paused();}
135

  
136
        notify_notification_clear_actions (notification);
137

  
138
        if(aud_get_bool ("notify", "actions")){
139
            notify_notification_add_action (notification,
140
                        "media-skip-backward",
141
                        N_("Previous"),
142
                        (NotifyActionCallback) goprevious,
143
                        NULL,
144
                        NULL);
145

  
146
            notify_notification_add_action (notification,
147
                            playing ? "media-playback-pause" : "media-playback-start",
148
                            playing ? N_("Pause") : N_("Play"),
149
                            (NotifyActionCallback) playpause,
150
                            NULL,
151
                            NULL);
152

  
153
            notify_notification_add_action (notification,
154
                        "media-skip-forward",
155
                        N_("Next"),
156
                        (NotifyActionCallback) gonext,
157
                        NULL,
158
                        NULL);
159

  
160
            notify_notification_add_action (notification,
161
                        "default",
162
                        N_("Afficher Audacious"),
163
                        (NotifyActionCallback) show_audacious,
164
                        NULL,
165
                        NULL);
166
            notify_notification_set_timeout(notification, 1);
167
        }
168
    }
169
    else
170
    {
171
        if (actiononly)
172
        {
173
            g_list_free_full(server_caps, g_free);
174
            g_free (message);
175
            return;
67 176
        }
68 177
    }
69 178

  
70
    if(pb != NULL)
71
        notify_notification_set_icon_from_pixbuf(notification, pb);
179
    g_list_free_full(server_caps, g_free);
180

  
181

  
182

  
183
    if (coverfile && g_strcmp0(coverfile,""))
184
        notify_notification_set_hint_string (notification, "image_path", coverfile);
185
    else{
186
        GtkIconInfo * iconinfo = gtk_icon_theme_lookup_icon(gtk_icon_theme_get_default(),"audio-x-generic",512,GTK_ICON_LOOKUP_FORCE_SVG);
187
        notify_notification_set_hint_string (notification, "image_path", gtk_icon_info_get_filename(iconinfo));
188
        gtk_icon_info_free(iconinfo);
189
        }
190

  
191
    if(!notify_notification_update(notification, title, message, "audacious")) 
192
         AUDDBG("Could not update osd!\n");
72 193

  
73 194
    if(!notify_notification_show(notification, &error))
74 195
        AUDDBG("%s!\n", error->message);
src/notify/osd.h
21 21
#include <libnotify/notify.h>
22 22

  
23 23
// Prototypes
24
gboolean osd_init();
24
void* osd_init();
25 25
void osd_uninit();
26 26
void osd_closed_handler(NotifyNotification *notification2, gpointer data);
27
void osd_show(const gchar *title, const gchar *message, const gchar *icon, GdkPixbuf *pb);
27
void osd_show(const gchar *title, const gchar *message, const gchar *coverfile, void *notification, gboolean actiononly);