]> git.sesse.net Git - vlc/blob - modules/gui/macosx/applescript.m
macosx/open: added missing sanity checks
[vlc] / modules / gui / macosx / applescript.m
1 /*****************************************************************************
2  * applescript.m: MacOS X AppleScript support
3  *****************************************************************************
4  * Copyright (C) 2002-2012 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Derk-Jan Hartman <thedj@users.sourceforge.net>
8  *          Felix Paul Kühne <fkuehne at videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include "intf.h"
29 #include "applescript.h"
30 #include "CoreInteraction.h"
31 #include "vlc_aout_intf.h"
32
33 /*****************************************************************************
34  * VLGetURLScriptCommand implementation
35  *****************************************************************************/
36 @implementation VLGetURLScriptCommand
37
38 - (id)performDefaultImplementation {
39     NSString *o_command = [[self commandDescription] commandName];
40     NSString *o_urlString = [self directParameter];
41
42     if ( [o_command isEqualToString:@"GetURL"] || [o_command isEqualToString:@"OpenURL"] )
43     {
44         intf_thread_t * p_intf = VLCIntf;
45         playlist_t * p_playlist = pl_Get( p_intf );
46         if( p_playlist == NULL )
47         {
48             return nil;
49         }
50
51         if ( o_urlString )
52         {
53             NSURL * o_url;
54             input_item_t *p_input;
55
56             p_input = input_item_New( [o_urlString fileSystemRepresentation],
57                                     [[[NSFileManager defaultManager]
58                                     displayNameAtPath: o_urlString] UTF8String] );
59             /* FIXME: playlist_AddInput() can fail */
60             playlist_AddInput( p_playlist, p_input, PLAYLIST_INSERT,
61                                PLAYLIST_END, true, pl_Unlocked );
62
63             vlc_gc_decref( p_input );
64
65             o_url = [NSURL fileURLWithPath: o_urlString];
66             if( o_url != nil )
67             {
68                 [[NSDocumentController sharedDocumentController]
69                     noteNewRecentDocumentURL: o_url];
70             }
71         }
72     }
73     return nil;
74 }
75
76 @end
77
78
79 /*****************************************************************************
80  * VLControlScriptCommand implementation
81  *****************************************************************************/
82 /*
83  * This entire control command needs a better design. more object oriented.
84  * Applescript developers would be very welcome (hartman)
85  */
86 @implementation VLControlScriptCommand
87
88 - (id)performDefaultImplementation {
89     NSString *o_command = [[self commandDescription] commandName];
90     NSString *o_parameter = [self directParameter];
91
92     intf_thread_t * p_intf = VLCIntf;
93     playlist_t * p_playlist = pl_Get( p_intf );
94     if( p_playlist == NULL )
95     {
96         return nil;
97     }
98
99     if ( [o_command isEqualToString:@"play"] )
100     {
101         [[VLCCoreInteraction sharedInstance] play];
102     }
103     else if ( [o_command isEqualToString:@"stop"] )
104     {
105         [[VLCCoreInteraction sharedInstance] stop];
106     }
107     else if ( [o_command isEqualToString:@"previous"] )
108     {
109         [[VLCCoreInteraction sharedInstance] previous];
110     }
111     else if ( [o_command isEqualToString:@"next"] )
112     {
113         [[VLCCoreInteraction sharedInstance] next];
114     }
115     else if ( [o_command isEqualToString:@"fullscreen"] )
116     {
117         [[VLCCoreInteraction sharedInstance] toggleFullscreen];
118     }
119     else if ( [o_command isEqualToString:@"mute"] )
120     {
121         [[VLCCoreInteraction sharedInstance] mute];
122     }
123     else if ( [o_command isEqualToString:@"volumeUp"] )
124     {
125         [[VLCCoreInteraction sharedInstance] volumeUp];
126     }
127     else if ( [o_command isEqualToString:@"volumeDown"] )
128     {
129         [[VLCCoreInteraction sharedInstance] volumeDown];
130     }
131         else if ( [o_command isEqualToString:@"stepForward"] )
132     {
133         //default: forwardShort
134         if (o_parameter)
135         {
136             int i_parameter = [o_parameter intValue];
137             switch (i_parameter)
138             {
139                 case 1:
140                     [[VLCCoreInteraction sharedInstance] forwardExtraShort];
141                     break;
142                 case 2:
143                     [[VLCCoreInteraction sharedInstance] forwardShort];
144                     break;
145                 case 3:
146                     [[VLCCoreInteraction sharedInstance] forwardMedium];
147                     break;
148                 case 4:
149                     [[VLCCoreInteraction sharedInstance] forwardLong];
150                     break;
151                 default:
152                     [[VLCCoreInteraction sharedInstance] forwardShort];
153                     break;
154             }
155         }
156         else
157         {
158             [[VLCCoreInteraction sharedInstance] forwardShort];
159         }
160     }
161     else if ( [o_command isEqualToString:@"stepBackward"] )
162     {
163         //default: backwardShort
164         if (o_parameter)
165         {
166             int i_parameter = [o_parameter intValue];
167             switch (i_parameter)
168             {
169                 case 1:
170                     [[VLCCoreInteraction sharedInstance] backwardExtraShort];
171                     break;
172                 case 2:
173                     [[VLCCoreInteraction sharedInstance] backwardShort];
174                     break;
175                 case 3:
176                     [[VLCCoreInteraction sharedInstance] backwardMedium];
177                     break;
178                 case 4:
179                     [[VLCCoreInteraction sharedInstance] backwardLong];
180                     break;
181                 default:
182                     [[VLCCoreInteraction sharedInstance] backwardShort];
183                     break;
184             }
185         }
186         else
187         {
188             [[VLCCoreInteraction sharedInstance] backwardShort];
189         }
190     }
191    return nil;
192 }
193
194 @end
195
196 /*****************************************************************************
197  * Category that adds AppleScript support to NSApplication
198  *****************************************************************************/
199 @implementation NSApplication(ScriptSupport)
200
201 - (BOOL) scriptFullscreenMode {
202     vout_thread_t * p_vout = getVout();
203     if( !p_vout )
204         return NO;
205     BOOL b_value = var_GetBool( p_vout, "fullscreen");
206     vlc_object_release( p_vout );
207     return b_value;
208 }
209 - (void) setScriptFullscreenMode: (BOOL) mode {
210     vout_thread_t * p_vout = getVout();
211     if( !p_vout )
212         return;
213     if (var_GetBool( p_vout, "fullscreen") == mode)
214     {
215         vlc_object_release( p_vout );
216         return;
217     }
218     vlc_object_release( p_vout );
219     [[VLCCoreInteraction sharedInstance] toggleFullscreen];
220 }
221
222 - (BOOL) muted {
223     return [[VLCCoreInteraction sharedInstance] isMuted];
224 }
225
226 - (BOOL) playing {
227     return [[VLCCoreInteraction sharedInstance] isPlaying];
228 }
229
230 - (int) audioVolume {
231     return ( [[VLCCoreInteraction sharedInstance] volume] );
232 }
233
234 - (void) setAudioVolume: (int) i_audioVolume {
235     [[VLCCoreInteraction sharedInstance] setVolume:(int)i_audioVolume];
236 }
237
238 - (int) currentTime {
239     return [[VLCCoreInteraction sharedInstance] currentTime];
240 }
241
242 - (void) setCurrentTime: (int) i_currentTime {
243     if (i_currentTime)
244         [[VLCCoreInteraction sharedInstance] setCurrentTime:i_currentTime];
245 }
246
247 #pragma mark -
248 //TODO:whenever VLC should implement NSDocument, the methods below should move or be additionaly implemented in the NSDocument category
249 - (int) durationOfCurrentItem {
250     return [[VLCCoreInteraction sharedInstance] durationOfCurrentPlaylistItem];
251 }
252
253 - (NSString*) pathOfCurrentItem {
254     return [[[VLCCoreInteraction sharedInstance] URLOfCurrentPlaylistItem] path];
255 }
256
257 - (NSString*) nameOfCurrentItem {
258     return [[VLCCoreInteraction sharedInstance] nameOfCurrentPlaylistItem];
259 }
260
261 @end