]> git.sesse.net Git - vlc/blob - modules/gui/skins2/utils/var_text.cpp
* all: added a new skin text variable "$S" to get the audio sample rate
[vlc] / modules / gui / skins2 / utils / var_text.cpp
1 /*****************************************************************************
2  * var_text.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
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
31
32 const string VarText::m_type = "text";
33
34
35 VarText::VarText( intf_thread_t *pIntf, bool substVars ): Variable( pIntf ),
36     m_text( pIntf, "" ), m_lastText( pIntf, "" ), m_substVars( substVars )
37 {
38 }
39
40
41 VarText::~VarText()
42 {
43     if( m_substVars )
44     {
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         pVlcProc->getStreamSampleRateVar().delObserver( this );
53         VarManager *pVarManager = VarManager::instance( getIntf() );
54         pVarManager->getHelpText().delObserver( this );
55     }
56 }
57
58
59 const UString VarText::get() const
60 {
61     if( !m_substVars )
62     {
63         // Do not substitute "$X" variables
64         return m_text;
65     }
66
67     uint32_t pos;
68     VlcProc *pVlcProc = VlcProc::instance( getIntf() );
69
70     // Fill a temporary UString object, and replace the escape characters
71     // ($H for help, $T for current time, $L for time left, $D for duration,
72     // $V for volume)
73     UString temp( m_text );
74
75     // $H is processed first, in case the help string contains other variables
76     // to replace. And it is replaced only once, in case one of these other
77     // variables is $H...
78     if( (pos = temp.find( "$H" )) != UString::npos )
79     {
80         VarManager *pVarManager = VarManager::instance( getIntf() );
81         temp.replace( pos, 2, pVarManager->getHelpText().get() );
82     }
83     while( (pos = temp.find( "$T" )) != UString::npos )
84     {
85         temp.replace( pos, 2,
86                       pVlcProc->getTimeVar().getAsStringCurrTime().c_str() );
87     }
88     while( (pos = temp.find( "$t" )) != UString::npos )
89     {
90         temp.replace( pos, 2,
91                       pVlcProc->getTimeVar().getAsStringCurrTime(true).c_str() );
92     }
93     while( (pos = temp.find( "$L" )) != UString::npos )
94     {
95         temp.replace( pos, 2,
96                       pVlcProc->getTimeVar().getAsStringTimeLeft().c_str() );
97     }
98     while( (pos = temp.find( "$l" )) != UString::npos )
99     {
100         temp.replace( pos, 2,
101                       pVlcProc->getTimeVar().getAsStringTimeLeft(true).c_str() );
102     }
103     while( (pos = temp.find( "$D" )) != UString::npos )
104     {
105         temp.replace( pos, 2,
106                       pVlcProc->getTimeVar().getAsStringDuration().c_str() );
107     }
108     while( (pos = temp.find( "$d" )) != UString::npos )
109     {
110         temp.replace( pos, 2,
111                       pVlcProc->getTimeVar().getAsStringDuration(true).c_str() );
112     }
113     while( (pos = temp.find( "$V" )) != UString::npos )
114     {
115         temp.replace( pos, 2,
116                       pVlcProc->getVolumeVar().getAsStringPercent().c_str() );
117     }
118     while( (pos = temp.find( "$N" )) != UString::npos )
119     {
120         temp.replace( pos, 2, pVlcProc->getStreamNameVar().get() );
121     }
122     while( (pos = temp.find( "$F" )) != UString::npos )
123     {
124         temp.replace( pos, 2, pVlcProc->getStreamURIVar().get() );
125     }
126     while( (pos = temp.find( "$B" )) != UString::npos )
127     {
128         temp.replace( pos, 2, pVlcProc->getStreamBitRateVar().get() );
129     }
130     while( (pos = temp.find( "$S" )) != UString::npos )
131     {
132         temp.replace( pos, 2, pVlcProc->getStreamSampleRateVar().get() );
133     }
134
135     return temp;
136 }
137
138
139 void VarText::set( const UString &rText )
140 {
141     // Avoid an infinite loop
142     if( rText == m_text )
143     {
144         return;
145     }
146
147     m_text = rText;
148
149     if( m_substVars )
150     {
151         // Stop observing other variables
152         VlcProc *pVlcProc = VlcProc::instance( getIntf() );
153         pVlcProc->getTimeVar().delObserver( this );
154         pVlcProc->getVolumeVar().delObserver( this );
155         pVlcProc->getStreamNameVar().delObserver( this );
156         pVlcProc->getStreamURIVar().delObserver( this );
157         pVlcProc->getStreamBitRateVar().delObserver( this );
158         pVlcProc->getStreamSampleRateVar().delObserver( this );
159         VarManager *pVarManager = VarManager::instance( getIntf() );
160         pVarManager->getHelpText().delObserver( this );
161
162         // Observe needed variables
163         if( m_text.find( "$H" ) != UString::npos )
164         {
165             pVarManager->getHelpText().addObserver( this );
166         }
167         if( m_text.find( "$T" ) != UString::npos ||
168             m_text.find( "$t" ) != UString::npos )
169         {
170             pVlcProc->getTimeVar().addObserver( this );
171         }
172         if( m_text.find( "$L" ) != UString::npos ||
173             m_text.find( "$l" ) != UString::npos )
174         {
175             pVlcProc->getTimeVar().addObserver( this );
176         }
177         if( m_text.find( "$D" ) != UString::npos ||
178             m_text.find( "$d" ) != UString::npos )
179         {
180             pVlcProc->getTimeVar().addObserver( this );
181         }
182         if( m_text.find( "$V" ) != UString::npos )
183         {
184             pVlcProc->getVolumeVar().addObserver( this );
185         }
186         if( m_text.find( "$N" ) != UString::npos )
187         {
188             pVlcProc->getStreamNameVar().addObserver( this );
189         }
190         if( m_text.find( "$F" ) != UString::npos )
191         {
192             pVlcProc->getStreamURIVar().addObserver( this );
193         }
194         if( m_text.find( "$B" ) != UString::npos )
195         {
196             pVlcProc->getStreamBitRateVar().addObserver( this );
197         }
198         if( m_text.find( "$S" ) != UString::npos )
199         {
200             pVlcProc->getStreamSampleRateVar().addObserver( this );
201         }
202     }
203
204     notify();
205 }
206
207
208 void VarText::onUpdate( Subject<VarPercent, void*> &rVariable, void *arg )
209 {
210     UString newText = get();
211     // If the text has changed, notify the observers
212     if( newText != m_lastText )
213     {
214         m_lastText = newText;
215         notify();
216     }
217 }
218
219
220 void VarText::onUpdate( Subject<VarText,void*> &rVariable, void *arg )
221 {
222     UString newText = get();
223     // If the text has changed, notify the observers
224     if( newText != m_lastText )
225     {
226         m_lastText = newText;
227         notify();
228     }
229 }
230