]> git.sesse.net Git - vlc/blob - modules/gui/skins2/utils/var_text.cpp
* skins2/utils/var_text.cpp: avoid an infinite loop when $H is specified in
[vlc] / modules / gui / skins2 / utils / var_text.cpp
1 /*****************************************************************************
2  * var_text.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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 #include "../vars/stream.hpp"
31
32
33 const string VarText::m_type = "text";
34
35
36 VarText::VarText( intf_thread_t *pIntf ): Variable( pIntf ),
37     m_text( pIntf, "" ), m_lastText( pIntf, "" )
38 {
39 }
40
41
42 VarText::~VarText()
43 {
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 );
51 }
52
53
54 const UString VarText::get() const
55 {
56     uint32_t pos;
57     VlcProc *pVlcProc = VlcProc::instance( getIntf() );
58
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,
61     // $V for volume)
62     UString temp( m_text );
63
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
66     // variables is $H...
67     if( (pos = temp.find( "$H" )) != UString::npos )
68     {
69         VarManager *pVarManager = VarManager::instance( getIntf() );
70         // We use .getRaw() to avoid replacing the $H recursively!
71         temp.replace( pos, 2, pVarManager->getHelpText().getRaw() );
72     }
73     while( (pos = temp.find( "$T" )) != UString::npos )
74     {
75         temp.replace( pos, 2,
76                       pVlcProc->getTimeVar().getAsStringCurrTime().c_str() );
77     }
78     while( (pos = temp.find( "$L" )) != UString::npos )
79     {
80         temp.replace( pos, 2,
81                       pVlcProc->getTimeVar().getAsStringTimeLeft().c_str() );
82     }
83     while( (pos = temp.find( "$D" )) != UString::npos )
84     {
85         temp.replace( pos, 2,
86                       pVlcProc->getTimeVar().getAsStringDuration().c_str() );
87     }
88     while( (pos = temp.find( "$V" )) != UString::npos )
89     {
90         temp.replace( pos, 2,
91                       pVlcProc->getVolumeVar().getAsStringPercent().c_str() );
92     }
93     while( (pos = temp.find( "$N" )) != UString::npos )
94     {
95         temp.replace( pos, 2,
96                       pVlcProc->getStreamVar().getAsStringName().c_str() );
97     }
98     while( (pos = temp.find( "$F" )) != UString::npos )
99     {
100         temp.replace( pos, 2,
101                       pVlcProc->getStreamVar().getAsStringFullName().c_str() );
102     }
103
104     return temp;
105 }
106
107
108 void VarText::set( const UString &rText )
109 {
110     // Avoid an infinite loop
111     if( rText == m_text )
112     {
113         return;
114     }
115
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 );
123
124     m_text = rText;
125
126     // Observe needed variables
127     if( m_text.find( "$H" ) != UString::npos )
128     {
129         pVarManager->getHelpText().addObserver( this );
130     }
131     if( m_text.find( "$T" ) != UString::npos )
132     {
133         pVlcProc->getTimeVar().addObserver( this );
134     }
135     if( m_text.find( "$L" ) != UString::npos )
136     {
137         pVlcProc->getTimeVar().addObserver( this );
138     }
139     if( m_text.find( "$D" ) != UString::npos )
140     {
141         pVlcProc->getTimeVar().addObserver( this );
142     }
143     if( m_text.find( "$V" ) != UString::npos )
144     {
145         pVlcProc->getVolumeVar().addObserver( this );
146     }
147     if( m_text.find( "$N" ) != UString::npos )
148     {
149         pVlcProc->getStreamVar().addObserver( this );
150     }
151     if( m_text.find( "$F" ) != UString::npos )
152     {
153         pVlcProc->getStreamVar().addObserver( this );
154     }
155
156     notify();
157 }
158
159
160 void VarText::onUpdate( Subject<VarPercent> &rVariable )
161 {
162     UString newText = get();
163     // If the text has changed, notify the observers
164     if( newText != m_lastText )
165     {
166         m_lastText = newText;
167         notify();
168     }
169 }
170
171
172 void VarText::onUpdate( Subject<VarText> &rVariable )
173 {
174     UString newText = get();
175     // If the text has changed, notify the observers
176     if( newText != m_lastText )
177     {
178         m_lastText = newText;
179         notify();
180     }
181 }