]> git.sesse.net Git - vlc/blob - modules/gui/macosx/bookmarks.m
Merge branch 'master' into lpcm_encoder
[vlc] / modules / gui / macosx / bookmarks.m
1 /*****************************************************************************
2  * bookmarks.m: MacOS X Bookmarks window
3  *****************************************************************************
4  * Copyright (C) 2005 - 2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Felix Kühne <fkuehne at videolan dot org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24
25 /*****************************************************************************
26  * Note:
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  *****************************************************************************/
31
32
33 /*****************************************************************************
34  * Preamble
35  *****************************************************************************/
36
37 #import "bookmarks.h"
38 #import "wizard.h"
39 #import <vlc_interface.h>
40
41 /*****************************************************************************
42  * VLCExtended implementation
43  *
44  * implements the GUI functions for the window, the data source and the
45  * delegate for o_tbl_dataTable
46  *****************************************************************************/
47
48 @implementation VLCBookmarks
49
50 static VLCBookmarks *_o_sharedInstance = nil;
51
52 + (VLCBookmarks *)sharedInstance
53 {
54     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
55 }
56
57 - (id)init
58 {
59     if (_o_sharedInstance) {
60         [self dealloc];
61     } else {
62         _o_sharedInstance = [super init];
63     }
64
65     return _o_sharedInstance;
66 }
67
68 /*****************************************************************************
69  * GUI methods
70  *****************************************************************************/
71
72 - (void)awakeFromNib
73 {
74     [self initStrings];
75 }
76
77 - (void)dealloc
78 {
79     [super dealloc];
80 }
81
82 - (void)initStrings
83 {
84     /* localise the items */
85  
86     /* main window */
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")];
99  
100     /* edit window */
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")];
106 }
107
108 - (void)showBookmarks
109 {
110     /* show the window, called from intf.m */
111     [o_bookmarks_window displayIfNeeded];
112     [o_bookmarks_window makeKeyAndOrderFront:nil];
113 }
114
115 - (IBAction)add:(id)sender
116 {
117     /* add item to list */
118     input_thread_t * p_input = pl_CurrentInput( VLCIntf );
119
120     if( !p_input ) return;
121  
122     seekpoint_t bookmark;
123
124     if( !input_Control( p_input, INPUT_GET_BOOKMARK, &bookmark ) )
125         {
126                 bookmark.psz_name = _("Untitled");
127                 input_Control( p_input, INPUT_ADD_BOOKMARK, &bookmark );
128         }
129  
130     vlc_object_release( p_input );
131  
132     [o_tbl_dataTable reloadData];
133 }
134
135 - (IBAction)clear:(id)sender
136 {
137     /* clear table */
138     input_thread_t * p_input = pl_CurrentInput( VLCIntf );
139  
140     if( !p_input )
141         return;
142
143     input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
144
145     vlc_object_release( p_input );
146  
147     [o_tbl_dataTable reloadData];
148 }
149
150 - (IBAction)edit:(id)sender
151 {
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
154      * really trust it */
155     input_thread_t * p_input = pl_CurrentInput( VLCIntf );
156     seekpoint_t **pp_bookmarks;
157     int i_bookmarks;
158     int row;
159     row = [o_tbl_dataTable selectedRow];
160  
161     if( !p_input && row < 0 )
162         return;
163
164     if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
165         &i_bookmarks ) != VLC_SUCCESS )
166     {
167         vlc_object_release( p_input );
168         return;
169     }
170
171     [o_edit_fld_name setStringValue: [NSString stringWithUTF8String:
172             pp_bookmarks[row]->psz_name]];
173     [o_edit_fld_time setStringValue: [[NSNumber numberWithInt:
174             (pp_bookmarks[row]->i_time_offset / 1000000)] stringValue]];
175     [o_edit_fld_bytes setStringValue: [[NSNumber numberWithInt:
176             pp_bookmarks[row]->i_byte_offset] stringValue]];
177  
178     /* Just keep the pointer value to check if it
179      * changes. Note, we don't need to keep a reference to the object.
180      * so release it now. */
181     p_old_input = p_input;
182     vlc_object_release( p_input );
183
184     [NSApp beginSheet: o_edit_window
185         modalForWindow: o_bookmarks_window
186         modalDelegate: o_edit_window
187         didEndSelector: nil
188         contextInfo: nil];
189
190     // Clear the bookmark list
191     for( int i = 0; i < i_bookmarks; i++)
192         vlc_seekpoint_Delete( pp_bookmarks[i] );
193     free( pp_bookmarks );
194
195 }
196
197 - (IBAction)edit_cancel:(id)sender
198 {
199     /* close sheet */
200     [NSApp endSheet:o_edit_window];
201     [o_edit_window close];
202 }
203
204 - (IBAction)edit_ok:(id)sender
205 {
206     /* save field contents and close sheet */
207      seekpoint_t **pp_bookmarks;
208     int i_bookmarks, i;
209     input_thread_t * p_input = pl_CurrentInput( VLCIntf );
210  
211     if( !p_input )
212     {
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."));
217         return;
218     }
219     if( p_old_input != p_input )
220     {
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 "
225             "input."));
226         vlc_object_release( p_input );
227         return;
228     }
229  
230     if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
231         &i_bookmarks ) != VLC_SUCCESS )
232     {
233         vlc_object_release( p_input );
234         return;
235     }
236
237     i = [o_tbl_dataTable selectedRow];
238  
239     free( pp_bookmarks[i]->psz_name );
240
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);
244  
245     if( input_Control( p_input, INPUT_CHANGE_BOOKMARK, pp_bookmarks[i], i )
246         != VLC_SUCCESS )
247     {
248         msg_Warn( VLCIntf, "Unable to change the bookmark");
249         goto clear;
250     }
251  
252     [o_tbl_dataTable reloadData];
253     vlc_object_release( p_input );
254  
255  
256     [NSApp endSheet: o_edit_window];
257     [o_edit_window close];
258
259 clear:
260     // Clear the bookmark list
261     for( int i = 0; i < i_bookmarks; i++)
262         vlc_seekpoint_Delete( pp_bookmarks[i] );
263     free( pp_bookmarks );
264 }
265
266 - (IBAction)extract:(id)sender
267 {
268     /* extract */
269     if( [o_tbl_dataTable numberOfSelectedRows] < 2 )
270     {
271         NSBeginAlertSheet(_NS("Invalid selection"), _NS("OK"),
272             @"", @"", o_bookmarks_window, nil, nil, nil, nil,
273             _NS("Two bookmarks have to be selected."));
274         return;
275     }
276     input_thread_t * p_input = pl_CurrentInput( VLCIntf );
277     if( !p_input )
278     {
279         NSBeginCriticalAlertSheet(_NS("No input found"), _NS("OK"),
280             @"", @"", o_bookmarks_window, nil, nil, nil, nil,
281             _NS("The stream must be playing or paused for bookmarks to work."));
282         return;
283     }
284  
285     seekpoint_t **pp_bookmarks;
286     int i_bookmarks ;
287     int i_first = -1;
288     int i_second = -1;
289     int x = 0;
290     int c = 0;
291     while (c != 2)
292     {
293         if([o_tbl_dataTable isRowSelected:x])
294         {
295             if (i_first == -1)
296             {
297                 i_first = x;
298                 c = 1;
299             }
300             else if (i_second == -1)
301             {
302                 i_second = x;
303                 c = 2;
304             }
305         }
306         x = (x + 1);
307     }
308  
309     msg_Dbg( VLCIntf, "got the bookmark-indexes");
310  
311     if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
312         &i_bookmarks ) != VLC_SUCCESS )
313     {
314         vlc_object_release( p_input );
315         msg_Err( VLCIntf, "already defined bookmarks couldn't be retrieved");
316         return;
317     }
318     msg_Dbg( VLCIntf, "calling wizard");
319
320     char *psz_uri = input_item_GetURI( input_GetItem( p_input ) );
321     [[[VLCMain sharedInstance] wizard] initWithExtractValuesFrom:
322             [[NSNumber numberWithInt:
323             (pp_bookmarks[i_first]->i_time_offset/1000000)] stringValue]
324             to: [[NSNumber numberWithInt:
325             (pp_bookmarks[i_second]->i_time_offset/1000000)] stringValue]
326             ofItem: [NSString stringWithUTF8String: psz_uri]];
327     free( psz_uri );
328     vlc_object_release( p_input );
329     msg_Dbg( VLCIntf, "released input");
330
331     // Clear the bookmark list
332     for( int i = 0; i < i_bookmarks; i++)
333         vlc_seekpoint_Delete( pp_bookmarks[i] );
334     free( pp_bookmarks );
335 }
336
337 - (IBAction)goToBookmark:(id)sender
338 {
339     input_thread_t * p_input = pl_CurrentInput( VLCIntf );
340  
341     if( !p_input ) return;
342
343     input_Control( p_input, INPUT_SET_BOOKMARK, [o_tbl_dataTable selectedRow] );
344
345     vlc_object_release( p_input );
346 }
347
348 - (IBAction)remove:(id)sender
349 {
350     /* remove selected item */
351     input_thread_t * p_input = pl_CurrentInput( VLCIntf );
352  
353     if( !p_input ) return;
354
355     int i_focused = [o_tbl_dataTable selectedRow];
356
357     if( i_focused >= 0 )
358         input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
359
360     vlc_object_release( p_input );
361  
362     [o_tbl_dataTable reloadData];
363 }
364
365 /*****************************************************************************
366  * callback stuff
367  *****************************************************************************/
368
369 -(id)dataTable
370 {
371     return o_tbl_dataTable;
372 }
373
374 /*****************************************************************************
375  * data source methods
376  *****************************************************************************/
377
378 - (NSInteger)numberOfRowsInTableView:(NSTableView *)theDataTable
379 {
380     /* return the number of bookmarks */
381     input_thread_t * p_input = pl_CurrentInput( VLCIntf );
382     seekpoint_t **pp_bookmarks;
383     int i_bookmarks;
384  
385     if( !p_input ) return 0;
386     else if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
387                        &i_bookmarks ) != VLC_SUCCESS )
388     {
389         vlc_object_release( p_input );
390         return 0;
391     }
392     else {
393         vlc_object_release( p_input );
394         // Clear the bookmark list
395         for( int i = 0; i < i_bookmarks; i++)
396             vlc_seekpoint_Delete( pp_bookmarks[i] );
397         free( pp_bookmarks );
398         return i_bookmarks;
399     }
400 }
401
402 - (id)tableView:(NSTableView *)theDataTable objectValueForTableColumn:
403     (NSTableColumn *)theTableColumn row: (NSInteger)row
404 {
405     /* return the corresponding data as NSString */
406     input_thread_t * p_input = pl_CurrentInput( VLCIntf );
407     seekpoint_t **pp_bookmarks;
408     int i_bookmarks;
409     char *toBeReturned;
410     int i_toBeReturned = 0;
411     id ret;
412
413     if( !p_input ) return @"";
414     else if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
415                        &i_bookmarks ) != VLC_SUCCESS )
416     {
417         ret = @"";
418     }
419     else
420     {
421         if ([[theTableColumn identifier] isEqualToString: @"description"])
422         {
423             toBeReturned = pp_bookmarks[row]->psz_name;
424             ret = [NSString stringWithUTF8String: toBeReturned];
425         }
426         else if ([[theTableColumn identifier] isEqualToString: @"size_offset"])
427         {
428             i_toBeReturned = pp_bookmarks[row]->i_byte_offset;
429             ret = [[NSNumber numberWithInt: i_toBeReturned] stringValue];
430         }
431         else if ([[theTableColumn identifier] isEqualToString: @"time_offset"])
432         {
433             i_toBeReturned = pp_bookmarks[row]->i_time_offset;
434             ret = [[NSNumber numberWithInt: (i_toBeReturned / 1000000)]
435                 stringValue];
436         }
437         else
438         {
439             /* may not happen, just in case */
440             msg_Err( VLCIntf, "unknown table column identifier (%s) while "
441                 "updating the bookmark table", [[theTableColumn identifier]
442                 UTF8String] );
443             ret = @"unknown identifier";
444         }
445
446         // Clear the bookmark list
447         for( int i = 0; i < i_bookmarks; i++)
448             vlc_seekpoint_Delete( pp_bookmarks[i] );
449         free( pp_bookmarks );
450     }
451     vlc_object_release( p_input );
452     return ret;
453 }
454
455 /*****************************************************************************
456  * delegate methods
457  *****************************************************************************/
458
459 - (void)tableViewSelectionDidChange:(NSNotification *)aNotification
460 {
461     /* check whether a row is selected and en-/disable the edit/remove buttons */
462     if ([o_tbl_dataTable selectedRow] == -1)
463     {
464         /* no row is selected */
465         [o_btn_edit setEnabled: NO];
466         [o_btn_rm setEnabled: NO];
467         [o_btn_extract setEnabled: NO];
468     }
469     else
470     {
471         /* a row is selected */
472         [o_btn_edit setEnabled: YES];
473         [o_btn_rm setEnabled: YES];
474         if ([o_tbl_dataTable numberOfSelectedRows] == 2)
475         {
476             [o_btn_extract setEnabled: YES];
477         }
478     }
479 }
480
481 @end