]> git.sesse.net Git - vlc/blob - modules/misc/notify/growl.m
new growl plugin: local only, supports album art (you'll need the Growl Framework)
[vlc] / modules / misc / notify / growl.m
1 /*****************************************************************************
2  * growl.m : growl notification plugin
3  *****************************************************************************
4  * VLC specific code:
5  * 
6  * Copyright © 2008 the VideoLAN team
7  * $Id$
8  *
9  * Authors: Rafaël Carré <funman@videolanorg>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *
25  * Growl specific code, ripped from growlnotify:
26  *
27  * Copyright (c) The Growl Project, 2004-2005
28  * All rights reserved.
29  *
30  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
31  *
32  * 1. Redistributions of source code must retain the above copyright
33  notice, this list of conditions and the following disclaimer.
34  * 2. Redistributions in binary form must reproduce the above copyright
35  notice, this list of conditions and the following disclaimer in the
36  documentation and/or other materials provided with the distribution.
37  * 3. Neither the name of Growl nor the names of its contributors
38  may be used to endorse or promote products derived from this software
39  without specific prior written permission.
40  *
41  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  *****************************************************************************/
44
45 /*****************************************************************************
46  * Preamble
47  *****************************************************************************/
48
49 #ifdef HAVE_CONFIG_H
50 # include "config.h"
51 #endif
52
53 #import <Foundation/Foundation.h>
54 #import <Growl/GrowlDefines.h>
55
56 #include <vlc/vlc.h>
57 #include <vlc_playlist.h>
58 #include <vlc_meta.h>
59 #include <vlc_interface.h>
60
61
62 /*****************************************************************************
63  * intf_sys_t
64  *****************************************************************************/
65 struct intf_sys_t
66 {
67     CFDataRef           default_icon;
68     NSAutoreleasePool   *p_pool;
69     CFStringRef         app_name;
70     CFStringRef         notification_type;
71 };
72
73 /*****************************************************************************
74  * Local prototypes
75  *****************************************************************************/
76 static int  Open    ( vlc_object_t * );
77 static void Close   ( vlc_object_t * );
78
79 static int ItemChange( vlc_object_t *, const char *,
80                        vlc_value_t, vlc_value_t, void * );
81
82 static void RegisterToGrowl( vlc_object_t * );
83 static void NotifyToGrowl( intf_thread_t *, const char *, CFDataRef );
84
85 static CFDataRef readFile(const char *);
86
87 /*****************************************************************************
88  * Module descriptor
89  ****************************************************************************/
90
91 vlc_module_begin();
92     set_category( CAT_INTERFACE );
93     set_subcategory( SUBCAT_INTERFACE_CONTROL );
94     set_shortname( "Growl" );
95     set_description( _("Growl Notification Plugin") );
96     set_capability( "interface", 0 );
97     set_callbacks( Open, Close );
98 vlc_module_end();
99
100 /*****************************************************************************
101  * Open: initialize and create stuff
102  *****************************************************************************/
103 static int Open( vlc_object_t *p_this )
104 {
105     intf_thread_t *p_intf = (intf_thread_t *)p_this;
106     intf_sys_t    *p_sys;
107
108     p_sys = p_intf->p_sys = calloc( sizeof(intf_sys_t), 1);
109     if( !p_sys )
110         return VLC_ENOMEM;
111
112     p_sys->p_pool = [[NSAutoreleasePool alloc] init];
113     p_sys->app_name = CFSTR( "VLC media player" );
114     p_sys->notification_type = CFSTR( "New input playing" );
115
116     const char *data_path = config_GetDataDir ();
117     char buf[strlen (data_path) + sizeof ("/vlc48x48.png")];
118     snprintf (buf, sizeof (buf), "%s/vlc48x48.png", data_path);
119     p_sys->default_icon = (CFDataRef) readFile( buf );
120
121     playlist_t *p_playlist = pl_Yield( p_intf );
122     var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
123     pl_Release( p_intf );
124
125     RegisterToGrowl( p_this );
126     return VLC_SUCCESS;
127 }
128
129 /*****************************************************************************
130  * Close: destroy interface stuff
131  *****************************************************************************/
132 static void Close( vlc_object_t *p_this )
133 {
134     intf_sys_t *p_sys = ((intf_thread_t*)p_this)->p_sys;
135
136     CFRelease( p_sys->default_icon );
137     CFRelease( p_sys->app_name );
138     CFRelease( p_sys->notification_type );
139     [p_sys->p_pool release];
140     free( p_sys );
141
142     playlist_t *p_playlist = pl_Yield( p_this );
143     var_DelCallback( p_playlist, "playlist-current", ItemChange, p_this );
144     pl_Release( p_this );
145 }
146
147 /*****************************************************************************
148  * ItemChange: Playlist item change callback
149  *****************************************************************************/
150 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
151                        vlc_value_t oldval, vlc_value_t newval, void *param )
152 {
153     VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(newval);
154
155     intf_thread_t *p_intf   = (intf_thread_t*)param;
156     char *psz_tmp           = NULL;
157     char *psz_title         = NULL;
158     char *psz_artist        = NULL;
159     char *psz_album         = NULL;
160     input_thread_t *p_input;
161     playlist_t *p_playlist = pl_Yield( p_this );
162
163     p_input = p_playlist->p_input;
164     vlc_object_release( p_playlist );
165
166     if( !p_input ) return VLC_SUCCESS;
167     vlc_object_yield( p_input );
168
169     char *psz_name = input_item_GetName( input_GetItem( p_input ) );
170     if( p_input->b_dead || !psz_name )
171     {
172         /* Not playing anything ... */
173         free( psz_name );
174         vlc_object_release( p_input );
175         return VLC_SUCCESS;
176     }
177     free( psz_name );
178
179     /* Playing something ... */
180     input_item_t *p_item = input_GetItem( p_input );
181
182     psz_title = input_item_GetTitle( p_item );
183     if( psz_title == NULL || EMPTY_STR( psz_title ) )
184     {
185         free( psz_title );
186         psz_title = input_item_GetName( input_GetItem( p_input ) );
187         if( psz_title == NULL || EMPTY_STR( psz_title ) )
188         {
189             free( psz_title );
190             vlc_object_release( p_input );
191             return VLC_SUCCESS;
192         }
193     }
194
195     psz_artist = input_item_GetArtist( p_item );
196     if( EMPTY_STR( psz_artist ) ) FREENULL( psz_artist );
197     psz_album = input_item_GetAlbum( p_item ) ;
198     if( EMPTY_STR( psz_album ) ) FREENULL( psz_album );
199
200     int i_ret;
201     if( psz_artist && psz_album )
202         i_ret = asprintf( &psz_tmp, "%s\n%s [%s]",
203                 psz_title, psz_artist, psz_album );
204     else if( psz_artist )
205         i_ret = asprintf( &psz_tmp, "%s\n%s", psz_title, psz_artist );
206     else
207         i_ret = asprintf( &psz_tmp, "%s", psz_title );
208
209     if( i_ret == -1 )
210     {
211         free( psz_title );
212         free( psz_artist );
213         free( psz_album );
214         vlc_object_release( p_input );
215         return VLC_ENOMEM;
216     }
217
218     char *psz_arturl = input_item_GetArtURL( p_item );
219     CFDataRef art = NULL;
220     if( psz_arturl && !strncmp( psz_arturl, "file://", 7 ) &&
221                     strlen( psz_arturl ) > 7 )
222         art = (CFDataRef) readFile( psz_arturl + 7 );
223
224     free( psz_title );
225     free( psz_artist );
226     free( psz_album );
227     free( psz_arturl );
228
229     NotifyToGrowl( p_intf, psz_tmp, art );
230
231     if( art ) CFRelease( art );
232
233     vlc_object_release( p_input );
234     return VLC_SUCCESS;
235 }
236
237 /*****************************************************************************
238  * RegisterToGrowl
239  *****************************************************************************/
240 static void RegisterToGrowl( vlc_object_t *p_this )
241 {
242     intf_sys_t *p_sys = ((intf_thread_t *)p_this)->p_sys;
243
244     CFArrayRef defaultAndAllNotifications = CFArrayCreate(
245         kCFAllocatorDefault, (const void **)&(p_sys->notification_type), 1,
246         &kCFTypeArrayCallBacks );
247     
248     CFTypeRef registerKeys[4] = {
249         GROWL_APP_NAME,
250         GROWL_NOTIFICATIONS_ALL,
251         GROWL_NOTIFICATIONS_DEFAULT,
252         GROWL_APP_ICON
253     };
254
255     CFTypeRef registerValues[4] = {
256         p_sys->app_name,
257         defaultAndAllNotifications,
258         defaultAndAllNotifications,
259         p_sys->default_icon
260     };
261
262     CFDictionaryRef registerInfo = CFDictionaryCreate(
263         kCFAllocatorDefault, registerKeys, registerValues, 4,
264         &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks );
265
266     CFRelease( defaultAndAllNotifications );
267
268     CFNotificationCenterPostNotificationWithOptions(
269         CFNotificationCenterGetDistributedCenter(),
270         (CFStringRef)GROWL_APP_REGISTRATION, NULL, registerInfo,
271         kCFNotificationPostToAllSessions );
272     CFRelease( registerInfo );
273 }
274
275 static void NotifyToGrowl( intf_thread_t *p_intf, const char *psz_desc, CFDataRef art )
276 {
277     intf_sys_t *p_sys = p_intf->p_sys;
278
279     CFStringRef title = CFStringCreateWithCString( kCFAllocatorDefault, _("Now playing"), kCFStringEncodingUTF8 );
280     CFStringRef desc = CFStringCreateWithCString( kCFAllocatorDefault, psz_desc, kCFStringEncodingUTF8 );
281
282     CFMutableDictionaryRef notificationInfo = CFDictionaryCreateMutable(
283         kCFAllocatorDefault, 5, &kCFTypeDictionaryKeyCallBacks,
284         &kCFTypeDictionaryValueCallBacks);
285
286     CFDictionarySetValue( notificationInfo, GROWL_NOTIFICATION_NAME, p_sys->notification_type );
287     CFDictionarySetValue( notificationInfo, GROWL_APP_NAME, p_sys->app_name );
288     CFDictionarySetValue( notificationInfo, GROWL_NOTIFICATION_TITLE, title );
289     CFDictionarySetValue( notificationInfo, GROWL_NOTIFICATION_DESCRIPTION, desc );
290
291     CFDictionarySetValue( notificationInfo, GROWL_NOTIFICATION_ICON, 
292         art ? art : p_sys->default_icon );
293
294     CFRelease( title );
295     CFRelease( desc );
296
297     CFNotificationCenterPostNotificationWithOptions(
298         CFNotificationCenterGetDistributedCenter(),
299         (CFStringRef)GROWL_NOTIFICATION, NULL, notificationInfo,
300         kCFNotificationPostToAllSessions );
301
302     CFRelease( notificationInfo );
303 }
304
305 /* Ripped from CFGrowlAdditions.c 
306  * Strangely, this function does exist in Growl shared library, but is not
307  * defined in public header files */
308 static CFDataRef readFile(const char *filename)
309 {
310     CFDataRef data;
311     // read the file into a CFDataRef
312     FILE *fp = fopen(filename, "r");
313     if( !fp )
314     return NULL;
315
316     fseek(fp, 0, SEEK_END);
317     long dataLength = ftell(fp);
318     fseek(fp, 0, SEEK_SET);
319     unsigned char *fileData = malloc(dataLength);
320     fread(fileData, 1, dataLength, fp);
321     fclose(fp);
322     return CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, fileData,
323         dataLength, kCFAllocatorMalloc);
324 }
325