]> git.sesse.net Git - vlc/blobdiff - modules/gui/macosx/bookmarks.m
macosx: avoid possible index out of range problem in bookmarks table view
[vlc] / modules / gui / macosx / bookmarks.m
index f0fb02e7171b049dcc2fbd2257b75f935c187783..adb1b6ae6e1bc5d367c713556a2fd3426f937fdb 100644 (file)
@@ -1,10 +1,10 @@
 /*****************************************************************************
  * bookmarks.m: MacOS X Bookmarks window
  *****************************************************************************
- * Copyright (C) 2005 - 2007 the VideoLAN team
+ * Copyright (C) 2005 - 2012 VLC authors and VideoLAN
  * $Id$
  *
- * Authors: Felix Kühne <fkuehne@users.sf.net>
+ * Authors: Felix Paul Kühne <fkuehne at videolan dot org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 #import "bookmarks.h"
 #import "wizard.h"
-#import <vlc_interface.h>
+#import "CompatibilityFixes.h"
 
-/*****************************************************************************
- * VLCExtended implementation
- *
- * implements the GUI functions for the window, the data source and the
- * delegate for o_tbl_dataTable
- *****************************************************************************/
+@interface VLCBookmarks (Internal)
+- (void)initStrings;
+@end
 
 @implementation VLCBookmarks
 
@@ -56,11 +53,10 @@ static VLCBookmarks *_o_sharedInstance = nil;
 
 - (id)init
 {
-    if (_o_sharedInstance) {
+    if (_o_sharedInstance)
         [self dealloc];
-    } else {
+    else
         _o_sharedInstance = [super init];
-    }
 
     return _o_sharedInstance;
 }
@@ -71,18 +67,24 @@ static VLCBookmarks *_o_sharedInstance = nil;
 
 - (void)awakeFromNib
 {
+    if (!OSX_SNOW_LEOPARD)
+        [o_bookmarks_window setCollectionBehavior: NSWindowCollectionBehaviorFullScreenAuxiliary];
+
     [self initStrings];
 }
 
 - (void)dealloc
 {
+    if (p_old_input)
+        vlc_object_release(p_old_input);
+
     [super dealloc];
 }
 
 - (void)initStrings
 {
     /* localise the items */
+
     /* main window */
     [o_bookmarks_window setTitle: _NS("Bookmarks")];
     [o_btn_add setTitle: _NS("Add")];
@@ -96,7 +98,7 @@ static VLCBookmarks *_o_sharedInstance = nil;
         setStringValue: _NS("Position")];
     [[[o_tbl_dataTable tableColumnWithIdentifier:@"time_offset"] headerCell]
         setStringValue: _NS("Time")];
+
     /* edit window */
     [o_edit_btn_ok setTitle: _NS("OK")];
     [o_edit_btn_cancel setTitle: _NS("Cancel")];
@@ -105,50 +107,57 @@ static VLCBookmarks *_o_sharedInstance = nil;
     [o_edit_lbl_bytes setStringValue: _NS("Position")];
 }
 
+- (void)updateCocoaWindowLevel:(NSInteger)i_level
+{
+    if (o_bookmarks_window && [o_bookmarks_window isVisible] && [o_bookmarks_window level] != i_level)
+        [o_bookmarks_window setLevel: i_level];
+}
+
 - (void)showBookmarks
 {
     /* show the window, called from intf.m */
     [o_bookmarks_window displayIfNeeded];
+    [o_bookmarks_window setLevel: [[[VLCMain sharedInstance] voutController] currentWindowLevel]];
     [o_bookmarks_window makeKeyAndOrderFront:nil];
 }
 
