1 /*****************************************************************************
2 * bookmarks.m: MacOS X Bookmarks window
3 *****************************************************************************
4 * Copyright (C) 2005 - 2007 the VideoLAN team
7 * Authors: Felix Kühne <fkuehne@users.sf.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
25 /*****************************************************************************
27 * the code used to bind with VLC's modules is heavily based upon
28 * ../wxwidgets/bookmarks.cpp, written by Gildas Bazin.
29 * (he is a member of the VideoLAN team)
30 *****************************************************************************/
33 /*****************************************************************************
35 *****************************************************************************/
39 #import <vlc_interface.h>
41 /*****************************************************************************
42 * VLCExtended implementation
44 * implements the GUI functions for the window, the data source and the
45 * delegate for o_tbl_dataTable
46 *****************************************************************************/
48 @implementation VLCBookmarks
50 static VLCBookmarks *_o_sharedInstance = nil;
52 + (VLCBookmarks *)sharedInstance
54 return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
59 if (_o_sharedInstance) {
62 _o_sharedInstance = [super init];
65 return _o_sharedInstance;
68 /*****************************************************************************
70 *****************************************************************************/
80 vlc_object_release( p_old_input );
86 /* localise the items */
89 [o_bookmarks_window setTitle: _NS("Bookmarks")];
90 [o_btn_add setTitle: _NS("Add")];
91 [o_btn_clear setTitle: _NS("Clear")];
92 [o_btn_edit setTitle: _NS("Edit")];
93 [o_btn_extract setTitle: _NS("Extract")];
94 [o_btn_rm setTitle: _NS("Remove")];
95 [[[o_tbl_dataTable tableColumnWithIdentifier:@"description"] headerCell]
96 setStringValue: _NS("Description")];
97 [[[o_tbl_dataTable tableColumnWithIdentifier:@"size_offset"] headerCell]
98 setStringValue: _NS("Position")];
99 [[[o_tbl_dataTable tableColumnWithIdentifier:@"time_offset"] headerCell]
100 setStringValue: _NS("Time")];
103 [o_edit_btn_ok setTitle: _NS("OK")];
104 [o_edit_btn_cancel setTitle: _NS("Cancel")];
105 [o_edit_lbl_name setStringValue: _NS("Name")];
106 [o_edit_lbl_time setStringValue: _NS("Time")];
107 [o_edit_lbl_bytes setStringValue: _NS("Position")];
110 - (void)showBookmarks
112 /* show the window, called from intf.m */
113 [o_bookmarks_window displayIfNeeded];
114 [o_bookmarks_window makeKeyAndOrderFront:nil];
117 - (IBAction)add:(id)sender
119 /* add item to list */
120 input_thread_t * p_input = pl_CurrentInput( VLCIntf );
122 if( !p_input ) return;
124 seekpoint_t bookmark;
126 bookmark.psz_name = NULL;
127 bookmark.i_byte_offset = 0;
128 bookmark.i_time_offset = 0;
130 var_Get( VLCIntf, "position", &pos);
131 bookmark.psz_name = _("Untitled");
132 input_Control( p_input, INPUT_GET_BYTE_POSITION, &bookmark.i_byte_offset );
133 var_Get( p_input, "time", &pos );
134 bookmark.i_time_offset = pos.i_time;
135 input_Control( p_input, INPUT_ADD_BOOKMARK, &bookmark );
137 vlc_object_release( p_input );
139 [o_tbl_dataTable reloadData];
142 - (IBAction)clear:(id)sender
145 input_thread_t * p_input = pl_CurrentInput( VLCIntf );
150 input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
152 vlc_object_release( p_input );
154 [o_tbl_dataTable reloadData];
157 - (IBAction)edit:(id)sender
159 /* put values to the sheet's fields and show sheet */
160 /* we take the values from the core and not the table, because we cannot
162 input_thread_t * p_input = pl_CurrentInput( VLCIntf );
163 seekpoint_t **pp_bookmarks;
166 row = [o_tbl_dataTable selectedRow];
172 else if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
173 &i_bookmarks ) != VLC_SUCCESS )
175 vlc_object_release( p_input );
180 vlc_object_release( p_input );
183 [o_edit_fld_name setStringValue: [NSString stringWithUTF8String:
184 pp_bookmarks[row]->psz_name]];
185 [o_edit_fld_time setStringValue: [[NSNumber numberWithInt:
186 (pp_bookmarks[row]->i_time_offset / 1000000)] stringValue]];
187 [o_edit_fld_bytes setStringValue: [[NSNumber numberWithInt:
188 pp_bookmarks[row]->i_byte_offset] stringValue]];
191 p_old_input = p_input;
192 vlc_object_release( p_input );
194 [NSApp beginSheet: o_edit_window
195 modalForWindow: o_bookmarks_window
196 modalDelegate: o_edit_window
201 - (IBAction)edit_cancel:(id)sender
204 [NSApp endSheet:o_edit_window];
205 [o_edit_window close];
208 - (IBAction)edit_ok:(id)sender
210 /* save field contents and close sheet */
211 seekpoint_t **pp_bookmarks;
213 input_thread_t * p_input = pl_CurrentInput( VLCIntf );
217 NSBeginCriticalAlertSheet(_NS("No input"), _NS("OK"),
218 @"", @"", o_bookmarks_window, nil, nil, nil, nil, _NS("No "
219 "input found. A stream must be playing or paused for "
220 "bookmarks to work."));
223 if( p_old_input != p_input )
225 NSBeginCriticalAlertSheet(_NS("Input has changed"), _NS("OK"),
226 @"", @"", o_bookmarks_window, nil, nil, nil, nil, _NS("Input "
227 "has changed, unable to save bookmark. Suspending playback with "
228 "\"Pause\" while editing bookmarks to ensure to keep the same "
230 vlc_object_release( p_input );
234 if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
235 &i_bookmarks ) != VLC_SUCCESS )
237 vlc_object_release( p_input );
241 i = [o_tbl_dataTable selectedRow];
243 free( pp_bookmarks[i]->psz_name );
245 pp_bookmarks[i]->psz_name = strdup([[o_edit_fld_name stringValue] UTF8String]);
246 pp_bookmarks[i]->i_byte_offset = [[o_edit_fld_bytes stringValue] intValue];
247 pp_bookmarks[i]->i_time_offset = ([[o_edit_fld_time stringValue] intValue] * 1000000);
249 if( input_Control( p_input, INPUT_CHANGE_BOOKMARK, pp_bookmarks[i], i )
252 msg_Warn( VLCIntf, "Unable to change the bookmark");
253 vlc_object_release( p_input );
257 [o_tbl_dataTable reloadData];
258 vlc_object_release( p_input );
261 [NSApp endSheet: o_edit_window];
262 [o_edit_window close];
265 - (IBAction)extract:(id)sender
268 if( [o_tbl_dataTable numberOfSelectedRows] < 2 )
270 NSBeginAlertSheet(_NS("Invalid selection"), _NS("OK"),
271 @"", @"", o_bookmarks_window, nil, nil, nil, nil,
272 _NS("Two bookmarks have to be selected."));
275 input_thread_t * p_input = pl_CurrentInput( VLCIntf );
278 NSBeginCriticalAlertSheet(_NS("No input found"), _NS("OK"),
279 @"", @"", o_bookmarks_window, nil, nil, nil, nil,
280 _NS("The stream must be playing or paused for bookmarks to work."));
284 seekpoint_t **pp_bookmarks;
292 if([o_tbl_dataTable isRowSelected:x])
299 else if (i_second == -1)
308 msg_Dbg( VLCIntf, "got the bookmark-indexes");
310 if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
311 &i_bookmarks ) != VLC_SUCCESS )
313 vlc_object_release( p_input );
314 msg_Err( VLCIntf, "already defined bookmarks couldn't be retrieved");
317 msg_Dbg( VLCIntf, "calling wizard");
319 char *psz_uri = input_item_GetURI( input_GetItem( p_input ) );
320 [[[VLCMain sharedInstance] getWizard] initWithExtractValuesFrom:
321 [[NSNumber numberWithInt:
322 (pp_bookmarks[i_first]->i_time_offset/1000000)] stringValue]
323 to: [[NSNumber numberWithInt:
324 (pp_bookmarks[i_second]->i_time_offset/1000000)] stringValue]
325 ofItem: [NSString stringWithUTF8String: psz_uri]];
327 vlc_object_release( p_input );
328 msg_Dbg( VLCIntf, "released input");
331 - (IBAction)goToBookmark:(id)sender
333 input_thread_t * p_input = pl_CurrentInput( VLCIntf );
335 if( !p_input ) return;
337 input_Control( p_input, INPUT_SET_BOOKMARK, [o_tbl_dataTable selectedRow] );
339 vlc_object_release( p_input );
342 - (IBAction)remove:(id)sender
344 /* remove selected item */
345 input_thread_t * p_input = pl_CurrentInput( VLCIntf );
347 if( !p_input ) return;
349 int i_focused = [o_tbl_dataTable selectedRow];
352 input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
354 vlc_object_release( p_input );
356 [o_tbl_dataTable reloadData];
359 /*****************************************************************************
361 *****************************************************************************/
365 return o_tbl_dataTable;
368 /*****************************************************************************
369 * data source methods
370 *****************************************************************************/
372 - (int)numberOfRowsInTableView:(NSTableView *)theDataTable
374 /* return the number of bookmarks */
375 input_thread_t * p_input = pl_CurrentInput( VLCIntf );
376 seekpoint_t **pp_bookmarks;
379 if( !p_input ) return 0;
380 else if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
381 &i_bookmarks ) != VLC_SUCCESS )
383 vlc_object_release( p_input );
387 vlc_object_release( p_input );
392 - (id)tableView:(NSTableView *)theDataTable objectValueForTableColumn:
393 (NSTableColumn *)theTableColumn row: (int)row
395 /* return the corresponding data as NSString */
396 input_thread_t * p_input = pl_CurrentInput( VLCIntf );
397 seekpoint_t **pp_bookmarks;
400 int i_toBeReturned = 0;
403 if( !p_input ) return @"";
404 else if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
405 &i_bookmarks ) != VLC_SUCCESS )
411 if ([[theTableColumn identifier] isEqualToString: @"description"])
413 toBeReturned = pp_bookmarks[row]->psz_name;
414 ret = [NSString stringWithUTF8String: toBeReturned];
416 else if ([[theTableColumn identifier] isEqualToString: @"size_offset"])
418 i_toBeReturned = pp_bookmarks[row]->i_byte_offset;
419 ret = [[NSNumber numberWithInt: i_toBeReturned] stringValue];
421 else if ([[theTableColumn identifier] isEqualToString: @"time_offset"])
423 i_toBeReturned = pp_bookmarks[row]->i_time_offset;
424 ret = [[NSNumber numberWithInt: (i_toBeReturned / 1000000)]
429 /* may not happen, just in case */
430 msg_Err( VLCIntf, "unknown table column identifier (%s) while "
431 "updating the bookmark table", [[theTableColumn identifier]
433 ret = @"unknown identifier";
436 vlc_object_release( p_input );
440 /*****************************************************************************
442 *****************************************************************************/
444 - (void)tableViewSelectionDidChange:(NSNotification *)aNotification
446 /* check whether a row is selected and en-/disable the edit/remove buttons */
447 if ([o_tbl_dataTable selectedRow] == -1)
449 /* no row is selected */
450 [o_btn_edit setEnabled: NO];
451 [o_btn_rm setEnabled: NO];
452 [o_btn_extract setEnabled: NO];
456 /* a row is selected */
457 [o_btn_edit setEnabled: YES];
458 [o_btn_rm setEnabled: YES];
459 if ([o_tbl_dataTable numberOfSelectedRows] == 2)
461 [o_btn_extract setEnabled: YES];