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 intf_thread_t * p_intf = VLCIntf;
121 input_thread_t * p_input = (input_thread_t *)vlc_object_find( p_intf,
122 VLC_OBJECT_INPUT, FIND_ANYWHERE );
126 seekpoint_t bookmark;
128 bookmark.psz_name = NULL;
129 bookmark.i_byte_offset = 0;
130 bookmark.i_time_offset = 0;
132 var_Get(p_intf, "position", &pos);
133 bookmark.psz_name = _("Untitled");
134 input_Control( p_input, INPUT_GET_BYTE_POSITION, &bookmark.i_byte_offset );
135 var_Get( p_input, "time", &pos );
136 bookmark.i_time_offset = pos.i_time;
137 input_Control( p_input, INPUT_ADD_BOOKMARK, &bookmark );
139 vlc_object_release( p_input );
141 [o_tbl_dataTable reloadData];
144 - (IBAction)clear:(id)sender
147 intf_thread_t * p_intf = VLCIntf;
148 input_thread_t *p_input = (input_thread_t *)vlc_object_find( p_intf,
149 VLC_OBJECT_INPUT, FIND_ANYWHERE );
154 input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
156 vlc_object_release( p_input );
158 [o_tbl_dataTable reloadData];
161 - (IBAction)edit:(id)sender
163 /* put values to the sheet's fields and show sheet */
164 /* we take the values from the core and not the table, because we cannot
166 intf_thread_t * p_intf = VLCIntf;
167 input_thread_t * p_input = (input_thread_t *)vlc_object_find( p_intf,
168 VLC_OBJECT_INPUT, FIND_ANYWHERE );
169 seekpoint_t **pp_bookmarks;
172 row = [o_tbl_dataTable selectedRow];
178 else if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
179 &i_bookmarks ) != VLC_SUCCESS )
181 vlc_object_release( p_input );
186 vlc_object_release( p_input );
189 [o_edit_fld_name setStringValue: [NSString stringWithUTF8String:
190 pp_bookmarks[row]->psz_name]];
191 [o_edit_fld_time setStringValue: [[NSNumber numberWithInt:
192 (pp_bookmarks[row]->i_time_offset / 1000000)] stringValue]];
193 [o_edit_fld_bytes setStringValue: [[NSNumber numberWithInt:
194 pp_bookmarks[row]->i_byte_offset] stringValue]];
197 p_old_input = p_input;
198 vlc_object_release( p_input );
200 [NSApp beginSheet: o_edit_window
201 modalForWindow: o_bookmarks_window
202 modalDelegate: o_edit_window
207 - (IBAction)edit_cancel:(id)sender
210 [NSApp endSheet:o_edit_window];
211 [o_edit_window close];
214 - (IBAction)edit_ok:(id)sender
216 /* save field contents and close sheet */
218 intf_thread_t * p_intf = VLCIntf;
219 seekpoint_t **pp_bookmarks;
221 input_thread_t *p_input = (input_thread_t *)vlc_object_find( p_intf,
222 VLC_OBJECT_INPUT, FIND_ANYWHERE );
226 NSBeginCriticalAlertSheet(_NS("No input"), _NS("OK"),
227 @"", @"", o_bookmarks_window, nil, nil, nil, nil, _NS("No "
228 "input found. A stream must be playing or paused for "
229 "bookmarks to work."));
232 if( p_old_input != p_input )
234 NSBeginCriticalAlertSheet(_NS("Input has changed"), _NS("OK"),
235 @"", @"", o_bookmarks_window, nil, nil, nil, nil, _NS("Input "
236 "has changed, unable to save bookmark. Suspending playback with "
237 "\"Pause\" while editing bookmarks to ensure to keep the same "
239 vlc_object_release( p_input );
243 if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
244 &i_bookmarks ) != VLC_SUCCESS )
246 vlc_object_release( p_input );
250 i = [o_tbl_dataTable selectedRow];
252 free( pp_bookmarks[i]->psz_name );
254 pp_bookmarks[i]->psz_name = strdup([[o_edit_fld_name stringValue] UTF8String]);
255 pp_bookmarks[i]->i_byte_offset = [[o_edit_fld_bytes stringValue] intValue];
256 pp_bookmarks[i]->i_time_offset = ([[o_edit_fld_time stringValue] intValue] * 1000000);
258 if( input_Control( p_input, INPUT_CHANGE_BOOKMARK, pp_bookmarks[i], i )
261 msg_Warn( p_intf, "Unable to change the bookmark");
262 vlc_object_release( p_input );
266 [o_tbl_dataTable reloadData];
267 vlc_object_release( p_input );
270 [NSApp endSheet: o_edit_window];
271 [o_edit_window close];
274 - (IBAction)extract:(id)sender
278 intf_thread_t * p_intf = VLCIntf;
280 if( [o_tbl_dataTable numberOfSelectedRows] < 2 )
282 NSBeginAlertSheet(_NS("Invalid selection"), _NS("OK"),
283 @"", @"", o_bookmarks_window, nil, nil, nil, nil,
284 _NS("Two bookmarks have to be selected."));
287 input_thread_t *p_input =
288 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
292 NSBeginCriticalAlertSheet(_NS("No input found"), _NS("OK"),
293 @"", @"", o_bookmarks_window, nil, nil, nil, nil,
294 _NS("The stream must be playing or paused for bookmarks to work."));
298 seekpoint_t **pp_bookmarks;
306 if([o_tbl_dataTable isRowSelected:x])
313 else if (i_second == -1)
322 msg_Dbg(p_intf, "got the bookmark-indexes");
324 if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
325 &i_bookmarks ) != VLC_SUCCESS )
327 vlc_object_release( p_input );
328 msg_Err(p_intf, "already defined bookmarks couldn't be retrieved");
331 msg_Dbg(p_intf, "calling wizard");
333 char *psz_uri = input_item_GetURI( input_GetItem( p_input ) );
334 [[[VLCMain sharedInstance] getWizard] initWithExtractValuesFrom:
335 [[NSNumber numberWithInt:
336 (pp_bookmarks[i_first]->i_time_offset/1000000)] stringValue]
337 to: [[NSNumber numberWithInt:
338 (pp_bookmarks[i_second]->i_time_offset/1000000)] stringValue]
339 ofItem: [NSString stringWithUTF8String: psz_uri]];
341 vlc_object_release( p_input );
342 msg_Dbg(p_intf, "released input");
345 - (IBAction)goToBookmark:(id)sender
347 intf_thread_t * p_intf = VLCIntf;
348 input_thread_t *p_input =
349 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
352 if( !p_input ) return;
354 input_Control( p_input, INPUT_SET_BOOKMARK, [o_tbl_dataTable selectedRow] );
356 vlc_object_release( p_input );
359 - (IBAction)remove:(id)sender
361 /* remove selected item */
362 intf_thread_t * p_intf = VLCIntf;
363 input_thread_t *p_input =
364 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
367 if( !p_input ) return;
369 int i_focused = [o_tbl_dataTable selectedRow];
372 input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
374 vlc_object_release( p_input );
376 [o_tbl_dataTable reloadData];
379 /*****************************************************************************
381 *****************************************************************************/
385 return o_tbl_dataTable;
388 /*****************************************************************************
389 * data source methods
390 *****************************************************************************/
392 - (int)numberOfRowsInTableView:(NSTableView *)theDataTable
394 /* return the number of bookmarks */
395 intf_thread_t * p_intf = VLCIntf;
396 input_thread_t * p_input = (input_thread_t *)vlc_object_find( p_intf,
397 VLC_OBJECT_INPUT, FIND_ANYWHERE );
398 seekpoint_t **pp_bookmarks;
401 if( !p_input ) return 0;
402 else if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
403 &i_bookmarks ) != VLC_SUCCESS )
405 vlc_object_release( p_input );
409 vlc_object_release( p_input );
414 - (id)tableView:(NSTableView *)theDataTable objectValueForTableColumn:
415 (NSTableColumn *)theTableColumn row: (int)row
417 /* return the corresponding data as NSString */
418 intf_thread_t * p_intf = VLCIntf;
419 input_thread_t * p_input = (input_thread_t *)vlc_object_find( p_intf,
420 VLC_OBJECT_INPUT, FIND_ANYWHERE );
421 seekpoint_t **pp_bookmarks;
424 int i_toBeReturned = 0;
426 if( !p_input ) return @"";
427 else if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
428 &i_bookmarks ) != VLC_SUCCESS )
430 vlc_object_release( p_input );
435 if ([[theTableColumn identifier] isEqualToString: @"description"])
437 toBeReturned = pp_bookmarks[row]->psz_name;
438 vlc_object_release( p_input );
439 return [NSString stringWithUTF8String: toBeReturned];
441 else if ([[theTableColumn identifier] isEqualToString: @"size_offset"])
443 i_toBeReturned = pp_bookmarks[row]->i_byte_offset;
444 vlc_object_release( p_input );
445 return [[NSNumber numberWithInt: i_toBeReturned] stringValue];
447 else if ([[theTableColumn identifier] isEqualToString: @"time_offset"])
449 i_toBeReturned = pp_bookmarks[row]->i_time_offset;
450 vlc_object_release( p_input );
451 return [[NSNumber numberWithInt: (i_toBeReturned / 1000000)]
456 /* may not happen, just in case */
457 vlc_object_release( p_input );
458 msg_Err(p_intf, "unknown table column identifier (%s) while "
459 "updating the bookmark table", [[theTableColumn identifier]
461 return @"unknown identifier";
467 /*****************************************************************************
469 *****************************************************************************/
471 - (void)tableViewSelectionDidChange:(NSNotification *)aNotification
473 /* check whether a row is selected and en-/disable the edit/remove buttons */
474 if ([o_tbl_dataTable selectedRow] == -1)
476 /* no row is selected */
477 [o_btn_edit setEnabled: NO];
478 [o_btn_rm setEnabled: NO];
479 [o_btn_extract setEnabled: NO];
483 /* a row is selected */
484 [o_btn_edit setEnabled: YES];
485 [o_btn_rm setEnabled: YES];
486 if ([o_tbl_dataTable numberOfSelectedRows] == 2)
488 [o_btn_extract setEnabled: YES];