]> git.sesse.net Git - vlc/blob - modules/gui/skins2/utils/var_text.cpp
* all: new skin text variable "$B" to get the stream bitrate
[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         VarManager *pVarManager = VarManager::instance( getIntf() );
53         pVarManager->getHelpText().delObserver( this );
54     }
55 }
56
57
58 const UString VarText::get() const
59 {
60     if( !m_substVars )
61     {
62         // Do not substitute "$X" variables
63         return m_text;
64     }
65
66     uint32_t pos;
67     VlcProc *pVlcProc = VlcProc::instance( getIntf() );
68
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,
71     // $V for volume)
72     UString temp( m_text );
73
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
76     // variables is $H...
77     if( (pos = temp.find( "$H" )) != UString::npos )
78     {
79         VarManager *pVarManager = VarManager::instance( getIntf() );
80         temp.replace( pos, 2, pVarManager->getHelpText().get() );
81     }
82     while( (pos = temp.find( "$T" )) != UString::npos )
83     {
84         temp.replace( pos, 2,
85                       pVlcProc->getTimeVar().getAsStringCurrTime().c_str() );
86     }
87     while( (pos = temp.find( "$t" )) != UString::npos )
88     {
89         temp.replace( pos, 2,
90                       pVlcProc->getTimeVar().getAsStringCurrTime(true).c_str() );
91     }
92     while( (pos = temp.find( "$L" )) != UString::npos )
93     {
94         temp.replace( pos, 2,
95                       pVlcProc->getTimeVar().getAsStringTimeLeft().c_str() );
96     }
97     while( (pos = temp.find( "$l" )) != UString::npos )
98     {
99         temp.replace( pos, 2,
100                       pVlcProc->getTimeVar().getAsStringTimeLeft(true).c_str() );
101     }
102     while( (pos = temp.find( "$D" )) != UString::npos )
103     {
104         temp.replace( pos, 2,
105                       pVlcProc->getTimeVar().getAsStringDuration().c_str() );
106     }
107     while( (pos = temp.find( "$d" )) != UString::npos )
108     {
109         temp.replace( pos, 2,
110                       pVlcProc->getTimeVar().getAsStringDuration(true).c_str() );
111     }
112     while( (pos = temp.find( "$V" )) != UString::npos )
113     {
114         temp.replace( pos, 2,
115                       pVlcProc->getVolumeVar().getAsStringPercent().c_str() );
116     }
117     while( (pos = temp.find( "$N" )) != UString::npos )
118     {
119         temp.replace( pos, 2, pVlcProc->getStreamNameVar().get() );
120     }
121     while( (pos = temp.find( "$F" )) != UString::npos )
122     {
123         temp.replace( pos, 2, pVlcProc->getStreamURIVar().get() );
124     }
125     while( (pos = temp.find( "$B" )) != UString::npos )
126     {
127         temp.replace( pos, 2, pVlcProc->getStreamBitRateVar().get() );
128     }
129
130     return temp;
131 }
132
133
134 void VarText::set( const UString &rText )
135 {
136     // Avoid an infinite loop
137     if( rText == m_text )
138     {
139         return;
140     }
141
142     m_text = rText;
143
144     if( m_substVars )
145     {
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 );
155
156         // Observe needed variables
157         if( m_text.find( "$H" ) != UString::npos )
158         {
159             pVarManager->getHelpText().addObserver( this );
160         }
161         if( m_text.find( "$T" ) != UString::npos ||
162             m_text.find( "$t" ) != UString::npos )
163         {
164             pVlcProc->getTimeVar().addObserver( this );
165         }
166         if( m_text.find( "$L" ) != UString::npos ||
167             m_text.find( "$l" ) != UString::npos )
168         {
169             pVlcProc->getTimeVar().addObserver( this );
170         }
171         if( m_text.find( "$D" ) != UString::npos ||
172             m_text.find( "$d" ) != UString::npos )
173         {
174             pVlcProc->getTimeVar().addObserver( this );
175         }
176         if( m_text.find( "$V" ) != UString::npos )
177         {
178             pVlcProc->getVolumeVar().addObserver( this );
179         }
180         if( m_text.find( "$N" ) != UString::npos )
181         {
182             pVlcProc->getStreamNameVar().addObserver( this );
183         }
184         if( m_text.find( "$F" ) != UString::npos )
185         {
186             pVlcProc->getStreamURIVar().addObserver( this );
187         }
188         if( m_text.find( "$B" ) != UString::npos )
189         {
190             pVlcProc->getStreamBitRateVar().addObserver( this );
191         }
192     }
193
194     notify();
195 }
196
197
198 void VarText::onUpdate( Subject<VarPercent, void*> &rVariable, void *arg )
199 {
200     UString newText = get();
201     // If the text has changed, notify the observers
202     if( newText != m_lastText )
203     {
204         m_lastText = newText;
205         notify();
206     }
207 }
208
209
210 void VarText::onUpdate( Subject<VarText,void*> &rVariable, void *arg )
211 {
212     UString newText = get();
213     // If the text has changed, notify the observers
214     if( newText != m_lastText )
215     {
216         m_lastText = newText;
217         notify();
218     }
219 }