Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

history.c

00001 /*****************************************************************************
00002  * history.c: vlc_history_t (web-browser-like back/forward history) handling
00003  *****************************************************************************
00004  * Copyright (C) 2004 Commonwealth Scientific and Industrial Research
00005  *                    Organisation (CSIRO) Australia
00006  * Copyright (C) 2004 the VideoLAN team
00007  *
00008  * $Id: history.c 11664 2005-07-09 06:17:09Z courmisch $
00009  *
00010  * Authors: Andre Pang <[email protected]>
00011  *
00012  * This program is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation; either version 2 of the License, or
00015  * (at your option) any later version.
00016  *
00017  * This program is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License
00023  * along with this program; if not, write to the Free Software
00024  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
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>                                          /* realloc() */
00035 #endif
00036 
00037 #undef HISTORY_DEBUG
00038 
00039 /*****************************************************************************
00040  * Local prototypes
00041  *****************************************************************************/
00042 static void history_Dump( history_t *p_history );
00043 
00044 /*****************************************************************************
00045  * Local structure lock
00046  *****************************************************************************/
00047 
00048 /*****************************************************************************
00049  * Actual history code
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    /* make dummy reference to history_Dump to avoid compiler warnings */
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     /* PruneAndInsert will increment the index, so we need to go
00085      * back one position to reset the index to the place we were at
00086      * before saving the current state, and then go back one more to
00087      * actually go back */
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 

Generated on Tue Dec 20 10:14:28 2005 for vlc-0.8.4a by  doxygen 1.4.2