1 /*****************************************************************************
2 * bookmarks.m: MacOS X Bookmarks window
3 *****************************************************************************
4 * Copyright (C) 2005 - 2012 VLC authors and VideoLAN
7 * Authors: Felix Paul Kühne <fkuehne at videolan dot org>
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>
40 #import "CompatibilityFixes.h"
42 @interface VLCBookmarks (Internal)
46 @implementation VLCBookmarks
48 static VLCBookmarks *_o_sharedInstance = nil;
50 + (VLCBookmarks *)sharedInstance
52 return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
57 if (_o_sharedInstance)
60 _o_sharedInstance = [super init];
62 return _o_sharedInstance;
65 /*****************************************************************************
67 *****************************************************************************/
71 if (!OSX_SNOW_LEOPARD)
72 [o_bookmarks_window setCollectionBehavior: NSWindowCollectionBehaviorFullScreenAuxiliary];
80 vlc_object_release(p_old_input);
87 /* localise the items */
90 [o_bookmarks_window setTitle: _NS("Bookmarks")];
91 [o_btn_add setTitle: _NS("Add")];
92 [o_btn_clear setTitle: _NS("Clear")];
93 [o_btn_edit setTitle: _NS("Edit")];
94 [o_btn_extract setTitle: _NS("Extract")];
95 [o_btn_rm setTitle: _NS("Remove")];
96 [[[o_tbl_dataTable tableColumnWithIdentifier:@"description"] headerCell]
97 setStringValue: _NS("Description")];
98 [[[o_tbl_dataTable tableColumnWithIdentifier:@"size_offset"] headerCell]
99 setStringValue: _NS("Position")];
100 [[[o_tbl_dataTable tableColumnWithIdentifier:@"time_offset"] headerCell]
101 setStringValue: _NS("Time")];
104 [o_edit_btn_ok setTitle: _NS("OK")];
105 [o_edit_btn_cancel setTitle: _NS("Cancel")];
106 [o_edit_lbl_name setStringValue: _NS("Name")];
107 [o_edit_lbl_time setStringValue: _NS("Time")];
108 [o_edit_lbl_bytes setStringValue: _NS("Position")];
111 - (void)updateCocoaWindowLevel:(NSInteger)i_level
113 if (o_bookmarks_window && [o_bookmarks_window isVisible] && [o_bookmarks_window level] != i_level)
114 [o_bookmarks_window setLevel: i_level];
117 - (void)showBookmarks
119 /* show the window, called from intf.m */
120 [o_bookmarks_window displayIfNeeded];
121 [o_bookmarks_window setLevel: [[[VLCMain sharedInstance] voutController] currentWindowLevel]];
122 [o_bookmarks_window makeKeyAndOrderFront:nil];
125 - (IBAction)add:(id)sender
127 /* add item to list */
128 input_thread_t * p_input = pl_CurrentInput(VLCIntf);
133 seekpoint_t bookmark;
135 if (!input_Control(p_input, INPUT_GET_BOOKMARK, &bookmark)) {
136 bookmark.psz_name = _("Untitled");
137 input_Control(p_input, INPUT_ADD_BOOKMARK, &bookmark);
140 vlc_object_release(p_input);
142 [o_tbl_dataTable reloadData];
145 - (IBAction)clear:(id)sender
148 input_thread_t * p_input = pl_CurrentInput(VLCIntf);
153 input_Control(p_input, INPUT_CLEAR_BOOKMARKS);
155 vlc_object_release(p_input);
157 [o_tbl_dataTable reloadData];
160 - (IBAction)edit:(id)sender
162 /* put values to the sheet's fields and show sheet */
163 /* we take the values from the core and not the table, because we cannot
165 input_thread_t * p_input = pl_CurrentInput(VLCIntf);
166 seekpoint_t **pp_bookmarks;
169 row = [o_tbl_dataTable selectedRow];
175 vlc_object_release(p_input);
179 if (input_Control(p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks, &i_bookmarks) != VLC_SUCCESS) {
180 vlc_object_release(p_input);
184 [o_edit_fld_name setStringValue: [NSString stringWithFormat:@"%s", pp_bookmarks[row]->psz_name]];
185 int total = pp_bookmarks[row]->i_time_offset/ 1000000;
186 int hour = total / (60*60);
187 int min = (total - hour*60*60) / 60;
188 int sec = total - hour*60*60 - min*60;
189 [o_edit_fld_time setStringValue: [NSString stringWithFormat:@"%02d:%02d:%02d", hour, min, sec]];
190 [o_edit_fld_bytes setStringValue: [NSString stringWithFormat:@"%lli", pp_bookmarks[row]->i_byte_offset]];
192 /* Just keep the pointer value to check if it
193 * changes. Note, we don't need to keep a reference to the object.
194 * so release it now. */
195 p_old_input = p_input;
196 vlc_object_release(p_input);
198 [NSApp beginSheet: o_edit_window modalForWindow: o_bookmarks_window modalDelegate: o_edit_window didEndSelector: nil contextInfo: nil];
200 // Clear the bookmark list
201 for (int i = 0; i < i_bookmarks; i++)
202 vlc_seekpoint_Delete(pp_bookmarks[i]);
206 - (IBAction)edit_cancel:(id)sender
209 [NSApp endSheet:o_edit_window];
210 [o_edit_window close];
213 - (IBAction)edit_ok:(id)sender
215 /* save field contents and close sheet */
216 seekpoint_t **pp_bookmarks;
218 input_thread_t * p_input = pl_CurrentInput(VLCIntf);
221 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."));
224 if (p_old_input != p_input) {
225 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."));
226 vlc_object_release(p_input);
230 if (input_Control(p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks, &i_bookmarks) != VLC_SUCCESS) {
231 vlc_object_release(p_input);
235 i = [o_tbl_dataTable selectedRow];
237 free(pp_bookmarks[i]->psz_name);
239 pp_bookmarks[i]->psz_name = strdup([[o_edit_fld_name stringValue] UTF8String]);
240 pp_bookmarks[i]->i_byte_offset = [[o_edit_fld_bytes stringValue] intValue];
242 NSArray * components = [[o_edit_fld_time stringValue] componentsSeparatedByString:@":"];
243 NSUInteger componentCount = [components count];
244 if (componentCount == 1)
245 pp_bookmarks[i]->i_time_offset = 1000000 * ([components[0] intValue]);
246 else if (componentCount == 2)
247 pp_bookmarks[i]->i_time_offset = 1000000 * ([components[0] intValue] * 60 + [components[1] intValue]);
248 else if (componentCount == 3)
249 pp_bookmarks[i]->i_time_offset = 1000000 * ([components[0] intValue] * 3600 + [components[1] intValue] * 60 + [components[2] intValue]);
251 msg_Err(VLCIntf, "Invalid string format for time");
255 if (input_Control(p_input, INPUT_CHANGE_BOOKMARK, pp_bookmarks[i], i) != VLC_SUCCESS) {
256 msg_Warn(VLCIntf, "Unable to change the bookmark");
260 [o_tbl_dataTable reloadData];
261 vlc_object_release(p_input);
263 [NSApp endSheet: o_edit_window];
264 [o_edit_window close];
267 // Clear the bookmark list
268 for (int i = 0; i < i_bookmarks; i++)
269 vlc_seekpoint_Delete(pp_bookmarks[i]);
273 - (IBAction)extract:(id)sender
275 if ([o_tbl_dataTable numberOfSelectedRows] < 2) {
276 NSBeginAlertSheet(_NS("Invalid selection"), _NS("OK"), @"", @"", o_bookmarks_window, nil, nil, nil, nil, @"%@",_NS("Two bookmarks have to be selected."));
279 input_thread_t * p_input = pl_CurrentInput(VLCIntf);
281 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."));
285 seekpoint_t **pp_bookmarks;
290 for (NSUInteger x = 0; c != 2; x++) {
291 if ([o_tbl_dataTable isRowSelected:x]) {
295 } else if (i_second == -1) {
302 if (input_Control(p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks, &i_bookmarks) != VLC_SUCCESS) {
303 vlc_object_release(p_input);
304 msg_Err(VLCIntf, "already defined bookmarks couldn't be retrieved");
308 char *psz_uri = input_item_GetURI(input_GetItem(p_input));
309 [[[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: [NSString stringWithFormat:@"%s", psz_uri]];
311 vlc_object_release(p_input);
313 // Clear the bookmark list
314 for (int i = 0; i < i_bookmarks; i++)
315 vlc_seekpoint_Delete(pp_bookmarks[i]);
319 - (IBAction)goToBookmark:(id)sender
321 input_thread_t * p_input = pl_CurrentInput(VLCIntf);
326 input_Control(p_input, INPUT_SET_BOOKMARK, [o_tbl_dataTable selectedRow]);
328 vlc_object_release(p_input);
331 - (IBAction)remove:(id)sender
333 input_thread_t * p_input = pl_CurrentInput(VLCIntf);
338 int i_focused = [o_tbl_dataTable selectedRow];
341 input_Control(p_input, INPUT_DEL_BOOKMARK, i_focused);
343 vlc_object_release(p_input);
345 [o_tbl_dataTable reloadData];
348 /*****************************************************************************
350 *****************************************************************************/
354 return o_tbl_dataTable;
357 /*****************************************************************************
358 * data source methods
359 *****************************************************************************/
361 - (NSInteger)numberOfRowsInTableView:(NSTableView *)theDataTable
363 /* return the number of bookmarks */
364 input_thread_t * p_input = pl_CurrentInput(VLCIntf);
365 seekpoint_t **pp_bookmarks;
370 else if (input_Control(p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks, &i_bookmarks) != VLC_SUCCESS) {
371 vlc_object_release(p_input);
374 vlc_object_release(p_input);
375 // Clear the bookmark list
376 for (int i = 0; i < i_bookmarks; i++)
377 vlc_seekpoint_Delete(pp_bookmarks[i]);
383 - (id)tableView:(NSTableView *)theDataTable objectValueForTableColumn: (NSTableColumn *)theTableColumn row: (NSInteger)row
385 /* return the corresponding data as NSString */
386 input_thread_t * p_input = pl_CurrentInput(VLCIntf);
387 seekpoint_t **pp_bookmarks;
393 else if (input_Control(p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks, &i_bookmarks) != VLC_SUCCESS)
396 NSString * identifier = [theTableColumn identifier];
397 if ([identifier isEqualToString: @"description"])
398 ret = [NSString stringWithFormat:@"%s", pp_bookmarks[row]->psz_name];
399 else if ([identifier isEqualToString: @"size_offset"])
400 ret = [NSString stringWithFormat:@"%lli", pp_bookmarks[row]->i_byte_offset];
401 else if ([identifier isEqualToString: @"time_offset"]) {
402 int total = pp_bookmarks[row]->i_time_offset/ 1000000;
403 int hour = total / (60*60);
404 int min = (total - hour*60*60) / 60;
405 int sec = total - hour*60*60 - min*60;
406 ret = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, min, sec];
409 // Clear the bookmark list
410 for (int i = 0; i < i_bookmarks; i++)
411 vlc_seekpoint_Delete(pp_bookmarks[i]);
414 vlc_object_release(p_input);
418 /*****************************************************************************
420 *****************************************************************************/
422 - (void)tableViewSelectionDidChange:(NSNotification *)aNotification
424 /* check whether a row is selected and en-/disable the edit/remove buttons */
425 if ([o_tbl_dataTable selectedRow] == -1) {
426 /* no row is selected */
427 [o_btn_edit setEnabled: NO];
428 [o_btn_rm setEnabled: NO];
429 [o_btn_extract setEnabled: NO];
431 /* a row is selected */
432 [o_btn_edit setEnabled: YES];
433 [o_btn_rm setEnabled: YES];
434 if ([o_tbl_dataTable numberOfSelectedRows] == 2)
435 [o_btn_extract setEnabled: YES];