+- (void)refresh
+{
+    [o_tbl_dataTable reloadData];
+}
+
 - (IBAction)add:(id)sender
 {
     /* add item to list */
-    input_thread_t * p_input = pl_CurrentInput( VLCIntf );
+    input_thread_t * p_input = pl_CurrentInput(VLCIntf);
+
+    if (!p_input)
+        return;
 
-    if( !p_input ) return;
     seekpoint_t bookmark;
-    vlc_value_t pos;
-    bookmark.psz_name = NULL;
-    bookmark.i_byte_offset = 0;
-    bookmark.i_time_offset = 0;
-    var_Get( VLCIntf, "position", &pos);
-    bookmark.psz_name = _("Untitled");
-    input_Control( p_input, INPUT_GET_BYTE_POSITION, &bookmark.i_byte_offset );
-    var_Get( p_input, "time", &pos );
-    bookmark.i_time_offset = pos.i_time;
-    input_Control( p_input, INPUT_ADD_BOOKMARK, &bookmark );
-    vlc_object_release( p_input );
+
+    if (!input_Control(p_input, INPUT_GET_BOOKMARK, &bookmark)) {
+        bookmark.psz_name = _("Untitled");
+        input_Control(p_input, INPUT_ADD_BOOKMARK, &bookmark);
+    }
+
+    vlc_object_release(p_input);
+
     [o_tbl_dataTable reloadData];
 }
 
 - (IBAction)clear:(id)sender
 {
     /* clear table */
-    input_thread_t * p_input = pl_CurrentInput( VLCIntf );
-    if( !p_input )
+    input_thread_t * p_input = pl_CurrentInput(VLCIntf);
+
+    if (!p_input)
         return;
 
-    input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
+    input_Control(p_input, INPUT_CLEAR_BOOKMARKS);
+
+    vlc_object_release(p_input);
 
-    vlc_object_release( p_input );
     [o_tbl_dataTable reloadData];
 }
 
@@ -157,46 +166,45 @@ static VLCBookmarks *_o_sharedInstance = nil;
     /* put values to the sheet's fields and show sheet */
     /* we take the values from the core and not the table, because we cannot
      * really trust it */
-    input_thread_t * p_input = pl_CurrentInput( VLCIntf );
+    input_thread_t * p_input = pl_CurrentInput(VLCIntf);
     seekpoint_t **pp_bookmarks;
     int i_bookmarks;
     int row;
     row = [o_tbl_dataTable selectedRow];
