1 /*****************************************************************************
2 * growl.m : growl notification plugin
3 *****************************************************************************
6 * Copyright © 2008,2011,2012 the VideoLAN team
9 * Authors: Rafaël Carré <funman@videolanorg>
10 * Felix Paul Kühne <fkuehne@videolan.org
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 * Growl specific code, ripped from growlnotify:
28 * Copyright (c) The Growl Project, 2004-2005
29 * All rights reserved.
31 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
33 * 1. Redistributions of source code must retain the above copyright
34 notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 notice, this list of conditions and the following disclaimer in the
37 documentation and/or other materials provided with the distribution.
38 * 3. Neither the name of Growl nor the names of its contributors
39 may be used to endorse or promote products derived from this software
40 without specific prior written permission.
42 * 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.
44 *****************************************************************************/
46 /*****************************************************************************
48 *****************************************************************************/
54 #import <Foundation/Foundation.h>
55 #import <Growl/Growl.h>
57 #include <vlc_common.h>
58 #include <vlc_plugin.h>
59 #include <vlc_playlist.h>
61 #include <vlc_interface.h>
65 /*****************************************************************************
66 * intf_sys_t, VLCGrowlDelegate
67 *****************************************************************************/
68 @interface VLCGrowlDelegate : NSObject <GrowlApplicationBridgeDelegate>
70 NSString *o_applicationName;
71 NSString *o_notificationType;
72 NSMutableDictionary *o_registrationDictionary;
75 - (void)registerToGrowl;
76 - (void)notifyWithDescription: (const char *)psz_desc
77 artUrl: (const char *)psz_arturl;
82 VLCGrowlDelegate *o_growl_delegate;
87 /*****************************************************************************
89 *****************************************************************************/
90 static int Open ( vlc_object_t * );
91 static void Close ( vlc_object_t * );
93 static int ItemChange( vlc_object_t *, const char *,
94 vlc_value_t, vlc_value_t, void * );
96 /*****************************************************************************
98 ****************************************************************************/
101 set_category( CAT_INTERFACE )
102 set_subcategory( SUBCAT_INTERFACE_CONTROL )
103 set_shortname( "Growl" )
104 set_description( N_("Growl Notification Plugin") )
105 set_capability( "interface", 0 )
106 set_callbacks( Open, Close )
109 /*****************************************************************************
110 * Open: initialize and create stuff
111 *****************************************************************************/
112 static int Open( vlc_object_t *p_this )
114 intf_thread_t *p_intf = (intf_thread_t *)p_this;
115 playlist_t *p_playlist;
118 p_sys = p_intf->p_sys = calloc( 1, sizeof(intf_sys_t) );
122 p_sys->o_growl_delegate = [[VLCGrowlDelegate alloc] init];
123 if( !p_sys->o_growl_delegate )
126 p_playlist = pl_Get( p_intf );
127 var_AddCallback( p_playlist, "item-change", ItemChange, p_intf );
128 var_AddCallback( p_playlist, "item-current", ItemChange, p_intf );
130 [p_sys->o_growl_delegate registerToGrowl];
134 /*****************************************************************************
135 * Close: destroy interface stuff
136 *****************************************************************************/
137 static void Close( vlc_object_t *p_this )
139 intf_thread_t *p_intf = (intf_thread_t *)p_this;
140 playlist_t *p_playlist = pl_Get( p_this );
141 intf_sys_t *p_sys = p_intf->p_sys;
143 var_DelCallback( p_playlist, "item-change", ItemChange, p_intf );
144 var_DelCallback( p_playlist, "item-current", ItemChange, p_intf );
146 [p_sys->o_growl_delegate release];
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 )
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_item_t *p_item = newval.p_address;
165 bool b_is_item_current = !strcmp( "item-current", psz_var );
167 /* Don't update each time an item has been preparsed */
168 if( b_is_item_current )
169 { /* stores the current input item id */
170 if( p_intf->p_sys->i_id != p_item->i_id )
172 p_intf->p_sys->i_id = p_item->i_id;
173 p_intf->p_sys->i_item_changes = 0;
177 /* ignore items which weren't pre-parsed yet */
178 else if( !input_item_IsPreparsed(p_item) )
181 { /* "item-change" */
183 if( p_item->i_id != p_intf->p_sys->i_id )
186 /* Some variable bitrate inputs call "item-change" callbacks each time
187 * their length is updated, that is several times per second.
188 * We'll limit the number of changes to 1 per input. */
189 if( p_intf->p_sys->i_item_changes > 0 )
192 p_intf->p_sys->i_item_changes++;
195 /* Playing something ... */
196 if( input_item_GetNowPlaying( p_item ) )
197 psz_title = input_item_GetNowPlaying( p_item );
199 psz_title = input_item_GetTitleFbName( p_item );
200 if( EMPTY_STR( psz_title ) )
206 psz_artist = input_item_GetArtist( p_item );
207 if( EMPTY_STR( psz_artist ) ) FREENULL( psz_artist );
208 psz_album = input_item_GetAlbum( p_item ) ;
209 if( EMPTY_STR( psz_album ) ) FREENULL( psz_album );
212 if( psz_artist && psz_album )
213 i_ret = asprintf( &psz_tmp, "%s\n%s [%s]",
214 psz_title, psz_artist, psz_album );
215 else if( psz_artist )
216 i_ret = asprintf( &psz_tmp, "%s\n%s", psz_title, psz_artist );
218 i_ret = asprintf(&psz_tmp, "%s", psz_title );
228 char *psz_arturl = input_item_GetArtURL( p_item );
231 char *psz = make_path( psz_arturl );
236 [p_intf->p_sys->o_growl_delegate notifyWithDescription: psz_tmp artUrl: psz_arturl];
247 /*****************************************************************************
249 *****************************************************************************/
250 @implementation VLCGrowlDelegate
254 if( !( self = [super init] ) )
257 o_applicationName = nil;
258 o_notificationType = nil;
259 o_registrationDictionary = nil;
266 [o_applicationName release];
267 [o_notificationType release];
268 [o_registrationDictionary release];
272 - (void)registerToGrowl
274 o_applicationName = [[NSString alloc] initWithUTF8String: _( "VLC media player" )];
275 o_notificationType = [[NSString alloc] initWithUTF8String: _( "New input playing" )];
277 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
278 NSArray *o_defaultAndAllNotifications = [NSArray arrayWithObject: o_notificationType];
280 o_registrationDictionary = [[NSMutableDictionary alloc] init];
281 [o_registrationDictionary setObject: o_defaultAndAllNotifications
282 forKey: GROWL_NOTIFICATIONS_ALL];
283 [o_registrationDictionary setObject: o_defaultAndAllNotifications
284 forKey: GROWL_NOTIFICATIONS_DEFAULT];
286 [GrowlApplicationBridge setGrowlDelegate: self];
290 - (void)notifyWithDescription: (const char *)psz_desc artUrl: (const char *)psz_arturl
292 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
296 o_art = [NSData dataWithContentsOfFile: [NSString stringWithUTF8String: psz_arturl]];
298 [GrowlApplicationBridge notifyWithTitle: [NSString stringWithUTF8String: _( "Now playing" )]
299 description: [NSString stringWithUTF8String: psz_desc]
300 notificationName: o_notificationType
308 /*****************************************************************************
310 *****************************************************************************/
311 - (NSDictionary *)registrationDictionaryForGrowl
313 return o_registrationDictionary;
316 - (NSString *)applicationNameForGrowl
318 return o_applicationName;