]> git.sesse.net Git - vlc/blob - modules/notify/growl.m
e901e3dbe95ac3515f9e12c22c44ef7d11163bbc
[vlc] / modules / notify / growl.m
1 /*****************************************************************************
2  * growl.m : growl notification plugin
3  *****************************************************************************
4  * VLC specific code:
5  *
6  * Copyright © 2008,2011,2012 the VideoLAN team
7  * $Id$
8  *
9  * Authors: Rafaël Carré <funman@videolanorg>
10  *          Felix Paul Kühne <fkuehne@videolan.org
11  *
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.
16  *
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.
21  *
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.
25  *
26  * Growl specific code, ripped from growlnotify:
27  *
28  * Copyright (c) The Growl Project, 2004-2005
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
32  *
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.
41  *
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.
43  *
44  *****************************************************************************/
45
46 /*****************************************************************************
47  * Preamble
48  *****************************************************************************/
49
50 #ifdef HAVE_CONFIG_H
51 # include "config.h"
52 #endif
53
54 #import <Foundation/Foundation.h>
55 #import <Growl/Growl.h>
56
57 #include <vlc_common.h>
58 #include <vlc_plugin.h>
59 #include <vlc_playlist.h>
60 #include <vlc_input.h>
61 #include <vlc_meta.h>
62 #include <vlc_interface.h>
63 #include <vlc_url.h>
64
65
66 /*****************************************************************************
67  * intf_sys_t, VLCGrowlDelegate
68  *****************************************************************************/
69 @interface VLCGrowlDelegate : NSObject <GrowlApplicationBridgeDelegate>
70 {
71     NSString *o_applicationName;
72     NSString *o_notificationType;
73     NSMutableDictionary *o_registrationDictionary;
74 }
75
76 - (void)registerToGrowl;
77 - (void)notifyWithDescription: (const char *)psz_desc
78                        artUrl: (const char *)psz_arturl;
79 @end
80
81 struct intf_sys_t
82 {
83     VLCGrowlDelegate *o_growl_delegate;
84     int             i_id;
85     int             i_item_changes;
86 };
87
88 /*****************************************************************************
89  * Local prototypes
90  *****************************************************************************/
91 static int  Open    ( vlc_object_t * );
92 static void Close   ( vlc_object_t * );
93
94 static int ItemChange( vlc_object_t *, const char *,
95                       vlc_value_t, vlc_value_t, void * );
96
97 /*****************************************************************************
98  * Module descriptor
99  ****************************************************************************/
100
101 vlc_module_begin ()
102 set_category( CAT_INTERFACE )
103 set_subcategory( SUBCAT_INTERFACE_CONTROL )
104 set_shortname( "Growl" )
105 set_description( N_("Growl Notification Plugin") )
106 set_capability( "interface", 0 )
107 set_callbacks( Open, Close )
108 vlc_module_end ()
109
110 /*****************************************************************************
111  * Open: initialize and create stuff
112  *****************************************************************************/
113 static int Open( vlc_object_t *p_this )
114 {
115     intf_thread_t *p_intf = (intf_thread_t *)p_this;
116     playlist_t *p_playlist;
117     intf_sys_t    *p_sys;
118
119     p_sys = p_intf->p_sys = calloc( 1, sizeof(intf_sys_t) );
120     if( !p_sys )
121         return VLC_ENOMEM;
122
123     p_sys->o_growl_delegate = [[VLCGrowlDelegate alloc] init];
124     if( !p_sys->o_growl_delegate )
125       return VLC_ENOMEM;
126
127     p_playlist = pl_Get( p_intf );
128     var_AddCallback( p_playlist, "item-change", ItemChange, p_intf );
129     var_AddCallback( p_playlist, "input-current", ItemChange, p_intf );
130
131     [p_sys->o_growl_delegate registerToGrowl];
132     return VLC_SUCCESS;
133 }
134
135 /*****************************************************************************
136  * Close: destroy interface stuff
137  *****************************************************************************/
138 static void Close( vlc_object_t *p_this )
139 {
140     intf_thread_t *p_intf = (intf_thread_t *)p_this;
141     playlist_t *p_playlist = pl_Get( p_intf );
142     intf_sys_t *p_sys = p_intf->p_sys;
143
144     var_DelCallback( p_playlist, "item-change", ItemChange, p_intf );
145     var_DelCallback( p_playlist, "input-current", ItemChange, p_intf );
146
147     [p_sys->o_growl_delegate release];
148     free( p_sys );
149 }
150
151 /*****************************************************************************
152  * ItemChange: Playlist item change callback
153  *****************************************************************************/
154 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
155                       vlc_value_t oldval, vlc_value_t newval, void *param )
156 {
157     VLC_UNUSED(oldval);
158
159     intf_thread_t *p_intf = (intf_thread_t *)param;
160     char *psz_tmp           = NULL;
161     char *psz_title         = NULL;
162     char *psz_artist        = NULL;
163     char *psz_album         = NULL;
164     input_item_t *p_item = newval.p_address;
165
166     bool b_is_item_current = !strcmp( "input-current", psz_var );
167
168     /* Don't update each time an item has been preparsed */
169     if( b_is_item_current )
170     { /* stores the current input item id */
171         input_thread_t *p_input = newval.p_address;
172         if( !p_input )
173             return VLC_SUCCESS;
174
175         p_item = input_GetItem( p_input );
176         if( p_intf->p_sys->i_id != p_item->i_id )
177         {
178             p_intf->p_sys->i_id = p_item->i_id;
179             p_intf->p_sys->i_item_changes = 0;
180         }
181
182         return VLC_SUCCESS;
183     }
184     /* ignore items which weren't pre-parsed yet */
185     else if( !input_item_IsPreparsed(p_item) )
186         return VLC_SUCCESS;
187     else
188     {   /* "item-change" */
189         
190         if( p_item->i_id != p_intf->p_sys->i_id )
191             return VLC_SUCCESS;
192
193         /* Some variable bitrate inputs call "item-change" callbacks each time
194          * their length is updated, that is several times per second.
195          * We'll limit the number of changes to 1 per input. */
196         if( p_intf->p_sys->i_item_changes > 0 )
197             return VLC_SUCCESS;
198
199         p_intf->p_sys->i_item_changes++;
200     }
201
202     /* Playing something ... */
203     if( input_item_GetNowPlayingFb( p_item ) )
204         psz_title = input_item_GetNowPlayingFb( p_item );
205     else
206         psz_title = input_item_GetTitleFbName( p_item );
207     if( EMPTY_STR( psz_title ) )
208     {
209         free( psz_title );
210         return VLC_SUCCESS;
211     }
212
213     psz_artist = input_item_GetArtist( p_item );
214     if( EMPTY_STR( psz_artist ) ) FREENULL( psz_artist );
215     psz_album = input_item_GetAlbum( p_item ) ;
216     if( EMPTY_STR( psz_album ) ) FREENULL( psz_album );
217
218     int i_ret;
219     if( psz_artist && psz_album )
220         i_ret = asprintf( &psz_tmp, "%s\n%s [%s]",
221                          psz_title, psz_artist, psz_album );
222     else if( psz_artist )
223         i_ret = asprintf( &psz_tmp, "%s\n%s", psz_title, psz_artist );
224     else
225         i_ret = asprintf(&psz_tmp, "%s", psz_title );
226
227     if( i_ret == -1 )
228     {
229         free( psz_title );
230         free( psz_artist );
231         free( psz_album );
232         return VLC_ENOMEM;
233     }
234
235     char *psz_arturl = input_item_GetArtURL( p_item );
236     if( psz_arturl )
237     {
238         char *psz = make_path( psz_arturl );
239         free( psz_arturl );
240         psz_arturl = psz;
241     }
242
243     [p_intf->p_sys->o_growl_delegate notifyWithDescription: psz_tmp artUrl: psz_arturl];
244
245     free( psz_title );
246     free( psz_artist );
247     free( psz_album );
248     free( psz_arturl );
249     free( psz_tmp );
250
251     return VLC_SUCCESS;
252 }
253
254 /*****************************************************************************
255  * VLCGrowlDelegate
256  *****************************************************************************/
257 @implementation VLCGrowlDelegate
258
259 - (id)init
260 {
261     if( !( self = [super init] ) )
262         return nil;
263
264     o_applicationName = nil;
265     o_notificationType = nil;
266     o_registrationDictionary = nil;
267
268     return self;
269 }
270
271 - (void)dealloc
272 {
273     [o_applicationName release];
274     [o_notificationType release];
275     [o_registrationDictionary release];
276     [super dealloc];
277 }
278
279 - (void)registerToGrowl
280 {
281     o_applicationName = [[NSString alloc] initWithUTF8String: _( "VLC media player" )];
282     o_notificationType = [[NSString alloc] initWithUTF8String: _( "New input playing" )];
283
284     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
285     NSArray *o_defaultAndAllNotifications = [NSArray arrayWithObject: o_notificationType];
286
287     o_registrationDictionary = [[NSMutableDictionary alloc] init];
288     [o_registrationDictionary setObject: o_defaultAndAllNotifications
289                                  forKey: GROWL_NOTIFICATIONS_ALL];
290     [o_registrationDictionary setObject: o_defaultAndAllNotifications
291                                  forKey: GROWL_NOTIFICATIONS_DEFAULT];
292
293     [GrowlApplicationBridge setGrowlDelegate: self];
294     [o_pool drain];
295 }
296
297 - (void)notifyWithDescription: (const char *)psz_desc artUrl: (const char *)psz_arturl
298 {
299     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
300     NSData *o_art = nil;
301
302     if( psz_arturl )
303         o_art = [NSData dataWithContentsOfFile: [NSString stringWithUTF8String: psz_arturl]];
304
305     [GrowlApplicationBridge notifyWithTitle: [NSString stringWithUTF8String: _( "Now playing" )]
306                                 description: [NSString stringWithUTF8String: psz_desc]
307                            notificationName: o_notificationType
308                                    iconData: o_art
309                                    priority: 0
310                                    isSticky: NO
311                                clickContext: nil];
312     [o_pool drain];
313 }
314
315 /*****************************************************************************
316  * Delegate methods
317  *****************************************************************************/
318 - (NSDictionary *)registrationDictionaryForGrowl
319 {
320     return o_registrationDictionary;
321 }
322
323 - (NSString *)applicationNameForGrowl
324 {
325     return o_applicationName;
326 }
327 @end