]> git.sesse.net Git - vlc/blob - modules/codec/cmml/history.c
de4e8e2c76db7d3d769dde1f64350cb9f507a687
[vlc] / modules / codec / cmml / history.c
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
7  *
8  * $Id$
9  *
10  * Authors: Andre Pang <Andre.Pang@csiro.au>
11  *
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.
16  *
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.
21  *
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  *****************************************************************************/
26
27 #include <vlc/vlc.h>
28 #include <vlc_input.h>
29
30 #include "history.h"
31
32 #include "xarray.h"
33
34 #ifdef HAVE_STDLIB_H
35 #   include <stdlib.h>                                          /* realloc() */
36 #endif
37
38 #undef HISTORY_DEBUG
39
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 static void history_Dump( history_t *p_history );
44
45 /*****************************************************************************
46  * Local structure lock
47  *****************************************************************************/
48
49 /*****************************************************************************
50  * Actual history code
51  *****************************************************************************/
52
53 history_t *history_New( void )
54 {
55    history_t *p_new_history;
56    
57    p_new_history = calloc( 1, sizeof( struct history_t ) );
58    if( p_new_history == NULL ) return NULL;
59
60    p_new_history->p_xarray = xarray_New( 0 );
61    if( p_new_history->p_xarray == NULL )
62    {
63        free( p_new_history );
64        return NULL;
65    }
66
67 #ifndef HISTORY_DEBUG
68    /* make dummy reference to history_Dump to avoid compiler warnings */
69    while (0)
70    {
71        void *p_tmp;
72
73        p_tmp = history_Dump;
74    }
75 #endif
76
77    return p_new_history;
78 }
79
80 vlc_bool_t history_GoBackSavingCurrentItem ( history_t *p_history,
81                                              history_item_t *p_item )
82 {
83     history_PruneAndInsert( p_history, p_item );
84
85     /* PruneAndInsert will increment the index, so we need to go
86      * back one position to reset the index to the place we were at
87      * before saving the current state, and then go back one more to
88      * actually go back */
89     p_history->i_index -= 2;
90
91 #ifdef HISTORY_DEBUG
92     history_Dump( p_history );
93 #endif
94     return VLC_TRUE;
95 }
96
97 static void history_Dump( history_t *p_history )
98 {
99     unsigned int i_count;
100     int i;
101
102     if( xarray_Count( p_history->p_xarray, &i_count ) != XARRAY_SUCCESS )
103         return;
104
105     for (i = 0; i < (int) i_count; i++)
106     {
107         history_item_t *p_item;
108         void *pv_item;
109
110         xarray_ObjectAtIndex( p_history->p_xarray, i, &pv_item );
111
112         p_item = (history_item_t *) pv_item;
113
114         if( p_item == NULL )
115             fprintf( stderr, "HISTORY: [%d] NULL\n", i );
116         else
117         {
118             char *psz_uri = input_item_GetURI( p_item );
119             fprintf( stderr, "HISTORY: [%d] %p (%p->%s)\n", i, p_item,
120                      psz_uri, psz_uri );
121             free( psz_uri );
122         }
123     }
124 }
125
126 vlc_bool_t history_GoForwardSavingCurrentItem ( history_t *p_history,
127                                                 history_item_t *p_item )
128 {
129 #ifdef HISTORY_DEBUG
130     history_Dump( p_history );
131 #endif
132
133     if( xarray_ReplaceObject( p_history->p_xarray, p_history->i_index, p_item )
134         == XARRAY_SUCCESS )
135     {
136         p_history->i_index++;
137         return VLC_TRUE;
138     }
139     else
140     {
141         return VLC_FALSE;
142     }
143 }
144
145 vlc_bool_t history_CanGoBack( history_t *p_history )
146 {
147     if( p_history->i_index > 0 )
148         return VLC_TRUE;
149     else
150         return VLC_FALSE;
151 }
152
153 vlc_bool_t history_CanGoForward( history_t *p_history )
154 {
155     unsigned int i_count;
156
157     if( xarray_Count( p_history->p_xarray, &i_count ) != XARRAY_SUCCESS )
158         return VLC_FALSE;
159
160     if( p_history->i_index < i_count )
161         return VLC_TRUE;
162     else
163         return VLC_FALSE;
164 }
165
166 history_item_t *history_Item( history_t *p_history )
167 {
168     history_item_t *p_item;
169     void *pv_item;
170
171     if( xarray_ObjectAtIndex( p_history->p_xarray, p_history->i_index,
172                               &pv_item )
173         == XARRAY_SUCCESS )
174     {
175         p_item = (history_item_t *) pv_item;
176         return p_item;
177     }
178     else
179     {
180         return NULL;
181     }
182 }
183
184 void history_Prune( history_t *p_history )
185 {
186     xarray_RemoveObjectsAfter( p_history->p_xarray, p_history->i_index );
187     xarray_RemoveObject( p_history->p_xarray, p_history->i_index );
188 }
189
190 void history_PruneAndInsert( history_t *p_history, history_item_t *p_item )
191 {
192     unsigned int i_count;
193
194     xarray_Count( p_history->p_xarray, &i_count );
195
196     if( i_count == 0 )
197     {
198         xarray_InsertObject( p_history->p_xarray, p_item, 0 );
199         p_history->i_index = 1;
200     }
201     else
202     {
203         history_Prune( p_history );
204         xarray_InsertObject( p_history->p_xarray, p_item, p_history->i_index );
205         p_history->i_index++;
206     }
207 }
208
209 unsigned int history_Count( history_t *p_history )
210 {
211     unsigned int i_count;
212     xarray_Count( p_history->p_xarray, &i_count );
213     return i_count;
214 }
215
216 unsigned int history_Index( history_t *p_history )
217 {
218     return p_history->i_index;
219 }
220
221 history_item_t * historyItem_New( char *psz_name, char *psz_uri )
222 {
223     history_item_t *p_history_item = NULL;
224
225     p_history_item = (history_item_t *) malloc( sizeof(history_item_t) );
226     if( !p_history_item ) return NULL;
227
228     p_history_item->psz_uri = strdup( psz_uri );
229     p_history_item->psz_name = strdup( psz_name );
230
231     return p_history_item;
232 }
233