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 // $H is processed first, in case the help string contains other variables
65 // to replace. And it is replaced only once, in case one of these other
67 if( (pos = temp.find( "$H" )) != UString::npos )
69 VarManager *pVarManager = VarManager::instance( getIntf() );
70 // We use .getRaw() to avoid replacing the $H recursively!
71 temp.replace( pos, 2, pVarManager->getHelpText().getRaw() );
73 while( (pos = temp.find( "$T" )) != UString::npos )
76 pVlcProc->getTimeVar().getAsStringCurrTime().c_str() );
78 while( (pos = temp.find( "$L" )) != UString::npos )
81 pVlcProc->getTimeVar().getAsStringTimeLeft().c_str() );
83 while( (pos = temp.find( "$D" )) != UString::npos )
86 pVlcProc->getTimeVar().getAsStringDuration().c_str() );
88 while( (pos = temp.find( "$V" )) != UString::npos )
91 pVlcProc->getVolumeVar().getAsStringPercent().c_str() );
93 while( (pos = temp.find( "$N" )) != UString::npos )
96 pVlcProc->getStreamVar().getAsStringName().c_str() );
98 while( (pos = temp.find( "$F" )) != UString::npos )
100 temp.replace( pos, 2,
101 pVlcProc->getStreamVar().getAsStringFullName().c_str() );
108 void VarText::set( const UString &rText )
110 // Avoid an infinite loop
111 if( rText == m_text )
116 // Stop observing other variables
117 VlcProc *pVlcProc = VlcProc::instance( getIntf() );
118 pVlcProc->getTimeVar().delObserver( this );
119 pVlcProc->getVolumeVar().delObserver( this );
120 pVlcProc->getStreamVar().delObserver( this );
121 VarManager *pVarManager = VarManager::instance( getIntf() );
122 pVarManager->getHelpText().delObserver( this );
126 // Observe needed variables
127 if( m_text.find( "$H" ) != UString::npos )
129 pVarManager->getHelpText().addObserver( this );
131 if( m_text.find( "$T" ) != UString::npos )
133 pVlcProc->getTimeVar().addObserver( this );
135 if( m_text.find( "$L" ) != UString::npos )
137 pVlcProc->getTimeVar().addObserver( this );
139 if( m_text.find( "$D" ) != UString::npos )
141 pVlcProc->getTimeVar().addObserver( this );
143 if( m_text.find( "$V" ) != UString::npos )
145 pVlcProc->getVolumeVar().addObserver( this );
147 if( m_text.find( "$N" ) != UString::npos )
149 pVlcProc->getStreamVar().addObserver( this );
151 if( m_text.find( "$F" ) != UString::npos )
153 pVlcProc->getStreamVar().addObserver( this );
160 void VarText::onUpdate( Subject<VarPercent> &rVariable )
162 UString newText = get();
163 // If the text has changed, notify the observers
164 if( newText != m_lastText )
166 m_lastText = newText;
172 void VarText::onUpdate( Subject<VarText> &rVariable )
174 UString newText = get();
175 // If the text has changed, notify the observers
176 if( newText != m_lastText )
178 m_lastText = newText;