]> git.sesse.net Git - vlc/blobdiff - modules/gui/macosx/MainMenu.m
macosx: robustify playlist table columns initialization
[vlc] / modules / gui / macosx / MainMenu.m
index 6f169080156d7458a7318ffe4797134c9bf7b1d1..baa83d2f7fdf98f42f7bf9b56046625bf095cff8 100644 (file)
@@ -44,6 +44,8 @@
 #import "ControlsBar.h"
 #import "ExtensionsManager.h"
 #import "ConvertAndSave.h"
+#import "DebugMessageVisualizer.h"
+#import "AddonManager.h"
 
 @implementation VLCMainMenu
 static VLCMainMenu *_o_sharedInstance = nil;
@@ -75,11 +77,13 @@ static VLCMainMenu *_o_sharedInstance = nil;
                       _NS("Date"),          DATE_COLUMN,
                       _NS("Language"),      LANGUAGE_COLUMN,
                       _NS("URI"),           URI_COLUMN,
+                      _NS("File Size"),     FILESIZE_COLUMN,
                       nil];
         // this array also assigns tags (index) to type of menu item
         o_ptc_menuorder = [[NSArray alloc] initWithObjects: TRACKNUM_COLUMN, TITLE_COLUMN,
                            ARTIST_COLUMN, DURATION_COLUMN, GENRE_COLUMN, ALBUM_COLUMN,
-                           DESCRIPTION_COLUMN, DATE_COLUMN, LANGUAGE_COLUMN, URI_COLUMN, nil];
+                           DESCRIPTION_COLUMN, DATE_COLUMN, LANGUAGE_COLUMN, URI_COLUMN,
+                           FILESIZE_COLUMN,nil];
     }
 
     return _o_sharedInstance;
@@ -126,7 +130,7 @@ static VLCMainMenu *_o_sharedInstance = nil;
 
     /* check whether the user runs OSX with a RTL language */
     NSArray* languages = [NSLocale preferredLanguages];
-    NSString* preferredLanguage = languages[0];
+    NSString* preferredLanguage = [languages objectAtIndex:0];
 
     if ([NSLocale characterDirectionForLanguage:preferredLanguage] == NSLocaleLanguageDirectionRightToLeft) {
         msg_Dbg(VLCIntf, "adapting interface since '%s' is a RTL language", [preferredLanguage UTF8String]);
@@ -288,12 +292,15 @@ static VLCMainMenu *_o_sharedInstance = nil;
             mi = [[NSMenuItem alloc] initWithTitle: _NS(p_item->list_text[i]) action:NULL keyEquivalent: @""];
         else if (p_item->list.i[i])
             mi = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"%d", p_item->list.i[i]] action:NULL keyEquivalent: @""];
-        else
+        else {
             msg_Err(p_intf, "item %d of pref %s failed to be created", i, psz_name);
+            continue;
+        }
+
         [mi setTarget:self];
         [mi setAction:selector];
         [mi setTag:p_item->list.i[i]];
-        [mi setRepresentedObject:@(psz_name)];
+        [mi setRepresentedObject:[NSString stringWithUTF8String:psz_name]];
         [menu addItem: [mi autorelease]];
         if (p_item->value.i == p_item->list.i[i])
             [mi setState:NSOnState];
@@ -309,6 +316,7 @@ static VLCMainMenu *_o_sharedInstance = nil;
     [o_mi_prefs setTitle: _NS("Preferences...")];
     [o_mi_extensions setTitle: _NS("Extensions")];
     [o_mu_extensions setTitle: _NS("Extensions")];
+    [o_mi_addonManager setTitle: _NS("Addon Manager")];
     [o_mi_add_intf setTitle: _NS("Add Interface")];
     [o_mu_add_intf setTitle: _NS("Add Interface")];
     [o_mi_services setTitle: _NS("Services")];
