1 /*****************************************************************************
2 * history.c: vlc_history_t (web-browser-like back/forward history) handling
3 *****************************************************************************
4 * Copyright (C) 2004 Commonwealth Scientific and Industrial Research
5 * Organisation (CSIRO) Australia
6 * Copyright (C) 2004 the VideoLAN team
10 * Authors: Andre Pang <Andre.Pang@csiro.au>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_input.h>
39 # include <stdlib.h> /* realloc() */
44 /*****************************************************************************
46 *****************************************************************************/
48 static void history_Dump( history_t *p_history );
51 /*****************************************************************************
52 * Local structure lock
53 *****************************************************************************/
55 /*****************************************************************************
57 *****************************************************************************/
59 history_t *history_New( void )
61 history_t *p_new_history;
63 p_new_history = calloc( 1, sizeof( struct history_t ) );
64 if( p_new_history == NULL ) return NULL;
66 p_new_history->p_xarray = xarray_New( 0 );
67 if( p_new_history->p_xarray == NULL )
69 free( p_new_history );
76 bool history_GoBackSavingCurrentItem ( history_t *p_history,
77 history_item_t *p_item )
79 history_PruneAndInsert( p_history, p_item );
81 /* PruneAndInsert will increment the index, so we need to go
82 * back one position to reset the index to the place we were at
83 * before saving the current state, and then go back one more to
85 p_history->i_index -= 2;
88 history_Dump( p_history );
94 static void history_Dump( history_t *p_history )
99 if( xarray_Count( p_history->p_xarray, &i_count ) != XARRAY_SUCCESS )
102 for (i = 0; i < (int) i_count; i++)
104 history_item_t *p_item;
107 xarray_ObjectAtIndex( p_history->p_xarray, i, &pv_item );
109 p_item = (history_item_t *) pv_item;
112 fprintf( stderr, "HISTORY: [%d] NULL\n", i );
115 fprintf( stderr, "HISTORY: [%d] %p (%p->%s)\n", i, p_item,
116 p_item->psz_uri, p_item->psz_uri );
122 bool history_GoForwardSavingCurrentItem ( history_t *p_history,
123 history_item_t *p_item )
126 history_Dump( p_history );
129 if( xarray_ReplaceObject( p_history->p_xarray, p_history->i_index, p_item )
132 p_history->i_index++;
141 bool history_CanGoBack( history_t *p_history )
143 if( p_history->i_index > 0 )
149 bool history_CanGoForward( history_t *p_history )
151 unsigned int i_count;
153 if( xarray_Count( p_history->p_xarray, &i_count ) != XARRAY_SUCCESS )
156 if( p_history->i_index < i_count )
162 history_item_t *history_Item( history_t *p_history )
164 history_item_t *p_item;
167 if( xarray_ObjectAtIndex( p_history->p_xarray, p_history->i_index,
171 p_item = (history_item_t *) pv_item;
180 void history_Prune( history_t *p_history )
182 xarray_RemoveObjectsAfter( p_history->p_xarray, p_history->i_index );
183 xarray_RemoveObject( p_history->p_xarray, p_history->i_index );
186 void history_PruneAndInsert( history_t *p_history, history_item_t *p_item )
188 unsigned int i_count;
190 xarray_Count( p_history->p_xarray, &i_count );
194 xarray_InsertObject( p_history->p_xarray, p_item, 0 );
195 p_history->i_index = 1;
199 history_Prune( p_history );
200 xarray_InsertObject( p_history->p_xarray, p_item, p_history->i_index );
201 p_history->i_index++;
205 unsigned int history_Count( history_t *p_history )
207 unsigned int i_count;
208 xarray_Count( p_history->p_xarray, &i_count );
212 unsigned int history_Index( history_t *p_history )
214 return p_history->i_index;
217 history_item_t * historyItem_New( char *psz_name, char *psz_uri )
219 history_item_t *p_history_item = NULL;
221 p_history_item = (history_item_t *) malloc( sizeof(history_item_t) );
222 if( !p_history_item ) return NULL;
224 p_history_item->psz_uri = strdup( psz_uri );
225 p_history_item->psz_name = strdup( psz_name );
227 return p_history_item;