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 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
34 # include <stdlib.h> /* realloc() */
39 /*****************************************************************************
41 *****************************************************************************/
42 static void history_Dump( history_t *p_history );
44 /*****************************************************************************
45 * Local structure lock
46 *****************************************************************************/
48 /*****************************************************************************
50 *****************************************************************************/
52 history_t *history_New()
54 history_t *p_new_history;
56 p_new_history = calloc( 1, sizeof( struct history_t ) );
57 if( p_new_history == NULL ) return NULL;
59 p_new_history->p_xarray = xarray_New( 0 );
60 if( p_new_history->p_xarray == NULL )
62 free( p_new_history );
67 /* make dummy reference to history_Dump to avoid compiler warnings */
79 vlc_bool_t history_GoBackSavingCurrentItem ( history_t *p_history,
80 history_item_t *p_item )
82 history_PruneAndInsert( p_history, p_item );
84 /* PruneAndInsert will increment the index, so we need to go
85 * back one position to reset the index to the place we were at
86 * before saving the current state, and then go back one more to
88 p_history->i_index -= 2;
91 history_Dump( p_history );
96 static void history_Dump( history_t *p_history )
101 if( xarray_Count( p_history->p_xarray, &i_count ) != XARRAY_SUCCESS )
104 for (i = 0; i < (int) i_count; i++)
106 history_item_t *p_item;
109 xarray_ObjectAtIndex( p_history->p_xarray, i, &pv_item );
111 p_item = (history_item_t *) pv_item;
114 fprintf( stderr, "HISTORY: [%d] NULL\n", i );
116 fprintf( stderr, "HISTORY: [%d] %p (%p->%s)\n", i, p_item,
117 p_item->psz_uri, p_item->psz_uri );
121 vlc_bool_t history_GoForwardSavingCurrentItem ( history_t *p_history,
122 history_item_t *p_item )
125 history_Dump( p_history );
128 if( xarray_ReplaceObject( p_history->p_xarray, p_history->i_index, p_item )
131 p_history->i_index++;
140 vlc_bool_t history_CanGoBack( history_t *p_history )
142 if( p_history->i_index > 0 )
148 vlc_bool_t history_CanGoForward( history_t *p_history )
150 unsigned int i_count;
152 if( xarray_Count( p_history->p_xarray, &i_count ) != XARRAY_SUCCESS )
155 if( p_history->i_index < i_count )
161 history_item_t *history_Item( history_t *p_history )
163 history_item_t *p_item;
166 if( xarray_ObjectAtIndex( p_history->p_xarray, p_history->i_index,
170 p_item = (history_item_t *) pv_item;
179 void history_Prune( history_t *p_history )
181 xarray_RemoveObjectsAfter( p_history->p_xarray, p_history->i_index );
182 xarray_RemoveObject( p_history->p_xarray, p_history->i_index );
185 void history_PruneAndInsert( history_t *p_history, history_item_t *p_item )
187 unsigned int i_count;
189 xarray_Count( p_history->p_xarray, &i_count );
193 xarray_InsertObject( p_history->p_xarray, p_item, 0 );
194 p_history->i_index = 1;
198 history_Prune( p_history );
199 xarray_InsertObject( p_history->p_xarray, p_item, p_history->i_index );
200 p_history->i_index++;
204 unsigned int history_Count( history_t *p_history )
207 xarray_Count( p_history->p_xarray, &i_count );
211 unsigned int history_Index( history_t *p_history )
213 return p_history->i_index;
216 history_item_t * historyItem_New( char *psz_name, char *psz_uri )
218 history_item_t *p_history_item = NULL;
220 p_history_item = (history_item_t *) malloc( sizeof(history_item_t) );
221 if( !p_history_item ) return NULL;
223 p_history_item->psz_uri = strdup( psz_uri );
224 p_history_item->psz_name = strdup( psz_name );
226 return p_history_item;