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