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