Project

General

Profile

gitdiff.patch

Jean-Alexandre Anglès d'Auriac, November 27, 2012 16:46

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);
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
    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);
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);
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
 {WIDGET_CHK_BTN, N_("Silent mode (don't show the notification if activated"),
62
 .cfg_type = VALUE_BOOLEAN, .csect = "notify", .cname = "silent"},
63
};
64

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

  
69

  
70

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

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

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

  
20 20
#include <glib.h>
21
#include <audacious/debug.h>
21
#include <libaudcore/hook.h>
22
/*#include <audacious/debug.h>*/
23
#define AUDDBG(...) do {printf ("%s:%d %s(): ", __FILE__, __LINE__, __FUNCTION__); printf (__VA_ARGS__);} while (0)
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);
50 74
}
51 75

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

  
77
static void
78
goprevious (NotifyNotification *notification,
79
            const char *action,
80
            gpointer user_data)
54 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();}
110
}
111

  
112

  
113
void osd_show (const gchar * title, const gchar * _message, const gchar * coverfile, void *notif)
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
    AUDDBG("Title : «%s» Message : «%s»\n", title, message);
120
    if(notify_notification_update(notification, title, message, "audacious")) {
121
        AUDDBG("Osd updated!\n");
122
     } else {
123
         AUDDBG("Could not update osd!\n");
124
    }
125

  
126

  
127
    GList *server_caps;
128
    server_caps = notify_get_server_caps ();
129
    if (g_list_find_custom (server_caps, "actions", (GCompareFunc)g_strcmp0))
130
    {
131
        gboolean playing = FALSE;
132
        if (aud_drct_get_playing()) {playing = !aud_drct_get_paused();}
133

  
134
        notify_notification_clear_actions (notification);
135

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

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

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

  
158
            notify_notification_add_action (notification,
159
                        "default",
160
                        N_("Afficher Audacious"),
161
                        (NotifyActionCallback) show_audacious,
162
                        NULL,
163
                        NULL);
164
            notify_notification_set_timeout(notification, 1);
67 165
        }
68 166
    }
69 167

  
70
    if(pb != NULL)
71
        notify_notification_set_icon_from_pixbuf(notification, pb);
168
    if(aud_get_bool ("notify", "silent"))
169
        notify_notification_set_hint (notif, "transient", g_variant_new_boolean (TRUE));
170
    else
171
        notify_notification_set_hint (notif, "transient", g_variant_new_boolean (FALSE));
172

  
173
    if (g_list_find_custom (server_caps, "persistence", (GCompareFunc)g_strcmp0))
174
        {
175
            AUDDBG("Notification server supports persistence\n");
176
            if (aud_get_bool ("notify", "actions"))
177
                notify_notification_set_hint (notif, "resident", g_variant_new_boolean (TRUE));
178
            else
179
                notify_notification_set_hint (notif, "resident", g_variant_new_boolean (FALSE));
180
        }
181

  
182
    g_list_free_full(server_caps, g_free);
183

  
184

  
185

  
186
    if (coverfile)
187
        notify_notification_set_hint_string (notification, "image_path", coverfile);
188
    else
189
        notify_notification_set_hint_string (notification, "image_path", "");
72 190

  
73 191
    if(!notify_notification_show(notification, &error))
74 192
        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);