Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

bookmarks.m

00001 /*****************************************************************************
00002  * bookmarks.m: MacOS X Bookmarks window
00003  *****************************************************************************
00004  * Copyright (C) 2005 the VideoLAN team
00005  * $Id: bookmarks.m 12986 2005-10-27 18:15:35Z fkuehne $
00006  *
00007  * Authors: Felix KŸhne <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 
00025 /*****************************************************************************
00026  * Note: 
00027  * the code used to bind with VLC's modules is heavily based upon 
00028  * ../wxwidgets/bookmarks.cpp, written by Gildas Bazin. 
00029  * (he is a member of the VideoLAN team) 
00030  *****************************************************************************/
00031 
00032 
00033 /*****************************************************************************
00034  * Preamble
00035  *****************************************************************************/
00036 
00037 #import "bookmarks.h"
00038 #import "intf.h"
00039 #import "wizard.h"
00040 #import <vlc/intf.h>
00041 
00042 /*****************************************************************************
00043  * VLCExtended implementation
00044  *
00045  * implements the GUI functions for the window, the data source and the
00046  * delegate for o_tbl_dataTable
00047  *****************************************************************************/
00048 
00049 @implementation VLCBookmarks
00050 
00051 static VLCBookmarks *_o_sharedInstance = nil;
00052 
00053 + (VLCBookmarks *)sharedInstance
00054 {
00055     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
00056 }
00057 
00058 - (id)init
00059 {
00060     if (_o_sharedInstance) {
00061         [self dealloc];
00062     } else {
00063         _o_sharedInstance = [super init];
00064     }
00065 
00066     return _o_sharedInstance;
00067 }
00068 
00069 /*****************************************************************************
00070  * GUI methods
00071  *****************************************************************************/
00072 
00073 - (void)awakeFromNib
00074 {
00075     [self initStrings];
00076 }
00077 
00078 - (void)dealloc
00079 {
00080     if(p_old_input)
00081     {
00082         free(p_old_input);
00083     }
00084     [super dealloc];
00085 }
00086 
00087 - (void)initStrings
00088 {
00089     /* localise the items */
00090     
00091     /* main window */
00092     [o_bookmarks_window setTitle: _NS("Bookmarks")];
00093     [o_btn_add setTitle: _NS("Add")];
00094     [o_btn_clear setTitle: _NS("Clear")];
00095     [o_btn_edit setTitle: _NS("Edit")];
00096     [o_btn_extract setTitle: _NS("Extract")];
00097     [o_btn_rm setTitle: _NS("Remove")];
00098     [[[o_tbl_dataTable tableColumnWithIdentifier:@"description"] headerCell] \
00099         setStringValue: _NS("Description")];
00100     [[[o_tbl_dataTable tableColumnWithIdentifier:@"size_offset"] headerCell] \
00101         setStringValue: _NS("Size offset")];
00102     [[[o_tbl_dataTable tableColumnWithIdentifier:@"time_offset"] headerCell] \
00103         setStringValue: _NS("Time offset")];
00104         
00105     /* edit window */
00106     [o_edit_btn_ok setTitle: _NS("OK")];
00107     [o_edit_btn_cancel setTitle: _NS("Cancel")];
00108     [o_edit_lbl_name setStringValue: _NS("Name")];
00109     [o_edit_lbl_time setStringValue: _NS("Time")];
00110     [o_edit_lbl_bytes setStringValue: _NS("Bytes")];
00111 }
00112 
00113 - (void)showBookmarks
00114 {
00115     /* show the window, called from intf.m */
00116     [o_bookmarks_window displayIfNeeded];
00117     [o_bookmarks_window makeKeyAndOrderFront:nil];
00118 }
00119 
00120 - (IBAction)add:(id)sender
00121 {
00122     /* add item to list */
00123     intf_thread_t * p_intf = VLCIntf;
00124     input_thread_t * p_input = (input_thread_t *)vlc_object_find( p_intf, \
00125         VLC_OBJECT_INPUT, FIND_ANYWHERE );
00126     if( !p_input )
00127         return;
00128     
00129     seekpoint_t bookmark;
00130     vlc_value_t pos;
00131     bookmark.psz_name = NULL;
00132     bookmark.i_byte_offset = 0;
00133     bookmark.i_time_offset = 0;
00134     
00135     var_Get(p_intf, "position", &pos);
00136     bookmark.psz_name = _("Untitled");
00137     input_Control( p_input, INPUT_GET_BYTE_POSITION, &bookmark.i_byte_offset );
00138     var_Get( p_input, "time", &pos );
00139     bookmark.i_time_offset = pos.i_time;
00140     input_Control( p_input, INPUT_ADD_BOOKMARK, &bookmark );
00141     
00142     vlc_object_release( p_input );
00143     
00144     [o_tbl_dataTable reloadData];
00145 }
00146 
00147 - (IBAction)clear:(id)sender
00148 {
00149     /* clear table */
00150     intf_thread_t * p_intf = VLCIntf;
00151     input_thread_t *p_input = (input_thread_t *)vlc_object_find( p_intf, \
00152         VLC_OBJECT_INPUT, FIND_ANYWHERE );
00153     
00154     if( !p_input )
00155         return;
00156 
00157     input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
00158 
00159     vlc_object_release( p_input );
00160     
00161     [o_tbl_dataTable reloadData];
00162 }
00163 
00164 - (IBAction)edit:(id)sender
00165 {
00166     /* put values to the sheet's fields and show sheet */
00167     /* we take the values from the core and not the table, because we cannot
00168      * really trust it */
00169     intf_thread_t * p_intf = VLCIntf;
00170     input_thread_t * p_input = (input_thread_t *)vlc_object_find( p_intf, \
00171         VLC_OBJECT_INPUT, FIND_ANYWHERE );
00172     seekpoint_t **pp_bookmarks;
00173     int i_bookmarks;
00174     char * toBeReturned;
00175     toBeReturned = "";
00176     int i_toBeReturned;
00177     i_toBeReturned = 0;
00178     int row;
00179     row = [o_tbl_dataTable selectedRow];
00180     
00181     if( !p_input )
00182     {
00183         return;
00184     } 
00185     else if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks, \
00186         &i_bookmarks ) != VLC_SUCCESS )
00187     {
00188         vlc_object_release( p_input );
00189         return;
00190     } 
00191     else if(row < 0)
00192     {
00193         vlc_object_release( p_input );
00194         return;
00195     } else {
00196         [o_edit_fld_name setStringValue: [NSString stringWithUTF8String: \
00197             pp_bookmarks[row]->psz_name]];
00198         [o_edit_fld_time setStringValue: [[NSNumber numberWithInt: \
00199             (pp_bookmarks[row]->i_time_offset / 1000000)] stringValue]];
00200         [o_edit_fld_bytes setStringValue: [[NSNumber numberWithInt: \
00201             pp_bookmarks[row]->i_byte_offset] stringValue]];
00202     }
00203     
00204     p_old_input = p_input;
00205     vlc_object_release( p_input );
00206 
00207     [NSApp beginSheet: o_edit_window
00208         modalForWindow: o_bookmarks_window
00209         modalDelegate: o_edit_window
00210         didEndSelector: nil
00211         contextInfo: nil];
00212 }
00213 
00214 - (IBAction)edit_cancel:(id)sender
00215 {
00216     /* close sheet */
00217     [NSApp endSheet:o_edit_window];
00218     [o_edit_window close];
00219 }
00220 
00221 - (IBAction)edit_ok:(id)sender
00222 {
00223     /* save field contents and close sheet */
00224     
00225     intf_thread_t * p_intf = VLCIntf;
00226     seekpoint_t **pp_bookmarks;
00227     int i_bookmarks, i;
00228     input_thread_t *p_input = (input_thread_t *)vlc_object_find( p_intf, \
00229         VLC_OBJECT_INPUT, FIND_ANYWHERE );
00230     
00231     if( !p_input )
00232     {
00233         NSBeginCriticalAlertSheet(_NS("No input"), _NS("OK"), \
00234                 @"", @"", o_bookmarks_window, nil, nil, nil, nil, _NS("No " \
00235                 "input found. The stream must be playing or paused for " \
00236                 "bookmarks to work."));
00237         return;
00238     }
00239     if( p_old_input != p_input )
00240     {
00241         NSBeginCriticalAlertSheet(_NS("Input has changed"), _NS("OK"), \
00242             @"", @"", o_bookmarks_window, nil, nil, nil, nil, _NS("Input " \
00243             "has changed, unable to save bookmark. Use \"Pause\" while " \
00244             "editing bookmarks to keep the same input."));
00245         vlc_object_release( p_input );
00246         return;
00247     }
00248     
00249     if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks, \
00250         &i_bookmarks ) != VLC_SUCCESS )
00251     {
00252         vlc_object_release( p_input );
00253         return;
00254     } 
00255 
00256     i = [o_tbl_dataTable selectedRow];
00257     
00258     if( pp_bookmarks[i]->psz_name ) 
00259     {
00260         free( pp_bookmarks[i]->psz_name );
00261     }
00262 
00263     pp_bookmarks[i]->psz_name = strdup([[o_edit_fld_name stringValue] UTF8String]); 
00264     pp_bookmarks[i]->i_byte_offset = [[o_edit_fld_bytes stringValue] intValue];
00265     pp_bookmarks[i]->i_time_offset = ([[o_edit_fld_time stringValue] intValue]  * 1000000);
00266     
00267     if( input_Control( p_input, INPUT_CHANGE_BOOKMARK, pp_bookmarks[i], i ) \
00268         != VLC_SUCCESS )
00269     {
00270         msg_Warn( p_intf, "VLCBookmarks: changing bookmark failed");
00271         vlc_object_release( p_input );
00272         return;
00273     }
00274     
00275     [o_tbl_dataTable reloadData];
00276     vlc_object_release( p_input );
00277      
00278     
00279     [NSApp endSheet: o_edit_window];
00280     [o_edit_window close];
00281 }
00282 
00283 - (IBAction)extract:(id)sender
00284 {
00285     /* extract */
00286     
00287     intf_thread_t * p_intf = VLCIntf;
00288     
00289     if( [o_tbl_dataTable numberOfSelectedRows] < 2 )
00290     {
00291         NSBeginAlertSheet(_NS("Invalid selection"), _NS("OK"), \
00292             @"", @"", o_bookmarks_window, nil, nil, nil, nil, _NS("" \
00293             "You have to select two bookmarks."));
00294         return;
00295     }
00296     input_thread_t *p_input =
00297         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
00298                                            FIND_ANYWHERE );
00299     if( !p_input )
00300     {
00301         NSBeginCriticalAlertSheet(_NS("No input found"), _NS("OK"), \
00302             @"", @"", o_bookmarks_window, nil, nil, nil, nil, _NS("" \
00303             "The stream must be playing or paused for bookmarks to work."));
00304         return;
00305     }
00306     
00307     seekpoint_t **pp_bookmarks;
00308     int i_bookmarks ;
00309     int i_first = -1;
00310     int i_second = -1;
00311     int x = 0;
00312     int c = 0;
00313     while (c != 2)
00314     {
00315         if([o_tbl_dataTable isRowSelected:x])
00316         {
00317             if (i_first == -1)
00318             {
00319                 i_first = x;
00320                 c = 1;
00321             } 
00322             else if (i_second == -1)
00323             {
00324                 i_second = x;
00325                 c = 2;
00326             }
00327         }
00328         x = (x + 1);
00329     }
00330     
00331     msg_Dbg(p_intf, "got the bookmark-indexes");
00332     
00333     if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks, \
00334         &i_bookmarks ) != VLC_SUCCESS )
00335     {
00336         vlc_object_release( p_input );
00337         msg_Err(p_intf, "bookmarks couldn't be retrieved from core");
00338         return;
00339     }
00340     msg_Dbg(p_intf, "calling wizard");
00341 
00342     [[[VLCMain sharedInstance] getWizard] initWithExtractValuesFrom: \
00343             [[NSNumber numberWithInt: \
00344             (pp_bookmarks[i_first]->i_time_offset/1000000)] stringValue] \
00345             to: [[NSNumber numberWithInt: \
00346             (pp_bookmarks[i_second]->i_time_offset/1000000)] stringValue] \
00347             ofItem: [NSString stringWithUTF8String: \
00348             p_input->input.p_item->psz_uri]];
00349     vlc_object_release( p_input );
00350     msg_Dbg(p_intf, "released input");
00351 }
00352 
00353 - (IBAction)goToBookmark:(id)sender
00354 {
00355     intf_thread_t * p_intf = VLCIntf;
00356     input_thread_t *p_input =
00357     (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
00358     
00359     if( !p_input ) 
00360     {
00361         return;
00362     }
00363 
00364     input_Control( p_input, INPUT_SET_BOOKMARK, [o_tbl_dataTable selectedRow] );
00365 
00366     vlc_object_release( p_input );
00367 }
00368 
00369 - (IBAction)remove:(id)sender
00370 {
00371     /* remove selected item */
00372     intf_thread_t * p_intf = VLCIntf;
00373     input_thread_t *p_input =
00374     (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
00375     
00376     if( !p_input ) return;
00377 
00378     int i_focused = [o_tbl_dataTable selectedRow];
00379     if( i_focused >= 0 )
00380     {
00381         input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
00382     }
00383 
00384     vlc_object_release( p_input );
00385     
00386     [o_tbl_dataTable reloadData];
00387 }
00388 
00389 /*****************************************************************************
00390  * callback stuff
00391  *****************************************************************************/
00392 
00393 -(id)getDataTable
00394 {
00395     return o_tbl_dataTable;
00396 }
00397 
00398 /*****************************************************************************
00399  * data source methods
00400  *****************************************************************************/
00401 
00402 - (int)numberOfRowsInTableView:(NSTableView *)theDataTable
00403 {
00404     /* return the number of bookmarks */
00405     intf_thread_t * p_intf = VLCIntf;
00406     input_thread_t * p_input = (input_thread_t *)vlc_object_find( p_intf, \
00407         VLC_OBJECT_INPUT, FIND_ANYWHERE );
00408     seekpoint_t **pp_bookmarks;
00409     int i_bookmarks;
00410     
00411     if( !p_input )
00412     {
00413         return 0;
00414     }
00415     else if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks, \
00416                        &i_bookmarks ) != VLC_SUCCESS )
00417     {
00418         vlc_object_release( p_input );
00419         return 0;
00420     }
00421     else {
00422         vlc_object_release( p_input );
00423         return i_bookmarks;
00424     }
00425 }
00426 
00427 - (id)tableView:(NSTableView *)theDataTable objectValueForTableColumn: \
00428     (NSTableColumn *)theTableColumn row: (int)row
00429 {
00430     /* return the corresponding data as NSString */
00431     intf_thread_t * p_intf = VLCIntf;
00432     input_thread_t * p_input = (input_thread_t *)vlc_object_find( p_intf, \
00433         VLC_OBJECT_INPUT, FIND_ANYWHERE );
00434     seekpoint_t **pp_bookmarks;
00435     int i_bookmarks;
00436     char * toBeReturned;
00437     toBeReturned = "";
00438     int i_toBeReturned;
00439     i_toBeReturned = 0;
00440     
00441     if( !p_input )
00442     {
00443         return @"";
00444     } 
00445     else if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks, \
00446                        &i_bookmarks ) != VLC_SUCCESS )
00447     {
00448         vlc_object_release( p_input );
00449         return @"";
00450     }
00451     else
00452     {
00453         if ([[theTableColumn identifier] isEqualToString: @"description"])
00454         {
00455             toBeReturned = pp_bookmarks[row]->psz_name;
00456             vlc_object_release( p_input );
00457             return [NSString stringWithUTF8String: toBeReturned];
00458         } 
00459         else if ([[theTableColumn identifier] isEqualToString: @"size_offset"])
00460         {
00461             i_toBeReturned = pp_bookmarks[row]->i_byte_offset;
00462             vlc_object_release( p_input );
00463             return [[NSNumber numberWithInt: i_toBeReturned] stringValue];
00464         }
00465         else if ([[theTableColumn identifier] isEqualToString: @"time_offset"])
00466         {
00467             i_toBeReturned = pp_bookmarks[row]->i_time_offset;
00468             vlc_object_release( p_input );
00469             return [[NSNumber numberWithInt: (i_toBeReturned / 1000000)] \
00470                 stringValue];
00471         }
00472         else
00473         {
00474             /* may not happen, but just in case */
00475             vlc_object_release( p_input );
00476             msg_Err(p_intf, "VLCBookmarks: unknown table column identifier " \
00477                 "(%s) while updating table", [[theTableColumn identifier] \
00478                 UTF8String] );
00479             return @"unknown identifier";
00480         }
00481     }
00482 
00483 }
00484 
00485 /*****************************************************************************
00486  * delegate methods
00487  *****************************************************************************/
00488 
00489 - (void)tableViewSelectionDidChange:(NSNotification *)aNotification
00490 {
00491     /* check whether a row is selected and en-/disable the edit/remove buttons */
00492     if ([o_tbl_dataTable selectedRow] == -1)
00493     {
00494         /* no row is selected */
00495         [o_btn_edit setEnabled: NO];
00496         [o_btn_rm setEnabled: NO];
00497         [o_btn_extract setEnabled: NO];
00498     }
00499     else
00500     {
00501         /* a row is selected */
00502         [o_btn_edit setEnabled: YES];
00503         [o_btn_rm setEnabled: YES];
00504         if ([o_tbl_dataTable numberOfSelectedRows] == 2)
00505         {
00506             [o_btn_extract setEnabled: YES];
00507         }
00508     }
00509 }
00510 
00511 @end

Generated on Tue Dec 20 10:14:38 2005 for vlc-0.8.4a by  doxygen 1.4.2