]> git.sesse.net Git - vlc/blob - modules/gui/macosx/MainWindow.m
osx: fix crash with --no-media-library
[vlc] / modules / gui / macosx / MainWindow.m
1 /*****************************************************************************
2  * MainWindow.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2002-2013 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Felix Paul Kühne <fkuehne -at- videolan -dot- org>
8  *          Jon Lech Johansen <jon-vl@nanocrew.net>
9  *          Christophe Massiot <massiot@via.ecp.fr>
10  *          Derk-Jan Hartman <hartman at videolan.org>
11  *          David Fuhrmann <david dot fuhrmann at googlemail dot com>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27
28 #import "CompatibilityFixes.h"
29 #import "MainWindow.h"
30 #import "intf.h"
31 #import "CoreInteraction.h"
32 #import "AudioEffects.h"
33 #import "MainMenu.h"
34 #import "open.h"
35 #import "controls.h" // TODO: remove me
36 #import "playlist.h"
37 #import "SideBarItem.h"
38 #import <math.h>
39 #import <vlc_playlist.h>
40 #import <vlc_url.h>
41 #import <vlc_strings.h>
42 #import <vlc_services_discovery.h>
43
44 #import "ControlsBar.h"
45 #import "VideoView.h"
46 #import "VLCVoutWindowController.h"
47
48
49 @interface VLCMainWindow (Internal)
50 - (void)resizePlaylistAfterCollapse;
51 - (void)makeSplitViewVisible;
52 - (void)makeSplitViewHidden;
53 - (void)showPodcastControls;
54 - (void)hidePodcastControls;
55 @end
56
57
58 @implementation VLCMainWindow
59
60 @synthesize nativeFullscreenMode=b_nativeFullscreenMode;
61 @synthesize nonembedded=b_nonembedded;
62 @synthesize fsPanel=o_fspanel;
63
64 static VLCMainWindow *_o_sharedInstance = nil;
65
66 + (VLCMainWindow *)sharedInstance
67 {
68     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
69 }
70
71 #pragma mark -
72 #pragma mark Initialization
73
74 - (id)init
75 {
76     if (_o_sharedInstance) {
77         [self dealloc];
78         return _o_sharedInstance;
79     } else
80         _o_sharedInstance = [super init];
81
82     return _o_sharedInstance;
83 }
84
85 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask
86                   backing:(NSBackingStoreType)backingType defer:(BOOL)flag
87 {
88     self = [super initWithContentRect:contentRect styleMask:styleMask
89                               backing:backingType defer:flag];
90     _o_sharedInstance = self;
91
92     [[VLCMain sharedInstance] updateTogglePlaylistState];
93
94     return self;
95 }
96
97 - (BOOL)isEvent:(NSEvent *)o_event forKey:(const char *)keyString
98 {
99     char *key;
100     NSString *o_key;
101
102     key = config_GetPsz(VLCIntf, keyString);
103     o_key = [NSString stringWithFormat:@"%s", key];
104     FREENULL(key);
105
106     unsigned int i_keyModifiers = [[VLCStringUtility sharedInstance] VLCModifiersToCocoa:o_key];
107
108     NSString * characters = [o_event charactersIgnoringModifiers];
109     if ([characters length] > 0) {
110         return [[characters lowercaseString] isEqualToString: [[VLCStringUtility sharedInstance] VLCKeyToString: o_key]] &&
111                 (i_keyModifiers & NSShiftKeyMask)     == ([o_event modifierFlags] & NSShiftKeyMask) &&
112                 (i_keyModifiers & NSControlKeyMask)   == ([o_event modifierFlags] & NSControlKeyMask) &&
113                 (i_keyModifiers & NSAlternateKeyMask) == ([o_event modifierFlags] & NSAlternateKeyMask) &&
114                 (i_keyModifiers & NSCommandKeyMask)   == ([o_event modifierFlags] & NSCommandKeyMask);
115     }
116     return NO;
117 }
118
119 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
120 {
121     BOOL b_force = NO;
122     // these are key events which should be handled by vlc core, but are attached to a main menu item
123     if (![self isEvent: o_event forKey: "key-vol-up"] &&
124         ![self isEvent: o_event forKey: "key-vol-down"] &&
125         ![self isEvent: o_event forKey: "key-vol-mute"] &&
126         ![self isEvent: o_event forKey: "key-prev"] &&
127         ![self isEvent: o_event forKey: "key-next"] &&
128         ![self isEvent: o_event forKey: "key-jump+short"] &&
129         ![self isEvent: o_event forKey: "key-jump-short"]) {
130         /* We indeed want to prioritize some Cocoa key equivalent against libvlc,
131          so we perform the menu equivalent now. */
132         if ([[NSApp mainMenu] performKeyEquivalent:o_event])
133             return TRUE;
134     }
135     else
136         b_force = YES;
137
138     return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event force:b_force] ||
139            [(VLCControls *)[[VLCMain sharedInstance] controls] keyEvent:o_event];
140 }
141
142 - (void)dealloc
143 {
144     if (b_dark_interface)
145         [o_color_backdrop release];
146
147     [[NSNotificationCenter defaultCenter] removeObserver: self];
148     [o_sidebaritems release];
149
150     [super dealloc];
151 }
152
153 - (void)awakeFromNib
154 {
155     // sets lion fullscreen behaviour
156     [super awakeFromNib];
157
158     BOOL b_splitviewShouldBeHidden = NO;
159
160     /* setup the styled interface */
161     b_nativeFullscreenMode = NO;
162 #ifdef MAC_OS_X_VERSION_10_7
163     if (!OSX_SNOW_LEOPARD)
164         b_nativeFullscreenMode = var_InheritBool(VLCIntf, "macosx-nativefullscreenmode");
165 #endif
166     [self useOptimizedDrawing: YES];
167
168     [[o_search_fld cell] setPlaceholderString: _NS("Search")];
169     [[o_search_fld cell] accessibilitySetOverrideValue:_NS("Enter a term to search the playlist. Results will be selected in the table.") forAttribute:NSAccessibilityDescriptionAttribute];
170
171     [o_dropzone_btn setTitle: _NS("Open media...")];
172     [[o_dropzone_btn cell] accessibilitySetOverrideValue:_NS("Click to open an advanced dialog to select the media to play. You can also drop files here to play.") forAttribute:NSAccessibilityDescriptionAttribute];
173     [o_dropzone_lbl setStringValue: _NS("Drop media here")];
174
175     [o_podcast_add_btn setTitle: _NS("Subscribe")];
176     [o_podcast_remove_btn setTitle: _NS("Unsubscribe")];
177     [o_podcast_subscribe_title_lbl setStringValue: _NS("Subscribe to a podcast")];
178     [o_podcast_subscribe_subtitle_lbl setStringValue: _NS("Enter URL of the podcast to subscribe to:")];
179     [o_podcast_subscribe_cancel_btn setTitle: _NS("Cancel")];
180     [o_podcast_subscribe_ok_btn setTitle: _NS("Subscribe")];
181     [o_podcast_unsubscribe_title_lbl setStringValue: _NS("Unsubscribe from a podcast")];
182     [o_podcast_unsubscribe_subtitle_lbl setStringValue: _NS("Select the podcast you would like to unsubscribe from:")];
183     [o_podcast_unsubscribe_ok_btn setTitle: _NS("Unsubscribe")];
184     [o_podcast_unsubscribe_cancel_btn setTitle: _NS("Cancel")];
185
186     /* interface builder action */
187     float f_threshold_height = f_min_video_height + [o_controls_bar height];
188     if (b_dark_interface)
189         f_threshold_height += [o_titlebar_view frame].size.height;
190     if ([[self contentView] frame].size.height < f_threshold_height)
191         b_splitviewShouldBeHidden = YES;
192
193     [self setDelegate: self];
194     [self setExcludedFromWindowsMenu: YES];
195     [self setAcceptsMouseMovedEvents: YES];
196     // Set that here as IB seems to be buggy
197     if (b_dark_interface)
198         [self setContentMinSize:NSMakeSize(604., 288. + [o_titlebar_view frame].size.height)];
199     else
200         [self setContentMinSize:NSMakeSize(604., 288.)];
201
202     [self setTitle: _NS("VLC media player")];
203
204     b_dropzone_active = YES;
205     [o_dropzone_view setFrame: [o_playlist_table frame]];
206     [o_left_split_view setFrame: [o_sidebar_view frame]];
207
208     if (!OSX_SNOW_LEOPARD) {
209         /* the default small size of the search field is slightly different on Lion, let's work-around that */
210         NSRect frame;
211         frame = [o_search_fld frame];
212         frame.origin.y = frame.origin.y + 2.0;
213         frame.size.height = frame.size.height - 1.0;
214         [o_search_fld setFrame: frame];
215     }
216
217     /* create the sidebar */
218     o_sidebaritems = [[NSMutableArray alloc] init];
219     SideBarItem *libraryItem = [SideBarItem itemWithTitle:_NS("LIBRARY") identifier:@"library"];
220     SideBarItem *playlistItem = [SideBarItem itemWithTitle:_NS("Playlist") identifier:@"playlist"];
221     [playlistItem setIcon: [NSImage imageNamed:@"sidebar-playlist"]];
222     SideBarItem *medialibraryItem = [SideBarItem itemWithTitle:_NS("Media Library") identifier:@"medialibrary"];
223     [medialibraryItem setIcon: [NSImage imageNamed:@"sidebar-playlist"]];
224     SideBarItem *mycompItem = [SideBarItem itemWithTitle:_NS("MY COMPUTER") identifier:@"mycomputer"];
225     SideBarItem *devicesItem = [SideBarItem itemWithTitle:_NS("DEVICES") identifier:@"devices"];
226     SideBarItem *lanItem = [SideBarItem itemWithTitle:_NS("LOCAL NETWORK") identifier:@"localnetwork"];
227     SideBarItem *internetItem = [SideBarItem itemWithTitle:_NS("INTERNET") identifier:@"internet"];
228
229     /* SD subnodes, inspired by the Qt4 intf */
230     char **ppsz_longnames;
231     int *p_categories;
232     char **ppsz_names = vlc_sd_GetNames(pl_Get(VLCIntf), &ppsz_longnames, &p_categories);
233     if (!ppsz_names)
234         msg_Err(VLCIntf, "no sd item found"); //TODO
235     char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
236     int *p_category = p_categories;
237     NSMutableArray *internetItems = [[NSMutableArray alloc] init];
238     NSMutableArray *devicesItems = [[NSMutableArray alloc] init];
239     NSMutableArray *lanItems = [[NSMutableArray alloc] init];
240     NSMutableArray *mycompItems = [[NSMutableArray alloc] init];
241     NSString *o_identifier;
242     for (; *ppsz_name; ppsz_name++, ppsz_longname++, p_category++) {
243         o_identifier = [NSString stringWithCString: *ppsz_name encoding: NSUTF8StringEncoding];
244         switch (*p_category) {
245             case SD_CAT_INTERNET:
246                     [internetItems addObject: [SideBarItem itemWithTitle: _NS(*ppsz_longname) identifier: o_identifier]];
247                     if (!strncmp(*ppsz_name, "podcast", 7))
248                         [[internetItems lastObject] setIcon: [NSImage imageNamed:@"sidebar-podcast"]];
249                     else
250                         [[internetItems lastObject] setIcon: [NSImage imageNamed:@"NSApplicationIcon"]];
251                     [[internetItems lastObject] setSdtype: SD_CAT_INTERNET];
252                     [[internetItems lastObject] setUntranslatedTitle: @(*ppsz_longname)];
253                 break;
254             case SD_CAT_DEVICES:
255                     [devicesItems addObject: [SideBarItem itemWithTitle: _NS(*ppsz_longname) identifier: o_identifier]];
256                     [[devicesItems lastObject] setIcon: [NSImage imageNamed:@"NSApplicationIcon"]];
257                     [[devicesItems lastObject] setSdtype: SD_CAT_DEVICES];
258                     [[devicesItems lastObject] setUntranslatedTitle: @(*ppsz_longname)];
259                 break;
260             case SD_CAT_LAN:
261                     [lanItems addObject: [SideBarItem itemWithTitle: _NS(*ppsz_longname) identifier: o_identifier]];
262                     [[lanItems lastObject] setIcon: [NSImage imageNamed:@"sidebar-local"]];
263                     [[lanItems lastObject] setSdtype: SD_CAT_LAN];
264                     [[lanItems lastObject] setUntranslatedTitle: @(*ppsz_longname)];
265                 break;
266             case SD_CAT_MYCOMPUTER:
267                     [mycompItems addObject: [SideBarItem itemWithTitle: _NS(*ppsz_longname) identifier: o_identifier]];
268                     if (!strncmp(*ppsz_name, "video_dir", 9))
269                         [[mycompItems lastObject] setIcon: [NSImage imageNamed:@"sidebar-movie"]];
270                     else if (!strncmp(*ppsz_name, "audio_dir", 9))
271                         [[mycompItems lastObject] setIcon: [NSImage imageNamed:@"sidebar-music"]];
272                     else if (!strncmp(*ppsz_name, "picture_dir", 11))
273                         [[mycompItems lastObject] setIcon: [NSImage imageNamed:@"sidebar-pictures"]];
274                     else
275                         [[mycompItems lastObject] setIcon: [NSImage imageNamed:@"NSApplicationIcon"]];
276                     [[mycompItems lastObject] setUntranslatedTitle: @(*ppsz_longname)];
277                     [[mycompItems lastObject] setSdtype: SD_CAT_MYCOMPUTER];
278                 break;
279             default:
280                 msg_Warn(VLCIntf, "unknown SD type found, skipping (%s)", *ppsz_name);
281                 break;
282         }
283
284         free(*ppsz_name);
285         free(*ppsz_longname);
286     }
287     [mycompItem setChildren: [NSArray arrayWithArray: mycompItems]];
288     [devicesItem setChildren: [NSArray arrayWithArray: devicesItems]];
289     [lanItem setChildren: [NSArray arrayWithArray: lanItems]];
290     [internetItem setChildren: [NSArray arrayWithArray: internetItems]];
291     [mycompItems release];
292     [devicesItems release];
293     [lanItems release];
294     [internetItems release];
295     free(ppsz_names);
296     free(ppsz_longnames);
297     free(p_categories);
298
299     [libraryItem setChildren: @[playlistItem, medialibraryItem]];
300     [o_sidebaritems addObject: libraryItem];
301     if ([mycompItem hasChildren])
302         [o_sidebaritems addObject: mycompItem];
303     if ([devicesItem hasChildren])
304         [o_sidebaritems addObject: devicesItem];
305     if ([lanItem hasChildren])
306         [o_sidebaritems addObject: lanItem];
307     if ([internetItem hasChildren])
308         [o_sidebaritems addObject: internetItem];
309
310     [o_sidebar_view reloadData];
311     [o_sidebar_view selectRowIndexes:[NSIndexSet indexSetWithIndex:1] byExtendingSelection:NO];
312     [o_sidebar_view setDropItem:playlistItem dropChildIndex:NSOutlineViewDropOnItemIndex];
313     [o_sidebar_view registerForDraggedTypes:@[NSFilenamesPboardType, @"VLCPlaylistItemPboardType"]];
314
315     [o_sidebar_view setAutosaveName:@"mainwindow-sidebar"];
316     [(PXSourceList *)o_sidebar_view setDataSource:self];
317     [o_sidebar_view setDelegate:self];
318     [o_sidebar_view setAutosaveExpandedItems:YES];
319
320     [o_sidebar_view expandItem: libraryItem expandChildren: YES];
321
322     /* make sure we display the desired default appearance when VLC launches for the first time */
323     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
324     if (![defaults objectForKey:@"VLCFirstRun"]) {
325         [defaults setObject:[NSDate date] forKey:@"VLCFirstRun"];
326
327         NSUInteger i_sidebaritem_count = [o_sidebaritems count];
328         for (NSUInteger x = 0; x < i_sidebaritem_count; x++)
329             [o_sidebar_view expandItem: [o_sidebaritems objectAtIndex:x] expandChildren: YES];
330
331         [o_fspanel center];
332     }
333
334     if (b_dark_interface) {
335         [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(windowResizedOrMoved:) name: NSWindowDidResizeNotification object: nil];
336         [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(windowResizedOrMoved:) name: NSWindowDidMoveNotification object: nil];
337
338         [self setBackgroundColor: [NSColor clearColor]];
339         [self setOpaque: NO];
340         [self display];
341         [self setHasShadow:NO];
342         [self setHasShadow:YES];
343
344         NSRect winrect = [self frame];
345         CGFloat f_titleBarHeight = [o_titlebar_view frame].size.height;
346
347         [o_titlebar_view setFrame: NSMakeRect(0, winrect.size.height - f_titleBarHeight,
348                                               winrect.size.width, f_titleBarHeight)];
349         [[self contentView] addSubview: o_titlebar_view positioned: NSWindowAbove relativeTo: o_split_view];
350
351         if (winrect.size.height > 100) {
352             [self setFrame: winrect display:YES animate:YES];
353             previousSavedFrame = winrect;
354         }
355
356         winrect = [o_split_view frame];
357         winrect.size.height = winrect.size.height - f_titleBarHeight;
358         [o_split_view setFrame: winrect];
359         [o_video_view setFrame: winrect];
360
361         o_color_backdrop = [[VLCColorView alloc] initWithFrame: [o_split_view frame]];
362         [[self contentView] addSubview: o_color_backdrop positioned: NSWindowBelow relativeTo: o_split_view];
363         [o_color_backdrop setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
364     } else {
365         [o_video_view setFrame: [o_split_view frame]];
366         [o_playlist_table setBorderType: NSNoBorder];
367         [o_sidebar_scrollview setBorderType: NSNoBorder];
368     }
369
370     [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(someWindowWillClose:) name: NSWindowWillCloseNotification object: nil];
371     [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(someWindowWillMiniaturize:) name: NSWindowWillMiniaturizeNotification object:nil];
372     [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(applicationWillTerminate:) name: NSApplicationWillTerminateNotification object: nil];
373     [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(mainSplitViewDidResizeSubviews:) name: NSSplitViewDidResizeSubviewsNotification object:o_split_view];
374
375     if (b_splitviewShouldBeHidden) {
376         [self hideSplitView];
377         i_lastSplitViewHeight = 300;
378     }
379
380     /* sanity check for the window size */
381     NSRect frame = [self frame];
382     NSSize screenSize = [[self screen] frame].size;
383     if (screenSize.width <= frame.size.width || screenSize.height <= frame.size.height) {
384         nativeVideoSize = screenSize;
385         [self resizeWindow];
386     }
387
388     /* update fs button to reflect state for next startup */
389     if (var_InheritBool(pl_Get(VLCIntf), "fullscreen"))
390         [o_controls_bar setFullscreenState:YES];
391
392     /* restore split view */
393     i_lastLeftSplitViewWidth = 200;
394     /* trick NSSplitView implementation, which pretends to know better than us */
395     if (!config_GetInt(VLCIntf, "macosx-show-sidebar"))
396         [self performSelector:@selector(toggleLeftSubSplitView) withObject:nil afterDelay:0.05];
397 }
398
399 #pragma mark -
400 #pragma mark appearance management
401
402 - (VLCMainWindowControlsBar *)controlsBar;
403 {
404     return (VLCMainWindowControlsBar *)o_controls_bar;
405 }
406
407 - (void)resizePlaylistAfterCollapse
408 {
409     NSRect plrect;
410     plrect = [o_playlist_table frame];
411     plrect.size.height = i_lastSplitViewHeight - 20.0; // actual pl top bar height, which differs from its frame
412     [[o_playlist_table animator] setFrame: plrect];
413
414     NSRect rightSplitRect;
415     rightSplitRect = [o_right_split_view frame];
416     plrect = [o_dropzone_box frame];
417     plrect.origin.x = (rightSplitRect.size.width - plrect.size.width) / 2;
418     plrect.origin.y = (rightSplitRect.size.height - plrect.size.height) / 2;
419     [[o_dropzone_box animator] setFrame: plrect];
420 }
421
422 - (void)makeSplitViewVisible
423 {
424     if (b_dark_interface)
425         [self setContentMinSize: NSMakeSize(604., 288. + [o_titlebar_view frame].size.height)];
426     else
427         [self setContentMinSize: NSMakeSize(604., 288.)];
428
429     NSRect old_frame = [self frame];
430     float newHeight = [self minSize].height;
431     if (old_frame.size.height < newHeight) {
432         NSRect new_frame = old_frame;
433         new_frame.origin.y = old_frame.origin.y + old_frame.size.height - newHeight;
434         new_frame.size.height = newHeight;
435
436         [[self animator] setFrame: new_frame display: YES animate: YES];
437     }
438
439     [o_video_view setHidden: YES];
440     [o_split_view setHidden: NO];
441     if ([self fullscreen]) {
442         [[o_controls_bar bottomBarView] setHidden: NO];
443         [o_fspanel setNonActive:nil];
444     }
445
446     [self makeFirstResponder: o_playlist_table];
447 }
448
449 - (void)makeSplitViewHidden
450 {
451     if (b_dark_interface)
452         [self setContentMinSize: NSMakeSize(604., f_min_video_height + [o_titlebar_view frame].size.height)];
453     else
454         [self setContentMinSize: NSMakeSize(604., f_min_video_height)];
455
456     [o_split_view setHidden: YES];
457     [o_video_view setHidden: NO];
458     if ([self fullscreen]) {
459         [[o_controls_bar bottomBarView] setHidden: YES];
460         [o_fspanel setActive:nil];
461     }
462
463     if ([[o_video_view subviews] count] > 0)
464         [self makeFirstResponder: [[o_video_view subviews] objectAtIndex:0]];
465 }
466
467 // only exception for an controls bar button action
468 - (IBAction)togglePlaylist:(id)sender
469 {
470     if (![self isVisible] && sender != nil) {
471         [self makeKeyAndOrderFront: sender];
472         return;
473     }
474
475     BOOL b_activeVideo = [[VLCMain sharedInstance] activeVideoPlayback];
476     BOOL b_restored = NO;
477
478     BOOL b_have_alt_key = ([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask) != 0;
479     if (sender && [sender isKindOfClass: [NSMenuItem class]])
480         b_have_alt_key = NO;
481
482     if (b_dropzone_active && b_have_alt_key) {
483         [self hideDropZone];
484         return;
485     }
486
487     if (!(b_nativeFullscreenMode && b_fullscreen) && !b_splitview_removed && ((b_have_alt_key && b_activeVideo)
488                                                                               || (b_nonembedded && sender != nil)
489                                                                               || (!b_activeVideo && sender != nil)
490                                                                               || b_minimized_view))
491         [self hideSplitView];
492     else {
493         if (b_splitview_removed) {
494             if (!b_nonembedded || (sender != nil && b_nonembedded))
495                 [self showSplitView];
496
497             if (sender == nil)
498                 b_minimized_view = YES;
499             else
500                 b_minimized_view = NO;
501
502             if (b_activeVideo)
503                 b_restored = YES;
504         }
505
506         if (!b_nonembedded) {
507             if (([o_video_view isHidden] && b_activeVideo) || b_restored || (b_activeVideo && sender == nil))
508                 [self makeSplitViewHidden];
509             else
510                 [self makeSplitViewVisible];
511         } else {
512             [o_split_view setHidden: NO];
513             [o_playlist_table setHidden: NO];
514             [o_video_view setHidden: YES];
515         }
516     }
517 }
518
519 - (IBAction)dropzoneButtonAction:(id)sender
520 {
521     [[[VLCMain sharedInstance] open] openFileGeneric];
522 }
523
524 #pragma mark -
525 #pragma mark overwritten default functionality
526
527 - (void)windowResizedOrMoved:(NSNotification *)notification
528 {
529     [self saveFrameUsingName: [self frameAutosaveName]];
530 }
531
532 - (void)applicationWillTerminate:(NSNotification *)notification
533 {
534     config_PutInt(VLCIntf, "macosx-show-sidebar", ![o_split_view isSubviewCollapsed:o_left_split_view]);
535
536     [self saveFrameUsingName: [self frameAutosaveName]];
537 }
538
539
540 - (void)someWindowWillClose:(NSNotification *)notification
541 {
542     id obj = [notification object];
543
544     // hasActiveVideo is defined for VLCVideoWindowCommon and subclasses
545     if ([obj respondsToSelector:@selector(hasActiveVideo)] && [obj hasActiveVideo]) {
546         if ([[VLCMain sharedInstance] activeVideoPlayback])
547             [[VLCCoreInteraction sharedInstance] stop];
548     }
549 }
550
551 - (void)someWindowWillMiniaturize:(NSNotification *)notification
552 {
553     if (config_GetInt(VLCIntf, "macosx-pause-minimized")) {
554         id obj = [notification object];
555
556         if ([obj class] == [VLCVideoWindowCommon class] || [obj class] == [VLCDetachedVideoWindow class] || ([obj class] == [VLCMainWindow class] && !b_nonembedded)) {
557             if ([[VLCMain sharedInstance] activeVideoPlayback])
558                 [[VLCCoreInteraction sharedInstance] pause];
559         }
560     }
561 }
562
563 #pragma mark -
564 #pragma mark Update interface and respond to foreign events
565 - (void)showDropZone
566 {
567     b_dropzone_active = YES;
568     [o_right_split_view addSubview: o_dropzone_view positioned:NSWindowAbove relativeTo:o_playlist_table];
569     [o_dropzone_view setFrame: [o_playlist_table frame]];
570     [[o_playlist_table animator] setHidden:YES];
571 }
572
573 - (void)hideDropZone
574 {
575     b_dropzone_active = NO;
576     [o_dropzone_view removeFromSuperview];
577     [[o_playlist_table animator] setHidden: NO];
578 }
579
580 - (void)hideSplitView
581 {
582     NSRect winrect = [self frame];
583     i_lastSplitViewHeight = [o_split_view frame].size.height;
584     winrect.size.height = winrect.size.height - i_lastSplitViewHeight;
585     winrect.origin.y = winrect.origin.y + i_lastSplitViewHeight;
586     [self setFrame: winrect display: YES animate: YES];
587     [self performSelector:@selector(hideDropZone) withObject:nil afterDelay:0.1];
588     if (b_dark_interface) {
589         [self setContentMinSize: NSMakeSize(604., [o_controls_bar height] + [o_titlebar_view frame].size.height)];
590         [self setContentMaxSize: NSMakeSize(FLT_MAX, [o_controls_bar height] + [o_titlebar_view frame].size.height)];
591     } else {
592         [self setContentMinSize: NSMakeSize(604., [o_controls_bar height])];
593         [self setContentMaxSize: NSMakeSize(FLT_MAX, [o_controls_bar height])];
594     }
595
596     b_splitview_removed = YES;
597 }
598
599 - (void)showSplitView
600 {
601     [self updateWindow];
602     if (b_dark_interface)
603         [self setContentMinSize:NSMakeSize(604., 288. + [o_titlebar_view frame].size.height)];
604     else
605         [self setContentMinSize:NSMakeSize(604., 288.)];
606     [self setContentMaxSize: NSMakeSize(FLT_MAX, FLT_MAX)];
607
608     NSRect winrect;
609     winrect = [self frame];
610     winrect.size.height = winrect.size.height + i_lastSplitViewHeight;
611     winrect.origin.y = winrect.origin.y - i_lastSplitViewHeight;
612     [self setFrame: winrect display: YES animate: YES];
613
614     [self performSelector:@selector(resizePlaylistAfterCollapse) withObject: nil afterDelay:0.75];
615
616     b_splitview_removed = NO;
617 }
618
619 - (void)updateTimeSlider
620 {
621     [o_controls_bar updateTimeSlider];
622     [o_fspanel updatePositionAndTime];
623
624     [[[VLCMain sharedInstance] voutController] updateWindowsControlsBarWithSelector:@selector(updateTimeSlider)];
625 }
626
627 - (void)updateName
628 {
629     input_thread_t * p_input;
630     p_input = pl_CurrentInput(VLCIntf);
631     if (p_input) {
632         NSString *aString;
633
634         if (!config_GetPsz(VLCIntf, "video-title")) {
635             char *format = var_InheritString(VLCIntf, "input-title-format");
636             char *formated = str_format_meta(pl_Get(VLCIntf), format);
637             free(format);
638             aString = @(formated);
639             free(formated);
640         } else
641             aString = @(config_GetPsz(VLCIntf, "video-title"));
642
643         char *uri = input_item_GetURI(input_GetItem(p_input));
644
645         NSURL * o_url = [NSURL URLWithString: @(uri)];
646         if ([o_url isFileURL]) {
647             [self setRepresentedURL: o_url];
648             [[[VLCMain sharedInstance] voutController] updateWindowsUsingBlock:^(VLCVideoWindowCommon *o_window) {
649                 [o_window setRepresentedURL:o_url];
650             }];
651         } else {
652             [self setRepresentedURL: nil];
653             [[[VLCMain sharedInstance] voutController] updateWindowsUsingBlock:^(VLCVideoWindowCommon *o_window) {
654                 [o_window setRepresentedURL:nil];
655             }];
656         }
657         free(uri);
658
659         if ([aString isEqualToString:@""]) {
660             if ([o_url isFileURL])
661                 aString = [[NSFileManager defaultManager] displayNameAtPath: [o_url path]];
662             else
663                 aString = [o_url absoluteString];
664         }
665
666         if ([aString length] > 0) {
667             [self setTitle: aString];
668             [[[VLCMain sharedInstance] voutController] updateWindowsUsingBlock:^(VLCVideoWindowCommon *o_window) {
669                 [o_window setTitle:aString];
670             }];
671
672             [o_fspanel setStreamTitle: aString];
673         } else {
674             [self setTitle: _NS("VLC media player")];
675             [self setRepresentedURL: nil];
676         }
677
678         vlc_object_release(p_input);
679     } else {
680         [self setTitle: _NS("VLC media player")];
681         [self setRepresentedURL: nil];
682     }
683 }
684
685 - (void)updateWindow
686 {
687     [o_controls_bar updateControls];
688     [[[VLCMain sharedInstance] voutController] updateWindowsControlsBarWithSelector:@selector(updateControls)];
689
690     bool b_seekable = false;
691
692     playlist_t * p_playlist = pl_Get(VLCIntf);
693     input_thread_t * p_input = playlist_CurrentInput(p_playlist);
694     if (p_input) {
695         /* seekable streams */
696         b_seekable = var_GetBool(p_input, "can-seek");
697
698         vlc_object_release(p_input);
699     }
700
701     [self updateTimeSlider];
702     if ([o_fspanel respondsToSelector:@selector(setSeekable:)])
703         [o_fspanel setSeekable: b_seekable];
704
705     PL_LOCK;
706     if ([[[VLCMain sharedInstance] playlist] currentPlaylistRoot] != p_playlist->p_local_category || p_playlist->p_local_category->i_children > 0)
707         [self hideDropZone];
708     else
709         [self showDropZone];
710     PL_UNLOCK;
711     [o_sidebar_view setNeedsDisplay:YES];
712 }
713
714 - (void)setPause
715 {
716     [o_controls_bar setPause];
717     [o_fspanel setPause];
718
719     [[[VLCMain sharedInstance] voutController] updateWindowsControlsBarWithSelector:@selector(setPause)];
720 }
721
722 - (void)setPlay
723 {
724     [o_controls_bar setPlay];
725     [o_fspanel setPlay];
726
727     [[[VLCMain sharedInstance] voutController] updateWindowsControlsBarWithSelector:@selector(setPlay)];
728
729 }
730
731 - (void)updateVolumeSlider
732 {
733     [[self controlsBar] updateVolumeSlider];
734     [o_fspanel setVolumeLevel: [[VLCCoreInteraction sharedInstance] volume]];
735 }
736
737 #pragma mark -
738 #pragma mark Video Output handling
739
740 - (void)videoplayWillBeStarted
741 {
742     if (!b_fullscreen)
743         frameBeforePlayback = [self frame];
744 }
745
746 - (void)setVideoplayEnabled
747 {
748     BOOL b_videoPlayback = [[VLCMain sharedInstance] activeVideoPlayback];
749         
750     if (!b_videoPlayback) {
751         if (!b_nonembedded && (!b_nativeFullscreenMode || (b_nativeFullscreenMode && !b_fullscreen)) && frameBeforePlayback.size.width > 0 && frameBeforePlayback.size.height > 0)
752             [[self animator] setFrame:frameBeforePlayback display:YES];
753
754         // update fs button to reflect state for next startup
755         if (var_InheritBool(VLCIntf, "fullscreen") || var_GetBool(pl_Get(VLCIntf), "fullscreen")) {
756             [o_controls_bar setFullscreenState:YES];
757         }
758
759         [self makeFirstResponder: o_playlist_table];
760         [[[VLCMain sharedInstance] voutController] updateWindowLevelForHelperWindows: NSNormalWindowLevel];
761
762         // restore alpha value to 1 for the case that macosx-opaqueness is set to < 1
763         [self setAlphaValue:1.0];
764     }
765
766     if (b_nativeFullscreenMode) {
767         if ([self hasActiveVideo] && [self fullscreen]) {
768             [[o_controls_bar bottomBarView] setHidden: b_videoPlayback];
769             [o_fspanel setActive: nil];
770         } else {
771             [[o_controls_bar bottomBarView] setHidden: NO];
772             [o_fspanel setNonActive: nil];
773         }
774     }
775 }
776
777 #pragma mark -
778 #pragma mark Lion native fullscreen handling
779 - (void)windowWillEnterFullScreen:(NSNotification *)notification
780 {
781     [super windowWillEnterFullScreen:notification];
782
783     // update split view frame after removing title bar
784     if (b_dark_interface) {
785         NSRect frame = [[self contentView] frame];
786         frame.origin.y += [o_controls_bar height];
787         frame.size.height -= [o_controls_bar height];
788         [o_split_view setFrame:frame];
789     }
790 }
791
792 - (void)windowWillExitFullScreen:(NSNotification *)notification
793 {
794     [super windowWillExitFullScreen: notification];
795
796     // update split view frame after readding title bar
797     if (b_dark_interface) {
798         NSRect frame = [o_split_view frame];
799         frame.size.height -= [o_titlebar_view frame].size.height;
800         [o_split_view setFrame:frame];
801     }
802 }
803 #pragma mark -
804 #pragma mark Fullscreen support
805
806 - (void)showFullscreenController
807 {
808     id currentWindow = [NSApp keyWindow];
809     if ([currentWindow respondsToSelector:@selector(hasActiveVideo)] && [currentWindow hasActiveVideo]) {
810         if ([currentWindow respondsToSelector:@selector(fullscreen)] && [currentWindow fullscreen] && ![[currentWindow videoView] isHidden]) {
811
812             if ([[VLCMain sharedInstance] activeVideoPlayback])
813                 [o_fspanel fadeIn];
814         }
815     }
816
817 }
818
819 #pragma mark -
820 #pragma mark split view delegate
821 - (CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMax ofSubviewAt:(NSInteger)dividerIndex
822 {
823     if (dividerIndex == 0)
824         return 300.;
825     else
826         return proposedMax;
827 }
828
829 - (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMin ofSubviewAt:(NSInteger)dividerIndex
830 {
831     if (dividerIndex == 0)
832         return 100.;
833     else
834         return proposedMin;
835 }
836
837 - (BOOL)splitView:(NSSplitView *)splitView canCollapseSubview:(NSView *)subview
838 {
839     return ([subview isEqual:o_left_split_view]);
840 }
841
842 - (BOOL)splitView:(NSSplitView *)splitView shouldAdjustSizeOfSubview:(NSView *)subview
843 {
844     if ([subview isEqual:o_left_split_view])
845         return NO;
846     return YES;
847 }
848
849 - (void)mainSplitViewDidResizeSubviews:(id)object
850 {
851     i_lastLeftSplitViewWidth = [o_left_split_view frame].size.width;
852     config_PutInt(VLCIntf, "macosx-show-sidebar", ![o_split_view isSubviewCollapsed:o_left_split_view]);
853     [[[VLCMain sharedInstance] mainMenu] updateSidebarMenuItem];
854 }
855
856 - (void)toggleLeftSubSplitView
857 {
858     [o_split_view adjustSubviews];
859     if ([o_split_view isSubviewCollapsed:o_left_split_view])
860         [o_split_view setPosition:i_lastLeftSplitViewWidth ofDividerAtIndex:0];
861     else
862         [o_split_view setPosition:[o_split_view minPossiblePositionOfDividerAtIndex:0] ofDividerAtIndex:0];
863     [[[VLCMain sharedInstance] mainMenu] updateSidebarMenuItem];
864 }
865
866 #pragma mark -
867 #pragma mark Side Bar Data handling
868 /* taken under BSD-new from the PXSourceList sample project, adapted for VLC */
869 - (NSUInteger)sourceList:(PXSourceList*)sourceList numberOfChildrenOfItem:(id)item
870 {
871     //Works the same way as the NSOutlineView data source: `nil` means a parent item
872     if (item==nil)
873         return [o_sidebaritems count];
874     else
875         return [[item children] count];
876 }
877
878
879 - (id)sourceList:(PXSourceList*)aSourceList child:(NSUInteger)index ofItem:(id)item
880 {
881     //Works the same way as the NSOutlineView data source: `nil` means a parent item
882     if (item==nil)
883         return [o_sidebaritems objectAtIndex:index];
884     else
885         return [[item children] objectAtIndex:index];
886 }
887
888
889 - (id)sourceList:(PXSourceList*)aSourceList objectValueForItem:(id)item
890 {
891     return [item title];
892 }
893
894 - (void)sourceList:(PXSourceList*)aSourceList setObjectValue:(id)object forItem:(id)item
895 {
896     [item setTitle:object];
897 }
898
899 - (BOOL)sourceList:(PXSourceList*)aSourceList isItemExpandable:(id)item
900 {
901     return [item hasChildren];
902 }
903
904
905 - (BOOL)sourceList:(PXSourceList*)aSourceList itemHasBadge:(id)item
906 {
907     if ([[item identifier] isEqualToString: @"playlist"] || [[item identifier] isEqualToString: @"medialibrary"])
908         return YES;
909
910     return [item hasBadge];
911 }
912
913
914 - (NSInteger)sourceList:(PXSourceList*)aSourceList badgeValueForItem:(id)item
915 {
916     playlist_t * p_playlist = pl_Get(VLCIntf);
917     NSInteger i_playlist_size = 0;
918
919     if ([[item identifier] isEqualToString: @"playlist"]) {
920         PL_LOCK;
921         i_playlist_size = p_playlist->p_local_category->i_children;
922         PL_UNLOCK;
923
924         return i_playlist_size;
925     }
926     if ([[item identifier] isEqualToString: @"medialibrary"]) {
927         PL_LOCK;
928         if (p_playlist->p_ml_category)
929             i_playlist_size = p_playlist->p_ml_category->i_children;
930         PL_UNLOCK;
931
932         return i_playlist_size;
933     }
934
935     return [item badgeValue];
936 }
937
938
939 - (BOOL)sourceList:(PXSourceList*)aSourceList itemHasIcon:(id)item
940 {
941     return [item hasIcon];
942 }
943
944
945 - (NSImage*)sourceList:(PXSourceList*)aSourceList iconForItem:(id)item
946 {
947     return [item icon];
948 }
949
950 - (NSMenu*)sourceList:(PXSourceList*)aSourceList menuForEvent:(NSEvent*)theEvent item:(id)item
951 {
952     if ([theEvent type] == NSRightMouseDown || ([theEvent type] == NSLeftMouseDown && ([theEvent modifierFlags] & NSControlKeyMask) == NSControlKeyMask)) {
953         if (item != nil) {
954             NSMenu * m;
955             if ([item sdtype] > 0)
956             {
957                 m = [[NSMenu alloc] init];
958                 playlist_t * p_playlist = pl_Get(VLCIntf);
959                 BOOL sd_loaded = playlist_IsServicesDiscoveryLoaded(p_playlist, [[item identifier] UTF8String]);
960                 if (!sd_loaded)
961                     [m addItemWithTitle:_NS("Enable") action:@selector(sdmenuhandler:) keyEquivalent:@""];
962                 else
963                     [m addItemWithTitle:_NS("Disable") action:@selector(sdmenuhandler:) keyEquivalent:@""];
964                 [[m itemAtIndex:0] setRepresentedObject: [item identifier]];
965             }
966             return [m autorelease];
967         }
968     }
969
970     return nil;
971 }
972
973 - (IBAction)sdmenuhandler:(id)sender
974 {
975     NSString * identifier = [sender representedObject];
976     if ([identifier length] > 0 && ![identifier isEqualToString:@"lua{sd='freebox',longname='Freebox TV'}"]) {
977         playlist_t * p_playlist = pl_Get(VLCIntf);
978         BOOL sd_loaded = playlist_IsServicesDiscoveryLoaded(p_playlist, [identifier UTF8String]);
979
980         if (!sd_loaded)
981             playlist_ServicesDiscoveryAdd(p_playlist, [identifier UTF8String]);
982         else
983             playlist_ServicesDiscoveryRemove(p_playlist, [identifier UTF8String]);
984     }
985 }
986
987 #pragma mark -
988 #pragma mark Side Bar Delegate Methods
989 /* taken under BSD-new from the PXSourceList sample project, adapted for VLC */
990 - (BOOL)sourceList:(PXSourceList*)aSourceList isGroupAlwaysExpanded:(id)group
991 {
992     if ([[group identifier] isEqualToString:@"library"])
993         return YES;
994
995     return NO;
996 }
997
998 - (void)sourceListSelectionDidChange:(NSNotification *)notification
999 {
1000     playlist_t * p_playlist = pl_Get(VLCIntf);
1001
1002     NSIndexSet *selectedIndexes = [o_sidebar_view selectedRowIndexes];
1003     id item = [o_sidebar_view itemAtRow:[selectedIndexes firstIndex]];
1004
1005
1006     //Set the label text to represent the new selection
1007     if ([item sdtype] > -1 && [[item identifier] length] > 0) {
1008         BOOL sd_loaded = playlist_IsServicesDiscoveryLoaded(p_playlist, [[item identifier] UTF8String]);
1009         if (!sd_loaded)
1010             playlist_ServicesDiscoveryAdd(p_playlist, [[item identifier] UTF8String]);
1011     }
1012
1013     [o_chosen_category_lbl setStringValue:[item title]];
1014
1015     if ([[item identifier] isEqualToString:@"playlist"]) {
1016         [[[VLCMain sharedInstance] playlist] setPlaylistRoot:p_playlist->p_local_category];
1017     } else if ([[item identifier] isEqualToString:@"medialibrary"]) {
1018         if (p_playlist->p_ml_category)
1019             [[[VLCMain sharedInstance] playlist] setPlaylistRoot:p_playlist->p_ml_category];
1020     } else {
1021         playlist_item_t * pl_item;
1022         PL_LOCK;
1023         pl_item = playlist_ChildSearchName(p_playlist->p_root, [[item untranslatedTitle] UTF8String]);
1024         PL_UNLOCK;
1025         [[[VLCMain sharedInstance] playlist] setPlaylistRoot: pl_item];
1026     }
1027
1028     PL_LOCK;
1029     if ([[[VLCMain sharedInstance] playlist] currentPlaylistRoot] != p_playlist->p_local_category || p_playlist->p_local_category->i_children > 0)
1030         [self hideDropZone];
1031     else
1032         [self showDropZone];
1033     PL_UNLOCK;
1034
1035     if ([[item identifier] isEqualToString:@"podcast{longname=\"Podcasts\"}"])
1036         [self showPodcastControls];
1037     else
1038         [self hidePodcastControls];
1039
1040     [[NSNotificationCenter defaultCenter] postNotificationName: @"VLCMediaKeySupportSettingChanged"
1041                                                         object: nil
1042                                                       userInfo: nil];
1043 }
1044
1045 - (NSDragOperation)sourceList:(PXSourceList *)aSourceList validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)index
1046 {
1047     if ([[item identifier] isEqualToString:@"playlist"] || [[item identifier] isEqualToString:@"medialibrary"]) {
1048         NSPasteboard *o_pasteboard = [info draggingPasteboard];
1049         if ([[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] || [[o_pasteboard types] containsObject: NSFilenamesPboardType])
1050             return NSDragOperationGeneric;
1051     }
1052     return NSDragOperationNone;
1053 }
1054
1055 - (BOOL)sourceList:(PXSourceList *)aSourceList acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(NSInteger)index
1056 {
1057     NSPasteboard *o_pasteboard = [info draggingPasteboard];
1058
1059     playlist_t * p_playlist = pl_Get(VLCIntf);
1060     playlist_item_t *p_node;
1061
1062     if ([[item identifier] isEqualToString:@"playlist"])
1063         p_node = p_playlist->p_local_category;
1064     else
1065         p_node = p_playlist->p_ml_category;
1066
1067     if ([[o_pasteboard types] containsObject: NSFilenamesPboardType]) {
1068         NSArray *o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType] sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)];
1069         NSUInteger count = [o_values count];
1070         NSMutableArray *o_array = [NSMutableArray arrayWithCapacity:count];
1071
1072         for(NSUInteger i = 0; i < count; i++) {
1073             NSDictionary *o_dic;
1074             char *psz_uri = vlc_path2uri([[o_values objectAtIndex:i] UTF8String], NULL);
1075             if (!psz_uri)
1076                 continue;
1077
1078             o_dic = [NSDictionary dictionaryWithObject:[NSString stringWithCString:psz_uri encoding:NSUTF8StringEncoding] forKey:@"ITEM_URL"];
1079
1080             free(psz_uri);
1081
1082             [o_array addObject: o_dic];
1083         }
1084
1085         [[[VLCMain sharedInstance] playlist] appendNodeArray:o_array inNode: p_node atPos:-1 enqueue:YES];
1086         return YES;
1087     }
1088     else if ([[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"]) {
1089         NSArray * array = [[[VLCMain sharedInstance] playlist] draggedItems];
1090
1091         NSUInteger count = [array count];
1092         playlist_item_t * p_item = NULL;
1093
1094         PL_LOCK;
1095         for(NSUInteger i = 0; i < count; i++) {
1096             p_item = [[array objectAtIndex:i] pointerValue];
1097             if (!p_item) continue;
1098             playlist_NodeAddCopy(p_playlist, p_item, p_node, PLAYLIST_END);
1099         }
1100         PL_UNLOCK;
1101
1102         return YES;
1103     }
1104     return NO;
1105 }
1106
1107 - (id)sourceList:(PXSourceList *)aSourceList persistentObjectForItem:(id)item
1108 {
1109     return [item identifier];
1110 }
1111
1112 - (id)sourceList:(PXSourceList *)aSourceList itemForPersistentObject:(id)object
1113 {
1114     /* the following code assumes for sakes of simplicity that only the top level
1115      * items are allowed to have children */
1116
1117     NSArray * array = [NSArray arrayWithArray: o_sidebaritems]; // read-only arrays are noticebly faster
1118     NSUInteger count = [array count];
1119     if (count < 1)
1120         return nil;
1121
1122     for (NSUInteger x = 0; x < count; x++) {
1123         id item = [array objectAtIndex:x]; // save one objc selector call
1124         if ([[item identifier] isEqualToString:object])
1125             return item;
1126     }
1127
1128     return nil;
1129 }
1130
1131 #pragma mark -
1132 #pragma mark Podcast
1133
1134 - (IBAction)addPodcast:(id)sender
1135 {
1136     [NSApp beginSheet:o_podcast_subscribe_window modalForWindow:self modalDelegate:self didEndSelector:NULL contextInfo:nil];
1137 }
1138
1139 - (IBAction)addPodcastWindowAction:(id)sender
1140 {
1141     [o_podcast_subscribe_window orderOut:sender];
1142     [NSApp endSheet: o_podcast_subscribe_window];
1143
1144     if (sender == o_podcast_subscribe_ok_btn && [[o_podcast_subscribe_url_fld stringValue] length] > 0) {
1145         NSMutableString * podcastConf = [[NSMutableString alloc] init];
1146         if (config_GetPsz(VLCIntf, "podcast-urls") != NULL)
1147             [podcastConf appendFormat:@"%s|", config_GetPsz(VLCIntf, "podcast-urls")];
1148
1149         [podcastConf appendString: [o_podcast_subscribe_url_fld stringValue]];
1150         config_PutPsz(VLCIntf, "podcast-urls", [podcastConf UTF8String]);
1151         var_SetString(pl_Get(VLCIntf), "podcast-urls", [podcastConf UTF8String]);
1152         [podcastConf release];
1153     }
1154 }
1155
1156 - (IBAction)removePodcast:(id)sender
1157 {
1158     if (config_GetPsz(VLCIntf, "podcast-urls") != NULL) {
1159         [o_podcast_unsubscribe_pop removeAllItems];
1160         [o_podcast_unsubscribe_pop addItemsWithTitles:[@(config_GetPsz(VLCIntf, "podcast-urls")) componentsSeparatedByString:@"|"]];
1161         [NSApp beginSheet:o_podcast_unsubscribe_window modalForWindow:self modalDelegate:self didEndSelector:NULL contextInfo:nil];
1162     }
1163 }
1164
1165 - (IBAction)removePodcastWindowAction:(id)sender
1166 {
1167     [o_podcast_unsubscribe_window orderOut:sender];
1168     [NSApp endSheet: o_podcast_unsubscribe_window];
1169
1170     if (sender == o_podcast_unsubscribe_ok_btn) {
1171         NSMutableArray * urls = [[NSMutableArray alloc] initWithArray:[@(config_GetPsz(VLCIntf, "podcast-urls")) componentsSeparatedByString:@"|"]];
1172         [urls removeObjectAtIndex: [o_podcast_unsubscribe_pop indexOfSelectedItem]];
1173         config_PutPsz(VLCIntf, "podcast-urls", [[urls componentsJoinedByString:@"|"] UTF8String]);
1174         var_SetString(pl_Get(VLCIntf), "podcast-urls", config_GetPsz(VLCIntf, "podcast-urls"));
1175         [urls release];
1176
1177         /* reload the podcast module, since it won't update its list when removing podcasts */
1178         playlist_t * p_playlist = pl_Get(VLCIntf);
1179         if (playlist_IsServicesDiscoveryLoaded(p_playlist, "podcast{longname=\"Podcasts\"}")) {
1180             playlist_ServicesDiscoveryRemove(p_playlist, "podcast{longname=\"Podcasts\"}");
1181             playlist_ServicesDiscoveryAdd(p_playlist, "podcast{longname=\"Podcasts\"}");
1182             [o_playlist_table reloadData];
1183         }
1184
1185     }
1186 }
1187
1188 - (void)showPodcastControls
1189 {
1190     NSRect podcastViewDimensions = [o_podcast_view frame];
1191     NSRect rightSplitRect = [o_right_split_view frame];
1192     NSRect playlistTableRect = [o_playlist_table frame];
1193
1194     podcastViewDimensions.size.width = rightSplitRect.size.width;
1195     podcastViewDimensions.origin.x = podcastViewDimensions.origin.y = .0;
1196     [o_podcast_view setFrame:podcastViewDimensions];
1197
1198     playlistTableRect.origin.y = playlistTableRect.origin.y + podcastViewDimensions.size.height;
1199     playlistTableRect.size.height = playlistTableRect.size.height - podcastViewDimensions.size.height;
1200     [o_playlist_table setFrame:playlistTableRect];
1201     [o_playlist_table setNeedsDisplay:YES];
1202
1203     [o_right_split_view addSubview: o_podcast_view positioned: NSWindowAbove relativeTo: o_right_split_view];
1204     b_podcastView_displayed = YES;
1205 }
1206
1207 - (void)hidePodcastControls
1208 {
1209     if (b_podcastView_displayed) {
1210         NSRect podcastViewDimensions = [o_podcast_view frame];
1211         NSRect playlistTableRect = [o_playlist_table frame];
1212
1213         playlistTableRect.origin.y = playlistTableRect.origin.y - podcastViewDimensions.size.height;
1214         playlistTableRect.size.height = playlistTableRect.size.height + podcastViewDimensions.size.height;
1215
1216         [o_podcast_view removeFromSuperviewWithoutNeedingDisplay];
1217         [o_playlist_table setFrame: playlistTableRect];
1218         b_podcastView_displayed = NO;
1219     }
1220 }
1221
1222 @end
1223
1224 @implementation VLCDetachedVideoWindow
1225
1226 - (void)awakeFromNib
1227 {
1228     // sets lion fullscreen behaviour
1229     [super awakeFromNib];
1230     [self setAcceptsMouseMovedEvents: YES];
1231
1232     if (b_dark_interface) {
1233         [self setBackgroundColor: [NSColor clearColor]];
1234
1235         [self setOpaque: NO];
1236         [self display];
1237         [self setHasShadow:NO];
1238         [self setHasShadow:YES];
1239
1240         NSRect winrect = [self frame];
1241         CGFloat f_titleBarHeight = [o_titlebar_view frame].size.height;
1242
1243         [self setTitle: _NS("VLC media player")];
1244         [o_titlebar_view setFrame: NSMakeRect(0, winrect.size.height - f_titleBarHeight, winrect.size.width, f_titleBarHeight)];
1245         [[self contentView] addSubview: o_titlebar_view positioned: NSWindowAbove relativeTo: nil];
1246
1247     } else {
1248         [self setBackgroundColor: [NSColor blackColor]];
1249     }
1250
1251     NSRect videoViewRect = [[self contentView] bounds];
1252     if (b_dark_interface)
1253         videoViewRect.size.height -= [o_titlebar_view frame].size.height;
1254     CGFloat f_bottomBarHeight = [[self controlsBar] height];
1255     videoViewRect.size.height -= f_bottomBarHeight;
1256     videoViewRect.origin.y = f_bottomBarHeight;
1257     [o_video_view setFrame: videoViewRect];
1258
1259     if (b_dark_interface) {
1260         o_color_backdrop = [[VLCColorView alloc] initWithFrame: [o_video_view frame]];
1261         [[self contentView] addSubview: o_color_backdrop positioned: NSWindowBelow relativeTo: o_video_view];
1262         [o_color_backdrop setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
1263
1264         [self setContentMinSize: NSMakeSize(363., f_min_video_height + [[self controlsBar] height] + [o_titlebar_view frame].size.height)];
1265     } else {
1266         [self setContentMinSize: NSMakeSize(363., f_min_video_height + [[self controlsBar] height])];
1267     }
1268 }
1269
1270 - (void)dealloc
1271 {
1272     if (b_dark_interface)
1273         [o_color_backdrop release];
1274
1275     [super dealloc];
1276 }
1277
1278 @end