@@ -324,8 +332,10 @@ static VLCMainMenu *_o_sharedInstance = nil;
     [o_mi_open_net setTitle: _NS("Open Network...")];
     [o_mi_open_capture setTitle: _NS("Open Capture Device...")];
     [o_mi_open_recent setTitle: _NS("Open Recent")];
+    [o_mi_close_window setTitle: _NS("Close Window")];
     [o_mi_open_wizard setTitle: _NS("Streaming/Exporting Wizard...")];
     [o_mi_convertandsave setTitle: _NS("Convert / Stream...")];
+    [o_mi_save_playlist setTitle: _NS("Save Playlist...")];
 
     [o_mu_edit setTitle: _NS("Edit")];
     [o_mi_cut setTitle: _NS("Cut")];
@@ -344,6 +354,7 @@ static VLCMainMenu *_o_sharedInstance = nil;
     [o_mi_toggleSidebar setTitle: _NS("Show Sidebar")];
     [o_mi_toggleSidebar setState: config_GetInt(VLCIntf, "macosx-show-sidebar")];
     [o_mu_playlistTableColumns setTitle: _NS("Playlist Table Columns")];
+    [o_mi_playlistTableColumns setTitle: _NS("Playlist Table Columns")];
 
     [o_mu_controls setTitle: _NS("Playback")];
     [o_mi_play setTitle: _NS("Play")];
@@ -379,8 +390,8 @@ static VLCMainMenu *_o_sharedInstance = nil;
     [o_mi_mute setTitle: _NS("Mute")];
     [o_mi_audiotrack setTitle: _NS("Audio Track")];
     [o_mu_audiotrack setTitle: _NS("Audio Track")];
-    [o_mi_channels setTitle: _NS("Audio Channels")];
-    [o_mu_channels setTitle: _NS("Audio Channels")];
+    [o_mi_channels setTitle: _NS("Stereo audio mode")];
+    [o_mu_channels setTitle: _NS("Stereo audio mode")];
     [o_mi_device setTitle: _NS("Audio Device")];
     [o_mu_device setTitle: _NS("Audio Device")];
     [o_mi_visual setTitle: _NS("Visualizations")];
@@ -429,8 +440,8 @@ static VLCMainMenu *_o_sharedInstance = nil;
     [o_mi_teletext_blue setTitle: _NS("Blue")];
 
     [o_mu_window setTitle: _NS("Window")];
-    [o_mi_minimize setTitle: _NS("Minimize Window")];
-    [o_mi_close_window setTitle: _NS("Close Window")];
+    [o_mi_minimize setTitle: _NS("Minimize")];
+    [o_mi_zoom_window setTitle: _NS("Zoom")];
     [o_mi_player setTitle: _NS("Player...")];
     [o_mi_controller setTitle: _NS("Main Window...")];
     [o_mi_audioeffects setTitle: _NS("Audio Effects...")];
@@ -478,12 +489,12 @@ static VLCMainMenu *_o_sharedInstance = nil;
     NSMenuItem *o_mi_tmp;
     NSUInteger count = [o_ptc_menuorder count];
     for (NSUInteger i = 0; i < count; i++) {
-        NSString *o_title = [o_ptc_translation_dict objectForKey:o_ptc_menuorder[i]];
+        NSString *o_title = [o_ptc_translation_dict objectForKey:[o_ptc_menuorder objectAtIndex:i]];
         o_mi_tmp = [o_mu_playlistTableColumns addItemWithTitle:o_title
                                                         action:@selector(togglePlaylistColumnTable:)
                                                  keyEquivalent:@""];
         /* don't set a valid target for the title column selector, since we want it to be disabled */
-        if (![o_ptc_menuorder[i] isEqualToString: TITLE_COLUMN])
+        if (![[o_ptc_menuorder objectAtIndex:i] isEqualToString: TITLE_COLUMN])
             [o_mi_tmp setTarget:self];
         [o_mi_tmp setTag:i];
 
@@ -491,7 +502,7 @@ static VLCMainMenu *_o_sharedInstance = nil;
                                              action:@selector(togglePlaylistColumnTable:)
                                       keyEquivalent:@""];
         /* don't set a valid target for the title column selector, since we want it to be disabled */
