00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <vlc/vlc.h>
00028
00029 #include "history.h"
00030
00031 #include "xarray.h"
00032
00033 #ifdef HAVE_STDLIB_H
00034 # include <stdlib.h>
00035 #endif
00036
00037 #undef HISTORY_DEBUG
00038
00039
00040
00041
00042 static void history_Dump( history_t *p_history );
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 history_t *history_New()
00053 {
00054 history_t *p_new_history;
00055
00056 p_new_history = calloc( 1, sizeof( struct history_t ) );
00057 if( p_new_history == NULL ) return NULL;
00058
00059 p_new_history->p_xarray = xarray_New( 0 );
00060 if( p_new_history->p_xarray == NULL )
00061 {
00062 free( p_new_history );
00063 return NULL;
00064 }
00065
00066 #ifndef HISTORY_DEBUG
00067
00068 while (0)
00069 {
00070 void *p_tmp;
00071
00072 p_tmp = history_Dump;
00073 }
00074 #endif
00075
00076 return p_new_history;
00077 }
00078
00079 vlc_bool_t history_GoBackSavingCurrentItem ( history_t *p_history,
00080 history_item_t *p_item )
00081 {
00082 history_PruneAndInsert( p_history, p_item );
00083
00084
00085
00086
00087
00088 p_history->i_index -= 2;
00089
00090 #ifdef HISTORY_DEBUG
00091 history_Dump( p_history );
00092 #endif
00093 return VLC_TRUE;
00094 }
00095
00096 static void history_Dump( history_t *p_history )
00097 {
00098 unsigned int i_count;
00099 int i;
00100
00101 if( xarray_Count( p_history->p_xarray, &i_count ) != XARRAY_SUCCESS )
00102 return;
00103
00104 for (i = 0; i < (int) i_count; i++)
00105 {
00106 history_item_t *p_item;
00107 void *pv_item;
00108
00109 xarray_ObjectAtIndex( p_history->p_xarray, i, &pv_item );
00110
00111 p_item = (history_item_t *) pv_item;
00112
00113 if( p_item == NULL )
00114 fprintf( stderr, "HISTORY: [%d] NULL\n", i );
00115 else
00116 fprintf( stderr, "HISTORY: [%d] %p (%p->%s)\n", i, p_item,
00117 p_item->psz_uri, p_item->psz_uri );
00118 }
00119 }
00120
00121 vlc_bool_t history_GoForwardSavingCurrentItem ( history_t *p_history,
00122 history_item_t *p_item )
00123 {
00124 #ifdef HISTORY_DEBUG
00125 history_Dump( p_history );
00126 #endif
00127
00128 if( xarray_ReplaceObject( p_history->p_xarray, p_history->i_index, p_item )
00129 == XARRAY_SUCCESS )
00130 {
00131 p_history->i_index++;
00132 return VLC_TRUE;
00133 }
00134 else
00135 {
00136 return VLC_FALSE;
00137 }
00138 }
00139
00140 vlc_bool_t history_CanGoBack( history_t *p_history )
00141 {
00142 if( p_history->i_index > 0 )
00143 return VLC_TRUE;
00144 else
00145 return VLC_FALSE;
00146 }
00147
00148 vlc_bool_t history_CanGoForward( history_t *p_history )
00149 {
00150 unsigned int i_count;
00151
00152 if( xarray_Count( p_history->p_xarray, &i_count ) != XARRAY_SUCCESS )
00153 return VLC_FALSE;
00154
00155 if( p_history->i_index < i_count )
00156 return VLC_TRUE;
00157 else
00158 return VLC_FALSE;
00159 }
00160
00161 history_item_t *history_Item( history_t *p_history )
00162 {
00163 history_item_t *p_item;
00164 void *pv_item;
00165
00166 if( xarray_ObjectAtIndex( p_history->p_xarray, p_history->i_index,
00167 &pv_item )
00168 == XARRAY_SUCCESS )
00169 {
00170 p_item = (history_item_t *) pv_item;
00171 return p_item;
00172 }
00173 else
00174 {
00175 return NULL;
00176 }
00177 }
00178
00179 void history_Prune( history_t *p_history )
00180 {
00181 xarray_RemoveObjectsAfter( p_history->p_xarray, p_history->i_index );
00182 xarray_RemoveObject( p_history->p_xarray, p_history->i_index );
00183 }
00184
00185 void history_PruneAndInsert( history_t *p_history, history_item_t *p_item )
00186 {
00187 unsigned int i_count;
00188
00189 xarray_Count( p_history->p_xarray, &i_count );
00190
00191 if( i_count == 0 )
00192 {
00193 xarray_InsertObject( p_history->p_xarray, p_item, 0 );
00194 p_history->i_index = 1;
00195 }
00196 else
00197 {
00198 history_Prune( p_history );
00199 xarray_InsertObject( p_history->p_xarray, p_item, p_history->i_index );
00200 p_history->i_index++;
00201 }
00202 }
00203
00204 unsigned int history_Count( history_t *p_history )
00205 {
00206 int i_count;
00207 xarray_Count( p_history->p_xarray, &i_count );
00208 return i_count;
00209 }
00210
00211 unsigned int history_Index( history_t *p_history )
00212 {
00213 return p_history->i_index;
00214 }
00215
00216 history_item_t * historyItem_New( char *psz_name, char *psz_uri )
00217 {
00218 history_item_t *p_history_item = NULL;
00219
00220 p_history_item = (history_item_t *) malloc( sizeof(history_item_t) );
00221 if( !p_history_item ) return NULL;
00222
00223 p_history_item->psz_uri = strdup( psz_uri );
00224 p_history_item->psz_name = strdup( psz_name );
00225
00226 return p_history_item;
00227 }
00228