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., 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"
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 VarManager *pVarManager = VarManager::instance( getIntf() );
52 pVarManager->getHelpText().delObserver( this );
57 const UString VarText::get() const
61 // Do not substitute "$X" variables
66 VlcProc *pVlcProc = VlcProc::instance( getIntf() );
68 // Fill a temporary UString object, and replace the escape characters
69 // ($H for help, $T for current time, $L for time left, $D for duration,
71 UString temp( m_text );
73 // $H is processed first, in case the help string contains other variables
74 // to replace. And it is replaced only once, in case one of these other
76 if( (pos = temp.find( "$H" )) != UString::npos )
78 VarManager *pVarManager = VarManager::instance( getIntf() );
79 temp.replace( pos, 2, pVarManager->getHelpText().get() );
81 while( (pos = temp.find( "$T" )) != UString::npos )
84 pVlcProc->getTimeVar().getAsStringCurrTime().c_str() );
86 while( (pos = temp.find( "$t" )) != UString::npos )
89 pVlcProc->getTimeVar().getAsStringCurrTime(true).c_str() );
91 while( (pos = temp.find( "$L" )) != UString::npos )
94 pVlcProc->getTimeVar().getAsStringTimeLeft().c_str() );
96 while( (pos = temp.find( "$l" )) != UString::npos )
99 pVlcProc->getTimeVar().getAsStringTimeLeft(true).c_str() );
101 while( (pos = temp.find( "$D" )) != UString::npos )
103 temp.replace( pos, 2,
104 pVlcProc->getTimeVar().getAsStringDuration().c_str() );
106 while( (pos = temp.find( "$d" )) != UString::npos )
108 temp.replace( pos, 2,
109 pVlcProc->getTimeVar().getAsStringDuration(true).c_str() );
111 while( (pos = temp.find( "$V" )) != UString::npos )
113 temp.replace( pos, 2,
114 pVlcProc->getVolumeVar().getAsStringPercent().c_str() );
116 while( (pos = temp.find( "$N" )) != UString::npos )
118 temp.replace( pos, 2, pVlcProc->getStreamNameVar().get() );
120 while( (pos = temp.find( "$F" )) != UString::npos )
122 temp.replace( pos, 2, pVlcProc->getStreamURIVar().get() );
129 void VarText::set( const UString &rText )
131 // Avoid an infinite loop
132 if( rText == m_text )
141 // Stop observing other variables
142 VlcProc *pVlcProc = VlcProc::instance( getIntf() );
143 pVlcProc->getTimeVar().delObserver( this );
144 pVlcProc->getVolumeVar().delObserver( this );
145 pVlcProc->getStreamNameVar().delObserver( this );
146 pVlcProc->getStreamURIVar().delObserver( this );
147 VarManager *pVarManager = VarManager::instance( getIntf() );
148 pVarManager->getHelpText().delObserver( this );
150 // Observe needed variables
151 if( m_text.find( "$H" ) != UString::npos )
153 pVarManager->getHelpText().addObserver( this );
155 if( m_text.find( "$T" ) != UString::npos ||
156 m_text.find( "$t" ) != UString::npos )
158 pVlcProc->getTimeVar().addObserver( this );
160 if( m_text.find( "$L" ) != UString::npos ||
161 m_text.find( "$l" ) != UString::npos )
163 pVlcProc->getTimeVar().addObserver( this );
165 if( m_text.find( "$D" ) != UString::npos ||
166 m_text.find( "$d" ) != UString::npos )
168 pVlcProc->getTimeVar().addObserver( this );
170 if( m_text.find( "$V" ) != UString::npos )
172 pVlcProc->getVolumeVar().addObserver( this );
174 if( m_text.find( "$N" ) != UString::npos )
176 pVlcProc->getStreamNameVar().addObserver( this );
178 if( m_text.find( "$F" ) != UString::npos )
180 pVlcProc->getStreamURIVar().addObserver( this );
188 void VarText::onUpdate( Subject<VarPercent> &rVariable )
190 UString newText = get();
191 // If the text has changed, notify the observers
192 if( newText != m_lastText )
194 m_lastText = newText;
200 void VarText::onUpdate( Subject<VarText> &rVariable )
202 UString newText = get();
203 // If the text has changed, notify the observers
204 if( newText != m_lastText )
206 m_lastText = newText;