-        if (![o_ptc_menuorder[i] isEqualToString: TITLE_COLUMN])
+        if (![[o_ptc_menuorder objectAtIndex:i] isEqualToString: TITLE_COLUMN])
             [o_mi_tmp setTarget:self];
         [o_mi_tmp setTag:i];
     }
@@ -510,7 +521,7 @@ static VLCMainMenu *_o_sharedInstance = nil;
     NSArray *menuitems_array = [the_menu itemArray];
     NSUInteger menuItemCount = [menuitems_array count];
     for (NSUInteger i=0; i < menuItemCount; i++) {
-        NSMenuItem *one_item = menuitems_array[i];
+        NSMenuItem *one_item = [menuitems_array objectAtIndex:i];
         if ([one_item hasSubmenu])
             [self releaseRepresentedObjects: [one_item submenu]];
 
@@ -571,11 +582,9 @@ static VLCMainMenu *_o_sharedInstance = nil;
             [self setupVarMenuItem: o_mi_deinterlace_mode target: (vlc_object_t *)p_vout
                                      var: "deinterlace-mode" selector: @selector(toggleVar:)];
 
-#if 1
-            [self setupVarMenuItem: o_mi_ffmpeg_pp target:
-             (vlc_object_t *)p_vout var:"postprocess" selector:
-             @selector(toggleVar:)];
-#endif
+            [self setupVarMenuItem: o_mi_ffmpeg_pp target: (vlc_object_t *)p_vout
+                               var:"postprocess" selector: @selector(toggleVar:)];
+
             vlc_object_release(p_vout);
 
             [self refreshVoutDeviceMenu:nil];
@@ -588,7 +597,7 @@ static VLCMainMenu *_o_sharedInstance = nil;
 
 - (void)refreshVoutDeviceMenu:(NSNotification *)o_notification
 {
-    NSUInteger count = [o_mu_screen numberOfItems];
+    NSUInteger count = (NSUInteger) [o_mu_screen numberOfItems];
     NSMenu * o_submenu = o_mu_screen;
     if (count > 0)
         [o_submenu removeAllItems];
@@ -604,11 +613,11 @@ static VLCMainMenu *_o_sharedInstance = nil;
     [o_mitem setTarget: self];
     NSRect s_rect;
     for (NSUInteger i = 0; i < count; i++) {
-        s_rect = [o_screens[i] frame];
+        s_rect = [[o_screens objectAtIndex:i] frame];
         [o_submenu addItemWithTitle: [NSString stringWithFormat: @"%@ %li (%ix%i)", _NS("Screen"), i+1,
                                       (int)s_rect.size.width, (int)s_rect.size.height] action:@selector(toggleFullscreenDevice:) keyEquivalent:@""];
         o_mitem = [o_submenu itemAtIndex:i+1];
-        [o_mitem setTag: (int)[o_screens[i] displayID]];
+        [o_mitem setTag: (int)[[o_screens objectAtIndex:i] displayID]];
         [o_mitem setEnabled: YES];
         [o_mitem setTarget: self];
     }
@@ -704,7 +713,12 @@ static VLCMainMenu *_o_sharedInstance = nil;
 {
     BOOL b_value = !config_GetInt(VLCIntf, "macosx-show-playback-buttons");
     config_PutInt(VLCIntf, "macosx-show-playback-buttons", b_value);
+
     [[[[VLCMain sharedInstance] mainWindow] controlsBar] toggleJumpButtons];
+    [[[VLCMain sharedInstance] voutController] updateWindowsUsingBlock:^(VLCVideoWindowCommon *o_window) {
+        [[o_window controlsBar] toggleForwardBackwardMode: b_value];
+    }];
+
     [o_mi_toggleJumpButtons setState: b_value];
 }
 
@@ -733,20 +747,35 @@ static VLCMainMenu *_o_sharedInstance = nil;
     [[o_mu_playlistTableColumns            itemWithTag: i_tag] setState: i_new_state];
     [[o_mu_playlistTableColumnsContextMenu itemWithTag: i_tag] setState: i_new_state];
 
-    NSString *o_column = o_ptc_menuorder[i_tag];
+    NSString *o_column = [o_ptc_menuorder objectAtIndex:i_tag];
     [[[VLCMain sharedInstance] playlist] setColumn: o_column state: i_new_state translationDict: o_ptc_translation_dict];
 }
 
-- (void)setPlaylistColumnTableState:(NSInteger)i_state forColumn:(NSString *)o_column
+- (BOOL)setPlaylistColumnTableState:(NSInteger)i_state forColumn:(NSString *)o_column
 {
-    NSInteger i_tag = [o_ptc_menuorder indexOfObject: o_column];
+    NSUInteger i_tag = [o_ptc_menuorder indexOfObject: o_column];
+    // prevent setting unknown columns
+    if(i_tag == NSNotFound)
+        return NO;
+
     [[o_mu_playlistTableColumns            itemWithTag: i_tag] setState: i_state];
     [[o_mu_playlistTableColumnsContextMenu itemWithTag: i_tag] setState: i_state];
     [[[VLCMain sharedInstance] playlist] setColumn: o_column state: i_state translationDict: o_ptc_translation_dict];
+
+    return YES;
 }
 
 #pragma mark -
 #pragma mark Playback
+
+- (IBAction)quitAfterPlayback:(id)sender
+{
+    playlist_t *p_playlist = pl_Get(VLCIntf);
+    bool b_value = !var_CreateGetBool(p_playlist, "play-and-exit");
+    var_SetBool(p_playlist, "play-and-exit", b_value);
+    config_PutInt(p_intf, "play-and-exit", b_value);
+}
+
 - (IBAction)toggleRecord:(id)sender
 {
     [[VLCCoreInteraction sharedInstance] toggleRecord];
@@ -801,7 +830,7 @@ static VLCMainMenu *_o_sharedInstance = nil;
     NSMenuItem * o_mi_tmp;
 
     for (NSUInteger x = 0; x < n; x++) {
-        o_mi_tmp = [o_mu_device addItemWithTitle:[NSString stringWithFormat:@"%s", names[x]] action:@selector(toggleAudioDevice:) keyEquivalent:@""];
+        o_mi_tmp = [o_mu_device addItemWithTitle:toNSStr(names[x]) action:@selector(toggleAudioDevice:) keyEquivalent:@""];
         [o_mi_tmp setTarget:self];
         [o_mi_tmp setTag:[[NSString stringWithFormat:@"%s", ids[x]] intValue]];
     }
@@ -809,6 +838,8 @@ static VLCMainMenu *_o_sharedInstance = nil;
 
     [[o_mu_device itemWithTag:[[NSString stringWithFormat:@"%s", currentDevice] intValue]] setState:NSOnState];
 
+    free(currentDevice);
+
     for (NSUInteger x = 0; x < n; x++) {
         free(ids[x]);
         free(names[x]);
@@ -922,6 +953,7 @@ static VLCMainMenu *_o_sharedInstance = nil;
     }
 
     char *path = input_item_GetURI(p_item);
+
     if (!path)
         path = strdup("");
 
@@ -929,11 +961,16 @@ static VLCMainMenu *_o_sharedInstance = nil;
     [openPanel setCanChooseFiles: YES];
     [openPanel setCanChooseDirectories: NO];
     [openPanel setAllowsMultipleSelection: YES];
-    [openPanel setAllowedFileTypes: @[@"cdg",@"@idx",@"srt",@"sub",@"utf",@"ass",@"ssa",@"aqt",@"jss",@"psb",@"rt",@"smi",@"txt",@"smil"]];
-    [openPanel setDirectoryURL:[NSURL fileURLWithPath:[@(path) stringByExpandingTildeInPath]]];
-    i_returnValue = [openPanel runModal];
+
+    [openPanel setAllowedFileTypes: [NSArray arrayWithObjects:@"cdg",@"idx",@"srt",@"sub",@"utf",@"ass",@"ssa",@"aqt",@"jss",@"psb",@"rt",@"smi",@"txt",@"smil",nil]];
+
+    NSURL *o_url = [NSURL URLWithString:[[NSString stringWithUTF8String:path] stringByExpandingTildeInPath]];
+    o_url = [o_url URLByDeletingLastPathComponent];
+    [openPanel setDirectoryURL: o_url];
     free(path);
 
+    i_returnValue = [openPanel runModal];
+
     if (i_returnValue == NSOKButton) {
         NSUInteger c = 0;
         if (!p_input)
@@ -942,10 +979,10 @@ static VLCMainMenu *_o_sharedInstance = nil;
         c = [[openPanel URLs] count];
 
         for (int i = 0; i < c ; i++) {
-            msg_Dbg(VLCIntf, "loading subs from %s", [[[openPanel URLs][i] path] UTF8String]);
-            if (input_AddSubtitle(p_input, [[[openPanel URLs][i] path] UTF8String], TRUE))
+            msg_Dbg(VLCIntf, "loading subs from %s", [[[[openPanel URLs] objectAtIndex:i] path] UTF8String]);
+            if (input_AddSubtitle(p_input, [[[[openPanel URLs] objectAtIndex:i] path] UTF8String], TRUE))
                 msg_Warn(VLCIntf, "unable to load subtitles from '%s'",
-                         [[[openPanel URLs][i] path] UTF8String]);
+                         [[[[openPanel URLs] objectAtIndex:i] path] UTF8String]);
         }
     }
     vlc_object_release(p_input);
@@ -953,34 +990,21 @@ static VLCMainMenu *_o_sharedInstance = nil;
 
 - (IBAction)switchSubtitleOption:(id)sender
 {
-    vlc_object_t *p_freetype;
-    p_freetype = (vlc_object_t *) vlc_object_find_name(pl_Get(VLCIntf), "freetype");
     int intValue = [sender tag];
     NSString *representedObject = [sender representedObject];
 
-    if (p_freetype) {
-        var_SetInteger(p_freetype, [representedObject UTF8String], intValue);
-        NSMenu *menu = [sender menu];
-        NSUInteger count = [menu numberOfItems];
-        for (NSUInteger x = 0; x < count; x++)
-            [[menu itemAtIndex:x] setState:NSOffState];
-        [[menu itemWithTag:intValue] setState:NSOnState];
-        vlc_object_release(p_freetype);
-    }
-    config_PutInt(p_freetype, [representedObject UTF8String], intValue);
+    config_PutInt(p_intf, [representedObject UTF8String], intValue);
+
+    NSMenu *menu = [sender menu];
+    NSUInteger count = (NSUInteger) [menu numberOfItems];
+    for (NSUInteger x = 0; x < count; x++)
+        [[menu itemAtIndex:x] setState:NSOffState];
+    [[menu itemWithTag:intValue] setState:NSOnState];
 }
 
 - (IBAction)switchSubtitleBackgroundOpacity:(id)sender
 {
-    vlc_object_t *p_freetype;
-    p_freetype = (vlc_object_t *) vlc_object_find_name(pl_Get(VLCIntf), "freetype");
-    int intValue = [sender intValue];
-
-    if (p_freetype) {
-        var_SetInteger(p_freetype, "freetype-background-opacity", intValue);
-        vlc_object_release(p_freetype);
-    }
-    config_PutInt(p_freetype, "freetype-background-opacity", intValue);
+    config_PutInt(p_intf, "freetype-background-opacity", [sender intValue]);
 }
 
 - (IBAction)telxTransparent:(id)sender
@@ -996,7 +1020,6 @@ static VLCMainMenu *_o_sharedInstance = nil;
 
 - (IBAction)telxNavLink:(id)sender
 {
-    intf_thread_t * p_intf = VLCIntf;
     vlc_object_t *p_vbi;
     int i_page = 0;
 
@@ -1053,6 +1076,11 @@ static VLCMainMenu *_o_sharedInstance = nil;
     [[[VLCMain sharedInstance] wizard] showWizard];
 }
 
+- (IBAction)savePlaylist:(id)sender
+{
+    [[[VLCMain sharedInstance] playlist] savePlaylist:sender];
+}
+
 - (IBAction)showConvertAndSave:(id)sender
 {
     if (o_convertandsave == nil)
@@ -1108,6 +1136,32 @@ static VLCMainMenu *_o_sharedInstance = nil;
     [[[VLCMain sharedInstance] simplePreferences] showSimplePrefsWithLevel:i_level];
 }
 
+- (IBAction)openAddonManager:(id)sender
+{
+    if (!o_addonManager)
+        o_addonManager = [[VLCAddonManager alloc] init];
+
+    if (!b_nib_addonmanager_loaded)
+        b_nib_addonmanager_loaded = [NSBundle loadNibNamed:@"AddonManager" owner:NSApp];
+
+    [o_addonManager showWindow];
+}
+
+- (IBAction)showMessagesPanel:(id)showMessagesPanel
+{
+    [[VLCDebugMessageVisualizer sharedInstance] showPanel];
+}
+
+- (IBAction)showMainWindow:(id)sender
+{
+    [[VLCMainWindow sharedInstance] makeKeyAndOrderFront:sender];
+}
+
+- (IBAction)showPlaylist:(id)sender
+{
+    [[VLCMainWindow sharedInstance] changePlaylistState: psUserMenuEvent];
+}
+
 #pragma mark -
 #pragma mark Help and Docs
 
@@ -1249,6 +1303,7 @@ static VLCMainMenu *_o_sharedInstance = nil;
             break;
         default:
             /* Variable doesn't exist or isn't handled */
+            msg_Warn(p_object, "variable %s doesn't exist or isn't handled", psz_variable);
             return;
     }
 
