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 *****************************************************************************/
84 /* localise the items */
87 [o_bookmarks_window setTitle: _NS("Bookmarks")];
88 [o_btn_add setTitle: _NS("Add")];
89 [o_btn_clear setTitle: _NS("Clear")];
90 [o_btn_edit setTitle: _NS("Edit")];
91 [o_btn_extract setTitle: _NS("Extract")];
92 [o_btn_rm setTitle: _NS("Remove")];
93 [[[o_tbl_dataTable tableColumnWithIdentifier:@"description"] headerCell]
94 setStringValue: _NS("Description")];
95 [[[o_tbl_dataTable tableColumnWithIdentifier:@"size_offset"] headerCell]
96 setStringValue: _NS("Position")];
97 [[[o_tbl_dataTable tableColumnWithIdentifier:@"time_offset"] headerCell]
98 setStringValue: _NS("Time")];
101 [o_edit_btn_ok setTitle: _NS("OK")];
102 [o_edit_btn_cancel setTitle: _NS("Cancel")];
103 [o_edit_lbl_name setStringValue: _NS("Name")];
104 [o_edit_lbl_time setStringValue: _NS("Time")];
105 [o_edit_lbl_bytes setStringValue: _NS("Position")];
108 - (void)showBookmarks
110 /* show the window, called from intf.m */
111 [o_bookmarks_window displayIfNeeded];
112 [o_bookmarks_window makeKeyAndOrderFront:nil];
115 - (IBAction)add:(id)sender
117 /* add item to list */
118 input_thread_t * p_input = pl_CurrentInput( VLCIntf );
120 if( !p_input ) return;
122 seekpoint_t bookmark;
124 if( !input_Control( p_input, INPUT_GET_BOOKMARK, &bookmark ) )
126 bookmark.psz_name = _("Untitled");
127 input_Control( p_input, INPUT_ADD_BOOKMARK, &bookmark );
130 vlc_object_release( p_input );
132 [o_tbl_dataTable reloadData];
135 - (IBAction)clear:(id)sender
138 input_thread_t * p_input = pl_CurrentInput( VLCIntf );
143 input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
145 vlc_object_release( p_input );
147 [o_tbl_dataTable reloadData];
150 - (IBAction)edit:(id)sender
152 /* put values to the sheet's fields and show sheet */
153 /* we take the values from the core and not the table, because we cannot
155 input_thread_t * p_input = pl_CurrentInput( VLCIntf );
156 seekpoint_t **pp_bookmarks;
159 row = [o_tbl_dataTable selectedRow];
165 else if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
166 &i_bookmarks ) != VLC_SUCCESS )
168 vlc_object_release( p_input );
173 vlc_object_release( p_input );
176 [o_edit_fld_name setStringValue: [NSString stringWithUTF8String:
177 pp_bookmarks[row]->psz_name]];
178 [o_edit_fld_time setStringValue: [[NSNumber numberWithInt:
179 (pp_bookmarks[row]->i_time_offset / 1000000)] stringValue]];
180 [o_edit_fld_bytes setStringValue: [[NSNumber numberWithInt:
181 pp_bookmarks[row]->i_byte_offset] stringValue]];
184 /* Just keep the pointer value to check if it
185 * changes. Note, we don't need to keep a reference to the object.
186 * so release it now. */
187 p_old_input = p_input;
188 vlc_object_release( p_input );
190 [NSApp beginSheet: o_edit_window
191 modalForWindow: o_bookmarks_window
192 modalDelegate: o_edit_window
197 - (IBAction)edit_cancel:(id)sender
200 [NSApp endSheet:o_edit_window];
201 [o_edit_window close];
204 - (IBAction)edit_ok:(id)sender
206 /* save field contents and close sheet */
207 seekpoint_t **pp_bookmarks;
209 input_thread_t * p_input = pl_CurrentInput( VLCIntf );
213 NSBeginCriticalAlertSheet(_NS("No input"), _NS("OK"),
214 @"", @"", o_bookmarks_window, nil, nil, nil, nil, _NS("No "
215 "input found. A stream must be playing or paused for "
216 "bookmarks to work."));
219 if( p_old_input != p_input )
221 NSBeginCriticalAlertSheet(_NS("Input has changed"), _NS("OK"),
222 @"", @"", o_bookmarks_window, nil, nil, nil, nil, _NS("Input "
223 "has changed, unable to save bookmark. Suspending playback with "
224 "\"Pause\" while editing bookmarks to ensure to keep the same "
226 vlc_object_release( p_input );
230 if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
231 &i_bookmarks ) != VLC_SUCCESS )
233 vlc_object_release( p_input );
237 i = [o_tbl_dataTable selectedRow];
239 free( pp_bookmarks[i]->psz_name );
241 pp_bookmarks[i]->psz_name = strdup([[o_edit_fld_name stringValue] UTF8String]);
242 pp_bookmarks[i]->i_byte_offset = [[o_edit_fld_bytes stringValue] intValue];
243 pp_bookmarks[i]->i_time_offset = ([[o_edit_fld_time stringValue] intValue] * 1000000);
245 if( input_Control( p_input, INPUT_CHANGE_BOOKMARK, pp_bookmarks[i], i )
248 msg_Warn( VLCIntf, "Unable to change the bookmark");
249 vlc_object_release( p_input );
253 [o_tbl_dataTable reloadData];
254 vlc_object_release( p_input );
257 [NSApp endSheet: o_edit_window];
258 [o_edit_window close];
261 - (IBAction)extract:(id)sender
264 if( [o_tbl_dataTable numberOfSelectedRows] < 2 )
266 NSBeginAlertSheet(_NS("Invalid selection"), _NS("OK"),
267 @"", @"", o_bookmarks_window, nil, nil, nil, nil,
268 _NS("Two bookmarks have to be selected."));
271 input_thread_t * p_input = pl_CurrentInput( VLCIntf );
274 NSBeginCriticalAlertSheet(_NS("No input found"), _NS("OK"),
275 @"", @"", o_bookmarks_window, nil, nil, nil, nil,
276 _NS("The stream must be playing or paused for bookmarks to work."));
280 seekpoint_t **pp_bookmarks;
288 if([o_tbl_dataTable isRowSelected:x])
295 else if (i_second == -1)
304 msg_Dbg( VLCIntf, "got the bookmark-indexes");
306 if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
307 &i_bookmarks ) != VLC_SUCCESS )
309 vlc_object_release( p_input );
310 msg_Err( VLCIntf, "already defined bookmarks couldn't be retrieved");
313 msg_Dbg( VLCIntf, "calling wizard");
315 char *psz_uri = input_item_GetURI( input_GetItem( p_input ) );
316 [[[VLCMain sharedInstance] getWizard] initWithExtractValuesFrom:
317 [[NSNumber numberWithInt:
318 (pp_bookmarks[i_first]->i_time_offset/1000000)] stringValue]
319 to: [[NSNumber numberWithInt:
320 (pp_bookmarks[i_second]->i_time_offset/1000000)] stringValue]
321 ofItem: [NSString stringWithUTF8String: psz_uri]];
323 vlc_object_release( p_input );
324 msg_Dbg( VLCIntf, "released input");
327 - (IBAction)goToBookmark:(id)sender
329 input_thread_t * p_input = pl_CurrentInput( VLCIntf );
331 if( !p_input ) return;
333 input_Control( p_input, INPUT_SET_BOOKMARK, [o_tbl_dataTable selectedRow] );
335 vlc_object_release( p_input );
338 - (IBAction)remove:(id)sender
340 /* remove selected item */
341 input_thread_t * p_input = pl_CurrentInput( VLCIntf );
343 if( !p_input ) return;
345 int i_focused = [o_tbl_dataTable selectedRow];
348 input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
350 vlc_object_release( p_input );
352 [o_tbl_dataTable reloadData];
355 /*****************************************************************************
357 *****************************************************************************/
361 return o_tbl_dataTable;
364 /*****************************************************************************
365 * data source methods
366 *****************************************************************************/
368 - (int)numberOfRowsInTableView:(NSTableView *)theDataTable
370 /* return the number of bookmarks */
371 input_thread_t * p_input = pl_CurrentInput( VLCIntf );
372 seekpoint_t **pp_bookmarks;
375 if( !p_input ) return 0;
376 else if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
377 &i_bookmarks ) != VLC_SUCCESS )
379 vlc_object_release( p_input );
383 vlc_object_release( p_input );
388 - (id)tableView:(NSTableView *)theDataTable objectValueForTableColumn:
389 (NSTableColumn *)theTableColumn row: (int)row
391 /* return the corresponding data as NSString */
392 input_thread_t * p_input = pl_CurrentInput( VLCIntf );
393 seekpoint_t **pp_bookmarks;
396 int i_toBeReturned = 0;
399 if( !p_input ) return @"";
400 else if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
401 &i_bookmarks ) != VLC_SUCCESS )
407 if ([[theTableColumn identifier] isEqualToString: @"description"])
409 toBeReturned = pp_bookmarks[row]->psz_name;
410 ret = [NSString stringWithUTF8String: toBeReturned];
412 else if ([[theTableColumn identifier] isEqualToString: @"size_offset"])
414 i_toBeReturned = pp_bookmarks[row]->i_byte_offset;
415 ret = [[NSNumber numberWithInt: i_toBeReturned] stringValue];
417 else if ([[theTableColumn identifier] isEqualToString: @"time_offset"])
419 i_toBeReturned = pp_bookmarks[row]->i_time_offset;
420 ret = [[NSNumber numberWithInt: (i_toBeReturned / 1000000)]
425 /* may not happen, just in case */
426 msg_Err( VLCIntf, "unknown table column identifier (%s) while "
427 "updating the bookmark table", [[theTableColumn identifier]
429 ret = @"unknown identifier";
432 vlc_object_release( p_input );
436 /*****************************************************************************
438 *****************************************************************************/
440 - (void)tableViewSelectionDidChange:(NSNotification *)aNotification
442 /* check whether a row is selected and en-/disable the edit/remove buttons */
443 if ([o_tbl_dataTable selectedRow] == -1)
445 /* no row is selected */
446 [o_btn_edit setEnabled: NO];
447 [o_btn_rm setEnabled: NO];
448 [o_btn_extract setEnabled: NO];
452 /* a row is selected */
453 [o_btn_edit setEnabled: YES];
454 [o_btn_rm setEnabled: YES];
455 if ([o_tbl_dataTable numberOfSelectedRows] == 2)
457 [o_btn_extract setEnabled: YES];