Project

General

Profile

gitdiff.patch

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

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 <libaudcore/hook.h>
21 22
#include <audacious/debug.h>
23
#include <audacious/i18n.h>
24
#include <audacious/drct.h>
25
#include <audacious/misc.h>
22 26
#include <libnotify/notify.h>
23

  
24 27
#include "osd.h"
25 28

  
26
NotifyNotification *notification = NULL;
27

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

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

  
41 40
    notify_uninit();
42 41
}
43 42

  
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");
43
void* osd_init() {
44
    if (notify_is_initted())
45
        return NULL;
46
    if (notify_init("Audacious"))
47
    {
48
        NotifyNotification *notif = notify_notification_new("Notification", NULL, NULL);
49

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

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

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

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

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

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

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

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

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

  
111

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

  
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);
118
    if(notify_notification_update(notification, title, message, "audacious")) {
119
        AUDDBG("Osd updated!\n");
120
     } else {
121
         AUDDBG("Could not update osd!\n");
122
    }
123

  
124

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

  
132
        notify_notification_clear_actions (notification);
133

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

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

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

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

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

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

  
180
    g_list_free_full(server_caps, g_free);
181

  
182

  
183

  
184
    if (coverfile)
185
        notify_notification_set_hint_string (notification, "image_path", coverfile);
186
    else
187
        notify_notification_set_hint_string (notification, "image_path", "");
72 188

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