~ubuntu-branches/ubuntu/saucy/parole/saucy

« back to all changes in this revision

Viewing changes to src/plugins/notify/notify-provider.c

  • Committer: Package Import Robot
  • Author(s): Lionel Le Folgoc
  • Date: 2013-01-09 19:08:53 UTC
  • mfrom: (3.1.3 raring-proposed)
  • Revision ID: package-import@ubuntu.com-20130109190853-zeh9ksa0ijwzgmrs
Tags: 0.4.0-0ubuntu1
* New upstream release.
* debian/control:
  - add b-dep on libxfconf-0-dev since parole now uses Xfconf
  - suggests gnome-codec-install for the missing codec feature.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * * Copyright (C) 2012 Sean Davis <smd.seandavis@gmail.com>
 
3
 *
 
4
 * Licensed under the GNU General Public License Version 2
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 */
 
20
 
 
21
#ifdef HAVE_CONFIG_H
 
22
#include <config.h>
 
23
#endif
 
24
 
 
25
#include <glib.h>
 
26
#include <gtk/gtk.h>
 
27
 
 
28
#include <src/misc/parole.h>
 
29
 
 
30
#include <libnotify/notify.h>
 
31
 
 
32
#include "notify-provider.h"
 
33
 
 
34
#define RESOURCE_FILE   "xfce4/src/misc/parole-plugins/notify.rc"
 
35
 
 
36
static void   notify_provider_iface_init    (ParoleProviderPluginIface *iface);
 
37
static void   notify_provider_finalize      (GObject                   *object);
 
38
 
 
39
 
 
40
struct _NotifyProviderClass
 
41
{
 
42
    GObjectClass parent_class;
 
43
};
 
44
 
 
45
struct _NotifyProvider
 
46
{
 
47
    GObject                 parent;
 
48
    ParoleProviderPlayer   *player;
 
49
 
 
50
    NotifyNotification     *notification;
 
51
};
 
52
 
 
53
PAROLE_DEFINE_TYPE_WITH_CODE (NotifyProvider, 
 
54
                        notify_provider, 
 
55
                        G_TYPE_OBJECT,
 
56
                        PAROLE_IMPLEMENT_INTERFACE (PAROLE_TYPE_PROVIDER_PLUGIN, 
 
57
                                            notify_provider_iface_init));
 
58
 
 
59
static void
 
60
notification_closed_cb (NotifyNotification *n, NotifyProvider *notify)
 
61
{
 
62
    g_object_unref (notify->notification);
 
63
    notify->notification = NULL;
 
64
}
 
65
 
 
66
static void
 
67
close_notification (NotifyProvider *notify)
 
68
{
 
69
    if ( notify->notification )
 
70
    {
 
71
        GError *error = NULL;
 
72
        notify_notification_close (notify->notification, &error);
 
73
        if ( error )
 
74
        {
 
75
            g_warning ("Failed to close notification : %s", error->message);
 
76
            g_error_free (error);
 
77
        }
 
78
        g_object_unref (notify->notification);
 
79
        notify->notification = NULL;
 
80
    }
 
81
}
 
82
 
 
83
static void
 
84
notify_playing (NotifyProvider *notify, const ParoleStream *stream)
 
