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