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
46 VlcProc *pVlcProc = VlcProc::instance( getIntf() );
47 pVlcProc->getTimeVar().delObserver( this );
48 pVlcProc->getVolumeVar().delObserver( this );
49 pVlcProc->getStreamURIVar().delObserver( this );
50 pVlcProc->getStreamNameVar().delObserver( this );
51 pVlcProc->getStreamBitRateVar().delObserver( this );
52 VarManager *pVarManager = VarManager::instance( getIntf() );
53 pVarManager->getHelpText().delObserver( this );
58 const UString VarText::get() const
62 // Do not substitute "$X" variables
67 VlcProc *pVlcProc = VlcProc::instance( getIntf() );
69 // Fill a temporary UString object, and replace the escape characters
70 // ($H for help, $T for current time, $L for time left, $D for duration,
72 UString temp( m_text );
74 // $H is processed first, in case the help string contains other variables
75 // to replace. And it is replaced only once, in case one of these other
77 if( (pos = temp.find( "$H" )) != UString::npos )
79 VarManager *pVarManager = VarManager::instance( getIntf() );
80 temp.replace( pos, 2, pVarManager->getHelpText().get() );
82 while( (pos = temp.find( "$T" )) != UString::npos )
85 pVlcProc->getTimeVar().getAsStringCurrTime().c_str() );
87 while( (pos = temp.find( "$t" )) != UString::npos )
90 pVlcProc->getTimeVar().getAsStringCurrTime(true).c_str() );
92 while( (pos = temp.find( "$L" )) != UString::npos )
95 pVlcProc->getTimeVar().getAsStringTimeLeft().c_str() );
97 while( (pos = temp.find( "$l" )) != UString::npos )
100 pVlcProc->getTimeVar().getAsStringTimeLeft(true).c_str() );
102 while( (pos = temp.find( "$D" )) != UString::npos )
104 temp.replace( pos, 2,
105 pVlcProc->getTimeVar().getAsStringDuration().c_str() );
107 while( (pos = temp.find( "$d" )) != UString::npos )
109 temp.replace( pos, 2,
110 pVlcProc->getTimeVar().getAsStringDuration(true).c_str() );
112 while( (pos = temp.find( "$V" )) != UString::npos )
114 temp.replace( pos, 2,
115 pVlcProc->getVolumeVar().getAsStringPercent().c_str() );
117 while( (pos = temp.find( "$N" )) != UString::npos )
119 temp.replace( pos, 2, pVlcProc->getStreamNameVar().get() );
121 while( (pos = temp.find( "$F" )) != UString::npos )
123 temp.replace( pos, 2, pVlcProc->getStreamURIVar().get() );
125 while( (pos = temp.find( "$B" )) != UString::npos )
127 temp.replace( pos, 2, pVlcProc->getStreamBitRateVar().get() );
134 void VarText::set( const UString &rText )
136 // Avoid an infinite loop
137 if( rText == m_text )
146 // Stop observing other variables
147 VlcProc *pVlcProc = VlcProc::instance( getIntf() );
148 pVlcProc->getTimeVar().delObserver( this );
149 pVlcProc->getVolumeVar().delObserver( this );
150 pVlcProc->getStreamNameVar().delObserver( this );
151 pVlcProc->getStreamURIVar().delObserver( this );
152 pVlcProc->getStreamBitRateVar().delObserver( this );
153 VarManager *pVarManager = VarManager::instance( getIntf() );
154 pVarManager->getHelpText().delObserver( this );
156 // Observe needed variables
157 if( m_text.find( "$H" ) != UString::npos )
159 pVarManager->getHelpText().addObserver( this );
161 if( m_text.find( "$T" ) != UString::npos ||
162 m_text.find( "$t" ) != UString::npos )
164 pVlcProc->getTimeVar().addObserver( this );
166 if( m_text.find( "$L" ) != UString::npos ||
167 m_text.find( "$l" ) != UString::npos )
169 pVlcProc->getTimeVar().addObserver( this );
171 if( m_text.find( "$D" ) != UString::npos ||
172 m_text.find( "$d" ) != UString::npos )
174 pVlcProc->getTimeVar().addObserver( this );
176 if( m_text.find( "$V" ) != UString::npos )
178 pVlcProc->getVolumeVar().addObserver( this );
180 if( m_text.find( "$N" ) != UString::npos )
182 pVlcProc->getStreamNameVar().addObserver( this );
184 if( m_text.find( "$F" ) != UString::npos )
186 pVlcProc->getStreamURIVar().addObserver( this );
188 if( m_text.find( "$B" ) != UString::npos )
190 pVlcProc->getStreamBitRateVar().addObserver( this );
198 void VarText::onUpdate( Subject<VarPercent, void*> &rVariable, void *arg )
200 UString newText = get();
201 // If the text has changed, notify the observers
202 if( newText != m_lastText )
204 m_lastText = newText;
210 void VarText::onUpdate( Subject<VarText,void*> &rVariable, void *arg )
212 UString newText = get();
213 // If the text has changed, notify the observers
214 if( newText != m_lastText )
216 m_lastText = newText;