1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 #include "var_text.hpp"
26 #include "../src/vlcproc.hpp"
27 #include "../src/var_manager.hpp"
28 #include "../vars/time.hpp"
29 #include "../vars/volume.hpp"
30 #include "../vars/stream.hpp"
33 const string VarText::m_type = "text";
36 VarText::VarText( intf_thread_t *pIntf ): Variable( pIntf ),
37 m_text( pIntf, "" ), m_lastText( pIntf, "" )
44 // Remove the observers
45 VlcProc *pVlcProc = VlcProc::instance( getIntf() );
46 pVlcProc->getTimeVar().delObserver( this );
47 pVlcProc->getVolumeVar().delObserver( this );
48 pVlcProc->getStreamVar().delObserver( this );
49 VarManager *pVarManager = VarManager::instance( getIntf() );
50 pVarManager->getHelpText().delObserver( this );
54 const UString VarText::get() const
57 VlcProc *pVlcProc = VlcProc::instance( getIntf() );
59 // Fill a temporary UString object, and replace the escape characters
60 // ($H for help, $T for current time, $L for time left, $D for duration,
62 UString temp( m_text );
64 while( (pos = temp.find( "$H" )) != UString::npos )
66 VarManager *pVarManager = VarManager::instance( getIntf() );
67 // We use .getRaw() to avoid replacing the $H recursively!
68 temp.replace( pos, 2, pVarManager->getHelpText().getRaw() );
70 while( (pos = temp.find( "$T" )) != UString::npos )
73 pVlcProc->getTimeVar().getAsStringCurrTime().c_str() );
75 while( (pos = temp.find( "$L" )) != UString::npos )
78 pVlcProc->getTimeVar().getAsStringTimeLeft().c_str() );
80 while( (pos = temp.find( "$D" )) != UString::npos )
83 pVlcProc->getTimeVar().getAsStringDuration().c_str() );
85 while( (pos = temp.find( "$V" )) != UString::npos )
88 pVlcProc->getVolumeVar().getAsStringPercent().c_str() );
90 while( (pos = temp.find( "$N" )) != UString::npos )
93 pVlcProc->getStreamVar().getAsStringName().c_str() );
95 while( (pos = temp.find( "$F" )) != UString::npos )
98 pVlcProc->getStreamVar().getAsStringFullName().c_str() );
105 void VarText::set( const UString &rText )
107 // Avoid an infinite loop
108 if( rText == m_text )
113 // Stop observing other variables
114 VlcProc *pVlcProc = VlcProc::instance( getIntf() );
115 pVlcProc->getTimeVar().delObserver( this );
116 pVlcProc->getVolumeVar().delObserver( this );
117 pVlcProc->getStreamVar().delObserver( this );
118 VarManager *pVarManager = VarManager::instance( getIntf() );
119 pVarManager->getHelpText().delObserver( this );
123 // Observe needed variables
124 if( m_text.find( "$H" ) != UString::npos )
126 pVarManager->getHelpText().addObserver( this );
128 if( m_text.find( "$T" ) != UString::npos )
130 pVlcProc->getTimeVar().addObserver( this );
132 if( m_text.find( "$L" ) != UString::npos )
134 pVlcProc->getTimeVar().addObserver( this );
136 if( m_text.find( "$D" ) != UString::npos )
138 pVlcProc->getTimeVar().addObserver( this );
140 if( m_text.find( "$V" ) != UString::npos )
142 pVlcProc->getVolumeVar().addObserver( this );
144 if( m_text.find( "$N" ) != UString::npos )
146 pVlcProc->getStreamVar().addObserver( this );
148 if( m_text.find( "$F" ) != UString::npos )
150 pVlcProc->getStreamVar().addObserver( this );
157 void VarText::onUpdate( Subject<VarPercent> &rVariable )
159 UString newText = get();
160 // If the text has changed, notify the observers
161 if( newText != m_lastText )
163 m_lastText = newText;
169 void VarText::onUpdate( Subject<VarText> &rVariable )
171 UString newText = get();
172 // If the text has changed, notify the observers
173 if( newText != m_lastText )
175 m_lastText = newText;