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

var_text.cpp

00001 /*****************************************************************************
00002  * var_text.cpp
00003  *****************************************************************************
00004  * Copyright (C) 2003 the VideoLAN team
00005  * $Id: var_text.cpp 11664 2005-07-09 06:17:09Z courmisch $
00006  *
00007  * Authors: Cyril Deguet     <[email protected]>
00008  *          Olivier Teulière <[email protected]>
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00023  *****************************************************************************/
00024 
00025 #include "var_text.hpp"
00026 #include "../src/vlcproc.hpp"
00027 #include "../src/var_manager.hpp"
00028 #include "../vars/time.hpp"
00029 #include "../vars/volume.hpp"
00030 
00031 
00032 const string VarText::m_type = "text";
00033 
00034 
00035 VarText::VarText( intf_thread_t *pIntf, bool substVars ): Variable( pIntf ),
00036     m_text( pIntf, "" ), m_lastText( pIntf, "" ), m_substVars( substVars )
00037 {
00038 }
00039 
00040 
00041 VarText::~VarText()
00042 {
00043     if( m_substVars )
00044     {
00045         // Remove the observers
00046         VlcProc *pVlcProc = VlcProc::instance( getIntf() );
00047         pVlcProc->getTimeVar().delObserver( this );
00048         pVlcProc->getVolumeVar().delObserver( this );
00049         pVlcProc->getStreamURIVar().delObserver( this );
00050         pVlcProc->getStreamNameVar().delObserver( this );
00051         VarManager *pVarManager = VarManager::instance( getIntf() );
00052         pVarManager->getHelpText().delObserver( this );
00053     }
00054 }
00055 
00056 
00057 const UString VarText::get() const
00058 {
00059     if( !m_substVars )
00060     {
00061         // Do not substitute "$X" variables
00062         return m_text;
00063     }
00064 
00065     uint32_t pos;
00066     VlcProc *pVlcProc = VlcProc::instance( getIntf() );
00067 
00068     // Fill a temporary UString object, and replace the escape characters
00069     // ($H for help, $T for current time, $L for time left, $D for duration,
00070     // $V for volume)
00071     UString temp( m_text );
00072 
00073     // $H is processed first, in case the help string contains other variables
00074     // to replace. And it is replaced only once, in case one of these other
00075     // variables is $H...
00076     if( (pos = temp.find( "$H" )) != UString::npos )
00077     {
00078         VarManager *pVarManager = VarManager::instance( getIntf() );
00079         temp.replace( pos, 2, pVarManager->getHelpText().get() );
00080     }
00081     while( (pos = temp.find( "$T" )) != UString::npos )
00082     {
00083         temp.replace( pos, 2,
00084                       pVlcProc->getTimeVar().getAsStringCurrTime().c_str() );
00085     }
00086     while( (pos = temp.find( "$L" )) != UString::npos )
00087     {
00088         temp.replace( pos, 2,
00089                       pVlcProc->getTimeVar().getAsStringTimeLeft().c_str() );
00090     }
00091     while( (pos = temp.find( "$D" )) != UString::npos )
00092     {
00093         temp.replace( pos, 2,
00094                       pVlcProc->getTimeVar().getAsStringDuration().c_str() );
00095     }
00096     while( (pos = temp.find( "$V" )) != UString::npos )
00097     {
00098         temp.replace( pos, 2,
00099                       pVlcProc->getVolumeVar().getAsStringPercent().c_str() );
00100     }
00101     while( (pos = temp.find( "$N" )) != UString::npos )
00102     {
00103         temp.replace( pos, 2, pVlcProc->getStreamNameVar().get() );
00104     }
00105     while( (pos = temp.find( "$F" )) != UString::npos )
00106     {
00107         temp.replace( pos, 2, pVlcProc->getStreamURIVar().get() );
00108     }
00109 
00110     return temp;
00111 }
00112 
00113 
00114 void VarText::set( const UString &rText )
00115 {
00116     // Avoid an infinite loop
00117     if( rText == m_text )
00118     {
00119         return;
00120     }
00121 
00122     m_text = rText;
00123 
00124     if( m_substVars )
00125     {
00126         // Stop observing other variables
00127         VlcProc *pVlcProc = VlcProc::instance( getIntf() );
00128         pVlcProc->getTimeVar().delObserver( this );
00129         pVlcProc->getVolumeVar().delObserver( this );
00130         pVlcProc->getStreamNameVar().delObserver( this );
00131         pVlcProc->getStreamURIVar().delObserver( this );
00132         VarManager *pVarManager = VarManager::instance( getIntf() );
00133         pVarManager->getHelpText().delObserver( this );
00134 
00135         // Observe needed variables
00136         if( m_text.find( "$H" ) != UString::npos )
00137         {
00138             pVarManager->getHelpText().addObserver( this );
00139         }
00140         if( m_text.find( "$T" ) != UString::npos )
00141         {
00142             pVlcProc->getTimeVar().addObserver( this );
00143         }
00144         if( m_text.find( "$L" ) != UString::npos )
00145         {
00146             pVlcProc->getTimeVar().addObserver( this );
00147         }
00148         if( m_text.find( "$D" ) != UString::npos )
00149         {
00150             pVlcProc->getTimeVar().addObserver( this );
00151         }
00152         if( m_text.find( "$V" ) != UString::npos )
00153         {
00154             pVlcProc->getVolumeVar().addObserver( this );
00155         }
00156         if( m_text.find( "$N" ) != UString::npos )
00157         {
00158             pVlcProc->getStreamNameVar().addObserver( this );
00159         }
00160         if( m_text.find( "$F" ) != UString::npos )
00161         {
00162             pVlcProc->getStreamURIVar().addObserver( this );
00163         }
00164     }
00165 
00166     notify();
00167 }
00168 
00169 
00170 void VarText::onUpdate( Subject<VarPercent> &rVariable )
00171 {
00172     UString newText = get();
00173     // If the text has changed, notify the observers
00174     if( newText != m_lastText )
00175     {
00176         m_lastText = newText;
00177         notify();
00178     }
00179 }
00180 
00181 
00182 void VarText::onUpdate( Subject<VarText> &rVariable )
00183 {
00184     UString newText = get();
00185     // If the text has changed, notify the observers
00186     if( newText != m_lastText )
00187     {
00188         m_lastText = newText;
00189         notify();
00190     }
00191 }

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