1 /*****************************************************************************
2 * bookmarks.m: MacOS X Bookmarks window
3 *****************************************************************************
4 * Copyright (C) 2005, 2006 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 *****************************************************************************/
40 #import <vlc_interface.h>
42 /*****************************************************************************
43 * VLCExtended implementation
45 * implements the GUI functions for the window, the data source and the
46 * delegate for o_tbl_dataTable
47 *****************************************************************************/
49 @implementation VLCBookmarks
51 static VLCBookmarks *_o_sharedInstance = nil;
53 + (VLCBookmarks *)sharedInstance
55 return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
60 if (_o_sharedInstance) {
63 _o_sharedInstance = [super init];
66 return _o_sharedInstance;
69 /*****************************************************************************
71 *****************************************************************************/
82 vlc_object_release( p_old_input );
89 /* localise the items */
92 [o_bookmarks_window setTitle: _NS("Bookmarks")];
93 [o_btn_add setTitle: _NS("Add")];
94 [o_btn_clear setTitle: _NS("Clear")];
95 [o_btn_edit setTitle: _NS("Edit")];
96 [o_btn_extract setTitle: _NS("Extract")];
97 [o_btn_rm setTitle: _NS("Remove")];
98 [[[o_tbl_dataTable tableColumnWithIdentifier:@"description"] headerCell]
99 setStringValue: _NS("Description")];
100 [[[o_tbl_dataTable tableColumnWithIdentifier:@"size_offset"] headerCell]
101 setStringValue: _NS("Position")];
102 [[[o_tbl_dataTable tableColumnWithIdentifier:@"time_offset"] headerCell]
103 setStringValue: _NS("Time")];
106 [o_edit_btn_ok setTitle: _NS("OK")];
107 [o_edit_btn_cancel setTitle: _NS("Cancel")];
108 [o_edit_lbl_name setStringValue: _NS("Name")];
109 [o_edit_lbl_time setStringValue: _NS("Time")];
110 [o_edit_lbl_bytes setStringValue: _NS("Position")];
113 - (void)showBookmarks
115 /* show the window, called from intf.m */
116 [o_bookmarks_window displayIfNeeded];
117 [o_bookmarks_window makeKeyAndOrderFront:nil];
120 - (IBAction)add:(id)sender
122 /* add item to list */
123 intf_thread_t * p_intf = VLCIntf;
124 input_thread_t * p_input = (input_thread_t *)vlc_object_find( p_intf,
125 VLC_OBJECT_INPUT, FIND_ANYWHERE );
129 seekpoint_t bookmark;
131 bookmark.psz_name = NULL;
132 bookmark.i_byte_offset = 0;
133 bookmark.i_time_offset = 0;
135 var_Get(p_intf, "position", &pos);
136 bookmark.psz_name = _("Untitled");
137 input_Control( p_input, INPUT_GET_BYTE_POSITION, &bookmark.i_byte_offset );
138 var_Get( p_input, "time", &pos );
139 bookmark.i_time_offset = pos.i_time;
140 input_Control( p_input, INPUT_ADD_BOOKMARK, &bookmark );
142 vlc_object_release( p_input );
144 [o_tbl_dataTable reloadData];
147 - (IBAction)clear:(id)sender
150 intf_thread_t * p_intf = VLCIntf;
151 input_thread_t *p_input = (input_thread_t *)vlc_object_find( p_intf,
152 VLC_OBJECT_INPUT, FIND_ANYWHERE );
157 input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
159 vlc_object_release( p_input );
161 [o_tbl_dataTable reloadData];
164 - (IBAction)edit:(id)sender
166 /* put values to the sheet's fields and show sheet */
167 /* we take the values from the core and not the table, because we cannot
169 intf_thread_t * p_intf = VLCIntf;
170 input_thread_t * p_input = (input_thread_t *)vlc_object_find( p_intf,
171 VLC_OBJECT_INPUT, FIND_ANYWHERE );
172 seekpoint_t **pp_bookmarks;
175 row = [o_tbl_dataTable selectedRow];
181 else if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
182 &i_bookmarks ) != VLC_SUCCESS )
184 vlc_object_release( p_input );
189 vlc_object_release( p_input );
192 [o_edit_fld_name setStringValue: [NSString stringWithUTF8String:
193 pp_bookmarks[row]->psz_name]];
194 [o_edit_fld_time setStringValue: [[NSNumber numberWithInt:
195 (pp_bookmarks[row]->i_time_offset / 1000000)] stringValue]];
196 [o_edit_fld_bytes setStringValue: [[NSNumber numberWithInt:
197 pp_bookmarks[row]->i_byte_offset] stringValue]];
200 p_old_input = p_input;
201 vlc_object_release( p_input );
203 [NSApp beginSheet: o_edit_window
204 modalForWindow: o_bookmarks_window
205 modalDelegate: o_edit_window
210 - (IBAction)edit_cancel:(id)sender
213 [NSApp endSheet:o_edit_window];
214 [o_edit_window close];
217 - (IBAction)edit_ok:(id)sender
219 /* save field contents and close sheet */
221 intf_thread_t * p_intf = VLCIntf;
222 seekpoint_t **pp_bookmarks;
224 input_thread_t *p_input = (input_thread_t *)vlc_object_find( p_intf,
225 VLC_OBJECT_INPUT, FIND_ANYWHERE );
229 NSBeginCriticalAlertSheet(_NS("No input"), _NS("OK"),
230 @"", @"", o_bookmarks_window, nil, nil, nil, nil, _NS("No "
231 "input found. A stream must be playing or paused for "
232 "bookmarks to work."));
235 if( p_old_input != p_input )
237 NSBeginCriticalAlertSheet(_NS("Input has changed"), _NS("OK"),
238 @"", @"", o_bookmarks_window, nil, nil, nil, nil, _NS("Input "
239 "has changed, unable to save bookmark. Suspending playback with "
240 "\"Pause\" while editing bookmarks to ensure to keep the same "
242 vlc_object_release( p_input );
246 if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
247 &i_bookmarks ) != VLC_SUCCESS )
249 vlc_object_release( p_input );
253 i = [o_tbl_dataTable selectedRow];
255 if( pp_bookmarks[i]->psz_name )
257 free( pp_bookmarks[i]->psz_name );
260 pp_bookmarks[i]->psz_name = strdup([[o_edit_fld_name stringValue] UTF8String]);
261 pp_bookmarks[i]->i_byte_offset = [[o_edit_fld_bytes stringValue] intValue];
262 pp_bookmarks[i]->i_time_offset = ([[o_edit_fld_time stringValue] intValue] * 1000000);
264 if( input_Control( p_input, INPUT_CHANGE_BOOKMARK, pp_bookmarks[i], i )
267 msg_Warn( p_intf, "Unable to change the bookmark");
268 vlc_object_release( p_input );
272 [o_tbl_dataTable reloadData];
273 vlc_object_release( p_input );
276 [NSApp endSheet: o_edit_window];
277 [o_edit_window close];
280 - (IBAction)extract:(id)sender
284 intf_thread_t * p_intf = VLCIntf;
286 if( [o_tbl_dataTable numberOfSelectedRows] < 2 )
288 NSBeginAlertSheet(_NS("Invalid selection"), _NS("OK"),
289 @"", @"", o_bookmarks_window, nil, nil, nil, nil,
290 _NS("Two bookmarks have to be selected."));
293 input_thread_t *p_input =
294 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
298 NSBeginCriticalAlertSheet(_NS("No input found"), _NS("OK"),
299 @"", @"", o_bookmarks_window, nil, nil, nil, nil,
300 _NS("The stream must be playing or paused for bookmarks to work."));
304 seekpoint_t **pp_bookmarks;
312 if([o_tbl_dataTable isRowSelected:x])
319 else if (i_second == -1)
328 msg_Dbg(p_intf, "got the bookmark-indexes");
330 if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
331 &i_bookmarks ) != VLC_SUCCESS )
333 vlc_object_release( p_input );
334 msg_Err(p_intf, "already defined bookmarks couldn't be retrieved");
337 msg_Dbg(p_intf, "calling wizard");
339 [[[VLCMain sharedInstance] getWizard] initWithExtractValuesFrom:
340 [[NSNumber numberWithInt:
341 (pp_bookmarks[i_first]->i_time_offset/1000000)] stringValue]
342 to: [[NSNumber numberWithInt:
343 (pp_bookmarks[i_second]->i_time_offset/1000000)] stringValue]
344 ofItem: [NSString stringWithUTF8String:
345 input_GetItem(p_input)->psz_uri]];
346 vlc_object_release( p_input );
347 msg_Dbg(p_intf, "released input");
350 - (IBAction)goToBookmark:(id)sender
352 intf_thread_t * p_intf = VLCIntf;
353 input_thread_t *p_input =
354 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
362 input_Control( p_input, INPUT_SET_BOOKMARK, [o_tbl_dataTable selectedRow] );
364 vlc_object_release( p_input );
367 - (IBAction)remove:(id)sender
369 /* remove selected item */
370 intf_thread_t * p_intf = VLCIntf;
371 input_thread_t *p_input =
372 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
375 if( !p_input ) return;
377 int i_focused = [o_tbl_dataTable selectedRow];
380 input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
383 vlc_object_release( p_input );
385 [o_tbl_dataTable reloadData];
388 /*****************************************************************************
390 *****************************************************************************/
394 return o_tbl_dataTable;
397 /*****************************************************************************
398 * data source methods
399 *****************************************************************************/
401 - (int)numberOfRowsInTableView:(NSTableView *)theDataTable
403 /* return the number of bookmarks */
404 intf_thread_t * p_intf = VLCIntf;
405 input_thread_t * p_input = (input_thread_t *)vlc_object_find( p_intf,
406 VLC_OBJECT_INPUT, FIND_ANYWHERE );
407 seekpoint_t **pp_bookmarks;
414 else if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
415 &i_bookmarks ) != VLC_SUCCESS )
417 vlc_object_release( p_input );
421 vlc_object_release( p_input );
426 - (id)tableView:(NSTableView *)theDataTable objectValueForTableColumn:
427 (NSTableColumn *)theTableColumn row: (int)row
429 /* return the corresponding data as NSString */
430 intf_thread_t * p_intf = VLCIntf;
431 input_thread_t * p_input = (input_thread_t *)vlc_object_find( p_intf,
432 VLC_OBJECT_INPUT, FIND_ANYWHERE );
433 seekpoint_t **pp_bookmarks;
436 int i_toBeReturned = 0;
442 else if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
443 &i_bookmarks ) != VLC_SUCCESS )
445 vlc_object_release( p_input );
450 if ([[theTableColumn identifier] isEqualToString: @"description"])
452 toBeReturned = pp_bookmarks[row]->psz_name;
453 vlc_object_release( p_input );
454 return [NSString stringWithUTF8String: toBeReturned];
456 else if ([[theTableColumn identifier] isEqualToString: @"size_offset"])
458 i_toBeReturned = pp_bookmarks[row]->i_byte_offset;
459 vlc_object_release( p_input );
460 return [[NSNumber numberWithInt: i_toBeReturned] stringValue];
462 else if ([[theTableColumn identifier] isEqualToString: @"time_offset"])
464 i_toBeReturned = pp_bookmarks[row]->i_time_offset;
465 vlc_object_release( p_input );
466 return [[NSNumber numberWithInt: (i_toBeReturned / 1000000)]
471 /* may not happen, but just in case */
472 vlc_object_release( p_input );
473 msg_Err(p_intf, "unknown table column identifier (%s) while "
474 "updating the bookmark table", [[theTableColumn identifier]
477 return @"unknown identifier";
483 /*****************************************************************************
485 *****************************************************************************/
487 - (void)tableViewSelectionDidChange:(NSNotification *)aNotification
489 /* check whether a row is selected and en-/disable the edit/remove buttons */
490 if ([o_tbl_dataTable selectedRow] == -1)
492 /* no row is selected */
493 [o_btn_edit setEnabled: NO];
494 [o_btn_rm setEnabled: NO];
495 [o_btn_extract setEnabled: NO];
499 /* a row is selected */
500 [o_btn_edit setEnabled: YES];
501 [o_btn_rm setEnabled: YES];
502 if ([o_tbl_dataTable numberOfSelectedRows] == 2)
504 [o_btn_extract setEnabled: YES];