]> git.sesse.net Git - vlc/blob - modules/misc/notify/growl.m
growl: fix compilation.
[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_common.h>
57 #include <vlc_plugin.h>
58 #include <vlc_playlist.h>
59 #include <vlc_meta.h>
60 #include <vlc_interface.h>
61
62
63 /*****************************************************************************
64  * intf_sys_t
65  *****************************************************************************/
66 struct intf_sys_t
67 {
68     CFDataRef           default_icon;
69     NSAutoreleasePool   *p_pool;
70     CFStringRef         app_name;
71     CFStringRef         notification_type;
72 };
73
74 /*****************************************************************************
75  * Local prototypes
76  *****************************************************************************/
77 static int  Open    ( vlc_object_t * );
78 static void Close   ( vlc_object_t * );
79
80 static int ItemChange( vlc_object_t *, const char *,
81                        vlc_value_t, vlc_value_t, void * );
82
83 static void RegisterToGrowl( vlc_object_t * );
84 static void NotifyToGrowl( intf_thread_t *, const char *, CFDataRef );
85
86 static CFDataRef readFile(const char *);
87
88 /*****************************************************************************
89  * Module descriptor
90  ****************************************************************************/
91
92 vlc_module_begin ()
93     set_category( CAT_INTERFACE )
94     set_subcategory( SUBCAT_INTERFACE_CONTROL )
95     set_shortname( "Growl" )
96     set_description( N_("Growl Notification Plugin") )
97     set_capability( "interface", 0 )
98     set_callbacks( Open, Close )
99 vlc_module_end ()
100
101 /*****************************************************************************
102  * Open: initialize and create stuff
103  *****************************************************************************/
104 static int Open( vlc_object_t *p_this )
105 {
106     intf_thread_t *p_intf = (intf_thread_t *)p_this;
107     intf_sys_t    *p_sys;
108
109     p_sys = p_intf->p_sys = calloc( 1, sizeof(intf_sys_t) );
110     if( !p_sys )
111         return VLC_ENOMEM;
112
113     p_sys->p_pool = [[NSAutoreleasePool alloc] init];
114     p_sys->app_name = CFSTR( "VLC media player" );
115     p_sys->notification_type = CFSTR( "New input playing" );
116
117     const char *data_path = config_GetDataDir ();
118     char buf[strlen (data_path) + sizeof ("/vlc48x48.png")];
119     snprintf (buf, sizeof (buf), "%s/vlc48x48.png", data_path);
120     p_sys->default_icon = (CFDataRef) readFile( buf );
121
122     playlist_t *p_playlist = pl_Hold( p_intf );
123     var_AddCallback( p_playlist, "item-current", ItemChange, p_intf );
124     pl_Release( p_intf );
125
126     RegisterToGrowl( p_this );
127     return VLC_SUCCESS;
128 }
129
130 /*****************************************************************************
131  * Close: destroy interface stuff
132  *****************************************************************************/
133 static void Close( vlc_object_t *p_this )
134 {
135     intf_sys_t *p_sys = ((intf_thread_t*)p_this)->p_sys;
136
137     CFRelease( p_sys->default_icon );
138     CFRelease( p_sys->app_name );
139     CFRelease( p_sys->notification_type );
140     [p_sys->p_pool release];
141     free( p_sys );
142
143     playlist_t *p_playlist = pl_Hold( p_this );
144     var_DelCallback( p_playlist, "item-current", ItemChange, p_this );
145     pl_Release( p_this );
146 }
147
148 /*****************************************************************************
149  * ItemChange: Playlist item change callback
150  *****************************************************************************/
151 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
152                        vlc_value_t oldval, vlc_value_t newval, void *param )
153 {
154     VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(newval);
155
156     intf_thread_t *p_intf   = (intf_thread_t*)param;
157     char *psz_tmp           = NULL;
158     char *psz_title         = NULL;
159     char *psz_artist        = NULL;
160     char *psz_album         = NULL;
161     input_thread_t *p_input;
162     playlist_t *p_playlist = pl_Hold( p_this );
163
164     p_input = playlist_CurrentInput( p_playlist );
165     pl_Release( p_this );
166
167     if( !p_input ) return VLC_SUCCESS;
168     vlc_object_hold( p_input );
169
170     char *psz_name = input_item_GetName( input_GetItem( p_input ) );
171     if( p_input->b_dead || !psz_name )
172     {
173         /* Not playing anything ... */
174         free( psz_name );
175         vlc_object_release( p_input );
176         return VLC_SUCCESS;
177     }
178     free( psz_name );
179
180     /* Playing something ... */
181     input_item_t *p_item = input_GetItem( p_input );
182
183     psz_title = input_item_GetTitleFbName( p_item );
184     if( EMPTY_STR( psz_title ) )
185     {
186         free( psz_title );
187         vlc_object_release( p_input );
188         return VLC_SUCCESS;
189     }
190
191     psz_artist = input_item_GetArtist( p_item );
192     if( EMPTY_STR( psz_artist ) ) FREENULL( psz_artist );
193     psz_album = input_item_GetAlbum( p_item ) ;
194     if( EMPTY_STR( psz_album ) ) FREENULL( psz_album );
195
196     int i_ret;
197     if( psz_artist && psz_album )
198         i_ret = asprintf( &psz_tmp, "%s\n%s [%s]",
199                 psz_title, psz_artist, psz_album );
200     else if( psz_artist )
201         i_ret = asprintf( &psz_tmp, "%s\n%s", psz_title, psz_artist );
202     else
203         i_ret = asprintf( &psz_tmp, "%s", psz_title );
204
205     if( i_ret == -1 )
206     {
207         free( psz_title );
208         free( psz_artist );
209         free( psz_album );
210         vlc_object_release( p_input );
211         return VLC_ENOMEM;
212     }
213
214     char *psz_arturl = input_item_GetArtURL( p_item );
215     CFDataRef art = NULL;
216     if( psz_arturl && !strncmp( psz_arturl, "file://", 7 ) &&
217                     strlen( psz_arturl ) > 7 )
218         art = (CFDataRef) readFile( psz_arturl + 7 );
219
220     free( psz_title );
221     free( psz_artist );
222     free( psz_album );
223     free( psz_arturl );
224
225     NotifyToGrowl( p_intf, psz_tmp, art );
226
227     if( art ) CFRelease( art );
228
229     vlc_object_release( p_input );
230     return VLC_SUCCESS;
231 }
232
233 /*****************************************************************************
234  * RegisterToGrowl
235  *****************************************************************************/
236 static void RegisterToGrowl( vlc_object_t *p_this )
237 {
238     intf_sys_t *p_sys = ((intf_thread_t *)p_this)->p_sys;
239
240     CFArrayRef defaultAndAllNotifications = CFArrayCreate(
241         kCFAllocatorDefault, (const void **)&(p_sys->notification_type), 1,
242         &kCFTypeArrayCallBacks );
243     
244     CFTypeRef registerKeys[4] = {
245         GROWL_APP_NAME,
246         GROWL_NOTIFICATIONS_ALL,
247         GROWL_NOTIFICATIONS_DEFAULT,
248         GROWL_APP_ICON
249     };
250
251     CFTypeRef registerValues[4] = {
252         p_sys->app_name,
253         defaultAndAllNotifications,
254         defaultAndAllNotifications,
255         p_sys->default_icon
256     };
257
258     CFDictionaryRef registerInfo = CFDictionaryCreate(
259         kCFAllocatorDefault, registerKeys, registerValues, 4,
260         &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks );
261
262     CFRelease( defaultAndAllNotifications );
263
264     CFNotificationCenterPostNotificationWithOptions(
265         CFNotificationCenterGetDistributedCenter(),
266         (CFStringRef)GROWL_APP_REGISTRATION, NULL, registerInfo,
267         kCFNotificationPostToAllSessions );
268     CFRelease( registerInfo );
269 }
270
271 static void NotifyToGrowl( intf_thread_t *p_intf, const char *psz_desc, CFDataRef art )
272 {
273     intf_sys_t *p_sys = p_intf->p_sys;
274
275     CFStringRef title = CFStringCreateWithCString( kCFAllocatorDefault, _("Now playing"), kCFStringEncodingUTF8 );
276     CFStringRef desc = CFStringCreateWithCString( kCFAllocatorDefault, psz_desc, kCFStringEncodingUTF8 );
277
278     CFMutableDictionaryRef notificationInfo = CFDictionaryCreateMutable(
279         kCFAllocatorDefault, 5, &kCFTypeDictionaryKeyCallBacks,
280         &kCFTypeDictionaryValueCallBacks);
281
282     CFDictionarySetValue( notificationInfo, GROWL_NOTIFICATION_NAME, p_sys->notification_type );
283     CFDictionarySetValue( notificationInfo, GROWL_APP_NAME, p_sys->app_name );
284     CFDictionarySetValue( notificationInfo, GROWL_NOTIFICATION_TITLE, title );
285     CFDictionarySetValue( notificationInfo, GROWL_NOTIFICATION_DESCRIPTION, desc );
286
287     CFDictionarySetValue( notificationInfo, GROWL_NOTIFICATION_ICON, 
288         art ? art : p_sys->default_icon );
289
290     CFRelease( title );
291     CFRelease( desc );
292
293     CFNotificationCenterPostNotificationWithOptions(
294         CFNotificationCenterGetDistributedCenter(),
295         (CFStringRef)GROWL_NOTIFICATION, NULL, notificationInfo,
296         kCFNotificationPostToAllSessions );
297
298     CFRelease( notificationInfo );
299 }
300
301 /* Ripped from CFGrowlAdditions.c 
302  * Strangely, this function does exist in Growl shared library, but is not
303  * defined in public header files */
304 static CFDataRef readFile(const char *filename)
305 {
306     CFDataRef data;
307     // read the file into a CFDataRef
308     FILE *fp = fopen(filename, "r");
309     if( !fp )
310     return NULL;
311
312     fseek(fp, 0, SEEK_END);
313     long dataLength = ftell(fp);
314     fseek(fp, 0, SEEK_SET);
315     unsigned char *fileData = malloc(dataLength);
316     fread(fileData, 1, dataLength, fp);
317     fclose(fp);
318     return CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, fileData,
319         dataLength, kCFAllocatorMalloc);
320 }
321