-    if( !p_input )
-    {
+
+    if (!p_input)
         return;
-    }
-    else if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
-        &i_bookmarks ) != VLC_SUCCESS )
-    {
-        vlc_object_release( p_input );
+
+    if (row < 0) {
+        vlc_object_release(p_input);
         return;
     }
-    else if(row < 0)
-    {
-        vlc_object_release( p_input );
+
+    if (input_Control(p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks, &i_bookmarks) != VLC_SUCCESS) {
+        vlc_object_release(p_input);
         return;
-    } else {
-        [o_edit_fld_name setStringValue: [NSString stringWithUTF8String:
-            pp_bookmarks[row]->psz_name]];
-        [o_edit_fld_time setStringValue: [[NSNumber numberWithInt:
-            (pp_bookmarks[row]->i_time_offset / 1000000)] stringValue]];
-        [o_edit_fld_bytes setStringValue: [[NSNumber numberWithInt:
-            pp_bookmarks[row]->i_byte_offset] stringValue]];
     }
+
+    [o_edit_fld_name setStringValue: toNSStr(pp_bookmarks[row]->psz_name)];
+    int total = pp_bookmarks[row]->i_time_offset/ 1000000;
+    int hour = total / (60*60);
+    int min = (total - hour*60*60) / 60;
+    int sec = total - hour*60*60 - min*60;
+    [o_edit_fld_time setStringValue: [NSString stringWithFormat:@"%02d:%02d:%02d", hour, min, sec]];
+    [o_edit_fld_bytes setStringValue: [NSString stringWithFormat:@"%lli", pp_bookmarks[row]->i_byte_offset]];
+
     /* Just keep the pointer value to check if it
      * changes. Note, we don't need to keep a reference to the object.
      * so release it now. */
     p_old_input = p_input;
-    vlc_object_release( p_input );
+    vlc_object_release(p_input);
 
-    [NSApp beginSheet: o_edit_window
-        modalForWindow: o_bookmarks_window
-        modalDelegate: o_edit_window
-        didEndSelector: nil
-        contextInfo: nil];
+    [NSApp beginSheet: o_edit_window modalForWindow: o_bookmarks_window modalDelegate: o_edit_window didEndSelector: nil contextInfo: nil];
+
+    // Clear the bookmark list
+    for (int i = 0; i < i_bookmarks; i++)
+        vlc_seekpoint_Delete(pp_bookmarks[i]);
+    free(pp_bookmarks);
 }
 
 - (IBAction)edit_cancel:(id)sender
@@ -211,149 +219,133 @@ static VLCBookmarks *_o_sharedInstance = nil;
     /* save field contents and close sheet */
      seekpoint_t **pp_bookmarks;
     int i_bookmarks, i;
-    input_thread_t * p_input = pl_CurrentInput( VLCIntf );
-    if( !p_input )
-    {
-        NSBeginCriticalAlertSheet(_NS("No input"), _NS("OK"),
-                @"", @"", o_bookmarks_window, nil, nil, nil, nil, _NS("No "
-                "input found. A stream must be playing or paused for "
-                "bookmarks to work."));
+    input_thread_t * p_input = pl_CurrentInput(VLCIntf);
+
+    if (!p_input) {
+        NSBeginCriticalAlertSheet(_NS("No input"), _NS("OK"), @"", @"", o_bookmarks_window, nil, nil, nil, nil, @"%@",_NS("No input found. A stream must be playing or paused for bookmarks to work."));
         return;
     }
-    if( p_old_input != p_input )
-    {
-        NSBeginCriticalAlertSheet(_NS("Input has changed"), _NS("OK"),
-            @"", @"", o_bookmarks_window, nil, nil, nil, nil, _NS("Input "
-            "has changed, unable to save bookmark. Suspending playback with "
-            "\"Pause\" while editing bookmarks to ensure to keep the same "
-            "input."));
-        vlc_object_release( p_input );
+    if (p_old_input != p_input) {
+        NSBeginCriticalAlertSheet(_NS("Input has changed"), _NS("OK"), @"", @"", o_bookmarks_window, nil, nil, nil, nil, @"%@",_NS("Input has changed, unable to save bookmark. Suspending playback with \"Pause\" while editing bookmarks to ensure to keep the same input."));
+        vlc_object_release(p_input);
         return;
     }
-    if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
-        &i_bookmarks ) != VLC_SUCCESS )
-    {
-        vlc_object_release( p_input );
+
+    if (input_Control(p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks, &i_bookmarks) != VLC_SUCCESS) {
+        vlc_object_release(p_input);
         return;
     }
 
     i = [o_tbl_dataTable selectedRow];
-    free( pp_bookmarks[i]->psz_name );
+
+    free(pp_bookmarks[i]->psz_name);
 
     pp_bookmarks[i]->psz_name = strdup([[o_edit_fld_name stringValue] UTF8String]);
     pp_bookmarks[i]->i_byte_offset = [[o_edit_fld_bytes stringValue] intValue];
-    pp_bookmarks[i]->i_time_offset = ([[o_edit_fld_time stringValue] intValue]  * 1000000);
-    if( input_Control( p_input, INPUT_CHANGE_BOOKMARK, pp_bookmarks[i], i )
-        != VLC_SUCCESS )
-    {
-        msg_Warn( VLCIntf, "Unable to change the bookmark");
-        vlc_object_release( p_input );
-        return;
+
+    NSArray * components = [[o_edit_fld_time stringValue] componentsSeparatedByString:@":"];
+    NSUInteger componentCount = [components count];
+    if (componentCount == 1)
+        pp_bookmarks[i]->i_time_offset = 1000000 * ([[components objectAtIndex:0] intValue]);
+    else if (componentCount == 2)
+        pp_bookmarks[i]->i_time_offset = 1000000 * ([[components objectAtIndex:0] intValue] * 60 + [[components objectAtIndex:1] intValue]);
+    else if (componentCount == 3)
+        pp_bookmarks[i]->i_time_offset = 1000000 * ([[components objectAtIndex:0] intValue] * 3600 + [[components objectAtIndex:1] intValue] * 60 + [[components objectAtIndex:2] intValue]);
+    else {
+        msg_Err(VLCIntf, "Invalid string format for time");
+        goto clear;
+    }
+
+    if (input_Control(p_input, INPUT_CHANGE_BOOKMARK, pp_bookmarks[i], i) != VLC_SUCCESS) {
+        msg_Warn(VLCIntf, "Unable to change the bookmark");
+        goto clear;
     }
+
     [o_tbl_dataTable reloadData];
-    vlc_object_release( p_input );
+    vlc_object_release(p_input);
+
     [NSApp endSheet: o_edit_window];
     [o_edit_window close];
+
+clear:
+    // Clear the bookmark list
+    for (int i = 0; i < i_bookmarks; i++)
+        vlc_seekpoint_Delete(pp_bookmarks[i]);
+    free(pp_bookmarks);
 }
 
 - (IBAction)extract:(id)sender
 {
-    /* extract */
-    if( [o_tbl_dataTable numberOfSelectedRows] < 2 )
-    {
-        NSBeginAlertSheet(_NS("Invalid selection"), _NS("OK"),
-            @"", @"", o_bookmarks_window, nil, nil, nil, nil,
-            _NS("Two bookmarks have to be selected."));
+    if ([o_tbl_dataTable numberOfSelectedRows] < 2) {
+        NSBeginAlertSheet(_NS("Invalid selection"), _NS("OK"), @"", @"", o_bookmarks_window, nil, nil, nil, nil, @"%@",_NS("Two bookmarks have to be selected."));
         return;
     }
-    input_thread_t * p_input = pl_CurrentInput( VLCIntf );
-    if( !p_input )
-    {
-        NSBeginCriticalAlertSheet(_NS("No input found"), _NS("OK"),
-            @"", @"", o_bookmarks_window, nil, nil, nil, nil,
-            _NS("The stream must be playing or paused for bookmarks to work."));
+    input_thread_t * p_input = pl_CurrentInput(VLCIntf);
+    if (!p_input) {
+        NSBeginCriticalAlertSheet(_NS("No input found"), _NS("OK"), @"", @"", o_bookmarks_window, nil, nil, nil, nil, @"%@",_NS("The stream must be playing or paused for bookmarks to work."));
         return;
     }
+
     seekpoint_t **pp_bookmarks;
     int i_bookmarks ;
     int i_first = -1;
     int i_second = -1;
-    int x = 0;
     int c = 0;
-    while (c != 2)
-    {
-        if([o_tbl_dataTable isRowSelected:x])
-        {
-            if (i_first == -1)
-            {
+    for (NSUInteger x = 0; c != 2; x++) {
+        if ([o_tbl_dataTable isRowSelected:x]) {
+            if (i_first == -1) {
                 i_first = x;
                 c = 1;
-            }
-            else if (i_second == -1)
-            {
+            } else if (i_second == -1) {
                 i_second = x;
                 c = 2;
             }
         }
-        x = (x + 1);
     }
-    msg_Dbg( VLCIntf, "got the bookmark-indexes");
-    if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
-        &i_bookmarks ) != VLC_SUCCESS )
-    {
-        vlc_object_release( p_input );
-        msg_Err( VLCIntf, "already defined bookmarks couldn't be retrieved");
+
+    if (input_Control(p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks, &i_bookmarks) != VLC_SUCCESS) {
+        vlc_object_release(p_input);
+        msg_Err(VLCIntf, "already defined bookmarks couldn't be retrieved");
         return;
     }
-    msg_Dbg( VLCIntf, "calling wizard");
-
-    char *psz_uri = input_item_GetURI( input_GetItem( p_input ) );
-    [[[VLCMain sharedInstance] getWizard] initWithExtractValuesFrom:
-            [[NSNumber numberWithInt:
-            (pp_bookmarks[i_first]->i_time_offset/1000000)] stringValue]
-            to: [[NSNumber numberWithInt:
-            (pp_bookmarks[i_second]->i_time_offset/1000000)] stringValue]
-            ofItem: [NSString stringWithUTF8String: psz_uri]];
-    free( psz_uri );
-    vlc_object_release( p_input );
-    msg_Dbg( VLCIntf, "released input");
+
+    char *psz_uri = input_item_GetURI(input_GetItem(p_input));
+    [[[VLCMain sharedInstance] wizard] initWithExtractValuesFrom: [NSString stringWithFormat:@"%lli", pp_bookmarks[i_first]->i_time_offset/1000000] to: [NSString stringWithFormat:@"%lli", pp_bookmarks[i_second]->i_time_offset/1000000] ofItem: toNSStr(psz_uri)];
+    free(psz_uri);
+    vlc_object_release(p_input);
+
+    // Clear the bookmark list
+    for (int i = 0; i < i_bookmarks; i++)
+        vlc_seekpoint_Delete(pp_bookmarks[i]);
+    free(pp_bookmarks);
 }
 
 - (IBAction)goToBookmark:(id)sender
 {
-    input_thread_t * p_input = pl_CurrentInput( VLCIntf );
-    if( !p_input ) return;
+    input_thread_t * p_input = pl_CurrentInput(VLCIntf);
+
+    if (!p_input)
+        return;
 
-    input_Control( p_input, INPUT_SET_BOOKMARK, [o_tbl_dataTable selectedRow] );
+    input_Control(p_input, INPUT_SET_BOOKMARK, [o_tbl_dataTable selectedRow]);
 
-    vlc_object_release( p_input );
+    vlc_object_release(p_input);
 }
 
 - (IBAction)remove:(id)sender
 {
-    /* remove selected item */
-    input_thread_t * p_input = pl_CurrentInput( VLCIntf );
-    if( !p_input ) return;
+    input_thread_t * p_input = pl_CurrentInput(VLCIntf);
+
+    if (!p_input)
+        return;
 
     int i_focused = [o_tbl_dataTable selectedRow];
 
-    if( i_focused >= 0 )
-        input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
+    if (i_focused >= 0)
+        input_Control(p_input, INPUT_DEL_BOOKMARK, i_focused);
+
+    vlc_object_release(p_input);
 
-    vlc_object_release( p_input );
     [o_tbl_dataTable reloadData];
 }
 
@@ -361,7 +353,7 @@ static VLCBookmarks *_o_sharedInstance = nil;
  * callback stuff
  *****************************************************************************/
 
--(id)getDataTable
+-(id)dataTable
 {
     return o_tbl_dataTable;
 }
@@ -370,71 +362,63 @@ static VLCBookmarks *_o_sharedInstance = nil;
  * data source methods
  *****************************************************************************/
 
-- (int)numberOfRowsInTableView:(NSTableView *)theDataTable
+- (NSInteger)numberOfRowsInTableView:(NSTableView *)theDataTable
 {
     /* return the number of bookmarks */
-    input_thread_t * p_input = pl_CurrentInput( VLCIntf );
+    input_thread_t * p_input = pl_CurrentInput(VLCIntf);
     seekpoint_t **pp_bookmarks;
     int i_bookmarks;
-    if( !p_input ) return 0;
-    else if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
-                       &i_bookmarks ) != VLC_SUCCESS )
-    {
-        vlc_object_release( p_input );
+
+    if (!p_input)
         return 0;
-    }
-    else {
-        vlc_object_release( p_input );
-        return i_bookmarks;
-    }
+
+    int returnValue = input_Control(p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks, &i_bookmarks);
+    vlc_object_release(p_input);
+
+    if (returnValue != VLC_SUCCESS)
+        return 0;
+
+    for (int i = 0; i < i_bookmarks; i++)
+        vlc_seekpoint_Delete(pp_bookmarks[i]);
+    free(pp_bookmarks);
+
+    return i_bookmarks;
 }
 
-- (id)tableView:(NSTableView *)theDataTable objectValueForTableColumn:
-    (NSTableColumn *)theTableColumn row: (int)row
+- (id)tableView:(NSTableView *)theDataTable objectValueForTableColumn: (NSTableColumn *)theTableColumn row: (NSInteger)row
 {
     /* return the corresponding data as NSString */
-    input_thread_t * p_input = pl_CurrentInput( VLCIntf );
+    input_thread_t * p_input = pl_CurrentInput(VLCIntf);
     seekpoint_t **pp_bookmarks;
     int i_bookmarks;
-    char *toBeReturned;
-    int i_toBeReturned = 0;
-    id ret;
-
-    if( !p_input ) return @"";
-    else if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
-                       &i_bookmarks ) != VLC_SUCCESS )
-    {
+    id ret = @"";
+
+    if (!p_input)
+        return @"";
+    else if (input_Control(p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks, &i_bookmarks) != VLC_SUCCESS)
         ret = @"";
-    }
-    else
-    {
-        if ([[theTableColumn identifier] isEqualToString: @"description"])
-        {
-            toBeReturned = pp_bookmarks[row]->psz_name;
-            ret = [NSString stringWithUTF8String: toBeReturned];
-        }
-        else if ([[theTableColumn identifier] isEqualToString: @"size_offset"])
-        {
-            i_toBeReturned = pp_bookmarks[row]->i_byte_offset;
-            ret = [[NSNumber numberWithInt: i_toBeReturned] stringValue];
-        }
-        else if ([[theTableColumn identifier] isEqualToString: @"time_offset"])
-        {
-            i_toBeReturned = pp_bookmarks[row]->i_time_offset;
-            ret = [[NSNumber numberWithInt: (i_toBeReturned / 1000000)]
-                stringValue];
-        }
-        else
-        {
-            /* may not happen, just in case */
-            msg_Err( VLCIntf, "unknown table column identifier (%s) while "
-                "updating the bookmark table", [[theTableColumn identifier]
-                UTF8String] );
-            ret = @"unknown identifier";
+    else if (row >= i_bookmarks)
+        ret = @"";
+    else {
+        NSString * identifier = [theTableColumn identifier];
+        if ([identifier isEqualToString: @"description"])
+            ret = toNSStr(pp_bookmarks[row]->psz_name);
+        else if ([identifier isEqualToString: @"size_offset"])
+            ret = [NSString stringWithFormat:@"%lli", pp_bookmarks[row]->i_byte_offset];
+        else if ([identifier isEqualToString: @"time_offset"]) {
+            int total = pp_bookmarks[row]->i_time_offset/ 1000000;
+            int hour = total / (60*60);
+            int min = (total - hour*60*60) / 60;
+            int sec = total - hour*60*60 - min*60;
+            ret = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, min, sec];
         }
+
+        // Clear the bookmark list
+        for (int i = 0; i < i_bookmarks; i++)
+            vlc_seekpoint_Delete(pp_bookmarks[i]);
+        free(pp_bookmarks);
     }
-    vlc_object_release( p_input );
+    vlc_object_release(p_input);
     return ret;
 }
 
@@ -445,22 +429,17 @@ static VLCBookmarks *_o_sharedInstance = nil;
 - (void)tableViewSelectionDidChange:(NSNotification *)aNotification
 {
     /* check whether a row is selected and en-/disable the edit/remove buttons */
-    if ([o_tbl_dataTable selectedRow] == -1)
-    {
+    if ([o_tbl_dataTable selectedRow] == -1) {
         /* no row is selected */
         [o_btn_edit setEnabled: NO];
         [o_btn_rm setEnabled: NO];
         [o_btn_extract setEnabled: NO];
-    }
-    else
-    {
+    } else {
         /* a row is selected */
         [o_btn_edit setEnabled: YES];
         [o_btn_rm setEnabled: YES];
         if ([o_tbl_dataTable numberOfSelectedRows] == 2)
-        {
             [o_btn_extract setEnabled: YES];
-        }
     }
 }