@@ -1266,9 +1321,8 @@ static VLCMainMenu *_o_sharedInstance = nil;
         return;
     }
 
-    if (var_Get(p_object, psz_variable, &val) < 0) {
+    if (var_Get(p_object, psz_variable, &val) < 0)
         return;
-    }
 
     VLCAutoGeneratedMenuContent *o_data;
     switch(i_type & VLC_VAR_TYPE) {
@@ -1478,12 +1532,12 @@ static VLCMainMenu *_o_sharedInstance = nil;
         [o_mi setState: i_state];
     } else if ([o_title isEqualToString: _NS("Quit after Playback")]) {
         int i_state;
-        var_Get(p_playlist, "play-and-exit", &val);
-        i_state = val.b_bool ? NSOnState : NSOffState;
+        bool b_value = var_InheritBool(p_playlist, "play-and-exit");
+        i_state = b_value ? NSOnState : NSOffState;
         [o_mi setState: i_state];
     } else if ([o_title isEqualToString: _NS("Step Forward")] ||
                [o_title isEqualToString: _NS("Step Backward")] ||
-               [o_title isEqualToString: _NS("Jump To Time")]) {
+               [o_title isEqualToString: _NS("Jump to Time")]) {
         if (p_input != NULL) {
             var_Get(p_input, "can-seek", &val);
             bEnabled = val.b_bool;
@@ -1517,6 +1571,10 @@ static VLCMainMenu *_o_sharedInstance = nil;
         }
 
         [self setupMenus]; /* Make sure video menu is up to date */
+
+    } else if ([o_title isEqualToString: _NS("Add Subtitle File...")]) {
+        bEnabled = [o_mi isEnabled];
+        [self setupMenus]; /* Make sure subtitles menu is up to date */
     } else {
         NSMenuItem *o_mi_parent = [o_mi parentItem];
         if (o_mi_parent == o_mi_subtitle_size || o_mi == o_mi_subtitle_size ||