1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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"
32 const string VarText::m_type = "text";
35 VarText::VarText( intf_thread_t *pIntf, bool substVars ): Variable( pIntf ),
36 m_text( pIntf, "" ), m_lastText( pIntf, "" ), m_substVars( substVars )
45 // Remove the observers
51 const UString VarText::get() const
55 // Do not substitute "$X" variables
60 VlcProc *pVlcProc = VlcProc::instance( getIntf() );
62 // Fill a temporary UString object, and replace the escape characters
63 // ($H for help, $T for current time, $L for time left, $D for duration,
65 UString temp( m_text );
67 // $H is processed first, in case the help string contains other variables
68 // to replace. And it is replaced only once, in case one of these other
70 if( (pos = temp.find( "$H" )) != UString::npos )
72 VarManager *pVarManager = VarManager::instance( getIntf() );
73 temp.replace( pos, 2, pVarManager->getHelpText().get() );
75 while( (pos = temp.find( "$T" )) != UString::npos )
78 pVlcProc->getTimeVar().getAsStringCurrTime().c_str() );
80 while( (pos = temp.find( "$t" )) != UString::npos )
83 pVlcProc->getTimeVar().getAsStringCurrTime(true).c_str() );
85 while( (pos = temp.find( "$L" )) != UString::npos )
88 pVlcProc->getTimeVar().getAsStringTimeLeft().c_str() );
90 while( (pos = temp.find( "$l" )) != UString::npos )
93 pVlcProc->getTimeVar().getAsStringTimeLeft(true).c_str() );
95 while( (pos = temp.find( "$D" )) != UString::npos )
98 pVlcProc->getTimeVar().getAsStringDuration().c_str() );
100 while( (pos = temp.find( "$d" )) != UString::npos )
102 temp.replace( pos, 2,
103 pVlcProc->getTimeVar().getAsStringDuration(true).c_str() );
105 while( (pos = temp.find( "$V" )) != UString::npos )
107 temp.replace( pos, 2,
108 pVlcProc->getVolumeVar().getAsStringPercent().c_str() );
110 while( (pos = temp.find( "$N" )) != UString::npos )
112 temp.replace( pos, 2, pVlcProc->getStreamNameVar().get() );
114 while( (pos = temp.find( "$F" )) != UString::npos )
116 temp.replace( pos, 2, pVlcProc->getStreamURIVar().get() );
118 while( (pos = temp.find( "$B" )) != UString::npos )
120 temp.replace( pos, 2, pVlcProc->getStreamBitRateVar().get() );
122 while( (pos = temp.find( "$S" )) != UString::npos )
124 temp.replace( pos, 2, pVlcProc->getStreamSampleRateVar().get() );
131 void VarText::set( const UString &rText )
133 // Avoid an infinite loop
134 if( rText == m_text )
143 // Stop observing other variables
146 VlcProc *pVlcProc = VlcProc::instance( getIntf() );
147 VarManager *pVarManager = VarManager::instance( getIntf() );
149 // Observe needed variables
150 if( m_text.find( "$H" ) != UString::npos )
152 pVarManager->getHelpText().addObserver( this );
154 if( m_text.find( "$T" ) != UString::npos ||
155 m_text.find( "$t" ) != UString::npos )
157 pVlcProc->getTimeVar().addObserver( this );
159 if( m_text.find( "$L" ) != UString::npos ||
160 m_text.find( "$l" ) != UString::npos )
162 pVlcProc->getTimeVar().addObserver( this );
164 if( m_text.find( "$D" ) != UString::npos ||
165 m_text.find( "$d" ) != UString::npos )
167 pVlcProc->getTimeVar().addObserver( this );
169 if( m_text.find( "$V" ) != UString::npos )
171 pVlcProc->getVolumeVar().addObserver( this );
173 if( m_text.find( "$N" ) != UString::npos )
175 pVlcProc->getStreamNameVar().addObserver( this );
177 if( m_text.find( "$F" ) != UString::npos )
179 pVlcProc->getStreamURIVar().addObserver( this );
181 if( m_text.find( "$B" ) != UString::npos )
183 pVlcProc->getStreamBitRateVar().addObserver( this );
185 if( m_text.find( "$S" ) != UString::npos )
187 pVlcProc->getStreamSampleRateVar().addObserver( this );
195 void VarText::onUpdate( Subject<VarPercent, void*> &rVariable, void *arg )
197 UString newText = get();
198 // If the text has changed, notify the observers
199 if( newText != m_lastText )
201 m_lastText = newText;
207 void VarText::onUpdate( Subject<VarText,void*> &rVariable, void *arg )
209 UString newText = get();
210 // If the text has changed, notify the observers
211 if( newText != m_lastText )
213 m_lastText = newText;
219 void VarText::delObservers()
221 // Stop observing other variables
222 VlcProc *pVlcProc = VlcProc::instance( getIntf() );
223 pVlcProc->getTimeVar().delObserver( this );
224 pVlcProc->getVolumeVar().delObserver( this );
225 pVlcProc->getStreamNameVar().delObserver( this );
226 pVlcProc->getStreamURIVar().delObserver( this );
227 pVlcProc->getStreamBitRateVar().delObserver( this );
228 pVlcProc->getStreamSampleRateVar().delObserver( this );
229 VarManager *pVarManager = VarManager::instance( getIntf() );
230 pVarManager->getHelpText().delObserver( this );