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