1 /*****************************************************************************
2 * growl.m : growl notification plugin
3 *****************************************************************************
6 * Copyright © 2008 the VideoLAN team
9 * Authors: Rafaël Carré <funman@videolanorg>
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.
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.
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.
25 * Growl specific code, ripped from growlnotify:
27 * Copyright (c) The Growl Project, 2004-2005
28 * All rights reserved.
30 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
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.
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.
43 *****************************************************************************/
45 /*****************************************************************************
47 *****************************************************************************/
53 #import <Foundation/Foundation.h>
54 #import <Growl/GrowlDefines.h>
56 #include <vlc_common.h>
57 #include <vlc_plugin.h>
58 #include <vlc_playlist.h>
60 #include <vlc_interface.h>
64 /*****************************************************************************
66 *****************************************************************************/
69 CFDataRef default_icon;
70 NSAutoreleasePool *p_pool;
72 CFStringRef notification_type;
75 /*****************************************************************************
77 *****************************************************************************/
78 static int Open ( vlc_object_t * );
79 static void Close ( vlc_object_t * );
81 static int ItemChange( vlc_object_t *, const char *,
82 vlc_value_t, vlc_value_t, void * );
84 static void RegisterToGrowl( vlc_object_t * );
85 static void NotifyToGrowl( intf_thread_t *, const char *, CFDataRef );
87 static CFDataRef readFile(const char *);
89 /*****************************************************************************
91 ****************************************************************************/
94 set_category( CAT_INTERFACE )
95 set_subcategory( SUBCAT_INTERFACE_CONTROL )
96 set_shortname( "Growl" )
97 set_description( N_("Growl Notification Plugin") )
98 set_capability( "interface", 0 )
99 set_callbacks( Open, Close )
102 /*****************************************************************************
103 * Open: initialize and create stuff
104 *****************************************************************************/
105 static int Open( vlc_object_t *p_this )
107 intf_thread_t *p_intf = (intf_thread_t *)p_this;
110 p_sys = p_intf->p_sys = calloc( 1, sizeof(intf_sys_t) );
114 p_sys->p_pool = [[NSAutoreleasePool alloc] init];
115 p_sys->app_name = CFSTR( "VLC media player" );
116 p_sys->notification_type = CFSTR( "New input playing" );
118 char *data_path = config_GetDataDir ( p_this );
119 char buf[strlen (data_path) + sizeof ("/vlc48x48.png")];
120 snprintf (buf, sizeof (buf), "%s/vlc48x48.png", data_path);
122 p_sys->default_icon = (CFDataRef) readFile( buf );
124 playlist_t *p_playlist = pl_Hold( p_intf );
125 var_AddCallback( p_playlist, "item-current", ItemChange, p_intf );
126 pl_Release( p_intf );
128 RegisterToGrowl( p_this );
132 /*****************************************************************************
133 * Close: destroy interface stuff
134 *****************************************************************************/
135 static void Close( vlc_object_t *p_this )
137 intf_sys_t *p_sys = ((intf_thread_t*)p_this)->p_sys;
139 CFRelease( p_sys->default_icon );
140 CFRelease( p_sys->app_name );
141 CFRelease( p_sys->notification_type );
142 [p_sys->p_pool release];
145 playlist_t *p_playlist = pl_Hold( p_this );
146 var_DelCallback( p_playlist, "item-current", ItemChange, p_this );
147 pl_Release( p_this );
150 /*****************************************************************************
151 * ItemChange: Playlist item change callback
152 *****************************************************************************/
153 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
154 vlc_value_t oldval, vlc_value_t newval, void *param )
156 VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(newval);
158 intf_thread_t *p_intf = (intf_thread_t*)param;
159 char *psz_tmp = NULL;
160 char *psz_title = NULL;
161 char *psz_artist = NULL;
162 char *psz_album = NULL;
163 input_thread_t *p_input;
164 p_input = playlist_CurrentInput( (playlist_t*)p_this );
166 if( !p_input ) return VLC_SUCCESS;
168 char *psz_name = input_item_GetName( input_GetItem( p_input ) );
169 if( p_input->b_dead || !psz_name )
171 /* Not playing anything ... */
173 vlc_object_release( p_input );
178 /* Playing something ... */
179 input_item_t *p_item = input_GetItem( p_input );
181 psz_title = input_item_GetTitleFbName( p_item );
182 if( EMPTY_STR( psz_title ) )
185 vlc_object_release( p_input );
189 psz_artist = input_item_GetArtist( p_item );
190 if( EMPTY_STR( psz_artist ) ) FREENULL( psz_artist );
191 psz_album = input_item_GetAlbum( p_item ) ;
192 if( EMPTY_STR( psz_album ) ) FREENULL( psz_album );
195 if( psz_artist && psz_album )
196 i_ret = asprintf( &psz_tmp, "%s\n%s [%s]",
197 psz_title, psz_artist, psz_album );
198 else if( psz_artist )
199 i_ret = asprintf( &psz_tmp, "%s\n%s", psz_title, psz_artist );
201 i_ret = asprintf( &psz_tmp, "%s", psz_title );
208 vlc_object_release( p_input );
212 char *psz_arturl = input_item_GetArtURL( p_item );
213 CFDataRef art = NULL;
214 if( psz_arturl && !strncmp( psz_arturl, "file://", 7 ) &&
215 decode_URI( psz_arturl + 7 ) )
216 art = (CFDataRef) readFile( psz_arturl + 7 );
223 NotifyToGrowl( p_intf, psz_tmp, art );
225 if( art ) CFRelease( art );
228 vlc_object_release( p_input );
232 /*****************************************************************************
234 *****************************************************************************/
235 static void RegisterToGrowl( vlc_object_t *p_this )
237 intf_sys_t *p_sys = ((intf_thread_t *)p_this)->p_sys;
239 CFArrayRef defaultAndAllNotifications = CFArrayCreate(
240 kCFAllocatorDefault, (const void **)&(p_sys->notification_type), 1,
241 &kCFTypeArrayCallBacks );
243 CFTypeRef registerKeys[4] = {
245 GROWL_NOTIFICATIONS_ALL,
246 GROWL_NOTIFICATIONS_DEFAULT,
250 CFTypeRef registerValues[4] = {
252 defaultAndAllNotifications,
253 defaultAndAllNotifications,
257 CFDictionaryRef registerInfo = CFDictionaryCreate(
258 kCFAllocatorDefault, registerKeys, registerValues, 4,
259 &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks );
261 CFRelease( defaultAndAllNotifications );
263 CFNotificationCenterPostNotificationWithOptions(
264 CFNotificationCenterGetDistributedCenter(),
265 (CFStringRef)GROWL_APP_REGISTRATION, NULL, registerInfo,
266 kCFNotificationPostToAllSessions );
267 CFRelease( registerInfo );
270 static void NotifyToGrowl( intf_thread_t *p_intf, const char *psz_desc, CFDataRef art )
272 intf_sys_t *p_sys = p_intf->p_sys;
274 CFStringRef title = CFStringCreateWithCString( kCFAllocatorDefault, _("Now playing"), kCFStringEncodingUTF8 );
275 CFStringRef desc = CFStringCreateWithCString( kCFAllocatorDefault, psz_desc, kCFStringEncodingUTF8 );
277 CFMutableDictionaryRef notificationInfo = CFDictionaryCreateMutable(
278 kCFAllocatorDefault, 5, &kCFTypeDictionaryKeyCallBacks,
279 &kCFTypeDictionaryValueCallBacks);
281 CFDictionarySetValue( notificationInfo, GROWL_NOTIFICATION_NAME, p_sys->notification_type );
282 CFDictionarySetValue( notificationInfo, GROWL_APP_NAME, p_sys->app_name );
283 CFDictionarySetValue( notificationInfo, GROWL_NOTIFICATION_TITLE, title );
284 CFDictionarySetValue( notificationInfo, GROWL_NOTIFICATION_DESCRIPTION, desc );
286 CFDictionarySetValue( notificationInfo, GROWL_NOTIFICATION_ICON,
287 art ? art : p_sys->default_icon );
292 CFNotificationCenterPostNotificationWithOptions(
293 CFNotificationCenterGetDistributedCenter(),
294 (CFStringRef)GROWL_NOTIFICATION, NULL, notificationInfo,
295 kCFNotificationPostToAllSessions );
297 CFRelease( notificationInfo );
300 /* Ripped from CFGrowlAdditions.c
301 * Strangely, this function does exist in Growl shared library, but is not
302 * defined in public header files */
303 static CFDataRef readFile(const char *filename)
306 // read the file into a CFDataRef
307 FILE *fp = fopen(filename, "r");
311 fseek(fp, 0, SEEK_END);
312 long dataLength = ftell(fp);
313 fseek(fp, 0, SEEK_SET);
314 unsigned char *fileData = malloc(dataLength);
315 fread(fileData, 1, dataLength, fp);
317 return CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, fileData,
318 dataLength, kCFAllocatorMalloc);