85
{
 
86
    GdkPixbuf *pix;
 
87
    gboolean has_video;
 
88
    gchar *title, *album, *artist, *year;
 
89
    gchar *message;
 
90
    ParoleMediaType media_type;
 
91
    
 
92
    g_object_get (G_OBJECT (stream), 
 
93
                  "title", &title,
 
94
                  "album", &album,
 
95
                  "artist", &artist,
 
96
                  "year", &year,
 
97
                  "has-video", &has_video,
 
98
          "media-type", &media_type,              
 
99
                  NULL);
 
100
                  
 
101
    if ( has_video )
 
102
    return;
 
103
 
 
104
    if ( !title )
 
105
    {
 
106
        gchar *uri;
 
107
        gchar *filename;
 
108
        g_object_get (G_OBJECT (stream),
 
109
                      "uri", &uri,
 
110
                      NULL);
 
111
                      
 
112
        filename = g_filename_from_uri (uri, NULL, NULL);
 
113
        g_free (uri);
 
114
        if ( filename )
 
115
        {
 
116
            title  = g_path_get_basename (filename);
 
117
            g_free (filename);
 
118
            if ( !title )
 
119
                return;
 
120
        }
 
121
    }
 
122
    
 
123
    if (!album)
 
124
        album = g_strdup( _("Unknown Album") );
 
125
    if (!artist)
 
126
        artist = g_strdup( _("Unknown Artist") );
 
127
    
 
128
    if (!year)
 
129
    message = g_strdup_printf ("%s %s\n%s %s", _("<i>on</i>"), album, _("<i>by</i>"), artist);
 
130
    else
 
131
    {
 
132
    message = g_strdup_printf ("%s %s (%s)\n%s %s", _("<i>on</i>"), album, year, _("<i>by</i>"), artist);
 
133
    g_free(year);
 
134
    }
 
135
    
 
136
    g_free(artist);
 
137
    g_free(album);
 
138
    
 
139
#ifdef NOTIFY_CHECK_VERSION
 
140
#if NOTIFY_CHECK_VERSION (0, 7, 0)    
 
141
    notify->notification = notify_notification_new (title, message, NULL);
 
142
#else
 
143
    notify->notification = notify_notification_new (title, message, NULL, NULL);
 
144
#endif
 
145
#else
 
146
    notify->notification = notify_notification_new (title, message, NULL, NULL);
 
147
#endif
 
148
    g_free (title);
 
149
    g_free (message);
 
150
    
 
151
    if (media_type == PAROLE_MEDIA_TYPE_CDDA)
 
152
        pix = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
 
153
                                        "media-cdrom-audio",
 
154
                                        48,
 
155
                                        GTK_ICON_LOOKUP_USE_BUILTIN,
 
156
                                        NULL);
 
157
    else
 
158
        pix  = parole_stream_get_image(G_OBJECT(stream));
 
159
    
 
160
    if (!pix)
 
161
        pix = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
 
162
                                        "audio-x-generic",
 
163
                                        48,
 
164
                                        GTK_ICON_LOOKUP_USE_BUILTIN,
 
165
                                        NULL);
 
166
 
 
167
    if ( pix )
 
168
    {
 
169
        notify_notification_set_icon_from_pixbuf (notify->notification, pix);
 
170
        g_object_unref (pix);
 
171
    }
 
172
    notify_notification_set_urgency (notify->notification, NOTIFY_URGENCY_LOW);
 
173
    notify_notification_set_timeout (notify->notification, 5000);
 
174
    
 
175
    notify_notification_show (notify->notification, NULL);
 
176
    g_signal_connect (notify->notification, "closed",
 
177
                      G_CALLBACK (notification_closed_cb), notify);
 
178
}
 
179
 
 
180
static void
 
181
state_changed_cb (ParoleProviderPlayer *player, const ParoleStream *stream, ParoleState state, NotifyProvider *notify)
 
182
{
 
183
    if ( state == PAROLE_STATE_PLAYING )
 
184
            notify_playing (notify, stream);
 
185
            
 
186
    else if ( state <= PAROLE_STATE_PAUSED )
 
187
            close_notification (notify);
 
188
}
 
189
 
 
190
static gboolean notify_provider_is_configurable (ParoleProviderPlugin *plugin)
 
191
{
 
192
    return FALSE;
 
193
}
 
194
 
 
195
static void
 
196
notify_provider_set_player (ParoleProviderPlugin *plugin, ParoleProviderPlayer *player)
 
197
{
 
198
    NotifyProvider *notify;
 
199
    
 
200
    notify = NOTIFY_PROVIDER (plugin);
 
201
    
 
202
    notify->player = player;
 
203
 
 
204
    notify->notification = NULL;
 
205
    notify_init ("parole-notify");
 
206
                                  
 
207
    g_signal_connect (player, "state_changed", 
 
208
                      G_CALLBACK (state_changed_cb), notify);
 
209
}
 
210
 
 
211
static void
 
212
notify_provider_iface_init (ParoleProviderPluginIface *iface)
 
213
{
 
214
    iface->get_is_configurable = notify_provider_is_configurable;
 
215
    iface->set_player = notify_provider_set_player;
 
216
}
 
217
 
 
218
static void notify_provider_class_init (NotifyProviderClass *klass)
 
219
{
 
220
    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
221
    
 
222
    gobject_class->finalize = notify_provider_finalize;
 
223
}
 
224
 
 
225
static void notify_provider_init (NotifyProvider *provider)
 
226
{
 
227
    provider->player = NULL;
 
228
}
 
229
 
 
230
static void notify_provider_finalize (GObject *object)
 
231
{
 
232
    NotifyProvider *notify;
 
233
    
 
234
    notify = NOTIFY_PROVIDER (object);
 
235
 
 
236
    close_notification (notify);
 
237
 
 
238
    G_OBJECT_CLASS (notify_provider_parent_class)->finalize (object);
 
239
}